summaryrefslogtreecommitdiff
path: root/libbackground/applier.c
blob: e39797b294df5bcc856db672103e3f1a758d92d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
/* -*- mode: c; style: linux -*- */

/* applier.c
 * Copyright (C) 2001 Ximian, Inc.
 *
 * Written by Bradford Hovinen <hovinen@ximian.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <gnome.h>
#include <gdk/gdk.h>
#include <gdk/gdkx.h>
#include <gdk/gdkprivate.h>

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <unistd.h>
#include <bonobo.h>

#include "applier.h"

#define MONITOR_CONTENTS_X 20
#define MONITOR_CONTENTS_Y 10
#define MONITOR_CONTENTS_WIDTH 157
#define MONITOR_CONTENTS_HEIGHT 111

enum {
	PROP_0,
	PROP_TYPE
};

struct _ApplierPrivate 
{
	GtkWidget          *preview_widget;        /* The widget for previewing
						    * -- this is not used for
						    * actual rendering; it is
						    * returned if requested */
	BGPreferences      *last_prefs;            /* A cache of the last
						    * bg_preferences structure to
						    * be applied */

	GdkPixbuf          *wallpaper_pixbuf;      /* The "raw" wallpaper pixbuf */

	gboolean            nautilus_running;      /* TRUE iff nautilus is
						    * running, in which case we
						    * block the renderer */

	ApplierType         type;                  /* Whether we render to the
						    * root or the preview */

	/* Where on the pixmap we should render the background image. Should
         * have origin 0,0 and width and height equal to the desktop size if we
         * are rendering to the desktop. The area to which we render the pixbuf
         * will be smaller if we are rendering a centered image smaller than the
         * screen or scaling and keeping aspect ratio and the background color
         * is solid. */
	GdkRectangle        render_geom;

	/* Where to render the pixbuf, relative to the pixmap. This will be the
	 * same as render_geom above if we have no solid color area to worry
	 * about. By convention, a negative value means that the pixbuf is
	 * larger than the pixmap, so the region should be fetched from a subset
	 * of the pixbuf. */
	GdkRectangle        pixbuf_render_geom;

	/* Where to fetch the data from the pixbuf. We use this in case we are
	 * rendering a centered image that is larger than the size of the
	 * desktop. Otherwise it is (0,0) */
	GdkPoint            pixbuf_xlate;

	/* Geometry of the pixbuf used to render the gradient. If the wallpaper
	 * is not enabled, we use the following optimization: On one dimension,
	 * this should be equal to the dimension of render_geom, while on the
	 * other dimension it should be the constant 32. This avoids wasting
	 * memory with rendundant data. */
	GdkPoint            grad_geom;

	GdkPixbuf          *pixbuf;            /* "working" pixbuf - All data
						* are rendered onto this for
						* display */
	GdkPixmap          *pixmap;            /* Pixmap onto which we dump the
						* pixbuf above when we are ready
						* to render to the screen */
	gboolean            pixmap_is_set;     /* TRUE iff the pixmap above
						* has been set as the root
						* pixmap */
};

static GObjectClass *parent_class;

static void applier_init             (Applier           *prefs,
				      ApplierClass      *class);
static void applier_class_init       (ApplierClass      *class);
static void applier_base_init        (ApplierClass      *class);

static void applier_set_prop         (GObject           *object, 
				      guint              prop_id,
				      const GValue      *value,
				      GParamSpec        *pspec);
static void applier_get_prop         (GObject           *object, 
				      guint              prop_id,
				      GValue            *value,
				      GParamSpec        *pspec);

static void applier_dispose          (GObject           *object);
static void applier_finalize         (GObject           *object);

static void run_render_pipeline      (Applier           *applier, 
				      const BGPreferences *prefs);
static void draw_disabled_message    (GtkWidget         *widget);

static void render_background        (Applier           *applier,
				      const BGPreferences *prefs);
static void render_wallpaper         (Applier           *applier,
				      const BGPreferences *prefs);
static void render_to_screen         (Applier           *applier,
				      const BGPreferences *prefs);
static void create_pixmap            (Applier           *applier,
				      const BGPreferences *prefs);
static void get_geometry             (wallpaper_type_t   wallpaper_type,
				      GdkPixbuf         *pixbuf,
				      GdkRectangle      *field_geom,
				      GdkRectangle      *virtual_geom,
				      GdkRectangle      *dest_geom,
				      GdkRectangle      *src_geom);

static GdkPixbuf *place_pixbuf       (GdkPixbuf         *dest_pixbuf,
				      GdkPixbuf         *src_pixbuf,
				      GdkRectangle      *dest_geom,
				      GdkRectangle      *src_geom,
				      guint              alpha,
				      GdkColor          *bg_color);
static GdkPixbuf *tile_pixbuf        (GdkPixbuf         *dest_pixbuf,
				      GdkPixbuf         *src_pixbuf,
				      GdkRectangle      *field_geom,
				      guint              alpha,
				      GdkColor          *bg_color);
static void fill_gradient            (GdkPixbuf         *pixbuf,
				      GdkColor          *c1,
				      GdkColor          *c2,
				      orientation_t      orientation);

static gboolean need_wallpaper_load_p  (const Applier     *applier,
					const BGPreferences *prefs);
static gboolean need_root_pixmap_p     (const Applier     *applier,
					const BGPreferences *prefs);
static gboolean wallpaper_full_cover_p (const Applier     *applier,
					const BGPreferences *prefs);
static gboolean render_small_pixmap_p  (const BGPreferences *prefs);

static GdkPixmap *make_root_pixmap   (gint               width,
				      gint               height);
static void set_root_pixmap          (GdkPixmap         *pixmap);

static gboolean is_nautilus_running  (void);

guint
applier_get_type (void)
{
	static guint applier_type = 0;

	if (!applier_type) {
		static GTypeInfo applier_info = {
			sizeof (ApplierClass),
			(GBaseInitFunc) applier_base_init,
			NULL, /* GBaseFinalizeFunc */
			(GClassInitFunc) applier_class_init,
			NULL, /* GClassFinalizeFunc */
			NULL, /* user-supplied data */
			sizeof (Applier),
			0, /* n_preallocs */
			(GInstanceInitFunc) applier_init,
			NULL
		};

		applier_type = 
			g_type_register_static (G_TYPE_OBJECT, 
						"Applier",
						&applier_info, 0);
	}

	return applier_type;
}

static void
applier_init (Applier *applier, ApplierClass *class)
{
	applier->p                   = g_new0 (ApplierPrivate, 1);
	applier->p->last_prefs       = NULL;
	applier->p->pixbuf           = NULL;
	applier->p->wallpaper_pixbuf = NULL;
	applier->p->nautilus_running = is_nautilus_running ();
}

static void
applier_class_init (ApplierClass *class) 
{
	GObjectClass *object_class;
	GdkVisual *visual;

	object_class = G_OBJECT_CLASS (class);

	object_class->dispose = applier_dispose;
	object_class->finalize = applier_finalize;
	object_class->set_property = applier_set_prop;
	object_class->get_property = applier_get_prop;

	g_object_class_install_property
		(object_class, PROP_TYPE,
		 g_param_spec_int ("type",
				   _("Type"),
				   _("Type of applier: APPLIER_ROOT for root window or APPLIER_PREVIEW for preview"),
				   0, 1, 0,
				   G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

	parent_class = 
		G_OBJECT_CLASS (g_type_class_ref (G_TYPE_OBJECT));
}

static void
applier_base_init (ApplierClass *class) 
{
}

static void
applier_set_prop (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) 
{
	Applier *applier;

	g_return_if_fail (object != NULL);
	g_return_if_fail (IS_APPLIER (object));

	applier = APPLIER (object);

	switch (prop_id) {
	case PROP_TYPE:
		applier->p->type = g_value_get_int (value);

		switch (applier->p->type) {
		case APPLIER_ROOT:
			applier->p->render_geom.x = 0;
			applier->p->render_geom.y = 0;
			applier->p->render_geom.width = gdk_screen_width ();
			applier->p->render_geom.height = gdk_screen_height ();
			applier->p->pixmap = NULL;
			applier->p->pixmap_is_set = FALSE;
			break;

		case APPLIER_PREVIEW:
			applier->p->render_geom.x = MONITOR_CONTENTS_X;
			applier->p->render_geom.y = MONITOR_CONTENTS_Y;
			applier->p->render_geom.width = MONITOR_CONTENTS_WIDTH;
			applier->p->render_geom.height = MONITOR_CONTENTS_HEIGHT;
			break;

		default:
			g_critical ("Bad applier type: %d", applier->p->type);
			break;
		}

		break;

	default:
		g_warning ("Bad property set");
		break;
	}
}

static void
applier_get_prop (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) 
{
	Applier *applier;

	g_return_if_fail (object != NULL);
	g_return_if_fail (IS_APPLIER (object));

	applier = APPLIER (object);

	switch (prop_id) {
	case PROP_TYPE:
		g_value_set_int (value, applier->p->type);
		break;

	default:
		g_warning ("Bad property get");
		break;
	}
}

static void
applier_dispose (GObject *object) 
{
	Applier *applier;

	g_return_if_fail (object != NULL);
	g_return_if_fail (IS_APPLIER (object));

	applier = APPLIER (object);

	g_assert (applier->p->pixbuf == NULL);

	if (applier->p->last_prefs != NULL)
		g_object_unref (G_OBJECT (applier->p->last_prefs));

	if (applier->p->wallpaper_pixbuf != NULL)
		gdk_pixbuf_unref (applier->p->wallpaper_pixbuf);

	parent_class->dispose (object);
}

static void
applier_finalize (GObject *object) 
{
	Applier *applier;

	g_return_if_fail (object != NULL);
	g_return_if_fail (IS_APPLIER (object));

	applier = APPLIER (object);

	g_free (applier->p);

	parent_class->finalize (object);
}

GObject *
applier_new (ApplierType type) 
{
	GObject *object;

	object = g_object_new (applier_get_type (),
			       "type", type,
			       NULL);

	return object;
}

void
applier_apply_prefs (Applier           *applier, 
		     const BGPreferences *prefs)
{
	g_return_if_fail (applier != NULL);
	g_return_if_fail (IS_APPLIER (applier));

	if (applier->p->type == APPLIER_ROOT && applier->p->nautilus_running)
		set_root_pixmap ((GdkPixmap *) -1);

	if (!prefs->enabled) {
		if (applier->p->type == APPLIER_PREVIEW)
			draw_disabled_message (applier_get_preview_widget (applier));
		return;
	}

	if (need_wallpaper_load_p (applier, prefs)) {
		if (applier->p->wallpaper_pixbuf != NULL)
			gdk_pixbuf_unref (applier->p->wallpaper_pixbuf);

		applier->p->wallpaper_pixbuf = NULL;

		if (prefs->wallpaper_enabled) {
			g_return_if_fail (prefs->wallpaper_filename != NULL);

			applier->p->wallpaper_pixbuf = 
				gdk_pixbuf_new_from_file (prefs->wallpaper_filename, NULL);

			if (applier->p->wallpaper_pixbuf == NULL)
				g_warning (_("Could not load pixbuf \"%s\"; disabling wallpaper."),
					   prefs->wallpaper_filename);
		}
	}

	if (applier->p->type == APPLIER_ROOT)
		nice (20);

	run_render_pipeline (applier, prefs);

	if (applier->p->last_prefs != NULL)
		g_object_unref (G_OBJECT (applier->p->last_prefs));

	applier->p->last_prefs = BG_PREFERENCES (bg_preferences_clone (prefs));

	if (applier->p->type == APPLIER_PREVIEW && applier->p->preview_widget != NULL)
		gtk_widget_queue_draw (applier->p->preview_widget);
}

gboolean
applier_render_color_p (const Applier *applier, const BGPreferences *prefs) 
{
	g_return_val_if_fail (applier != NULL, FALSE);
	g_return_val_if_fail (IS_APPLIER (applier), FALSE);
	g_return_val_if_fail (prefs != NULL, FALSE);
	g_return_val_if_fail (IS_BG_PREFERENCES (prefs), FALSE);

	return prefs->enabled && !wallpaper_full_cover_p (applier, prefs);
}

GtkWidget *
applier_get_preview_widget (Applier *applier) 
{
	GdkPixbuf *pixbuf;
	GdkPixmap *pixmap;
	GdkBitmap *mask;
	GdkVisual *visual;
	GdkColormap *colormap;
	gchar *filename;
	GdkGC *gc;

	g_return_val_if_fail (applier != NULL, NULL);
	g_return_val_if_fail (IS_APPLIER (applier), NULL);

	if (applier->p->type != APPLIER_PREVIEW)
		return NULL;

	if (applier->p->preview_widget != NULL)
		return applier->p->preview_widget;

	filename = gnome_pixmap_file ("monitor.png");
	visual = gdk_window_get_visual (GDK_ROOT_PARENT ());
	colormap = gdk_window_get_colormap (GDK_ROOT_PARENT ());

	gtk_widget_push_visual (visual);
	gtk_widget_push_colormap (colormap);

	pixbuf = gdk_pixbuf_new_from_file (filename, NULL);

	if (pixbuf == NULL) return NULL;

	pixmap = gdk_pixmap_new (GDK_ROOT_PARENT (),
				 gdk_pixbuf_get_width (pixbuf),
				 gdk_pixbuf_get_height (pixbuf),
				 visual->depth);
	mask = gdk_pixmap_new (GDK_ROOT_PARENT (),
			       gdk_pixbuf_get_width (pixbuf),
			       gdk_pixbuf_get_height (pixbuf),
			       1);

	gc = gdk_gc_new (GDK_ROOT_PARENT ());

	gdk_pixbuf_render_threshold_alpha (pixbuf, mask,
					   0, 0, 0, 0,
					   gdk_pixbuf_get_width (pixbuf),
					   gdk_pixbuf_get_height (pixbuf),
					   1);

	gdk_gc_set_clip_mask (gc, mask);

	gdk_pixbuf_render_to_drawable (pixbuf, pixmap, gc,
				       0, 0, 0, 0,
				       gdk_pixbuf_get_width (pixbuf),
				       gdk_pixbuf_get_height (pixbuf),
				       GDK_RGB_DITHER_MAX, 0, 0);

	applier->p->preview_widget = gtk_pixmap_new (pixmap, mask);
	gtk_widget_show (applier->p->preview_widget);
	gdk_pixbuf_unref (pixbuf);
	g_free (filename);

	gtk_widget_pop_visual ();
	gtk_widget_pop_colormap ();

	return applier->p->preview_widget;
}

GdkPixbuf *
applier_get_wallpaper_pixbuf (Applier *applier)
{
	g_return_val_if_fail (applier != NULL, NULL);
	g_return_val_if_fail (IS_APPLIER (applier), NULL);

	return applier->p->wallpaper_pixbuf;
}

static void
draw_disabled_message (GtkWidget *widget)
{
	GdkPixmap      *pixmap;
	GdkColor        color;
	PangoLayout    *layout;
	PangoRectangle  extents;
	GdkGC          *gc;
	gint            x, y, w, h;
	gint            height, width;
	const char     *disabled_string = _("Disabled");

	g_return_if_fail (widget != NULL);
	g_return_if_fail (GTK_IS_PIXMAP (widget));

	w = MONITOR_CONTENTS_WIDTH;
	h = MONITOR_CONTENTS_HEIGHT;
	x = MONITOR_CONTENTS_X;
	y = MONITOR_CONTENTS_Y;

	if (!GTK_WIDGET_REALIZED (widget))
		gtk_widget_realize (widget);

	pixmap = GTK_PIXMAP (widget)->pixmap;

	gc = gdk_gc_new (widget->window);

	gdk_color_black (gtk_widget_get_colormap (widget), &color);
	gdk_gc_set_foreground (gc, &color);
	gdk_draw_rectangle (pixmap, gc, TRUE, x, y, w, h);

	layout = gtk_widget_create_pango_layout (widget, disabled_string);
	pango_layout_get_pixel_extents (layout, &extents, NULL);

	gdk_color_white (gtk_widget_get_colormap (widget), &color);
	gdk_gc_set_foreground (gc, &color);

	gdk_draw_layout (widget->window,
			 gc,
			 x + (w - extents.width) / 2,
			 y + (h - extents.height) / 2 + extents.height / 2,
			 layout);

	gdk_gc_unref (gc);
	g_object_unref (G_OBJECT (layout));
}

static void
run_render_pipeline (Applier *applier, const BGPreferences *prefs)
{
	g_return_if_fail (applier != NULL);
	g_return_if_fail (IS_APPLIER (applier));
	g_return_if_fail (prefs != NULL);
	g_return_if_fail (IS_BG_PREFERENCES (prefs));

	g_assert (applier->p->pixbuf == NULL);

	/* Initialize applier->p->render_geom */
	applier->p->pixbuf_render_geom.x = applier->p->render_geom.x;
	applier->p->pixbuf_render_geom.y = applier->p->render_geom.y;
	applier->p->pixbuf_render_geom.width = applier->p->render_geom.width;
	applier->p->pixbuf_render_geom.height = applier->p->render_geom.height;
	applier->p->pixbuf_xlate.x = 0;
	applier->p->pixbuf_xlate.y = 0;

	render_background (applier, prefs);

	if (need_root_pixmap_p (applier, prefs))
		create_pixmap (applier, prefs);

	render_wallpaper (applier, prefs);
	render_to_screen (applier, prefs);

	if (applier->p->pixbuf != NULL) {
		gdk_pixbuf_unref (applier->p->pixbuf);
		applier->p->pixbuf = NULL;
	}
}

/* Create the gradient image if necessary and put it into a fresh pixbuf
 *
 * Preconditions:
 *   1. prefs is valid
 *   2. The old applier->p->pixbuf, if it existed, has been destroyed
 *
 * Postconditions (assuming gradient is enabled):
 *   1. applier->p->pixbuf contains a newly rendered gradient
 */

static void
render_background (Applier *applier, const BGPreferences *prefs) 
{
	g_return_if_fail (applier != NULL);
	g_return_if_fail (IS_APPLIER (applier));
	g_return_if_fail (prefs != NULL);
	g_return_if_fail (IS_BG_PREFERENCES (prefs));

	if (prefs->gradient_enabled && !wallpaper_full_cover_p (applier, prefs)) {
		applier->p->grad_geom.x = applier->p->render_geom.width;
		applier->p->grad_geom.y = applier->p->render_geom.height;

		if (applier->p->type == APPLIER_ROOT && !prefs->wallpaper_enabled) {
			if (prefs->orientation == ORIENTATION_HORIZ)
				applier->p->grad_geom.y = 32;
			else
				applier->p->grad_geom.x = 32;
		}

		applier->p->pixbuf = 
			gdk_pixbuf_new (GDK_COLORSPACE_RGB, 
					FALSE, 8, 
					applier->p->grad_geom.x, 
					applier->p->grad_geom.y);

		fill_gradient (applier->p->pixbuf,
			       prefs->color1, prefs->color2, 
			       prefs->orientation);

		applier->p->pixbuf_render_geom.width = applier->p->grad_geom.x;
		applier->p->pixbuf_render_geom.height = applier->p->grad_geom.y;
	}
}

/* Render the wallpaper onto the pixbuf-in-progress.
 *
 * Preconditions:
 *   1. The wallpaper pixbuf has been loaded and is in
 *      applier->p->wallpaper_pixbuf.
 *   2. The structure applier->p->render_geom is filled out properly as
 *      described in the documentation above (this should be invariant).
 *   3. The various fields in prefs are valid
 *
 * Postconditions (assuming wallpaper is enabled):
 *   1. applier->p->pixbuf contains the pixbuf-in-progress with the wallpaper
 *      correctly rendered.
 *   2. applier->p->pixbuf_render_geom has been modified, if necessary,
 *      according to the requirements of the wallpaper; it should be set by
 *      default to be the same as applier->p->render_geom.
 */

static void
render_wallpaper (Applier *applier, const BGPreferences *prefs) 
{
	GdkRectangle  src_geom;
	GdkRectangle  dest_geom;
	GdkRectangle  virtual_geom;
	GdkPixbuf    *prescaled_pixbuf = NULL;
	guint         alpha;
	gint          tmp1, tmp2;
	gint          pwidth, pheight;

	g_return_if_fail (applier != NULL);
	g_return_if_fail (IS_APPLIER (applier));
	g_return_if_fail (prefs != NULL);
	g_return_if_fail (IS_BG_PREFERENCES (prefs));

	if (prefs->wallpaper_enabled) {
		if (applier->p->wallpaper_pixbuf == NULL)
			return;

		gdk_window_get_size (GDK_ROOT_PARENT (), &tmp1, &tmp2);
		virtual_geom.x = virtual_geom.y = 0;
		virtual_geom.width = tmp1;
		virtual_geom.height = tmp2;

		pwidth = gdk_pixbuf_get_width (applier->p->wallpaper_pixbuf);
		pheight = gdk_pixbuf_get_height (applier->p->wallpaper_pixbuf);

		get_geometry (prefs->wallpaper_type,
			      applier->p->wallpaper_pixbuf,
			      &(applier->p->render_geom),
			      &virtual_geom, &dest_geom, &src_geom);

		/* Modify applier->p->pixbuf_render_geom if necessary */
		if (applier->p->pixbuf == NULL) {   /* This means we didn't render a gradient */
			applier->p->pixbuf_render_geom.x = dest_geom.x + applier->p->render_geom.x;
			applier->p->pixbuf_render_geom.y = dest_geom.y + applier->p->render_geom.y;
			applier->p->pixbuf_render_geom.width = dest_geom.width;
			applier->p->pixbuf_render_geom.height = dest_geom.height;
		}

		if (prefs->wallpaper_type == WPTYPE_TILED) {
			if (dest_geom.width != pwidth || dest_geom.height != pheight) {
				prescaled_pixbuf = gdk_pixbuf_scale_simple
					(applier->p->wallpaper_pixbuf,
					 pwidth * applier->p->render_geom.width / virtual_geom.width,
					 pheight * applier->p->render_geom.height / virtual_geom.height,
					 GDK_INTERP_BILINEAR);
			} else {
				prescaled_pixbuf = applier->p->wallpaper_pixbuf;
				gdk_pixbuf_ref (prescaled_pixbuf);
			}
		}

		if (prefs->adjust_opacity) {
			alpha = 2.56 * prefs->opacity;
			alpha = alpha * alpha / 256;
			alpha = CLAMP (alpha, 0, 255);
		} else {
			alpha = 255;
		}

		if (prefs->wallpaper_type == WPTYPE_TILED)
			applier->p->pixbuf = tile_pixbuf (applier->p->pixbuf,
							  prescaled_pixbuf,
							  &(applier->p->render_geom),
							  alpha, prefs->color1);
		else
			applier->p->pixbuf = place_pixbuf (applier->p->pixbuf,
							   applier->p->wallpaper_pixbuf,
							   &dest_geom, &src_geom,
							   alpha, prefs->color1);

		if (applier->p->pixbuf == applier->p->wallpaper_pixbuf) {
			applier->p->pixbuf_xlate.x = src_geom.x;
			applier->p->pixbuf_xlate.y = src_geom.y;
		}

		if (prescaled_pixbuf != NULL)
			gdk_pixbuf_unref (prescaled_pixbuf);
	}
}

/* Take whatever we have rendered and transfer it to the display.
 *
 * Preconditions:
 *   1. We have already rendered the gradient and wallpaper, and
 *      applier->p->pixbuf is a valid GdkPixbuf containing that rendered data.
 *   2. The structure applier->p->pixbuf_render_geom contains the coordonites on
 *      the destination visual to which we should render the contents of
 *      applier->p->pixbuf
 *   3. The structure applier->p->render_geom contains the total area that the
 *      background should cover (i.e. the whole desktop if we are rendering to
 *      the root window, or the region inside the monitor if we are rendering to
 *      the preview).
 *   4. The strucutre prefs->color1 contains the background color to be used on
 *      areas not covered by the above pixbuf.
 */

static void
render_to_screen (Applier *applier, const BGPreferences *prefs) 
{
	GdkGC *gc;

	g_return_if_fail (applier != NULL);
	g_return_if_fail (IS_APPLIER (applier));
	g_return_if_fail (prefs != NULL);
	g_return_if_fail (IS_BG_PREFERENCES (prefs));

	gc = gdk_gc_new (GDK_ROOT_PARENT ());

	if (applier->p->pixbuf != NULL) {
		if (applier->p->pixbuf_render_geom.x != 0 ||
		    applier->p->pixbuf_render_geom.y != 0 ||
		    applier->p->pixbuf_render_geom.width != applier->p->render_geom.width ||
		    applier->p->pixbuf_render_geom.height != applier->p->render_geom.height)
		{
			gdk_color_alloc (gdk_window_get_colormap (GDK_ROOT_PARENT ()), prefs->color1);
			gdk_gc_set_foreground (gc, prefs->color1);
			gdk_draw_rectangle (applier->p->pixmap, gc, TRUE,
					    applier->p->render_geom.x,
					    applier->p->render_geom.y, 
					    applier->p->render_geom.width,
					    applier->p->render_geom.height);
		}

		gdk_pixbuf_render_to_drawable
			(applier->p->pixbuf,
			 applier->p->pixmap, gc,
			 applier->p->pixbuf_xlate.x,
			 applier->p->pixbuf_xlate.y,
			 applier->p->pixbuf_render_geom.x,
			 applier->p->pixbuf_render_geom.y,
			 applier->p->pixbuf_render_geom.width,
			 applier->p->pixbuf_render_geom.height,
			 GDK_RGB_DITHER_MAX, 0, 0);
	} else {
		if (applier->p->type == APPLIER_ROOT) {
			gdk_color_alloc (gdk_window_get_colormap (GDK_ROOT_PARENT()), prefs->color1);
			gdk_window_set_background (GDK_ROOT_PARENT (), prefs->color1);
			gdk_window_clear (GDK_ROOT_PARENT ());
		}
		else if (applier->p->type == APPLIER_PREVIEW) {
			gdk_color_alloc (gdk_window_get_colormap (applier->p->preview_widget->window), prefs->color1);
			gdk_gc_set_foreground (gc, prefs->color1);
			gdk_draw_rectangle (applier->p->pixmap, gc, TRUE,
					    applier->p->render_geom.x,
					    applier->p->render_geom.y, 
					    applier->p->render_geom.width,
					    applier->p->render_geom.height);
		}
	}

	if (applier->p->type == APPLIER_ROOT && !applier->p->pixmap_is_set &&
	    (prefs->wallpaper_enabled || prefs->gradient_enabled))
		set_root_pixmap (applier->p->pixmap);
	else if (applier->p->type == APPLIER_ROOT && !applier->p->pixmap_is_set)
		set_root_pixmap (NULL);

	gdk_gc_destroy (gc);
}

/* Create a pixmap that will replace the current root pixmap. This function has
 * no effect if the applier is for the preview window
 */

static void
create_pixmap (Applier *applier, const BGPreferences *prefs) 
{
	gint width, height;

	g_return_if_fail (applier != NULL);
	g_return_if_fail (IS_APPLIER (applier));
	g_return_if_fail (prefs != NULL);
	g_return_if_fail (IS_BG_PREFERENCES (prefs));

	switch (applier->p->type) {
	case APPLIER_ROOT:
		if (prefs->gradient_enabled && !prefs->wallpaper_enabled) {
			width = applier->p->grad_geom.x;
			height = applier->p->grad_geom.y;
		} else {
			width = applier->p->render_geom.width;
			height = applier->p->render_geom.height;
		}

		applier->p->pixmap = make_root_pixmap (width, height);
		applier->p->pixmap_is_set = FALSE;
		break;

	case APPLIER_PREVIEW:
		applier_get_preview_widget (applier);

		if (!GTK_WIDGET_REALIZED (applier->p->preview_widget))
			gtk_widget_realize (applier->p->preview_widget);

		applier->p->pixmap = GTK_PIXMAP (applier->p->preview_widget)->pixmap;
		applier->p->pixmap_is_set = TRUE;
		break;
	}
}

/* Compute geometry information based on the wallpaper type. In particular,
 * determine where on the destination visual the wallpaper should be rendered
 * and where the data from the source pixbuf the image should be fetched.
 */

static void
get_geometry (wallpaper_type_t  wallpaper_type,
	      GdkPixbuf        *pixbuf,
	      GdkRectangle     *field_geom,
	      GdkRectangle     *virtual_geom,
	      GdkRectangle     *dest_geom,
	      GdkRectangle     *src_geom)
{
	gdouble asp, xfactor, yfactor;
	gint pwidth, pheight;
	gint st = 0;

	if (field_geom->width != virtual_geom->width)
		xfactor = (gdouble) field_geom->width / (gdouble) virtual_geom->width;
	else
		xfactor = 1.0;

	if (field_geom->height != virtual_geom->height)
		yfactor = (gdouble) field_geom->height / (gdouble) virtual_geom->height;
	else
		yfactor = 1.0;

	pwidth = gdk_pixbuf_get_width (pixbuf);
	pheight = gdk_pixbuf_get_height (pixbuf);

	switch (wallpaper_type) {
	case WPTYPE_TILED:
		src_geom->x = src_geom->y = 0;
		dest_geom->x = dest_geom->y = 0;

		src_geom->width = pwidth;
		src_geom->height = pheight;

		dest_geom->width = field_geom->width;
		dest_geom->height = field_geom->height;

		break;

	case WPTYPE_CENTERED:
		if (virtual_geom->width < pwidth) {
			src_geom->width = virtual_geom->width;
			src_geom->x = (pwidth - virtual_geom->width) / 2;
			dest_geom->width = field_geom->width;
			dest_geom->x = 0;
		} else {
			src_geom->width = pwidth;
			src_geom->x = 0;
			dest_geom->width = MIN ((gdouble) src_geom->width * xfactor, field_geom->width);
			dest_geom->x = (field_geom->width - dest_geom->width) / 2;
		}

		if (virtual_geom->height < pheight) {
			src_geom->height = virtual_geom->height;
			src_geom->y = (pheight - virtual_geom->height) / 2;
			dest_geom->height = field_geom->height;
			dest_geom->y = 0;
		} else {
			src_geom->height = pheight;
			src_geom->y = 0;
			dest_geom->height = MIN ((gdouble) src_geom->height * yfactor, field_geom->height);
			dest_geom->y = (field_geom->height - dest_geom->height) / 2;
		}

		break;

	case WPTYPE_SCALED:
		asp = (gdouble) pwidth / (gdouble) virtual_geom->width;

		if (asp < (gdouble) pheight / virtual_geom->height) {
			asp = (gdouble) pheight / (gdouble) virtual_geom->height;
			st = 1;
		}

		if (st) {
			dest_geom->width = pwidth / asp * xfactor;
			dest_geom->height = field_geom->height;
			dest_geom->x = (field_geom->width - dest_geom->width) / 2;
			dest_geom->y = 0;
		} else {
			dest_geom->height = pheight / asp * yfactor;
			dest_geom->width = field_geom->width;
			dest_geom->x = 0;
			dest_geom->y = (field_geom->height - dest_geom->height) / 2;
		}

		src_geom->x = src_geom->y = 0;
		src_geom->width = pwidth;
		src_geom->height = pheight;

		break;

	case WPTYPE_STRETCHED:
		dest_geom->width = field_geom->width;
		dest_geom->height = field_geom->height;
		dest_geom->x = 0;
		dest_geom->y = 0;
		src_geom->x = src_geom->y = 0;
		src_geom->width = pwidth;
		src_geom->height = pheight;
		break;

	default:
		g_error ("Bad wallpaper type");
		break;
	}
}

/* Place one pixbuf onto another, compositing and scaling as necessary */

static GdkPixbuf *
place_pixbuf (GdkPixbuf *dest_pixbuf,
	      GdkPixbuf *src_pixbuf,
	      GdkRectangle *dest_geom,
	      GdkRectangle *src_geom,
	      guint alpha,
	      GdkColor *bg_color) 
{
	gboolean need_composite;
	gboolean need_scaling;
	gdouble scale_x, scale_y;
	gint real_dest_x, real_dest_y;
	guint colorv;

	need_composite = (alpha < 255 || gdk_pixbuf_get_has_alpha (src_pixbuf));
	need_scaling = ((dest_geom->width != src_geom->width) || (dest_geom->height != src_geom->height));

	if (need_scaling) {
		scale_x = (gdouble) dest_geom->width / (gdouble) src_geom->width;
		scale_y = (gdouble) dest_geom->height / (gdouble) src_geom->height;
	} else {
		scale_x = scale_y = 1.0;
	}

	if (need_composite && dest_pixbuf != NULL) {
		gdk_pixbuf_composite
			(src_pixbuf, dest_pixbuf,
			 dest_geom->x, dest_geom->y,
			 dest_geom->width, 
			 dest_geom->height,
			 dest_geom->x - src_geom->x * scale_x,
			 dest_geom->y - src_geom->y * scale_y,
			 scale_x, scale_y,
			 GDK_INTERP_BILINEAR,
			 alpha);
	}
	else if (need_composite) {
		dest_pixbuf = gdk_pixbuf_new
			(GDK_COLORSPACE_RGB, FALSE, 8,
			 dest_geom->width, dest_geom->height);

		colorv = ((bg_color->red & 0xff00) << 8) |
			(bg_color->green & 0xff00) |
			((bg_color->blue & 0xff00) >> 8);

		gdk_pixbuf_composite_color 
			(src_pixbuf, dest_pixbuf,
			 0, 0,
			 dest_geom->width, 
			 dest_geom->height,
			 -src_geom->x * scale_x,
			 -src_geom->y * scale_y,
			 scale_x, scale_y,
			 GDK_INTERP_BILINEAR,
			 alpha, 0, 0, 65536,
			 colorv, colorv);
	}
	else if (need_scaling) {
		if (dest_pixbuf == NULL) {
			dest_pixbuf = gdk_pixbuf_new
				(GDK_COLORSPACE_RGB, FALSE, 8,
				 dest_geom->width, dest_geom->height);
			real_dest_x = real_dest_y = 0;
		} else {
			real_dest_x = dest_geom->x;
			real_dest_y = dest_geom->y;
		}

		gdk_pixbuf_scale 
			(src_pixbuf, dest_pixbuf,
			 real_dest_x, real_dest_y,
			 dest_geom->width,
			 dest_geom->height,
			 real_dest_x - src_geom->x * scale_x,
			 real_dest_y - src_geom->y * scale_y,
			 scale_x, scale_y,
			 GDK_INTERP_BILINEAR);
	}
	else if (dest_pixbuf != NULL) {
		gdk_pixbuf_copy_area
			(src_pixbuf,
			 src_geom->x, src_geom->y, 
			 src_geom->width,
			 src_geom->height,
			 dest_pixbuf,
			 dest_geom->x, dest_geom->y);
	} else {
		dest_pixbuf = src_pixbuf;
		gdk_pixbuf_ref (dest_pixbuf);
	}

	return dest_pixbuf;
}

/* Tile one pixbuf repeatedly onto another, compositing as necessary. Assumes
 * that the source pixbuf has already been scaled properly
 */

static GdkPixbuf *
tile_pixbuf (GdkPixbuf *dest_pixbuf,
	     GdkPixbuf *src_pixbuf,
	     GdkRectangle *field_geom,
	     guint alpha,
	     GdkColor *bg_color) 
{
	gboolean need_composite;
	gboolean use_simple;
	gdouble cx, cy;
	gdouble colorv;
	gint pwidth, pheight;

	need_composite = (alpha < 255 || gdk_pixbuf_get_has_alpha (src_pixbuf));
	use_simple = (dest_pixbuf == NULL);

	if (dest_pixbuf == NULL)
		dest_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, field_geom->width, field_geom->height);

	if (need_composite && use_simple)
		colorv = ((bg_color->red & 0xff00) << 8) |
			(bg_color->green & 0xff00) |
			((bg_color->blue & 0xff00) >> 8);
	else
		colorv = 0;

	pwidth = gdk_pixbuf_get_width (src_pixbuf);
	pheight = gdk_pixbuf_get_height (src_pixbuf);

	for (cy = 0; cy < field_geom->height; cy += pheight) {
		for (cx = 0; cx < field_geom->width; cx += pwidth) {
			if (need_composite && !use_simple)
				gdk_pixbuf_composite
					(src_pixbuf, dest_pixbuf,
					 cx, cy,
					 MIN (pwidth, field_geom->width - cx), 
					 MIN (pheight, field_geom->height - cy),
					 cx, cy,
					 1.0, 1.0,
					 GDK_INTERP_BILINEAR,
					 alpha);
			else if (need_composite && use_simple)
				gdk_pixbuf_composite_color
					(src_pixbuf, dest_pixbuf,
					 cx, cy,
					 MIN (pwidth, field_geom->width - cx), 
					 MIN (pheight, field_geom->height - cy),
					 cx, cy,
					 1.0, 1.0,
					 GDK_INTERP_BILINEAR,
					 alpha,
					 65536, 65536, 65536,
					 colorv, colorv);
			else
				gdk_pixbuf_copy_area
					(src_pixbuf,
					 0, 0,
					 MIN (pwidth, field_geom->width - cx),
					 MIN (pheight, field_geom->height - cy),
					 dest_pixbuf,
					 cx, cy);
		}
	}

	return dest_pixbuf;
}

/* Fill a raw character array with gradient data; the data may then be imported
 * into a GdkPixbuf
 */

static void
fill_gradient (GdkPixbuf     *pixbuf,
	       GdkColor      *c1,
	       GdkColor      *c2,
	       orientation_t  orientation)
{
	int i, j;
	int dr, dg, db;
	int gs1;
	int vc = ((orientation == ORIENTATION_HORIZ) || (c1 == c2));
	int w = gdk_pixbuf_get_width (pixbuf);
	int h = gdk_pixbuf_get_height (pixbuf);
	guchar *b, *row;
	guchar *d = gdk_pixbuf_get_pixels (pixbuf);
	int rowstride = gdk_pixbuf_get_rowstride (pixbuf);

#define R1 c1->red
#define G1 c1->green
#define B1 c1->blue
#define R2 c2->red
#define G2 c2->green
#define B2 c2->blue

	dr = R2 - R1;
	dg = G2 - G1;
	db = B2 - B1;

	gs1 = (orientation == ORIENTATION_VERT) ? h-1 : w-1;

	row = g_new (unsigned char, rowstride);

	if (vc) {
		b = row;
		for (j = 0; j < w; j++) {
			*b++ = (R1 + (j * dr) / gs1) >> 8;
			*b++ = (G1 + (j * dg) / gs1) >> 8;
			*b++ = (B1 + (j * db) / gs1) >> 8;
		}
	}

	for (i = 0; i < h; i++) {
		if (!vc) {
			unsigned char cr, cg, cb;
			cr = (R1 + (i * dr) / gs1) >> 8;
			cg = (G1 + (i * dg) / gs1) >> 8;
			cb = (B1 + (i * db) / gs1) >> 8;
			b = row;
			for (j = 0; j < w; j++) {
				*b++ = cr;
				*b++ = cg;
				*b++ = cb;
			}
		}
		memcpy (d, row, w * 3);
		d += rowstride;
	}

#undef R1
#undef G1
#undef B1
#undef R2
#undef G2
#undef B2

	g_free (row);
}

/* Boolean predicates to assist optimization and rendering */

/* Return TRUE iff the wallpaper filename or enabled settings have changed
 * between old_prefs and new_prefs
 */

static gboolean
need_wallpaper_load_p (const Applier *applier, const BGPreferences *prefs)
{
	if (applier->p->last_prefs == NULL)
		return TRUE;
	else if (applier->p->last_prefs->wallpaper_enabled != prefs->wallpaper_enabled)
		return TRUE;
	else if (!applier->p->last_prefs->wallpaper_enabled && !prefs->wallpaper_enabled)
		return FALSE;
	else if (strcmp (applier->p->last_prefs->wallpaper_filename, prefs->wallpaper_filename))
		return TRUE;
	else
		return FALSE;
}

/* Return TRUE iff we need to create a new root pixmap */

static gboolean
need_root_pixmap_p (const Applier *applier, const BGPreferences *prefs) 
{
	if (applier->p->last_prefs == NULL)
		return TRUE;
	else if (render_small_pixmap_p (applier->p->last_prefs) != render_small_pixmap_p (prefs))
		return TRUE;
	else if (!render_small_pixmap_p (applier->p->last_prefs) &&
		 !render_small_pixmap_p (prefs))
		return FALSE;
	else if (applier->p->last_prefs->orientation != prefs->orientation)
		return TRUE;
	else
		return FALSE;
}

/* Return TRUE iff the colors are equal */

/* Return TRUE iff the wallpaper completely covers the colors in the given
 * bg_preferences structure, assuming we have already loaded the wallpaper pixbuf */

static gboolean
wallpaper_full_cover_p (const Applier *applier, const BGPreferences *prefs) 
{
	gint swidth, sheight;
	gint pwidth, pheight;
	gdouble asp1, asp2;

	/* We can't make this determination until the wallpaper is loaded, if
	 * wallpaper is enabled */
	g_return_val_if_fail (!prefs->wallpaper_enabled || applier->p->wallpaper_pixbuf != NULL, TRUE);

	if (!prefs->wallpaper_enabled)
		return FALSE;
	else if (gdk_pixbuf_get_has_alpha (applier->p->wallpaper_pixbuf))
		return FALSE;
	else if (prefs->wallpaper_type == WPTYPE_TILED)
		return TRUE;
	else if (prefs->wallpaper_type == WPTYPE_STRETCHED)
		return TRUE;

	gdk_window_get_size (GDK_ROOT_PARENT (), &swidth, &sheight);
	pwidth = gdk_pixbuf_get_width (applier->p->wallpaper_pixbuf);
	pheight = gdk_pixbuf_get_height (applier->p->wallpaper_pixbuf);

	if (prefs->wallpaper_type == WPTYPE_CENTERED) {
		if (pwidth >= swidth && pheight >= sheight)
			return TRUE;
		else
			return FALSE;
	}
	else if (prefs->wallpaper_type == WPTYPE_SCALED) {
		asp1 = (gdouble) swidth / (gdouble) sheight;
		asp2 = (gdouble) pwidth / (gdouble) pheight;

		if (swidth * (asp1 - asp2) < 1 && swidth * (asp2 - asp1) < 1)
			return TRUE;
		else
			return FALSE;
	}

	return FALSE;
}

/* Return TRUE if we can optimize the rendering by using a small thin pixmap */

static gboolean
render_small_pixmap_p (const BGPreferences *prefs) 
{
	return prefs->gradient_enabled && !prefs->wallpaper_enabled;
}

/* Create a persistant pixmap. We create a separate display
 * and set the closedown mode on it to RetainPermanent
 */
static GdkPixmap *
make_root_pixmap (gint width, gint height)
{
	Display *display;
	Pixmap xpixmap;
	
	display = XOpenDisplay (g_getenv ("DISPLAY"));
	XSetCloseDownMode (display, RetainPermanent);

	xpixmap = XCreatePixmap (display,
				 DefaultRootWindow (display),
				 width, height,
				 DefaultDepthOfScreen (DefaultScreenOfDisplay (GDK_DISPLAY ())));

	XCloseDisplay (display);

	return gdk_pixmap_foreign_new (xpixmap);
}

/* Set the root pixmap, and properties pointing to it. We
 * do this atomically with XGrabServer to make sure that
 * we won't leak the pixmap if somebody else it setting
 * it at the same time. (This assumes that they follow the
 * same conventions we do)
 */

static void 
set_root_pixmap (GdkPixmap *pixmap) 
{
	Atom type;
	gulong nitems, bytes_after;
	gint format;
	guchar *data_esetroot;
	Pixmap pixmap_id;

	if (pixmap != NULL && pixmap != (GdkPixmap *) -1)
		pixmap_id = GDK_WINDOW_XWINDOW (pixmap);
	else
		pixmap_id = 0;

	XGrabServer (GDK_DISPLAY ());

	XGetWindowProperty (GDK_DISPLAY (), GDK_ROOT_WINDOW (),
			    XInternAtom (GDK_DISPLAY (), "ESETROOT_PMAP_ID", False),
			    0L, 1L, False, XA_PIXMAP,
			    &type, &format, &nitems, &bytes_after,
			    &data_esetroot);

	if (type == XA_PIXMAP) {
		if (format == 32 && nitems == 1) {
			Pixmap old_pixmap;

			old_pixmap = *((Pixmap *) data_esetroot);

			if (pixmap != (GdkPixmap *) -1 && old_pixmap != pixmap_id)
				XKillClient (GDK_DISPLAY (), old_pixmap);
			else if (pixmap == (GdkPixmap *) -1)
				pixmap_id = old_pixmap;
		}

		XFree (data_esetroot);
	}

	if (pixmap != NULL && pixmap != (GdkPixmap *) -1) {
		XChangeProperty (GDK_DISPLAY (), GDK_ROOT_WINDOW (),
				 XInternAtom (GDK_DISPLAY (), "ESETROOT_PMAP_ID", FALSE),
				 XA_PIXMAP, 32, PropModeReplace,
				 (guchar *) &pixmap_id, 1);
		XChangeProperty (GDK_DISPLAY (), GDK_ROOT_WINDOW (),
				 XInternAtom (GDK_DISPLAY (), "_XROOTPMAP_ID", FALSE),
				 XA_PIXMAP, 32, PropModeReplace,
				 (guchar *) &pixmap_id, 1);

		XSetWindowBackgroundPixmap (GDK_DISPLAY (), GDK_ROOT_WINDOW (),
					    pixmap_id);
	} else if (pixmap == NULL) {
		XDeleteProperty (GDK_DISPLAY (), GDK_ROOT_WINDOW (),
				 XInternAtom (GDK_DISPLAY (), "ESETROOT_PMAP_ID", FALSE));
		XDeleteProperty (GDK_DISPLAY (), GDK_ROOT_WINDOW (),
				 XInternAtom (GDK_DISPLAY (), "_XROOTPMAP_ID", FALSE));
	}

	XClearWindow (GDK_DISPLAY (), GDK_ROOT_WINDOW ());
	XUngrabServer (GDK_DISPLAY ());
	XFlush (GDK_DISPLAY ());
}

static gboolean
is_nautilus_running (void)
{
	Atom window_id_atom;
	Window nautilus_xid;
	Atom actual_type;
	int actual_format;
	unsigned long nitems, bytes_after;
	unsigned char *data;
	int retval;
	Atom wmclass_atom;
	gboolean running;
	gint error;

	window_id_atom = XInternAtom (GDK_DISPLAY (), 
				      "NAUTILUS_DESKTOP_WINDOW_ID", True);

	if (window_id_atom == None) return FALSE;

	retval = XGetWindowProperty (GDK_DISPLAY (), GDK_ROOT_WINDOW (),
				     window_id_atom, 0, 1, False, XA_WINDOW,
				     &actual_type, &actual_format, &nitems,
				     &bytes_after, &data);

	if (data != NULL) {
		nautilus_xid = *(Window *) data;
		XFree (data);
	} else {
		return FALSE;
	}

	if (actual_type != XA_WINDOW) return FALSE;
	if (actual_format != 32) return FALSE;

	wmclass_atom = XInternAtom (GDK_DISPLAY (), "WM_CLASS", False);

	gdk_error_trap_push ();

	retval = XGetWindowProperty (GDK_DISPLAY (), nautilus_xid,
				     wmclass_atom, 0, 24, False, XA_STRING,
				     &actual_type, &actual_format, &nitems,
				     &bytes_after, &data);

	error = gdk_error_trap_pop ();

	if (error == BadWindow) return FALSE;

	if (actual_type == XA_STRING &&
	    nitems == 24 &&
	    bytes_after == 0 &&
	    actual_format == 8 &&
	    data != NULL &&
	    !strcmp (data, "desktop_window") &&
	    !strcmp (data + strlen (data) + 1, "Nautilus"))
		running = TRUE;
	else
		running = FALSE;

	if (data != NULL)
		XFree (data);

	return running;
}