[Xfce4-commits] [panel-plugins/xfce4-clipman-plugin] 01/03: Switch to GtkApplication
noreply at xfce.org
noreply at xfce.org
Fri Mar 24 12:35:40 CET 2017
This is an automated email from the git hooks/post-receive script.
mmassonnet pushed a commit to branch master
in repository panel-plugins/xfce4-clipman-plugin.
commit 33e4feb880d009eef848fc9b74e383c949cd5be8
Author: Mike Massonnet <m8t at gandi.net>
Date: Sun Mar 5 15:55:56 2017 +0100
Switch to GtkApplication
Replace the X11 Selection code with GtkApplication to do the IPC.
---
panel-plugin/main-status-icon.c | 26 +++++-
panel-plugin/plugin.c | 171 +------------------------------------
panel-plugin/xfce4-popup-clipman.c | 75 +++++-----------
3 files changed, 48 insertions(+), 224 deletions(-)
diff --git a/panel-plugin/main-status-icon.c b/panel-plugin/main-status-icon.c
index 335dbbf..6a0f1b5 100644
--- a/panel-plugin/main-status-icon.c
+++ b/panel-plugin/main-status-icon.c
@@ -56,18 +56,42 @@ main (gint argc,
gchar *argv[])
{
MyPlugin *plugin;
+ GtkApplication *app;
+ GError *error = NULL;
gtk_init (&argc, &argv);
+ app = gtk_application_new ("org.xfce.clipman", 0);
+
+ g_application_register (G_APPLICATION (app), NULL, &error);
+ if (error != NULL)
+ {
+ g_warning ("Unable to register GApplication: %s", error->message);
+ g_error_free (error);
+ error = NULL;
+ }
+
+ if (g_application_get_is_remote (G_APPLICATION (app)))
+ {
+ g_message ("Primary instance org.xfce.clipman already running");
+ g_object_unref (app);
+ return FALSE;
+ }
g_set_application_name (_("Clipman"));
plugin = status_icon_register ();
install_autostart_file ();
+ /* TODO this callback popups at systray position, need to popup at cursor
+ * position if option is set, this logic can be handled directly by
+ * plugin_popup_menu function. */
+ g_signal_connect_swapped (app, "activate", G_CALLBACK (plugin_popup_menu), plugin);
+
gtk_main ();
g_object_unref (plugin->status_icon);
+ g_object_unref (app);
- return 0;
+ return FALSE;
}
/*
diff --git a/panel-plugin/plugin.c b/panel-plugin/plugin.c
index a7fc9ec..6bc5c32 100644
--- a/panel-plugin/plugin.c
+++ b/panel-plugin/plugin.c
@@ -38,18 +38,11 @@
#include "history.h"
#include "menu.h"
-/*
- * Popup command
- */
-
-static gboolean my_plugin_set_popup_selection (MyPlugin *plugin);
-static GdkFilterReturn event_filter_popup_menu (GdkXEvent *xevent,
- GdkEvent *event,
- MyPlugin *plugin);
-static gboolean xfce_popup_grab_available (GdkWindow *win,
- guint32 timestamp);
+/*
+ * Private functions
+ */
static gboolean
clipboard_manager_ownership_exists (void)
@@ -133,8 +126,6 @@ plugin_register (void)
G_CALLBACK (plugin_save), plugin);
g_signal_connect_swapped (plugin->history, "clear",
G_CALLBACK (plugin_save), plugin);
- /* Set the selection for the popup command */
- my_plugin_set_popup_selection (plugin);
return plugin;
}
@@ -280,7 +271,6 @@ plugin_free (MyPlugin *plugin)
gsd_clipboard_manager_stop (plugin->daemon);
g_object_unref (plugin->daemon);
}
- gdk_window_remove_filter (gtk_widget_get_window(plugin->menu), (GdkFilterFunc) event_filter_popup_menu, plugin);
gtk_widget_destroy (plugin->menu);
g_object_unref (plugin->channel);
g_object_unref (plugin->actions);
@@ -381,158 +371,3 @@ G_GNUC_END_IGNORE_DEPRECATIONS
#endif
}
-/*
- * X11 Selection for the popup command
- */
-
-static gboolean
-my_plugin_set_popup_selection (MyPlugin *plugin)
-{
- GdkScreen *gscreen;
- gchar *selection_name;
- Atom selection_atom;
- GtkWidget *win;
- Window id;
- Display *display;
- GdkWindow *window;
-
- win = gtk_invisible_new ();
- gtk_widget_realize (win);
- id = GDK_WINDOW_XID (gtk_widget_get_window (win));
- display = gdk_x11_get_default_xdisplay ();
-
- gscreen = gtk_widget_get_screen (win);
- selection_name = g_strdup_printf (XFCE_CLIPMAN_SELECTION"%d",
- gdk_screen_get_number (gscreen));
- selection_atom = XInternAtom (display, selection_name, FALSE);
- g_free(selection_name);
-
- if (XGetSelectionOwner (display, selection_atom))
- {
- gtk_widget_destroy (win);
- return FALSE;
- }
-
- XSelectInput (display, id, PropertyChangeMask);
- XSetSelectionOwner (display, selection_atom, id, GDK_CURRENT_TIME);
-
- window = gtk_widget_get_window (win);
- gdk_window_add_filter (window, (GdkFilterFunc) event_filter_popup_menu, plugin);
-
- return TRUE;
-}
-
-static GdkFilterReturn
-event_filter_popup_menu (GdkXEvent *xevent, GdkEvent *event, MyPlugin *plugin)
-{
- XClientMessageEvent *evt;
- GdkScreen *screen;
- GdkWindow *root;
- Atom message_type;
- evt = (XClientMessageEvent *)xevent;
-
- if (((XEvent *)xevent)->type != ClientMessage)
- return GDK_FILTER_CONTINUE;
-
- message_type = XInternAtom (gdk_x11_get_default_xdisplay (), "STRING", FALSE);
- if (evt->message_type != message_type)
- return GDK_FILTER_CONTINUE;
-
- /* Copy workaround from xfdesktop to handle the awkward case where binding
- * a keyboard shortcut to the popup command doesn't always work out... */
-#ifdef PANEL_PLUGIN
- screen = gtk_widget_get_screen (GTK_WIDGET (plugin->button));
-#elif defined (STATUS_ICON)
-G_GNUC_BEGIN_IGNORE_DEPRECATIONS
- screen = gtk_status_icon_get_screen (plugin->status_icon);
-G_GNUC_END_IGNORE_DEPRECATIONS
-#endif
- root = gdk_screen_get_root_window (screen);
-
- if (!xfce_popup_grab_available (root, GDK_CURRENT_TIME))
- {
- g_critical ("Unable to get keyboard/mouse grab.");
- return FALSE;
- }
-
- if (G_LIKELY (evt->format == 8) && (*(evt->data.b) != '\0'))
- {
-
- if (!g_ascii_strcasecmp (XFCE_CLIPMAN_MESSAGE, evt->data.b))
- {
- DBG ("Message received: %s", evt->data.b);
-
- if (xfconf_channel_get_bool (plugin->channel, "/tweaks/popup-at-pointer", FALSE))
- {
- gtk_menu_popup (GTK_MENU (plugin->menu), NULL, NULL, NULL, NULL,
- 0, gtk_get_current_event_time ());
- }
- else
- {
- plugin_popup_menu (plugin);
- }
-
- return TRUE;
- } else if (!g_ascii_strcasecmp (XFCE_CLIPMAN_ACTION_MESSAGE, evt->data.b))
- {
- clipman_collector_show_actions();
- }
- }
-
- return FALSE;
-}
-
-#if GTK_CHECK_VERSION (3, 20, 0)
-static void
-make_window_visible (GdkSeat *seat,
- GdkWindow *window,
- gpointer user_data)
-{
- gdk_window_show (window);
-}
-#endif
-
-/* Code taken from xfwm4/src/menu.c:grab_available(). This should fix the case
- * where binding 'xfdesktop -menu' to a keyboard shortcut sometimes works and
- * sometimes doesn't. Credit for this one goes to Olivier.
- */
-static gboolean
-xfce_popup_grab_available (GdkWindow *win, guint32 timestamp)
-{
- GdkDisplay* display = gdk_window_get_display(win);
-#if GTK_CHECK_VERSION (3, 20, 0)
- GdkSeat *seat = gdk_display_get_default_seat (display);
-#endif
- GdkGrabStatus g = GDK_GRAB_ALREADY_GRABBED;
- gboolean grab_failed = TRUE;
- gint i = 0;
-
- TRACE ("entering grab_available");
-
- /* With a keyboard grab elsewhere, we have to wait on that to clear.
- * So try up to 2500 times and only keep trying when the failure is
- * already grabbed, any other failure mode will never succeed.
- */
- while ((i++ < 2500) && grab_failed && g == GDK_GRAB_ALREADY_GRABBED)
- {
-#if GTK_CHECK_VERSION (3, 20, 0)
- g = gdk_seat_grab(seat, win, GDK_SEAT_CAPABILITY_KEYBOARD, TRUE, NULL, NULL, make_window_visible, NULL);
- if (g == GDK_GRAB_SUCCESS)
- {
- gdk_seat_ungrab (seat);
- grab_failed = FALSE;
- }
-#else
- G_GNUC_BEGIN_IGNORE_DEPRECATIONS
- g = gdk_keyboard_grab (win, TRUE, timestamp);
- if (g == GDK_GRAB_SUCCESS)
- {
- gdk_keyboard_ungrab(timestamp);
- grab_failed = FALSE;
- }
- G_GNUC_END_IGNORE_DEPRECATIONS
-#endif
- }
-
- return (!grab_failed);
-}
diff --git a/panel-plugin/xfce4-popup-clipman.c b/panel-plugin/xfce4-popup-clipman.c
index 718542a..3dc39b6 100644
--- a/panel-plugin/xfce4-popup-clipman.c
+++ b/panel-plugin/xfce4-popup-clipman.c
@@ -23,75 +23,40 @@
#include <config.h>
#endif
-#include <X11/Xlib.h>
-#include <gdk/gdkx.h>
#include <gtk/gtk.h>
-#include "common.h"
-
-static gboolean
-clipman_plugin_check_is_running (GtkWidget *widget,
- Window *xid)
-{
- GdkScreen *gscreen;
- gchar *selection_name;
- Atom selection_atom;
- Display *display;
-
- display = gdk_x11_get_default_xdisplay ();
- gscreen = gtk_widget_get_screen (widget);
- selection_name = g_strdup_printf (XFCE_CLIPMAN_SELECTION"%d",
- gdk_screen_get_number (gscreen));
- selection_atom = XInternAtom (display, selection_name, FALSE);
- g_free(selection_name);
-
- if ((*xid = XGetSelectionOwner (display, selection_atom)))
- return TRUE;
-
- return FALSE;
-}
-
gint
main (gint argc, gchar *argv[])
{
- XEvent event;
- GtkWidget *win;
- GdkWindow *window;
- Window id;
- Display *display;
+ GtkApplication *app;
+ GError *error = NULL;
gtk_init (&argc, &argv);
+ app = gtk_application_new ("org.xfce.clipman", 0);
- win = gtk_invisible_new ();
- gtk_widget_realize (win);
+ g_application_register (G_APPLICATION (app), NULL, &error);
+ if (error != NULL)
+ {
+ g_warning ("Unable to register GApplication: %s", error->message);
+ g_error_free (error);
+ error = NULL;
+ }
- window = gtk_widget_get_window (GTK_WIDGET (win));
- display = gdk_x11_display_get_xdisplay (gdk_window_get_display (window));
- event.xclient.type = ClientMessage;
- event.xclient.message_type = XInternAtom (display, "STRING", False);
- event.xclient.format = 8;
- if (!g_ascii_strcasecmp(argv[0], "xfce4-popup-clipman-actions"))
+ if (g_application_get_is_remote (G_APPLICATION (app)))
{
- g_snprintf (event.xclient.data.b, sizeof (event.xclient.data.b), XFCE_CLIPMAN_ACTION_MESSAGE);
+ /* FIXME g_usleep is a workaround when using the popup command through a
+ * keyboard shortcut, in fact the code needs to call gdk_seat_grb/ungrab
+ * for the gtkmenu to show up.
+ */
+ g_usleep(500000);
+ g_application_activate (G_APPLICATION (app));
+ g_object_unref (app);
+ return 0;
}
else
{
- g_snprintf (event.xclient.data.b, sizeof (event.xclient.data.b), XFCE_CLIPMAN_MESSAGE);
+ g_warning ("Unable to find the primary instance org.xfce.clipman");
}
- if (clipman_plugin_check_is_running (win, &id)) {
- event.xclient.window = id;
- XSendEvent (display,
- (Window) id,
- False,
- NoEventMask,
- &event);
- }
- else
- g_warning ("Can't find the xfce4-clipman-plugin.\n");
- gdk_flush ();
-
- gtk_widget_destroy (win);
-
return FALSE;
}
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the Xfce4-commits
mailing list