]> git.kernelconcepts.de Git - metawatch.git/blobdiff - mw_main.c
Add DBus based freedesktop.org Notify support (uh, still a little hacky),
[metawatch.git] / mw_main.c
index 5dae442aa9f10780cc4e8af3bb9b0ce2ee0dc4b8..10ce47d8ad24d9b5966dd82a79f79fe102c388b1 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 "metawatch.h"
 #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)
 {
@@ -46,12 +61,78 @@ void bitmap_test(int mw_fd)
        mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
 }
 
+void bitmap_test2(int mw_fd)
+{
+       mw_buffer *mwbuf1, *mwbuf2;
+       unsigned char *bbuf;
+       int len, x;
+
+       mwbuf1 = mw_alloc_pbuffer(96, 66, 1);
+       mwbuf2 = mw_alloc_pbuffer(96, 66, 1);
+
+       mw_buf_clear(mwbuf1, MW_BLACK);
+       mw_buf_clear(mwbuf2, MW_WHITE);
+
+       for (x=0; x<66; x++) {
+               //mw_buf_clear(mwbuf1, MW_BLACK);
+               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);
+               getchar();
+       };
+
+       mw_free_pbuffer(mwbuf1);
+       mw_free_pbuffer(mwbuf2);
+}
+
+void text_test(int mw_fd)
+{
+       mw_buffer *mwbuf;
+       unsigned char *bbuf;
+       int len;
+
+       mwbuf = mw_alloc_pbuffer(96, 66, 1);
+       mw_buf_clear(mwbuf, MW_BLACK);
+
+       mw_buf_print(mwbuf, 1, 10, "Font 0", 0, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 1, 20, "Font 1", 1, MW_WHITE, MW_BLACK);
+       mw_buf_print(mwbuf, 1, 30, "Font 2", 2, MW_WHITE, MW_BLACK);
+       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_free_pbuffer(mwbuf);
+}
+
+void line_test(int mw_fd)
+{
+       mw_buffer *mwbuf;
+       unsigned char *bbuf;
+       int len, p;
+
+       mwbuf = mw_alloc_pbuffer(96, 66, 1);
+       mw_buf_clear(mwbuf, MW_BLACK);
+
+       for (p=0; p<=96; p+=8)
+               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_free_pbuffer(mwbuf);
+}
 
 void bitmap_read(int mw_fd)
 {
        int ffd, ret;
        char rbuf[256];
-       unsigned int width, height, i, x, y;
+       unsigned int width, height, i;
+#ifdef DEBUG
+       unsigned int x, y;
+#endif
        unsigned int rowlength;
        unsigned char *bmapbuf;
        // unsigned char mw_buf[24];
@@ -103,7 +184,7 @@ void bitmap_read(int mw_fd)
 #endif
 
        /* reverse bits and invert the bmap */
-       bmap_buffer_flipinvert(1, 0, bmapbuf, rowlength * height);
+       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);
        /* update the display */
@@ -112,6 +193,107 @@ void bitmap_read(int mw_fd)
        free(bmapbuf);
 }
 
+void test_notification(int mw_fd)
+{
+       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(mw_fd, MW_SCREEN_MODE_NOTIFICATION, 96, 96, 0, bbuf, len);
+       mw_update_display(mw_fd, MW_SCREEN_MODE_NOTIFICATION, 1);
+       mw_free_pbuffer(mwbuf);
+
+       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;
+       unsigned char *bbuf;
+       int len;
+
+       mw_enable_button(mw_fd, MW_SCREEN_MODE_APPLICATION, MW_BUTTON_A, MW_BUTTON_IMMEDIATE, MW_BUTTON_EVENT_MESSAGE, MW_BUTTON_A);
+       mw_configure_watch_mode(mw_fd, 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(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);
+}
+
 void print_help(void)
 {
 }
@@ -119,12 +301,14 @@ void print_help(void)
 void process_cmd(char *cmdline, int clinep, int mw_fd)
 {
        unsigned char mdata[32];
+       unsigned int intopt;
 
        fprintf(stderr, "command: '%s'\n", cmdline);
 
        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();
@@ -138,7 +322,11 @@ void process_cmd(char *cmdline, int clinep, int mw_fd)
                mw_send_frame(mw_fd, MW_GET_REAL_TIME_CLOCK, 0, NULL, 0);
        }
        if (strncmp(cmdline, "gistr", 5) == 0) {
-               mdata[0] = 0;
+               intopt = cmdline[6]-0x30;
+               if (intopt>=0 && intopt <=3)
+                       mdata[0] = intopt;
+               else
+                       mdata[0] = 0;
                mw_send_frame(mw_fd, MW_GET_INFORMATION_STRING, 0, mdata, 1);
        }
        if (strncmp(cmdline, "gdtype", 6) == 0) {
@@ -147,17 +335,22 @@ void process_cmd(char *cmdline, int clinep, int mw_fd)
        if (strncmp(cmdline, "rvbat", 5) == 0) {
                mw_send_frame(mw_fd, 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);
+       }
        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);
        }
        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(mw_fd, MW_READ_BUTTON_CONFIG, 0, mdata, 5);
        }
        if (strncmp(cmdline, "svib", 4) == 0) {
                mw_set_vibrate_mode(mw_fd, 1, 300, 300, 5);
@@ -165,75 +358,47 @@ void process_cmd(char *cmdline, int clinep, int mw_fd)
        if (strncmp(cmdline, "tbmp", 4) == 0) {
                bitmap_test(mw_fd);
        }
+       if (strncmp(cmdline, "t2bmp", 5) == 0) {
+               bitmap_test2(mw_fd);
+       }
+       if (strncmp(cmdline, "text", 4) == 0) {
+               text_test(mw_fd);
+       }
+       if (strncmp(cmdline, "tline", 5) == 0) {
+               line_test(mw_fd);
+       }
        if (strncmp(cmdline, "rbmp", 4) == 0) {
                bitmap_read(mw_fd);
        }
+       if (strncmp(cmdline, "tnote", 5) == 0) {
+               test_notification(mw_fd);
+       }
+       if (strncmp(cmdline, "tapp", 4) == 0) {
+               test_application(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);
-                               fprintf(stderr, "read %d bytes:\n", rcvd);
-                               if (rcvd > 0) {
-                                       dump_frame(msg_buf, rcvd);
-                                       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->mw_fd);
+                       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;
 }
 
@@ -241,7 +406,7 @@ int open_socket(bdaddr_t *bdaddr, uint8_t channel)
 {
        struct sockaddr_rc addr;
        int sk, opt;
-       int f;
+       //int f;
 
        sk = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
        if (sk < 0) {
@@ -308,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)
@@ -351,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
@@ -375,15 +624,63 @@ int main(int argc, char **argv)
        mw_fd = open_socket(&btaddr, 1);
        if (mw_fd < 0) {
                return 1;
+       } else {
+               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);
 
-       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;
 };