summaryrefslogtreecommitdiff
path: root/plugins/background/msd-background-manager-wayland.c
blob: b4ab88d9961261ebfd0f7c2ad0f548d1e965aa70 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2026 MATE Developers
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "config.h"

#include <glib.h>
#include <glib/gi18n.h>
#include <gdk/gdk.h>
#include <gdk/gdkwayland.h>
#include <gtk/gtk.h>
#include <wayland-client.h>

#include <gtk-layer-shell.h>

#define MATE_DESKTOP_USE_UNSTABLE_API
#include <libmate-desktop/mate-bg.h>

#include "msd-background-manager-wayland.h"

struct _MsdBackgroundManagerWayland
{
	GdkDisplay   *display;
	GSettings    *settings;
	MateBG       *bg;
	GPtrArray    *monitor_windows;
	gboolean      caja_desktop_active;
};

typedef struct
{
	GtkWidget                 *window;
	GdkMonitor                *monitor;
	MsdBackgroundManagerWayland *manager;
	cairo_surface_t           *surface;
	cairo_surface_t           *fading_surface;
	cairo_surface_t           *end_surface;
	guint                      fade_timeout_id;
	gdouble                    fade_start_time;
	gdouble                    fade_total_duration;
	gboolean                   fade_is_first_frame;
	int                        width;
	int                        height;
} MonitorWindow;

static void
free_monitor_window_surface (MonitorWindow *mw)
{
	if (mw->surface != NULL) {
		cairo_surface_destroy (mw->surface);
		mw->surface = NULL;
	}
}

static gdouble
get_current_time (void)
{
	return g_get_monotonic_time () / 1000000.0;
}

/* Check the GSettings key that indicates whether Caja is configured to
 * draw desktop icons. When this is enabled Caja draws the desktop itself
 * so we must not create our own layer-shell background windows.
 */
static gboolean
caja_desktop_is_showing (MsdBackgroundManagerWayland *manager)
{
	return g_settings_get_boolean (manager->settings, MATE_BG_KEY_SHOW_DESKTOP);
}

static gboolean
animations_are_disabled (MonitorWindow *mw G_GNUC_UNUSED)
{
	GtkSettings *settings;
	gboolean    enabled;

	settings = gtk_settings_get_default ();
	if (settings == NULL)
		return TRUE;

	g_object_get (settings, "gtk-enable-animations", &enabled, NULL);

	return !enabled;
}

static void
cancel_fade (MonitorWindow *mw)
{
	if (mw->fade_timeout_id == 0)
		return;

	g_source_remove (mw->fade_timeout_id);
	mw->fade_timeout_id = 0;
}

static void
on_fade_finished (MonitorWindow *mw)
{
	cairo_t *cr;

	mw->fade_timeout_id = 0;

	if (mw->fading_surface == NULL || mw->end_surface == NULL) {
		if (mw->end_surface != NULL) {
			cairo_surface_destroy (mw->end_surface);
			mw->end_surface = NULL;
		}
		return;
	}

	cr = cairo_create (mw->fading_surface);
	cairo_set_source_surface (cr, mw->end_surface, 0, 0);
	cairo_paint (cr);
	cairo_destroy (cr);

	cairo_surface_destroy (mw->end_surface);
	mw->end_surface = NULL;

	free_monitor_window_surface (mw);
	mw->surface = mw->fading_surface;
	mw->fading_surface = NULL;

	gtk_widget_queue_draw (mw->window);
}

static gboolean
on_fade_tick (MonitorWindow *mw)
{
	gdouble         now, percent_done;
	cairo_t        *cr;
	cairo_status_t  status;

	if (mw->fading_surface == NULL || mw->end_surface == NULL)
		return FALSE;

	now = get_current_time ();
	percent_done = (now - mw->fade_start_time) / mw->fade_total_duration;
	percent_done = CLAMP (percent_done, 0.0, 1.0);

	/* If it's taking a long time to get to the first frame,
	 * then lengthen the duration, so the user will get to see
	 * the effect.
	 */
	if (mw->fade_is_first_frame && percent_done > .33) {
		mw->fade_is_first_frame = FALSE;
		mw->fade_total_duration *= 1.5;
		return TRUE;
	}

	if (animations_are_disabled (mw))
		return FALSE;

	/* Accumulate the end surface in place, so the fade is
	 * exponential rather than linear (looks good, matches X11).
	 */
	cr = cairo_create (mw->fading_surface);
	cairo_set_source_surface (cr, mw->end_surface, 0, 0);
	cairo_paint_with_alpha (cr, percent_done);
	status = cairo_status (cr);
	cairo_destroy (cr);

	if (status == CAIRO_STATUS_SUCCESS)
		gtk_widget_queue_draw (mw->window);

	return percent_done <= .99;
}

static void
set_surface_instantly (MonitorWindow *mw, cairo_surface_t *new_surface)
{
	if (mw->fade_timeout_id != 0) {
		if (mw->fading_surface != NULL) {
			cairo_surface_destroy (mw->fading_surface);
			mw->fading_surface = NULL;
		}
		if (mw->end_surface != NULL) {
			cairo_surface_destroy (mw->end_surface);
			mw->end_surface = NULL;
		}
		g_source_remove (mw->fade_timeout_id);
		mw->fade_timeout_id = 0;
	}
	free_monitor_window_surface (mw);
	mw->surface = new_surface;
	gtk_widget_queue_draw (mw->window);
}

static void
start_or_continue_fade (MsdBackgroundManagerWayland *manager,
			MonitorWindow               *mw,
			cairo_surface_t             *new_surface)
{
	gboolean fade_enabled;
	gboolean animations_enabled;
	cairo_t *cr;
	GSource *source;

	fade_enabled = g_settings_get_boolean (manager->settings,
					       MATE_BG_KEY_BACKGROUND_FADE);
	animations_enabled = !animations_are_disabled (mw);

	if (!fade_enabled || !animations_enabled || mw->surface == NULL) {
		/* No fade: swap instantly. This also covers the first render
		 * after startup.
		 */
		set_surface_instantly (mw, new_surface);
		return;
	}

	/* If a fade is already running, restart from the current blended
	 * state (its completion commits the blend into mw->surface).
	 */
	cancel_fade (mw);

	mw->fading_surface = cairo_surface_create_similar (mw->surface,
							   cairo_surface_get_content (mw->surface),
							   mw->width, mw->height);
	cr = cairo_create (mw->fading_surface);
	cairo_set_source_surface (cr, mw->surface, 0, 0);
	cairo_paint (cr);
	cairo_destroy (cr);

	mw->end_surface = new_surface;
	mw->fade_start_time = get_current_time ();
	mw->fade_total_duration = 0.75;
	mw->fade_is_first_frame = TRUE;

	source = g_timeout_source_new (1000 / 60.0);
	g_source_set_callback (source, (GSourceFunc) on_fade_tick, mw,
			       (GDestroyNotify) on_fade_finished);
	mw->fade_timeout_id = g_source_attach (source, NULL);
	g_source_unref (source);

	gtk_widget_queue_draw (mw->window);
}

static void
render_surface_at_size (MsdBackgroundManagerWayland *manager,
			MonitorWindow               *mw,
			int                          width,
			int                          height,
			gboolean                     animate)
{
	GdkPixbuf       *pixbuf;
	cairo_surface_t *new_surface;
	cairo_t         *cr;

	if (width <= 0 || height <= 0)
		return;

	pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
				 width, height);
	if (pixbuf == NULL) {
		g_warning ("msd-background-wayland: unable to create %dx%d pixbuf",
			   width, height);
		return;
	}

	mate_bg_draw (manager->bg, pixbuf, NULL, FALSE);

	new_surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
						  width, height);
	cr = cairo_create (new_surface);
	gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
	cairo_paint (cr);
	cairo_destroy (cr);

	g_object_unref (pixbuf);

	mw->width = width;
	mw->height = height;

	if (animate)
		start_or_continue_fade (manager, mw, new_surface);
	else
		set_surface_instantly (mw, new_surface);
}

static void
render_surface_for_monitor (MsdBackgroundManagerWayland *manager,
			    MonitorWindow               *mw,
			    gboolean                     animate)
{
	GdkRectangle rect;

	gdk_monitor_get_geometry (mw->monitor, &rect);
	render_surface_at_size (manager, mw, rect.width, rect.height, animate);
}

static void
on_window_size_allocate (GtkWidget       *widget G_GNUC_UNUSED,
			 GtkAllocation   *alloc,
			 MonitorWindow   *mw)
{
	if (alloc->width <= 0 || alloc->height <= 0)
		return;

	if (mw->surface == NULL)
		return;

	/* The layer surface is anchored to the output edges, so when the
	 * output resolution or scale changes the compositor resizes the
	 * window. Re-render at the new size so the wallpaper adapts even
	 * if no monitors-changed event arrives.
	 */
	if (alloc->width == mw->width && alloc->height == mw->height)
		return;

	g_debug ("msd-background-wayland: window resized to %dx%d, re-rendering",
		 alloc->width, alloc->height);
	render_surface_at_size (mw->manager, mw,
				alloc->width, alloc->height, FALSE);
}

static gboolean
on_window_draw (GtkWidget     *widget,
		cairo_t       *cr,
		MonitorWindow *mw)
{
	GtkAllocation   alloc;
	cairo_surface_t *src;
	gdouble         sx, sy;

	if (mw->surface == NULL && mw->fading_surface == NULL)
		return FALSE;

	src = (mw->fading_surface != NULL) ? mw->fading_surface : mw->surface;

	gtk_widget_get_allocation (widget, &alloc);
	if (alloc.width <= 0 || alloc.height <= 0)
		return FALSE;

	sx = alloc.width / (gdouble) cairo_image_surface_get_width (src);
	sy = alloc.height / (gdouble) cairo_image_surface_get_height (src);
	cairo_scale (cr, sx, sy);
	cairo_set_source_surface (cr, src, 0, 0);
	cairo_paint (cr);

	return FALSE;
}

static void
redraw_all (MsdBackgroundManagerWayland *manager)
{
	guint i;

	for (i = 0; i < manager->monitor_windows->len; i++) {
		MonitorWindow *mw = g_ptr_array_index (manager->monitor_windows, i);
		render_surface_for_monitor (manager, mw, TRUE);
		gtk_widget_queue_draw (mw->window);
	}
}

static void
on_window_destroy (GtkWidget     *widget G_GNUC_UNUSED,
		   MonitorWindow *mw)
{
	if (mw->fade_timeout_id != 0) {
		/* Make on_fade_finished a no-op, then drop the source */
		if (mw->fading_surface != NULL) {
			cairo_surface_destroy (mw->fading_surface);
			mw->fading_surface = NULL;
		}
		if (mw->end_surface != NULL) {
			cairo_surface_destroy (mw->end_surface);
			mw->end_surface = NULL;
		}
		g_source_remove (mw->fade_timeout_id);
		mw->fade_timeout_id = 0;
	}
	free_monitor_window_surface (mw);
	g_free (mw);
}

static void
set_input_region_empty (GtkWidget *window)
{
	struct wl_surface *wl_surface;
	GdkWindow         *gdk_window;

	gdk_window = gtk_widget_get_window (window);
	if (gdk_window == NULL)
		return;

	wl_surface = gdk_wayland_window_get_wl_surface (gdk_window);
	if (wl_surface != NULL)
		wl_surface_set_input_region (wl_surface, NULL);
}

static MonitorWindow *
create_monitor_window (MsdBackgroundManagerWayland *manager G_GNUC_UNUSED,
		       GdkMonitor                  *monitor)
{
	MonitorWindow *mw;
	GtkWidget     *window;
	GdkRectangle   rect;
	guint          i;

	gdk_monitor_get_geometry (monitor, &rect);

	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_widget_set_app_paintable (window, TRUE);
	gtk_widget_set_size_request (window, rect.width, rect.height);
	gtk_window_set_default_size (GTK_WINDOW (window), rect.width, rect.height);
	gtk_window_set_resizable (GTK_WINDOW (window), FALSE);

	g_debug ("msd-background-wayland: monitor %dx%d at %d,%d", rect.width, rect.height,
		 rect.x, rect.y);

	gtk_layer_init_for_window (GTK_WINDOW (window));
	gtk_layer_set_layer (GTK_WINDOW (window), GTK_LAYER_SHELL_LAYER_BACKGROUND);
	gtk_layer_set_namespace (GTK_WINDOW (window), "mate-background");
	gtk_layer_set_monitor (GTK_WINDOW (window), monitor);
	for (i = 0; i < GTK_LAYER_SHELL_EDGE_ENTRY_NUMBER; i++)
		gtk_layer_set_anchor (GTK_WINDOW (window), i, TRUE);
	gtk_layer_set_keyboard_mode (GTK_WINDOW (window), GTK_LAYER_SHELL_KEYBOARD_MODE_NONE);

	mw = g_new0 (MonitorWindow, 1);
	mw->window = window;
	mw->monitor = monitor;
	mw->manager = manager;
	mw->width = rect.width;
	mw->height = rect.height;

	g_signal_connect (window, "draw", G_CALLBACK (on_window_draw), mw);
	g_signal_connect (window, "size-allocate", G_CALLBACK (on_window_size_allocate), mw);
	g_signal_connect (window, "destroy", G_CALLBACK (on_window_destroy), mw);

	gtk_widget_show (window);

	set_input_region_empty (window);

	return mw;
}

static void
destroy_all_monitor_windows (MsdBackgroundManagerWayland *manager)
{
	guint i;

	for (i = 0; i < manager->monitor_windows->len; i++) {
		MonitorWindow *mw = g_ptr_array_index (manager->monitor_windows, i);
		gtk_widget_destroy (mw->window);
	}
	g_ptr_array_set_size (manager->monitor_windows, 0);
}

static void
rebuild_monitor_windows (MsdBackgroundManagerWayland *manager)
{
	GdkDisplay *display = manager->display;
	int         n_monitors;
	int         i;

	destroy_all_monitor_windows (manager);

	/* Caja's desktop window (layer "desktop", BACKGROUND) owns the
	 * wallpaper and icons. When the "show-desktop-icons" setting is
	 * enabled we must not map our own layer windows, or we would
	 * cover it.
	 */
	if (caja_desktop_is_showing (manager)) {
		manager->caja_desktop_active = TRUE;
		g_debug ("msd-background-wayland: caja desktop detected, background disabled");
		return;
	}
	manager->caja_desktop_active = FALSE;

	n_monitors = gdk_display_get_n_monitors (display);
	for (i = 0; i < n_monitors; i++) {
		GdkMonitor    *monitor;
		MonitorWindow *mw;

		monitor = gdk_display_get_monitor (display, i);
		mw = create_monitor_window (manager, monitor);
		g_ptr_array_add (manager->monitor_windows, mw);
	}
}

static void
on_show_desktop_icons_changed (GSettings                  *settings G_GNUC_UNUSED,
			       const gchar                *key G_GNUC_UNUSED,
			       MsdBackgroundManagerWayland *manager)
{
	gboolean active;

	active = caja_desktop_is_showing (manager);
	if (active == manager->caja_desktop_active)
		return;

	manager->caja_desktop_active = active;

	if (active) {
		g_debug ("msd-background-wayland: caja desktop icons enabled, hiding background");
		destroy_all_monitor_windows (manager);
	} else {
		g_debug ("msd-background-wayland: caja desktop icons disabled, showing background");
		rebuild_monitor_windows (manager);
		redraw_all (manager);
	}
}

static void
on_monitors_changed (GdkScreen                     *screen G_GNUC_UNUSED,
		     MsdBackgroundManagerWayland *manager)
{
	guint n_monitors = gdk_display_get_n_monitors (manager->display);
	guint i;

	/* A monitor set change (added/removed) requires recreating the
	 * layer windows. A pure geometry/scale change (e.g. the compositor
	 * output is being resized) keeps the same window count, so just
	 * re-render in place at the new size - destroying and recreating
	 * windows mid-resize races with the compositor and leaves the
	 * wallpaper missing.
	 */
	if (n_monitors != manager->monitor_windows->len) {
		g_debug ("msd-background-wayland: monitors changed, rebuilding (%u monitors)",
			 n_monitors);
		rebuild_monitor_windows (manager);
		redraw_all (manager);
		return;
	}

	g_debug ("msd-background-wayland: monitors changed, re-rendering in place (%u monitors)",
		 n_monitors);

	for (i = 0; i < manager->monitor_windows->len; i++) {
		MonitorWindow *mw = g_ptr_array_index (manager->monitor_windows, i);
		render_surface_for_monitor (manager, mw, FALSE);
		gtk_widget_queue_draw (mw->window);
	}
}

static void
on_bg_changed (MateBG                       *bg G_GNUC_UNUSED,
	       MsdBackgroundManagerWayland *manager)
{
	if (manager->caja_desktop_active)
		return;

	g_debug ("msd-background-wayland: background changed, redrawing");
	redraw_all (manager);
}

static gboolean
settings_change_event_idle_cb (MsdBackgroundManagerWayland *manager)
{
	g_debug ("msd-background-wayland: applying settings change");
	mate_bg_load_from_preferences (manager->bg);
	return FALSE;
}

static gboolean
settings_change_event_cb (GSettings                  *settings G_GNUC_UNUSED,
			  gpointer                    keys G_GNUC_UNUSED,
			  gint                        n_keys G_GNUC_UNUSED,
			  MsdBackgroundManagerWayland *manager)
{
	g_idle_add ((GSourceFunc) settings_change_event_idle_cb, manager);
	return FALSE;
}

MsdBackgroundManagerWayland *
msd_background_manager_wayland_new (GError **error)
{
	MsdBackgroundManagerWayland *manager;
	GdkDisplay                  *display;

	display = gdk_display_get_default ();
	if (display == NULL || !GDK_IS_WAYLAND_DISPLAY (display)) {
		g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
			     "A Wayland display is required for the Wayland background backend");
		return NULL;
	}

	manager = g_new0 (MsdBackgroundManagerWayland, 1);
	manager->display = display;
	manager->monitor_windows = g_ptr_array_new ();

	manager->settings = g_settings_new (MATE_BG_SCHEMA);
	manager->bg = mate_bg_new ();

	g_signal_connect (manager->bg, "changed",
			  G_CALLBACK (on_bg_changed), manager);
	g_signal_connect (manager->bg, "transitioned",
			  G_CALLBACK (on_bg_changed), manager);
	g_signal_connect (manager->settings, "change-event",
			  G_CALLBACK (settings_change_event_cb), manager);
	g_signal_connect (gdk_screen_get_default (), "monitors-changed",
			  G_CALLBACK (on_monitors_changed), manager);

	g_debug ("msd-background-wayland: initializing on %d monitors",
		 gdk_display_get_n_monitors (display));

	mate_bg_load_from_gsettings (manager->bg, manager->settings);

	rebuild_monitor_windows (manager);
	redraw_all (manager);

	g_signal_connect (manager->settings, "changed::show-desktop-icons",
			  G_CALLBACK (on_show_desktop_icons_changed), manager);

	return manager;
}

void
msd_background_manager_wayland_destroy (MsdBackgroundManagerWayland *manager)
{
	if (manager == NULL)
		return;

	g_signal_handlers_disconnect_by_func (gdk_screen_get_default (),
					      G_CALLBACK (on_monitors_changed), manager);

	destroy_all_monitor_windows (manager);
	g_ptr_array_free (manager->monitor_windows, TRUE);

	if (manager->settings != NULL) {
		g_signal_handlers_disconnect_by_func (manager->settings,
						      G_CALLBACK (settings_change_event_cb), manager);
		g_signal_handlers_disconnect_by_func (manager->settings,
						      G_CALLBACK (on_show_desktop_icons_changed), manager);
		g_object_unref (manager->settings);
	}

	if (manager->bg != NULL) {
		g_signal_handlers_disconnect_by_func (manager->bg,
						      G_CALLBACK (on_bg_changed), manager);
		g_object_unref (manager->bg);
	}

	g_free (manager);
}