comparison audacious/widgets/skin.c @ 1653:a6e6d3500c13 trunk

[svn] - revert back to r2216
author nenolod
date Thu, 07 Sep 2006 22:26:54 -0700
parents
children 0abfd27b3849
comparison
equal deleted inserted replaced
1652:62c5bff8a05b 1653:a6e6d3500c13
1 /* BMP - Cross-platform multimedia player
2 * Copyright (C) 2003-2004 BMP development team.
3 *
4 * Based on XMMS:
5 * Copyright (C) 1998-2003 XMMS development team.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 /* TODO: enforce default sizes! */
27
28 #include <glib.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
33
34 #include "widgetcore.h"
35
36 #include "equalizer.h"
37 #include "main.h"
38 #include "ui_playlist.h"
39 #include "skinwin.h"
40 #include "util.h"
41
42 #include "debug.h"
43
44 #include "platform/smartinclude.h"
45
46 #define EXTENSION_TARGETS 7
47
48 static gchar *ext_targets[EXTENSION_TARGETS] = { "bmp", "xpm", "png", "svg",
49 "gif", "jpg", "jpeg" };
50
51 struct _SkinPixmapIdMapping {
52 SkinPixmapId id;
53 const gchar *name;
54 const gchar *alt_name;
55 gint width, height;
56 };
57
58 struct _SkinMaskInfo {
59 gint width, height;
60 gchar *inistr;
61 };
62
63 typedef struct _SkinPixmapIdMapping SkinPixmapIdMapping;
64 typedef struct _SkinMaskInfo SkinMaskInfo;
65
66
67 Skin *bmp_active_skin = NULL;
68
69 static gint skin_current_num;
70
71 static SkinMaskInfo skin_mask_info[] = {
72 {275, 116, "Normal"},
73 {275, 16, "WindowShade"},
74 {275, 116, "Equalizer"},
75 {275, 16, "EqualizerWS"}
76 };
77
78 static SkinPixmapIdMapping skin_pixmap_id_map[] = {
79 {SKIN_MAIN, "main", NULL, 0, 0},
80 {SKIN_CBUTTONS, "cbuttons", NULL, 0, 0},
81 {SKIN_SHUFREP, "shufrep", NULL, 0, 0},
82 {SKIN_TEXT, "text", NULL, 0, 0},
83 {SKIN_TITLEBAR, "titlebar", NULL, 0, 0},
84 {SKIN_VOLUME, "volume", NULL, 0, 0},
85 {SKIN_BALANCE, "balance", "volume", 0, 0},
86 {SKIN_MONOSTEREO, "monoster", NULL, 0, 0},
87 {SKIN_PLAYPAUSE, "playpaus", NULL, 0, 0},
88 {SKIN_NUMBERS, "nums_ex", "numbers", 0, 0},
89 {SKIN_POSBAR, "posbar", NULL, 0, 0},
90 {SKIN_EQMAIN, "eqmain", NULL, 0, 0},
91 {SKIN_PLEDIT, "pledit", NULL, 0, 0},
92 {SKIN_EQ_EX, "eq_ex", NULL, 0, 0}
93 };
94
95 static guint skin_pixmap_id_map_size = G_N_ELEMENTS(skin_pixmap_id_map);
96
97 static const guchar skin_default_viscolor[24][3] = {
98 {9, 34, 53},
99 {10, 18, 26},
100 {0, 54, 108},
101 {0, 58, 116},
102 {0, 62, 124},
103 {0, 66, 132},
104 {0, 70, 140},
105 {0, 74, 148},
106 {0, 78, 156},
107 {0, 82, 164},
108 {0, 86, 172},
109 {0, 92, 184},
110 {0, 98, 196},
111 {0, 104, 208},
112 {0, 110, 220},
113 {0, 116, 232},
114 {0, 122, 244},
115 {0, 128, 255},
116 {0, 128, 255},
117 {0, 104, 208},
118 {0, 80, 160},
119 {0, 56, 112},
120 {0, 32, 64},
121 {200, 200, 200}
122 };
123
124 static GdkBitmap *
125 skin_create_transparent_mask(const gchar *,
126 const gchar *,
127 const gchar *,
128 GdkWindow *,
129 gint, gint);
130
131 static void
132 skin_setup_masks(Skin * skin);
133
134 static void
135 skin_set_default_vis_color(Skin * skin);
136
137
138 void
139 skin_lock(Skin * skin)
140 {
141 g_mutex_lock(skin->lock);
142 }
143
144 void
145 skin_unlock(Skin * skin)
146 {
147 g_mutex_unlock(skin->lock);
148 }
149
150 gboolean
151 bmp_active_skin_reload(void)
152 {
153 return bmp_active_skin_load(bmp_active_skin->path);
154 }
155
156 gboolean
157 bmp_active_skin_load(const gchar * path)
158 {
159 g_return_val_if_fail(bmp_active_skin != NULL, FALSE);
160
161 memset(&bmp_active_skin->properties, 0, sizeof(SkinProperties));
162
163 if (!skin_load(bmp_active_skin, path))
164 return FALSE;
165
166 skin_setup_masks(bmp_active_skin);
167
168 if (cfg.playlist_transparent)
169 {
170 if (rootpix != NULL)
171 g_object_unref(rootpix);
172
173 rootpix = shade_pixmap(get_transparency_pixmap(), 0, 0, 0, 0, gdk_screen_width(), gdk_screen_height(),
174 skin_get_color(bmp_active_skin, SKIN_PLEDIT_NORMALBG));
175 }
176
177 draw_main_window(TRUE);
178 draw_playlist_window(TRUE);
179 draw_equalizer_window(TRUE);
180
181 vis_set_window(mainwin_vis, mainwin->window);
182 playlistwin_update_list();
183
184 return TRUE;
185 }
186
187 void
188 skin_pixmap_free(SkinPixmap * p)
189 {
190 g_return_if_fail(p != NULL);
191 g_return_if_fail(p->pixmap != NULL);
192
193 g_object_unref(p->pixmap);
194 p->pixmap = NULL;
195 }
196
197 Skin *
198 skin_new(void)
199 {
200 Skin *skin;
201 skin = g_new0(Skin, 1);
202 skin->lock = g_mutex_new();
203 return skin;
204 }
205
206 void
207 skin_free(Skin * skin)
208 {
209 gint i;
210
211 g_return_if_fail(skin != NULL);
212
213 skin_lock(skin);
214
215 for (i = 0; i < SKIN_PIXMAP_COUNT; i++)
216 skin_pixmap_free(&skin->pixmaps[i]);
217
218 for (i = 0; i < SKIN_PIXMAP_COUNT; i++) {
219 if (skin->masks[i])
220 g_object_unref(skin->masks[i]);
221
222 skin->masks[i] = NULL;
223 }
224
225 skin_set_default_vis_color(skin);
226 skin_unlock(skin);
227 }
228
229 void
230 skin_destroy(Skin * skin)
231 {
232 g_return_if_fail(skin != NULL);
233 skin_free(skin);
234 g_mutex_free(skin->lock);
235 g_free(skin);
236 }
237
238 const SkinPixmapIdMapping *
239 skin_pixmap_id_lookup(guint id)
240 {
241 guint i;
242
243 for (i = 0; i < skin_pixmap_id_map_size; i++) {
244 if (id == skin_pixmap_id_map[i].id) {
245 return &skin_pixmap_id_map[i];
246 }
247 }
248
249 return NULL;
250 }
251
252 const gchar *
253 skin_pixmap_id_to_name(SkinPixmapId id)
254 {
255 guint i;
256
257 for (i = 0; i < skin_pixmap_id_map_size; i++) {
258 if (id == skin_pixmap_id_map[i].id)
259 return skin_pixmap_id_map[i].name;
260 }
261 return NULL;
262 }
263
264 static void
265 skin_set_default_vis_color(Skin * skin)
266 {
267 memcpy(skin->vis_color, skin_default_viscolor,
268 sizeof(skin_default_viscolor));
269 }
270
271 /*
272 * I have rewritten this to take an array of possible targets,
273 * once we find a matching target we now return, instead of loop
274 * recursively. This allows for us to support many possible format
275 * targets for our skinning engine than just the original winamp
276 * formats.
277 *
278 * -- nenolod, 16 January 2006
279 */
280 gchar *
281 skin_pixmap_locate(const gchar * dirname, gchar ** basenames)
282 {
283 gchar *filename;
284 gint i;
285
286 for (i = 0; basenames[i]; i++)
287 if (!(filename = find_file_recursively(dirname, basenames[i])))
288 g_free(filename);
289 else
290 return filename;
291
292 /* can't find any targets -- sorry */
293 return NULL;
294 }
295
296 /* FIXME: this function is temporary. It will be removed when the skinning system
297 uses GdkPixbuf in place of GdkPixmap */
298
299 static GdkPixmap *
300 pixmap_new_from_file(const gchar * filename)
301 {
302 GdkPixbuf *pixbuf;
303 GdkPixmap *pixmap;
304 gint width, height;
305
306 if (!(pixbuf = gdk_pixbuf_new_from_file(filename, NULL)))
307 return NULL;
308
309 width = gdk_pixbuf_get_width(pixbuf);
310 height = gdk_pixbuf_get_height(pixbuf);
311
312 if (!(pixmap = gdk_pixmap_new(mainwin->window, width, height,
313 gdk_rgb_get_visual()->depth))) {
314 g_object_unref(pixbuf);
315 return NULL;
316 }
317
318 gdk_pixbuf_render_to_drawable(pixbuf, pixmap, mainwin_gc, 0, 0, 0, 0,
319 width, height, GDK_RGB_DITHER_MAX, 0, 0);
320 g_object_unref(pixbuf);
321
322 return pixmap;
323 }
324
325 static gboolean
326 skin_load_pixmap_id(Skin * skin, SkinPixmapId id, const gchar * path_p)
327 {
328 const gchar *path;
329 gchar *filename;
330 gint width, height;
331 const SkinPixmapIdMapping *pixmap_id_mapping;
332 GdkPixmap *gpm;
333 SkinPixmap *pm = NULL;
334 gchar *basenames[EXTENSION_TARGETS * 2 + 1]; /* alternate basenames */
335 gint i, y;
336
337 g_return_val_if_fail(skin != NULL, FALSE);
338 g_return_val_if_fail(id < SKIN_PIXMAP_COUNT, FALSE);
339
340 pixmap_id_mapping = skin_pixmap_id_lookup(id);
341 g_return_val_if_fail(pixmap_id_mapping != NULL, FALSE);
342
343 memset(&basenames, 0, sizeof(basenames));
344
345 for (i = 0, y = 0; i < EXTENSION_TARGETS; i++, y++)
346 {
347 basenames[y] = g_strdup_printf("%s.%s", pixmap_id_mapping->name,
348 ext_targets[i]);
349
350 if (pixmap_id_mapping->alt_name)
351 basenames[++y] = g_strdup_printf("%s.%s",
352 pixmap_id_mapping->alt_name, ext_targets[i]);
353 }
354
355 path = path_p ? path_p : skin->path;
356 filename = skin_pixmap_locate(path, basenames);
357
358 for (i = 0; basenames[i] != NULL; i++)
359 {
360 g_free(basenames[i]);
361 basenames[i] = NULL;
362 }
363
364 if (!(gpm = pixmap_new_from_file(filename))) {
365 g_warning("loading of %s failed", filename);
366 g_free(filename);
367 return FALSE;
368 }
369
370 g_free(filename);
371
372 gdk_drawable_get_size(GDK_DRAWABLE(gpm), &width, &height);
373 pm = &skin->pixmaps[id];
374 pm->pixmap = gpm;
375 pm->width = width;
376 pm->height = height;
377 pm->current_width = width;
378 pm->current_height = height;
379
380 return TRUE;
381 }
382
383 void
384 skin_mask_create(Skin * skin,
385 const gchar * path,
386 gint id,
387 GdkWindow * window)
388 {
389 skin->masks[id] =
390 skin_create_transparent_mask(path, "region.txt",
391 skin_mask_info[id].inistr, window,
392 skin_mask_info[id].width,
393 skin_mask_info[id].height);
394 }
395
396 static void
397 skin_setup_masks(Skin * skin)
398 {
399 GdkBitmap *mask;
400
401 if (cfg.show_wm_decorations)
402 return;
403
404 if (cfg.player_visible) {
405 mask = skin_get_mask(skin, SKIN_MASK_MAIN + cfg.player_shaded);
406 gtk_widget_shape_combine_mask(mainwin, mask, 0, 0);
407 }
408
409 mask = skin_get_mask(skin, SKIN_MASK_EQ + cfg.equalizer_shaded);
410 gtk_widget_shape_combine_mask(equalizerwin, mask, 0, 0);
411 }
412
413 static GdkBitmap *
414 create_default_mask(GdkWindow * parent, gint w, gint h)
415 {
416 GdkBitmap *ret;
417 GdkGC *gc;
418 GdkColor pattern;
419
420 ret = gdk_pixmap_new(parent, w, h, 1);
421 gc = gdk_gc_new(ret);
422 pattern.pixel = 1;
423 gdk_gc_set_foreground(gc, &pattern);
424 gdk_draw_rectangle(ret, gc, TRUE, 0, 0, w, h);
425 gdk_gc_destroy(gc);
426
427 return ret;
428 }
429
430 static void
431 skin_query_color(GdkColormap * cm, GdkColor * c)
432 {
433 #ifdef GDK_WINDOWING_X11
434 XColor xc = { 0,0,0,0,0,0 };
435
436 xc.pixel = c->pixel;
437 XQueryColor(GDK_COLORMAP_XDISPLAY(cm), GDK_COLORMAP_XCOLORMAP(cm), &xc);
438 c->red = xc.red;
439 c->green = xc.green;
440 c->blue = xc.blue;
441 #else
442 /* do nothing. see what breaks? */
443 #endif
444 }
445
446 static glong
447 skin_calc_luminance(GdkColor * c)
448 {
449 return (0.212671 * c->red + 0.715160 * c->green + 0.072169 * c->blue);
450 }
451
452 static void
453 skin_get_textcolors(GdkPixmap * text, GdkColor * bgc, GdkColor * fgc)
454 {
455 /*
456 * Try to extract reasonable background and foreground colors
457 * from the font pixmap
458 */
459
460 GdkImage *gi;
461 GdkColormap *cm;
462 gint i;
463
464 g_return_if_fail(text != NULL);
465
466 /* Get the first line of text */
467 gi = gdk_drawable_get_image(text, 0, 0, 152, 6);
468 cm = gdk_window_get_colormap(playlistwin->window);
469 g_return_if_fail(GDK_IS_WINDOW(playlistwin->window));
470
471 for (i = 0; i < 6; i++) {
472 GdkColor c;
473 gint x;
474 glong d, max_d;
475
476 /* Get a pixel from the middle of the space character */
477 bgc[i].pixel = gdk_image_get_pixel(gi, 151, i);
478 skin_query_color(cm, &bgc[i]);
479
480 max_d = 0;
481 for (x = 1; x < 150; x++) {
482 c.pixel = gdk_image_get_pixel(gi, x, i);
483 skin_query_color(cm, &c);
484
485 d = labs(skin_calc_luminance(&c) - skin_calc_luminance(&bgc[i]));
486 if (d > max_d) {
487 memcpy(&fgc[i], &c, sizeof(GdkColor));
488 max_d = d;
489 }
490 }
491 }
492 gdk_image_destroy(gi);
493 }
494
495 gboolean
496 init_skins(const gchar * path)
497 {
498 bmp_active_skin = skin_new();
499
500 if (!bmp_active_skin_load(path)) {
501 /* FIXME: Oddly, g_message() causes a crash if path is NULL on
502 * Solaris (see bug #165) */
503 if (path)
504 g_message("Unable to load skin (%s), trying default...", path);
505
506 /* can't load configured skin, retry with default */
507 if (!bmp_active_skin_load(BMP_DEFAULT_SKIN_PATH)) {
508 g_message("Unable to load default skin (%s)! Giving up.",
509 BMP_DEFAULT_SKIN_PATH);
510 return FALSE;
511 }
512 }
513
514 if (cfg.random_skin_on_play)
515 skinlist_update();
516
517 return TRUE;
518 }
519
520 /*
521 * Opens and parses a skin's hints file.
522 * Hints files are somewhat like "scripts" in Winamp3/5.
523 * We'll probably add scripts to it next.
524 */
525 void
526 skin_parse_hints(Skin * skin, gchar *path_p)
527 {
528 gchar *filename, *tmp;
529
530 path_p = path_p ? path_p : skin->path;
531
532 filename = find_file_recursively(path_p, "skin.hints");
533
534 if (filename == NULL)
535 return;
536
537 #if 0
538 skin->description = read_ini_string(filename, "skin", "skinDescription");
539 #endif
540
541 tmp = read_ini_string(filename, "skin", "mainwinOthertext");
542
543 if (tmp != NULL)
544 skin->properties.mainwin_othertext = atoi(tmp);
545 }
546
547 static guint
548 hex_chars_to_int(gchar hi, gchar lo)
549 {
550 /*
551 * Converts a value in the range 0x00-0xFF
552 * to a integer in the range 0-65535
553 */
554 gchar str[3];
555
556 str[0] = hi;
557 str[1] = lo;
558 str[2] = 0;
559
560 return (CLAMP(strtol(str, NULL, 16), 0, 0xFF) << 8);
561 }
562
563 GdkColor *
564 skin_load_color(const gchar * path, const gchar * file,
565 const gchar * section, const gchar * key,
566 gchar * default_hex)
567 {
568 gchar *filename, *value;
569 GdkColor *color = NULL;
570
571 filename = find_file_recursively(path, file);
572 if (filename || default_hex) {
573 if (filename) {
574 value = read_ini_string(filename, section, key);
575 if (value == NULL) {
576 value = g_strdup(default_hex);
577 }
578 } else {
579 value = g_strdup(default_hex);
580 }
581 if (value) {
582 gchar *ptr = value;
583 gint len;
584
585 color = g_new0(GdkColor, 1);
586 g_strstrip(value);
587
588 if (value[0] == '#')
589 ptr++;
590 len = strlen(ptr);
591 /*
592 * The handling of incomplete values is done this way
593 * to maximize winamp compatibility
594 */
595 if (len >= 6) {
596 color->red = hex_chars_to_int(*ptr, *(ptr + 1));
597 ptr += 2;
598 }
599 if (len >= 4) {
600 color->green = hex_chars_to_int(*ptr, *(ptr + 1));
601 ptr += 2;
602 }
603 if (len >= 2)
604 color->blue = hex_chars_to_int(*ptr, *(ptr + 1));
605
606 gdk_color_alloc(gdk_window_get_colormap(playlistwin->window),
607 color);
608 g_free(value);
609 }
610 if (filename)
611 g_free(filename);
612 }
613 return color;
614 }
615
616
617
618 GdkBitmap *
619 skin_create_transparent_mask(const gchar * path,
620 const gchar * file,
621 const gchar * section,
622 GdkWindow * window,
623 gint width,
624 gint height)
625 {
626 GdkBitmap *mask = NULL;
627 GdkGC *gc = NULL;
628 GdkColor pattern;
629 GdkPoint *gpoints;
630
631 gchar *filename = NULL;
632 gboolean created_mask = FALSE;
633 GArray *num, *point;
634 guint i, j;
635 gint k;
636
637 if (path)
638 filename = find_file_recursively(path, file);
639
640 /* filename will be null if path wasn't set */
641 if (!filename) {
642 return create_default_mask(window, width, height);
643 }
644
645 if ((num = read_ini_array(filename, section, "NumPoints")) == NULL) {
646 g_free(filename);
647 return NULL;
648 }
649
650 if ((point = read_ini_array(filename, section, "PointList")) == NULL) {
651 g_array_free(num, TRUE);
652 g_free(filename);
653 return NULL;
654 }
655
656 mask = gdk_pixmap_new(window, width, height, 1);
657 gc = gdk_gc_new(mask);
658
659 pattern.pixel = 0;
660 gdk_gc_set_foreground(gc, &pattern);
661 gdk_draw_rectangle(mask, gc, TRUE, 0, 0, width, height);
662 pattern.pixel = 1;
663 gdk_gc_set_foreground(gc, &pattern);
664
665 j = 0;
666 for (i = 0; i < num->len; i++) {
667 if ((int)(point->len - j) >= (g_array_index(num, gint, i) * 2)) {
668 created_mask = TRUE;
669 gpoints = g_new(GdkPoint, g_array_index(num, gint, i));
670 for (k = 0; k < g_array_index(num, gint, i); k++) {
671 gpoints[k].x = g_array_index(point, gint, j + k * 2);
672 gpoints[k].y = g_array_index(point, gint, j + k * 2 + 1);
673 }
674 j += k * 2;
675 gdk_draw_polygon(mask, gc, TRUE, gpoints,
676 g_array_index(num, gint, i));
677 g_free(gpoints);
678 }
679 }
680 g_array_free(num, TRUE);
681 g_array_free(point, TRUE);
682 g_free(filename);
683
684 if (!created_mask)
685 gdk_draw_rectangle(mask, gc, TRUE, 0, 0, width, height);
686
687 gdk_gc_destroy(gc);
688
689 return mask;
690 }
691
692 void
693 skin_load_viscolor(Skin * skin, const gchar * path, const gchar * basename)
694 {
695 FILE *file;
696 gint i, c;
697 gchar line[256], *filename;
698 GArray *a;
699
700 g_return_if_fail(skin != NULL);
701 g_return_if_fail(path != NULL);
702 g_return_if_fail(basename != NULL);
703
704 skin_set_default_vis_color(skin);
705
706 filename = find_file_recursively(path, basename);
707 if (!filename)
708 return;
709
710 if (!(file = fopen(filename, "r"))) {
711 g_free(filename);
712 return;
713 }
714
715 g_free(filename);
716
717 for (i = 0; i < 24; i++) {
718 if (fgets(line, 255, file)) {
719 a = string_to_garray(line);
720 if (a->len > 2) {
721 for (c = 0; c < 3; c++)
722 skin->vis_color[i][c] = g_array_index(a, gint, c);
723 }
724 g_array_free(a, TRUE);
725 }
726 else
727 break;
728 }
729
730 fclose(file);
731 }
732
733 #if 0
734 static void
735 skin_numbers_generate_dash(Skin * skin)
736 {
737 GdkGC *gc;
738 GdkPixmap *pixmap;
739 SkinPixmap *numbers;
740
741 g_return_if_fail(skin != NULL);
742
743 numbers = &skin->pixmaps[SKIN_NUMBERS];
744 if (!numbers->pixmap || numbers->current_width < 99)
745 return;
746
747 gc = gdk_gc_new(numbers->pixmap);
748 pixmap = gdk_pixmap_new(mainwin->window, 108,
749 numbers->current_height,
750 -1);
751
752 skin_draw_pixmap(skin, pixmap, gc, SKIN_NUMBERS, 0, 0, 0, 0, 99, 13);
753 skin_draw_pixmap(skin, pixmap, gc, SKIN_NUMBERS, 90, 0, 99, 0, 9, 13);
754 skin_draw_pixmap(skin, pixmap, gc, SKIN_NUMBERS, 20, 6, 101, 6, 5, 1);
755
756 g_object_unref(numbers->pixmap);
757 g_object_unref(gc);
758
759 numbers->pixmap = pixmap;
760 numbers->current_width = 108;
761 }
762 #endif
763
764 static void
765 skin_load_cursor(Skin * skin, const gchar * dirname)
766 {
767 const gchar * basename = "normal.cur";
768 gchar * filename = NULL;
769 GdkPixbuf * cursor_pixbuf = NULL;
770 GdkPixbufAnimation * cursor_animated = NULL;
771 GdkCursor * cursor_gdk = NULL;
772 GError * error = NULL;
773
774 filename = find_file_recursively(dirname, basename);
775
776 if (filename && cfg.custom_cursors) {
777 cursor_animated = gdk_pixbuf_animation_new_from_file(filename, &error);
778 cursor_pixbuf = gdk_pixbuf_animation_get_static_image(cursor_animated);
779 cursor_gdk = gdk_cursor_new_from_pixbuf(gdk_display_get_default(),
780 cursor_pixbuf, 0, 0);
781 } else {
782 cursor_gdk = gdk_cursor_new(GDK_LEFT_PTR);
783 }
784
785 gdk_window_set_cursor(mainwin->window, cursor_gdk);
786 gdk_window_set_cursor(playlistwin->window, cursor_gdk);
787 gdk_window_set_cursor(equalizerwin->window, cursor_gdk);
788 gdk_cursor_unref(cursor_gdk);
789 }
790
791 static void
792 skin_load_pixmaps(Skin * skin, const gchar * path)
793 {
794 GdkPixmap *text_pm;
795 guint i;
796
797 for (i = 0; i < SKIN_PIXMAP_COUNT; i++)
798 skin_load_pixmap_id(skin, i, path);
799
800 text_pm = skin->pixmaps[SKIN_TEXT].pixmap;
801
802 if (text_pm)
803 skin_get_textcolors(text_pm, skin->textbg, skin->textfg);
804
805 #if 0
806 if (skin->pixmaps[SKIN_NUMBERS].pixmap)
807 skin_numbers_generate_dash(skin);
808 #endif
809
810 skin->colors[SKIN_PLEDIT_NORMAL] =
811 skin_load_color(path, "pledit.txt", "text", "normal", "#2499ff");
812 skin->colors[SKIN_PLEDIT_CURRENT] =
813 skin_load_color(path, "pledit.txt", "text", "current", "#ffeeff");
814 skin->colors[SKIN_PLEDIT_NORMALBG] =
815 skin_load_color(path, "pledit.txt", "text", "normalbg", "#0a120a");
816 skin->colors[SKIN_PLEDIT_SELECTEDBG] =
817 skin_load_color(path, "pledit.txt", "text", "selectedbg", "#0a124a");
818
819 skin_mask_create(skin, path, SKIN_MASK_MAIN, mainwin->window);
820 skin_mask_create(skin, path, SKIN_MASK_MAIN_SHADE, mainwin->window);
821
822 skin_mask_create(skin, path, SKIN_MASK_EQ, equalizerwin->window);
823 skin_mask_create(skin, path, SKIN_MASK_EQ_SHADE, equalizerwin->window);
824
825 skin_load_viscolor(skin, path, "viscolor.txt");
826 }
827
828 static gboolean
829 skin_load_nolock(Skin * skin, const gchar * path, gboolean force)
830 {
831 gchar *cpath;
832
833 g_return_val_if_fail(skin != NULL, FALSE);
834 g_return_val_if_fail(path != NULL, FALSE);
835 REQUIRE_LOCK(skin->lock);
836
837 if (!g_file_test(path, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_DIR))
838 return FALSE;
839
840 if (!force) {
841 if (skin->path)
842 if (!strcmp(skin->path, path))
843 return FALSE;
844 }
845
846 skin_current_num++;
847
848 skin->path = g_strdup(path);
849
850 if (!file_is_archive(path)) {
851 skin_load_pixmaps(skin, path);
852 skin_load_cursor(skin, path);
853
854 /* Parse the hints for this skin. */
855 skin_parse_hints(skin, NULL);
856
857 return TRUE;
858 }
859
860 if (!(cpath = archive_decompress(path))) {
861 g_message("Unable to extract skin archive (%s)", path);
862 return FALSE;
863 }
864
865 skin_load_pixmaps(skin, cpath);
866 skin_load_cursor(skin, cpath);
867
868 /* Parse the hints for this skin. */
869 skin_parse_hints(skin, cpath);
870
871 del_directory(cpath);
872 g_free(cpath);
873
874 return TRUE;
875 }
876
877 void
878 skin_install_skin(const gchar * path)
879 {
880 gchar *command;
881
882 g_return_if_fail(path != NULL);
883
884 command = g_strdup_printf("cp %s %s", path, bmp_paths[BMP_PATH_USER_SKIN_DIR]);
885 if (system(command)) {
886 g_message("Unable to install skin (%s) into user directory (%s)",
887 path, bmp_paths[BMP_PATH_USER_SKIN_DIR]);
888 }
889 g_free(command);
890 }
891
892
893 gboolean
894 skin_load(Skin * skin, const gchar * path)
895 {
896 gboolean error;
897
898 g_return_val_if_fail(skin != NULL, FALSE);
899
900 if (!path)
901 return FALSE;
902
903 skin_lock(skin);
904 error = skin_load_nolock(skin, path, FALSE);
905 skin_unlock(skin);
906
907 return error;
908 }
909
910 gboolean
911 skin_reload_forced(void)
912 {
913 gboolean error;
914
915 skin_lock(bmp_active_skin);
916 error = skin_load_nolock(bmp_active_skin, bmp_active_skin->path, TRUE);
917 skin_unlock(bmp_active_skin);
918
919 return error;
920 }
921
922 void
923 skin_reload(Skin * skin)
924 {
925 g_return_if_fail(skin != NULL);
926 skin_load_nolock(skin, skin->path, TRUE);
927 }
928
929
930 static SkinPixmap *
931 skin_get_pixmap(Skin * skin, SkinPixmapId map_id)
932 {
933 g_return_val_if_fail(skin != NULL, NULL);
934 g_return_val_if_fail(map_id < SKIN_PIXMAP_COUNT, NULL);
935
936 return &skin->pixmaps[map_id];
937 }
938
939 GdkBitmap *
940 skin_get_mask(Skin * skin, SkinMaskId mi)
941 {
942 g_return_val_if_fail(skin != NULL, NULL);
943 g_return_val_if_fail(mi < SKIN_PIXMAP_COUNT, NULL);
944
945 return skin->masks[mi];
946 }
947
948 GdkColor *
949 skin_get_color(Skin * skin, SkinColorId color_id)
950 {
951 GdkColor *ret = NULL;
952
953 g_return_val_if_fail(skin != NULL, NULL);
954
955 switch (color_id) {
956 case SKIN_TEXTBG:
957 if (skin->pixmaps[SKIN_TEXT].pixmap)
958 ret = skin->textbg;
959 else
960 ret = skin->def_textbg;
961 break;
962 case SKIN_TEXTFG:
963 if (skin->pixmaps[SKIN_TEXT].pixmap)
964 ret = skin->textfg;
965 else
966 ret = skin->def_textfg;
967 break;
968 default:
969 if (color_id < SKIN_COLOR_COUNT)
970 ret = skin->colors[color_id];
971 break;
972 }
973 return ret;
974 }
975
976 void
977 skin_get_viscolor(Skin * skin, guchar vis_color[24][3])
978 {
979 gint i;
980
981 g_return_if_fail(skin != NULL);
982
983 for (i = 0; i < 24; i++) {
984 vis_color[i][0] = skin->vis_color[i][0];
985 vis_color[i][1] = skin->vis_color[i][1];
986 vis_color[i][2] = skin->vis_color[i][2];
987 }
988 }
989
990 gint
991 skin_get_id(void)
992 {
993 return skin_current_num;
994 }
995
996 void
997 skin_draw_pixmap(Skin * skin, GdkDrawable * drawable, GdkGC * gc,
998 SkinPixmapId pixmap_id,
999 gint xsrc, gint ysrc, gint xdest, gint ydest,
1000 gint width, gint height)
1001 {
1002 SkinPixmap *pixmap;
1003
1004 g_return_if_fail(skin != NULL);
1005
1006 pixmap = skin_get_pixmap(skin, pixmap_id);
1007 g_return_if_fail(pixmap != NULL);
1008 g_return_if_fail(pixmap->pixmap != NULL);
1009
1010 if (xsrc > pixmap->width || ysrc > pixmap->height)
1011 return;
1012
1013 width = MIN(width, pixmap->width - xsrc);
1014 height = MIN(height, pixmap->height - ysrc);
1015 gdk_draw_pixmap(drawable, gc, pixmap->pixmap, xsrc, ysrc,
1016 xdest, ydest, width, height);
1017 }
1018
1019 void
1020 skin_get_eq_spline_colors(Skin * skin, guint32 colors[19])
1021 {
1022 gint i;
1023 GdkPixmap *pixmap;
1024 GdkImage *img;
1025 SkinPixmap *eqmainpm;
1026
1027 g_return_if_fail(skin != NULL);
1028
1029 eqmainpm = &skin->pixmaps[SKIN_EQMAIN];
1030 if (eqmainpm->pixmap &&
1031 eqmainpm->current_width >= 116 && eqmainpm->current_height >= 313)
1032 pixmap = eqmainpm->pixmap;
1033 else
1034 return;
1035
1036 if (!GDK_IS_DRAWABLE(pixmap))
1037 return;
1038
1039 if (!(img = gdk_drawable_get_image(pixmap, 115, 294, 1, 19)))
1040 return;
1041
1042 for (i = 0; i < 19; i++)
1043 colors[i] = gdk_image_get_pixel(img, 0, i);
1044
1045 gdk_image_destroy(img);
1046 }
1047
1048
1049 static void
1050 skin_draw_playlistwin_frame_top(Skin * skin,
1051 GdkDrawable * drawable,
1052 GdkGC * gc,
1053 gint width, gint height, gboolean focus)
1054 {
1055 /* The title bar skin consists of 2 sets of 4 images, 1 set
1056 * for focused state and the other for unfocused. The 4 images
1057 * are:
1058 *
1059 * a. right corner (25,20)
1060 * b. left corner (25,20)
1061 * c. tiler (25,20)
1062 * d. title (100,20)
1063 *
1064 * min allowed width = 100+25+25 = 150
1065 */
1066
1067 gint i, y, c;
1068
1069 /* get y offset of the pixmap set to use */
1070 if (focus)
1071 y = 0;
1072 else
1073 y = 21;
1074
1075 /* left corner */
1076 skin_draw_pixmap(skin, drawable, gc, SKIN_PLEDIT, 0, y, 0, 0, 25, 20);
1077
1078 /* titlebar title */
1079 skin_draw_pixmap(skin, drawable, gc, SKIN_PLEDIT, 26, y,
1080 (width - 100) / 2, 0, 100, 20);
1081
1082 /* titlebar right corner */
1083 skin_draw_pixmap(skin, drawable, gc, SKIN_PLEDIT, 153, y,
1084 width - 25, 0, 25, 20);
1085
1086 /* tile draw the remaining frame */
1087
1088 /* compute tile count */
1089 c = (width - (100 + 25 + 25)) / 25;
1090
1091 for (i = 0; i < c / 2; i++) {
1092 /* left of title */
1093 skin_draw_pixmap(skin, drawable, gc, SKIN_PLEDIT, 127, y,
1094 25 + i * 25, 0, 25, 20);
1095
1096 /* right of title */
1097 skin_draw_pixmap(skin, drawable, gc, SKIN_PLEDIT, 127, y,
1098 (width + 100) / 2 + i * 25, 0, 25, 20);
1099 }
1100
1101 if (c & 1) {
1102 /* Odd tile count, so one remaining to draw. Here we split
1103 * it into two and draw half on either side of the title */
1104 skin_draw_pixmap(skin, drawable, gc, SKIN_PLEDIT, 127, y,
1105 ((c / 2) * 25) + 25, 0, 12, 20);
1106 skin_draw_pixmap(skin, drawable, gc, SKIN_PLEDIT, 127, y,
1107 (width / 2) + ((c / 2) * 25) + 50, 0, 13, 20);
1108 }
1109 }
1110
1111 static void
1112 skin_draw_playlistwin_frame_bottom(Skin * skin,
1113 GdkDrawable * drawable,
1114 GdkGC * gc,
1115 gint width, gint height, gboolean focus)
1116 {
1117 /* The bottom frame skin consists of 1 set of 4 images. The 4
1118 * images are:
1119 *
1120 * a. left corner with menu buttons (125,38)
1121 * b. visualization window (75,38)
1122 * c. right corner with play buttons (150,38)
1123 * d. frame tile (25,38)
1124 *
1125 * (min allowed width = 125+150+25=300
1126 */
1127
1128 gint i, c;
1129
1130 /* bottom left corner (menu buttons) */
1131 skin_draw_pixmap(skin, drawable, gc, SKIN_PLEDIT, 0, 72,
1132 0, height - 38, 125, 38);
1133
1134 c = (width - 275) / 25;
1135
1136 /* draw visualization window, if width allows */
1137 if (c >= 3) {
1138 c -= 3;
1139 skin_draw_pixmap(skin, drawable, gc, SKIN_PLEDIT, 205, 0,
1140 width - (150 + 75), height - 38, 75, 38);
1141 }
1142
1143 /* Bottom right corner (playbuttons etc) */
1144 skin_draw_pixmap(skin, drawable, gc, SKIN_PLEDIT,
1145 126, 72, width - 150, height - 38, 150, 38);
1146
1147 /* Tile draw the remaining undrawn portions */
1148 for (i = 0; i < c; i++)
1149 skin_draw_pixmap(skin, drawable, gc, SKIN_PLEDIT, 179, 0,
1150 125 + i * 25, height - 38, 25, 38);
1151 }
1152
1153 static void
1154 skin_draw_playlistwin_frame_sides(Skin * skin,
1155 GdkDrawable * drawable,
1156 GdkGC * gc,
1157 gint width, gint height, gboolean focus)
1158 {
1159 /* The side frames consist of 2 tile images. 1 for the left, 1 for
1160 * the right.
1161 * a. left (12,29)
1162 * b. right (19,29)
1163 */
1164
1165 gint i;
1166
1167 /* frame sides */
1168 for (i = 0; i < (height - (20 + 38)) / 29; i++) {
1169 /* left */
1170 skin_draw_pixmap(skin, drawable, gc, SKIN_PLEDIT, 0, 42,
1171 0, 20 + i * 29, 12, 29);
1172
1173 /* right */
1174 skin_draw_pixmap(skin, drawable, gc, SKIN_PLEDIT, 32, 42,
1175 width - 19, 20 + i * 29, 19, 29);
1176 }
1177 }
1178
1179
1180 void
1181 skin_draw_playlistwin_frame(Skin * skin,
1182 GdkDrawable * drawable, GdkGC * gc,
1183 gint width, gint height, gboolean focus)
1184 {
1185 skin_draw_playlistwin_frame_top(skin, drawable, gc, width, height, focus);
1186 skin_draw_playlistwin_frame_bottom(skin, drawable, gc, width, height,
1187 focus);
1188 skin_draw_playlistwin_frame_sides(skin, drawable, gc, width, height,
1189 focus);
1190 }
1191
1192
1193 void
1194 skin_draw_playlistwin_shaded(Skin * skin,
1195 GdkDrawable * drawable, GdkGC * gc,
1196 gint width, gboolean focus)
1197 {
1198 /* The shade mode titlebar skin consists of 4 images:
1199 * a) left corner offset (72,42) size (25,14)
1200 * b) right corner, focused offset (99,57) size (50,14)
1201 * c) right corner, unfocused offset (99,42) size (50,14)
1202 * d) bar tile offset (72,57) size (25,14)
1203 */
1204
1205 gint i;
1206
1207 /* left corner */
1208 skin_draw_pixmap(skin, drawable, gc, SKIN_PLEDIT, 72, 42, 0, 0, 25, 14);
1209
1210 /* bar tile */
1211 for (i = 0; i < (width - 75) / 25; i++)
1212 skin_draw_pixmap(skin, drawable, gc, SKIN_PLEDIT, 72, 57,
1213 (i * 25) + 25, 0, 25, 14);
1214
1215 /* right corner */
1216 skin_draw_pixmap(skin, drawable, gc, SKIN_PLEDIT, 99, focus ? 57 : 42,
1217 width - 50, 0, 50, 14);
1218 }
1219
1220
1221 void
1222 skin_draw_mainwin_titlebar(Skin * skin,
1223 GdkDrawable * drawable, GdkGC * gc,
1224 gboolean shaded, gboolean focus)
1225 {
1226 /* The titlebar skin consists of 2 sets of 2 images, one for for
1227 * shaded and the other for unshaded mode, giving a total of 4.
1228 * The images are exactly 275x14 pixels, aligned and arranged
1229 * vertically on each other in the pixmap in the following order:
1230 *
1231 * a) unshaded, focused offset (27, 0)
1232 * b) unshaded, unfocused offset (27, 15)
1233 * c) shaded, focused offset (27, 29)
1234 * d) shaded, unfocused offset (27, 42)
1235 */
1236
1237 gint y_offset;
1238
1239 if (shaded) {
1240 if (focus)
1241 y_offset = 29;
1242 else
1243 y_offset = 42;
1244 }
1245 else {
1246 if (focus)
1247 y_offset = 0;
1248 else
1249 y_offset = 15;
1250 }
1251
1252 skin_draw_pixmap(skin, drawable, gc, SKIN_TITLEBAR, 27, y_offset,
1253 0, 0, MAINWIN_WIDTH, MAINWIN_TITLEBAR_HEIGHT);
1254 }