27e83044f81894f91329548b94c5b8f56faff0e5
[oswald.git] / ui / oswald-ui.c
1 #include <stdio.h>
2 #include <sys/stat.h>
3 #include <fcntl.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <string.h>
8 #include <poll.h>
9 #include <sys/socket.h>
10 #include <sys/un.h>
11 #include <time.h>
12
13 #include <gtk/gtk.h>
14
15 #include "Fonts.h" // the MetaWatch fonts
16 #include "oswald.h"
17 #include "oswald_main.h"
18
19 #include "oswald-ui.h"
20
21 #define BITMAP_WIDTH    192
22 #define BITMAP_HEIGHT   192
23
24 static oswald_ui *ui_g;
25
26 void lcd_set_pixel(gint x, gint y, gboolean state)
27 {
28         gint ix, iy;
29
30         ix = x*2;
31         iy = y*2;
32
33         gdk_draw_point(GDK_DRAWABLE(ui_g->pixmap), state ? ui_g->darea->style->black_gc : ui_g->darea->style->white_gc, ix, iy);
34         gdk_draw_point(GDK_DRAWABLE(ui_g->pixmap), state ? ui_g->darea->style->black_gc : ui_g->darea->style->white_gc, ix+1, iy);
35         gdk_draw_point(GDK_DRAWABLE(ui_g->pixmap), state ? ui_g->darea->style->black_gc : ui_g->darea->style->white_gc, ix, iy+1);
36         gdk_draw_point(GDK_DRAWABLE(ui_g->pixmap), state ? ui_g->darea->style->black_gc : ui_g->darea->style->white_gc, ix+1, iy+1);
37
38         gtk_widget_queue_draw(ui_g->darea);
39 }
40
41 void lcd_clear_display(void)
42 {
43         gdk_draw_rectangle (ui_g->pixmap,
44                 ui_g->darea->style->white_gc,
45                 TRUE,
46                 0, 0,
47                 ui_g->darea->allocation.width,
48                 ui_g->darea->allocation.height);
49
50         gtk_widget_queue_draw(ui_g->darea);
51 }
52
53 static gint
54 configure_event (GtkWidget *widget, GdkEventConfigure *event, gpointer user_data)
55 {
56         oswald_ui *ui = (oswald_ui *)user_data;
57
58         if (ui->pixmap)
59                 gdk_pixmap_unref(ui->pixmap);
60
61         ui->pixmap = gdk_pixmap_new(widget->window,
62                 widget->allocation.width,
63                 widget->allocation.height,
64                 -1);
65         gdk_draw_rectangle (ui->pixmap,
66                 widget->style->white_gc,
67                 TRUE,
68                 0, 0,
69                 widget->allocation.width,
70                 widget->allocation.height);
71
72         return TRUE;
73 }
74
75 static gint
76 expose_event (GtkWidget *widget, GdkEventExpose *event, gpointer user_data)
77 {
78         oswald_ui *ui = (oswald_ui *)user_data;
79
80         gdk_draw_pixmap(widget->window,
81                 widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
82                 ui->pixmap,
83                 event->area.x, event->area.y,
84                 event->area.x, event->area.y,
85                 event->area.width, event->area.height);
86
87         return FALSE;
88 }
89
90 void button_A_clicked (GtkButton *button, gpointer user_data)
91 {
92         oswald_ui *ui = (oswald_ui *)user_data;
93
94         g_print("Button-A clicked\n");
95         oswald_handle_button_press(BUTTON_A);
96 }
97
98 void button_B_clicked (GtkButton *button, gpointer user_data)
99 {
100         oswald_ui *ui = (oswald_ui *)user_data;
101
102         g_print("Button-B clicked\n");
103         oswald_handle_button_press(BUTTON_B);
104 }
105
106 void button_C_clicked (GtkButton *button, gpointer user_data)
107 {
108         oswald_ui *ui = (oswald_ui *)user_data;
109
110         g_print("Button-C clicked\n");
111         oswald_handle_button_press(BUTTON_C);
112 }
113
114 void button_D_clicked (GtkButton *button, gpointer user_data)
115 {
116         oswald_ui *ui = (oswald_ui *)user_data;
117
118         g_print("Button-D clicked\n");
119         oswald_handle_button_press(BUTTON_D);
120 }
121
122 void button_E_clicked (GtkButton *button, gpointer user_data)
123 {
124         oswald_ui *ui = (oswald_ui *)user_data;
125
126         g_print("Button-E clicked\n");
127         oswald_handle_button_press(BUTTON_E);
128 }
129
130 void button_F_clicked (GtkButton *button, gpointer user_data)
131 {
132         oswald_ui *ui = (oswald_ui *)user_data;
133
134         g_print("Button-F clicked\n");
135         oswald_handle_button_press(BUTTON_F);
136 }
137
138 gboolean button_F_pr (GtkWidget *widget, GdkEvent *event, gpointer user_data)
139 {
140         oswald_ui *ui = (oswald_ui *)user_data;
141         static gint32 press_time;
142
143         GdkEventButton *bev = (GdkEventButton *)event;
144         if (bev->type == GDK_BUTTON_PRESS) {
145                 press_time = bev->time;
146                 return FALSE;
147         };
148         if (bev->type == GDK_BUTTON_RELEASE) {
149                 if (bev->time > (press_time+1000)) {
150                         g_print("Button-F long press\n");
151                         return TRUE;
152                 };
153         };
154
155         return FALSE;
156 }
157
158 static void create_mainwin(oswald_ui *ui)
159 {
160         GtkWidget *mvb, *hb, *vb, *btn, *sc, *l;
161
162         ui->pixmap = NULL;
163
164         ui->mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
165         // gtk_window_set_default_size (GTK_WINDOW (ui->mainwin), 440, 240);
166         g_signal_connect(G_OBJECT(ui->mainwin), "destroy", gtk_main_quit, NULL);
167
168         mvb = gtk_vbox_new(FALSE, 5);
169         gtk_container_add(GTK_CONTAINER(ui->mainwin), mvb);
170
171         hb = gtk_hbox_new(FALSE, 5);
172         gtk_box_pack_start (GTK_BOX(mvb), hb, FALSE, FALSE, 5);
173
174         vb = gtk_vbox_new(FALSE, 5);
175         gtk_box_pack_start (GTK_BOX(hb), vb, FALSE, FALSE, 5);
176
177         btn = gtk_button_new_with_label(" D ");
178         gtk_box_pack_start (GTK_BOX(vb), btn, FALSE, FALSE, 10);
179         g_signal_connect(G_OBJECT(btn), "clicked", G_CALLBACK(button_D_clicked), ui);
180
181         btn = gtk_button_new_with_label(" E ");
182         gtk_box_pack_start (GTK_BOX(vb), btn, FALSE, FALSE, 10);
183         g_signal_connect(G_OBJECT(btn), "clicked", G_CALLBACK(button_D_clicked), ui);
184
185         btn = gtk_button_new_with_label(" F ");
186         gtk_box_pack_start (GTK_BOX(vb), btn, FALSE, FALSE, 10);
187         g_signal_connect(G_OBJECT(btn), "clicked", G_CALLBACK(button_F_clicked), ui);
188         g_signal_connect(G_OBJECT(btn), "button-press-event", G_CALLBACK(button_F_pr), ui);
189         g_signal_connect(G_OBJECT(btn), "button-release-event", G_CALLBACK(button_F_pr), ui);
190
191         ui->darea = gtk_drawing_area_new ();
192         gtk_box_pack_start (GTK_BOX(hb), GTK_WIDGET(ui->darea), FALSE, FALSE, 5);
193         gtk_drawing_area_size (GTK_DRAWING_AREA(ui->darea), BITMAP_WIDTH, BITMAP_HEIGHT);
194
195         gtk_signal_connect (GTK_OBJECT (ui->darea), "expose_event", (GtkSignalFunc) expose_event, ui);
196         gtk_signal_connect (GTK_OBJECT (ui->darea), "configure_event", (GtkSignalFunc) configure_event, ui);
197         // gtk_signal_connect (GTK_OBJECT (drawing_area), "motion_notify_event", (GtkSignalFunc) motion_notify_event, ui);
198         // gtk_signal_connect (GTK_OBJECT (drawing_area), "button_press_event", (GtkSignalFunc) button_press_event, ui);
199
200         gtk_widget_set_events (GTK_WIDGET(ui->darea), GDK_EXPOSURE_MASK
201                 | GDK_LEAVE_NOTIFY_MASK
202                 | GDK_BUTTON_PRESS_MASK
203                 | GDK_POINTER_MOTION_MASK
204                 | GDK_POINTER_MOTION_HINT_MASK);
205
206         vb = gtk_vbox_new(FALSE, 5);
207         gtk_box_pack_start (GTK_BOX(hb), vb, FALSE, FALSE, 5);
208
209         btn = gtk_button_new_with_label(" A ");
210         gtk_box_pack_start (GTK_BOX(vb), btn, FALSE, FALSE, 10);
211         g_signal_connect(G_OBJECT(btn), "clicked", G_CALLBACK(button_A_clicked), ui);
212
213         btn = gtk_button_new_with_label(" B ");
214         gtk_box_pack_start (GTK_BOX(vb), btn, FALSE, FALSE, 10);
215         g_signal_connect(G_OBJECT(btn), "clicked", G_CALLBACK(button_B_clicked), ui);
216
217         btn = gtk_button_new_with_label(" C ");
218         gtk_box_pack_start (GTK_BOX(vb), btn, FALSE, FALSE, 10);
219         g_signal_connect(G_OBJECT(btn), "clicked", G_CALLBACK(button_C_clicked), ui);
220
221         sc = gtk_vscale_new_with_range (0, 255, 1);
222         gtk_box_pack_start (GTK_BOX(hb), sc, FALSE, FALSE, 5);
223
224         hb = gtk_hbox_new(FALSE, 0);
225         gtk_box_pack_start (GTK_BOX(mvb), hb, FALSE, FALSE, 5);
226
227         l = gtk_label_new("X:");
228         gtk_box_pack_start (GTK_BOX(hb), l, FALSE, FALSE, 5);
229         sc = gtk_hscale_new_with_range (0, 255, 1);
230         gtk_box_pack_start (GTK_BOX(hb), sc, TRUE, TRUE, 5);
231
232         l = gtk_label_new("Y:");
233         gtk_box_pack_start (GTK_BOX(hb), l, FALSE, FALSE, 5);
234         sc = gtk_hscale_new_with_range (0, 255, 1);
235         gtk_box_pack_start (GTK_BOX(hb), sc, TRUE, TRUE, 5);
236
237         l = gtk_label_new("Z:");
238         gtk_box_pack_start (GTK_BOX(hb), l, FALSE, FALSE, 5);
239         sc = gtk_hscale_new_with_range (0, 255, 1);
240         gtk_box_pack_start (GTK_BOX(hb), sc, TRUE, TRUE, 5);
241
242         gtk_widget_show_all(ui->mainwin);
243 }
244
245 gboolean one_second_tmo_handler (gpointer userdata)
246 {
247         oswald_ui *ui = (oswald_ui *)userdata;
248
249         oswald_one_second_tick();
250
251         return TRUE;
252 }
253
254 gboolean app_idle_handler (gpointer user_data)
255 {
256         g_print("i");
257         return TRUE;
258 }
259
260 int main(int argc , char ** argv)
261 {
262         oswald_ui ui;
263         time_t mt;
264         struct tm mtime;
265
266         ui_g = &ui;
267
268         mt = time(NULL);
269         localtime_r(&mt, &mtime);
270
271         gtk_init (&argc, &argv);
272
273         create_mainwin(&ui);
274
275         oswald_init();
276         oswald_set_time(mtime.tm_hour, mtime.tm_min, mtime.tm_sec);
277
278         g_timeout_add_seconds(1, one_second_tmo_handler, &ui);
279         // g_idle_add(app_idle_handler, &ui);
280
281         gtk_main ();
282         return 0;
283 }
284