]> git.karo-electronics.de Git - oswald.git/blob - ui/oswald-ui.c
Add MetaWatch Fonts package, LcdDisplay and start demo
[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 "oswald-ui.h"
16 #include "Fonts.h" // the MetaWatch fonts
17
18 #define BITMAP_WIDTH    192
19 #define BITMAP_HEIGHT   192
20
21 void set_pixel(oswald_ui *ui, gint x, gint y, gboolean state)
22 {
23         GdkRectangle update_rect;
24         gint ix, iy;
25
26         ix = x*2;
27         iy = y*2;
28
29         update_rect.x = ix - 5;
30         update_rect.y = iy - 5;
31         update_rect.width = 10;
32         update_rect.height = 10;
33
34         gdk_draw_point(GDK_DRAWABLE(ui->pixmap), state ? ui->darea->style->black_gc : ui->darea->style->white_gc, ix, iy);
35         gdk_draw_point(GDK_DRAWABLE(ui->pixmap), state ? ui->darea->style->black_gc : ui->darea->style->white_gc, ix+1, iy);
36         gdk_draw_point(GDK_DRAWABLE(ui->pixmap), state ? ui->darea->style->black_gc : ui->darea->style->white_gc, ix, iy+1);
37         gdk_draw_point(GDK_DRAWABLE(ui->pixmap), state ? ui->darea->style->black_gc : ui->darea->style->white_gc, ix+1, iy+1);
38
39         gtk_widget_draw (GTK_WIDGET(ui->darea), &update_rect);
40 }
41
42 void clear_display(oswald_ui *ui)
43 {
44         GdkRectangle update_rect;
45
46         update_rect.x = 0;
47         update_rect.y = 0;
48         update_rect.width = BITMAP_WIDTH;
49         update_rect.height = BITMAP_HEIGHT;
50
51         gdk_draw_rectangle (ui->pixmap,
52                 ui->darea->style->white_gc,
53                 TRUE,
54                 0, 0,
55                 ui->darea->allocation.width,
56                 ui->darea->allocation.height);
57
58         gtk_widget_draw (GTK_WIDGET(ui->darea), &update_rect);
59 }
60
61 static gint
62 configure_event (GtkWidget *widget, GdkEventConfigure *event, gpointer user_data)
63 {
64         oswald_ui *ui = (oswald_ui *)user_data;
65
66         if (ui->pixmap)
67                 gdk_pixmap_unref(ui->pixmap);
68
69         ui->pixmap = gdk_pixmap_new(widget->window,
70                 widget->allocation.width,
71                 widget->allocation.height,
72                 -1);
73         gdk_draw_rectangle (ui->pixmap,
74                 widget->style->white_gc,
75                 TRUE,
76                 0, 0,
77                 widget->allocation.width,
78                 widget->allocation.height);
79
80         return TRUE;
81 }
82
83 static gint
84 expose_event (GtkWidget *widget, GdkEventExpose *event, gpointer user_data)
85 {
86         oswald_ui *ui = (oswald_ui *)user_data;
87
88         gdk_draw_pixmap(widget->window,
89                 widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
90                 ui->pixmap,
91                 event->area.x, event->area.y,
92                 event->area.x, event->area.y,
93                 event->area.width, event->area.height);
94
95         return FALSE;
96 }
97
98 static void create_mainwin(oswald_ui *ui)
99 {
100         GtkWidget *hb, *vb, *btn;
101
102         ui->pixmap = NULL;
103
104         ui->mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
105         // gtk_window_set_default_size (GTK_WINDOW (ui->mainwin), 440, 240);
106         g_signal_connect(G_OBJECT(ui->mainwin), "destroy", gtk_main_quit, NULL);
107
108         hb = gtk_hbox_new(FALSE, 5);
109         gtk_container_add(GTK_CONTAINER(ui->mainwin), hb);
110
111         vb = gtk_vbox_new(FALSE, 5);
112         gtk_box_pack_start (GTK_BOX(hb), vb, FALSE, FALSE, 5);
113
114         btn = gtk_button_new_with_label(" D ");
115         gtk_box_pack_start (GTK_BOX(vb), btn, FALSE, FALSE, 10);
116
117         btn = gtk_button_new_with_label(" E ");
118         gtk_box_pack_start (GTK_BOX(vb), btn, FALSE, FALSE, 10);
119
120         btn = gtk_button_new_with_label(" F ");
121         gtk_box_pack_start (GTK_BOX(vb), btn, FALSE, FALSE, 10);
122
123         ui->darea = gtk_drawing_area_new ();
124         gtk_box_pack_start (GTK_BOX(hb), GTK_WIDGET(ui->darea), FALSE, FALSE, 5);
125         gtk_drawing_area_size (GTK_DRAWING_AREA(ui->darea), BITMAP_WIDTH, BITMAP_HEIGHT);
126
127         gtk_signal_connect (GTK_OBJECT (ui->darea), "expose_event", (GtkSignalFunc) expose_event, ui);
128         gtk_signal_connect (GTK_OBJECT (ui->darea), "configure_event", (GtkSignalFunc) configure_event, ui);
129         // gtk_signal_connect (GTK_OBJECT (drawing_area), "motion_notify_event", (GtkSignalFunc) motion_notify_event, ui);
130         // gtk_signal_connect (GTK_OBJECT (drawing_area), "button_press_event", (GtkSignalFunc) button_press_event, ui);
131
132         gtk_widget_set_events (GTK_WIDGET(ui->darea), GDK_EXPOSURE_MASK
133                 | GDK_LEAVE_NOTIFY_MASK
134                 | GDK_BUTTON_PRESS_MASK
135                 | GDK_POINTER_MOTION_MASK
136                 | GDK_POINTER_MOTION_HINT_MASK);
137
138         vb = gtk_vbox_new(FALSE, 5);
139         gtk_box_pack_start (GTK_BOX(hb), vb, FALSE, FALSE, 5);
140
141         btn = gtk_button_new_with_label(" A ");
142         gtk_box_pack_start (GTK_BOX(vb), btn, FALSE, FALSE, 10);
143
144         btn = gtk_button_new_with_label(" B ");
145         gtk_box_pack_start (GTK_BOX(vb), btn, FALSE, FALSE, 10);
146
147         btn = gtk_button_new_with_label(" C ");
148         gtk_box_pack_start (GTK_BOX(vb), btn, FALSE, FALSE, 10);
149
150         gtk_widget_show_all(ui->mainwin);
151 }
152
153 gboolean app_tmo_handler (gpointer userdata)
154 {
155         oswald_ui *ui = (oswald_ui *)userdata;
156
157         // fprintf(stderr, "tmo...\n");
158
159         if (ui->OnIdleScreen) {
160                 update_idle_time_date(ui);
161 #if 0
162         } else {
163                 // go back to idle screen after IDLE_TIMEOUT seconds
164                 if ((time(NULL) - ui->idle_tmo) > ui->conf.idle_tmo /*IDLE_TIMEOUT*/)
165                         create_main_screen(ui);
166 #endif
167         }
168
169         return TRUE;
170 }
171
172 int main(int argc , char ** argv)
173 {
174         oswald_ui ui;
175
176         gtk_init (&argc, &argv);
177
178         create_mainwin(&ui);
179
180         //set_pixel(&ui, 48, 48, TRUE);
181         // demo_time(&ui);
182         ui.OnIdleScreen = TRUE;
183         g_timeout_add_seconds(1, app_tmo_handler, &ui);
184
185         gtk_main ();
186         return 0;
187 }
188