Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/cursor/cursorthemegen/main.c
blob: fa1d3d1a69442270ee7a2e1603799af08d12ecb4 (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
/*
 * 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 <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "cursortheme.h"

static CursorImage *
cursor_source_find_image (CursorSource *source,
			  int           use)
{
  GSList *tmp_list;

  for (tmp_list = source->images; tmp_list; tmp_list = tmp_list->next)
    {
      CursorImage *image = tmp_list->data;

      if (image->use == use)
	return image;
    }

  return NULL;
}

static gboolean
cursor_find_hotspot (Cursor       *cursor,
		     CursorSource *source,
		     int          *hot_x,
		     int          *hot_y)
{
  CursorImage *image;
  int x = cursor->column * source->gridsize;
  int y = cursor->row * source->gridsize;

  *hot_x = -1;
  *hot_y = -1;
  
  image = cursor_source_find_image (source, -1);
  if (!image)
    {
      g_printerr ("Source at size %d doesn't have a hotspot image\n",
		  source->size);
      return FALSE;
    }

  if (!gdk_pixbuf_get_has_alpha (image->image) ||
      gdk_pixbuf_get_colorspace (image->image) != GDK_COLORSPACE_RGB)
    {
      g_printerr ("Invalid format for hotspot file\n");
      return FALSE;
    }
  
  if (x + source->gridsize <= gdk_pixbuf_get_width (image->image) &&
      y + source->gridsize <= gdk_pixbuf_get_height (image->image))
    {
      int rowstride;
      guchar *pixels;
      int i, j;

      rowstride = gdk_pixbuf_get_rowstride (image->image);
      pixels = gdk_pixbuf_get_pixels (image->image) + y * rowstride + x * 4;
      for (j = 0; j < source->gridsize; j++)
	{
	  for (i = 0; i < source->gridsize; i++)
	    {
	      if (pixels[4*i + 3] > 0x80)
		{
		  if (*hot_x >=0 || *hot_y  >= 0)
		    {
		      g_printerr ("Multiple hotspots for cursor %s at size %d\n",
				  cursor->name, source->size);
		      return FALSE;
		    }
		  *hot_x = i;
		  *hot_y = j;
		}
	    }
	  pixels += rowstride;
	}
    }
  
  if (*hot_x == -1 || *hot_y == -1)
    {
      g_printerr ("Cannot find hotspot for cursor %s at size %d\n",
		  cursor->name, source->size);
      return FALSE;
    }

  return TRUE;
}

static gboolean
cursor_is_present (Cursor       *cursor,
		   CursorSource *source,
		   int           frame)
{
  CursorImage *image = cursor_source_find_image (source, frame);
  int x = cursor->column * source->gridsize;
  int y = cursor->row * source->gridsize;
  int i, j;
  
  const guchar *pixels;
  int rowstride;
  int n_channels;

  if (!image)
    return FALSE;

  if (gdk_pixbuf_get_colorspace (image->image) != GDK_COLORSPACE_RGB)
    {
      g_printerr ("Invalid format for image file\n");
      return FALSE;
    }

  if (x + source->gridsize > gdk_pixbuf_get_width (image->image) ||
      y + source->gridsize > gdk_pixbuf_get_height (image->image))
    return FALSE;

  rowstride = gdk_pixbuf_get_rowstride (image->image);
  n_channels = gdk_pixbuf_get_n_channels (image->image);

  if (n_channels == 3)
    return TRUE;
  
  pixels = gdk_pixbuf_get_pixels (image->image) + y * rowstride + x * 4;

  for (j = 0; j < source->gridsize; j++)
    {
      for (i = 0; i < source->gridsize; i++)
	{
	  if (pixels[4*i + 3] != 0)
	    return TRUE;
	}
      pixels += rowstride;
    }

  return FALSE;
}

static void
cursor_frame_find_bounds (Cursor       *cursor,
			  CursorFrame  *frame,
			  CursorSource *source,
			  int           frame_index,
			  int          *x,
			  int          *y,
			  int          *width,
			  int          *height)
{
  CursorImage *image = cursor_source_find_image (source, frame_index);
  int start_x = cursor->column * source->gridsize;
  int start_y = cursor->row * source->gridsize;
  int i, j;
  
  const guchar *pixels;
  int rowstride;
  int n_channels;

  rowstride = gdk_pixbuf_get_rowstride (image->image);
  n_channels = gdk_pixbuf_get_n_channels (image->image);

  if (n_channels == 3)
    {
      *x = start_x;
      *y = start_x;
      *width = source->gridsize;
      *height = source->gridsize;
      
      return;
    }
  else
    {
      int min_x = start_x + frame->hot_x;
      int max_x = start_x + frame->hot_x + 1;
      int min_y = start_y + frame->hot_y;
      int max_y = start_y + frame->hot_y + 1;
      
      pixels = gdk_pixbuf_get_pixels (image->image) + start_y * rowstride + start_x * 4;

      for (j = 0; j < source->gridsize; j++)
	{
	  for (i = 0; i < source->gridsize; i++)
	    {
	      if (pixels[4*i + 3] != 0)
		{
		  if (start_x + i < min_x)
		    min_x = start_x + i;
		  if (start_x + i >= max_x)
		    max_x = start_x + i + 1;
		  if (start_y + j < min_y)
		    min_y = start_y + j;
		  if (start_y + j >= max_y)
		    max_y = start_y + j + 1;
		}
	    }
	  
	  pixels += rowstride;
	}
	  
      *x = min_x;
      *y = min_y;
      *width = max_x - min_x;
      *height = max_y - min_y;
    }
}

static void
cursor_get_image (Cursor       *cursor,
		  CursorFrame  *frame,
		  CursorSource *source,
		  int           frame_index,
                  int		x,
                  int		y,
                  int           width,
                  int		height)
{
  CursorImage *image = cursor_source_find_image (source, frame_index);
  GdkPixbuf *tmp_pixbuf;
  
  /*cursor_frame_find_bounds (cursor, frame, source, frame_index,
			    &x, &y, &width, &height);
*/
  tmp_pixbuf = gdk_pixbuf_new_subpixbuf (image->image,
					 x, y, width, height);

  frame->hot_x -= x - cursor->column * source->gridsize;
  frame->hot_y -= y - cursor->row * source->gridsize;
  
  frame->image = gdk_pixbuf_copy (tmp_pixbuf);
  g_object_unref (tmp_pixbuf);
}

static gboolean
cursor_add_source (Cursor       *cursor,
		   CursorSource *source)
{
  GSList *tmp_list;
  int hot_x, hot_y;
  int i;
  int x, y, x2, y2, height, width;

  /* If the first frame is missing we silently treat
   * it as OK
   */
  if (!cursor_is_present (cursor, source, 0))
    return TRUE;
  
  if (!cursor_find_hotspot (cursor, source, &hot_x, &hot_y))
    return FALSE;

  /* generate a single hight width for all cursors in a
     given animation*/

  for (tmp_list = cursor->frame_configs, i = 0;
       tmp_list;
       tmp_list = tmp_list->next, i++)
    {
      CursorFrameConfig *frame_config = tmp_list->data;
      CursorFrame *frame;
      int tx, ty, tx2, ty2, twidth, theight;

      if (i != 0 && !cursor_is_present (cursor, source, i))
        continue;

      frame = g_new0 (CursorFrame, 1);
      frame->size = source->size;
      frame->hot_x = hot_x;
      frame->hot_y = hot_y;
      frame->delay = frame_config->delay;

      cursor_frame_find_bounds (cursor, frame, source, i,
                                &tx, &ty, &twidth, &theight);

      tx2 = tx + twidth;
      ty2 = ty + theight;

      if (i == 0)
        {
          x = tx;
          y = ty;
          x2 = tx2;
          y2 = ty2;
        } 
      else 
        {
          if (tx < x)
            x = tx;

          if (ty < y)
            y = ty;

          if (tx2 > x2)
            x2 = tx2;
	
          if (ty2 > y2)
            y2 = ty2;
        }
        
      g_free (frame);
    }

  width = x2 - x;
  height = y2 - y;

  for (tmp_list = cursor->frame_configs, i = 0;
       tmp_list;
       tmp_list = tmp_list->next, i++)
    {
      CursorFrameConfig *frame_config = tmp_list->data;
      CursorFrame *frame;

      if (i != 0 && !cursor_is_present (cursor, source, i))
	{
	  g_printerr ("Frame %d missing for cursor '%s' at size %d\n",
		      i, cursor->name, source->size);
	  frame = g_new0 (CursorFrame, 1);
	  frame->size = -1;
	  cursor->frames = g_slist_append (cursor->frames, frame);
	  continue;
	}

      frame = g_new0 (CursorFrame, 1);
      frame->size = source->size;
      frame->hot_x = hot_x;
      frame->hot_y = hot_y;
      frame->delay = frame_config->delay;

      cursor_get_image (cursor, frame, source, i, x, y, width, height);

      cursor->frames = g_slist_append (cursor->frames, frame);
    }
  
  return TRUE;
}

static gboolean
cursor_theme_read_cursor (CursorTheme *theme,
			  Cursor      *cursor)
{
  GSList *tmp_list;

  for (tmp_list = theme->sources; tmp_list; tmp_list = tmp_list->next)
    {
      if (!cursor_add_source (cursor, tmp_list->data))
	return FALSE;
    }

  return TRUE;
}

static void
cursor_theme_write_cursor (CursorTheme *theme,
			   Cursor      *cursor)
{
  GSList *tmp_list;
  char *config_filename;
  char *command;
  FILE *config_file;
  GError *error = NULL;
  int status;
  int i;

  if (g_hash_table_lookup (theme->aliases, cursor->name))
    {
      g_printerr ("Warning: cursor '%s' overridden by alias\n", cursor->name);
      return;
    }
  
  if (!cursor->frames)
    return;
  
  config_filename = g_strconcat (cursor->name, ".cfg", NULL);
  config_file = fopen (config_filename, "w");
  
  if (!config_file)
    {
      g_printerr ("Cannot open config file '%s'\n", config_filename);
      return;
    }

  for (tmp_list = cursor->frames, i = 0; tmp_list; tmp_list = tmp_list->next, i++)
    {
      CursorFrame *frame = tmp_list->data;
      char *filename;
      
      if (frame->size == -1)
	  continue;
      filename = g_strdup_printf ("%s-%d.png", cursor->name, i);
      if (gdk_pixbuf_save (frame->image, filename, "png", &error, NULL))
	{
	  if (frame->delay > 0)
	    fprintf (config_file, "%d %d %d %s %d\n",
		     frame->size, frame->hot_x, frame->hot_y, filename, frame->delay);
	  else
	    fprintf (config_file, "%d %d %d %s\n",
		     frame->size, frame->hot_x, frame->hot_y, filename);
	}
      else
	{
	  g_printerr ("Error saving image file: %s\n", error->message);
	  g_error_free (error);
	}
      g_free (filename);
    }

  fclose (config_file);

  command = g_strdup_printf ("sh -c 'xcursorgen %s > %s'\n",
			     config_filename, cursor->name);
  if (!g_spawn_command_line_sync (command, NULL, NULL, &status, &error))
    {
      g_printerr ("Error running xcursorgen for %s: %s\n",
		  cursor->name, error->message);
      g_error_free (error);
    }
  else if (status)
    {
      g_printerr ("Error running xcursorgen for %s\n",
		  cursor->name);
    }
  else
    {
      /* Only delete temporary files if no error occurred
       */
      unlink (config_filename);
      g_free (config_filename);
      
      for (tmp_list = cursor->frames, i = 0; tmp_list; tmp_list = tmp_list->next, i++)
	{
	  char *filename;
	  
	  filename = g_strdup_printf ("%s-%d.png", cursor->name, i);
	  unlink (filename);
	  g_free (filename);
	}
    }
}

static const char *
cursor_theme_check_alias (CursorTheme *theme,
			  CursorAlias *alias)
{
  /* Dereference, using tortoise-and-hare checkign for circular aliases
   */
  CursorAlias *tortoise = alias;
  CursorAlias *hare = alias;
  Cursor *target;

  while (TRUE)
    {
      CursorAlias *next;

      next = g_hash_table_lookup (theme->aliases, hare->target);
      if (!next)
	break;
      hare = next;

      if (hare == tortoise)
	goto found_loop;
      
      next = g_hash_table_lookup (theme->aliases, hare->target);
      if (!next)
	break;
      hare = next;

      if (hare == tortoise)
	goto found_loop;
      
      tortoise = g_hash_table_lookup (theme->aliases, tortoise->target);
    }

  target = g_hash_table_lookup (theme->cursors, hare->target);

  if (!target || !target->frames)
    {
      g_printerr ("Cursor '%s', which is the target of alias '%s', is not in theme\n",
		  hare->target, alias->name);
      return NULL;
    }

  return hare->target;

 found_loop:
  g_printerr ("Circular looop detected when dereferencing alias '%s'\n",
	      alias->name);
  return NULL;
}

static void
cursor_theme_write_alias (CursorTheme *theme,
			  CursorAlias *alias)
{
  const char *target = cursor_theme_check_alias (theme, alias);
  if (!target)
    return;

  if (symlink (target, alias->name) < 0)
    {
      g_printerr ("Error creating symlink for alias '%s' to '%s': %s\n",
		  alias->name, target, g_strerror (errno));
    }
}

static void
write_cursor_foreach (gpointer key,
		      gpointer value,
		      gpointer data)
{
  cursor_theme_write_cursor (data, value);
}

static void
write_alias_foreach (gpointer key,
		     gpointer value,
		     gpointer data)
{
  cursor_theme_write_alias (data, value);
}

static gboolean
cursor_theme_write (CursorTheme *theme,
		    const char  *output_dir)
{
  char *curdir;
  
  if (mkdir (output_dir, 0755) < 0 && errno != EEXIST)
    {
      g_printerr ("Error creating output directory '%s'\n: %s",
		  output_dir, g_strerror (errno));
      return FALSE;
    }

  curdir = g_get_current_dir ();
  if (chdir (output_dir) < 0)
    {
      g_printerr ("Could not change to output directory '%s'\n", output_dir);
      return FALSE;
    }

  g_hash_table_foreach (theme->cursors,
			write_cursor_foreach,
			theme);

  g_hash_table_foreach (theme->aliases,
			write_alias_foreach,
			theme);

  chdir (curdir);

  return TRUE;
}

void
usage (void)
{
  g_printerr ("Usage: cursorthemegen CONFIG_FILE OUTPUT_DIR\n");
  exit (1);
}

static void
read_cursor_foreach (gpointer key,
		     gpointer value,
		     gpointer data)
{
  if (!cursor_theme_read_cursor (data, value))
    exit (1);
}

int
main (int argc, char **argv)
{
  CursorTheme *theme;

  g_type_init ();
  
  if (argc != 3)
    usage ();

  theme = cursor_theme_read (argv[1]);
  if (!theme)
    exit (1);

  g_hash_table_foreach (theme->cursors,
			read_cursor_foreach,
			theme);

  if (!cursor_theme_write (theme, argv[2]))
    exit (1);

  return 0;
}