summaryrefslogtreecommitdiffstats
path: root/Xoo/src/config.c
blob: 07a70c4bb718a37705182aa2d2cb8c3c74b514b8 (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
/* Xoo - a graphical wrapper around xnest
 *
 *  Copyright 2004,2005 Matthew Allum, Openedhand Ltd <mallum@o-hand.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */

#include "config.h"

#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 <gdk-pixbuf/gdk-pixbuf.h>
#include <expat.h>
#include "fakedev.h"

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

  strncpy (tmp, filename, 1024);

  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;
	    }
	}
    }

  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 *
config_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 GdkPixbuf *
config_load_image (FakeApp * app, const char *filename)
{
  GError *error = NULL;
  GdkPixbuf *pixbuf;
  char tmp[1024];
  struct stat st;

  strncpy (tmp, filename, 1024);

  if (stat (tmp, &st))
    {
      snprintf (tmp, 1024, PKGDATADIR "/%s", filename);
      if (stat (tmp, &st))
	{
	  return NULL;
	}
    }

  pixbuf = gdk_pixbuf_new_from_file (tmp, &error);
  if (error == NULL)
    {
      return pixbuf;
    }
  else
    {
      g_warning ("Could not load %s: %s", filename, error->message);
      g_error_free (error);
      return NULL;
    }
}


static int
config_handle_button_tag (FakeApp * app, const char **attr)
{
  const char *s;
  FakeButton *button;
  int x, y, width, height;
  KeySym keysym;
  int repeat = 1;
  GdkPixbuf *img = NULL;

  if ((s = config_get_val ("width", attr)) == NULL)
    {
      fprintf (stderr, "Error: 'button' tag missing width param");
      return 0;
    }

  width = atoi (s);

  if ((s = config_get_val ("height", attr)) == NULL)
    {
      fprintf (stderr, "Error: 'button' tag missing height param");
      return 0;
    }

  height = atoi (s);

  if ((s = config_get_val ("x", attr)) == NULL)
    {
      fprintf (stderr, "Error: 'button' tag missing x param");
      return 0;
    }

  x = atoi (s);

  if ((s = config_get_val ("y", attr)) == NULL)
    {
      fprintf (stderr, "Error: 'button' tag missing y param");
      return 0;
    }

  y = atoi (s);

  if ((s = config_get_val ("key", attr)) == NULL)
    {
      fprintf (stderr, "Error: 'button' tag missing key param");
      return 0;
    }

  if ((keysym = XStringToKeysym (s)) == NoSymbol)
    {
      fprintf (stderr, "Error: Cant find keysym for '%s'", s);
      return 0;
    }

  /* optional attributes */

  if ((s = config_get_val ("img", attr)) != NULL)
    {
      if ((img = config_load_image (app, s)) == NULL)
	{
	  fprintf (stderr, "Error: Falied to load '%s' image.", s);
	}
    }

  if (((s = config_get_val ("repeat", attr)) != NULL)
      && !strcasecmp (s, "off"))
    repeat = 0;


  if (app->button_head == NULL)
    app->button_head = button_new (app, x, y, width, height,
				   keysym, repeat, img);
  else
    {
      for (button = app->button_head; button->next; button = button->next);
      button->next = button_new (app, x, y, width, height,
				 keysym, repeat, img);
    }

  return 1;
}

static int
config_handle_device_tag (FakeApp * app, const char **attr)
{
  /* 
     width, height, display_width, display_height, display_x, displa_y
   */
  const char *s;

  if ((s = config_get_val ("width", attr)) == NULL)
    {
      fprintf (stderr, "Error: 'device' tag missing width param");
      return 0;
    }

  app->device_width = atoi (s);

  if ((s = config_get_val ("height", attr)) == NULL)
    {
      fprintf (stderr, "Error: 'device' tag missing height param");
      return 0;
    }

  app->device_height = atoi (s);

  if ((s = config_get_val ("display_width", attr)) == NULL)
    {
      fprintf (stderr, "Error: 'device' tag missing display_width param");
      return 0;
    }

  app->device_display_width = atoi (s);

  if ((s = config_get_val ("display_height", attr)) == NULL)
    {
      fprintf (stderr, "Error: 'device' tag missing display_height param");
      return 0;
    }

  app->device_display_height = atoi (s);

  if ((s = config_get_val ("display_x", attr)) == NULL)
    {
      fprintf (stderr, "Error: 'device' tag missing display_x param");
      return 0;
    }

  app->device_display_x = atoi (s);

  if ((s = config_get_val ("display_y", attr)) == NULL)
    {
      fprintf (stderr, "Error: 'device' tag missing display_x param");
      return 0;
    }

  app->device_display_y = atoi (s);

  if ((s = config_get_val ("img", attr)) == NULL)
    {
      fprintf (stderr, "Error: 'device' tag missing img param");
      return 0;
    }

  if ((app->device_img = config_load_image (app, s)) == NULL)
    {
      fprintf (stderr, "Error: Falied to load '%s' image.", s);
      return 0;
    }

  return 1;
}

static void
config_xml_start_cb (void *data, const char *tag, const char **attr)
{
  FakeApp *app = (FakeApp *) data;

  if (!strcmp (tag, "button"))
    {
      if (!config_handle_button_tag (app, attr))
	{
	  fprintf (stderr, "Giving up.\n");
	  exit (1);
	}
    }
  else if (!strcmp (tag, "device"))
    {
      if (!config_handle_device_tag (app, attr))
	{
	  fprintf (stderr, "Giving up.\n");
	  exit (1);
	}
    }
}


int
config_init (FakeApp * app, char *conf_file)
{
  char *data;
  XML_Parser p;

  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);
    }

  XML_SetElementHandler (p, config_xml_start_cb, NULL);
  /* XML_SetCharacterDataHandler(p, chars); */

  XML_SetUserData (p, (void *) app);

  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;
}

void
config_reinit (FakeApp * app, char *conf_file)
{
  /* Save these so we can figure out if server needs restarting */

  /*
     int prev_device_display_x      = app->device_display_x;
     int prev_device_display_y      = app->device_display_y;
     int prev_device_display_width  = app->device_display_width;
     int prev_device_display_height = app->device_display_height;
   */

  /* XXX TODO: Free up our old structure images etc XXX */

  /* Reload UI - hard ? */

}