Debian Codegraph @

libgnome-desktop/gnome-bg/gnome-bg.c

raw
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-
2
3 gnomebg.c: Object for the desktop background.
4
5 Copyright (C) 2000 Eazel, Inc.
6 Copyright (C) 2007-2008 Red Hat, Inc.
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
17
18 You should have received a copy of the GNU Library General Public
19 License along with this program; if not, write to the
20 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA.
22
23 Derived from eel-background.c and eel-gdk-pixbuf-extensions.c by
24 Darin Adler <[email protected]> and Ramiro Estrugo <[email protected]>
25
26 Author: Soren Sandmann <[email protected]>
27
28 */
29
30 #include <string.h>
31 #include <math.h>
32 #include <stdarg.h>
33 #include <stdlib.h>
34
35 #include <glib/gstdio.h>
36 #include <gio/gio.h>
37
38 #include <cairo.h>
39
40 #define GNOME_DESKTOP_USE_UNSTABLE_API
41 #include "gnome-bg.h"
42 #include "gnome-bg-slide-show.h"
43
44 #define BG_KEY_PRIMARY_COLOR "primary-color"
45 #define BG_KEY_SECONDARY_COLOR "secondary-color"
46 #define BG_KEY_COLOR_TYPE "color-shading-type"
47 #define BG_KEY_PICTURE_PLACEMENT "picture-options"
48 #define BG_KEY_PICTURE_OPACITY "picture-opacity"
49 #define BG_KEY_PICTURE_URI "picture-uri"
50
51 /* We keep the large pixbufs around if the next update
52 in the slideshow is less than 60 seconds away */
53 #define KEEP_EXPENSIVE_CACHE_SECS 60
54
55 /* This is the size of the GdkRGB dither matrix, in order to avoid
56 * bad dithering when tiling the gradient
57 */
58 #define GRADIENT_PIXMAP_TILE_SIZE 128
59 #define THUMBNAIL_SIZE 256
60
61 typedef struct FileCacheEntry FileCacheEntry;
62 #define CACHE_SIZE 4
63
64 /*
65 * Implementation of the GnomeBG class
66 */
67 struct _GnomeBG
68 {
69 GObject parent_instance;
70 char * filename;
71 GDesktopBackgroundStyle placement;
72 GDesktopBackgroundShading color_type;
73 GdkRGBA primary;
74 GdkRGBA secondary;
75
76 GFileMonitor * file_monitor;
77
78 guint changed_id;
79 guint transitioned_id;
80 guint blow_caches_id;
81
82 /* Cached information, only access through cache accessor functions */
83 GnomeBGSlideShow * slideshow;
84 time_t file_mtime;
85 GdkPixbuf * pixbuf_cache;
86 int timeout_id;
87
88 GList * file_cache;
89 };
90
91 struct _GnomeBGClass
92 {
93 GObjectClass parent_class;
94 };
95
96 enum {
97 CHANGED,
98 TRANSITIONED,
99 N_SIGNALS
100 };
101
102 static guint signals[N_SIGNALS] = { 0 };
103
104 G_DEFINE_TYPE (GnomeBG, gnome_bg, G_TYPE_OBJECT)
105
106 /* Pixbuf utils */
107 static void pixbuf_average_value (GdkPixbuf *pixbuf,
108 GdkRGBA *result);
109 static GdkPixbuf *pixbuf_scale_to_fit (GdkPixbuf *src,
110 int max_width,
111 int max_height);
112 static GdkPixbuf *pixbuf_scale_to_min (GdkPixbuf *src,
113 int min_width,
114 int min_height);
115 static void pixbuf_draw_gradient (GdkPixbuf *pixbuf,
116 gboolean horizontal,
117 GdkRGBA *c1,
118 GdkRGBA *c2,
119 GdkRectangle *rect);
120 static void pixbuf_tile (GdkPixbuf *src,
121 GdkPixbuf *dest);
122 static void pixbuf_blend (GdkPixbuf *src,
123 GdkPixbuf *dest,
124 int src_x,
125 int src_y,
126 int width,
127 int height,
128 int dest_x,
129 int dest_y,
130 double alpha);
131
132 /* Thumbnail utilities */
133 static GdkPixbuf *create_thumbnail_for_filename (GnomeDesktopThumbnailFactory *factory,
134 const char *filename);
135 static gboolean get_thumb_annotations (GdkPixbuf *thumb,
136 int *orig_width,
137 int *orig_height);
138
139 /* Cache */
140 static GdkPixbuf *get_pixbuf_for_size (GnomeBG *bg,
141 gint num_monitor,
142 int width,
143 int height);
144 static void clear_cache (GnomeBG *bg);
145 static gboolean is_different (GnomeBG *bg,
146 const char *filename);
147 static time_t get_mtime (const char *filename);
148 static GdkPixbuf *create_img_thumbnail (GnomeBG *bg,
149 GnomeDesktopThumbnailFactory *factory,
150 const cairo_rectangle_int_t *screen_area,
151 int dest_width,
152 int dest_height,
153 int frame_num);
154 static GnomeBGSlideShow * get_as_slideshow (GnomeBG *bg,
155 const char *filename);
156 static GnomeBGSlideShow *read_slideshow_file (const char *filename,
157 GError **err);
158
159 static void
160 color_from_string (const char *string,
161 GdkRGBA *colorp)
162 {
163 /* If all else fails use black */
164 gdk_rgba_parse (colorp, "black");
165
166 if (!string)
167 return;
168
169 gdk_rgba_parse (colorp, string);
170 }
171
172 static char *
173 color_to_string (const GdkRGBA *color)
174 {
175 return g_strdup_printf ("#%02x%02x%02x",
176 (int) (0.5 + color->red * 255),
177 (int) (0.5 + color->green * 255),
178 (int) (0.5 + color->blue * 255));
179 }
180
181 static gboolean
182 do_changed (GnomeBG *bg)
183 {
184 gboolean ignore_pending_change;
185 bg->changed_id = 0;
186
187 ignore_pending_change =
188 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (bg),
189 "ignore-pending-change"));
190
191 if (!ignore_pending_change) {
192 g_signal_emit (G_OBJECT (bg), signals[CHANGED], 0);
193 }
194
195 return FALSE;
196 }
197
198 static void
199 queue_changed (GnomeBG *bg)
200 {
201 if (bg->changed_id > 0) {
202 g_source_remove (bg->changed_id);
203 }
204
205 /* We unset this here to allow apps to set it if they don't want
206 to get the change event. This is used by nautilus when it
207 gets the pixmap from the bg (due to a reason other than the changed
208 event). Because if there is no other change after this time the
209 pending changed event will just uselessly cause us to recreate
210 the pixmap. */
211 g_object_set_data (G_OBJECT (bg), "ignore-pending-change",
212 GINT_TO_POINTER (FALSE));
213 bg->changed_id = g_timeout_add_full (G_PRIORITY_LOW,
214 100,
215 (GSourceFunc)do_changed,
216 bg,
217 NULL);
218 }
219
220 static gboolean
221 do_transitioned (GnomeBG *bg)
222 {
223 bg->transitioned_id = 0;
224
225 if (bg->pixbuf_cache) {
226 g_object_unref (bg->pixbuf_cache);
227 bg->pixbuf_cache = NULL;
228 }
229
230 g_signal_emit (G_OBJECT (bg), signals[TRANSITIONED], 0);
231
232 return FALSE;
233 }
234
235 static void
236 queue_transitioned (GnomeBG *bg)
237 {
238 if (bg->transitioned_id > 0) {
239 g_source_remove (bg->transitioned_id);
240 }
241
242 bg->transitioned_id = g_timeout_add_full (G_PRIORITY_LOW,
243 100,
244 (GSourceFunc)do_transitioned,
245 bg,
246 NULL);
247 }
248
249 static gboolean
250 bg_gsettings_mapping (GVariant *value,
251 gpointer *result,
252 gpointer user_data)
253 {
254 const gchar *bg_key_value;
255 char *filename = NULL;
256
257 /* The final fallback if nothing matches is with a NULL value. */
258 if (value == NULL) {
259 *result = NULL;
260 return TRUE;
261 }
262
263 bg_key_value = g_variant_get_string (value, NULL);
264
265 if (bg_key_value && *bg_key_value != '\0') {
266 filename = g_filename_from_uri (bg_key_value, NULL, NULL);
267
268 if (filename != NULL && g_file_test (filename, G_FILE_TEST_EXISTS) == FALSE) {
269 g_free (filename);
270 return FALSE;
271 }
272
273 if (filename != NULL) {
274 *result = filename;
275 return TRUE;
276 }
277 }
278
279 return FALSE;
280 }
281
282 void
283 gnome_bg_load_from_preferences (GnomeBG *bg,
284 GSettings *settings)
285 {
286 char *tmp;
287 char *filename;
288 GDesktopBackgroundShading ctype;
289 GdkRGBA c1, c2;
290 GDesktopBackgroundStyle placement;
291
292 g_return_if_fail (GNOME_IS_BG (bg));
293 g_return_if_fail (G_IS_SETTINGS (settings));
294
295 /* Filename */
296 filename = g_settings_get_mapped (settings, BG_KEY_PICTURE_URI, bg_gsettings_mapping, NULL);
297
298 /* Colors */
299 tmp = g_settings_get_string (settings, BG_KEY_PRIMARY_COLOR);
300 color_from_string (tmp, &c1);
301 g_free (tmp);
302
303 tmp = g_settings_get_string (settings, BG_KEY_SECONDARY_COLOR);
304 color_from_string (tmp, &c2);
305 g_free (tmp);
306
307 /* Color type */
308 ctype = g_settings_get_enum (settings, BG_KEY_COLOR_TYPE);
309
310 /* Placement */
311 placement = g_settings_get_enum (settings, BG_KEY_PICTURE_PLACEMENT);
312
313 gnome_bg_set_rgba (bg, ctype, &c1, &c2);
314 gnome_bg_set_placement (bg, placement);
315 gnome_bg_set_filename (bg, filename);
316
317 g_free (filename);
318 }
319
320 void
321 gnome_bg_save_to_preferences (GnomeBG *bg,
322 GSettings *settings)
323 {
324 gchar *primary;
325 gchar *secondary;
326 gchar *uri;
327
328 g_return_if_fail (GNOME_IS_BG (bg));
329 g_return_if_fail (G_IS_SETTINGS (settings));
330
331 primary = color_to_string (&bg->primary);
332 secondary = color_to_string (&bg->secondary);
333
334 g_settings_delay (settings);
335
336 uri = NULL;
337 if (bg->filename != NULL)
338 uri = g_filename_to_uri (bg->filename, NULL, NULL);
339 if (uri == NULL)
340 uri = g_strdup ("");
341 g_settings_set_string (settings, BG_KEY_PICTURE_URI, uri);
342 g_settings_set_string (settings, BG_KEY_PRIMARY_COLOR, primary);
343 g_settings_set_string (settings, BG_KEY_SECONDARY_COLOR, secondary);
344 g_settings_set_enum (settings, BG_KEY_COLOR_TYPE, bg->color_type);
345 g_settings_set_enum (settings, BG_KEY_PICTURE_PLACEMENT, bg->placement);
346
347 /* Apply changes atomically. */
348 g_settings_apply (settings);
349
350 g_free (primary);
351 g_free (secondary);
352 g_free (uri);
353 }
354
355
356 static void
357 gnome_bg_init (GnomeBG *bg)
358 {
359 }
360
361 static void
362 gnome_bg_dispose (GObject *object)
363 {
364 GnomeBG *bg = GNOME_BG (object);
365
366 if (bg->file_monitor) {
367 g_object_unref (bg->file_monitor);
368 bg->file_monitor = NULL;
369 }
370
371 clear_cache (bg);
372
373 G_OBJECT_CLASS (gnome_bg_parent_class)->dispose (object);
374 }
375
376 static void
377 gnome_bg_finalize (GObject *object)
378 {
379 GnomeBG *bg = GNOME_BG (object);
380
381 if (bg->changed_id != 0) {
382 g_source_remove (bg->changed_id);
383 bg->changed_id = 0;
384 }
385
386 if (bg->transitioned_id != 0) {
387 g_source_remove (bg->transitioned_id);
388 bg->transitioned_id = 0;
389 }
390
391 if (bg->blow_caches_id != 0) {
392 g_source_remove (bg->blow_caches_id);
393 bg->blow_caches_id = 0;
394 }
395
396 g_free (bg->filename);
397 bg->filename = NULL;
398
399 G_OBJECT_CLASS (gnome_bg_parent_class)->finalize (object);
400 }
401
402 static void
403 gnome_bg_class_init (GnomeBGClass *klass)
404 {
405 GObjectClass *object_class = G_OBJECT_CLASS (klass);
406
407 object_class->dispose = gnome_bg_dispose;
408 object_class->finalize = gnome_bg_finalize;
409
410 signals[CHANGED] = g_signal_new ("changed",
411 G_OBJECT_CLASS_TYPE (object_class),
412 G_SIGNAL_RUN_LAST,
413 0,
414 NULL, NULL,
415 g_cclosure_marshal_VOID__VOID,
416 G_TYPE_NONE, 0);
417
418 signals[TRANSITIONED] = g_signal_new ("transitioned",
419 G_OBJECT_CLASS_TYPE (object_class),
420 G_SIGNAL_RUN_LAST,
421 0,
422 NULL, NULL,
423 g_cclosure_marshal_VOID__VOID,
424 G_TYPE_NONE, 0);
425 }
426
427 GnomeBG *
428 gnome_bg_new (void)
429 {
430 return g_object_new (GNOME_TYPE_BG, NULL);
431 }
432
433 void
434 gnome_bg_set_rgba (GnomeBG *bg,
435 GDesktopBackgroundShading type,
436 GdkRGBA *primary,
437 GdkRGBA *secondary)
438 {
439 g_return_if_fail (bg != NULL);
440 g_return_if_fail (primary != NULL);
441
442 if (bg->color_type != type ||
443 !gdk_rgba_equal (&bg->primary, primary) ||
444 (secondary && !gdk_rgba_equal (&bg->secondary, secondary))) {
445
446 bg->color_type = type;
447 bg->primary = *primary;
448 if (secondary) {
449 bg->secondary = *secondary;
450 }
451
452 queue_changed (bg);
453 }
454 }
455
456 void
457 gnome_bg_set_placement (GnomeBG *bg,
458 GDesktopBackgroundStyle placement)
459 {
460 g_return_if_fail (bg != NULL);
461
462 if (bg->placement != placement) {
463 bg->placement = placement;
464
465 queue_changed (bg);
466 }
467 }
468
469 GDesktopBackgroundStyle
470 gnome_bg_get_placement (GnomeBG *bg)
471 {
472 g_return_val_if_fail (bg != NULL, -1);
473
474 return bg->placement;
475 }
476
477 void
478 gnome_bg_get_rgba (GnomeBG *bg,
479 GDesktopBackgroundShading *type,
480 GdkRGBA *primary,
481 GdkRGBA *secondary)
482 {
483 g_return_if_fail (bg != NULL);
484
485 if (type)
486 *type = bg->color_type;
487
488 if (primary)
489 *primary = bg->primary;
490
491 if (secondary)
492 *secondary = bg->secondary;
493 }
494
495 const gchar *
496 gnome_bg_get_filename (GnomeBG *bg)
497 {
498 g_return_val_if_fail (bg != NULL, NULL);
499
500 return bg->filename;
501 }
502
503 static inline gchar *
504 get_wallpaper_cache_dir (void)
505 {
506 return g_build_filename (g_get_user_cache_dir(), "wallpaper", NULL);
507 }
508
509 static inline gchar *
510 get_wallpaper_cache_prefix_name (gint num_monitor,
511 GDesktopBackgroundStyle placement,
512 gint width,
513 gint height)
514 {
515 return g_strdup_printf ("%i_%i_%i_%i", num_monitor, (gint) placement, width, height);
516 }
517
518 static char *
519 get_wallpaper_cache_filename (const char *filename,
520 gint num_monitor,
521 GDesktopBackgroundStyle placement,
522 gint width,
523 gint height)
524 {
525 gchar *cache_filename;
526 gchar *cache_prefix_name;
527 gchar *md5_filename;
528 gchar *cache_basename;
529 gchar *cache_dir;
530
531 md5_filename = g_compute_checksum_for_data (G_CHECKSUM_MD5, (const guchar *) filename, strlen (filename));
532 cache_prefix_name = get_wallpaper_cache_prefix_name (num_monitor, placement, width, height);
533 cache_basename = g_strdup_printf ("%s_%s", cache_prefix_name, md5_filename);
534 cache_dir = get_wallpaper_cache_dir ();
535 cache_filename = g_build_filename (cache_dir, cache_basename, NULL);
536
537 g_free (cache_prefix_name);
538 g_free (md5_filename);
539 g_free (cache_basename);
540 g_free (cache_dir);
541
542 return cache_filename;
543 }
544
545 static void
546 cleanup_cache_for_monitor (gchar *cache_dir,
547 gint num_monitor)
548 {
549 GDir *g_cache_dir;
550 gchar *monitor_prefix;
551 const gchar *file;
552
553 g_cache_dir = g_dir_open (cache_dir, 0, NULL);
554 monitor_prefix = g_strdup_printf ("%i_", num_monitor);
555
556 file = g_dir_read_name (g_cache_dir);
557 while (file != NULL) {
558 gchar *path;
559
560 path = g_build_filename (cache_dir, file, NULL);
561 /* purge files with same monitor id */
562 if (g_str_has_prefix (file, monitor_prefix) &&
563 g_file_test (path, G_FILE_TEST_IS_REGULAR))
564 g_unlink (path);
565
566 g_free (path);
567
568 file = g_dir_read_name (g_cache_dir);
569 }
570
571 g_free (monitor_prefix);
572 g_dir_close (g_cache_dir);
573 }
574
575 static gboolean
576 cache_file_is_valid (const char *filename,
577 const char *cache_filename)
578 {
579 time_t mtime;
580 time_t cache_mtime;
581
582 if (!g_file_test (cache_filename, G_FILE_TEST_IS_REGULAR))
583 return FALSE;
584
585 mtime = get_mtime (filename);
586 cache_mtime = get_mtime (cache_filename);
587
588 return (mtime < cache_mtime);
589 }
590
591 static void
592 refresh_cache_file (GnomeBG *bg,
593 GdkPixbuf *new_pixbuf,
594 gint num_monitor,
595 gint width,
596 gint height)
597 {
598 gchar *cache_filename;
599 gchar *cache_dir;
600 GdkPixbufFormat *format;
601 gchar *format_name;
602
603 if ((num_monitor == -1) || (width <= 300) || (height <= 300))
604 return;
605
606 cache_filename = get_wallpaper_cache_filename (bg->filename, num_monitor, bg->placement, width, height);
607 cache_dir = get_wallpaper_cache_dir ();
608
609 /* Only refresh scaled file on disk if useful (and don't cache slideshow) */
610 if (!cache_file_is_valid (bg->filename, cache_filename)) {
611 format = gdk_pixbuf_get_file_info (bg->filename, NULL, NULL);
612
613 if (format != NULL) {
614 if (!g_file_test (cache_dir, G_FILE_TEST_IS_DIR)) {
615 g_mkdir_with_parents (cache_dir, 0700);
616 } else {
617 cleanup_cache_for_monitor (cache_dir, num_monitor);
618 }
619
620 format_name = gdk_pixbuf_format_get_name (format);
621
622 if (strcmp (format_name, "jpeg") == 0)
623 gdk_pixbuf_save (new_pixbuf, cache_filename, format_name, NULL, "quality", "100", NULL);
624 else
625 gdk_pixbuf_save (new_pixbuf, cache_filename, format_name, NULL, NULL);
626
627 g_free (format_name);
628 }
629 }
630
631 g_free (cache_filename);
632 g_free (cache_dir);
633 }
634
635 static void
636 file_changed (GFileMonitor *file_monitor,
637 GFile *child,
638 GFile *other_file,
639 GFileMonitorEvent event_type,
640 gpointer user_data)
641 {
642 GnomeBG *bg = GNOME_BG (user_data);
643
644 clear_cache (bg);
645 queue_changed (bg);
646 }
647
648 void
649 gnome_bg_set_filename (GnomeBG *bg,
650 const char *filename)
651 {
652 g_return_if_fail (bg != NULL);
653
654 if (is_different (bg, filename)) {
655 g_free (bg->filename);
656
657 bg->filename = g_strdup (filename);
658 bg->file_mtime = get_mtime (bg->filename);
659
660 if (bg->file_monitor) {
661 g_object_unref (bg->file_monitor);
662 bg->file_monitor = NULL;
663 }
664
665 if (bg->filename) {
666 GFile *f = g_file_new_for_path (bg->filename);
667
668 bg->file_monitor = g_file_monitor_file (f, 0, NULL, NULL);
669 g_signal_connect (bg->file_monitor, "changed",
670 G_CALLBACK (file_changed), bg);
671
672 g_object_unref (f);
673 }
674
675 clear_cache (bg);
676
677 queue_changed (bg);
678 }
679 }
680
681 static void
682 draw_color_area (GnomeBG *bg,
683 GdkPixbuf *dest,
684 GdkRectangle *rect)
685 {
686 guint32 pixel;
687 GdkRectangle extent;
688
689 extent.x = 0;
690 extent.y = 0;
691 extent.width = gdk_pixbuf_get_width (dest);
692 extent.height = gdk_pixbuf_get_height (dest);
693
694 gdk_rectangle_intersect (rect, &extent, rect);
695
696 switch (bg->color_type) {
697 case G_DESKTOP_BACKGROUND_SHADING_SOLID:
698 /* not really a big deal to ignore the area of interest */
699 pixel = ((int) (0.5 + bg->primary.red * 255) << 24) |
700 ((int) (0.5 + bg->primary.green * 255) << 16) |
701 ((int) (0.5 + bg->primary.blue * 255) << 8) |
702 (0xff);
703
704 gdk_pixbuf_fill (dest, pixel);
705 break;
706
707 case G_DESKTOP_BACKGROUND_SHADING_HORIZONTAL:
708 pixbuf_draw_gradient (dest, TRUE, &(bg->primary), &(bg->secondary), rect);
709 break;
710
711 case G_DESKTOP_BACKGROUND_SHADING_VERTICAL:
712 pixbuf_draw_gradient (dest, FALSE, &(bg->primary), &(bg->secondary), rect);
713 break;
714
715 default:
716 break;
717 }
718 }
719
720 static void
721 draw_color (GnomeBG *bg,
722 GdkPixbuf *dest)
723 {
724 GdkRectangle rect;
725 rect.x = 0;
726 rect.y = 0;
727 rect.width = gdk_pixbuf_get_width (dest);
728 rect.height = gdk_pixbuf_get_height (dest);
729 draw_color_area (bg, dest, &rect);
730 }
731
732 static GdkPixbuf *
733 pixbuf_clip_to_fit (GdkPixbuf *src,
734 int max_width,
735 int max_height)
736 {
737 int src_width, src_height;
738 int w, h;
739 int src_x, src_y;
740 GdkPixbuf *pixbuf;
741
742 src_width = gdk_pixbuf_get_width (src);
743 src_height = gdk_pixbuf_get_height (src);
744
745 if (src_width < max_width && src_height < max_height)
746 return g_object_ref (src);
747
748 w = MIN(src_width, max_width);
749 h = MIN(src_height, max_height);
750
751 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
752 gdk_pixbuf_get_has_alpha (src),
753 8, w, h);
754
755 src_x = (src_width - w) / 2;
756 src_y = (src_height - h) / 2;
757 gdk_pixbuf_copy_area (src,
758 src_x, src_y,
759 w, h,
760 pixbuf,
761 0, 0);
762 return pixbuf;
763 }
764
765 static GdkPixbuf *
766 get_scaled_pixbuf (GDesktopBackgroundStyle placement,
767 GdkPixbuf *pixbuf,
768 int width, int height,
769 int *x, int *y,
770 int *w, int *h)
771 {
772 GdkPixbuf *new;
773
774 #if 0
775 g_print ("original_width: %d %d\n",
776 gdk_pixbuf_get_width (pixbuf),
777 gdk_pixbuf_get_height (pixbuf));
778 #endif
779
780 switch (placement) {
781 case G_DESKTOP_BACKGROUND_STYLE_SPANNED:
782 new = pixbuf_scale_to_fit (pixbuf, width, height);
783 break;
784 case G_DESKTOP_BACKGROUND_STYLE_ZOOM:
785 new = pixbuf_scale_to_min (pixbuf, width, height);
786 break;
787
788 case G_DESKTOP_BACKGROUND_STYLE_STRETCHED:
789 new = gdk_pixbuf_scale_simple (pixbuf, width, height,
790 GDK_INTERP_BILINEAR);
791 break;
792
793 case G_DESKTOP_BACKGROUND_STYLE_SCALED:
794 new = pixbuf_scale_to_fit (pixbuf, width, height);
795 break;
796
797 case G_DESKTOP_BACKGROUND_STYLE_NONE:
798 /* This shouldn’t be true, but if it is, assert and
799 * fall through, in case assertions are disabled.
800 */
801 g_assert_not_reached ();
802 case G_DESKTOP_BACKGROUND_STYLE_CENTERED:
803 case G_DESKTOP_BACKGROUND_STYLE_WALLPAPER:
804 default:
805 new = pixbuf_clip_to_fit (pixbuf, width, height);
806 break;
807 }
808
809 *w = gdk_pixbuf_get_width (new);
810 *h = gdk_pixbuf_get_height (new);
811 *x = (width - *w) / 2;
812 *y = (height - *h) / 2;
813
814 return new;
815 }
816
817 static void
818 draw_image_area (GnomeBG *bg,
819 gint num_monitor,
820 GdkPixbuf *pixbuf,
821 GdkPixbuf *dest,
822 GdkRectangle *area)
823 {
824 int dest_width = area->width;
825 int dest_height = area->height;
826 int x, y, w, h;
827 GdkPixbuf *scaled;
828
829 if (!pixbuf)
830 return;
831
832 scaled = get_scaled_pixbuf (bg->placement, pixbuf, dest_width, dest_height, &x, &y, &w, &h);
833
834 switch (bg->placement) {
835 case G_DESKTOP_BACKGROUND_STYLE_WALLPAPER:
836 pixbuf_tile (scaled, dest);
837 break;
838 case G_DESKTOP_BACKGROUND_STYLE_ZOOM:
839 case G_DESKTOP_BACKGROUND_STYLE_CENTERED:
840 case G_DESKTOP_BACKGROUND_STYLE_STRETCHED:
841 case G_DESKTOP_BACKGROUND_STYLE_SCALED:
842 pixbuf_blend (scaled, dest, 0, 0, w, h, x + area->x, y + area->y, 1.0);
843 break;
844 case G_DESKTOP_BACKGROUND_STYLE_SPANNED:
845 pixbuf_blend (scaled, dest, 0, 0, w, h, x, y, 1.0);
846 break;
847 case G_DESKTOP_BACKGROUND_STYLE_NONE:
848 default:
849 g_assert_not_reached ();
850 break;
851 }
852
853 refresh_cache_file (bg, scaled, num_monitor, dest_width, dest_height);
854
855 g_object_unref (scaled);
856 }
857
858 static void
859 draw_image_for_thumb (GnomeBG *bg,
860 GdkPixbuf *pixbuf,
861 GdkPixbuf *dest)
862 {
863 GdkRectangle rect;
864
865 rect.x = 0;
866 rect.y = 0;
867 rect.width = gdk_pixbuf_get_width (dest);
868 rect.height = gdk_pixbuf_get_height (dest);
869
870 draw_image_area (bg, -1, pixbuf, dest, &rect);
871 }
872
873 static void
874 draw_once (GnomeBG *bg,
875 GdkPixbuf *dest)
876 {
877 GdkRectangle rect;
878 GdkPixbuf *pixbuf;
879 gint num_monitor;
880
881 /* we just draw on the whole screen */
882 num_monitor = 0;
883
884 rect.x = 0;
885 rect.y = 0;
886 rect.width = gdk_pixbuf_get_width (dest);
887 rect.height = gdk_pixbuf_get_height (dest);
888
889 pixbuf = get_pixbuf_for_size (bg, num_monitor, rect.width, rect.height);
890 if (pixbuf) {
891 GdkPixbuf *rotated;
892
893 rotated = gdk_pixbuf_apply_embedded_orientation (pixbuf);
894 if (rotated != NULL) {
895 g_object_unref (pixbuf);
896 pixbuf = rotated;
897 }
898
899 draw_image_area (bg,
900 num_monitor,
901 pixbuf,
902 dest,
903 &rect);
904 g_object_unref (pixbuf);
905 }
906 }
907
908 void
909 gnome_bg_draw (GnomeBG *bg,
910 GdkPixbuf *dest)
911 {
912 draw_color (bg, dest);
913 if (bg->placement != G_DESKTOP_BACKGROUND_STYLE_NONE) {
914 draw_once (bg, dest);
915 }
916 }
917
918 gboolean
919 gnome_bg_has_multiple_sizes (GnomeBG *bg)
920 {
921 GnomeBGSlideShow *show;
922 gboolean ret;
923
924 g_return_val_if_fail (bg != NULL, FALSE);
925
926 ret = FALSE;
927
928 show = get_as_slideshow (bg, bg->filename);
929 if (show) {
930 ret = gnome_bg_slide_show_get_has_multiple_sizes (show);
931 g_object_unref (show);
932 }
933
934 return ret;
935 }
936
937 static void
938 gnome_bg_get_pixmap_size (GnomeBG *bg,
939 int width,
940 int height,
941 int *pixmap_width,
942 int *pixmap_height)
943 {
944 int dummy;
945
946 if (!pixmap_width)
947 pixmap_width = &dummy;
948 if (!pixmap_height)
949 pixmap_height = &dummy;
950
951 *pixmap_width = width;
952 *pixmap_height = height;
953
954 if (!bg->filename) {
955 switch (bg->color_type) {
956 case G_DESKTOP_BACKGROUND_SHADING_SOLID:
957 *pixmap_width = 1;
958 *pixmap_height = 1;
959 break;
960
961 case G_DESKTOP_BACKGROUND_SHADING_HORIZONTAL:
962 case G_DESKTOP_BACKGROUND_SHADING_VERTICAL:
963 default:
964 break;
965 }
966
967 return;
968 }
969 }
970
971 static cairo_surface_t *
972 gnome_bg_cairo_surface_create_from_pixbuf (GdkPixbuf *pixbuf,
973 int scale,
974 cairo_surface_t *target)
975 {
976 cairo_format_t format;
977
978 if (gdk_pixbuf_get_n_channels (pixbuf) == 3)
979 format = CAIRO_FORMAT_RGB24;
980 else
981 format = CAIRO_FORMAT_ARGB32;
982
983 int pixbuf_width = gdk_pixbuf_get_width (pixbuf);
984 int pixbuf_height = gdk_pixbuf_get_height (pixbuf);
985 cairo_surface_t *pixbuf_surface =
986 cairo_surface_create_similar_image (target, format,
987 pixbuf_width,
988 pixbuf_height);
989
990 if (cairo_surface_status (pixbuf_surface) != CAIRO_STATUS_SUCCESS)
991 return pixbuf_surface;
992
993 cairo_surface_set_device_scale (pixbuf_surface, scale, scale);
994
995 cairo_surface_flush (pixbuf_surface);
996
997 guchar *gdk_pixels = gdk_pixbuf_get_pixels (pixbuf);
998 int gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
999 int n_channels = gdk_pixbuf_get_n_channels (pixbuf);
1000
1001 guchar *cairo_pixels = cairo_image_surface_get_data (pixbuf_surface);
1002 int cairo_stride = cairo_image_surface_get_stride (pixbuf_surface);
1003
1004 for (int j = pixbuf_height; j; j--) {
1005 guchar *p = gdk_pixels;
1006 guchar *q = cairo_pixels;
1007
1008 if (n_channels == 3) {
1009 guchar *end = p + 3 * pixbuf_width;
1010
1011 while (p < end) {
1012 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1013 q[0] = p[2];
1014 q[1] = p[1];
1015 q[2] = p[0];
1016 #else
1017 q[1] = p[0];
1018 q[2] = p[1];
1019 q[3] = p[2];
1020 #endif
1021 p += 3;
1022 q += 4;
1023 }
1024 } else {
1025 guchar *end = p + 4 * pixbuf_width;
1026
1027 #define MULT(d,c,a) G_STMT_START { guint __t = c * a + 0x80; d = ((__t >> 8) + __t) >> 8; } G_STMT_END
1028
1029 while (p < end) {
1030 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1031 MULT (q[0], p[2], p[3]);
1032 MULT (q[1], p[1], p[3]);
1033 MULT (q[2], p[0], p[3]);
1034 q[3] = p[3];
1035 #else
1036 q[0] = p[3];
1037 MULT(q[1], p[0], p[3]);
1038 MULT(q[2], p[1], p[3]);
1039 MULT(q[3], p[2], p[3]);
1040 #endif
1041
1042 p += 4;
1043 q += 4;
1044 }
1045 #undef MULT
1046 }
1047
1048 gdk_pixels += gdk_rowstride;
1049 cairo_pixels += cairo_stride;
1050 }
1051
1052 cairo_surface_mark_dirty (pixbuf_surface);
1053
1054 return pixbuf_surface;
1055 }
1056
1057 /**
1058 * gnome_bg_create_surface:
1059 * @bg: GnomeBG
1060 * @window:
1061 * @width:
1062 * @height:
1063 *
1064 * Create a surface that can be set as background for @window.
1065 *
1066 * Returns: %NULL on error (e.g. out of X connections)
1067 **/
1068 cairo_surface_t *
1069 gnome_bg_create_surface (GnomeBG *bg,
1070 GdkSurface *surface,
1071 int width,
1072 int height)
1073 {
1074 gint scale;
1075 int pm_width, pm_height;
1076 cairo_surface_t *target_surface;
1077 cairo_t *cr;
1078
1079 g_return_val_if_fail (GNOME_IS_BG (bg), NULL);
1080 g_return_val_if_fail (GDK_IS_SURFACE (surface), NULL);
1081
1082 scale = gdk_surface_get_scale_factor (surface);
1083
1084 if (bg->pixbuf_cache &&
1085 gdk_pixbuf_get_width (bg->pixbuf_cache) != width &&
1086 gdk_pixbuf_get_height (bg->pixbuf_cache) != height) {
1087 g_object_unref (bg->pixbuf_cache);
1088 bg->pixbuf_cache = NULL;
1089 }
1090
1091 /* has the side effect of loading and caching pixbuf only when in tile mode */
1092 gnome_bg_get_pixmap_size (bg, width, height, &pm_width, &pm_height);
1093 target_surface = gdk_surface_create_similar_surface (surface,
1094 CAIRO_CONTENT_COLOR,
1095 pm_width,
1096 pm_height);
1097
1098 if (target_surface == NULL)
1099 return NULL;
1100
1101 cr = cairo_create (target_surface);
1102 if (!bg->filename && bg->color_type == G_DESKTOP_BACKGROUND_SHADING_SOLID) {
1103 gdk_cairo_set_source_rgba (cr, &(bg->primary));
1104 }
1105 else {
1106 GdkPixbuf *pixbuf;
1107 cairo_surface_t *pixbuf_surface;
1108
1109 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
1110 scale * width,
1111 scale * height);
1112
1113 gnome_bg_draw (bg, pixbuf);
1114
1115 pixbuf_surface = gnome_bg_cairo_surface_create_from_pixbuf (pixbuf, scale, cairo_get_target (cr));
1116 cairo_set_source_surface (cr, pixbuf_surface, 0, 0);
1117 cairo_surface_destroy (pixbuf_surface);
1118 g_object_unref (pixbuf);
1119 }
1120
1121 cairo_paint (cr);
1122
1123 cairo_destroy (cr);
1124
1125 return target_surface;
1126 }
1127
1128
1129 /* determine if a background is darker or lighter than average, to help
1130 * clients know what colors to draw on top with
1131 */
1132 gboolean
1133 gnome_bg_is_dark (GnomeBG *bg,
1134 int width,
1135 int height)
1136 {
1137 GdkRGBA color;
1138 gdouble intensity;
1139 GdkPixbuf *pixbuf;
1140
1141 g_return_val_if_fail (bg != NULL, FALSE);
1142
1143 if (bg->color_type == G_DESKTOP_BACKGROUND_SHADING_SOLID) {
1144 color = bg->primary;
1145 } else {
1146 color.red = (bg->primary.red + bg->secondary.red) / 2;
1147 color.green = (bg->primary.green + bg->secondary.green) / 2;
1148 color.blue = (bg->primary.blue + bg->secondary.blue) / 2;
1149 }
1150 pixbuf = get_pixbuf_for_size (bg, -1, width, height);
1151 if (pixbuf) {
1152 GdkRGBA average;
1153
1154 pixbuf_average_value (pixbuf, &average);
1155
1156 color.red = color.red * (1.0 - average.alpha) + average.red * average.alpha;
1157 color.green = color.green * (1.0 - average.alpha) + average.green * average.alpha;
1158 color.blue = color.blue * (1.0 - average.alpha) + average.blue * average.alpha;
1159 g_object_unref (pixbuf);
1160 }
1161
1162 intensity = color.red * 77 +
1163 color.green * 150 +
1164 color.blue * 28;
1165
1166 return intensity < 160; /* biased slightly to be dark */
1167 }
1168
1169 static gboolean
1170 get_original_size (const char *filename,
1171 int *orig_width,
1172 int *orig_height)
1173 {
1174 gboolean result;
1175
1176 if (gdk_pixbuf_get_file_info (filename, orig_width, orig_height))
1177 result = TRUE;
1178 else
1179 result = FALSE;
1180
1181 return result;
1182 }
1183
1184 static const char *
1185 get_filename_for_size (GnomeBG *bg, gint best_width, gint best_height)
1186 {
1187 GnomeBGSlideShow *show;
1188 const char *file = NULL;
1189
1190 if (!bg->filename)
1191 return NULL;
1192
1193 show = get_as_slideshow (bg, bg->filename);
1194 if (!show) {
1195 return bg->filename;
1196 }
1197
1198 gnome_bg_slide_show_get_current_slide (show, best_width, best_height, NULL, NULL, NULL, &file, NULL);
1199 g_object_unref (show);
1200
1201 return file;
1202 }
1203
1204 gboolean
1205 gnome_bg_get_image_size (GnomeBG *bg,
1206 GnomeDesktopThumbnailFactory *factory,
1207 int best_width,
1208 int best_height,
1209 int *width,
1210 int *height)
1211 {
1212 GdkPixbuf *thumb;
1213 gboolean result = FALSE;
1214 const gchar *filename;
1215
1216 g_return_val_if_fail (bg != NULL, FALSE);
1217 g_return_val_if_fail (factory != NULL, FALSE);
1218
1219 if (!bg->filename)
1220 return FALSE;
1221
1222 filename = get_filename_for_size (bg, best_width, best_height);
1223 thumb = create_thumbnail_for_filename (factory, filename);
1224 if (thumb) {
1225 if (get_thumb_annotations (thumb, width, height))
1226 result = TRUE;
1227
1228 g_object_unref (thumb);
1229 }
1230
1231 if (!result) {
1232 if (get_original_size (filename, width, height))
1233 result = TRUE;
1234 }
1235
1236 return result;
1237 }
1238
1239 static double
1240 fit_factor (int from_width, int from_height,
1241 int to_width, int to_height)
1242 {
1243 return MIN (to_width / (double) from_width, to_height / (double) from_height);
1244 }
1245
1246 /**
1247 * gnome_bg_create_thumbnail:
1248 *
1249 * Returns: (transfer full): a #GdkPixbuf showing the background as a thumbnail
1250 */
1251 GdkPixbuf *
1252 gnome_bg_create_thumbnail (GnomeBG *bg,
1253 GnomeDesktopThumbnailFactory *factory,
1254 const cairo_rectangle_int_t *screen_area,
1255 int dest_width,
1256 int dest_height)
1257 {
1258 GdkPixbuf *result;
1259 GdkPixbuf *thumb;
1260
1261 g_return_val_if_fail (bg != NULL, NULL);
1262
1263 result = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, dest_width, dest_height);
1264
1265 draw_color (bg, result);
1266
1267 if (bg->placement != G_DESKTOP_BACKGROUND_STYLE_NONE) {
1268 thumb = create_img_thumbnail (bg, factory, screen_area, dest_width, dest_height, -1);
1269
1270 if (thumb) {
1271 draw_image_for_thumb (bg, thumb, result);
1272 g_object_unref (thumb);
1273 }
1274 }
1275
1276 return result;
1277 }
1278
1279 /* Implementation of the pixbuf cache */
1280 struct _SlideShow
1281 {
1282 gint ref_count;
1283 double start_time;
1284 double total_duration;
1285
1286 GQueue *slides;
1287
1288 gboolean has_multiple_sizes;
1289
1290 /* used during parsing */
1291 struct tm start_tm;
1292 GQueue *stack;
1293 };
1294
1295
1296 static GdkPixbuf *
1297 blend (GdkPixbuf *p1,
1298 GdkPixbuf *p2,
1299 double alpha)
1300 {
1301 GdkPixbuf *result = gdk_pixbuf_copy (p1);
1302 GdkPixbuf *tmp;
1303
1304 if (gdk_pixbuf_get_width (p2) != gdk_pixbuf_get_width (p1) ||
1305 gdk_pixbuf_get_height (p2) != gdk_pixbuf_get_height (p1)) {
1306 tmp = gdk_pixbuf_scale_simple (p2,
1307 gdk_pixbuf_get_width (p1),
1308 gdk_pixbuf_get_height (p1),
1309 GDK_INTERP_BILINEAR);
1310 }
1311 else {
1312 tmp = g_object_ref (p2);
1313 }
1314
1315 pixbuf_blend (tmp, result, 0, 0, -1, -1, 0, 0, alpha);
1316
1317 g_object_unref (tmp);
1318
1319 return result;
1320 }
1321
1322 typedef enum {
1323 PIXBUF,
1324 SLIDESHOW,
1325 THUMBNAIL
1326 } FileType;
1327
1328 struct FileCacheEntry
1329 {
1330 FileType type;
1331 char *filename;
1332 union {
1333 GdkPixbuf *pixbuf;
1334 GnomeBGSlideShow *slideshow;
1335 GdkPixbuf *thumbnail;
1336 } u;
1337 };
1338
1339 static void
1340 file_cache_entry_delete (FileCacheEntry *ent)
1341 {
1342 g_free (ent->filename);
1343
1344 switch (ent->type) {
1345 case PIXBUF:
1346 g_object_unref (ent->u.pixbuf);
1347 break;
1348 case SLIDESHOW:
1349 g_object_unref (ent->u.slideshow);
1350 break;
1351 case THUMBNAIL:
1352 g_object_unref (ent->u.thumbnail);
1353 break;
1354 default:
1355 break;
1356 }
1357
1358 g_free (ent);
1359 }
1360
1361 static void
1362 bound_cache (GnomeBG *bg)
1363 {
1364 while (g_list_length (bg->file_cache) >= CACHE_SIZE) {
1365 GList *last_link = g_list_last (bg->file_cache);
1366 FileCacheEntry *ent = last_link->data;
1367
1368 file_cache_entry_delete (ent);
1369
1370 bg->file_cache = g_list_delete_link (bg->file_cache, last_link);
1371 }
1372 }
1373
1374 static const FileCacheEntry *
1375 file_cache_lookup (GnomeBG *bg, FileType type, const char *filename)
1376 {
1377 GList *list;
1378
1379 for (list = bg->file_cache; list != NULL; list = list->next) {
1380 FileCacheEntry *ent = list->data;
1381
1382 if (ent && ent->type == type &&
1383 strcmp (ent->filename, filename) == 0) {
1384 return ent;
1385 }
1386 }
1387
1388 return NULL;
1389 }
1390
1391 static FileCacheEntry *
1392 file_cache_entry_new (GnomeBG *bg,
1393 FileType type,
1394 const char *filename)
1395 {
1396 FileCacheEntry *ent = g_new0 (FileCacheEntry, 1);
1397
1398 g_assert (!file_cache_lookup (bg, type, filename));
1399
1400 ent->type = type;
1401 ent->filename = g_strdup (filename);
1402
1403 bg->file_cache = g_list_prepend (bg->file_cache, ent);
1404
1405 bound_cache (bg);
1406
1407 return ent;
1408 }
1409
1410 static void
1411 file_cache_add_pixbuf (GnomeBG *bg,
1412 const char *filename,
1413 GdkPixbuf *pixbuf)
1414 {
1415 FileCacheEntry *ent = file_cache_entry_new (bg, PIXBUF, filename);
1416 ent->u.pixbuf = g_object_ref (pixbuf);
1417 }
1418
1419 static void
1420 file_cache_add_thumbnail (GnomeBG *bg,
1421 const char *filename,
1422 GdkPixbuf *pixbuf)
1423 {
1424 FileCacheEntry *ent = file_cache_entry_new (bg, THUMBNAIL, filename);
1425 ent->u.thumbnail = g_object_ref (pixbuf);
1426 }
1427
1428 static void
1429 file_cache_add_slide_show (GnomeBG *bg,
1430 const char *filename,
1431 GnomeBGSlideShow *show)
1432 {
1433 FileCacheEntry *ent = file_cache_entry_new (bg, SLIDESHOW, filename);
1434 ent->u.slideshow = g_object_ref (show);
1435 }
1436
1437 static GdkPixbuf *
1438 load_from_cache_file (GnomeBG *bg,
1439 const char *filename,
1440 gint num_monitor,
1441 gint best_width,
1442 gint best_height)
1443 {
1444 GdkPixbuf *pixbuf = NULL;
1445 gchar *cache_filename;
1446
1447 cache_filename = get_wallpaper_cache_filename (filename, num_monitor, bg->placement, best_width, best_height);
1448 if (cache_file_is_valid (filename, cache_filename))
1449 pixbuf = gdk_pixbuf_new_from_file (cache_filename, NULL);
1450 g_free (cache_filename);
1451
1452 return pixbuf;
1453 }
1454
1455 static GdkPixbuf *
1456 get_as_pixbuf_for_size (GnomeBG *bg,
1457 const char *filename,
1458 gint num_monitor,
1459 gint best_width,
1460 gint best_height)
1461 {
1462 const FileCacheEntry *ent;
1463 if ((ent = file_cache_lookup (bg, PIXBUF, filename))) {
1464 return g_object_ref (ent->u.pixbuf);
1465 }
1466 else {
1467 GdkPixbufFormat *format;
1468 GdkPixbuf *pixbuf;
1469 gchar *tmp;
1470 pixbuf = NULL;
1471
1472 /* Try to hit local cache first if relevant */
1473 if (num_monitor != -1)
1474 pixbuf = load_from_cache_file (bg, filename, num_monitor, best_width, best_height);
1475
1476 if (!pixbuf) {
1477 /* If scalable choose maximum size */
1478 format = gdk_pixbuf_get_file_info (filename, NULL, NULL);
1479
1480 if (format != NULL) {
1481 tmp = gdk_pixbuf_format_get_name (format);
1482 } else {
1483 tmp = NULL;
1484 }
1485
1486 if (tmp != NULL &&
1487 strcmp (tmp, "svg") == 0 &&
1488 (best_width > 0 && best_height > 0) &&
1489 (bg->placement == G_DESKTOP_BACKGROUND_STYLE_STRETCHED ||
1490 bg->placement == G_DESKTOP_BACKGROUND_STYLE_SCALED ||
1491 bg->placement == G_DESKTOP_BACKGROUND_STYLE_ZOOM))
1492 pixbuf = gdk_pixbuf_new_from_file_at_size (filename, best_width, best_height, NULL);
1493 else
1494 pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
1495 g_free (tmp);
1496 }
1497
1498 if (pixbuf)
1499 file_cache_add_pixbuf (bg, filename, pixbuf);
1500
1501 return pixbuf;
1502 }
1503 }
1504
1505 static GnomeBGSlideShow *
1506 get_as_slideshow (GnomeBG *bg, const char *filename)
1507 {
1508 const FileCacheEntry *ent;
1509 if ((ent = file_cache_lookup (bg, SLIDESHOW, filename))) {
1510 return g_object_ref (ent->u.slideshow);
1511 }
1512 else {
1513 GnomeBGSlideShow *show = read_slideshow_file (filename, NULL);
1514
1515 if (show)
1516 file_cache_add_slide_show (bg, filename, show);
1517
1518 return show;
1519 }
1520 }
1521
1522 static GdkPixbuf *
1523 get_as_thumbnail (GnomeBG *bg, GnomeDesktopThumbnailFactory *factory, const char *filename)
1524 {
1525 const FileCacheEntry *ent;
1526 if ((ent = file_cache_lookup (bg, THUMBNAIL, filename))) {
1527 return g_object_ref (ent->u.thumbnail);
1528 }
1529 else {
1530 GdkPixbuf *thumb = create_thumbnail_for_filename (factory, filename);
1531
1532 if (thumb)
1533 file_cache_add_thumbnail (bg, filename, thumb);
1534
1535 return thumb;
1536 }
1537 }
1538
1539 static gboolean
1540 blow_expensive_caches (gpointer data)
1541 {
1542 GnomeBG *bg = data;
1543 GList *list, *next;
1544
1545 bg->blow_caches_id = 0;
1546
1547 for (list = bg->file_cache; list != NULL; list = next) {
1548 FileCacheEntry *ent = list->data;
1549 next = list->next;
1550
1551 if (ent->type == PIXBUF) {
1552 file_cache_entry_delete (ent);
1553 bg->file_cache = g_list_delete_link (bg->file_cache,
1554 list);
1555 }
1556 }
1557
1558 if (bg->pixbuf_cache) {
1559 g_object_unref (bg->pixbuf_cache);
1560 bg->pixbuf_cache = NULL;
1561 }
1562
1563 return FALSE;
1564 }
1565
1566 static void
1567 blow_expensive_caches_in_idle (GnomeBG *bg)
1568 {
1569 if (bg->blow_caches_id == 0) {
1570 bg->blow_caches_id =
1571 g_idle_add (blow_expensive_caches,
1572 bg);
1573 }
1574 }
1575
1576
1577 static gboolean
1578 on_timeout (gpointer data)
1579 {
1580 GnomeBG *bg = data;
1581
1582 bg->timeout_id = 0;
1583
1584 queue_transitioned (bg);
1585
1586 return FALSE;
1587 }
1588
1589 static double
1590 get_slide_timeout (gboolean is_fixed,
1591 gdouble duration)
1592 {
1593 double timeout;
1594 if (is_fixed) {
1595 timeout = duration;
1596 } else {
1597 /* Maybe the number of steps should be configurable? */
1598
1599 /* In the worst case we will do a fade from 0 to 256, which mean
1600 * we will never use more than 255 steps, however in most cases
1601 * the first and last value are similar and users can't percieve
1602 * changes in pixel values as small as 1/255th. So, lets not waste
1603 * CPU cycles on transitioning to often.
1604 *
1605 * 64 steps is enough for each step to be just detectable in a 16bit
1606 * color mode in the worst case, so we'll use this as an approximation
1607 * of whats detectable.
1608 */
1609 timeout = duration / 64.0;
1610 }
1611 return timeout;
1612 }
1613
1614 static void
1615 ensure_timeout (GnomeBG *bg,
1616 gdouble timeout)
1617 {
1618 if (!bg->timeout_id) {
1619 /* G_MAXUINT means "only one slide" */
1620 if (timeout < G_MAXUINT) {
1621 bg->timeout_id = g_timeout_add_full (
1622 G_PRIORITY_LOW,
1623 timeout * 1000, on_timeout, bg, NULL);
1624 }
1625 }
1626 }
1627
1628 static time_t
1629 get_mtime (const char *filename)
1630 {
1631 GFile *file;
1632 GFileInfo *info;
1633 time_t mtime;
1634
1635 mtime = (time_t)-1;
1636
1637 if (filename) {
1638 file = g_file_new_for_path (filename);
1639 info = g_file_query_info (file, G_FILE_ATTRIBUTE_TIME_MODIFIED,
1640 G_FILE_QUERY_INFO_NONE, NULL, NULL);
1641 if (info) {
1642 mtime = g_file_info_get_attribute_uint64 (info,
1643 G_FILE_ATTRIBUTE_TIME_MODIFIED);
1644 g_object_unref (info);
1645 }
1646 g_object_unref (file);
1647 }
1648
1649 return mtime;
1650 }
1651
1652 static GdkPixbuf *
1653 scale_thumbnail (GDesktopBackgroundStyle placement,
1654 const char *filename,
1655 GdkPixbuf *thumb,
1656 const cairo_rectangle_int_t *screen_area,
1657 int dest_width,
1658 int dest_height)
1659 {
1660 int o_width;
1661 int o_height;
1662
1663 if (placement != G_DESKTOP_BACKGROUND_STYLE_WALLPAPER &&
1664 placement != G_DESKTOP_BACKGROUND_STYLE_CENTERED) {
1665
1666 /* In this case, the pixbuf will be scaled to fit the screen anyway,
1667 * so just return the pixbuf here
1668 */
1669 return g_object_ref (thumb);
1670 }
1671
1672 if (get_thumb_annotations (thumb, &o_width, &o_height) ||
1673 (filename && get_original_size (filename, &o_width, &o_height))) {
1674 int scr_width = screen_area->width;
1675 int scr_height = screen_area->height;
1676 int thumb_width = gdk_pixbuf_get_width (thumb);
1677 int thumb_height = gdk_pixbuf_get_height (thumb);
1678 double screen_to_dest = fit_factor (scr_width, scr_height, dest_width, dest_height);
1679 double thumb_to_orig = fit_factor (thumb_width, thumb_height, o_width, o_height);
1680 double f = thumb_to_orig * screen_to_dest;
1681 int new_width, new_height;
1682
1683 new_width = floor (thumb_width * f + 0.5);
1684 new_height = floor (thumb_height * f + 0.5);
1685
1686 if (placement == G_DESKTOP_BACKGROUND_STYLE_WALLPAPER) {
1687 /* Heuristic to make sure tiles don't become so small that
1688 * they turn into a blur.
1689 *
1690 * This is strictly speaking incorrect, but the resulting
1691 * thumbnail gives a much better idea what the background
1692 * will actually look like.
1693 */
1694
1695 if ((new_width < 32 || new_height < 32) &&
1696 (new_width < o_width / 4 || new_height < o_height / 4)) {
1697 new_width = o_width / 4;
1698 new_height = o_height / 4;
1699 }
1700 }
1701
1702 thumb = gdk_pixbuf_scale_simple (thumb, new_width, new_height,
1703 GDK_INTERP_BILINEAR);
1704 }
1705 else
1706 g_object_ref (thumb);
1707
1708 return thumb;
1709 }
1710
1711 /* frame_num determines which slide to thumbnail.
1712 * -1 means 'current slide'.
1713 */
1714 static GdkPixbuf *
1715 create_img_thumbnail (GnomeBG *bg,
1716 GnomeDesktopThumbnailFactory *factory,
1717 const cairo_rectangle_int_t *screen_area,
1718 int dest_width,
1719 int dest_height,
1720 int frame_num)
1721 {
1722 if (bg->filename) {
1723 GdkPixbuf *thumb;
1724
1725 thumb = get_as_thumbnail (bg, factory, bg->filename);
1726
1727 if (thumb) {
1728 GdkPixbuf *result;
1729 result = scale_thumbnail (bg->placement,
1730 bg->filename,
1731 thumb,
1732 screen_area,
1733 dest_width,
1734 dest_height);
1735 g_object_unref (thumb);
1736 return result;
1737 }
1738 else {
1739 GnomeBGSlideShow *show = get_as_slideshow (bg, bg->filename);
1740
1741 if (show) {
1742 double alpha;
1743 double duration;
1744 gboolean is_fixed;
1745 const char *file1;
1746 const char *file2;
1747 GdkPixbuf *tmp;
1748
1749 if (frame_num == -1)
1750 gnome_bg_slide_show_get_current_slide (show,
1751 dest_width,
1752 dest_height,
1753 &alpha,
1754 &duration,
1755 &is_fixed,
1756 &file1,
1757 &file2);
1758 else
1759 gnome_bg_slide_show_get_slide (show,
1760 frame_num,
1761 dest_width,
1762 dest_height,
1763 &alpha,
1764 &duration,
1765 &is_fixed,
1766 &file1,
1767 &file2);
1768
1769 if (is_fixed) {
1770 tmp = get_as_thumbnail (bg, factory, file1);
1771 if (tmp) {
1772 thumb = scale_thumbnail (bg->placement,
1773 file1,
1774 tmp,
1775 screen_area,
1776 dest_width,
1777 dest_height);
1778 g_object_unref (tmp);
1779 }
1780 }
1781 else {
1782 GdkPixbuf *p1, *p2;
1783 p1 = get_as_thumbnail (bg, factory, file1);
1784 p2 = get_as_thumbnail (bg, factory, file2);
1785
1786 if (p1 && p2) {
1787 GdkPixbuf *thumb1, *thumb2;
1788
1789 thumb1 = scale_thumbnail (bg->placement,
1790 file1,
1791 p1,
1792 screen_area,
1793 dest_width,
1794 dest_height);
1795
1796 thumb2 = scale_thumbnail (bg->placement,
1797 file2,
1798 p2,
1799 screen_area,
1800 dest_width,
1801 dest_height);
1802
1803 thumb = blend (thumb1, thumb2, alpha);
1804
1805 g_object_unref (thumb1);
1806 g_object_unref (thumb2);
1807 }
1808 if (p1)
1809 g_object_unref (p1);
1810 if (p2)
1811 g_object_unref (p2);
1812 }
1813
1814 ensure_timeout (bg, (guint)get_slide_timeout (is_fixed, duration));
1815
1816 g_object_unref (show);
1817 }
1818 }
1819
1820 return thumb;
1821 }
1822
1823 return NULL;
1824 }
1825
1826 static GdkPixbuf *
1827 get_pixbuf_for_size (GnomeBG *bg,
1828 gint num_monitor,
1829 gint best_width,
1830 gint best_height)
1831 {
1832 guint time_until_next_change;
1833 gboolean hit_cache = FALSE;
1834
1835 /* only hit the cache if the aspect ratio matches */
1836 if (bg->pixbuf_cache) {
1837 int width, height;
1838 width = gdk_pixbuf_get_width (bg->pixbuf_cache);
1839 height = gdk_pixbuf_get_height (bg->pixbuf_cache);
1840 hit_cache = 0.2 > fabs ((best_width / (double)best_height) - (width / (double)height));
1841 if (!hit_cache) {
1842 g_object_unref (bg->pixbuf_cache);
1843 bg->pixbuf_cache = NULL;
1844 }
1845 }
1846
1847 if (!hit_cache && bg->filename) {
1848 bg->file_mtime = get_mtime (bg->filename);
1849
1850 bg->pixbuf_cache = get_as_pixbuf_for_size (bg, bg->filename, num_monitor, best_width, best_height);
1851 time_until_next_change = G_MAXUINT;
1852 if (!bg->pixbuf_cache) {
1853 GnomeBGSlideShow *show = get_as_slideshow (bg, bg->filename);
1854
1855 if (show) {
1856 double alpha;
1857 double duration;
1858 gboolean is_fixed;
1859 const char *file1;
1860 const char *file2;
1861
1862 g_object_ref (show);
1863
1864 gnome_bg_slide_show_get_current_slide (show,
1865 best_width,
1866 best_height,
1867 &alpha,
1868 &duration,
1869 &is_fixed,
1870 &file1,
1871 &file2);
1872 time_until_next_change = (guint)get_slide_timeout (is_fixed, duration);
1873 if (is_fixed) {
1874 bg->pixbuf_cache = get_as_pixbuf_for_size (bg, file1, num_monitor, best_width, best_height);
1875 }
1876 else {
1877 GdkPixbuf *p1, *p2;
1878 p1 = get_as_pixbuf_for_size (bg, file1, num_monitor, best_width, best_height);
1879 p2 = get_as_pixbuf_for_size (bg, file2, num_monitor, best_width, best_height);
1880
1881 if (p1 && p2) {
1882 bg->pixbuf_cache = blend (p1, p2, alpha);
1883 }
1884 if (p1)
1885 g_object_unref (p1);
1886 if (p2)
1887 g_object_unref (p2);
1888 }
1889
1890 ensure_timeout (bg, time_until_next_change);
1891
1892 g_object_unref (show);
1893 }
1894 }
1895
1896 /* If the next slideshow step is a long time away then
1897 we blow away the expensive stuff (large pixbufs) from
1898 the cache */
1899 if (time_until_next_change > KEEP_EXPENSIVE_CACHE_SECS)
1900 blow_expensive_caches_in_idle (bg);
1901 }
1902
1903 if (bg->pixbuf_cache)
1904 g_object_ref (bg->pixbuf_cache);
1905
1906 return bg->pixbuf_cache;
1907 }
1908
1909 static gboolean
1910 is_different (GnomeBG *bg,
1911 const char *filename)
1912 {
1913 if (!filename && bg->filename) {
1914 return TRUE;
1915 }
1916 else if (filename && !bg->filename) {
1917 return TRUE;
1918 }
1919 else if (!filename && !bg->filename) {
1920 return FALSE;
1921 }
1922 else {
1923 time_t mtime = get_mtime (filename);
1924
1925 if (mtime != bg->file_mtime)
1926 return TRUE;
1927
1928 if (strcmp (filename, bg->filename) != 0)
1929 return TRUE;
1930
1931 return FALSE;
1932 }
1933 }
1934
1935 static void
1936 clear_cache (GnomeBG *bg)
1937 {
1938 GList *list;
1939
1940 if (bg->file_cache) {
1941 for (list = bg->file_cache; list != NULL; list = list->next) {
1942 FileCacheEntry *ent = list->data;
1943
1944 file_cache_entry_delete (ent);
1945 }
1946 g_list_free (bg->file_cache);
1947 bg->file_cache = NULL;
1948 }
1949
1950 if (bg->pixbuf_cache) {
1951 g_object_unref (bg->pixbuf_cache);
1952
1953 bg->pixbuf_cache = NULL;
1954 }
1955
1956 if (bg->timeout_id) {
1957 g_source_remove (bg->timeout_id);
1958
1959 bg->timeout_id = 0;
1960 }
1961 }
1962
1963 /* Pixbuf utilities */
1964 static void
1965 pixbuf_average_value (GdkPixbuf *pixbuf,
1966 GdkRGBA *result)
1967 {
1968 guint64 a_total, r_total, g_total, b_total;
1969 guint row, column;
1970 int row_stride;
1971 const guchar *pixels, *p;
1972 int r, g, b, a;
1973 guint64 dividend;
1974 guint width, height;
1975 gdouble dd;
1976
1977 width = gdk_pixbuf_get_width (pixbuf);
1978 height = gdk_pixbuf_get_height (pixbuf);
1979 row_stride = gdk_pixbuf_get_rowstride (pixbuf);
1980 pixels = gdk_pixbuf_get_pixels (pixbuf);
1981
1982 /* iterate through the pixbuf, counting up each component */
1983 a_total = 0;
1984 r_total = 0;
1985 g_total = 0;
1986 b_total = 0;
1987
1988 if (gdk_pixbuf_get_has_alpha (pixbuf)) {
1989 for (row = 0; row < height; row++) {
1990 p = pixels + (row * row_stride);
1991 for (column = 0; column < width; column++) {
1992 r = *p++;
1993 g = *p++;
1994 b = *p++;
1995 a = *p++;
1996
1997 a_total += a;
1998 r_total += r * a;
1999 g_total += g * a;
2000 b_total += b * a;
2001 }
2002 }
2003 dividend = height * width * 0xFF;
2004 a_total *= 0xFF;
2005 } else {
2006 for (row = 0; row < height; row++) {
2007 p = pixels + (row * row_stride);
2008 for (column = 0; column < width; column++) {
2009 r = *p++;
2010 g = *p++;
2011 b = *p++;
2012
2013 r_total += r;
2014 g_total += g;
2015 b_total += b;
2016 }
2017 }
2018 dividend = height * width;
2019 a_total = dividend * 0xFF;
2020 }
2021
2022 dd = dividend * 0xFF;
2023 result->alpha = a_total / dd;
2024 result->red = r_total / dd;
2025 result->green = g_total / dd;
2026 result->blue = b_total / dd;
2027 }
2028
2029 static GdkPixbuf *
2030 pixbuf_scale_to_fit (GdkPixbuf *src, int max_width, int max_height)
2031 {
2032 double factor;
2033 int src_width, src_height;
2034 int new_width, new_height;
2035
2036 src_width = gdk_pixbuf_get_width (src);
2037 src_height = gdk_pixbuf_get_height (src);
2038
2039 factor = MIN (max_width / (double) src_width, max_height / (double) src_height);
2040
2041 new_width = floor (src_width * factor + 0.5);
2042 new_height = floor (src_height * factor + 0.5);
2043
2044 return gdk_pixbuf_scale_simple (src, new_width, new_height, GDK_INTERP_BILINEAR);
2045 }
2046
2047 static GdkPixbuf *
2048 pixbuf_scale_to_min (GdkPixbuf *src, int min_width, int min_height)
2049 {
2050 double factor;
2051 int src_width, src_height;
2052 int new_width, new_height;
2053 GdkPixbuf *dest;
2054
2055 src_width = gdk_pixbuf_get_width (src);
2056 src_height = gdk_pixbuf_get_height (src);
2057
2058 factor = MAX (min_width / (double) src_width, min_height / (double) src_height);
2059
2060 new_width = floor (src_width * factor + 0.5);
2061 new_height = floor (src_height * factor + 0.5);
2062
2063 dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
2064 gdk_pixbuf_get_has_alpha (src),
2065 8, min_width, min_height);
2066 if (!dest)
2067 return NULL;
2068
2069 /* crop the result */
2070 gdk_pixbuf_scale (src, dest,
2071 0, 0,
2072 min_width, min_height,
2073 (new_width - min_width) / -2,
2074 (new_height - min_height) / -2,
2075 factor,
2076 factor,
2077 GDK_INTERP_BILINEAR);
2078 return dest;
2079 }
2080
2081 static guchar *
2082 create_gradient (const GdkRGBA *primary,
2083 const GdkRGBA *secondary,
2084 int n_pixels)
2085 {
2086 guchar *result = g_malloc (n_pixels * 3);
2087 int i;
2088
2089 for (i = 0; i < n_pixels; ++i) {
2090 double ratio = (i + 0.5) / n_pixels;
2091
2092 result[3 * i + 0] = (int) (0.5 + (primary->red * (1 - ratio) + secondary->red * ratio) * 255);
2093 result[3 * i + 1] = (int) (0.5 + (primary->green * (1 - ratio) + secondary->green * ratio) * 255);
2094 result[3 * i + 2] = (int) (0.5 + (primary->blue * (1 - ratio) + secondary->blue * ratio) * 255);
2095 }
2096
2097 return result;
2098 }
2099
2100 static void
2101 pixbuf_draw_gradient (GdkPixbuf *pixbuf,
2102 gboolean horizontal,
2103 GdkRGBA *primary,
2104 GdkRGBA *secondary,
2105 GdkRectangle *rect)
2106 {
2107 int width;
2108 int height;
2109 int rowstride;
2110 guchar *dst;
2111 int n_channels = 3;
2112
2113 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
2114 width = rect->width;
2115 height = rect->height;
2116 dst = gdk_pixbuf_get_pixels (pixbuf) + rect->x * n_channels + rowstride * rect->y;
2117
2118 if (horizontal) {
2119 guchar *gradient = create_gradient (primary, secondary, width);
2120 int copy_bytes_per_row = width * n_channels;
2121 int i;
2122
2123 for (i = 0; i < height; i++) {
2124 guchar *d;
2125 d = dst + rowstride * i;
2126 memcpy (d, gradient, copy_bytes_per_row);
2127 }
2128 g_free (gradient);
2129 } else {
2130 guchar *gb, *gradient;
2131 int i;
2132
2133 gradient = create_gradient (primary, secondary, height);
2134 for (i = 0; i < height; i++) {
2135 int j;
2136 guchar *d;
2137
2138 d = dst + rowstride * i;
2139 gb = gradient + n_channels * i;
2140 for (j = width; j > 0; j--) {
2141 int k;
2142
2143 for (k = 0; k < n_channels; k++) {
2144 *(d++) = gb[k];
2145 }
2146 }
2147 }
2148
2149 g_free (gradient);
2150 }
2151 }
2152
2153 static void
2154 pixbuf_blend (GdkPixbuf *src,
2155 GdkPixbuf *dest,
2156 int src_x,
2157 int src_y,
2158 int src_width,
2159 int src_height,
2160 int dest_x,
2161 int dest_y,
2162 double alpha)
2163 {
2164 int dest_width = gdk_pixbuf_get_width (dest);
2165 int dest_height = gdk_pixbuf_get_height (dest);
2166 int offset_x = dest_x - src_x;
2167 int offset_y = dest_y - src_y;
2168
2169 if (src_width < 0)
2170 src_width = gdk_pixbuf_get_width (src);
2171
2172 if (src_height < 0)
2173 src_height = gdk_pixbuf_get_height (src);
2174
2175 if (dest_x < 0)
2176 dest_x = 0;
2177
2178 if (dest_y < 0)
2179 dest_y = 0;
2180
2181 if (dest_x + src_width > dest_width) {
2182 src_width = dest_width - dest_x;
2183 }
2184
2185 if (dest_y + src_height > dest_height) {
2186 src_height = dest_height - dest_y;
2187 }
2188
2189 gdk_pixbuf_composite (src, dest,
2190 dest_x, dest_y,
2191 src_width, src_height,
2192 offset_x, offset_y,
2193 1, 1, GDK_INTERP_NEAREST,
2194 alpha * 0xFF + 0.5);
2195 }
2196
2197 static void
2198 pixbuf_tile (GdkPixbuf *src, GdkPixbuf *dest)
2199 {
2200 int x, y;
2201 int tile_width, tile_height;
2202 int dest_width = gdk_pixbuf_get_width (dest);
2203 int dest_height = gdk_pixbuf_get_height (dest);
2204 tile_width = gdk_pixbuf_get_width (src);
2205 tile_height = gdk_pixbuf_get_height (src);
2206
2207 for (y = 0; y < dest_height; y += tile_height) {
2208 for (x = 0; x < dest_width; x += tile_width) {
2209 pixbuf_blend (src, dest, 0, 0,
2210 tile_width, tile_height, x, y, 1.0);
2211 }
2212 }
2213 }
2214
2215 static GnomeBGSlideShow *
2216 read_slideshow_file (const char *filename,
2217 GError **err)
2218 {
2219 GnomeBGSlideShow *show;
2220
2221 /* Ignore anything not an XML file because we don't want to waste
2222 * time reading that file into memory only to try to parse it with
2223 * GMarkup.
2224 */
2225 if (!g_str_has_suffix (filename, ".xml")) {
2226 g_set_error_literal (err,
2227 G_IO_ERROR,
2228 G_IO_ERROR_NOT_SUPPORTED,
2229 "Not a slideshow file");
2230 return NULL;
2231 }
2232
2233 show = gnome_bg_slide_show_new (filename);
2234
2235 if (!gnome_bg_slide_show_load (show, err)) {
2236 g_object_unref (show);
2237 return NULL;
2238 }
2239
2240 return show;
2241 }
2242
2243 /* Thumbnail utilities */
2244 static GdkPixbuf *
2245 create_thumbnail_for_filename (GnomeDesktopThumbnailFactory *factory,
2246 const char *filename)
2247 {
2248 char *thumb;
2249 time_t mtime;
2250 GdkPixbuf *orig, *result = NULL;
2251 char *uri;
2252 GError *error = NULL;
2253
2254 mtime = get_mtime (filename);
2255
2256 if (mtime == (time_t)-1)
2257 return NULL;
2258
2259 uri = g_filename_to_uri (filename, NULL, NULL);
2260
2261 if (uri == NULL)
2262 return NULL;
2263
2264 thumb = gnome_desktop_thumbnail_factory_lookup (factory, uri, mtime);
2265
2266 if (thumb) {
2267 result = gdk_pixbuf_new_from_file (thumb, NULL);
2268 g_free (thumb);
2269 }
2270 else {
2271 orig = gdk_pixbuf_new_from_file (filename, NULL);
2272 if (orig) {
2273 int orig_width, orig_height;
2274 GdkPixbuf *rotated;
2275 gchar *orig_height_str, *orig_width_str;
2276
2277 rotated = gdk_pixbuf_apply_embedded_orientation (orig);
2278 if (rotated != NULL) {
2279 g_object_unref (orig);
2280 orig = rotated;
2281 }
2282
2283 orig_width = gdk_pixbuf_get_width (orig);
2284 orig_height = gdk_pixbuf_get_height (orig);
2285
2286 result = pixbuf_scale_to_fit (orig, THUMBNAIL_SIZE, THUMBNAIL_SIZE);
2287
2288 orig_height_str = g_strdup_printf ("%d", orig_height);
2289 orig_width_str = g_strdup_printf ("%d", orig_width);
2290
2291 gdk_pixbuf_set_option (result, "tEXt::Thumb::Image::Height", orig_height_str);
2292 gdk_pixbuf_set_option (result, "tEXt::Thumb::Image::Width", orig_width_str);
2293
2294 g_object_set_data_full (G_OBJECT (result), "gnome-thumbnail-height",
2295 g_steal_pointer (&orig_height_str), g_free);
2296 g_object_set_data_full (G_OBJECT (result), "gnome-thumbnail-width",
2297 g_steal_pointer (&orig_width_str), g_free);
2298
2299 g_object_unref (orig);
2300
2301 gnome_desktop_thumbnail_factory_save_thumbnail (factory, result, uri, mtime, NULL, &error);
2302 if (error) {
2303 g_warning ("Error while saving thumbnail: %s", error->message);
2304 g_error_free (error);
2305 }
2306 }
2307 else {
2308 gnome_desktop_thumbnail_factory_create_failed_thumbnail (factory, uri, mtime, NULL, &error);
2309 if (error) {
2310 g_warning ("Error while creating failed thumbnail: %s", error->message);
2311 g_error_free (error);
2312 }
2313 }
2314 }
2315
2316 g_free (uri);
2317
2318 return result;
2319 }
2320
2321 static gboolean
2322 get_thumb_annotations (GdkPixbuf *thumb,
2323 int *orig_width,
2324 int *orig_height)
2325 {
2326 char *end;
2327 const char *wstr, *hstr;
2328
2329 wstr = gdk_pixbuf_get_option (thumb, "tEXt::Thumb::Image::Width");
2330 hstr = gdk_pixbuf_get_option (thumb, "tEXt::Thumb::Image::Height");
2331
2332 if (hstr && wstr) {
2333 *orig_width = strtol (wstr, &end, 10);
2334 if (*end != 0)
2335 return FALSE;
2336
2337 *orig_height = strtol (hstr, &end, 10);
2338 if (*end != 0)
2339 return FALSE;
2340
2341 return TRUE;
2342 }
2343
2344 return FALSE;
2345 }
2346
2347 /*
2348 * Returns whether the background is a slideshow.
2349 */
2350 gboolean
2351 gnome_bg_changes_with_time (GnomeBG *bg)
2352 {
2353 GnomeBGSlideShow *show;
2354 gboolean ret = FALSE;
2355
2356 g_return_val_if_fail (bg != NULL, FALSE);
2357
2358 if (!bg->filename)
2359 return FALSE;
2360
2361 show = get_as_slideshow (bg, bg->filename);
2362 if (show) {
2363 ret = gnome_bg_slide_show_get_num_slides (show) > 1;
2364 g_object_unref (show);
2365 }
2366
2367 return ret;
2368 }
2369
2370 /**
2371 * gnome_bg_create_frame_thumbnail:
2372 *
2373 * Creates a thumbnail for a certain frame, where 'frame' is somewhat
2374 * vaguely defined as 'suitable point to show while single-stepping
2375 * through the slideshow'.
2376 *
2377 * Returns: (transfer full): the newly created thumbnail or
2378 * or NULL if frame_num is out of bounds.
2379 */
2380 GdkPixbuf *
2381 gnome_bg_create_frame_thumbnail (GnomeBG *bg,
2382 GnomeDesktopThumbnailFactory *factory,
2383 const cairo_rectangle_int_t *screen_area,
2384 int dest_width,
2385 int dest_height,
2386 int frame_num)
2387 {
2388 GnomeBGSlideShow *show;
2389 GdkPixbuf *result;
2390 GdkPixbuf *thumb;
2391 int skipped;
2392 gboolean is_fixed;
2393
2394 g_return_val_if_fail (bg != NULL, FALSE);
2395
2396 show = get_as_slideshow (bg, bg->filename);
2397
2398 if (!show)
2399 return NULL;
2400
2401
2402 if (frame_num < 0 || frame_num >= gnome_bg_slide_show_get_num_slides (show))
2403 return NULL;
2404
2405
2406 gnome_bg_slide_show_get_slide (show, frame_num, dest_width, dest_height, NULL, NULL, &is_fixed, NULL, NULL);
2407
2408 skipped = 0;
2409 while (!is_fixed) {
2410 skipped++;
2411 gnome_bg_slide_show_get_slide (show, frame_num, dest_width, dest_height, NULL, NULL, &is_fixed, NULL, NULL);
2412 }
2413
2414 result = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, dest_width, dest_height);
2415
2416 draw_color (bg, result);
2417
2418 if (bg->placement != G_DESKTOP_BACKGROUND_STYLE_NONE) {
2419 thumb = create_img_thumbnail (bg, factory, screen_area, dest_width, dest_height, frame_num + skipped);
2420
2421 if (thumb) {
2422 draw_image_for_thumb (bg, thumb, result);
2423 g_object_unref (thumb);
2424 }
2425 }
2426
2427 return result;
2428 }
2429