File: xpenguins_theme.c

package info (click to toggle)
xpenguins-applet 2.1.1-8
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,228 kB
  • ctags: 371
  • sloc: ansic: 3,989; sh: 3,792; xml: 457; makefile: 145
file content (968 lines) | stat: -rw-r--r-- 26,990 bytes parent folder | download | duplicates (5)
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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
/* xpenguins_theme.c - provides the themability of xpenguins
 * Copyright (C) 1999-2001  Robin Hogan
 *
 *  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 of the License, 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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <glob.h>

#include "xpenguins.h"
#include <X11/xpm.h>

char *xpenguins_directory = XPENGUINS_SYSTEM_DIRECTORY;
char xpenguins_verbose = 1;

#define MAX_STRING_LENGTH 513

/* Print a warning message */
static
void
__xpenguins_out_of_memory(int line)
{
  fprintf(stderr, _("%s: Out of memory at line %d\n"), __FILE__, line);
}

#define xpenguins_out_of_memory() __xpenguins_out_of_memory(__LINE__)

/* Initialise an XPenguinsTheme structure ready to be filled
 * with data */
static
int
__xpenguins_theme_init(XPenguinsTheme *theme)
{
  int j;
  if (!(theme->data = malloc(PENGUIN_NGENERA * sizeof(ToonData *)))) {
    return 1;
  }
  if (!(theme->data[0] = malloc(PENGUIN_NTYPES * sizeof(ToonData)))) {
    return 1;
  }
  if (!(theme->name = malloc(PENGUIN_NGENERA * sizeof(char *)))) {
    return 1;
  }
  theme->name[0] = NULL;
  if (!(theme->number = malloc(PENGUIN_NGENERA * sizeof(int)))) {
    return 1;
  }
  theme->number[0] = 1;
  theme->ngenera = 1;
  theme->delay = 60; /* milliseconds */
  theme->_nallocated = PENGUIN_NGENERA;

  for (j = 0; j < PENGUIN_NTYPES; ++j) {
    ToonData *data = theme->data[0] + j;
    data->exists = 0;
    data->image = NULL;
    data->filename = NULL;
  }
  return 0;
}

/* Grow a theme ready to accept another genus. */
static
int
__xpenguins_theme_grow(XPenguinsTheme *theme)
{
  int j;
  if (theme->_nallocated == theme->ngenera) {
    if (!(theme->data = realloc(theme->data, (theme->ngenera + PENGUIN_NGENERA)
				* sizeof(ToonData *)))) {
      return 1;
    }
    if (!(theme->name = realloc(theme->name, (theme->ngenera + PENGUIN_NGENERA)
				* sizeof(char *)))) {
      return 1;
    }
    if (!(theme->number = realloc(theme->number, (theme->ngenera + PENGUIN_NGENERA)
				  * sizeof(int)))) {
      return 1;
    }
    theme->_nallocated += PENGUIN_NGENERA;
  }

  if (!(theme->data[theme->ngenera] = malloc(PENGUIN_NTYPES * sizeof(ToonData)))) {
    return 1;
  }
  theme->name[theme->ngenera] = NULL;
  theme->number[theme->ngenera] = 1;

  for (j = 0; j < PENGUIN_NTYPES; ++j) {
    ToonData *data = theme->data[theme->ngenera] + j;
    data->exists = 0;
    data->image = NULL;
    data->filename = NULL;
  }
  ++(theme->ngenera);
  return 0;
}

/* Set the data directory to use to find themes */
void
xpenguins_set_directory(char *directory)
{
  xpenguins_directory = directory;
  return;
}     

/* Remove underscores from a theme name */
char *
xpenguins_remove_underscores(char *name)
{
  char *ch = name;
  while (*ch != '\0') {
    if (*ch == '_') {
      *ch = ' ';
    }
    ++ch;
  }
  return name;
}

/* Return a NULL-terminated list of names of apparently valid themes -
 * basically the directory names from either the user or the system
 * theme directories that contain a file called "config" are
 * returned. Underscores in the directory names are converted into
 * spaces, but directory names that already contain spaces are
 * rejected. This is because the install program seems to choke on
 * directory names containing spaces, but theme names containing
 * underscores are ugly. The list should be freed with
 * xpenguins_free_list(). If nthemes is not NULL then it is used to
 * return the number of themes that were found */
char **
xpenguins_list_themes(int *nthemes)
{
  char *home = getenv("HOME");
  char *home_root = XPENGUINS_USER_DIRECTORY;
  char *themes = XPENGUINS_THEME_DIRECTORY;
  char *config = XPENGUINS_CONFIG;
  char *config_path, *tmp_path;
  char *list = NULL;
  char **list_array = NULL;
  int list_len = 0;
  int i, string_length = 0;
  glob_t globbuf;
  int n = 0;

  /* First add themes from the users home directory */
  string_length = strlen(home) + strlen(home_root)
    + strlen(themes) + 2 + strlen(config) + 1;
  config_path = malloc(string_length);
  if (!config_path) {
    xpenguins_out_of_memory();
    return NULL;
  }
  snprintf(config_path, string_length, "%s%s%s/*%s",
	   home, home_root, themes, config);
  if (glob(config_path, 0, NULL, &globbuf) == GLOB_NOSPACE) {
    free(config_path);
    xpenguins_out_of_memory();
    return NULL;
  }

  /* Then add system themes */
  string_length = strlen(xpenguins_directory) + strlen(themes) + 2
    + strlen(config) + 1;
  tmp_path = realloc(config_path, string_length);
  if (!tmp_path) {
    globfree(&globbuf);
    free(config_path);
    xpenguins_out_of_memory();
    return NULL;
  }
  else {
    config_path = tmp_path;
  }
  snprintf(config_path, string_length, "%s%s/*%s",
	   xpenguins_directory, themes, config);
  if (glob(config_path, GLOB_APPEND, NULL, &globbuf) == GLOB_NOSPACE) {
    free(config_path);
    xpenguins_out_of_memory();
    return NULL;
  }
  free(config_path);

  /* Now add just the sub directory name to a list */
  for (i = 0; i < globbuf.gl_pathc; ++i) {
    char *file_path = globbuf.gl_pathv[i];
    int file_path_len = strlen(file_path);
    char *dir_name = file_path + file_path_len - strlen(config);
    int dir_name_len = 0;
    char valid_name = 1;

    while (dir_name > file_path && dir_name[-1] != '/') {
      --dir_name;
      ++dir_name_len;
      /* We convert all underscores in the directory name 
       * to spaces, but actual spaces in the directory
       * name are not allowed. */
      if (*dir_name == ' ') {
	valid_name = 0;
	break;
      }
      else if (*dir_name == '_') {
	*dir_name = ' ';
      }
    }

    /* Check that theme with this name does not exist - a user may
     * wish to override a system theme, but we don't want duplication
     * in the list that is returned. */
    if (list && valid_name) {
      char *listp = list;
      char *namep = dir_name;
      char possible = 1;
      do {
	if (*listp == '/' || *listp == '\0') {
	  if (possible && *namep == '/') {
	    /* Found a duplicate name */
	    valid_name = 0;
	    break;
	  }
	  else {
	    namep = dir_name;
	    possible = 1;
	  }
	}
	else if (possible) {
	  if (*listp != *namep) {
	    possible = 0;
	  }
	  else {
	    ++namep;
	  }
	}
      }
      while (*(listp++) != '\0');
    }

    if (dir_name_len && valid_name) {
      /* Add theme name to list */
      char *tmp_list;
      if (list) {
	list[list_len-1] = '/';
      }
      list_len += (dir_name_len + 1);
      tmp_list = realloc(list, list_len);
      if (!tmp_list) {
	if (list) {
	  free(list);
	}
	xpenguins_out_of_memory();
	globfree(&globbuf);
	return NULL;
      }
      else {
	list = tmp_list;
      }

      strncpy(list + list_len - dir_name_len - 1, dir_name, dir_name_len);
      list[list_len - 1] = '\0';
      ++n;
    }
  }
  globfree(&globbuf);

  if (list_len) {
    /* Allocate the array of pointers to point to each theme name */
    char *ch = list;
    int j = 1;
    list_array = (char **) malloc((n+1) * sizeof(char *));
    if (!list_array) {
      if (list) {
	free(list);
      }
      xpenguins_out_of_memory();
      return NULL;
    }
    list_array[0] = list;
    while (j < n) {
      if (*ch == '/') {
	*ch = '\0';
	list_array[j++] = ++ch;
      }
      else {
	++ch;
      }
    }
    list_array[j] = NULL;
    if (nthemes) {
      *nthemes = n;
    }
    return list_array;
  }

  return NULL;
}

/* Generic function for freeing character lists that are allocated for
 * verious purposes. */
void
xpenguins_free_list(char **list)
{
  if (list) {
    if (*list) {
      free(*list);
    }
    free(list);
  }
}

/* Return the full path of the specified theme. Spaces in the theme
 * name are converted into underscores. */
char *
xpenguins_theme_directory(char *name)
{
  char *home = getenv("HOME");
  char *home_root = XPENGUINS_USER_DIRECTORY;
  char *themes = XPENGUINS_THEME_DIRECTORY;
  char *config = XPENGUINS_CONFIG;
  char *config_path, *tmp_path, *ch;
  int string_length = 0, root_length;
  struct stat stat_buf;

  /* First look in $HOME/.xpenguins/themes for config */
  root_length = strlen(home) + strlen(home_root) + strlen(themes) + 1;
  string_length = root_length + strlen(name) + strlen(config) + 1;
  config_path = malloc(string_length);
  if (!config_path) {
    xpenguins_out_of_memory();
    return NULL;
  }
  snprintf(config_path, string_length, "%s%s%s/%s%s",
	   home, home_root, themes, name, config);
  /* Convert spaces to underscores */
  ch = config_path + root_length;
  while (*ch != '/') {
    if (*ch == ' ') {
      *ch = '_';
    }
    ++ch;
  }
  /* See if theme exists */
  if (stat(config_path, &stat_buf) == 0) {
    config_path[string_length-strlen(config)-1] = '\0';
    return config_path;
  }
  /* Theme not found in users theme directory... */
  /* Now look in [xpenguins_directory]/themes for config */
  root_length = strlen(xpenguins_directory) + strlen(themes) + 1;
  string_length = root_length + strlen(name) + strlen(config) + 1;
  tmp_path = realloc(config_path, string_length);
  if (!tmp_path) {
    xpenguins_out_of_memory();
    free(config_path);
    return NULL;
  }
  else {
    config_path = tmp_path;
  }
  snprintf(config_path, string_length, "%s%s/%s%s",
	   xpenguins_directory, themes, name, config);
  /* Convert spaces to underscores */
  ch = config_path + root_length;
  while (*ch !='/') {
    if (*ch == ' ') {
      *ch = '_';
    }
    ++ch;
  }
  /* Look for theme */
  if (stat(config_path, &stat_buf) == 0) {
    config_path[string_length-strlen(config)-1] = '\0';
    return config_path;
  }
  /* Theme not found */
  return NULL;
}

/* Copy select properties from one ToonData structure to another */
static
void
__xpenguins_copy_properties(ToonData *src, ToonData *dest)
{
  dest->nframes = src->nframes;
  dest->ndirections = src->ndirections;
  dest->width = src->width;
  dest->height = src->height;
  dest->acceleration = src->acceleration;
  dest->speed = src->speed;
  dest->terminal_velocity = src->terminal_velocity;
  dest->conf = src->conf;
  dest->loop = src->loop;
  dest->master = src->master;
  return;
}

/* Append the theme named "name" to the theme structure "theme". To
 * initialise the theme, set theme->ngenera to 0 before calling this
 * function. On error, a static error message is returned, on success
 * NULL (i.e. 0) is returned. */
static
char *
__xpenguins_append_theme(char *name, XPenguinsTheme *theme)
{
  FILE *config;
  char *file_name = xpenguins_theme_directory(name);
  char *file_base, *xpm_file_name, *tmp_name;
  char word[MAX_STRING_LENGTH];
  static char *out_of_memory;
  ToonData def; /* The default */
  ToonData duf; /* If toon type is unknown... */
  ToonData *current = &def;
  int i, j, word_length, tmp_int;
  int max_file_name_length = 0;
  unsigned int genus = theme->ngenera;
  unsigned int first_genus = theme->ngenera;
  char started = 0;

  out_of_memory = _("Out of memory");

  /* Find theme */
  if (!file_name) {
    return _("Theme not found or config file not present");
  }

  /* Open config file */
  max_file_name_length = strlen(file_name) + 1 + MAX_STRING_LENGTH;
  tmp_name = realloc(file_name, max_file_name_length);
  if (!tmp_name) {
    free(file_name);
    return out_of_memory;
  }
  else {
    file_name = tmp_name;
  }
  file_base = file_name + strlen(file_name);
  *file_base = '/';
  ++file_base;
  snprintf(file_base, MAX_STRING_LENGTH, "config");
  config = fopen(file_name, "r");
  if (!config) {
    free(file_name);
    return _("Unreadable config file");
  }

  /* Allocate space for theme if this is the first one loaded */
  if (genus == 0) {
    if (__xpenguins_theme_init(theme)) {
      return out_of_memory;
    }
  }
  else {
    if (__xpenguins_theme_grow(theme)) {
      xpenguins_free_theme(theme);
      return out_of_memory;
    }
  }

  /* Set default values */
  def.nframes = 1;
  def.ndirections = 1;
  def.width = PENGUIN_DEFAULTWIDTH;
  def.height = PENGUIN_DEFAULTHEIGHT;
  def.acceleration = 0;
  def.speed = 4;
  def.terminal_velocity = 0;
  def.conf = TOON_DEFAULTS;
  def.loop = 0;
  def.master = NULL;

  /* Read config file */
#define NEXT_WORD() (word_length = \
      xpenguins_read_word(config, MAX_STRING_LENGTH, word))
#define WORD_IS(string) (strcasecmp(word, string) == 0)
#define CURRENT_TYPE(type) started = 1; __xpenguins_copy_properties(&def, \
      current = theme->data[genus] + type)
#define GET_INT(dest) if (NEXT_WORD() > 0) { dest = atoi(word); }
#define GET_UINT(dest) \
      if (NEXT_WORD() > 0 && (tmp_int = atoi(word)) >= 0) { dest = tmp_int; }

#define WARNING if(xpenguins_verbose) fprintf

  while(NEXT_WORD() != 0) {
    if (word_length < 0) {
      WARNING(stderr, _("Warning: %s: \"%s...\" ignored: longer than %d characters\n"),
		file_name, word, MAX_STRING_LENGTH);
      continue;
    }
    /* Define a new genus of toon */
    else if (WORD_IS("toon")) {
      if (NEXT_WORD() > 0) {
	if (started) {
	  if (__xpenguins_theme_grow(theme)) {
	    xpenguins_free_theme(theme);
	    return out_of_memory;
	  }
	  ++genus;
	}
	else {
	  started = 1;
	}
	if (!(theme->name[genus] = strdup(word))) {
	  xpenguins_free_theme(theme);
	  return out_of_memory;
	}
      }
      else if (word_length < 0) {
	WARNING(stderr, _("Warning: %s: \"%s...\" ignored: longer than %d characters\n"),
		file_name, word, MAX_STRING_LENGTH);
      }
      else {
	WARNING(stderr, _("Warning: theme config file ended unexpectedly\n"));
      }
    }
    /* Set preferred frame delay in milliseconds */
    else if (WORD_IS("delay")) { GET_UINT(theme->delay); }
    /* Various types of toon */
    else if (WORD_IS("define")) { 
      if (NEXT_WORD() > 0) {
	/* Start defining the default properties of the current genus */
	if (WORD_IS("default")) { current = &def; }
	/* Now the other types of toon */
	else if (WORD_IS("walker")) { CURRENT_TYPE(PENGUIN_WALKER); }
	else if (WORD_IS("faller")) { CURRENT_TYPE(PENGUIN_FALLER); }
	else if (WORD_IS("tumbler")) { CURRENT_TYPE(PENGUIN_TUMBLER); }
	else if (WORD_IS("floater")) { CURRENT_TYPE(PENGUIN_FLOATER); }
	else if (WORD_IS("climber")) { CURRENT_TYPE(PENGUIN_CLIMBER); }
	else if (WORD_IS("runner")) { CURRENT_TYPE(PENGUIN_RUNNER); }
	else if (WORD_IS("action0")) { CURRENT_TYPE(PENGUIN_ACTION0); }
	else if (WORD_IS("action1")) { CURRENT_TYPE(PENGUIN_ACTION1); }
	else if (WORD_IS("action2")) { CURRENT_TYPE(PENGUIN_ACTION2); }
	else if (WORD_IS("action3")) { CURRENT_TYPE(PENGUIN_ACTION3); }
	else if (WORD_IS("action4")) { CURRENT_TYPE(PENGUIN_ACTION4); }
	else if (WORD_IS("action5")) { CURRENT_TYPE(PENGUIN_ACTION5); }
	else if (WORD_IS("exit"))  {
	  CURRENT_TYPE(PENGUIN_EXIT);
	  current->conf |= (TOON_NOCYCLE | TOON_INVULNERABLE);
	}
	else if (WORD_IS("explosion")) {
	  CURRENT_TYPE(PENGUIN_EXPLOSION);
	  current->conf |= (TOON_NOCYCLE | TOON_INVULNERABLE | TOON_NOBLOCK);
	}
	else if (WORD_IS("splatted")) {
	  CURRENT_TYPE(PENGUIN_SPLATTED);
	  current->conf |= (TOON_NOCYCLE | TOON_INVULNERABLE);
	}
	else if (WORD_IS("squashed")) {
	  CURRENT_TYPE(PENGUIN_SQUASHED);
	  current->conf |= (TOON_NOCYCLE | TOON_INVULNERABLE | TOON_NOBLOCK);
	}
	else if (WORD_IS("zapped")) {
	  CURRENT_TYPE(PENGUIN_ZAPPED);
	  current->conf |= (TOON_NOCYCLE | TOON_INVULNERABLE | TOON_NOBLOCK);
	}
	else if (WORD_IS("angel")) {
	  CURRENT_TYPE(PENGUIN_ANGEL);
	  current->conf |= (TOON_INVULNERABLE | TOON_NOBLOCK);
	}
	else /* Unknown type! */ {
	  WARNING(stderr, _("Warning: unknown type \"%s\": ignoring\n"), word);
	  current = &duf;
	}
      }
      else {
	WARNING(stderr, _("Warning: theme config file ended unexpectedly\n"));
      }
    }
    /* Toon properties */
    else if (WORD_IS("width")) { GET_UINT(current->width); }
    else if (WORD_IS("height")) { GET_UINT(current->height); }
    else if (WORD_IS("frames")) { GET_UINT(current->nframes); }
    else if (WORD_IS("directions")) { GET_UINT(current->ndirections); }
    else if (WORD_IS("speed")) { GET_UINT(current->speed); }
    else if (WORD_IS("acceleration")) { GET_UINT(current->acceleration); }
    else if (WORD_IS("terminal_velocity")) { GET_UINT(current->terminal_velocity); }
    else if (WORD_IS("loop")) { GET_INT(current->loop); }
    else if (WORD_IS("pixmap")) { 
      if (NEXT_WORD() > 0) {
	if (current == &def) {
	  WARNING(stderr, _("Warning: theme config file may not specify a default pixmap\n"));
	}
	else if (current == &duf) {
	  /* Don't want to allocate any memory for an unused type */
	  continue;
	}
	else {
	  int status;
	  int igenus, itype; /* For scanning for duplicated pixmaps */
	  char new_pixmap = 1;
	  if (word[0] == '/') {
	    xpm_file_name = word;
	  }
	  else {
	    snprintf(file_base, MAX_STRING_LENGTH, word);
	    xpm_file_name = file_name;
	  }
	  if (current->image) {
	    /* Pixmap is already defined! */
	    WARNING(stderr, _("Warning: resetting pixmap to %s\n"), word);
	    if (!current->master) {
	      /* Free old pixmap if it is not a copy */
	      XpmFree(current->image);
	      current->image = NULL;
	      if (current->filename) {
		free(current->filename);
	      }
	      current->exists = 0;
	    }
	  }

	  /* Check if the file has been used before, but only look in
             the pixmaps for the current theme... */
	  for (igenus = first_genus; igenus <= genus && new_pixmap; ++igenus) {
	    ToonData *data = theme->data[igenus];
	    for (itype = 0; itype < PENGUIN_NTYPES && new_pixmap; ++itype) {
	      if (data[itype].filename && !data[itype].master
		  && data[itype].exists
		  && strcmp(xpm_file_name, data[itype].filename) == 0) {
		current->master = data + itype;
		current->exists = 1;
		current->filename = data[itype].filename;
		current->image = data[itype].image;
		new_pixmap = 0;
	      }
	    }
	  }

	  if (new_pixmap) {
	    status = XpmReadFileToData(xpm_file_name, &(current->image));
	    switch (status) {
	    case XpmSuccess:
	      current->exists = 1;
	      current->filename = strdup(xpm_file_name);
	      current->master = NULL;
	      break;
	    case XpmNoMemory:
	      fclose(config);
	      free(file_name);
	      xpenguins_free_theme(theme);
	      return out_of_memory;
	      break;
	    case XpmOpenFailed:
	      WARNING(stderr, _("Warning: could not read %s\n"), xpm_file_name);
	      break;
	    case XpmFileInvalid:
	      WARNING(stderr, _("Warning: %s is not a valid xpm file\n"), xpm_file_name);
	      break;
	    }
	  }
	}	      
      }
      else if (word_length < 0) {
	WARNING(stderr, _("Warning: %s: \"%s...\" ignored: longer than %d characters\n"),
		  file_name, word, MAX_STRING_LENGTH);
      }
      else {
	WARNING(stderr, _("Warning: theme config file ended unexpectedly\n"));
      }
    }
    else if (WORD_IS("number")) {
      GET_UINT(theme->number[genus]); 
    }
    else {
      WARNING(stderr, _("Warning: %s ignored in config file\n"), word);
    }
  }
  fclose(config);
  free(file_name);

  theme->ngenera = genus+1;

  /* Now validate our widths, heights etc. with the size of the image */
  for (i = first_genus; i < theme->ngenera; ++i) {
    for (j = 0; j < PENGUIN_NTYPES; ++j) {
      current = theme->data[i] + j;
      if (current->exists) {
	unsigned int imwidth, imheight;
	int values;
	values = sscanf(current->image[0], "%d %d", &imwidth, &imheight);
	if (values < 2) {
	  WARNING(stderr, _("Error reading width and height of %s\n"),
		  current->filename);
	  xpenguins_free_theme(theme);
	  return _("Invalid xpm data");
	}
	if (imwidth < current->width * current->nframes) {
	  if ((current->nframes = imwidth / current->width) < 1) {
	    xpenguins_free_theme(theme);
	    return _("Width of xpm image too small for even a single frame");
	  }
	  else {
	    WARNING(stderr, _("Warning: width of %s is too small to display all the frames\n"),
		    current->filename);
	  }
	}
	if (imheight < current->height * current->ndirections) {
	  if ((current->ndirections = imheight / current->height) < 1) {
	    xpenguins_free_theme(theme);
	    return _("Height of xpm image too small for even a single frame");
	  }
	  else {
	    WARNING(stderr, _("Warning: height of %s is too small to display all the frames\n"),
		    current->filename);
	  }
	}
      }
    }
    if ((!theme->data[i][PENGUIN_WALKER].exists)
	|| (!theme->data[i][PENGUIN_FALLER].exists)) {
      xpenguins_free_theme(theme);
      return _("Theme must contain at least walkers and fallers");
    }
  }

  /* Work out the total number */
  for (i = first_genus; i < theme->ngenera; ++i) {
    theme->total += theme->number[i];
  }
  if (theme->total > PENGUIN_MAX) {
    theme->total = PENGUIN_MAX;
  }

  penguin_data = theme->data;
  penguin_ngenera = theme->ngenera;
  penguin_numbers = theme->number;
  return NULL;
}

/* Load the named theme */
char *
xpenguins_load_theme(char *name, XPenguinsTheme *theme)
{
  theme->ngenera = 0;
  theme->total = 0;
  return __xpenguins_append_theme(name, theme);
}

/* Load the themes in the string array (where the array is NULL
 * terminated, as well as all the strings that are pointed to) */
char *
xpenguins_load_themes(char **names, XPenguinsTheme *theme)
{
  theme->ngenera = 0;
  theme->total = 0;
  while (*names) {
    char *error;
    error = __xpenguins_append_theme(*names, theme);
    if (error) {
      return error;
    }
    ++names;
  }
  return NULL;
}

/* Free all the data associated with a theme */
void
xpenguins_free_theme(XPenguinsTheme *theme)
{
  int i, j;
  for (i = 0; i < theme->ngenera; ++i) {
    for (j = 0; j < PENGUIN_NTYPES; ++j) {
      ToonData *data = theme->data[i] + j;
      if (data->exists && data->image && !data->master) {
	XpmFree(data->image);
	data->exists = 0;
	data->image = NULL;
	if (data->filename) {
	  free(data->filename);
	  data->filename = NULL;
	}
      }
    }
    if (theme->name[i]) {
      free(theme->name[i]);
      theme->name[i] = NULL;
    }
    free(theme->data[i]);
  }
  free(theme->data);
  free(theme->name);
  free(theme->number);
  theme->_nallocated = 0;
  theme->ngenera = 0;
  return;
}

/* Print basic theme information to standard error - for debugging
 * purposes */
void
xpenguins_describe_theme(XPenguinsTheme *theme)
{
  int i, j;
  if (!theme) {
    fprintf(stderr, _("No theme!\n"));
    return;
  }
  for (i = 0; i < theme->ngenera; ++i) {
    if (theme->name[i]) {
      fprintf(stdout, "%s\n", theme->name[i]);
    }
    for (j = 0; j < PENGUIN_NTYPES; ++j) {
      ToonData *data = theme->data[i] + j;
      if (data->exists) {
	fprintf(stderr, _("  Toon %d: %s %ux%u %ux%u\n"), i, data->filename,
		data->nframes, data->ndirections, data->width, data->height);
      }
    }
  }
}

#define ADD_STRING(index) \
  if (string_length) { \
    if (!(tmp_string = realloc(list, list_length + string_length + 1))) { \
       xpenguins_out_of_memory(); \
       free(file_name); free(theme_dir); free(list); \
       return NULL; \
    } \
    else { list = tmp_string; \
       offsets[index] = list_length; \
       strncpy(list + offsets[index], string, string_length + 1); \
       list_length += (string_length + 1); \
    }; \
  }

/* Read the "about" file in a theme directory and return a string list
 * of theme properties. This list should be freed with
 * xpenguins_free_list(). */
char **
xpenguins_theme_info(char *name)
{
  char word[MAX_STRING_LENGTH];
  char string[MAX_STRING_LENGTH];
  char **info;
  char *list;
  char *theme_dir = xpenguins_theme_directory(name);
  char *file_name;
  char *about = "/about";
  FILE *about_file;

  int i, file_name_length;
  int word_length, string_length;
  int about_length = strlen(about);
  int theme_dir_length;
  int list_length = 0;
  int offsets[PENGUIN_NABOUTFIELDS];

  /* Determine "about" filename string */
  if (!theme_dir) {
    return NULL;
  }
  theme_dir_length = strlen(theme_dir);
  file_name_length = theme_dir_length + about_length + 1;
  file_name = malloc(file_name_length);
  if (!file_name) {
    xpenguins_out_of_memory();
    free(file_name);
    free(theme_dir);
    return NULL;
  }
  snprintf(file_name, file_name_length, "%s%s", theme_dir, about);

  for (i = 0; i < PENGUIN_NABOUTFIELDS; ++i) {
    offsets[i] = 0;
  }

  /* Open "about" file */
  about_file = fopen(file_name, "r");
  if (!about_file) {
    fprintf(stderr, _("Could not read %s\n"), file_name);
    free(file_name);
    free(theme_dir);
    return NULL;
  }

  list = malloc(1);
  if (!list) {
    xpenguins_out_of_memory();
    free(file_name);
    free(theme_dir);
    return NULL;
  }
  list_length = 1;
  *list = '\0';

  while ((word_length = xpenguins_read_word(about_file,
					    MAX_STRING_LENGTH,
					    word))) {
    char *tmp_string;
    string_length = xpenguins_read_line(about_file,
					MAX_STRING_LENGTH,
					string);

    if (WORD_IS("artist") && !offsets[PENGUIN_ARTIST]) {
      ADD_STRING(PENGUIN_ARTIST);
    }
    else if (WORD_IS("maintainer") && !offsets[PENGUIN_MAINTAINER]) {
      ADD_STRING(PENGUIN_MAINTAINER);
    }
    else if (WORD_IS("date") && !offsets[PENGUIN_DATE]) {
      ADD_STRING(PENGUIN_DATE);
    }
    else if (WORD_IS("copyright") && !offsets[PENGUIN_COPYRIGHT]) {
      ADD_STRING(PENGUIN_COPYRIGHT);
    }
    else if (WORD_IS("license") && !offsets[PENGUIN_LICENSE]) {
      ADD_STRING(PENGUIN_LICENSE);
    }
    else if (WORD_IS("comment") && !offsets[PENGUIN_COMMENT]) {
      ADD_STRING(PENGUIN_COMMENT);
    }
    else if (WORD_IS("icon") && !offsets[PENGUIN_ICON]) {
      if (string_length && string_length < MAX_STRING_LENGTH-1) {
	if (string[0] == '/') {
	  ADD_STRING(PENGUIN_ICON);
	}
	else if (!(tmp_string = realloc(list, list_length + theme_dir_length 
					+ string_length + 2 ))) {
	  xpenguins_out_of_memory();
	  free(file_name);
	  free(theme_dir);
	  free(list);
	  return NULL;
	} else {
	  list = tmp_string;
	  offsets[PENGUIN_ICON] = list_length;
	  snprintf(list + offsets[PENGUIN_ICON], theme_dir_length + string_length + 2,
		   "%s/%s", theme_dir, string);
	  list_length += (theme_dir_length + string_length + 2);
	}
      }
    }
  }

  /* Allocate array of strings */
  info = malloc(PENGUIN_NABOUTFIELDS * sizeof(char *));
  if (!info) {
    xpenguins_out_of_memory();
    free(file_name);
    free(theme_dir);
    return NULL;
  }

  info[0] = list;
  for (i = 1; i < PENGUIN_NABOUTFIELDS; ++i) {
    info[i] = list + offsets[i];
  }
  free(file_name);
  free(theme_dir);
  return info;
}