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