Add DBus based freedesktop.org Notify support (uh, still a little hacky),

rework to gmainloop for IO watching
This commit is contained in:
Nils Faerber 2011-08-07 13:55:23 +02:00
parent ebb651f825
commit fc1c62b9c0
2 changed files with 205 additions and 68 deletions

View file

@ -1,9 +1,14 @@
# Copyright (C) 2011 Nils Faerber <nils.faerber@kernelconcepts.de>
$(pkg-config --libs --cflags glib-2.0) $(pkg-config --libs --cflags dbus-glib-1) $(pkg-config --libs --cflags dbus-1)
# prefix for installation and search path (like icons)
PREFIX = /usr/local/
CFLAGS = -DDEBUG -Wall -O2 $(CCFLAGS)
CFLAGS = -DDEBUG -Wall -O2 $(CCFLAGS) `pkg-config --cflags glib-2.0` `pkg-config --cflags dbus-glib-1` `pkg-config --cflags dbus-1`
#CFLAGS = -Wall -O2 $(CCFLAGS)
LDFLAGS = `pkg-config --libs glib-2.0` `pkg-config --libs dbus-glib-1` `pkg-config --libs dbus-1`
PRGNAME = metawatch
MEMBERS = metawatch crc16ccitt mw_utility mw_main
@ -13,7 +18,7 @@ MEMBERS = metawatch crc16ccitt mw_utility mw_main
.SUFFIXES: .d .c
CFLAGS += -MD -DPREFIX=\"$(PREFIX)\" $(OPTIONS)
LDFLAGS = $(CLDFLAGS) $(SQLITELDFLAGS)
LDFLAGS += $(CLDFLAGS)
SOURCES = $(patsubst %,%.c,$(MEMBERS))
OBJS = $(patsubst %,%.o,$(MEMBERS))

252
mw_main.c
View file

@ -20,6 +20,11 @@
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
#include <glib.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
/*
#include <bluetooth/sdp.h>
#include <bluetooth/sdp_lib.h>
@ -29,6 +34,15 @@
#include "crc16ccitt.h"
#include "mw_utility.h"
typedef struct {
GMainLoop *mloop;
int mw_fd; /* MetaWatch RFCOMM BT socket */
unsigned char rcvbuf[128];
int rcvbuf_pos;
int con_fd; /* console input fd */
char cmdline[128];
int cmdline_pos;
} mwdata;
void bitmap_test(int mw_fd)
{
@ -217,6 +231,40 @@ void test_notification(int mw_fd)
mw_set_vibrate_mode(mw_fd, 1, 300, 300, 3);
}
void mw_send_notify(int mw_fd, char *header, char *body)
{
mw_buffer *mwbuf;
unsigned char *bbuf;
int len,i,c,r;
char sstr[32];
mwbuf = mw_alloc_pbuffer(96, 96, 1);
mw_buf_clear(mwbuf, MW_BLACK);
mw_buf_print(mwbuf, 0, 0, header, 0, MW_BLACK, MW_WHITE);
i=0;
c=0; r=1;
memset(sstr,0,32);
while (i<strlen(body)) {
sstr[c++] = body[i++];
if (c>=16 || i>=strlen(body)) {
mw_buf_print(mwbuf, 0, r*9, sstr, 0, MW_WHITE, MW_BLACK);
memset(sstr,0,32);
c=0; r++;
if (r>10)
break;
};
};
bbuf = mw_make_mw_buffer(mwbuf, &len);
mw_send_bitmap(mw_fd, MW_SCREEN_MODE_APPLICATION, 96, 96, 0, bbuf, len);
mw_update_display(mw_fd, MW_SCREEN_MODE_APPLICATION, 1);
mw_free_pbuffer(mwbuf);
mw_set_vibrate_mode(mw_fd, 1, 300, 300, 2);
}
void test_application(int mw_fd)
{
mw_buffer *mwbuf;
@ -260,6 +308,7 @@ void process_cmd(char *cmdline, int clinep, int mw_fd)
if (strncmp(cmdline, "quit", 4) == 0) {
close(mw_fd);
exit(0);
// g_main_loop_quit (mdata->mloop);
}
if (strncmp(cmdline, "help", 4) == 0) {
print_help();
@ -330,73 +379,26 @@ void process_cmd(char *cmdline, int clinep, int mw_fd)
}
int menu(int mw_fd)
int feed_menu(mwdata *mdata)
{
fd_set mfds;
struct termios tconfd, tmwfd;
char cmdline[128];
unsigned char msg_buf[64];
unsigned char clinep = 0;
int rcvd;
tcgetattr(0, &tconfd);
cfmakeraw(&tconfd);
tconfd.c_oflag |= ONLCR | OPOST;
tconfd.c_lflag |= ISIG;
tcsetattr(0, TCSANOW, &tconfd);
tcgetattr(mw_fd, &tmwfd);
cfmakeraw(&tmwfd);
tmwfd.c_oflag |= ONLCR | OPOST;
tmwfd.c_lflag |= ISIG;
tcsetattr(mw_fd, TCSANOW, &tmwfd);
FD_ZERO(&mfds);
FD_SET(0, &mfds);
FD_SET(mw_fd, &mfds);
memset(cmdline, 0, 128);
do {
rcvd = 0;
if (select(mw_fd+1, &mfds, NULL, NULL, NULL) > 0) {
if (FD_ISSET(mw_fd, &mfds)) {
rcvd = read(mw_fd, msg_buf, 64);
#ifdef DEBUG
fprintf(stderr, "read %d bytes:\n", rcvd);
#endif
rcvd = read(0, (mdata->cmdline+mdata->cmdline_pos), 1);
if (rcvd > 0) {
#ifdef DEBUG
dump_frame(msg_buf, rcvd);
#endif
decode_frame(mw_fd, msg_buf, rcvd);
}
};
if (FD_ISSET(0, &mfds)) {
rcvd = read(0, (cmdline+clinep), 1);
if (rcvd > 0) {
if (cmdline[clinep] == '\r') {
if (mdata->cmdline[mdata->cmdline_pos] == '\r') {
printf("\n");
cmdline[clinep--] = '\0';
process_cmd(cmdline, clinep, mw_fd);
clinep = 0;
memset(cmdline, 0, 128);
mdata->cmdline[mdata->cmdline_pos--] = '\0';
process_cmd(mdata->cmdline, mdata->cmdline_pos, mdata->mw_fd);
mdata->cmdline_pos = 0;
memset(mdata->cmdline, 0, 128);
} else {
clinep++;
if (clinep > 75)
clinep = 75;
printf("\r> %s", cmdline);
mdata->cmdline_pos++;
if (mdata->cmdline_pos > 75)
mdata->cmdline_pos = 75;
printf("\r> %s", mdata->cmdline);
fflush(stdout);
}
}
};
} else
break;
FD_ZERO(&mfds);
FD_SET(0, &mfds);
FD_SET(mw_fd, &mfds);
} while (rcvd > 0);
return 0;
}
@ -471,6 +473,7 @@ void baswap(bdaddr_t *dst, const bdaddr_t *src)
for (i = 0; i < 6; i++)
d[i] = s[5-i];
}
int bachk(const char *str)
{
if (!str)
@ -514,16 +517,99 @@ int str2ba(const char *str, bdaddr_t *ba)
return 0;
}
gboolean handle_mw_io(GIOChannel *mw_io, GIOCondition condition, gpointer udata)
{
mwdata *mdata = (mwdata *)udata;
int rcvd;
rcvd = read(mdata->mw_fd, mdata->rcvbuf+mdata->rcvbuf_pos, 64);
#ifdef DEBUG
fprintf(stderr, "read %d bytes:\n", rcvd);
#endif
if (rcvd > 0) {
#ifdef DEBUG
dump_frame(mdata->rcvbuf, rcvd);
#endif
decode_frame(mdata->mw_fd, mdata->rcvbuf, rcvd);
mdata->rcvbuf_pos = 0;
}
return TRUE;
}
gboolean handle_min_io(GIOChannel *mw_io, GIOCondition condition, gpointer udata)
{
mwdata *mdata = (mwdata *)udata;
feed_menu(mdata);
return TRUE;
}
static DBusHandlerResult
signal_filter (DBusConnection *connection, DBusMessage *message, void *user_data)
{
//GMainLoop *loop = user_data;
mwdata *mdata = (mwdata *)user_data;
DBusError error;
char *app_name, *app_icon, *summary, *body; // **actions;
int replace_id; // dict, expire;
/* A signal from the bus saying we are about to be disconnected */
if (dbus_message_is_signal(message, "org.freedesktop.Local", "Disconnected")) {
/* Tell the main loop to quit */
g_main_loop_quit (mdata->mloop);
/* We have handled this message, don't pass it on */
return DBUS_HANDLER_RESULT_HANDLED;
}
if (dbus_message_is_method_call (message,"org.freedesktop.Notifications", "Notify")) {
g_print("method call\n");
dbus_error_init (&error);
if (dbus_message_get_args(message, &error,
DBUS_TYPE_STRING, &app_name,
DBUS_TYPE_UINT32, &replace_id,
DBUS_TYPE_STRING, &app_icon,
DBUS_TYPE_STRING, &summary,
DBUS_TYPE_STRING, &body,
/* G_TYPE_STRV, &actions,
DBUS_TYPE_DICT_ENTRY_AS_STRING, &dict,
DBUS_TYPE_INT32, &expire, */
DBUS_TYPE_INVALID)) {
g_print("Notify received: from app %s, icon %s:\nSummary: %s\nBody: %s\n", app_name, app_icon, summary, body);
mw_send_notify(mdata->mw_fd, summary, body);
/* we just listen, we do not handle it here */
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
} else {
g_print("Notify received, but error getting message: %s\n", error.message);
dbus_error_free (&error);
}
} else {
g_print("Not signal received\n");
return DBUS_HANDLER_RESULT_HANDLED;
};
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
int main(int argc, char **argv)
{
int mw_fd;
mwdata mdata;
struct termios tconfd, otconfd, tmwfd;
bdaddr_t btaddr;
GIOChannel *mw_io, *m_in;
DBusConnection *bus;
DBusError error;
if (argc != 2) {
fprintf(stderr, "Usage:\n\t%s <devicename>\n", argv[0]);
return 1;
};
g_type_init();
mdata.mloop = g_main_loop_new (NULL, FALSE);
crc16ccitt_init();
#if USE_RFCOMM_FILE
@ -542,13 +628,59 @@ int main(int argc, char **argv)
fprintf(stderr, "connected to %s\n", argv[1]);
};
#endif
/* we have a connection, RFCOMM socket is on mw_fd */
/* make the tty raw */
tcgetattr(mw_fd, &tmwfd);
cfmakeraw(&tmwfd);
tmwfd.c_oflag |= ONLCR | OPOST;
tmwfd.c_lflag |= ISIG;
tcsetattr(mw_fd, TCSANOW, &tmwfd);
menu(mw_fd);
mdata.mw_fd = mw_fd;
mdata.rcvbuf_pos = 0;
memset(mdata.rcvbuf, 0, 128);
dbus_error_init (&error);
bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
if (!bus) {
g_warning ("Failed to connect to the D-BUS daemon: %s", error.message);
dbus_error_free (&error);
return 1;
}
dbus_connection_setup_with_g_main (bus, NULL);
dbus_bus_add_match (bus, "interface='org.freedesktop.Notifications'", &error);
dbus_connection_add_filter (bus, signal_filter, &mdata, NULL);
// menu(mw_fd);
mw_io = g_io_channel_unix_new(mw_fd);
g_io_add_watch(mw_io, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, handle_mw_io, &mdata);
tcgetattr(0, &tconfd);
tcgetattr(0, &otconfd);
cfmakeraw(&tconfd);
tconfd.c_oflag |= ONLCR | OPOST;
tconfd.c_lflag |= ISIG;
tcsetattr(0, TCSANOW, &tconfd);
mdata.con_fd = 0;
m_in = g_io_channel_unix_new(0); /* stdin for kbd */
g_io_add_watch(m_in, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, handle_min_io, &mdata);
memset(mdata.cmdline, 0, 128);
mdata.cmdline_pos = 0;
g_main_loop_run (mdata.mloop);
fsync(mw_fd);
close(mw_fd);
/* reset the controlling tty again */
tcsetattr(0, TCSANOW, &otconfd);
return 0;
};