X-Git-Url: https://git.kernelconcepts.de/?p=metawatch.git;a=blobdiff_plain;f=mw_main.c;h=10ce47d8ad24d9b5966dd82a79f79fe102c388b1;hp=eafa73a7f9bd18050164de6217206afac558d5b7;hb=fc1c62b9c0c3395297f2871028516a62d5effa19;hpb=b13e74e7cef18139e924327e23c39c116507a7a2 diff --git a/mw_main.c b/mw_main.c index eafa73a..10ce47d 100644 --- a/mw_main.c +++ b/mw_main.c @@ -20,6 +20,11 @@ #include #include +#include +#include +#include +#include + /* #include #include @@ -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,69 @@ 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=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) { } @@ -224,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(); @@ -243,7 +322,12 @@ 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) { - 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(mw_fd, 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); @@ -259,12 +343,14 @@ void process_cmd(char *cmdline, int clinep, int mw_fd) 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); @@ -287,76 +373,32 @@ void process_cmd(char *cmdline, int clinep, int 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); -#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->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; } @@ -431,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) @@ -474,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 \n", argv[0]); return 1; }; + g_type_init(); + mdata.mloop = g_main_loop_new (NULL, FALSE); + crc16ccitt_init(); #if USE_RFCOMM_FILE @@ -502,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); - 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; };