Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/art/cursor/cursorthemegen/themefile.c
blob: a8a703f3b3566a32d12feeb2f9b373653617ab5d (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
/*
 * Copyright © 2003 Red Hat, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Red Hat not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  Red Hat makes no representations about the
 * suitability of this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 *
 * RED HAT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL RED HAT
 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author:  Owen Taylor, Red Hat, Inc.
 */
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

#include "cursortheme.h"

typedef struct _ParseData ParseData;

typedef enum {
  OUTSIDE,
  IN_THEME,
  IN_SOURCE,
  IN_IMAGE,
  IN_ALIAS,
  IN_LAYOUT,
  IN_ROW,
  IN_CURSOR,
  IN_FRAME
} ParseState;

struct _ParseData {
  CursorTheme *theme;
  ParseState state;
  gboolean seen_theme;
  gboolean seen_layout;
  int row;
  int column;
  Cursor *current_cursor;
};

static gboolean
expect_tag (GMarkupParseContext *context,
	    const gchar *element_name,
	    const gchar *expected_name,	/* Null for expected empty */
	    GError             **error)
{
  if (!expected_name || strcmp (element_name, expected_name) != 0)
    {
      g_set_error (error,
		   G_MARKUP_ERROR,
		   G_MARKUP_ERROR_INVALID_CONTENT,
		   "Unexpected tag '%s'",
		   element_name);
      return FALSE;
    }
  return TRUE;
}

static gboolean
extract_attrs (GMarkupParseContext *context,
	       const gchar        **attribute_names,
	       const gchar        **attribute_values,
	       GError             **error,
	       ...)
{
  va_list vap;
  const char *name;
  gboolean *attr_map;
  gboolean nattrs = 0;
  int i;

  for (i = 0; attribute_names[i]; i++)
    nattrs++;

  attr_map = g_new0 (gboolean, nattrs);

  va_start (vap, error);
  name = va_arg (vap, const char *);
  while (name)
    {
      gboolean mandatory = va_arg (vap, gboolean);
      const char **loc = va_arg (vap, const char **);
      gboolean found = FALSE;

      for (i = 0; attribute_names[i]; i++)
	{
	  if (!attr_map[i] && strcmp (attribute_names[i], name) == 0)
	    {
	      if (found)
		{
		  g_set_error (error,
			       G_MARKUP_ERROR,
			       G_MARKUP_ERROR_INVALID_CONTENT,
			       "Duplicate attribute '%s'", name);
		  return FALSE;
		}
	  
	      *loc = attribute_values[i];
	      found = TRUE;
	      attr_map[i] = TRUE;
	    }
	}
      
      if (!found && mandatory)
	{
	  g_set_error (error,
		       G_MARKUP_ERROR,
		       G_MARKUP_ERROR_INVALID_CONTENT,
		       "Missing attribute '%s'", name);
	  return FALSE;
	}
      
      name = va_arg (vap, const char *);
    }

  for (i = 0; i < nattrs; i++)
    if (!attr_map[i])
      {
	g_set_error (error,
		     G_MARKUP_ERROR,
		     G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
		     "Unknown attribute '%s'", attribute_names[i]);
	      return FALSE;
      }

  return TRUE;
}

static gboolean
get_int (const char *str,
	 int        *result)
{
  long val;
  char *p;

  val = strtol (str, &p, 0);
  if (*str == '\0' || *p != '\0' ||
      val < G_MININT || val > G_MAXINT)
    return FALSE;

  *result = val;

  return TRUE;
}

static void
add_source (ParseData *parse_data,
	    int        size,
	    int        gridsize)
{
  CursorSource *source = g_new0 (CursorSource, 1);
  source->size = size;
  source->gridsize = gridsize;
  parse_data->theme->sources = g_slist_prepend (parse_data->theme->sources, source);
}

static void
add_alias (ParseData  *parse_data,
	   const char *name,
	   const char *target)
{
  CursorAlias *alias = g_new0 (CursorAlias, 1);
  alias->name = g_strdup (name);
  alias->target = g_strdup (target);

  g_hash_table_insert (parse_data->theme->aliases, alias->name, alias);
}

static gboolean
add_image (ParseData  *parse_data,
	   const char *file,
	   int         use,
	   GError    **error)
{
  GdkPixbuf *pixbuf;
  CursorSource *source;
  CursorImage *image;

  pixbuf = gdk_pixbuf_new_from_file (file, error);
  if (!pixbuf)
    return FALSE;

  source = parse_data->theme->sources->data;
  image = g_new0 (CursorImage, 1);
  image->image = pixbuf;
  image->use = use;
  source->images = g_slist_prepend (source->images, image);

  return TRUE;
}

static void
add_cursor (ParseData  *parse_data,
	    const char *name)
{
  Cursor *cursor = g_new0 (Cursor, 1);
  cursor->name = g_strdup (name);
  cursor->row = parse_data->row;
  cursor->column = parse_data->column;
  parse_data->current_cursor = cursor;
  g_hash_table_insert (parse_data->theme->cursors, cursor->name, cursor);
}

static void
add_frame_config (ParseData  *parse_data,
		  int         delay)
{
  Cursor *cursor = parse_data->current_cursor;
  CursorFrameConfig *frame_config = g_new0 (CursorFrameConfig, 1);
  frame_config->delay = delay;
  cursor->frame_configs = g_slist_prepend (cursor->frame_configs, frame_config);
}

/* Called for open tags <foo bar="baz"> */
static void
cursor_theme_start_element (GMarkupParseContext *context,
			    const gchar         *element_name,
			    const gchar        **attribute_names,
			    const gchar        **attribute_values,
			    gpointer             user_data,
			    GError             **error)
{
  ParseData *parse_data = user_data;

  switch (parse_data->state)
    {
    case OUTSIDE:
      {
	const char *name;
	
	if (!expect_tag (context, element_name, "theme", error))
	  return;
	if (parse_data->seen_theme)
	  {
	    g_set_error (error,
			 G_MARKUP_ERROR,
			 G_MARKUP_ERROR_INVALID_CONTENT,
			 "Multiple occurrences of <theme>");
	    return;
	  }
	parse_data->seen_theme = TRUE;
	parse_data->state = IN_THEME;

	if (!extract_attrs (context, attribute_names, attribute_values, error,
			    "name", TRUE, &name,
			    NULL))
	  return;

	parse_data->theme->name = g_strdup (name);
      }
      break;
    case IN_THEME:
      if (strcmp (element_name, "source") == 0)
	{
	  const char *size_str;
	  const char *gridsize_str;
	  int size, gridsize;
	  
	  parse_data->state = IN_SOURCE;
	  
	  if (!extract_attrs (context, attribute_names, attribute_values, error,
			      "size", TRUE, &size_str,
			      "gridsize", FALSE, &gridsize_str,
			      NULL))
	    return;
	  
	  if (!get_int (size_str, &size) || size < 0)
	    {
	      g_set_error (error,
			   G_MARKUP_ERROR,
			   G_MARKUP_ERROR_INVALID_CONTENT,
			   "Invalid size %s", size_str);
	      return;
	    }
	  
	  if (gridsize_str)
	    {
	      if (!get_int (gridsize_str, &gridsize) || size < 0)
		{
		  g_set_error (error,
			       G_MARKUP_ERROR,
			       G_MARKUP_ERROR_INVALID_CONTENT,
			       "Invalid size %s", size_str);
		  return;
		}
	    }
	  else
	    gridsize = size;
	  
	  add_source (parse_data, size, gridsize);
	}
      else if (strcmp (element_name, "alias") == 0)
	{
	  const char *name;
	  const char *target;

	  parse_data->state = IN_ALIAS;

	  if (!extract_attrs (context, attribute_names, attribute_values, error,
			      "name", TRUE, &name,
			      "target", FALSE, &target,
			      NULL))
	    return;

	  add_alias (parse_data, name, target);
	}
      else if (strcmp (element_name, "layout") == 0)
	{
	  if (parse_data->seen_layout)
	    {
	      g_set_error (error,
			   G_MARKUP_ERROR,
			   G_MARKUP_ERROR_INVALID_CONTENT,
			   "Multiple occurrences of <layout>");
	      return;
	    }
	  parse_data->seen_layout = TRUE;
	  parse_data->state = IN_LAYOUT;
	  
	  if (!extract_attrs (context, attribute_names, attribute_values, error,
			      NULL))
	    return;
	}
      else
	expect_tag (context, element_name, NULL, error);
      break;
    case IN_SOURCE:
      {
	const char *file;
	const char *use_str;
	int use;
	
	if (!expect_tag (context, element_name, "image", error))
	  return;
	parse_data->state = IN_IMAGE;
	
	if (!extract_attrs (context, attribute_names, attribute_values, error,
			    "file", TRUE, &file,
			    "use", TRUE, &use_str,
			    NULL))
	  return;

	if (strcmp (use_str, "hotspot") == 0)
	  use = -1;
	else
	  {
	    if (!get_int (use_str, &use) || use < 0)
	      {
		g_set_error (error,
			     G_MARKUP_ERROR,
			     G_MARKUP_ERROR_INVALID_CONTENT,
			     "Invalid use value %s", use_str);
		return;
	      }
	  }

	add_image (parse_data, file, use, error);
      }

      break;
    case IN_LAYOUT:
      if (!expect_tag (context, element_name, "row", error))
	return;
      parse_data->state = IN_ROW;
      parse_data->column = 0;
      break;
    case IN_ROW:
      {
	const char *name;
	
	if (!expect_tag (context, element_name, "cursor", error))
	  return;
	parse_data->state = IN_CURSOR;
	
	if (!extract_attrs (context, attribute_names, attribute_values, error,
			    "name", TRUE, &name,
			    NULL))
	  return;

	add_cursor (parse_data, name);
      }
      break;
    case IN_CURSOR:
      {
	const char *delay_str = NULL;
	int delay = -1;
      
	if (!expect_tag (context, element_name, "frame", error))
	  return;
	parse_data->state = IN_FRAME;
	
	if (!extract_attrs (context, attribute_names, attribute_values, error,
			    "delay", FALSE, &delay_str,
			    NULL))
	  return;

	if (delay_str)
	  {
	    if (!get_int (delay_str, &delay) || delay < 0)
	      {
		g_set_error (error,
			     G_MARKUP_ERROR,
			     G_MARKUP_ERROR_INVALID_CONTENT,
			     "Invalid delay value '%s'", delay_str);
		return;
	      }
	  }

	add_frame_config (parse_data, delay);
      }
      break;
    case IN_ALIAS:
    case IN_IMAGE:
    case IN_FRAME:
      expect_tag (context, element_name, NULL, error);
      break;
    }
}
  
/* Called for close tags </foo> */
static void
cursor_theme_end_element (GMarkupParseContext *context,
                          const gchar         *element_name,
                          gpointer             user_data,
                          GError             **error)
{
  ParseData *parse_data = user_data;
  
  switch (parse_data->state)
    {
    case OUTSIDE:
      g_assert_not_reached ();
      break;
    case IN_THEME:
      parse_data->state = OUTSIDE;
      parse_data->theme->sources = g_slist_reverse (parse_data->theme->sources);
      break;
    case IN_SOURCE:
      {
	CursorSource *source = parse_data->theme->sources->data;
	source->images = g_slist_reverse (source->images);
	parse_data->state = IN_THEME;
      }
      break;
    case IN_ALIAS:
      parse_data->state = IN_THEME;
      break;
    case IN_IMAGE:
      parse_data->state = IN_SOURCE;
      break;
    case IN_LAYOUT:
      parse_data->state = IN_THEME;
      break;
    case IN_ROW:
      parse_data->state = IN_LAYOUT;
      parse_data->row++;
      break;
    case IN_CURSOR:
      {
	Cursor *cursor = parse_data->current_cursor;
	if (!cursor->frame_configs)
	  add_frame_config (parse_data, -1);
	cursor->frame_configs = g_slist_reverse (cursor->frame_configs);
	parse_data->state = IN_ROW;
	parse_data->column++;
	parse_data->current_cursor = NULL;
      }
      break;
    case IN_FRAME:
      parse_data->state = IN_CURSOR;
      break;
    }
}

/* Called for character data */
/* text is not nul-terminated */
static void
cursor_theme_text (GMarkupParseContext *context,
		   const gchar         *text,
		   gsize                text_len,  
		   gpointer             user_data,
		   GError             **error)
{
  int i;

  for (i = 0; i < text_len; i++)
    if (!g_ascii_isspace (text[i]))
      {
	g_set_error (error,
		     G_MARKUP_ERROR,
		     G_MARKUP_ERROR_INVALID_CONTENT,
		     "Unexpected text in theme file");
	return;
      }
}

/* Called for strings that should be re-saved verbatim in this same
 * position, but are not otherwise interpretable.  At the moment
 * this includes comments and processing instructions.
 */
/* text is not nul-terminated. */
static void
cursor_theme_passthrough (GMarkupParseContext *context,
                          const gchar         *passthrough_text,
                          gsize                text_len,  
                          gpointer             user_data,
                          GError             **error)
{
  /* do nothing */
}

/* Called on error, including one set by other
 * methods in the vtable. The GError should not be freed.
 */
static void
cursor_theme_error (GMarkupParseContext *context,
		    GError              *error,
		    gpointer             user_data)
{
}

static GMarkupParser cursor_theme_parse = {
  cursor_theme_start_element,
  cursor_theme_end_element,
  cursor_theme_text,
  cursor_theme_passthrough,
  cursor_theme_error
};

static void
cursor_theme_free (CursorTheme *theme)
{
}

CursorTheme *
cursor_theme_read (const char *filename)
{
  ParseData parse_data;
  GMarkupParseContext *context;
  GError *error = NULL;
  char *text;
  gboolean have_error = FALSE;
  size_t len;

  if (!g_file_get_contents (filename, &text, &len, &error))
    {
      g_printerr ("Cannot read theme definition file: %s\n", error->message);
      g_error_free (error);
      return NULL;
    }

  parse_data.theme = g_new0 (CursorTheme, 1);
  parse_data.theme->cursors = g_hash_table_new (g_str_hash, g_str_equal);
  parse_data.theme->aliases = g_hash_table_new (g_str_hash, g_str_equal);
  
  parse_data.state = OUTSIDE;
  parse_data.seen_theme = FALSE;
  parse_data.seen_layout = FALSE;
  parse_data.row = 0;
  parse_data.column = 0;
  
  context = g_markup_parse_context_new (&cursor_theme_parse, 0,
					&parse_data, NULL);

  if (!g_markup_parse_context_parse (context, text, len, &error) ||
      !g_markup_parse_context_end_parse (context, &error))
    {
      g_printerr ("Error parsing theme definition file: %s\n", error->message);
      have_error = TRUE;
      g_error_free (error);
    }
  else if (!parse_data.seen_theme)
    {
      g_printerr ("Did not find <theme> element in theme file\n");
      have_error = TRUE;
    }
  else if (!parse_data.seen_layout)
    {
      g_printerr ("Did not find <layout> element in theme file\n");
      have_error = TRUE;
    }
  else if (!parse_data.theme->sources)
    {
      g_printerr ("No <source> element in theme file\n");
      have_error = TRUE;
    }
    
  g_markup_parse_context_free (context);

  if (!have_error)
    return parse_data.theme;
  else
    {
      cursor_theme_free (parse_data.theme);
      return NULL;
    }
}