From 970f027a7b245f4782827834f265294c5a0770db Mon Sep 17 00:00:00 2001 From: Alexei Sorokin Date: Sun, 12 Mar 2017 15:07:16 +0300 Subject: Avoid deprecated gdk_window_set_background functions Allows to fix backgrounds in Caja and fixes fading. These changes somewhat break API behavior. --- libmate-desktop/mate-bg-crossfade.c | 257 ++++++++++++++++++++++++++++++------ libmate-desktop/mate-bg-crossfade.h | 4 +- libmate-desktop/mate-bg.c | 37 +++--- 3 files changed, 244 insertions(+), 54 deletions(-) diff --git a/libmate-desktop/mate-bg-crossfade.c b/libmate-desktop/mate-bg-crossfade.c index 37fddd8..8dac621 100644 --- a/libmate-desktop/mate-bg-crossfade.c +++ b/libmate-desktop/mate-bg-crossfade.c @@ -41,9 +41,11 @@ struct _MateBGCrossfadePrivate { GdkWindow *window; + GtkWidget *widget; int width; int height; cairo_surface_t *fading_surface; + cairo_surface_t *start_surface; cairo_surface_t *end_surface; gdouble start_time; gdouble total_duration; @@ -136,6 +138,11 @@ mate_bg_crossfade_finalize (GObject *object) fade->priv->fading_surface = NULL; } + if (fade->priv->start_surface != NULL) { + cairo_surface_destroy (fade->priv->start_surface); + fade->priv->start_surface = NULL; + } + if (fade->priv->end_surface != NULL) { cairo_surface_destroy (fade->priv->end_surface); fade->priv->end_surface = NULL; @@ -203,7 +210,10 @@ mate_bg_crossfade_init (MateBGCrossfade *fade) { fade->priv = MATE_BG_CROSSFADE_GET_PRIVATE (fade); + fade->priv->window = NULL; + fade->priv->widget = NULL; fade->priv->fading_surface = NULL; + fade->priv->start_surface = NULL; fade->priv->end_surface = NULL; fade->priv->timeout_id = 0; } @@ -303,17 +313,17 @@ mate_bg_crossfade_set_start_surface (MateBGCrossfade* fade, cairo_surface_t *sur { g_return_val_if_fail (MATE_IS_BG_CROSSFADE (fade), FALSE); - if (fade->priv->fading_surface != NULL) + if (fade->priv->start_surface != NULL) { - cairo_surface_destroy (fade->priv->fading_surface); - fade->priv->fading_surface = NULL; + cairo_surface_destroy (fade->priv->start_surface); + fade->priv->start_surface = NULL; } - fade->priv->fading_surface = tile_surface (surface, - fade->priv->width, - fade->priv->height); + fade->priv->start_surface = tile_surface (surface, + fade->priv->width, + fade->priv->height); - return fade->priv->fading_surface != NULL; + return fade->priv->start_surface != NULL; } static gdouble @@ -398,21 +408,59 @@ send_root_property_change_notification (MateBGCrossfade *fade) static void draw_background (MateBGCrossfade *fade) { - if (gdk_window_get_window_type (fade->priv->window) == GDK_WINDOW_ROOT) { - XClearArea (GDK_WINDOW_XDISPLAY (fade->priv->window), - GDK_WINDOW_XID (fade->priv->window), - 0, 0, - gdk_window_get_width (fade->priv->window), - gdk_window_get_height (fade->priv->window), - False); - send_root_property_change_notification (fade); - gdk_flush (); - } else { + if (fade->priv->widget != NULL) { + gtk_widget_queue_draw (fade->priv->widget); + } else if (gdk_window_get_window_type (fade->priv->window) != GDK_WINDOW_ROOT) { +#if GTK_CHECK_VERSION (3, 22, 0) + cairo_t *cr; + cairo_region_t *region; + GdkDrawingContext *draw_context; + + region = gdk_window_get_visible_region (fade->priv->window); + draw_context = gdk_window_begin_draw_frame (fade->priv->window, + region); + cr = gdk_drawing_context_get_cairo_context (draw_context); + cairo_set_operator (cr, CAIRO_OPERATOR_OVER); + cairo_set_source_surface (cr, fade->priv->fading_surface, 0, 0); + cairo_paint (cr); + gdk_window_end_draw_frame (fade->priv->window, draw_context); + cairo_region_destroy (region); +#else + cairo_pattern_t *pattern; + + pattern = + cairo_pattern_create_for_surface (fade->priv->fading_surface); + gdk_window_set_background_pattern (fade->priv->window, pattern); + cairo_pattern_destroy (pattern); gdk_window_invalidate_rect (fade->priv->window, NULL, FALSE); gdk_window_process_updates (fade->priv->window, FALSE); +#endif + } else { + Display *xdisplay = GDK_WINDOW_XDISPLAY (fade->priv->window); + gdk_error_trap_push (); + XGrabServer (xdisplay); + XClearWindow (xdisplay, GDK_WINDOW_XID (fade->priv->window)); + send_root_property_change_notification (fade); + XFlush (xdisplay); + XUngrabServer (xdisplay); + gdk_error_trap_pop_ignored (); } } +static gboolean +on_widget_draw (GtkWidget *widget, + cairo_t *cr, + MateBGCrossfade *fade) +{ + g_assert (fade->priv->fading_surface != NULL); + + cairo_set_source_surface (cr, fade->priv->fading_surface, 0, 0); + cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_REPEAT); + cairo_paint (cr); + + return FALSE; +} + static gboolean on_tick (MateBGCrossfade *fade) { @@ -437,7 +485,8 @@ on_tick (MateBGCrossfade *fade) return on_tick (fade); } - if (fade->priv->fading_surface == NULL) { + if (fade->priv->fading_surface == NULL || + fade->priv->end_surface == NULL) { return FALSE; } @@ -471,54 +520,166 @@ on_tick (MateBGCrossfade *fade) static void on_finished (MateBGCrossfade *fade) { + cairo_t *cr; + if (fade->priv->timeout_id == 0) return; + g_assert (fade->priv->fading_surface != NULL); g_assert (fade->priv->end_surface != NULL); - cairo_pattern_t *pattern; - pattern = cairo_pattern_create_for_surface (fade->priv->end_surface); - gdk_window_set_background_pattern (fade->priv->window, pattern); - cairo_pattern_destroy (pattern); + cr = cairo_create (fade->priv->fading_surface); + cairo_set_source_surface (cr, fade->priv->end_surface, 0, 0); + cairo_paint (cr); + cairo_destroy (cr); draw_background (fade); + cairo_surface_destroy (fade->priv->fading_surface); + fade->priv->fading_surface = NULL; + cairo_surface_destroy (fade->priv->end_surface); fade->priv->end_surface = NULL; - g_assert (fade->priv->fading_surface != NULL); + g_assert (fade->priv->start_surface != NULL); - cairo_surface_destroy (fade->priv->fading_surface); - fade->priv->fading_surface = NULL; + cairo_surface_destroy (fade->priv->start_surface); + fade->priv->start_surface = NULL; + + if (fade->priv->widget != NULL) { + g_signal_handlers_disconnect_by_func (fade->priv->widget, + (GCallback) on_widget_draw, + fade); + } + fade->priv->widget = NULL; fade->priv->timeout_id = 0; g_signal_emit (fade, signals[FINISHED], 0, fade->priv->window); } +/* This function queries the _XROOTPMAP_ID property from the root window + * to determine the current root window background pixmap and returns a + * surface to draw directly to it. + * If _XROOTPMAP_ID is not set, then NULL returned. + */ +static cairo_surface_t * +get_root_pixmap_id_surface (GdkDisplay *display) +{ + GdkScreen *screen; + Display *xdisplay; + Visual *xvisual; + Window xroot; + Atom type; + int format, result; + unsigned long nitems, bytes_after; + unsigned char *data; + cairo_surface_t *surface = NULL; + + g_return_val_if_fail (display != NULL, NULL); + + screen = gdk_display_get_default_screen (display); + xdisplay = GDK_DISPLAY_XDISPLAY (display); + xvisual = GDK_VISUAL_XVISUAL (gdk_screen_get_system_visual (screen)); + xroot = RootWindow (xdisplay, GDK_SCREEN_XNUMBER (screen)); + + result = XGetWindowProperty (xdisplay, xroot, + gdk_x11_get_xatom_by_name ("_XROOTPMAP_ID"), + 0L, 1L, False, XA_PIXMAP, + &type, &format, &nitems, &bytes_after, + &data); + + if (result != Success || type != XA_PIXMAP || + format != 32 || nitems != 1) { + XFree (data); + data = NULL; + } + + if (data != NULL) { + Pixmap pixmap = *(Pixmap *) data; + Window root_ret; + int x_ret, y_ret; + unsigned int w_ret, h_ret, bw_ret, depth_ret; + + gdk_error_trap_push (); + if (XGetGeometry (xdisplay, pixmap, &root_ret, + &x_ret, &y_ret, &w_ret, &h_ret, + &bw_ret, &depth_ret)) + { + surface = cairo_xlib_surface_create (xdisplay, + pixmap, xvisual, + w_ret, h_ret); + } + + gdk_error_trap_pop_ignored (); + XFree (data); + } + + gdk_display_flush (display); + return surface; +} + /** * mate_bg_crossfade_start: * @fade: a #MateBGCrossfade * @window: The #GdkWindow to draw crossfade on * * This function initiates a quick crossfade between two surfaces on - * the background of @window. Before initiating the crossfade both - * mate_bg_crossfade_start() and mate_bg_crossfade_end() need to - * be called. If animations are disabled, the crossfade is skipped, - * and the window background is set immediately to the end surface. + * the background of @window. Before initiating the crossfade both + * mate_bg_crossfade_set_start_surface() and + * mate_bg_crossfade_set_end_surface() need to be called. If animations + * are disabled, the crossfade is skipped, and the window background is + * set immediately to the end surface. **/ void mate_bg_crossfade_start (MateBGCrossfade *fade, - GdkWindow *window) + GdkWindow *window) { GSource *source; GMainContext *context; g_return_if_fail (MATE_IS_BG_CROSSFADE (fade)); g_return_if_fail (window != NULL); - g_return_if_fail (fade->priv->fading_surface != NULL); + g_return_if_fail (fade->priv->start_surface != NULL); g_return_if_fail (fade->priv->end_surface != NULL); g_return_if_fail (!mate_bg_crossfade_is_started (fade)); g_return_if_fail (gdk_window_get_window_type (window) != GDK_WINDOW_FOREIGN); + /* If drawing is done on the root window, + * it is essential to have the root pixmap. + */ + if (gdk_window_get_window_type (window) == GDK_WINDOW_ROOT) { + GdkDisplay *display = gdk_window_get_display (window); + cairo_surface_t *surface = get_root_pixmap_id_surface (display); + + g_return_if_fail (surface != NULL); + cairo_surface_destroy (surface); + } + + if (fade->priv->fading_surface != NULL) { + cairo_surface_destroy (fade->priv->fading_surface); + fade->priv->fading_surface = NULL; + } + + fade->priv->window = window; + if (gdk_window_get_window_type (fade->priv->window) != GDK_WINDOW_ROOT) { + fade->priv->fading_surface = tile_surface (fade->priv->start_surface, + fade->priv->width, + fade->priv->height); + if (fade->priv->widget != NULL) { + g_signal_connect (fade->priv->widget, "draw", + (GCallback) on_widget_draw, fade); + } + } else { + cairo_t *cr; + GdkDisplay *display = gdk_window_get_display (fade->priv->window); + + fade->priv->fading_surface = get_root_pixmap_id_surface (display); + cr = cairo_create (fade->priv->fading_surface); + cairo_set_source_surface (cr, fade->priv->start_surface, 0, 0); + cairo_paint (cr); + cairo_destroy (cr); + } + draw_background (fade); + source = g_timeout_source_new (1000 / 60.0); g_source_set_callback (source, (GSourceFunc) on_tick, @@ -528,18 +689,38 @@ mate_bg_crossfade_start (MateBGCrossfade *fade, fade->priv->timeout_id = g_source_attach (source, context); g_source_unref (source); - fade->priv->window = window; - cairo_pattern_t *pattern; - pattern = cairo_pattern_create_for_surface (fade->priv->fading_surface); - gdk_window_set_background_pattern (fade->priv->window, pattern); - cairo_pattern_destroy (pattern); - draw_background (fade); - fade->priv->is_first_frame = TRUE; fade->priv->total_duration = .75; fade->priv->start_time = get_current_time (); } +/** + * mate_bg_crossfade_start_widget: + * @fade: a #MateBGCrossfade + * @widget: The #GtkWidget to draw crossfade on + * + * This function initiates a quick crossfade between two surfaces on + * the background of @widget. Before initiating the crossfade both + * mate_bg_crossfade_set_start_surface() and + * mate_bg_crossfade_set_end_surface() need to be called. If animations + * are disabled, the crossfade is skipped, and the window background is + * set immediately to the end surface. + **/ +void +mate_bg_crossfade_start_widget (MateBGCrossfade *fade, + GtkWidget *widget) +{ + GdkWindow *window; + + g_return_if_fail (MATE_IS_BG_CROSSFADE (fade)); + g_return_if_fail (widget != NULL); + + fade->priv->widget = widget; + gtk_widget_realize (fade->priv->widget); + window = gtk_widget_get_window (fade->priv->widget); + + mate_bg_crossfade_start (fade, window); +} /** * mate_bg_crossfade_is_started: diff --git a/libmate-desktop/mate-bg-crossfade.h b/libmate-desktop/mate-bg-crossfade.h index 912da20..af6b206 100644 --- a/libmate-desktop/mate-bg-crossfade.h +++ b/libmate-desktop/mate-bg-crossfade.h @@ -69,7 +69,9 @@ gboolean mate_bg_crossfade_set_end_surface (MateBGCrossfade *fade, cairo_surface_t *surface); void mate_bg_crossfade_start (MateBGCrossfade *fade, - GdkWindow *window); + GdkWindow *window); +void mate_bg_crossfade_start_widget (MateBGCrossfade *fade, + GtkWidget *widget); gboolean mate_bg_crossfade_is_started (MateBGCrossfade *fade); void mate_bg_crossfade_stop (MateBGCrossfade *fade); diff --git a/libmate-desktop/mate-bg.c b/libmate-desktop/mate-bg.c index d705cef..eb77652 100644 --- a/libmate-desktop/mate-bg.c +++ b/libmate-desktop/mate-bg.c @@ -1609,29 +1609,36 @@ MateBGCrossfade * mate_bg_set_surface_as_root_with_crossfade (GdkScreen *screen, cairo_surface_t *surface) { + GdkWindow *root_window; + int width, height; + MateBGCrossfade *fade; + cairo_t *cr; + cairo_surface_t *old_surface; + g_return_val_if_fail (screen != NULL, NULL); g_return_val_if_fail (surface != NULL, NULL); - GdkWindow *root_window = gdk_screen_get_root_window (screen); - int width = gdk_screen_get_width (screen); - int height = gdk_screen_get_height (screen); - - MateBGCrossfade *fade = mate_bg_crossfade_new (width, height); - - Display *display = GDK_DISPLAY_XDISPLAY (gdk_screen_get_display (screen)); - Pixmap pixmap_id = cairo_xlib_surface_get_drawable (surface); - - XGrabServer (display); - cairo_surface_t *old_surface = mate_bg_get_surface_from_root (screen); - mate_bg_set_root_pixmap_id (screen, display, pixmap_id); + root_window = gdk_screen_get_root_window (screen); + width = gdk_window_get_width (root_window); + height = gdk_window_get_height (root_window); + fade = mate_bg_crossfade_new (width, height); + old_surface = mate_bg_get_surface_from_root (screen); mate_bg_crossfade_set_start_surface (fade, old_surface); - cairo_surface_destroy (old_surface); mate_bg_crossfade_set_end_surface (fade, surface); - XFlush (display); - XUngrabServer (display); + /* Before setting the surface as a root pixmap, let's have it draw + * the old stuff, just so it won't be noticable + * (crossfade will later get it back) + */ + cr = cairo_create (surface); + cairo_set_source_surface (cr, old_surface, 0, 0); + cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_REPEAT); + cairo_paint (cr); + cairo_destroy (cr); + cairo_surface_destroy (old_surface); + mate_bg_set_surface_as_root (screen, surface); mate_bg_crossfade_start (fade, root_window); return fade; -- cgit v1.2.1