aboutsummaryrefslogtreecommitdiffstats
path: root/src/config-parser.c
blob: 4a30d2be3cfc290a5e996b687c82091b8878f73a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
/*
 *  Matchbox Keyboard - A lightweight software keyboard.
 *
 *  Authored By Matthew Allum <mallum@o-hand.com>
 *              Tomas Frydrych <tomas@sleepfive.com>
 *
 *  Copyright (c) 2005-2012 Intel Corp
 *  Copyright (c) 2012 Vernier Software & Technology
 *
 *  SPDX-License-Identifier: LGPL-2.1
 *
 */

#include "matchbox-keyboard.h"

/*
    <keyboard>

    <options>
       <font prefered-size=''>
       <size fixed='100x100'>
       <padding>
    </options>

    <layout id="name">
      <row>
        <key id="optional-id" obey-caps='true|false'
	     width="1000"   // 1/1000's of a unit key size
	     fill="true"    // Set width to available space

             >
	  <default
	     display="a"
	     display="image:"
	     action="utf8char"     // optional, action defaults to this
	     action="string"       // from lookup below
	     action="modifier:Shift|Alt|ctrl|mod1|mod2|mod3|caps"
	     action="xkeysym:XK_BLAH"
	  <shifted
	     ...... >
	  <mod1
	     ...... >

	/>
        <key ... />
	<key ... />
	<space width="1000"
      </row>
    </layout>


    </keyboard>
*/

struct _keysymlookup
{
  KeySym keysym;   char *name;
}
MBKeyboardKeysymLookup[] =
{
 { XK_BackSpace,   "backspace" },
 { XK_Tab,	   "tab"       },
 { XK_Linefeed,    "linefeed"  },
 { XK_Clear,       "clear"     },
 { XK_Return,      "return"    },
 { XK_Pause,       "pause" },
 { XK_Scroll_Lock, "scrolllock" },
 { XK_Sys_Req,     "sysreq" },
 { XK_Escape,      "escape" },
 { XK_Delete,      "delete" },
 { XK_Home,        "home" },
 { XK_Left,        "left" },
 { XK_Up,          "up"   },
 { XK_Right,       "right" },
 { XK_Down,        "down"  },
 { XK_Prior,       "prior" },
 { XK_Page_Up,     "pageup" },
 { XK_Next,        "next"   },
 { XK_Page_Down,   "pagedown" },
 { XK_End,         "end" },
 { XK_Begin,	   "begin" },
 { XK_space,        "space" },
 { XK_F1,          "f1" },
 { XK_F2,          "f2" },
 { XK_F3,          "f3" },
 { XK_F4,          "f4" },
 { XK_F5,          "f5" },
 { XK_F6,          "f6" },
 { XK_F7,          "f7" },
 { XK_F8,          "f8" },
 { XK_F9,          "f9" },
 { XK_F10,         "f10" },
 { XK_F11,         "f11" },
 { XK_F12,         "f12" }
};

struct _modlookup
{
  char *name; MBKeyboardKeyModType type;
}
ModLookup[] =
{
  { "shift",   MBKeyboardKeyModShift },
  { "alt",     MBKeyboardKeyModAlt },
  { "ctrl",    MBKeyboardKeyModControl },
  { "control", MBKeyboardKeyModControl },
  { "mod1",    MBKeyboardKeyModMod1 },
  { "mod2",    MBKeyboardKeyModMod2 },
  { "mod3",    MBKeyboardKeyModMod3 },
  { "caps",    MBKeyboardKeyModCaps }
};

typedef struct MBKeyboardConfigState
{
  MBKeyboard       *keyboard;
  MBKeyboardLayout *current_layout;
  MBKeyboardRow    *current_row;
  MBKeyboardKey    *current_key;
  Bool              error;
  char             *error_msg;
  int               error_lineno;
  char             *lang;
  XML_Parser        parser;
}
MBKeyboardConfigState;

static int load_include (MBKeyboardConfigState *state,
                         const char            *include,
                         int                    autolocale);

void
set_error(MBKeyboardConfigState *state, char *msg)
{
  state->error = True;
  state->error_lineno = XML_GetCurrentLineNumber(state->parser);
  state->error_msg = msg;
}

KeySym
config_str_to_keysym(const char* str)
{
  int i;

  DBG("checking %s", str);

  for (i=0; i<sizeof(MBKeyboardKeysymLookup)/sizeof(struct _keysymlookup); i++)
    if (streq(str, MBKeyboardKeysymLookup[i].name))
      return MBKeyboardKeysymLookup[i].keysym;

  DBG("didnt find it %s", str);

  return 0;
}

MBKeyboardKeyModType
config_str_to_modtype(const char* str)
{
  int i;

  for (i=0; i<sizeof(ModLookup)/sizeof(struct _modlookup); i++)
    {
      DBG("checking '%s' vs '%s'", str, ModLookup[i].name);
      if (streq(str, ModLookup[i].name))
	return ModLookup[i].type;
    }

  return 0;
}

static char*
load_config_file (const char *basename,
                  char       *variant_in,
                  char       *lang,
                  int         autolocale,
                  char      **path_out)
{
  struct stat    stat_info;
  FILE*          fp;
  char          *result;
  char          *country  = NULL;
  char          *variant  = NULL;
  int            n = 0, i = 0;
  char           path[1024]; 	/* XXX MAXPATHLEN */

  if (!basename)
    return NULL;

  if (path_out)
    *path_out = NULL;

  /* basename[-country][-variant].xml */

  /* This is an overide mainly for people developing keyboard layouts  */

  if (getenv("MB_KBD_CONFIG"))
    {
      snprintf(path, 1024, "%s", getenv("MB_KBD_CONFIG"));

      DBG("checking %s\n", path);

      if (util_file_readable(path))
	goto load;

      return NULL;
    }

  if (lang || autolocale)
    {
      if (autolocale)
        {
          lang = getenv("MB_KBD_LANG");

          if (lang == NULL)
            lang = getenv("LANG");
        }

      if (lang)
        {
          n = strlen(lang) + 2;

          country = alloca(n);

          snprintf(country, n, "-%s", lang);

          /* strip anything after first '.' */
          while(country[i] != '\0')
            if (country[i] == '.')
              country[i] = '\0';
            else
              i++;
        }
    }

  if (variant_in)
    {
      n = strlen(variant_in) + 2;
      variant = alloca(n);
      snprintf(variant, n, "-%s", variant_in);
    }

  if (getenv("HOME"))
    {
      snprintf(path, 1024, "%s/.matchbox/%s.xml", getenv("HOME"), basename);

      DBG("checking %s\n", path);

      if (util_file_readable(path))
	goto load;
    }

  /* Hmmm :/ */

  snprintf(path, 1024, PKGDATADIR "/%s%s%s.xml",
           basename,
	   country == NULL ? "" : country,
	   variant == NULL ? "" : variant);

  DBG("checking %s\n", path);

  if (util_file_readable(path))
    goto load;

  snprintf(path, 1024, PKGDATADIR "/%s%s.xml",
           basename, variant == NULL ? "" : variant);

  DBG("checking %s\n", path);

  if (util_file_readable(path))
    goto load;

  snprintf(path, 1024, PKGDATADIR "/%s%s.xml",
           basename, country == NULL ? "" : country);

  DBG("checking %s\n", path);

  if (util_file_readable(path))
    goto load;

  snprintf(path, 1024, PKGDATADIR "/%s.xml", basename);

  DBG("checking %s\n", path);

  if (!util_file_readable(path))
    goto load;

  return NULL;

 load:
  if (stat(path, &stat_info) || !(fp = fopen(path, "rb")))
    return NULL;

  DBG("loading config %s\n", path);

  if (path_out)
    *path_out = strdup (path);

  result = malloc(stat_info.st_size + 1);

  n = fread(result, 1, stat_info.st_size, fp);

  if (n >= 0)
    result[n] = '\0';

  fclose(fp);

  return result;
}

static const char *
attr_get_val (char *key, const char **attr)
{
  int i = 0;

  while (attr[i] != NULL)
    {
      if (!strcmp(attr[i], key))
	return attr[i+1];
      i += 2;
    }

  return NULL;
}


static void
config_handle_key_subtag(MBKeyboardConfigState *state,
			 const char            *tag,
			 const char           **attr)
{
  MBKeyboardKeyStateType keystate;
  const char            *val;
  KeySym                 found_keysym;

  /* TODO: Fix below with a lookup table
   */
  if (streq(tag, "normal") || streq(tag, "default"))
    {
      keystate = MBKeyboardKeyStateNormal;
    }
  else if (streq(tag, "shifted"))
    {
      keystate = MBKeyboardKeyStateShifted;
    }
  else if (streq(tag, "caps"))
    {
      keystate = MBKeyboardKeyStateCaps;
    }
  else if (streq(tag, "mod1"))
    {
      keystate = MBKeyboardKeyStateMod1;
    }
  else if (streq(tag, "mod2"))
    {
      keystate = MBKeyboardKeyStateMod2;
    }
  else if (streq(tag, "mod3"))
    {
      keystate = MBKeyboardKeyStateMod3;
    }
  else
    {
      set_error(state, "Unknown key subtag");
      return;
    }

  if ((val = attr_get_val("display", attr)) == NULL)
    {
      set_error(state, "Attribute 'display' is required");
      return;
    }

  if (!strncmp(val, "image:", 6))
    {
      MBKeyboardImage *img;


      if (val[6] != '/')
	{
	  /* Relative, rather than absolute path, try pkddatadir and home */
	  char buf[512];
	  snprintf(buf, 512, "%s/%s", PKGDATADIR, &val[6]);

	  if (!util_file_readable(buf))
	    snprintf(buf, 512, "%s/.matchbox/%s", getenv("HOME"), &val[6]);

#ifdef WANT_CAIRO
          img = cairo_image_surface_create_from_png (buf);
#else
	  img = mb_kbd_image_new (state->keyboard, buf);
#endif
	}
      else
#ifdef WANT_CAIRO
        img = cairo_image_surface_create_from_png (&val[6]);
#else
	img = mb_kbd_image_new (state->keyboard, &val[6]);
#endif

      if (!img)
	{
	  fprintf(stderr, "matchbox-keyboard: Failed to load '%s'\n", &val[6]);
	  state->error = True;
	  return;
	}
      mb_kbd_key_set_image_face(state->current_key, keystate, img);
    }
  else
    {
      mb_kbd_key_set_glyph_face(state->current_key, keystate,
				attr_get_val("display", attr));
    }

  if ((val = attr_get_val("action", attr)) != NULL)
    {
      /*
	     action="utf8char"     // optional, action defulats to this
	     action="modifier:Shift|Alt|ctrl|mod1|mod2|mod3|caps"
	     action="xkeysym:XK_BLAH"
	     action="control:">    // return etc - not needed use lookup
      */

      if (!strncmp(val, "modifier:", 9))
	{
	  MBKeyboardKeyModType found_type;

	  DBG("checking '%s'", &val[9]);

	  found_type = config_str_to_modtype(&val[9]);

	  if (found_type)
	    {
	      mb_kbd_key_set_modifer_action(state->current_key,
					    keystate,
					    found_type);
	    }
	  else
	    {
              set_error(state, "Unknown modifier");
	      return;
	    }

	}
      else if (!strncmp(val, "xkeysym:", 8))
	{
	  DBG("Checking %s\n", &val[8]);

	  found_keysym = XStringToKeysym(&val[8]);

	  if (found_keysym)
	    {
	      mb_kbd_key_set_keysym_action(state->current_key,
					   keystate,
					   found_keysym);
	    }
	  else
	    {
	      /* Should this error really be terminal */
              set_error(state, "Unknown keysym");
	      return;
	    }
	}
      else
	{
	  /* Its just 'regular' key  */

	  if (strlen(val) > 1  	/* match backspace, return etc */
	      && ((found_keysym  = config_str_to_keysym(val)) != 0))
	    {
	      mb_kbd_key_set_keysym_action(state->current_key,
					   keystate,
					   found_keysym);
	    }
	  else
	    {
	      /* XXX We should actually check its a single UTF8 Char here */
	      mb_kbd_key_set_char_action(state->current_key,
					 keystate, val);
	    }
	}
    }
  else /* fallback to reusing whats displayed  */
    {

      /* display could be an image in which case we should throw an error
       * or summin.
      */

      mb_kbd_key_set_char_action(state->current_key,
				 keystate,
				 attr_get_val("display", attr));
    }

}

static void
config_handle_layout_tag(MBKeyboardConfigState *state, const char **attr)
{
  const char            *val;

  if ((val = attr_get_val("id", attr)) == NULL)
    {
      set_error(state, "Attribute 'id' is required");
      return;
    }

  state->current_layout = mb_kbd_layout_new(state->keyboard, val);

  mb_kbd_add_layout(state->keyboard, state->current_layout);
}

static void
config_handle_row_tag(MBKeyboardConfigState *state, const char **attr)
{
  state->current_row = mb_kbd_row_new(state->keyboard);
  mb_kbd_layout_append_row(state->current_layout, state->current_row);
}

static void
config_handle_key_tag(MBKeyboardConfigState *state, const char **attr)
{
  const char *val;
  DBG("got key");

  state->current_key = mb_kbd_key_new(state->keyboard);

  if ((val = attr_get_val("obey-caps", attr)) != NULL)
    {
      if (strcaseeq(val, "true"))
	mb_kbd_key_set_obey_caps(state->current_key, True);
    }

  if ((val = attr_get_val("extended", attr)) != NULL)
    {
      if (strcaseeq(val, "true"))
	mb_kbd_key_set_extended(state->current_key, True);
    }

  if ((val = attr_get_val("width", attr)) != NULL)
    {
      if (atoi(val) > 0)
	mb_kbd_key_set_req_uwidth(state->current_key, atoi(val));
    }

  if ((val = attr_get_val("fill", attr)) != NULL)
    {
      if (strcaseeq(val, "true"))
	mb_kbd_key_set_fill(state->current_key, True);
    }

  mb_kbd_row_append_key(state->current_row, state->current_key);
}

static void
config_xml_start_cb(void *data, const char *tag, const char **attr)
{
  MBKeyboardConfigState *state = (MBKeyboardConfigState *)data;

  if (streq(tag, "layout"))
    {
      config_handle_layout_tag(state, attr);
    }
  else if (streq(tag, "row"))
    {
      config_handle_row_tag(state, attr);
    }
  else if (streq(tag, "key"))
    {
      config_handle_key_tag(state, attr);
    }
  else  if (streq(tag, "space"))
    {
      config_handle_key_tag(state, attr);
      mb_kbd_key_set_blank(state->current_key, True);
    }
  else if (streq(tag, "normal")
	   || streq(tag, "default")
	   || streq(tag, "shifted")
	   || streq(tag, "caps")
	   || streq(tag, "mod1")
	   || streq(tag, "mod2")
	   || streq(tag, "mod3"))
    {
      config_handle_key_subtag(state, tag, attr);
    }
  else  if (streq(tag, "include"))
    {
      const char *val;
      const char *loc;
      char       *inc;
      int         autoloc = 1;

      if (!(val = attr_get_val("file", attr)))
        return;
      else
        {
          char *p;

          inc = strdup (val);
          if ((p = strstr (inc, ".xml")))
            *p = 0;
        }

      if (state->lang || ((loc = attr_get_val("auto-locale", attr)) &&
                          streq(loc, "no")))
        autoloc = 0;

      if (!load_include (state, inc, autoloc))
        set_error (state, "Failed to load include");
    }
  else  if (streq(tag, "fragment"))
    {
      /* Do nothing; the fragment element is needed so that the fragments
         are a valid xml */
    }

  if (state->error)
    {
      fprintf(stderr, "matchbox-keyboard:%s:%d: %s\n", state->keyboard->config_file,
                                              state->error_lineno, state->error_msg);
      if (!state->keyboard->is_widget)
        util_fatal_error("Error parsing\n");
      else
        XML_StopParser(state->parser, 0);
    }
}

static int
load_include (MBKeyboardConfigState *state,
              const char            *include,
              int                    autolocale)
{
  XML_Parser  p, old_p;
  char        *data;
  int          retval = 1;

  if (!(data = load_config_file (include, NULL, state->lang, autolocale, NULL)))
    {
      if (!state->keyboard->is_widget)
        util_fatal_error("Couldn't find a keyboard config file\n");
      else
        return 0;
    }

  p = XML_ParserCreate(NULL);

  if (!p)
    {
      if (!state->keyboard->is_widget)
        util_fatal_error("Couldn't allocate memory for XML subparser\n");
      else
        return 0;
    }

  old_p = state->parser;
  state->parser = p;

  XML_SetElementHandler(p, config_xml_start_cb, NULL);
  XML_SetUserData(p, (void *)state);

  if (! XML_Parse(p, data, strlen(data), 1)) {
    fprintf(stderr,
	    "matchbox-keyboard:%s:%d: XML Parse error:%s\n",
	    include,
	    (int)XML_GetCurrentLineNumber(p),
	    XML_ErrorString(XML_GetErrorCode(p)));

    if (!state->keyboard->is_widget)
      util_fatal_error("XML Parse failed.\n");
    else
      retval = 0;
  }

  if (state->error)
    retval = 0;

  state->parser = old_p;

  XML_ParserFree (p);

  return retval;
}

int
mb_kbd_config_load(MBKeyboard *kbd, char *variant, char *lang)
{
  char                  *data;
  XML_Parser             p;
  MBKeyboardConfigState *state;
  int                    retval = 1;

  if (!(data = load_config_file ("keyboard", variant, lang, lang ? 0 : 1,
                                 &kbd->config_file)))
    {
      if (!state->keyboard->is_widget)
        util_fatal_error("Couldn't find a keyboard config file\n");
      else
        return 0;
    }

  p = XML_ParserCreate(NULL);

  if (!p)
    {
      if (!state->keyboard->is_widget)
        util_fatal_error("Couldn't allocate memory for XML parser\n");
      else
        return 0;
    }

  if (variant && !strstr(kbd->config_file, variant))
    fprintf(stderr,
	    "matchbox-keyboard: *Warning* Unable to locate variant: %s\n"
	    "                   falling back to %s\n",
	    variant, kbd->config_file);

  state = util_malloc0(sizeof(MBKeyboardConfigState));

  state->keyboard = kbd;
  state->parser = p;
  state->lang = lang;

  XML_SetElementHandler(p, config_xml_start_cb, NULL);

  /* XML_SetCharacterDataHandler(p, chars); */

  XML_SetUserData(p, (void *)state);

  if (! XML_Parse(p, data, strlen(data), 1)) {
    fprintf(stderr,
	    "matchbox-keyboard:%s:%lu: XML Parse error:%s\n",
	    kbd->config_file,
	    XML_GetCurrentLineNumber(p),
	    XML_ErrorString(XML_GetErrorCode(p)));

    if (!state->keyboard->is_widget)
      util_fatal_error("XML Parse failed.\n");
    else
      retval = 0;
  }

  if (state->error)
    retval = 0;

  XML_ParserFree (p);

  return retval;
}