]> git.kernelconcepts.de Git - metawatch.git/blob - mw_main.c
d391048088b244807346e0555f9fb8c15e358207
[metawatch.git] / mw_main.c
1 /*
2  * (c) 2011 Siegen, Germany by Nils Faerber <nils.faerber@kernelconcepts.de>
3  *
4  * license LGPL
5  */
6
7 #include <stdio.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <time.h>
15 #include <termios.h>
16 #include <ctype.h>
17
18 #include <errno.h>
19
20 #include <bluetooth/bluetooth.h>
21 #include <bluetooth/rfcomm.h>
22
23 #include <glib.h>
24 #include <dbus/dbus.h>
25 #include <dbus/dbus-glib.h>
26 #include <dbus/dbus-glib-lowlevel.h>
27
28 #include <libsoup/soup.h>
29
30 #include <libxml/tree.h>
31 #include <libxml/parser.h>
32
33 /*
34 #include <bluetooth/sdp.h>
35 #include <bluetooth/sdp_lib.h>
36 */
37
38 #include "metawatch.h"
39 #include "crc16ccitt.h"
40 #include "mw_utility.h"
41
42 #include "bt_helper.h"
43
44 typedef struct {
45         GMainLoop *mloop;
46         mwdevice_t mwdevice;
47         unsigned char rcvbuf[128];
48         int rcvbuf_pos;
49         int con_fd;             /* console input fd */
50         char cmdline[128];
51         int cmdline_pos;
52         SoupSession *msoup;
53         SoupMessage *smsg;
54 } mwdata_t;
55
56 void bitmap_test(mwdevice_t *mwdevice)
57 {
58         /* a nice checker-board pattern */
59         unsigned char checkbuf[24] = {
60                 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
61                 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
62                 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
63                 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
64         };
65         mw_write_buffer(mwdevice, MW_SCREEN_MODE_IDLE, 0, 31, checkbuf, 24);
66         mw_write_buffer(mwdevice, MW_SCREEN_MODE_IDLE, 0, 33, checkbuf, 24);
67         mw_write_buffer(mwdevice, MW_SCREEN_MODE_IDLE, 0, 35, checkbuf, 24);
68         mw_write_buffer(mwdevice, MW_SCREEN_MODE_IDLE, 0, 37, checkbuf, 24);
69
70         mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
71 }
72
73 void bitmap_test2(mwdevice_t *mwdevice)
74 {
75         mw_buffer *mwbuf1, *mwbuf2;
76         unsigned char *bbuf;
77         int len, x;
78
79         mwbuf1 = mw_alloc_pbuffer(96, 66, 1);
80         mwbuf2 = mw_alloc_pbuffer(96, 66, 1);
81
82         mw_buf_clear(mwbuf1, MW_BLACK);
83         mw_buf_clear(mwbuf2, MW_WHITE);
84
85         for (x=0; x<66; x++) {
86                 //mw_buf_clear(mwbuf1, MW_BLACK);
87                 mw_buf_draw_pixel(mwbuf1, x, x, MW_WHITE);
88                 mw_dump_mw_buffer(mwbuf1);
89                 bbuf = mw_make_mw_buffer(mwbuf1, &len);
90                 mw_send_bitmap(mwdevice, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
91                 mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
92                 getchar();
93         };
94
95         mw_free_pbuffer(mwbuf1);
96         mw_free_pbuffer(mwbuf2);
97 }
98
99 void text_test(mwdevice_t *mwdevice)
100 {
101         mw_buffer *mwbuf;
102         unsigned char *bbuf;
103         int len;
104
105         mwbuf = mw_alloc_pbuffer(96, 66, 1);
106         mw_buf_clear(mwbuf, MW_BLACK);
107
108         mw_buf_print(mwbuf, 1, 10, "Font 0", 0, MW_WHITE, MW_BLACK);
109         mw_buf_print(mwbuf, 1, 20, "Font 1", 1, MW_WHITE, MW_BLACK);
110         mw_buf_print(mwbuf, 1, 30, "Font 2", 2, MW_WHITE, MW_BLACK);
111         mw_buf_print(mwbuf, 1, 45, "Big Fat Clipping", 2, MW_BLACK, MW_WHITE);
112
113         bbuf = mw_make_mw_buffer(mwbuf, &len);
114         mw_send_bitmap(mwdevice, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
115         mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
116         mw_free_pbuffer(mwbuf);
117 }
118
119 void line_test(mwdevice_t *mwdevice)
120 {
121         mw_buffer *mwbuf;
122         unsigned char *bbuf;
123         int len, p;
124
125         mwbuf = mw_alloc_pbuffer(96, 66, 1);
126         mw_buf_clear(mwbuf, MW_BLACK);
127
128         for (p=0; p<=96; p+=8)
129                 mw_buf_draw_line_bresenham(mwbuf, p, 0, 95-p, 65, MW_WHITE);
130
131         bbuf = mw_make_mw_buffer(mwbuf, &len);
132         mw_send_bitmap(mwdevice, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
133         mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
134         mw_free_pbuffer(mwbuf);
135 }
136
137 void bitmap_read(mwdevice_t *mwdevice)
138 {
139         int ffd, ret;
140         char rbuf[256];
141         unsigned int width, height, i;
142 #ifdef DEBUG
143         unsigned int x, y;
144 #endif
145         unsigned int rowlength;
146         unsigned char *bmapbuf;
147         // unsigned char mw_buf[24];
148
149         ffd = open("test.pbm", O_RDONLY);
150         if (ffd < 0) {
151                 perror("open");
152                 return;
153         };
154         ret = read(ffd, rbuf, 3);
155         if (rbuf[0] != 'P' || rbuf[1] != '4') {
156                 fprintf(stderr, "not a PBM file\n");
157                 return;
158         }
159         memset(rbuf, 0, 256);
160         i = 0;
161         do {
162                 ret = read(ffd, (rbuf+i), 1);
163         } while (!isspace(rbuf[i++]));
164         width = atoi(rbuf);
165
166         memset(rbuf, 0, 256);
167         i = 0;
168         do {
169                 ret = read(ffd, (rbuf+i), 1);
170         } while (!isspace(rbuf[i++]));
171         height = atoi(rbuf);
172
173         rowlength = ((width / 8) + 1);
174
175         bmapbuf = malloc(rowlength * height);
176
177         ret = read(ffd, bmapbuf, rowlength * height);
178         close(ffd);
179
180 #ifdef DEBUG
181         fprintf(stderr, "row length = %d bytes\n", rowlength);
182         fprintf(stderr, "bitmap resolution is %d x %d\n", width, height);
183         fprintf(stderr, "read %d of %d bytes\n", ret, rowlength * height);
184         fprintf(stderr, "\n");
185         for (y=0; y<height; y++) {
186                 for (x=0; x<rowlength; x++) {
187                         for (i=0; i<8; i++)
188                                 fprintf(stderr, "%c", (bmapbuf[(y*rowlength)+x] & (1<<(7-i))) ? '.' : ' ');
189                 }
190                 fprintf(stderr, "\n");
191         }
192         fprintf(stderr, "\n");
193 #endif
194
195         /* reverse bits and invert the bmap */
196         bmap_buffer_flipinvert(1, 1, bmapbuf, rowlength * height);
197         /* send the buffer to the watch */
198         mw_send_bitmap(mwdevice, MW_SCREEN_MODE_IDLE, width, height, 31, bmapbuf, rowlength * height);
199         /* update the display */
200         mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
201
202         free(bmapbuf);
203 }
204
205 void test_notification(mwdevice_t *mwdevice)
206 {
207         mw_buffer *mwbuf;
208         unsigned char *bbuf;
209         int len;
210
211         mwbuf = mw_alloc_pbuffer(96, 96, 1);
212         mw_buf_clear(mwbuf, MW_BLACK);
213
214         mw_buf_draw_line_bresenham(mwbuf, 2, 0, 93, 0, MW_WHITE);
215         mw_buf_draw_line_bresenham(mwbuf, 0, 1, 95, 1, MW_WHITE);
216         mw_buf_draw_line_bresenham(mwbuf, 0, 94, 95, 94, MW_WHITE);
217         mw_buf_draw_line_bresenham(mwbuf, 2, 95, 93, 95, MW_WHITE);
218
219         mw_buf_draw_line_bresenham(mwbuf, 0, 2, 0, 93, MW_WHITE);
220         mw_buf_draw_line_bresenham(mwbuf, 1, 0, 1, 95, MW_WHITE);
221         mw_buf_draw_line_bresenham(mwbuf, 95, 2, 95, 93, MW_WHITE);
222         mw_buf_draw_line_bresenham(mwbuf, 94, 0, 94, 95, MW_WHITE);
223
224         mw_buf_print(mwbuf, 4,  4, "012345678901234", 0, MW_WHITE, MW_BLACK);
225         mw_buf_print(mwbuf, 4, 13, "012345678901234", 0, MW_WHITE, MW_BLACK);
226         mw_buf_print(mwbuf, 4, 22, "0123456789g1234", 0, MW_WHITE, MW_BLACK);
227         mw_buf_print(mwbuf, 4, 31, "012345678901234", 0, MW_WHITE, MW_BLACK);
228         mw_buf_print(mwbuf, 4, 40, "012345678901234", 0, MW_WHITE, MW_BLACK);
229         mw_buf_print(mwbuf, 4, 49, "012345678901234", 0, MW_WHITE, MW_BLACK);
230         mw_buf_print(mwbuf, 4, 58, "0123456789g1234", 0, MW_WHITE, MW_BLACK);
231         mw_buf_print(mwbuf, 4, 67, "012345678901234", 0, MW_WHITE, MW_BLACK);
232         mw_buf_print(mwbuf, 4, 76, "012345678901234", 0, MW_WHITE, MW_BLACK);
233         mw_buf_print(mwbuf, 4, 85, "012345678901234", 0, MW_WHITE, MW_BLACK);
234
235         bbuf = mw_make_mw_buffer(mwbuf, &len);
236         mw_send_bitmap(mwdevice, MW_SCREEN_MODE_NOTIFICATION, 96, 96, 0, bbuf, len);
237         mw_update_display(mwdevice, MW_SCREEN_MODE_NOTIFICATION, 1);
238         mw_free_pbuffer(mwbuf);
239
240         mw_set_vibrate_mode(mwdevice, 1, 300, 300, 3);
241 }
242
243 int days(int m1, int y1)
244 {
245         int d;
246         if(m1==1 || m1==3 || m1==5 || m1==7 || m1==8 || m1==10 || m1==12)
247                 d=31;
248         else if(m1==4 || m1==6 || m1==9 || m1==11)
249                 d=30;
250         else if((y1%100!=0 && y1%4==0) || y1%400==0)
251                 d=29;
252         else
253                 d=28;
254         return d;
255 }
256
257 void draw_idle_calendar(mwdevice_t *mwdevice)
258 {
259         long unsigned int t;
260         unsigned int y,y1,m,m1,d,i,j,k;
261         /* const char a[12][20]={"January","February","March","April","May","June","July","August","September","October","November","December"}; */
262         const char dnames[7][3] = { "Mo", "Di", "Mi", "Do", "Fr", "Sa", "So" };
263         mw_buffer *mwbuf;
264         unsigned char *bbuf;
265         int len, px, py;
266         char pnum[32];
267         int today;
268         time_t mtime;
269         struct tm mtm;
270                                
271         mtime = time(NULL);
272         localtime_r(&mtime, &mtm);
273
274         today = mtm.tm_mday;
275         y = mtm.tm_year + 1900;
276         m = mtm.tm_mon + 1;
277
278         mwbuf = mw_alloc_pbuffer(96, 66, 1);
279         mw_buf_clear(mwbuf, MW_BLACK);
280
281         if (y < 0)
282                 y = -y;
283         if (m <= 0 || m >= 13)
284                 m = 1;
285         y1 = 0;
286         t = 0;
287         while (y1 < y) {
288                 if (((y1 % 100) != 0 && (y1 % 4) == 0) || (y1 % 400) == 0)
289                         t += 366;
290                 else
291                         t += 365;
292                 y1++;
293         }
294         m1 = 1;
295         while (m1 < m) {
296                 d = days(m1, y);
297                 t = t + d;
298                 m1++;
299         };
300 #if 0
301         printf("Year: '%u'\n",y);
302         printf("Month: '%s'\n",a[m-1]);
303
304         printf("%6s%6s%6s%6s%6s%6s%6s\n","Sun","Mon","Tue","Wed","Thu","Fri","Sat");
305 #endif
306
307         k = 1;
308         /* 0 = 1st day is Monday, 1 = 1st day is Sunday */
309         d = (t % 7)-1;
310         px=0;
311         py=3;
312         for (i=0; i<7; i++) {
313                 snprintf(pnum, 3, "%s", dnames[i]);
314                 mw_buf_print(mwbuf, px, py, pnum, 0, MW_WHITE, MW_BLACK);
315                 px += 14;
316         };
317         px = 0;
318         py += 10;
319         for (i = 1; i <= days(m,y); i++, k++) {
320                 if (i == 1) {
321                         if (d == 0) {
322                                 for (j = 1; j < 7; j++, k++) {
323                                         printf("+--"); //printf("%3s","");
324                                         px += 14;
325                                 }
326                         } else {
327                                 for (j = 1; j < d; j++, k++) {
328                                         px += 14;
329                                         printf("--+"); //printf("%3s","");
330                                 }
331                         }
332                 }
333                 snprintf(pnum, 8, "%2d", i);
334                 if (i == today)
335                         mw_buf_print(mwbuf, px, py, pnum, 0, MW_BLACK, MW_WHITE);
336                 else
337                         mw_buf_print(mwbuf, px, py, pnum, 0, MW_WHITE, MW_BLACK);
338                 px += 12;
339                 printf("%2d", i);
340                 if ((k % 7) == 0) {
341                         printf("\n");
342                         px = 0;
343                         py += 10;
344                 } else {
345                         printf("+");
346                         px += 2;
347                 }
348         }
349         // printf("\n");
350
351         //mw_buf_print(mwbuf, 1, 10, "Font 0", 0, MW_WHITE, MW_BLACK);
352
353         bbuf = mw_make_mw_buffer(mwbuf, &len);
354         mw_send_bitmap(mwdevice, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
355         mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
356         mw_free_pbuffer(mwbuf);
357 }
358
359 void mw_send_notify(mwdevice_t *mwdevice, char *header, char *body)
360 {
361         mw_buffer *mwbuf;
362         unsigned char *bbuf;
363         int len,i,c,r;
364         char sstr[32];
365
366         mwbuf = mw_alloc_pbuffer(96, 96, 1);
367         mw_buf_clear(mwbuf, MW_BLACK);
368
369         mw_buf_print(mwbuf, 0,  0, header, 0, MW_BLACK, MW_WHITE);
370
371         i=0;
372         c=0; r=1;
373         memset(sstr,0,32);
374         while (i<strlen(body)) {
375                 sstr[c++] = body[i++];
376                 if (c>=16 || i>=strlen(body)) {
377                         mw_buf_print(mwbuf, 0,  r*9, sstr, 0, MW_WHITE, MW_BLACK);
378                         memset(sstr,0,32);
379                         c=0; r++;
380                         if (r>10)
381                                 break;
382                 };
383         };
384
385         bbuf = mw_make_mw_buffer(mwbuf, &len);
386         mw_send_bitmap(mwdevice, MW_SCREEN_MODE_APPLICATION, 96, 96, 0, bbuf, len);
387         mw_update_display(mwdevice, MW_SCREEN_MODE_APPLICATION, 1);
388         mw_free_pbuffer(mwbuf);
389
390         mw_set_vibrate_mode(mwdevice, 1, 300, 300, 2);
391 }
392
393 void test_application(mwdevice_t *mwdevice)
394 {
395         mw_buffer *mwbuf;
396         unsigned char *bbuf;
397         int len;
398
399         mw_enable_button(mwdevice, MW_SCREEN_MODE_IDLE, MW_BUTTON_A, MW_BUTTON_IMMEDIATE, MW_BUTTON_EVENT_MESSAGE, MW_BUTTON_A);
400
401         mw_enable_button(mwdevice, MW_SCREEN_MODE_APPLICATION, MW_BUTTON_A, MW_BUTTON_IMMEDIATE, MW_BUTTON_EVENT_MESSAGE, MW_BUTTON_A);
402         mw_enable_button(mwdevice, MW_SCREEN_MODE_APPLICATION, MW_BUTTON_B, MW_BUTTON_IMMEDIATE, MW_BUTTON_EVENT_MESSAGE, MW_BUTTON_B);
403         mw_enable_button(mwdevice, MW_SCREEN_MODE_APPLICATION, MW_BUTTON_C, MW_BUTTON_IMMEDIATE, MW_BUTTON_EVENT_MESSAGE, MW_BUTTON_C);
404         mw_enable_button(mwdevice, MW_SCREEN_MODE_APPLICATION, MW_BUTTON_E, MW_BUTTON_IMMEDIATE, MW_BUTTON_EVENT_MESSAGE, MW_BUTTON_E);
405         mw_enable_button(mwdevice, MW_SCREEN_MODE_APPLICATION, MW_BUTTON_F, MW_BUTTON_IMMEDIATE, MW_BUTTON_EVENT_MESSAGE, MW_BUTTON_F);
406         mw_configure_watch_mode(mwdevice, MW_SCREEN_MODE_APPLICATION, 0, 30, 0);
407
408         mwbuf = mw_alloc_pbuffer(96, 96, 1);
409         mw_buf_clear(mwbuf, MW_BLACK);
410
411         mw_buf_print(mwbuf, 0,  0, "  Application   ", 0, MW_BLACK, MW_WHITE);
412         mw_buf_print(mwbuf, 0,  9, "0123456789012345", 0, MW_WHITE, MW_BLACK);
413         mw_buf_print(mwbuf, 0, 18, "0123456789g12345", 0, MW_WHITE, MW_BLACK);
414         mw_buf_print(mwbuf, 0, 27, "0123456789012345", 0, MW_WHITE, MW_BLACK);
415         mw_buf_print(mwbuf, 0, 36, "0123456789012345", 0, MW_WHITE, MW_BLACK);
416         mw_buf_print(mwbuf, 0, 45, "0123456789012345", 0, MW_WHITE, MW_BLACK);
417         mw_buf_print(mwbuf, 0, 54, "0123456789g12345", 0, MW_WHITE, MW_BLACK);
418         mw_buf_print(mwbuf, 0, 63, "0123456789012345", 0, MW_WHITE, MW_BLACK);
419         mw_buf_print(mwbuf, 0, 72, "0123456789012345", 0, MW_WHITE, MW_BLACK);
420         mw_buf_print(mwbuf, 0, 81, "0123456789012345", 0, MW_WHITE, MW_BLACK);
421
422         bbuf = mw_make_mw_buffer(mwbuf, &len);
423         mw_send_bitmap(mwdevice, MW_SCREEN_MODE_APPLICATION, 96, 96, 0, bbuf, len);
424         mw_update_display(mwdevice, MW_SCREEN_MODE_APPLICATION, 1);
425         mw_free_pbuffer(mwbuf);
426 }
427
428 void test_oled(mwdevice_t *mwdevice, unsigned char oled)
429 {
430         mw_buffer *mwbuf;
431         unsigned char *bbuf;
432         int len;
433
434         mwbuf = mw_alloc_pbuffer(80, 16, 1);
435         mw_buf_clear(mwbuf, MW_BLACK);
436
437         mw_buf_print(mwbuf, 0,  0, "Application", 0, MW_WHITE, MW_BLACK);
438         mw_buf_print(mwbuf, 0,  8, "012345678901234", 0, MW_WHITE, MW_BLACK);
439
440         bbuf = mw_make_mw_oled_buffer(mwbuf, &len);
441         mw_write_oled_buffer(mwdevice, 0, oled, 80, 0, bbuf, len);
442
443         mw_free_pbuffer(mwbuf);
444 }
445
446 static void
447 weather_soup_callback (SoupSession *session, SoupMessage *msg, gpointer user_data)
448 {
449         xmlDocPtr xdoc;
450
451         /* Handle the response here */
452         if (msg->response_body->data != NULL && msg->response_body->length != 0) {
453                 fprintf(stderr, "%s\n", msg->response_body->data);
454                 xdoc = xmlReadMemory(msg->response_body->data, msg->response_body->length, "noname.xml", NULL, 0);
455                 if (xdoc == NULL) {
456                         fprintf(stderr, "Failed to parse document\n");
457                         return;
458                 }
459                 xmlFreeDoc(xdoc);
460                 xmlCleanupParser();
461         }
462 }
463
464 void do_weather(mwdata_t *mwdata)
465 {
466         mwdata->msoup = soup_session_async_new();
467         mwdata->smsg = soup_message_new ("GET", "http://www.google.com/ig/api?weather=siegen&hl=de");
468         soup_session_send_message (mwdata->msoup, mwdata->smsg);
469         soup_session_queue_message (mwdata->msoup, mwdata->smsg, weather_soup_callback, mwdata);
470 }
471
472 void print_help(void)
473 {
474 }
475
476 void process_cmd(char *cmdline, int clinep, mwdata_t *mwdata)
477 {
478         unsigned char mdata[32];
479         unsigned int intopt;
480
481         fprintf(stderr, "command: '%s'\n", cmdline);
482
483         if (strncmp(cmdline, "quit", 4) == 0) {
484                 //close(mw_fd);
485                 //exit(0);
486                 /* just in case we quit and show another screen */
487                 mw_update_display(&mwdata->mwdevice, MW_SCREEN_MODE_IDLE, 0);
488                 g_main_loop_quit (mwdata->mloop);
489         }
490         if (strncmp(cmdline, "help", 4) == 0) {
491                 print_help();
492         }
493         if (strncmp(cmdline, "ahand", 5) == 0) {
494                 intopt = atoi(cmdline+5);
495                 g_print("Advance analog hands by %d minutes\n", intopt);
496                 mdata[0] = intopt / 60;
497                 mdata[1] = intopt % 60;
498                 mdata[2] = 0;
499                 mw_send_frame(&mwdata->mwdevice, MW_ADVANCE_WATCH_HANDS, 0, mdata, 3);
500         }
501         if (strncmp(cmdline, "srtc", 4) == 0) {
502                 fprintf(stderr, "Setting RTC from system time...");
503                 mw_set_rtc(&mwdata->mwdevice);
504                 fprintf(stderr, "OK\n");
505         }
506         if (strncmp(cmdline, "grtc", 4) == 0) {
507                 mw_send_frame(&mwdata->mwdevice, MW_GET_REAL_TIME_CLOCK, 0, NULL, 0);
508         }
509         if (strncmp(cmdline, "gistr", 5) == 0) {
510                 intopt = cmdline[6]-0x30;
511                 if (intopt>=0 && intopt <=3)
512                         mdata[0] = intopt;
513                 else
514                         mdata[0] = 0;
515                 mw_send_frame(&mwdata->mwdevice, MW_GET_INFORMATION_STRING, 0, mdata, 1);
516         }
517         if (strncmp(cmdline, "gdtype", 6) == 0) {
518                 mw_send_frame(&mwdata->mwdevice, MW_GET_DEVICE_TYPE, 0, NULL, 0);
519         }
520         if (strncmp(cmdline, "rvbat", 5) == 0) {
521                 mw_send_frame(&mwdata->mwdevice, MW_READ_BATTERY_VOLTAGE_MSG, 0, NULL, 0);
522         }
523         if (strncmp(cmdline, "rlight", 6) == 0) {
524                 mw_send_frame(&mwdata->mwdevice, MW_READ_LIGHT_SENSOR_MSG, 0, NULL, 0);
525         }
526         if (strncmp(cmdline, "modecfg", 6) == 0) {
527                 mw_configure_watch_mode(&mwdata->mwdevice, MW_SCREEN_MODE_IDLE, 0, 4, 1);
528                 mw_update_display(&mwdata->mwdevice, MW_SCREEN_MODE_IDLE, 0);
529         }
530         if (strncmp(cmdline, "rbtcfg", 6) == 0) {
531                 intopt = cmdline[7]-0x30;
532                 mdata[0] = 0; /* idle screen */
533                 mdata[1] = intopt; /* button index */
534                 /* for reading the config those are unnecessary */
535                 mdata[2] = 0; /* button press type */
536                 mdata[3] = 0; /* callback message type */
537                 mdata[4] = 0; /* callback message option */
538                 mw_send_frame(&mwdata->mwdevice, MW_READ_BUTTON_CONFIG, 0, mdata, 5);
539         }
540         if (strncmp(cmdline, "svib", 4) == 0) {
541                 mw_set_vibrate_mode(&mwdata->mwdevice, 1, 300, 300, 5);
542         }
543         if (strncmp(cmdline, "tbmp", 4) == 0) {
544                 bitmap_test(&mwdata->mwdevice);
545         }
546         if (strncmp(cmdline, "t2bmp", 5) == 0) {
547                 bitmap_test2(&mwdata->mwdevice);
548         }
549         if (strncmp(cmdline, "text", 4) == 0) {
550                 text_test(&mwdata->mwdevice);
551         }
552         if (strncmp(cmdline, "tline", 5) == 0) {
553                 line_test(&mwdata->mwdevice);
554         }
555         if (strncmp(cmdline, "rbmp", 4) == 0) {
556                 bitmap_read(&mwdata->mwdevice);
557         }
558         if (strncmp(cmdline, "tnote", 5) == 0) {
559                 // test_notification(&mwdata->mwdevice);
560                 mw_do_notification(&mwdata->mwdevice, "TestNotification", "This is a pretty long text that needs to be broken and torn", 1);
561         }
562         if (strncmp(cmdline, "tapp", 4) == 0) {
563                 test_application(&mwdata->mwdevice);
564         }
565         if (strncmp(cmdline, "eoled", 5) == 0) {
566                 intopt = cmdline[6]-0x30;
567                 if (intopt == MW_OLED_UPPER || intopt == MW_OLED_LOWER)
568                         mw_send_frame(&mwdata->mwdevice, MW_ENABLE_OLED_DISPLAY_MSG, intopt, NULL, 0);
569         }
570         if (strncmp(cmdline, "toled", 5) == 0) {
571                 intopt = cmdline[6]-0x30;
572                 test_oled(&mwdata->mwdevice, intopt);
573                 //mw_write_oled_buffer(mwdevice, 0, 80, 0, mdata, 10);
574                 //mw_send_frame(mwdevice, MW_UPDATE_OLED_DISPLAY_MSG, 0, NULL, 0);
575         }
576         if (strncmp(cmdline, "cal", 3) == 0) {
577                 draw_idle_calendar(&mwdata->mwdevice);
578         }
579         if (strncmp(cmdline, "wet", 3) == 0) {
580                 do_weather(mwdata);
581         }
582         if (strncmp(cmdline, "c24", 3) == 0) {
583                 mdata[0] = MW_RTC_CLOCK_24HR;
584                 mw_nval_operation(&mwdata->mwdevice, MW_NVAL_OPERATION_WRITE, MW_NVAL_TIME_FORMAT, 1, mdata);
585         }
586         if (strncmp(cmdline, "g24", 3) == 0) {
587                 mdata[0] = 0;
588                 mw_nval_operation(&mwdata->mwdevice, MW_NVAL_OPERATION_READ, MW_NVAL_TIME_FORMAT, 1, mdata);
589         }
590 }
591
592
593 int feed_menu(mwdata_t *mdata)
594 {
595         int rcvd;
596
597         rcvd = read(0, (mdata->cmdline+mdata->cmdline_pos), 1);
598         if (rcvd > 0) {
599                 if (mdata->cmdline[mdata->cmdline_pos] == '\r') {
600                         printf("\n");
601                         mdata->cmdline[mdata->cmdline_pos--] = '\0';
602                         process_cmd(mdata->cmdline, mdata->cmdline_pos, mdata);
603                         mdata->cmdline_pos = 0;
604                         memset(mdata->cmdline, 0, 128);
605                 } else {
606                         mdata->cmdline_pos++;
607                         if (mdata->cmdline_pos > 75)
608                                 mdata->cmdline_pos = 75;
609                         printf("\r> %s", mdata->cmdline);
610                         fflush(stdout);
611                 }
612         }
613         return 0;
614 }
615
616
617 gboolean handle_mw_io(GIOChannel *mw_io, GIOCondition condition, gpointer udata)
618 {
619         mwdata_t *mdata = (mwdata_t *)udata;
620         int rcvd;
621
622         rcvd = read(mdata->mwdevice.mw_fd, mdata->rcvbuf, 64);
623 #ifdef DEBUG
624         fprintf(stderr, "read %d bytes:\n", rcvd);
625 #endif
626         if (rcvd > 0) {
627 #ifdef DEBUG
628                 dump_frame(mdata->rcvbuf, rcvd);
629 #endif
630                 mw_feed_msg_buffer(&mdata->mwdevice, mdata->rcvbuf, rcvd);
631         }
632
633         return TRUE;
634 }
635
636 gboolean handle_min_io(GIOChannel *mw_io, GIOCondition condition, gpointer udata)
637 {
638         mwdata_t *mdata = (mwdata_t *)udata;
639
640         feed_menu(mdata);
641
642         return TRUE;
643 }
644
645 static DBusHandlerResult
646 signal_filter (DBusConnection *connection, DBusMessage *message, void *user_data)
647 {
648         //GMainLoop *loop = user_data;
649         mwdata_t *mdata = (mwdata_t *)user_data;
650         DBusError error;
651         char *app_name, *app_icon, *summary, *body; // **actions;
652         int replace_id; // dict, expire;
653
654         /* A signal from the bus saying we are about to be disconnected */
655         if (dbus_message_is_signal(message, "org.freedesktop.Local", "Disconnected")) {
656                 /* Tell the main loop to quit */
657                 g_main_loop_quit (mdata->mloop);
658                 /* We have handled this message, don't pass it on */
659                 return DBUS_HANDLER_RESULT_HANDLED;
660         }
661
662         if (dbus_message_is_method_call (message,"org.freedesktop.Notifications", "Notify"))    {
663                 g_print("method call\n");
664                 dbus_error_init (&error);
665                 if (dbus_message_get_args(message, &error,
666                         DBUS_TYPE_STRING, &app_name,
667                         DBUS_TYPE_UINT32, &replace_id,
668                         DBUS_TYPE_STRING, &app_icon,
669                         DBUS_TYPE_STRING, &summary,
670                         DBUS_TYPE_STRING, &body,
671                         /*  G_TYPE_STRV, &actions,
672                         DBUS_TYPE_DICT_ENTRY_AS_STRING, &dict,
673                         DBUS_TYPE_INT32, &expire, */
674                         DBUS_TYPE_INVALID)) {
675                         g_print("Notify received: from app %s, icon %s:\nSummary: %s\nBody: %s\n", app_name, app_icon, summary, body);
676                         mw_send_notify(&mdata->mwdevice, summary, body);
677                         /* we just listen, we do not handle it here */
678                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
679                 } else {
680                         g_print("Notify received, but error getting message: %s\n", error.message);
681                         dbus_error_free (&error);
682                 }
683         } else {
684                 g_print("Not signal received\n");
685                 return DBUS_HANDLER_RESULT_HANDLED;
686         };
687
688         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
689 }
690
691 void mw_button_event_message_cb(mwdevice_t *mwdevice, unsigned char buttons, unsigned char options, void *user_data)
692 {
693         g_print("button event cb:0x%02x 0x%02x\n", buttons, options);
694         g_print("screen is %d\n", mwdevice->screen_mode);
695         if (mwdevice->screen_mode == MW_SCREEN_MODE_IDLE) {
696                 if (buttons == MW_BUTTON_A)
697                         test_application(mwdevice);
698         }
699         if (mwdevice->screen_mode == MW_SCREEN_MODE_APPLICATION) {
700                 if (buttons == MW_BUTTON_C)
701                         mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 0);
702         }
703 }
704
705 int main(int argc, char **argv)
706 {
707         int mw_fd;
708         mwdata_t mdata;
709         struct termios tconfd, otconfd, tmwfd;
710         bdaddr_t btaddr;
711         GIOChannel *mw_io, *m_in;
712         DBusConnection *bus;
713         DBusError error;
714
715         if (argc != 2) {
716                 fprintf(stderr, "Usage:\n\t%s <devicename>\n", argv[0]);
717                 return 1;
718         };
719   
720         g_type_init();
721         mdata.mloop = g_main_loop_new (NULL, FALSE);
722
723         crc16ccitt_init();
724
725 #if USE_RFCOMM_FILE
726         mw_fd = open(argv[1], O_RDWR);
727         if (mw_fd < 0) {
728                 perror("open");
729                 return 1;
730         };
731 #else
732         if (str2ba(argv[1], &btaddr))
733                 return 1;
734         mw_fd = open_socket(&btaddr, 1);
735         if (mw_fd < 0) {
736                 return 1;
737         } else {
738                 fprintf(stderr, "connected to %s\n", argv[1]);
739         };
740 #endif
741         /* we have a connection, RFCOMM socket is on mw_fd */
742         /* make the tty raw */
743         tcgetattr(mw_fd, &tmwfd);
744         cfmakeraw(&tmwfd);
745         tmwfd.c_oflag |= ONLCR | OPOST;
746         tmwfd.c_lflag |= ISIG;
747         tcsetattr(mw_fd, TCSANOW, &tmwfd);
748
749         mdata.mwdevice.mw_fd = mw_fd;
750         mdata.rcvbuf_pos = 0;
751         memset(mdata.rcvbuf, 0, 128);
752
753         dbus_error_init (&error);
754         bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
755
756         if (!bus) {
757                 g_warning ("Failed to connect to the D-BUS daemon: %s", error.message);
758                 dbus_error_free (&error);
759                 return 1;
760         }
761
762         dbus_connection_setup_with_g_main (bus, NULL);
763         dbus_bus_add_match (bus, "interface='org.freedesktop.Notifications'", &error);
764
765         dbus_connection_add_filter (bus, signal_filter, &mdata, NULL);
766
767         // menu(mw_fd);
768         mw_io = g_io_channel_unix_new(mw_fd);
769         g_io_add_watch(mw_io, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, handle_mw_io, &mdata);
770
771         tcgetattr(0, &tconfd);
772         tcgetattr(0, &otconfd);
773         cfmakeraw(&tconfd);
774         tconfd.c_oflag |= ONLCR | OPOST;
775         tconfd.c_lflag |= ISIG;
776         tcsetattr(0, TCSANOW, &tconfd);
777
778         mdata.con_fd = 0;
779
780         m_in = g_io_channel_unix_new(0); /* stdin for kbd */
781         g_io_add_watch(m_in, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, handle_min_io, &mdata);
782
783         memset(mdata.cmdline, 0, 128);
784         mdata.cmdline_pos = 0;
785
786         mw_init(&mdata.mwdevice, mw_fd);
787         mw_set_button_event_message_cb(&mdata.mwdevice, mw_button_event_message_cb, NULL);
788
789         g_main_loop_run (mdata.mloop);
790
791         fsync(mw_fd);
792         close(mw_fd);
793
794         /* reset the controlling tty again */
795         tcsetattr(0, TCSANOW, &otconfd);
796
797         return 0;
798 };
799