Debian Codegraph @

gtk/shell_skin.cc

raw
1 ///////////////////////////////////////////////////////////////////////////////
2 // Free42 -- an HP-42S calculator simulator
3 // Copyright (C) 2004-2026 Thomas Okken
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License, version 2,
7 // as published by the Free Software Foundation.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, see http://www.gnu.org/licenses/.
16 ///////////////////////////////////////////////////////////////////////////////
17
18 #include <gtk/gtk.h>
19 #include <ctype.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/types.h>
24 #include <dirent.h>
25 #include <math.h>
26 #include <string>
27 #include <set>
28
29 #include <string>
30 #include <vector>
31 #include <set>
32
33 #include "shell_skin.h"
34 #include "shell_main.h"
35 #include "shell_loadimage.h"
36 #include "core_main.h"
37
38 using std::string;
39 using std::vector;
40 using std::set;
41
42
43 /**************************/
44 /* Skin description stuff */
45 /**************************/
46
47 struct SkinPoint {
48 int x, y;
49 };
50
51 struct SkinRect {
52 int x, y, width, height;
53 };
54
55 struct SkinKey {
56 int code, shifted_code;
57 SkinRect sens_rect;
58 SkinRect disp_rect;
59 SkinPoint src;
60 };
61
62 #define SKIN_MAX_MACRO_LENGTH 63
63 #define SKIN_MAX_HALF_MACRO_LENGTH ((SKIN_MAX_MACRO_LENGTH - 1) / 2)
64
65 struct SkinMacro {
66 int code;
67 bool isName;
68 char secondType; // 0:none, 1:name, 2:text
69 unsigned char macro[SKIN_MAX_HALF_MACRO_LENGTH + 1];
70 unsigned char macro2[SKIN_MAX_HALF_MACRO_LENGTH + 1];
71 SkinMacro *next;
72 };
73
74 struct SkinAnnunciator {
75 SkinRect disp_rect;
76 SkinPoint src;
77 };
78
79 static SkinRect skin;
80 static SkinPoint display_loc;
81 static double display_scale_x;
82 static double display_scale_y;
83 static bool display_scale_int;
84 static SkinColor display_bg, display_fg;
85 static SkinKey *keylist = NULL;
86 static int nkeys = 0;
87 static int keys_cap = 0;
88 static SkinMacro *macrolist = NULL;
89 static SkinAnnunciator annunciators[7];
90
91 static FILE *external_file;
92 static long builtin_length;
93 static long builtin_pos;
94 static const unsigned char *builtin_file;
95
96 static GdkPixbuf *skin_image = NULL;
97 static int skin_y;
98 static int skin_type;
99 static const SkinColor *skin_cmap;
100
101 static char disp_bits[272];
102
103 static keymap_entry *keymap = NULL;
104 static int keymap_length;
105
106 static int window_width, window_height;
107
108
109 /**********************************************************/
110 /* Linked-in skins; defined in the skins.c, which in turn */
111 /* is generated by skin2c.c under control of skin2c.conf */
112 /**********************************************************/
113
114 extern const int skin_count;
115 extern const char * const skin_name[];
116 extern const long skin_layout_size[];
117 extern const unsigned char * const skin_layout_data[];
118 extern const long skin_bitmap_size[];
119 extern const unsigned char * const skin_bitmap_data[];
120
121
122 /*******************/
123 /* Local functions */
124 /*******************/
125
126 static void addMenuItem(GtkMenu *menu, const char *name, bool enabled);
127 static void selectSkinCB(GtkWidget *w, gpointer cd);
128 static bool skin_open(const char *name, bool open_layout, bool force_builtin);
129 static int skin_gets(char *buf, int buflen);
130 static void skin_close();
131
132
133 static void addMenuItem(GtkMenu *menu, const char *name, bool enabled) {
134 bool checked = false;
135 if (enabled) {
136 if (state.skinName[0] == 0) {
137 strcpy(state.skinName, name);
138 checked = true;
139 } else if (strcmp(state.skinName, name) == 0)
140 checked = true;
141 }
142
143 GtkWidget *w = gtk_check_menu_item_new_with_label(name);
144 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(w), checked);
145 gtk_widget_set_sensitive(w, enabled);
146
147 g_signal_connect(G_OBJECT(w), "activate",
148 G_CALLBACK(selectSkinCB), NULL);
149
150 gtk_menu_shell_append(GTK_MENU_SHELL(menu), w);
151 gtk_widget_show(w);
152 }
153
154 static void selectSkinCB(GtkWidget *w, gpointer cd) {
155 /* The gtk_check_menu_item_set_active() call causes this callback
156 * to be re-entered; this bit of logic is to prevent getting stuck
157 * in infinite recursion.
158 */
159 static bool busy = false;
160 if (busy)
161 return;
162 busy = true;
163
164 GtkWidget *skin_menu = gtk_widget_get_parent(w);
165 GList *children = gtk_container_get_children(GTK_CONTAINER(skin_menu));
166 GList *item = children;
167 while (item != NULL) {
168 GtkWidget *mi = GTK_WIDGET(item->data);
169 if (GTK_IS_CHECK_MENU_ITEM(mi))
170 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(mi), mi == w);
171 item = item->next;
172 }
173 g_list_free(children);
174
175 busy = false;
176
177 const char *label = gtk_menu_item_get_label(GTK_MENU_ITEM(w));
178 strcpy(state.skinName, label);
179 int sw, sh;
180 skin_load(&sw, &sh);
181 core_repaint_display();
182
183 skin_set_window_size(sw, sh);
184 gtk_window_resize(GTK_WINDOW(mainwindow), sw, sh + menu_bar_height);
185 gtk_widget_queue_draw(calc_widget);
186 }
187
188 static bool skin_open(const char *name, bool open_layout, bool force_builtin) {
189 if (!force_builtin) {
190 const char *suffix = open_layout ? ".layout" : ".gif";
191 // Try Free42 dir first...
192 string fname = string(free42dirname) + "/" + name + suffix;
193 external_file = fopen(fname.c_str(), "r");
194 if (external_file != NULL)
195 return true;
196 // Next, shared dirs...
197 const char *xdg_data_dirs = getenv("XDG_DATA_DIRS");
198 if (xdg_data_dirs == NULL || xdg_data_dirs[0] == 0)
199 xdg_data_dirs = "/usr/local/share:/usr/share";
200 char *buf = (char *) malloc(strlen(xdg_data_dirs) + 1);
201 strcpy(buf, xdg_data_dirs);
202 char *tok = strtok(buf, ":");
203 while (tok != NULL) {
204 string dirname = tok;
205 string fname = dirname + "/free42/" + name + suffix;
206 external_file = fopen(fname.c_str(), "r");
207 if (external_file != NULL) {
208 free(buf);
209 return true;
210 }
211 fname = dirname + "/free42/skins/" + name + suffix;
212 external_file = fopen(fname.c_str(), "r");
213 if (external_file != NULL) {
214 free(buf);
215 return true;
216 }
217 tok = strtok(NULL, ":");
218 }
219 free(buf);
220 }
221
222 // Look for built-in skin last
223 for (int i = 0; i < skin_count; i++) {
224 if (strcmp(name, skin_name[i]) == 0) {
225 external_file = NULL;
226 builtin_pos = 0;
227 if (open_layout) {
228 builtin_length = skin_layout_size[i];
229 builtin_file = skin_layout_data[i];
230 } else {
231 builtin_length = skin_bitmap_size[i];
232 builtin_file = skin_bitmap_data[i];
233 }
234 return true;
235 }
236 }
237
238 // Nothing found.
239 return false;
240 }
241
242 int skin_getchar() {
243 if (external_file != NULL)
244 return fgetc(external_file);
245 else if (builtin_pos < builtin_length)
246 return builtin_file[builtin_pos++];
247 else
248 return EOF;
249 }
250
251 static int skin_gets(char *buf, int buflen) {
252 int p = 0;
253 int eof = -1;
254 int comment = 0;
255 while (p < buflen - 1) {
256 int c = skin_getchar();
257 if (eof == -1)
258 eof = c == EOF;
259 if (c == EOF || c == '\n' || c == '\r')
260 break;
261 /* Remove comments */
262 if (c == '#')
263 comment = 1;
264 if (comment)
265 continue;
266 /* Suppress leading spaces */
267 if (p == 0 && isspace(c))
268 continue;
269 buf[p++] = c;
270 }
271 buf[p++] = 0;
272 return p > 1 || !eof;
273 }
274
275 static void skin_close() {
276 if (external_file != NULL)
277 fclose(external_file);
278 }
279
280 static void scan_skin_dir(const char *dirname, set<string> &names) {
281 DIR *dir = opendir(dirname);
282 if (dir == NULL)
283 return;
284
285 struct dirent *dent;
286 while ((dent = readdir(dir)) != NULL) {
287 int namelen = strlen(dent->d_name);
288 if (namelen < 7)
289 continue;
290 if (strcmp(dent->d_name + namelen - 7, ".layout") != 0)
291 continue;
292 string name = dent->d_name;
293 name.erase(namelen - 7);
294 names.insert(name);
295 }
296 closedir(dir);
297 }
298
299 void skin_menu_update(GtkWidget *w) {
300 GtkMenu *skin_menu = (GtkMenu *) gtk_menu_item_get_submenu(GTK_MENU_ITEM(w));
301 GList *children = gtk_container_get_children(GTK_CONTAINER(skin_menu));
302 GList *item = children;
303 while (item != NULL) {
304 gtk_widget_destroy(GTK_WIDGET(item->data));
305 item = item->next;
306 }
307 g_list_free(children);
308
309 set<string> shared_skins;
310 const char *xdg_data_dirs = getenv("XDG_DATA_DIRS");
311 if (xdg_data_dirs == NULL || xdg_data_dirs[0] == 0)
312 xdg_data_dirs = "/usr/local/share:/usr/share";
313 char *buf = (char *) malloc(strlen(xdg_data_dirs) + 1);
314 strcpy(buf, xdg_data_dirs);
315 char *tok = strtok(buf, ":");
316 while (tok != NULL) {
317 string dirname = tok;
318 scan_skin_dir((dirname + "/free42").c_str(), shared_skins);
319 scan_skin_dir((dirname + "/free42/skins").c_str(), shared_skins);
320 tok = strtok(NULL, ":");
321 }
322 free(buf);
323
324 set<string> private_skins;
325 scan_skin_dir(free42dirname, private_skins);
326
327 for (int i = 0; i < skin_count; i++) {
328 const char *name = skin_name[i];
329 bool enabled = private_skins.find(name) == private_skins.end()
330 && shared_skins.find(name) == shared_skins.end();
331 addMenuItem(skin_menu, name, enabled);
332 }
333
334 if (!shared_skins.empty()) {
335 GtkWidget *w = gtk_separator_menu_item_new();
336 gtk_menu_shell_append(GTK_MENU_SHELL(skin_menu), w);
337 gtk_widget_show(w);
338
339 for (set<string>::const_iterator i = shared_skins.begin(); i != shared_skins.end(); i++) {
340 const char *name = i->c_str();
341 bool enabled = private_skins.find(name) == private_skins.end();
342 addMenuItem(skin_menu, name, enabled);
343 }
344 }
345
346 if (!private_skins.empty()) {
347 GtkWidget *w = gtk_separator_menu_item_new();
348 gtk_menu_shell_append(GTK_MENU_SHELL(skin_menu), w);
349 gtk_widget_show(w);
350
351 for (set<string>::const_iterator i = private_skins.begin(); i != private_skins.end(); i++)
352 addMenuItem(skin_menu, i->c_str(), true);
353 }
354 }
355
356 void skin_load(int *width, int *height) {
357 char line[1024];
358 bool force_builtin = false;
359
360 if (state.skinName[0] == 0) {
361 fallback_on_1st_builtin_skin:
362 strcpy(state.skinName, skin_name[0]);
363 force_builtin = true;
364 }
365
366 /*************************/
367 /* Load skin description */
368 /*************************/
369
370 if (!skin_open(state.skinName, 1, force_builtin))
371 goto fallback_on_1st_builtin_skin;
372
373 if (keylist != NULL)
374 free(keylist);
375 keylist = NULL;
376 nkeys = 0;
377 keys_cap = 0;
378
379 while (macrolist != NULL) {
380 SkinMacro *m = macrolist->next;
381 free(macrolist);
382 macrolist = m;
383 }
384
385 if (keymap != NULL)
386 free(keymap);
387 keymap = NULL;
388 keymap_length = 0;
389 int kmcap = 0;
390
391 int lineno = 0;
392
393 while (skin_gets(line, 1024)) {
394 lineno++;
395 if (*line == 0)
396 continue;
397 if (strncasecmp(line, "skin:", 5) == 0) {
398 int x, y, width, height;
399 if (sscanf(line + 5, " %d,%d,%d,%d", &x, &y, &width, &height) == 4){
400 skin.x = x;
401 skin.y = y;
402 skin.width = width;
403 skin.height = height;
404 }
405 } else if (strncasecmp(line, "display:", 8) == 0) {
406 int x, y;
407 double xscale, yscale;
408 unsigned long bg, fg;
409 if (sscanf(line + 8, " %d,%d %lf %lf %lx %lx", &x, &y,
410 &xscale, &yscale, &bg, &fg) == 6) {
411 display_loc.x = x;
412 display_loc.y = y;
413 display_scale_x = xscale;
414 display_scale_y = yscale;
415 display_scale_int = xscale == (int) xscale && yscale == (int) yscale;
416 display_bg.r = (unsigned char) (bg >> 16);
417 display_bg.g = (unsigned char) (bg >> 8);
418 display_bg.b = (unsigned char) bg;
419 display_fg.r = (unsigned char) (fg >> 16);
420 display_fg.g = (unsigned char) (fg >> 8);
421 display_fg.b = (unsigned char) fg;
422 }
423 } else if (strncasecmp(line, "key:", 4) == 0) {
424 char keynumbuf[20];
425 int keynum, shifted_keynum;
426 int sens_x, sens_y, sens_width, sens_height;
427 int disp_x, disp_y, disp_width, disp_height;
428 int act_x, act_y;
429 if (sscanf(line + 4, " %s %d,%d,%d,%d %d,%d,%d,%d %d,%d",
430 keynumbuf,
431 &sens_x, &sens_y, &sens_width, &sens_height,
432 &disp_x, &disp_y, &disp_width, &disp_height,
433 &act_x, &act_y) == 11) {
434 int n = sscanf(keynumbuf, "%d,%d", &keynum, &shifted_keynum);
435 if (n > 0) {
436 if (n == 1)
437 shifted_keynum = keynum;
438 SkinKey *key;
439 if (nkeys == keys_cap) {
440 keys_cap += 50;
441 keylist = (SkinKey *)
442 realloc(keylist, keys_cap * sizeof(SkinKey));
443 // TODO - handle memory allocation failure
444 }
445 key = keylist + nkeys;
446 key->code = keynum;
447 key->shifted_code = shifted_keynum;
448 key->sens_rect.x = sens_x;
449 key->sens_rect.y = sens_y;
450 key->sens_rect.width = sens_width;
451 key->sens_rect.height = sens_height;
452 key->disp_rect.x = disp_x;
453 key->disp_rect.y = disp_y;
454 key->disp_rect.width = disp_width;
455 key->disp_rect.height = disp_height;
456 key->src.x = act_x;
457 key->src.y = act_y;
458 nkeys++;
459 }
460 }
461 } else if (strncasecmp(line, "macro:", 6) == 0) {
462 char *quot1 = strchr(line + 6, '"');
463 if (quot1 != NULL) {
464 char *quot2 = strchr(quot1 + 1, '"');
465 if (quot2 != NULL) {
466 long len = quot2 - quot1 - 1;
467 if (len > SKIN_MAX_HALF_MACRO_LENGTH)
468 len = SKIN_MAX_HALF_MACRO_LENGTH;
469 int n;
470 if (sscanf(line + 6, "%d", &n) == 1 && n >= 38 && n <= 255) {
471 SkinMacro *macro = (SkinMacro *) malloc(sizeof(SkinMacro));
472 // TODO - handle memory allocation failure
473 macro->code = n;
474 macro->isName = true;
475 memcpy(macro->macro, quot1 + 1, len);
476 macro->macro[len] = 0;
477 macro->secondType = 0;
478 quot1 = strchr(quot2 + 1, '"');
479 if (quot1 == NULL)
480 quot1 = strchr(quot2 + 1, '\'');
481 if (quot1 != NULL) {
482 quot2 = strchr(quot1 + 1, *quot1);
483 if (quot2 != NULL) {
484 len = quot2 - quot1 - 1;
485 if (len > SKIN_MAX_HALF_MACRO_LENGTH)
486 len = SKIN_MAX_HALF_MACRO_LENGTH;
487 memcpy(macro->macro2, quot1 + 1, len);
488 macro->macro2[len] = 0;
489 macro->secondType = *quot1 == '"' ? 1 : 2;
490 }
491 }
492 macro->next = macrolist;
493 macrolist = macro;
494 }
495 }
496 } else {
497 char *tok = strtok(line + 6, " \t");
498 int len = 0;
499 SkinMacro *macro = NULL;
500 while (tok != NULL) {
501 char *endptr;
502 long n = strtol(tok, &endptr, 10);
503 if (*endptr != 0) {
504 /* Not a proper number; ignore this macro */
505 if (macro != NULL) {
506 free(macro);
507 macro = NULL;
508 break;
509 }
510 }
511 if (macro == NULL) {
512 if (n < 38 || n > 255)
513 /* Macro code out of range; ignore this macro */
514 break;
515 macro = (SkinMacro *) malloc(sizeof(SkinMacro));
516 // TODO - handle memory allocation failure
517 macro->code = n;
518 macro->isName = false;
519 } else if (len < SKIN_MAX_MACRO_LENGTH) {
520 if (n < 1 || n > 37) {
521 /* Key code out of range; ignore this macro */
522 free(macro);
523 macro = NULL;
524 break;
525 }
526 macro->macro[len++] = n;
527 }
528 tok = strtok(NULL, " \t");
529 }
530 if (macro != NULL) {
531 macro->macro[len++] = 0;
532 macro->next = macrolist;
533 macrolist = macro;
534 }
535 }
536 } else if (strncasecmp(line, "annunciator:", 12) == 0) {
537 int annnum;
538 int disp_x, disp_y, disp_width, disp_height;
539 int act_x, act_y;
540 if (sscanf(line + 12, " %d %d,%d,%d,%d %d,%d",
541 &annnum,
542 &disp_x, &disp_y, &disp_width, &disp_height,
543 &act_x, &act_y) == 7) {
544 if (annnum >= 1 && annnum <= 7) {
545 SkinAnnunciator *ann = annunciators + (annnum - 1);
546 ann->disp_rect.x = disp_x;
547 ann->disp_rect.y = disp_y;
548 ann->disp_rect.width = disp_width;
549 ann->disp_rect.height = disp_height;
550 ann->src.x = act_x;
551 ann->src.y = act_y;
552 }
553 }
554 } else if (strncasecmp(line, "gtkkey:", 7) == 0) {
555 keymap_entry *entry = parse_keymap_entry(line + 7, lineno);
556 if (entry != NULL) {
557 if (keymap_length == kmcap) {
558 kmcap += 50;
559 keymap = (keymap_entry *)
560 realloc(keymap, kmcap * sizeof(keymap_entry));
561 // TODO - handle memory allocation failure
562 }
563 memcpy(keymap + (keymap_length++), entry, sizeof(keymap_entry));
564 }
565 }
566 }
567
568 skin_close();
569
570 /********************/
571 /* Load skin bitmap */
572 /********************/
573
574 if (!skin_open(state.skinName, 0, force_builtin))
575 goto fallback_on_1st_builtin_skin;
576
577 /* shell_loadimage() calls skin_getchar() to load the image from the
578 * compiled-in or on-disk file; it calls skin_init_image(),
579 * skin_put_pixels(), and skin_finish_image() to create the in-memory
580 * representation.
581 */
582 bool success = shell_loadimage();
583 skin_close();
584
585 if (!success)
586 goto fallback_on_1st_builtin_skin;
587
588 *width = skin.width;
589 *height = skin.height;
590
591 /*********************************/
592 /* Initialize the display bitmap */
593 /*********************************/
594
595 memset(disp_bits, 0, 272);
596 }
597
598 bool skin_init_image(int type, int ncolors, const SkinColor *colors,
599 int width, int height) {
600 if (skin_image != NULL) {
601 g_object_unref(skin_image);
602 skin_image = NULL;
603 }
604
605 skin_image = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, width, height);
606
607 skin_y = 0;
608 skin_type = type;
609 skin_cmap = colors;
610 return true;
611 }
612
613 void skin_put_pixels(unsigned const char *data) {
614 guchar *pix = gdk_pixbuf_get_pixels(skin_image);
615 int bytesperline = gdk_pixbuf_get_rowstride(skin_image);
616 int width = gdk_pixbuf_get_width(skin_image);
617 guchar *p = pix + skin_y * bytesperline;
618
619 if (skin_type == IMGTYPE_MONO) {
620 for (int x = 0; x < width; x++) {
621 guchar c;
622 if ((data[x >> 3] & (1 << (x & 7))) == 0)
623 c = 0;
624 else
625 c = 255;
626 *p++ = c;
627 *p++ = c;
628 *p++ = c;
629 }
630 } else if (skin_type == IMGTYPE_GRAY) {
631 for (int x = 0; x < width; x++) {
632 guchar c = data[x];
633 *p++ = c;
634 *p++ = c;
635 *p++ = c;
636 }
637 } else if (skin_type == IMGTYPE_COLORMAPPED) {
638 for (int x = 0; x < width; x++) {
639 guchar c = data[x];
640 *p++ = skin_cmap[c].r;
641 *p++ = skin_cmap[c].g;
642 *p++ = skin_cmap[c].b;
643 }
644 } else { // skin_type == IMGTYPE_TRUECOLOR
645 int xx = 0;
646 for (int x = 0; x < width; x++) {
647 *p++ = data[xx++];
648 *p++ = data[xx++];
649 *p++ = data[xx++];
650 }
651 }
652
653 skin_y++;
654 }
655
656 void skin_finish_image() {
657 // Nothing to do.
658 }
659
660 void skin_repaint(cairo_t *cr) {
661 cairo_save(cr);
662 gdk_cairo_set_source_pixbuf(cr, skin_image, -skin.x, -skin.y);
663 cairo_rectangle(cr, 0, 0, skin.width, skin.height);
664 cairo_clip(cr);
665 cairo_paint(cr);
666 cairo_restore(cr);
667 }
668
669 void skin_repaint_annunciator(cairo_t *cr, int which) {
670 SkinAnnunciator *ann = annunciators + (which - 1);
671 cairo_save(cr);
672 gdk_cairo_set_source_pixbuf(cr, skin_image,
673 ann->disp_rect.x - ann->src.x,
674 ann->disp_rect.y - ann->src.y);
675 cairo_rectangle(cr, ann->disp_rect.x, ann->disp_rect.y, ann->disp_rect.width, ann->disp_rect.height);
676 cairo_clip(cr);
677 cairo_paint(cr);
678 cairo_restore(cr);
679 }
680
681 struct KeyShortcutInfo {
682 int x, y, width, height;
683 string unshifted, shifted;
684 KeyShortcutInfo *next;
685
686 KeyShortcutInfo(SkinKey *k) {
687 x = k->sens_rect.x;
688 y = k->sens_rect.y;
689 width = k->sens_rect.width;
690 height = k->sens_rect.height;
691 unshifted = "";
692 shifted = "";
693 }
694
695 bool sameRect(SkinKey *that) {
696 return x == that->sens_rect.x
697 && y == that->sens_rect.y
698 && width == that->sens_rect.width
699 && height == that->sens_rect.height;
700 }
701
702 void add(string entryStr, bool shifted) {
703 string *str = shifted ? &this->shifted : &this->unshifted;
704 *str = entryStr + " " + *str;
705 }
706
707 string text() {
708 string u, s;
709 if (unshifted.size() == 0)
710 u = "n/a";
711 else
712 u = unshifted.substr(0, unshifted.size() - 1);
713 if (shifted.size() == 0)
714 s = "n/a";
715 else
716 s = shifted.substr(0, shifted.size() - 1);
717 return s + "\n" + u;
718 }
719 };
720
721 static string entry_to_text(keymap_entry *e) {
722 string c;
723 if (e->keyval >= 33 && e->keyval <= 126) {
724 /* Corresponds to the printable ASCII range */
725 char s[2] = { (char) e->keyval, 0 };
726 c = s;
727 } else {
728 switch (e->keyval) {
729 case GDK_KEY_Escape: c = "Esc"; break;
730 case GDK_KEY_BackSpace: c = "\342\214\253"; break;
731 case GDK_KEY_Up: c = "\342\206\221"; break;
732 case GDK_KEY_Down: c = "\342\206\223"; break;
733 case GDK_KEY_Left: c = "\342\206\220"; break;
734 case GDK_KEY_Right: c = "\342\206\222"; break;
735 case GDK_KEY_Insert: c = "Ins"; break;
736 case GDK_KEY_Delete: c = "\342\214\246"; break;
737 case GDK_KEY_Page_Up: c = "PgUp"; break;
738 case GDK_KEY_Page_Down: c = "PgDn"; break;
739 default: c = string(gdk_keyval_name(e->keyval));
740 }
741 }
742 string mods = "";
743 bool printable = !e->ctrl && c.size() == 1 && c[0] >= 33 && c[0] <= 126;
744 if (e->ctrl)
745 mods += "^";
746 if (e->alt)
747 mods += "\342\214\245";
748 if (e->shift && !printable)
749 mods += "\342\207\247";
750 return mods + c;
751 }
752
753 static KeyShortcutInfo *get_shortcut_info() {
754 KeyShortcutInfo *head = NULL;
755 set<string> seen;
756 for (int km = 0; km < 2; km++) {
757 keymap_entry *kmap;
758 int kmap_len;
759 if (km == 0) {
760 kmap = keymap;
761 kmap_len = keymap_length;
762 } else
763 get_keymap(&kmap, &kmap_len);
764 for (int i = kmap_len - 1; i >= 0; i--) {
765 keymap_entry *e = kmap + i;
766 if (e->cshift)
767 continue;
768 int key;
769 bool shifted;
770 if (e->macro[1] == 0) {
771 key = e->macro[0];
772 shifted = false;
773 } else if (e->macro[0] == 28 && e->macro[2] == 0) {
774 key = e->macro[1];
775 shifted = true;
776 } else
777 continue;
778 SkinKey *k = NULL;
779 for (int j = 0; j < nkeys; j++) {
780 k = keylist + j;
781 if (key == k->code)
782 break;
783 if (key == k->shifted_code) {
784 shifted = true;
785 break;
786 }
787 k = NULL;
788 }
789 if (k == NULL)
790 continue;
791 string entryStr = entry_to_text(e);
792 if (seen.find(entryStr) != seen.end())
793 continue;
794 seen.insert(entryStr);
795 for (KeyShortcutInfo *p = head; p != NULL; p = p->next) {
796 if (p->sameRect(k)) {
797 p->add(entryStr, shifted);
798 goto endloop;
799 }
800 }
801 KeyShortcutInfo *ki;
802 ki = new KeyShortcutInfo(k);
803 ki->add(entryStr, shifted);
804 ki->next = head;
805 head = ki;
806 endloop:;
807 }
808 }
809 return head;
810 }
811
812 static void draw_text_in_rect(cairo_t *cr, const char *text, int x, int y, int width, int height) {
813 cairo_font_extents_t extents;
814 cairo_font_extents(cr, &extents);
815 cairo_save(cr);
816 cairo_rectangle(cr, x, y, width, height);
817 cairo_clip(cr);
818
819 size_t len = strlen(text);
820 char *buf = (char *) malloc(len + 1);
821 if (buf == NULL)
822 return;
823 size_t pos = 0;
824 double ypos = y + extents.ascent;
825
826 /* This is not efficient, but bear in mind we're only using this for pretty short chunks of text */
827 while (pos < len) {
828 strcpy(buf, text + pos);
829 char *lf = strchr(buf, '\n');
830 if (lf != NULL)
831 *lf = 0;
832 int extra = 1;
833 while (true) {
834 cairo_text_extents_t ext;
835 cairo_text_extents(cr, buf, &ext);
836 if (ext.width <= width)
837 break;
838 /* Too long; look for a word break */
839 char *sp = strrchr(buf, ' ');
840 if (sp != NULL && sp > buf) {
841 *sp = 0;
842 continue;
843 }
844 /* The whole line is one word; trim one char at a time until it's short enough */
845 size_t last = strlen(buf) - 1;
846 extra = 0;
847 do {
848 while (last > 0 && (buf[last] & 0xc0) == 0x80)
849 last--;
850 if (last == 0)
851 break;
852 buf[last--] = 0;
853 cairo_text_extents(cr, buf, &ext);
854 } while (ext.width > width);
855 }
856 cairo_move_to(cr, x, ypos);
857 cairo_show_text(cr, buf);
858 ypos += extents.height;
859 pos += strlen(buf) + extra;
860 }
861
862 free(buf);
863 cairo_restore(cr);
864 }
865
866 void skin_draw_keyboard_shortcuts(cairo_t *cr) {
867 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.5);
868 cairo_rectangle(cr, 0, 0, skin.width, skin.height);
869 cairo_fill(cr);
870 KeyShortcutInfo *ksinfo = get_shortcut_info();
871 cairo_select_font_face(cr, "sans-serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
872 cairo_set_font_size(cr, sqrt(((double) skin.width) * skin.height) / 42);
873 while (ksinfo != NULL) {
874 cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.5);
875 cairo_rectangle(cr, ksinfo->x + 2, ksinfo->y + 2, ksinfo->width - 4, ksinfo->height - 4);
876 cairo_fill(cr);
877 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
878 draw_text_in_rect(cr, ksinfo->text().c_str(), ksinfo->x + 4, ksinfo->y + 4, ksinfo->width - 8, ksinfo->height - 8);
879 KeyShortcutInfo *next = ksinfo->next;
880 delete ksinfo;
881 ksinfo = next;
882 }
883 }
884
885 void skin_make_darker(cairo_t *cr) {
886 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.15);
887 cairo_rectangle(cr, 0, 0, skin.width, skin.height);
888 cairo_fill(cr);
889 }
890
891 static void scaled_gdk_window_invalidate_rect(GdkWindow *win, const GdkRectangle *rect, gboolean invalidate_children) {
892 GdkRectangle scaled_rect;
893 scaled_rect.x = (int) (((double) rect->x) * window_width / skin.width);
894 scaled_rect.y = (int) (((double) rect->y) * window_height / skin.height);
895 scaled_rect.width = (int) ceil(((double) rect->width) * window_width / skin.width);
896 scaled_rect.height = (int) ceil(((double) rect->height) * window_height / skin.height);
897 gdk_window_invalidate_rect(win, &scaled_rect, invalidate_children);
898 }
899
900 void skin_invalidate_annunciator(GdkWindow *win, int which) {
901 SkinAnnunciator *ann = annunciators + (which - 1);
902 GdkRectangle clip;
903 clip.x = ann->disp_rect.x;
904 clip.y = ann->disp_rect.y;
905 clip.width = ann->disp_rect.width;
906 clip.height = ann->disp_rect.height;
907 scaled_gdk_window_invalidate_rect(win, &clip, FALSE);
908 }
909
910 void skin_find_key(int x, int y, bool cshift, int *skey, int *ckey) {
911 int i;
912 if (core_menu()
913 && x >= display_loc.x
914 && x < display_loc.x + 131 * display_scale_x
915 && y >= display_loc.y + 9 * display_scale_y
916 && y < display_loc.y + 16 * display_scale_y) {
917 int softkey = (x - display_loc.x) / (22 * display_scale_x) + 1;
918 *skey = -1 - softkey;
919 *ckey = softkey;
920 return;
921 }
922 for (i = 0; i < nkeys; i++) {
923 SkinKey *k = keylist + i;
924 int rx = x - k->sens_rect.x;
925 int ry = y - k->sens_rect.y;
926 if (rx >= 0 && rx < k->sens_rect.width
927 && ry >= 0 && ry < k->sens_rect.height) {
928 *skey = i;
929 *ckey = cshift ? k->shifted_code : k->code;
930 return;
931 }
932 }
933 *skey = -1;
934 *ckey = 0;
935 }
936
937 int skin_find_skey(int ckey, bool cshift) {
938 int fuzzy_match = -1;
939 for (int i = 0; i < nkeys; i++)
940 if (keylist[i].code == ckey || keylist[i].shifted_code == ckey)
941 if ((cshift ? keylist[i].shifted_code : keylist[i].code) == ckey)
942 return i;
943 else if (fuzzy_match == -1)
944 fuzzy_match = i;
945 return fuzzy_match;
946 }
947
948 unsigned char *skin_find_macro(int ckey, int *type) {
949 SkinMacro *m = macrolist;
950 while (m != NULL) {
951 if (m->code == ckey) {
952 if (!m->isName || m->secondType == 0 || !core_alpha_menu()) {
953 *type = m->isName ? 1 : 0;
954 return m->macro;
955 } else {
956 *type = m->secondType;
957 return m->macro2;
958 }
959 }
960 m = m->next;
961 }
962 return NULL;
963 }
964
965 unsigned char *skin_keymap_lookup(guint keyval, bool printable,
966 bool ctrl, bool alt, bool shift, bool cshift,
967 bool *exact) {
968 int i;
969 unsigned char *macro = NULL;
970 for (i = 0; i < keymap_length; i++) {
971 keymap_entry *entry = keymap + i;
972 if (ctrl == entry->ctrl
973 && alt == entry->alt
974 && (printable || shift == entry->shift)
975 && keyval == entry->keyval) {
976 if (shift == entry->shift && cshift == entry->cshift) {
977 *exact = true;
978 return entry->macro;
979 }
980 if ((shift || !entry->shift) && (cshift || !entry->cshift) && macro == NULL)
981 macro = entry->macro;
982 }
983 }
984 *exact = false;
985 return macro;
986 }
987
988 void skin_repaint_key(cairo_t *cr, int key, bool state) {
989 SkinKey *k;
990
991 if (key >= -7 && key <= -2) {
992 /* Soft key */
993 key = -1 - key;
994 int kx = (key - 1) * 22;
995 int ky = 9;
996 int kw = 21;
997 int kh = 7;
998 int x = kx - 1;
999 int y = ky - 1;
1000 int width = kw + 2;
1001 int height = kh + 2;
1002
1003 cairo_save(cr);
1004 cairo_translate(cr, display_loc.x, display_loc.y);
1005 cairo_scale(cr, display_scale_x, display_scale_y);
1006 cairo_rectangle(cr, x, y, width, height);
1007 cairo_clip(cr);
1008 cairo_set_source_rgb(cr, display_bg.r / 255.0, display_bg.g / 255.0, display_bg.b / 255.0);
1009 cairo_paint(cr);
1010 cairo_set_source_rgb(cr, display_fg.r / 255.0, display_fg.g / 255.0, display_fg.b / 255.0);
1011
1012 if (x < 0)
1013 x = 0;
1014 if (y < 0)
1015 y = 0;
1016 if (x + width > 131)
1017 width = 131 - x;
1018 if (y + height > 16)
1019 height = 16 - y;
1020
1021 for (int v = y; v < y + height; v++) {
1022 bool in_key_v = v >= ky && v < ky + kh;
1023 for (int h = x; h < x + width; h++) {
1024 bool ks = !(in_key_v && h >= kx && h < kx + kw) ^ state;
1025 if (((disp_bits[v * 17 + (h >> 3)] & (1 << (h & 7))) != 0) != ks) {
1026 cairo_rectangle(cr, h, v, 1, 1);
1027 cairo_fill(cr);
1028 }
1029 }
1030 }
1031
1032 cairo_restore(cr);
1033 return;
1034 }
1035
1036 if (key < 0 || key >= nkeys)
1037 return;
1038 k = keylist + key;
1039 if (state)
1040 gdk_cairo_set_source_pixbuf(cr, skin_image,
1041 k->disp_rect.x - k->src.x,
1042 k->disp_rect.y - k->src.y);
1043 else
1044 gdk_cairo_set_source_pixbuf(cr, skin_image, -skin.x, -skin.y);
1045 cairo_save(cr);
1046 cairo_rectangle(cr, k->disp_rect.x, k->disp_rect.y, k->disp_rect.width, k->disp_rect.height);
1047 cairo_clip(cr);
1048 cairo_paint(cr);
1049 cairo_restore(cr);
1050 }
1051
1052 void skin_invalidate_key(GdkWindow *win, int key) {
1053 if (key >= -7 && key <= -2) {
1054 /* Soft key */
1055 key = -1 - key;
1056 int x = (key - 1) * 22 * display_scale_x - 1;
1057 int y = 9 * display_scale_y - 1;
1058 int width = 21 * display_scale_x + 2;
1059 int height = 7 * display_scale_y + 2;
1060 GdkRectangle clip;
1061 clip.x = display_loc.x + x;
1062 clip.y = display_loc.y + y;
1063 clip.width = width;
1064 clip.height = height;
1065 scaled_gdk_window_invalidate_rect(win, &clip, FALSE);
1066 return;
1067 }
1068 if (key < 0 || key >= nkeys)
1069 return;
1070 SkinKey *k = keylist + key;
1071 GdkRectangle clip;
1072 clip.x = k->disp_rect.x;
1073 clip.y = k->disp_rect.y;
1074 clip.width = k->disp_rect.width;
1075 clip.height = k->disp_rect.height;
1076 scaled_gdk_window_invalidate_rect(win, &clip, FALSE);
1077 }
1078
1079 void skin_display_invalidater(GdkWindow *win, const char *bits, int bytesperline,
1080 int x, int y, int width, int height) {
1081 for (int v = y; v < y + height; v++)
1082 for (int h = x; h < x + width; h++)
1083 if ((bits[v * bytesperline + (h >> 3)] & (1 << (h & 7))) != 0)
1084 disp_bits[v * 17 + (h >> 3)] |= 1 << (h & 7);
1085 else
1086 disp_bits[v * 17 + (h >> 3)] &= ~(1 << (h & 7));
1087
1088 if (win != NULL) {
1089 if (allow_paint) {
1090 GdkRectangle clip;
1091 clip.x = display_loc.x + x * display_scale_x;
1092 clip.y = display_loc.y + y * display_scale_y;
1093 clip.width = width * display_scale_x;
1094 clip.height = height * display_scale_y;
1095 scaled_gdk_window_invalidate_rect(win, &clip, FALSE);
1096 }
1097 } else {
1098 gtk_widget_queue_draw_area(calc_widget,
1099 display_loc.x - display_scale_x,
1100 display_loc.y - display_scale_y,
1101 133 * display_scale_x,
1102 18 * display_scale_y);
1103 }
1104 }
1105
1106 bool need_to_paint_only_display(cairo_t *cr) {
1107 double left, top, right, bottom;
1108 cairo_clip_extents(cr, &left, &top, &right, &bottom);
1109 return left >= display_loc.x - display_scale_x
1110 && top >= display_loc.y - display_scale_y
1111 && right <= display_loc.x + 132 * display_scale_x
1112 && bottom <= display_loc.y + 17 * display_scale_y;
1113 }
1114
1115 void skin_repaint_display(cairo_t *cr) {
1116 cairo_save(cr);
1117 cairo_translate(cr, display_loc.x, display_loc.y);
1118 cairo_scale(cr, display_scale_x, display_scale_y);
1119 cairo_rectangle(cr, -1, -1, 131 + 2, 16 + 2);
1120 cairo_clip(cr);
1121 cairo_set_source_rgb(cr, display_bg.r / 255.0, display_bg.g / 255.0, display_bg.b / 255.0);
1122 cairo_paint(cr);
1123 cairo_set_source_rgb(cr, display_fg.r / 255.0, display_fg.g / 255.0, display_fg.b / 255.0);
1124 for (int v = 0; v < 16; v++) {
1125 for (int h = 0; h < 131; h++) {
1126 if ((disp_bits[v * 17 + (h >> 3)] & (1 << (h & 7))) != 0) {
1127 cairo_rectangle(cr, h, v, 1, 1);
1128 cairo_fill(cr);
1129 }
1130 }
1131 }
1132 cairo_restore(cr);
1133 }
1134
1135 void skin_get_size(int *width, int *height) {
1136 *width = skin.width;
1137 *height = skin.height;
1138 }
1139
1140 void skin_set_window_size(int width, int height) {
1141 window_width = width;
1142 window_height = height;
1143 }
1144
1145 void skin_get_window_size(int *width, int *height) {
1146 *width = window_width;
1147 *height = window_height;
1148 }