]> git.kernelconcepts.de Git - metawatch.git/blobdiff - mw_main.c
Add GTK test client, rework API with callbacks, make library
[metawatch.git] / mw_main.c
index 7a0831f06866ce9a4f7a3a02a1f12ea95fc341ef..4e871b43d8617af5682c5cd8ded56d8840ba3050 100644 (file)
--- a/mw_main.c
+++ b/mw_main.c
 #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>
 #include "crc16ccitt.h"
 #include "mw_utility.h"
 
+typedef struct {
+       GMainLoop *mloop;
+       mwdevice_t mwdevice;
+       unsigned char rcvbuf[128];
+       int rcvbuf_pos;
+       int con_fd;             /* console input fd */
+       char cmdline[128];
+       int cmdline_pos;
+} mwdata_t;
 
-void bitmap_test(int mw_fd)
+void bitmap_test(mwdevice_t *mwdevice)
 {
        /* a nice checker-board pattern */
        unsigned char checkbuf[24] = {
@@ -39,15 +53,15 @@ void bitmap_test(int mw_fd)
                0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
                0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
        };
-       mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 31, checkbuf, 24);
-       mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 33, checkbuf, 24);
-       mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 35, checkbuf, 24);
-       mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 37, checkbuf, 24);
+       mw_write_buffer(mwdevice, MW_SCREEN_MODE_IDLE, 0, 31, checkbuf, 24);
+       mw_write_buffer(mwdevice, MW_SCREEN_MODE_IDLE, 0, 33, checkbuf, 24);
+       mw_write_buffer(mwdevice, MW_SCREEN_MODE_IDLE, 0, 35, checkbuf, 24);
+       mw_write_buffer(mwdevice, MW_SCREEN_MODE_IDLE, 0, 37, checkbuf, 24);
 
-       mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
+       mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
 }
 
-void bitmap_test2(int mw_fd)
+void bitmap_test2(mwdevice_t *mwdevice)
 {
        mw_buffer *mwbuf1, *mwbuf2;
        unsigned char *bbuf;
@@ -64,8 +78,8 @@ void bitmap_test2(int mw_fd)
                mw_buf_draw_pixel(mwbuf1, x, x, MW_WHITE);
                mw_dump_mw_buffer(mwbuf1);
                bbuf = mw_make_mw_buffer(mwbuf1, &len);
-               mw_send_bitmap(mw_fd, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
-               mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
+               mw_send_bitmap(mwdevice, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
+               mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
                getchar();
        };
 
@@ -73,7 +87,7 @@ void bitmap_test2(int mw_fd)
        mw_free_pbuffer(mwbuf2);
 }
 
-void text_test(int mw_fd)
+void text_test(mwdevice_t *mwdevice)
 {
        mw_buffer *mwbuf;
        unsigned char *bbuf;
@@ -88,12 +102,12 @@ void text_test(int mw_fd)
        mw_buf_print(mwbuf, 1, 45, "Big Fat Clipping", 2, MW_BLACK, MW_WHITE);
 
        bbuf = mw_make_mw_buffer(mwbuf, &len);
-       mw_send_bitmap(mw_fd, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
-       mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
+       mw_send_bitmap(mwdevice, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
+       mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
        mw_free_pbuffer(mwbuf);
 }
 
-void line_test(int mw_fd)
+void line_test(mwdevice_t *mwdevice)
 {
        mw_buffer *mwbuf;
        unsigned char *bbuf;
@@ -106,12 +120,12 @@ void line_test(int mw_fd)
                mw_buf_draw_line_bresenham(mwbuf, p, 0, 95-p, 65, MW_WHITE);
 
        bbuf = mw_make_mw_buffer(mwbuf, &len);
-       mw_send_bitmap(mw_fd, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
-       mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
+       mw_send_bitmap(mwdevice, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
+       mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
        mw_free_pbuffer(mwbuf);
 }
 
-void bitmap_read(int mw_fd)
+void bitmap_read(mwdevice_t *mwdevice)
 {
        int ffd, ret;
        char rbuf[256];
@@ -172,150 +186,256 @@ void bitmap_read(int mw_fd)
        /* reverse bits and invert the bmap */
        bmap_buffer_flipinvert(1, 1, bmapbuf, rowlength * height);
        /* send the buffer to the watch */
-       mw_send_bitmap(mw_fd, MW_SCREEN_MODE_IDLE, width, height, 31, bmapbuf, rowlength * height);
+       mw_send_bitmap(mwdevice, MW_SCREEN_MODE_IDLE, width, height, 31, bmapbuf, rowlength * height);
        /* update the display */
-       mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
+       mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
 
        free(bmapbuf);
 }
 
+void test_notification(mwdevice_t *mwdevice)
+{
+       mw_buffer *mwbuf;
+       unsigned char *bbuf;
+       int len;
+
+       mwbuf = mw_alloc_pbuffer(96, 96, 1);
+       mw_buf_clear(mwbuf, MW_BLACK);
+
+       mw_buf_draw_line_bresenham(mwbuf, 2, 0, 93, 0, MW_WHITE);
+       mw_buf_draw_line_bresenham(mwbuf, 0, 1, 95, 1, MW_WHITE);
+       mw_buf_draw_line_bresenham(mwbuf, 0, 94, 95, 94, MW_WHITE);
+       mw_buf_draw_line_bresenham(mwbuf, 2, 95, 93, 95, MW_WHITE);
+
+       mw_buf_draw_line_bresenham(mwbuf, 0, 2, 0, 93, MW_WHITE);
+       mw_buf_draw_line_bresenham(mwbuf, 1, 0, 1, 95, MW_WHITE);
+       mw_buf_draw_line_bresenham(mwbuf, 95, 2, 95, 93, MW_WHITE);
+       mw_buf_draw_line_bresenham(mwbuf, 94, 0, 94, 95, MW_WHITE);
+
+       mw_buf_print(mwbuf, 4,  4, "012345678901234", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 4, 13, "012345678901234", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 4, 22, "0123456789g1234", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 4, 31, "012345678901234", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 4, 40, "012345678901234", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 4, 49, "012345678901234", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 4, 58, "0123456789g1234", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 4, 67, "012345678901234", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 4, 76, "012345678901234", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 4, 85, "012345678901234", 0, MW_WHITE, MW_BLACK);
+
+       bbuf = mw_make_mw_buffer(mwbuf, &len);
+       mw_send_bitmap(mwdevice, MW_SCREEN_MODE_NOTIFICATION, 96, 96, 0, bbuf, len);
+       mw_update_display(mwdevice, MW_SCREEN_MODE_NOTIFICATION, 1);
+       mw_free_pbuffer(mwbuf);
+
+       mw_set_vibrate_mode(mwdevice, 1, 300, 300, 3);
+}
+
+void mw_send_notify(mwdevice_t *mwdevice, 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(mwdevice, MW_SCREEN_MODE_APPLICATION, 96, 96, 0, bbuf, len);
+       mw_update_display(mwdevice, MW_SCREEN_MODE_APPLICATION, 1);
+       mw_free_pbuffer(mwbuf);
+
+       mw_set_vibrate_mode(mwdevice, 1, 300, 300, 2);
+}
+
+void test_application(mwdevice_t *mwdevice)
+{
+       mw_buffer *mwbuf;
+       unsigned char *bbuf;
+       int len;
+
+       mw_enable_button(mwdevice, MW_SCREEN_MODE_APPLICATION, MW_BUTTON_A, MW_BUTTON_IMMEDIATE, MW_BUTTON_EVENT_MESSAGE, MW_BUTTON_A);
+       mw_configure_watch_mode(mwdevice, MW_SCREEN_MODE_APPLICATION, 0, 30, 0);
+
+       mwbuf = mw_alloc_pbuffer(96, 96, 1);
+       mw_buf_clear(mwbuf, MW_BLACK);
+
+       mw_buf_print(mwbuf, 0,  0, "  Application   ", 0, MW_BLACK, MW_WHITE);
+       mw_buf_print(mwbuf, 0,  9, "0123456789012345", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 0, 18, "0123456789g12345", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 0, 27, "0123456789012345", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 0, 36, "0123456789012345", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 0, 45, "0123456789012345", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 0, 54, "0123456789g12345", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 0, 63, "0123456789012345", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 0, 72, "0123456789012345", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 0, 81, "0123456789012345", 0, MW_WHITE, MW_BLACK);
+
+       bbuf = mw_make_mw_buffer(mwbuf, &len);
+       mw_send_bitmap(mwdevice, MW_SCREEN_MODE_APPLICATION, 96, 96, 0, bbuf, len);
+       mw_update_display(mwdevice, MW_SCREEN_MODE_APPLICATION, 1);
+       mw_free_pbuffer(mwbuf);
+}
+
+void test_oled(mwdevice_t *mwdevice, unsigned char oled)
+{
+       mw_buffer *mwbuf;
+       unsigned char *bbuf;
+       int len, i;
+
+       mwbuf = mw_alloc_pbuffer(80, 16, 1);
+       mw_buf_clear(mwbuf, MW_BLACK);
+
+       mw_buf_print(mwbuf, 0,  0, "Application", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 0,  8, "012345678901234", 0, MW_WHITE, MW_BLACK);
+
+       bbuf = mw_make_mw_oled_buffer(mwbuf, &len);
+       mw_write_oled_buffer(mwdevice, 0, oled, 80, 0, bbuf, len);
+
+       mw_free_pbuffer(mwbuf);
+}
+
 void print_help(void)
 {
 }
 
-void process_cmd(char *cmdline, int clinep, int mw_fd)
+void process_cmd(char *cmdline, int clinep, mwdata_t *mwdata)
 {
        unsigned char mdata[32];
+       unsigned int intopt;
 
        fprintf(stderr, "command: '%s'\n", cmdline);
 
        if (strncmp(cmdline, "quit", 4) == 0) {
-               close(mw_fd);
-               exit(0);
+               //close(mw_fd);
+               //exit(0);
+               g_main_loop_quit (mwdata->mloop);
        }
        if (strncmp(cmdline, "help", 4) == 0) {
                print_help();
        }
+       if (strncmp(cmdline, "ahand", 5) == 0) {
+               intopt = atoi(cmdline+5);
+               g_print("Advance analog hands by %d minutes\n", intopt);
+               mdata[0] = intopt / 60;
+               mdata[1] = intopt % 60;
+               mdata[2] = 0;
+               mw_send_frame(&mwdata->mwdevice, MW_ADVANCE_WATCH_HANDS, 0, mdata, 3);
+       }
        if (strncmp(cmdline, "srtc", 4) == 0) {
                fprintf(stderr, "Setting RTC from system time...");
-               mw_set_rtc(mw_fd, MW_RTC_CLOCK_24HR, MW_RTC_DATE_DDMM);
+               mw_set_rtc(&mwdata->mwdevice, MW_RTC_CLOCK_24HR, MW_RTC_DATE_DDMM);
                fprintf(stderr, "OK\n");
        }
        if (strncmp(cmdline, "grtc", 4) == 0) {
-               mw_send_frame(mw_fd, MW_GET_REAL_TIME_CLOCK, 0, NULL, 0);
+               mw_send_frame(&mwdata->mwdevice, MW_GET_REAL_TIME_CLOCK, 0, NULL, 0);
        }
        if (strncmp(cmdline, "gistr", 5) == 0) {
-               mw_send_frame(mw_fd, MW_GET_INFORMATION_STRING, 0, NULL, 0);
+               intopt = cmdline[6]-0x30;
+               if (intopt>=0 && intopt <=3)
+                       mdata[0] = intopt;
+               else
+                       mdata[0] = 0;
+               mw_send_frame(&mwdata->mwdevice, MW_GET_INFORMATION_STRING, 0, mdata, 1);
        }
        if (strncmp(cmdline, "gdtype", 6) == 0) {
-               mw_send_frame(mw_fd, MW_GET_DEVICE_TYPE, 0, NULL, 0);
+               mw_send_frame(&mwdata->mwdevice, MW_GET_DEVICE_TYPE, 0, NULL, 0);
        }
        if (strncmp(cmdline, "rvbat", 5) == 0) {
-               mw_send_frame(mw_fd, MW_READ_BATTERY_VOLTAGE_MSG, 0, NULL, 0);
+               mw_send_frame(&mwdata->mwdevice, MW_READ_BATTERY_VOLTAGE_MSG, 0, NULL, 0);
        }
        if (strncmp(cmdline, "rlight", 6) == 0) {
-               mw_send_frame(mw_fd, MW_READ_LIGHT_SENSOR_MSG, 0, NULL, 0);
+               mw_send_frame(&mwdata->mwdevice, MW_READ_LIGHT_SENSOR_MSG, 0, NULL, 0);
        }
        if (strncmp(cmdline, "modecfg", 6) == 0) {
-               mw_configure_watch_mode(mw_fd, MW_SCREEN_MODE_IDLE, 0, 4, 1);
-               mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 0);
+               mw_configure_watch_mode(&mwdata->mwdevice, MW_SCREEN_MODE_IDLE, 0, 4, 1);
+               mw_update_display(&mwdata->mwdevice, MW_SCREEN_MODE_IDLE, 0);
        }
        if (strncmp(cmdline, "rbtcfg", 6) == 0) {
+               intopt = cmdline[7]-0x30;
                mdata[0] = 0; /* idle screen */
-               mdata[1] = 1; /* button index */
-               mdata[2] = 2; /* button press type */
-               mdata[3] = 3; /* callback message type */
-               mdata[4] = 4; /* callback message option */
-               mw_send_frame(mw_fd, MW_READ_BUTTON_CONFIG, 0, NULL, 0);
+               mdata[1] = intopt; /* button index */
+               /* for reading the config those are unnecessary */
+               mdata[2] = 0; /* button press type */
+               mdata[3] = 0; /* callback message type */
+               mdata[4] = 0; /* callback message option */
+               mw_send_frame(&mwdata->mwdevice, MW_READ_BUTTON_CONFIG, 0, mdata, 5);
        }
        if (strncmp(cmdline, "svib", 4) == 0) {
-               mw_set_vibrate_mode(mw_fd, 1, 300, 300, 5);
+               mw_set_vibrate_mode(&mwdata->mwdevice, 1, 300, 300, 5);
        }
        if (strncmp(cmdline, "tbmp", 4) == 0) {
-               bitmap_test(mw_fd);
+               bitmap_test(&mwdata->mwdevice);
        }
        if (strncmp(cmdline, "t2bmp", 5) == 0) {
-               bitmap_test2(mw_fd);
+               bitmap_test2(&mwdata->mwdevice);
        }
        if (strncmp(cmdline, "text", 4) == 0) {
-               text_test(mw_fd);
+               text_test(&mwdata->mwdevice);
        }
        if (strncmp(cmdline, "tline", 5) == 0) {
-               line_test(mw_fd);
+               line_test(&mwdata->mwdevice);
        }
        if (strncmp(cmdline, "rbmp", 4) == 0) {
-               bitmap_read(mw_fd);
+               bitmap_read(&mwdata->mwdevice);
+       }
+       if (strncmp(cmdline, "tnote", 5) == 0) {
+               test_notification(&mwdata->mwdevice);
+       }
+       if (strncmp(cmdline, "tapp", 4) == 0) {
+               test_application(&mwdata->mwdevice);
+       }
+       if (strncmp(cmdline, "eoled", 5) == 0) {
+               intopt = cmdline[6]-0x30;
+               if (intopt == MW_OLED_UPPER || intopt == MW_OLED_LOWER)
+                       mw_send_frame(&mwdata->mwdevice, MW_ENABLE_OLED_DISPLAY_MSG, intopt, NULL, 0);
+       }
+       if (strncmp(cmdline, "toled", 5) == 0) {
+               intopt = cmdline[6]-0x30;
+               test_oled(&mwdata->mwdevice, intopt);
+               //mw_write_oled_buffer(mwdevice, 0, 80, 0, mdata, 10);
+               //mw_send_frame(mwdevice, MW_UPDATE_OLED_DISPLAY_MSG, 0, NULL, 0);
        }
 }
 
 
-int menu(int mw_fd)
+int feed_menu(mwdata_t *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
-                               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') {
-                                               printf("\n");
-                                               cmdline[clinep--] = '\0';
-                                               process_cmd(cmdline, clinep, mw_fd);
-                                               clinep = 0;
-                                               memset(cmdline, 0, 128);
-                                       } else {
-                                               clinep++;
-                                               if (clinep > 75)
-                                                       clinep = 75;
-                                               printf("\r> %s", cmdline);
-                                               fflush(stdout);
-                                       }
-                               }
-                       };
-               } else
-                       break;
-               FD_ZERO(&mfds);
-               FD_SET(0, &mfds);
-               FD_SET(mw_fd, &mfds);
-       } while (rcvd > 0);
-
+       rcvd = read(0, (mdata->cmdline+mdata->cmdline_pos), 1);
+       if (rcvd > 0) {
+               if (mdata->cmdline[mdata->cmdline_pos] == '\r') {
+                       printf("\n");
+                       mdata->cmdline[mdata->cmdline_pos--] = '\0';
+                       process_cmd(mdata->cmdline, mdata->cmdline_pos, mdata);
+                       mdata->cmdline_pos = 0;
+                       memset(mdata->cmdline, 0, 128);
+               } else {
+                       mdata->cmdline_pos++;
+                       if (mdata->cmdline_pos > 75)
+                               mdata->cmdline_pos = 75;
+                       printf("\r> %s", mdata->cmdline);
+                       fflush(stdout);
+               }
+       }
        return 0;
 }
 
@@ -390,6 +510,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)
@@ -433,16 +554,99 @@ int str2ba(const char *str, bdaddr_t *ba)
         return 0;
 }
 
+gboolean handle_mw_io(GIOChannel *mw_io, GIOCondition condition, gpointer udata)
+{
+       mwdata_t *mdata = (mwdata_t *)udata;
+       int rcvd;
+
+       rcvd = read(mdata->mwdevice.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->mwdevice, mdata->rcvbuf, rcvd);
+               mdata->rcvbuf_pos = 0;
+       }
+
+       return TRUE;
+}
+
+gboolean handle_min_io(GIOChannel *mw_io, GIOCondition condition, gpointer udata)
+{
+       mwdata_t *mdata = (mwdata_t *)udata;
+
+       feed_menu(mdata);
+
+       return TRUE;
+}
+
+static DBusHandlerResult
+signal_filter (DBusConnection *connection, DBusMessage *message, void *user_data)
+{
+       //GMainLoop *loop = user_data;
+       mwdata_t *mdata = (mwdata_t *)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->mwdevice, 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_t 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
@@ -461,13 +665,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.mwdevice.mw_fd = mw_fd;
+       mdata.rcvbuf_pos = 0;
+       memset(mdata.rcvbuf, 0, 128);
 
-       fsync(mw_fd);
+       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;
 };