Added bluetooth hardware monitoring

This commit is contained in:
Pierre Pronchery 2010-01-27 13:10:07 +00:00
parent 3071a3266f
commit 198877c1f0
5 changed files with 194 additions and 8 deletions

View File

@ -31,6 +31,7 @@ dist:
$(PACKAGE)-$(VERSION)/src/common.h \
$(PACKAGE)-$(VERSION)/src/project.conf \
$(PACKAGE)-$(VERSION)/src/applets/battery.c \
$(PACKAGE)-$(VERSION)/src/applets/bluetooth.c \
$(PACKAGE)-$(VERSION)/src/applets/clock.c \
$(PACKAGE)-$(VERSION)/src/applets/cpu.c \
$(PACKAGE)-$(VERSION)/src/applets/cpufreq.c \

View File

@ -1,4 +1,4 @@
TARGETS = battery.so clock.so cpu.so cpufreq.so desktop.so lock.so logout.so main.so memory.so pager.so systray.so tasks.so volume.so
TARGETS = battery.so bluetooth.so clock.so cpu.so cpufreq.so desktop.so lock.so logout.so main.so memory.so pager.so systray.so tasks.so volume.so
PREFIX = /usr/local
DESTDIR =
LIBDIR = $(PREFIX)/lib
@ -25,6 +25,13 @@ battery_LDFLAGS = $(LDFLAGSF) $(LDFLAGS)
battery.so: $(battery_OBJS)
$(LD) -o battery.so $(battery_OBJS)
bluetooth_OBJS = bluetooth.o
bluetooth_CFLAGS = $(CPPFLAGSF) $(CPPFLAGS) $(CFLAGSF) $(CFLAGS)
bluetooth_LDFLAGS = $(LDFLAGSF) $(LDFLAGS)
bluetooth.so: $(bluetooth_OBJS)
$(LD) -o bluetooth.so $(bluetooth_OBJS)
clock_OBJS = clock.o
clock_CFLAGS = $(CPPFLAGSF) $(CPPFLAGS) $(CFLAGSF) $(CFLAGS)
clock_LDFLAGS = $(LDFLAGSF) $(LDFLAGS)
@ -112,6 +119,9 @@ volume.so: $(volume_OBJS)
battery.o: battery.c ../../include/Panel.h
$(CC) $(battery_CFLAGS) -c battery.c
bluetooth.o: bluetooth.c ../../include/Panel.h
$(CC) $(bluetooth_CFLAGS) -c bluetooth.c
clock.o: clock.c ../../include/Panel.h
$(CC) $(clock_CFLAGS) -c clock.c
@ -149,7 +159,7 @@ volume.o: volume.c ../../include/Panel.h
$(CC) $(volume_CFLAGS) -c volume.c
clean:
$(RM) $(battery_OBJS) $(clock_OBJS) $(cpu_OBJS) $(cpufreq_OBJS) $(desktop_OBJS) $(lock_OBJS) $(logout_OBJS) $(main_OBJS) $(memory_OBJS) $(pager_OBJS) $(systray_OBJS) $(tasks_OBJS) $(volume_OBJS)
$(RM) $(battery_OBJS) $(bluetooth_OBJS) $(clock_OBJS) $(cpu_OBJS) $(cpufreq_OBJS) $(desktop_OBJS) $(lock_OBJS) $(logout_OBJS) $(main_OBJS) $(memory_OBJS) $(pager_OBJS) $(systray_OBJS) $(tasks_OBJS) $(volume_OBJS)
distclean: clean
$(RM) $(TARGETS)
@ -158,6 +168,8 @@ install: all
$(MKDIR) $(DESTDIR)$(LIBDIR)/Panel/applets
$(INSTALL) -m 0644 battery.so $(DESTDIR)$(LIBDIR)/Panel/applets/battery.so
$(MKDIR) $(DESTDIR)$(LIBDIR)/Panel/applets
$(INSTALL) -m 0644 bluetooth.so $(DESTDIR)$(LIBDIR)/Panel/applets/bluetooth.so
$(MKDIR) $(DESTDIR)$(LIBDIR)/Panel/applets
$(INSTALL) -m 0644 clock.so $(DESTDIR)$(LIBDIR)/Panel/applets/clock.so
$(MKDIR) $(DESTDIR)$(LIBDIR)/Panel/applets
$(INSTALL) -m 0644 cpu.so $(DESTDIR)$(LIBDIR)/Panel/applets/cpu.so
@ -184,6 +196,7 @@ install: all
uninstall:
$(RM) $(DESTDIR)$(LIBDIR)/Panel/applets/battery.so
$(RM) $(DESTDIR)$(LIBDIR)/Panel/applets/bluetooth.so
$(RM) $(DESTDIR)$(LIBDIR)/Panel/applets/clock.so
$(RM) $(DESTDIR)$(LIBDIR)/Panel/applets/cpu.so
$(RM) $(DESTDIR)$(LIBDIR)/Panel/applets/cpufreq.so

164
src/applets/bluetooth.c Normal file
View File

@ -0,0 +1,164 @@
/* $Id$ */
/* Copyright (c) 2010 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS Desktop Panel */
/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifdef __NetBSD__
# include <sys/types.h>
# include <sys/ioctl.h>
# include <bluetooth.h>
# include <unistd.h>
# include <stdio.h>
# include <string.h>
# include <errno.h>
#endif
#include <stdlib.h>
#include <System.h>
#include "Panel.h"
/* Bluetooth */
/* private */
/* types */
typedef struct _Bluetooth
{
PanelAppletHelper * helper;
GtkWidget * image;
guint timeout;
#ifdef __NetBSD__
int fd;
#endif
} Bluetooth;
/* prototypes */
static GtkWidget * _bluetooth_init(PanelApplet * applet);
static void _bluetooth_destroy(PanelApplet * applet);
static gboolean _bluetooth_get(Bluetooth * bluetooth);
static void _bluetooth_set(Bluetooth * bluetooth, gboolean on);
/* callbacks */
static gboolean _on_timeout(gpointer data);
/* public */
/* variables */
PanelApplet applet =
{
NULL,
_bluetooth_init,
_bluetooth_destroy,
PANEL_APPLET_POSITION_END,
FALSE,
TRUE,
NULL
};
/* private */
/* functions */
/* bluetooth_init */
static GtkWidget * _bluetooth_init(PanelApplet * applet)
{
Bluetooth * bluetooth;
if((bluetooth = malloc(sizeof(*bluetooth))) == NULL)
return NULL;
applet->priv = bluetooth;
bluetooth->helper = applet->helper;
bluetooth->timeout = 0;
#ifdef __NetBSD__
bluetooth->fd = -1;
#endif
bluetooth->image = gtk_image_new_from_icon_name("network-wireless",
applet->helper->icon_size);
bluetooth->timeout = g_timeout_add(1000, _on_timeout, bluetooth);
_on_timeout(bluetooth);
return bluetooth->image;
}
/* bluetooth_destroy */
static void _bluetooth_destroy(PanelApplet * applet)
{
Bluetooth * bluetooth = applet->priv;
if(bluetooth->timeout > 0)
g_source_remove(bluetooth->timeout);
#ifdef __NetBSD__
if(bluetooth->fd != -1)
close(bluetooth->fd);
#endif
free(bluetooth);
}
/* bluetooth_set */
static void _bluetooth_set(Bluetooth * bluetooth, gboolean on)
{
if(on == TRUE)
gtk_widget_show(bluetooth->image);
else
gtk_widget_hide(bluetooth->image);
}
/* callbacks */
/* on_timeout */
#ifdef __NetBSD__
static gboolean _bluetooth_get(Bluetooth * bluetooth)
{
struct btreq btr;
const char name[] = "ubt0";
if(bluetooth->fd == -1 && (bluetooth->fd = socket(PF_BLUETOOTH,
SOCK_RAW, BTPROTO_HCI)) == -1)
{
error_set("%s: %s", "socket", strerror(errno));
return FALSE;
}
memset(&btr, 0, sizeof(btr));
strncpy(btr.btr_name, name, sizeof(name));
if(ioctl(bluetooth->fd, SIOCGBTINFO, &btr) == -1)
{
error_set("%s: %s", name, strerror(errno));
close(bluetooth->fd);
bluetooth->fd = -1;
return FALSE;
}
/* XXX should not be necessary but EBADF happens once otherwise */
close(bluetooth->fd);
bluetooth->fd = -1;
return TRUE;
}
#else
static gdouble _bluetooth_get(Bluetooth * bluetooth)
{
/* FIXME not supported */
return FALSE;
}
#endif
/* callbacks */
/* on_timeout */
static gboolean _on_timeout(gpointer data)
{
Bluetooth * bluetooth = data;
_bluetooth_set(bluetooth, _bluetooth_get(bluetooth));
return TRUE;
}

View File

@ -1,4 +1,4 @@
targets=battery,clock,cpu,cpufreq,desktop,lock,logout,main,memory,pager,systray,tasks,volume
targets=battery,bluetooth,clock,cpu,cpufreq,desktop,lock,logout,main,memory,pager,systray,tasks,volume
cppflags_force=-I $(PREFIX)/include -I ../../include
#cppflags=-D EMBEDDED
cflags_force=-W `pkg-config --cflags gtk+-2.0` -fPIC
@ -13,6 +13,14 @@ install=$(LIBDIR)/Panel/applets
[battery.c]
depends=../../include/Panel.h
[bluetooth]
type=plugin
sources=bluetooth.c
install=$(LIBDIR)/Panel/applets
[bluetooth.c]
depends=../../include/Panel.h
[clock]
type=plugin
sources=clock.c

View File

@ -150,12 +150,12 @@ static gboolean _on_idle(gpointer data)
Panel * panel = data;
/* FIXME load all plugins, a configuration file or ask the user */
#ifndef EMBEDDED
const char * plugins[] = { "volume", "systray", "battery", "clock",
"memory", "cpufreq", "cpu", "desktop", "lock", "logout", "main",
"pager", "tasks", NULL };
const char * plugins[] = { "volume", "systray", "battery", "bluetooth",
"clock", "memory", "cpufreq", "cpu", "desktop", "lock",
"logout", "main", "pager", "tasks", NULL };
#else
const char * plugins[] = { "volume", "systray", "battery", "clock",
"cpufreq", "desktop", "main", "tasks", NULL };
const char * plugins[] = { "volume", "systray", "battery", "bluetooth",
"clock", "cpufreq", "desktop", "main", "tasks", NULL };
#endif
size_t i;