aboutsummaryrefslogtreecommitdiffstats
path: root/src/config-parser.c
blob: 34b4a66145ae3ec9ba76d37333c5e0053731b1cb (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <libgen.h>    /* dirname() */
#include <expat.h>

#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' geometry padding
	  <defualt
	     display="a"                
	     display="image:" 
	     action="utf8char"     // optional, action defulats to this    
	     action="modifier:Shift|Alt|ctrl|mod1|mod2|mod3|caps"
	     action="xkeysym:XK_BLAH"
	     action="control:">    // return etc 
	  <shifted 
	     ...... >
	  <mod1
	     ...... >
	     
	/>
        <key ... />
	<key ... />
	<spacer size="100px/%"
      </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_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" }
};


typedef struct MBKeyboardConfigState
{
  MBKeyboard       *keyboard;
  MBKeyboardLayout *current_layout;
  MBKeyboardRow    *current_row;
  MBKeyboardKey    *current_key;
  Bool              error;
  char             *error_msg;
}
MBKeyboardConfigState;

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

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

  return 0;
}

static unsigned char* 
config_load_file(const char* filename) 
{
  struct stat    st;
  FILE*          fp;
  unsigned char* str;
  int            len;
  char           tmp[1024];

  strncpy(tmp, filename, 1024);

  stat(tmp, &st);

#if 0  
  if (stat(tmp, &st)) 
    {
      snprintf(tmp, 1024, "%s.xml", filename);
      if (stat(tmp, &st)) 
	{/*
	  snprintf(tmp, 1024, PKGDATADIR "/%s", filename);
	  if (stat(tmp, &st)) 
	    {
	      snprintf(tmp, 1024, PKGDATADIR "/%s.xml", filename);
	      if (stat(tmp, &st)) 
		return NULL;
	    }
	 */
	}
  }  
#endif

  if (!(fp = fopen(tmp, "rb"))) return NULL;
  
  /* Read in the file. */
  str = malloc(sizeof(char)*(st.st_size + 1));
  len = fread(str, 1, st.st_size, fp);
  if (len >= 0) str[len] = '\0';
  
  fclose(fp);
  
  /* change to the same directory as the conf file - for images */
  // chdir(dirname(tmp));
  return str;
}

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"))
    {
      keystate = MBKeyboardKeyStateNormal;
    }
  else if (streq(tag, "shifted"))
    {
      keystate = MBKeyboardKeyStateShifted;
    }
  else if (streq(tag, "mod1"))
    {
      keystate = MBKeyboardKeyStateMod1;
    }
  else if (streq(tag, "mod2"))
    {
      keystate = MBKeyboardKeyStateMod2;
    }
  else if (streq(tag, "mod3"))
    {
      keystate = MBKeyboardKeyStateMod3;
    }
  else
    {
      state->error = True;
      return;
    }

  if ((val = attr_get_val("display", attr)) == NULL)
    {
      state->error = True;
      return;
    }

  if (strlen(val) > 1 
      && ((found_keysym  = mb_kbd_config_str_to_keysym(val)) != NULL))
    {


    }

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

  if (attr_get_val("action", attr) != NULL)
    {
      mb_kbd_key_set_char_action(state->current_key, 
				 keystate, 
				 attr_get_val("action", attr));
    }
  else 
    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)
    {
      state->error = True;
      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)
{
  state->current_key = mb_kbd_key_new(state->keyboard);
  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, "normal") 
	   || streq(tag, "shift")
	   || streq(tag, "mod1")
	   || streq(tag, "mod2")
	   || streq(tag, "mod3"))
    {
      config_handle_key_subtag(state, tag, attr);
    }

  if (state->error)
    {
      util_fatal_error("Error parsing\n");
    }
}


int
mb_kbd_config_load(MBKeyboard *kbd, char *conf_file)
{
  unsigned char         *data;
  XML_Parser             p;
  MBKeyboardConfigState *state;

  if ((data = config_load_file(conf_file)) == NULL)
    {
      fprintf(stderr, "Couldn't find '%s' device config file\n", conf_file);
      exit(1);
    }

  p = XML_ParserCreate(NULL);

  if (! p) {
    fprintf(stderr, "Couldn't allocate memory for XML parser\n");
    exit(1);
  }

  state = util_malloc0(sizeof(MBKeyboardConfigState));

  state->keyboard = kbd;

  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, "XML Parse error at line %d:\n%s\n",
	    XML_GetCurrentLineNumber(p),
	    XML_ErrorString(XML_GetErrorCode(p)));
    exit(1);
  }

  return 1;
}