[mutter/wip/wayland-work: 51/54] Add support for GTK application menus



commit 1249b0484be76542f59cec1f8a0c2fcc5950cdd1
Author: Giovanni Campagna <gcampagn redhat com>
Date:   Fri Aug 30 19:07:03 2013 +0200

    Add support for GTK application menus
    
    To do so, we need to be able to set surface state before creating
    the MetaWindow, so we introduce MetaWaylandSurfaceInitialState as
    a staging area.
    The gtk-shell-surface implementation would either write to the
    initial state, or directly to the window.
    
    At the same, implement set_title and set_class too, because it's
    easy enough.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=707128

 protocol/gtk-shell.xml             |   11 +++-
 src/core/window-private.h          |   13 ++++
 src/core/window-props.c            |   43 ++++---------
 src/core/window.c                  |   80 ++++++++++++++++++++++++-
 src/wayland/meta-wayland-surface.c |  116 +++++++++++++++++++++++++++++++++++-
 src/wayland/meta-wayland-surface.h |   20 ++++++
 6 files changed, 248 insertions(+), 35 deletions(-)
---
diff --git a/protocol/gtk-shell.xml b/protocol/gtk-shell.xml
index ccec34a..a4e2565 100644
--- a/protocol/gtk-shell.xml
+++ b/protocol/gtk-shell.xml
@@ -1,6 +1,15 @@
 <protocol name="gtk">
 
   <interface name="gtk_shell" version="1">
+    <enum name="capability">
+      <entry name="global_app_menu" value="1"/>
+      <entry name="global_menu_bar" value="2"/>
+    </enum>
+    
+    <event name="capabilities">
+      <arg name="capabilities" type="uint"/>
+    </event>
+
     <request name="get_gtk_surface">
       <arg name="gtk_surface" type="new_id" interface="gtk_surface"/>
       <arg name="surface" type="object" interface="wl_surface"/>
@@ -12,7 +21,7 @@
       <arg name="application_id" type="string" allow-null="true"/>
       <arg name="app_menu_path" type="string" allow-null="true"/>
       <arg name="menubar_path" type="string" allow-null="true"/>
-      <arg name="window_object_path" type="string" allow-null="true">
+      <arg name="window_object_path" type="string" allow-null="true"/>
       <arg name="application_object_path" type="string" allow-null="true"/>
       <arg name="unique_bus_name" type="string" allow-null="true"/>
     </request>
diff --git a/src/core/window-private.h b/src/core/window-private.h
index f7769d0..3ca2b0a 100644
--- a/src/core/window-private.h
+++ b/src/core/window-private.h
@@ -703,4 +703,17 @@ void meta_window_set_shape_region         (MetaWindow     *window,
                                            cairo_region_t *region);
 void meta_window_update_shape_region_x11  (MetaWindow *window);
 
+void meta_window_set_title                (MetaWindow *window,
+                                           const char *title);
+void meta_window_set_wm_class             (MetaWindow *window,
+                                           const char *wm_class,
+                                           const char *wm_instance);
+void meta_window_set_gtk_dbus_properties  (MetaWindow *window,
+                                           const char *application_id,
+                                           const char *unique_bus_name,
+                                           const char *appmenu_path,
+                                           const char *menubar_path,
+                                           const char *application_object_path,
+                                           const char *window_object_path);
+
 #endif
diff --git a/src/core/window-props.c b/src/core/window-props.c
index 13ee3bb..73452cc 100644
--- a/src/core/window-props.c
+++ b/src/core/window-props.c
@@ -489,28 +489,19 @@ static void
 set_window_title (MetaWindow *window,
                   const char *title)
 {
-  char *str;
+  char *new_title = NULL;
  
   gboolean modified =
     set_title_text (window,
                     window->using_net_wm_visible_name,
                     title,
                     window->display->atom__NET_WM_VISIBLE_NAME,
-                    &window->title);
+                    &new_title);
   window->using_net_wm_visible_name = modified;
   
-  /* strndup is a hack since GNU libc has broken %.10s */
-  str = g_strndup (window->title, 10);
-  g_free (window->desc);
-  window->desc = g_strdup_printf ("0x%lx (%s)", window->xwindow, str);
-  g_free (str);
-
-  if (window->frame)
-    meta_ui_set_frame_title (window->screen->ui,
-                             window->frame->xwindow,
-                             window->title);
-
-  g_object_notify (G_OBJECT (window), "title");
+  meta_window_set_title (window, new_title);
+
+  g_free (new_title);
 }
 
 static void
@@ -875,23 +866,15 @@ reload_wm_class (MetaWindow    *window,
                  MetaPropValue *value,
                  gboolean       initial)
 {
-  if (window->res_class)
-    g_free (window->res_class);
-  if (window->res_name)
-    g_free (window->res_name);
-
-  window->res_class = NULL;
-  window->res_name = NULL;
-
   if (value->type != META_PROP_VALUE_INVALID)
-    { 
-      if (value->v.class_hint.res_name)
-        window->res_name = g_strdup (value->v.class_hint.res_name);
-
-      if (value->v.class_hint.res_class)
-        window->res_class = g_strdup (value->v.class_hint.res_class);
-
-      g_object_notify (G_OBJECT (window), "wm-class");
+    {
+      meta_window_set_wm_class (window,
+                                value->v.class_hint.res_class,
+                                value->v.class_hint.res_name);
+    }
+  else
+    {
+      meta_window_set_wm_class (window, NULL, NULL);
     }
 
   meta_verbose ("Window %s class: '%s' name: '%s'\n",
diff --git a/src/core/window.c b/src/core/window.c
index 5e87e58..199799d 100644
--- a/src/core/window.c
+++ b/src/core/window.c
@@ -1064,7 +1064,10 @@ meta_window_new_shared (MetaDisplay         *display,
   window->xgroup_leader = None;
   meta_window_compute_group (window);
 
-  meta_window_load_initial_properties (window);
+  if (client_type == META_WINDOW_CLIENT_TYPE_X11)
+    meta_window_load_initial_properties (window);
+  else
+    meta_wayland_surface_set_initial_state (window->surface, window);
 
   if (!window->override_redirect &&
       client_type == META_WINDOW_CLIENT_TYPE_X11)
@@ -11467,3 +11470,78 @@ meta_window_can_close (MetaWindow *window)
 {
   return window->has_close_func;
 }
+
+void
+meta_window_set_title (MetaWindow *window,
+                       const char *title)
+{
+  char *str;
+
+  g_free (window->title);
+  window->title = g_strdup (title);
+
+  /* strndup is a hack since GNU libc has broken %.10s */
+  str = g_strndup (window->title, 10);
+  g_free (window->desc);
+  window->desc = g_strdup_printf ("0x%lx (%s)", window->xwindow, str);
+  g_free (str);
+
+  if (window->frame)
+    meta_ui_set_frame_title (window->screen->ui,
+                             window->frame->xwindow,
+                             window->title);
+
+  g_object_notify (G_OBJECT (window), "title");
+}
+
+void
+meta_window_set_wm_class (MetaWindow *window,
+                          const char *wm_class,
+                          const char *wm_instance)
+{
+  g_free (window->res_class);
+  g_free (window->res_name);
+
+  window->res_name = g_strdup (wm_instance);
+  window->res_class = g_strdup (wm_class);
+
+  g_object_notify (G_OBJECT (window), "wm-class");
+}
+
+void
+meta_window_set_gtk_dbus_properties (MetaWindow *window,
+                                     const char *application_id,
+                                     const char *unique_bus_name,
+                                     const char *appmenu_path,
+                                     const char *menubar_path,
+                                     const char *application_object_path,
+                                     const char *window_object_path)
+{
+  g_object_freeze_notify (G_OBJECT (window));
+
+  g_free (window->gtk_application_id);
+  window->gtk_application_id = g_strdup (application_id);
+  g_object_notify (G_OBJECT (window), "gtk-application-id");
+
+  g_free (window->gtk_unique_bus_name);
+  window->gtk_unique_bus_name = g_strdup (unique_bus_name);
+  g_object_notify (G_OBJECT (window), "gtk-unique-bus-name");
+
+  g_free (window->gtk_app_menu_object_path);
+  window->gtk_app_menu_object_path = g_strdup (appmenu_path);
+  g_object_notify (G_OBJECT (window), "gtk-app-menu-object-path");
+
+  g_free (window->gtk_menubar_object_path);
+  window->gtk_menubar_object_path = g_strdup (menubar_path);
+  g_object_notify (G_OBJECT (window), "gtk-menubar-object-path");
+
+  g_free (window->gtk_application_object_path);
+  window->gtk_application_object_path = g_strdup (application_object_path);
+  g_object_notify (G_OBJECT (window), "gtk-application-object-path");
+
+  g_free (window->gtk_window_object_path);
+  window->gtk_window_object_path = g_strdup (window_object_path);
+  g_object_notify (G_OBJECT (window), "gtk-window-object-path");
+
+  g_object_thaw_notify (G_OBJECT (window));
+}
diff --git a/src/wayland/meta-wayland-surface.c b/src/wayland/meta-wayland-surface.c
index 4a908de..1ce77f7 100644
--- a/src/wayland/meta-wayland-surface.c
+++ b/src/wayland/meta-wayland-surface.c
@@ -57,6 +57,9 @@
 #include "meta-weston-launch.h"
 #include "monitor-private.h"
 
+static void ensure_initial_state (MetaWaylandSurface *surface);
+static void free_initial_state   (MetaWaylandSurfaceInitialState *surface);
+
 static void
 surface_process_damage (MetaWaylandSurface *surface,
                         cairo_region_t *region)
@@ -337,6 +340,9 @@ meta_wayland_surface_free (MetaWaylandSurface *surface)
   MetaWaylandCompositor *compositor = surface->compositor;
   MetaWaylandFrameCallback *cb, *next;
 
+  if (surface->initial_state)
+    free_initial_state (surface->initial_state);
+
   compositor->surfaces = g_list_remove (compositor->surfaces, surface);
 
   meta_wayland_buffer_reference (&surface->buffer_ref, NULL);
@@ -738,7 +744,15 @@ shell_surface_set_title (struct wl_client *client,
   MetaWaylandSurfaceExtension *extension = wl_resource_get_user_data (resource);
   MetaWaylandSurface *surface = extension->surface;
 
-  g_warning ("TODO: support shell_surface_set_title request");
+  if (surface->window)
+    meta_window_set_title (surface->window, title);
+  else
+    {
+      ensure_initial_state (surface);
+
+      g_free (surface->initial_state->title);
+      surface->initial_state->title = g_strdup (title);
+    }
 }
 
 static void
@@ -749,7 +763,15 @@ shell_surface_set_class (struct wl_client *client,
   MetaWaylandSurfaceExtension *extension = wl_resource_get_user_data (resource);
   MetaWaylandSurface *surface = extension->surface;
 
-  g_warning ("TODO: support shell_surface_set_class request");
+  if (surface->window)
+    meta_window_set_wm_class (surface->window, class_, class_);
+  else
+    {
+      ensure_initial_state (surface);
+
+      g_free (surface->initial_state->wm_class);
+      surface->initial_state->wm_class = g_strdup (class_);
+    }
 }
 
 static const struct wl_shell_surface_interface meta_wayland_shell_surface_interface =
@@ -874,7 +896,41 @@ set_dbus_properties (struct wl_client   *client,
       return;
     }
 
-  /* TODO */
+  if (surface->window)
+    {
+      meta_window_set_gtk_dbus_properties (surface->window,
+                                          application_id,
+                                          unique_bus_name,
+                                          app_menu_path,
+                                          menubar_path,
+                                          application_object_path,
+                                          window_object_path);
+    }
+  else
+    {
+      MetaWaylandSurfaceInitialState *initial;
+
+      ensure_initial_state (surface);
+      initial = surface->initial_state;
+
+      g_free (initial->gtk_application_id);
+      initial->gtk_application_id = g_strdup (application_id);
+
+      g_free (initial->gtk_unique_bus_name);
+      initial->gtk_unique_bus_name = g_strdup (unique_bus_name);
+
+      g_free (initial->gtk_app_menu_path);
+      initial->gtk_app_menu_path = g_strdup (app_menu_path);
+
+      g_free (initial->gtk_menubar_path);
+      initial->gtk_menubar_path = g_strdup (menubar_path);
+
+      g_free (initial->gtk_application_object_path);
+      initial->gtk_application_object_path = g_strdup (application_object_path);
+
+      g_free (initial->gtk_window_object_path);
+      initial->gtk_window_object_path = g_strdup (window_object_path);
+    }
 }
 
 static const struct gtk_surface_interface meta_wayland_gtk_surface_interface =
@@ -919,6 +975,9 @@ bind_gtk_shell (struct wl_client *client,
 
   resource = wl_resource_create (client, &gtk_shell_interface, version, id);
   wl_resource_set_implementation (resource, &meta_wayland_gtk_shell_interface, data, NULL);
+
+  /* FIXME: ask the plugin */
+  gtk_shell_send_capabilities (resource, GTK_SHELL_CAPABILITY_GLOBAL_APP_MENU);
 }
 
 void
@@ -935,3 +994,54 @@ meta_wayland_init_shell (MetaWaylandCompositor *compositor)
     g_error ("Failed to register a global gtk-shell object");
 }
 
+void
+meta_wayland_surface_set_initial_state (MetaWaylandSurface *surface,
+                                       MetaWindow         *window)
+{
+  MetaWaylandSurfaceInitialState *initial = surface->initial_state;
+
+  if (initial == NULL)
+    return;
+
+  if (initial->title)
+    meta_window_set_title (window, initial->title);
+
+  if (initial->wm_class)
+    meta_window_set_wm_class (window, initial->wm_class, initial->wm_class);
+
+  meta_window_set_gtk_dbus_properties (window,
+                                      initial->gtk_application_id,
+                                      initial->gtk_unique_bus_name,
+                                      initial->gtk_app_menu_path,
+                                      initial->gtk_menubar_path,
+                                      initial->gtk_application_object_path,
+                                      initial->gtk_window_object_path);
+
+  free_initial_state (initial);
+  surface->initial_state = NULL;
+}
+
+static void
+ensure_initial_state (MetaWaylandSurface *surface)
+{
+  if (surface->initial_state)
+    return;
+
+  surface->initial_state = g_slice_new0 (MetaWaylandSurfaceInitialState);
+}
+
+static void
+free_initial_state (MetaWaylandSurfaceInitialState *initial)
+{
+  g_free (initial->title);
+  g_free (initial->wm_class);
+
+  g_free (initial->gtk_application_id);
+  g_free (initial->gtk_unique_bus_name);
+  g_free (initial->gtk_app_menu_path);
+  g_free (initial->gtk_menubar_path);
+  g_free (initial->gtk_application_object_path);
+  g_free (initial->gtk_window_object_path);
+
+  g_slice_free (MetaWaylandSurfaceInitialState, initial);
+}
diff --git a/src/wayland/meta-wayland-surface.h b/src/wayland/meta-wayland-surface.h
index 30978cd..5957bce 100644
--- a/src/wayland/meta-wayland-surface.h
+++ b/src/wayland/meta-wayland-surface.h
@@ -65,6 +65,19 @@ typedef struct
   struct wl_list frame_callback_list;
 } MetaWaylandDoubleBufferedState;
 
+typedef struct
+{
+  char *title;
+  char *wm_class;
+
+  char *gtk_application_id;
+  char *gtk_unique_bus_name;
+  char *gtk_app_menu_path;
+  char *gtk_menubar_path;
+  char *gtk_application_object_path;
+  char *gtk_window_object_path;
+} MetaWaylandSurfaceInitialState;
+
 struct _MetaWaylandSurface
 {
   struct wl_resource *resource;
@@ -79,6 +92,10 @@ struct _MetaWaylandSurface
 
   /* All the pending state, that wl_surface.commit will apply. */
   MetaWaylandDoubleBufferedState pending;
+
+  /* All the initial state, that wl_shell_surface.set_* will apply
+     (through meta_window_new_for_wayland) */
+  MetaWaylandSurfaceInitialState *initial_state;
 };
 
 typedef struct
@@ -96,4 +113,7 @@ MetaWaylandSurface *meta_wayland_surface_create (MetaWaylandCompositor *composit
                                                 guint32                version);
 void                meta_wayland_surface_free   (MetaWaylandSurface    *surface);
 
+void                meta_wayland_surface_set_initial_state (MetaWaylandSurface *surface,
+                                                           MetaWindow         *window);
+
 #endif


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]