:pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Fri Dec 14 20:46 CET 2001
[gnokii.git] / xgnokii / xgnokii_xkeyb.c
1 /*
2
3   $Id$
4
5   X G N O K I I
6
7   A Linux/Unix GUI for Nokia mobile phones.
8   Copyright (C) 1999 Pavel Janík ml., Hugh Blemings
9   & Ján Derfiòák <ja@mail.upjs.sk>.
10
11   Released under the terms of the GNU GPL, see file COPYING for more details.
12
13 */
14
15 #include <string.h>
16 #include <gtk/gtk.h>
17 #include "misc.h"
18 #include "links/fbus.h"
19 #include "xgnokii_common.h"
20 #include "xgnokii.h"
21 #include "xgnokii_lowlevel.h"
22 #include "xgnokii_xkeyb.h"
23
24 typedef struct {
25   int top_left_x, top_left_y;
26   int bottom_right_x, bottom_right_y;
27   int code;
28 } ButtonT;
29
30
31 static GtkWidget *GUI_XkeybWindow;
32 static GtkWidget *pixArea;
33 static GtkWidget *phonePixmap = NULL;
34 static ErrorDialog errorDialog = {NULL, NULL};
35 static ButtonT *button = NULL;
36
37 static ButtonT button_6110[30] = {
38   { 103,  91, 114, 107, 0x0d }, /* Power */
39   {  28, 240,  54, 263, 0x19 }, /* Menu */
40   {  84, 240, 110, 263, 0x1a }, /* Names */
41   {  58, 245,  82, 258, 0x17 }, /* Up */
42   {  55, 263,  85, 276, 0x18 }, /* Down */
43   {  22, 271,  50, 289, 0x0e }, /* Green */
44   {  91, 271, 115, 289, 0x0f }, /* Red */
45   {  18, 294,  44, 310, 0x01 }, /* 1 */
46   {  56, 294,  85, 310, 0x02 }, /* 2 */
47   {  98, 294, 121, 310, 0x03 }, /* 3 */
48   {  18, 317,  44, 333, 0x04 }, /* 4 */
49   {  56, 317,  85, 333, 0x05 }, /* 5 */
50   {  98, 317, 121, 333, 0x06 }, /* 6 */
51   {  18, 342,  44, 356, 0x07 }, /* 7 */
52   {  56, 342,  85, 356, 0x08 }, /* 8 */
53   {  98, 342, 121, 356, 0x09 }, /* 9 */
54   {  18, 365,  44, 380, 0x0c }, /* * */
55   {  56, 365,  85, 380, 0x0a }, /* 0 */
56   {  98, 365, 121, 380, 0x0b }, /* # */
57   {   1, 138,  10, 150, 0x10 } , /* Volume + */
58   {   1, 165,  10, 176, 0x11 } , /* Volume - */
59   {   0,   0,   0,   0, 0x00 }
60 };
61
62 static ButtonT button_6150[30] = {
63   {  99,  78, 114,  93, 0x0d }, /* Power */
64   {  20, 223,  49, 245, 0x19 }, /* Menu */
65   {  90, 223, 120, 245, 0x1a }, /* Names */
66   {  59, 230,  83, 247, 0x17 }, /* Up */
67   {  56, 249,  84, 265, 0x18 }, /* Down */
68   {  14, 254,  51, 273, 0x0e }, /* Green */
69   {  90, 255, 126, 273, 0x0f }, /* Red */
70   {  18, 281,  53, 299, 0x01 }, /* 1 */
71   {  55, 280,  86, 299, 0x02 }, /* 2 */
72   {  90, 281, 122, 299, 0x03 }, /* 3 */
73   {  18, 303,  53, 323, 0x04 }, /* 4 */
74   {  55, 303,  87, 323, 0x05 }, /* 5 */
75   {  90, 303, 122, 323, 0x06 }, /* 6 */
76   {  18, 327,  53, 346, 0x07 }, /* 7 */
77   {  53, 327,  87, 346, 0x08 }, /* 8 */
78   {  90, 327, 122, 346, 0x09 }, /* 9 */
79   {  18, 349,  53, 370, 0x0c }, /* * */
80   {  56, 349,  87, 370, 0x0a }, /* 0 */
81   {  98, 349, 122, 370, 0x0b }, /* # */
82   {   2, 131,  10, 147, 0x10 }, /* Volume + */
83   {   2, 155,  10, 173, 0x11 }, /* Volume - */
84   {   0,   0,   0,   0, 0x00 }
85 };
86
87 static ButtonT button_5110[30] = {
88   { 100,  85, 114,  99, 0x0d }, /* Power */
89   {  50, 240,  85, 265, 0x19 }, /* Menu */
90   {  20, 240,  45, 260, 0x1a }, /* Names */
91   { 100, 240, 117, 258, 0x17 }, /* Up */
92   {  93, 267, 112, 287, 0x18 }, /* Down */
93   {  14, 294,  44, 312, 0x01 }, /* 1 */
94   {  54, 294,  83, 312, 0x02 }, /* 2 */
95   {  94, 294, 122, 312, 0x03 }, /* 3 */
96   {  14, 320,  44, 338, 0x04 }, /* 4 */
97   {  54, 320,  83, 338, 0x05 }, /* 5 */
98   {  94, 320, 122, 338, 0x06 }, /* 6 */
99   {  14, 345,  44, 363, 0x07 }, /* 7 */
100   {  54, 345,  83, 363, 0x08 }, /* 8 */
101   {  94, 345, 122, 363, 0x09 }, /* 9 */
102   {  18, 374,  49, 389, 0x0c }, /* * */
103   {  53, 371,  82, 387, 0x0a }, /* 0 */
104   {  96, 374, 119, 389, 0x0b }, /* # */
105   {   0,   0,   0,   0, 0x00 }
106 };
107
108 static inline void Help1 (GtkWidget *w, gpointer data)
109 {
110   gchar *indx = g_strdup_printf ("/help/%s/windows/xkeyb/index.html", xgnokiiConfig.locale);
111   Help (w, indx);
112   g_free (indx);
113 }
114
115
116 static GtkWidget *GetPixmap (void)
117 {
118   GtkWidget *wpixmap;
119   GdkPixmap *pixmap;
120   GdkBitmap *mask;
121   gchar *file;
122
123   if (!strcmp (phoneMonitor.phone.model, "6110") ||
124       !strcmp (phoneMonitor.phone.model, "6120"))
125   {
126     button = button_6110;
127     file = g_strdup_printf ("%s%s", xgnokiiConfig.xgnokiidir, "/xpm/6110.xpm");
128   }
129   else if (!strcmp (phoneMonitor.phone.model, "6130") ||
130            !strcmp (phoneMonitor.phone.model, "6150") ||
131            !strcmp (phoneMonitor.phone.model, "616x") ||
132            !strcmp (phoneMonitor.phone.model, "6185") ||
133            !strcmp (phoneMonitor.phone.model, "6190"))
134   {
135     button = button_6150;
136     file = g_strdup_printf ("%s%s", xgnokiiConfig.xgnokiidir, "/xpm/6150.xpm");
137   }
138   else if (!strcmp (phoneMonitor.phone.model, "5110") ||
139            !strcmp (phoneMonitor.phone.model, "5130") ||
140            !strcmp (phoneMonitor.phone.model, "5160") ||
141            !strcmp (phoneMonitor.phone.model, "5190"))
142   {
143     button = button_5110;
144     file = g_strdup_printf ("%s%s", xgnokiiConfig.xgnokiidir, "/xpm/5110.xpm");
145   }
146   else
147     return NULL;
148
149   pixmap = gdk_pixmap_create_from_xpm (pixArea->window, &mask,
150                                        &pixArea->style->bg[GTK_STATE_NORMAL],
151                                        file);
152   g_free (file);
153
154   if (pixmap == NULL)
155     return NULL;
156     
157   wpixmap = gtk_pixmap_new (pixmap, mask);
158   
159   return wpixmap;
160 }
161
162
163 static inline void CloseXkeyb (GtkWidget *w, gpointer data)
164 {
165   gtk_widget_hide (GUI_XkeybWindow);
166 }
167
168
169 void GUI_ShowXkeyb (void)
170 {
171   if (phonePixmap == NULL)
172   {
173     phonePixmap = GetPixmap ();
174     if (phonePixmap != NULL)
175     {
176       gtk_fixed_put (GTK_FIXED (pixArea), phonePixmap, 0 , 0);
177       gtk_widget_show (phonePixmap);
178     }
179     else
180     {
181       gtk_label_set_text (GTK_LABEL (errorDialog.text),
182                           _("Cannot load background pixmap!"));
183       gtk_widget_show (errorDialog.dialog);
184     }
185   }
186   gtk_widget_show (GUI_XkeybWindow);
187 }
188
189
190 static gint ButtonEvent (GtkWidget *widget, GdkEventButton *event)
191 {
192   unsigned char req[] = {FBUS_FRAME_HEADER, 0x42, 0x01, 0x00, 0x01};
193   register gint i = 0;
194
195   if (button == NULL)
196     return TRUE;
197
198   if (event->button != 1)
199     return TRUE;
200
201   if (event->type == GDK_BUTTON_PRESS) 
202     req[4] = 0x01;
203   else if (event->type == GDK_BUTTON_RELEASE)
204     req[4] = 0x02;
205   else
206     return TRUE;
207
208 //  g_print ("%f %f\n", event->x, event->y);
209
210   while (button[i].top_left_x != 0) {
211     if (button[i].top_left_x <= event->x &&
212         event->x <= button[i].bottom_right_x &&
213         button[i].top_left_y <= event->y &&
214         event->y <= button[i].bottom_right_y)
215     {
216       PhoneEvent *e = g_malloc (sizeof (PhoneEvent));
217       
218       req[5]=button[i].code;
219       e->event = Event_SendKeyStroke;
220       e->data = g_memdup (req, sizeof (req));
221       GUI_InsertEvent (e);
222     }
223
224     i++;
225   }
226
227   return TRUE;
228 }
229
230
231 static GtkItemFactoryEntry menu_items[] = {
232   {NULL,        NULL,           NULL, 0, "<Branch>"},
233   {NULL,        "<control>W",   CloseXkeyb, 0, NULL},
234   {NULL,        NULL,           NULL, 0, "<LastBranch>"},
235   {NULL,        NULL,           Help1, 0, NULL},
236   {NULL,        NULL,           GUI_ShowAbout, 0, NULL},
237 };
238
239 static void InitMainMenu (void)
240 {
241   menu_items[0].path = g_strdup (_("/_File"));
242   menu_items[1].path = g_strdup (_("/File/_Close"));
243   menu_items[2].path = g_strdup (_("/_Help"));
244   menu_items[3].path = g_strdup (_("/Help/_Help"));
245   menu_items[4].path = g_strdup (_("/Help/_About"));
246 }
247
248
249 void GUI_CreateXkeybWindow (void)
250 {
251   int nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);
252   GtkItemFactory *item_factory;
253   GtkAccelGroup *accel_group;
254   GtkWidget *menubar;
255   GtkWidget *main_vbox;
256
257   InitMainMenu ();
258   GUI_XkeybWindow = gtk_window_new (GTK_WINDOW_TOPLEVEL);
259   gtk_window_set_wmclass (GTK_WINDOW (GUI_XkeybWindow), "XkeybWindow", "Xgnokii");
260   gtk_window_set_title (GTK_WINDOW (GUI_XkeybWindow), _("XGnokii Keyboard"));
261   //gtk_widget_set_usize (GTK_WIDGET (GUI_XkeybWindow), 436, 220);
262   gtk_signal_connect (GTK_OBJECT (GUI_XkeybWindow), "delete_event",
263                       GTK_SIGNAL_FUNC (DeleteEvent), NULL);
264   gtk_widget_realize (GUI_XkeybWindow);
265
266   accel_group = gtk_accel_group_new ();
267   item_factory = gtk_item_factory_new (GTK_TYPE_MENU_BAR, "<main>", 
268                                        accel_group);
269
270   gtk_item_factory_create_items (item_factory, nmenu_items, menu_items, NULL);
271
272   gtk_accel_group_attach (accel_group, GTK_OBJECT (GUI_XkeybWindow));
273
274   /* Finally, return the actual menu bar created by the item factory. */ 
275   menubar = gtk_item_factory_get_widget (item_factory, "<main>");
276
277   main_vbox = gtk_vbox_new (FALSE, 1);
278   gtk_container_border_width (GTK_CONTAINER (main_vbox), 1);
279   gtk_container_add (GTK_CONTAINER (GUI_XkeybWindow), main_vbox);
280   gtk_widget_show (main_vbox);
281
282   gtk_box_pack_start (GTK_BOX (main_vbox), menubar, FALSE, FALSE, 0);
283   gtk_widget_show (menubar);
284
285   pixArea = gtk_fixed_new ();
286   gtk_signal_connect (GTK_OBJECT (pixArea), "button_press_event",
287                       (GtkSignalFunc) ButtonEvent, NULL);
288   gtk_signal_connect (GTK_OBJECT (pixArea), "button_release_event",
289                       (GtkSignalFunc) ButtonEvent, NULL);
290   gtk_widget_set_events (pixArea, GDK_EXPOSURE_MASK
291                          | GDK_LEAVE_NOTIFY_MASK
292                          | GDK_BUTTON_PRESS_MASK
293                          | GDK_BUTTON_RELEASE_MASK
294                          | GDK_POINTER_MOTION_MASK
295                          | GDK_POINTER_MOTION_HINT_MASK);
296
297   gtk_box_pack_start (GTK_BOX (main_vbox), pixArea, FALSE, FALSE, 3);
298   gtk_widget_show (pixArea);
299
300   CreateErrorDialog (&errorDialog, GUI_XkeybWindow);
301 }