Skip to content

Commit

Permalink
Rework stack management, separate backup locker lifecycle from main
Browse files Browse the repository at this point in the history
screensaver's.
  • Loading branch information
mtwebster committed Sep 25, 2022
1 parent 6ea66e1 commit 3d0c406
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 70 deletions.
62 changes: 50 additions & 12 deletions backup-locker/cs-backup-locker.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,22 @@

#include <libcscreensaver/cs-gdk-event-filter.h>
#include <libcscreensaver/cs-event-grabber.h>
#include <libcscreensaver/cs-screen.h>

static gboolean debug = FALSE;
static guint term_tty = 0;
static guint session_tty = 0;

static GMutex pretty_xid_mutex;
static GCancellable *window_monitor_cancellable = NULL;
static guint sigterm_src_id;

#define BACKUP_TYPE_WINDOW (backup_window_get_type ())

G_DECLARE_FINAL_TYPE (BackupWindow, backup_window, BACKUP, WINDOW, GtkWindow)

static void setup_window_monitor (BackupWindow *window, gulong xid);

struct _BackupWindow
{
GtkWindow parent_instance;
Expand All @@ -33,6 +40,7 @@ struct _BackupWindow
CsEventGrabber *grabber;

gulong pretty_xid;

gboolean should_grab;
};

Expand Down Expand Up @@ -156,6 +164,8 @@ on_composited_changed (gpointer data)
{
BackupWindow *window = BACKUP_WINDOW (data);

g_debug ("Received composited-changed");

if (gtk_widget_get_realized (GTK_WIDGET (window)))
{
gtk_widget_hide (GTK_WIDGET (window));
Expand All @@ -179,16 +189,32 @@ on_composited_changed (gpointer data)
}
}

static void
screensaver_window_changed (CsGdkEventFilter *filter,
Window xwindow,
BackupWindow *window)
{
backup_window_ungrab (window);

setup_window_monitor (window, xwindow);
}

static void
backup_window_realize (GtkWidget *widget)
{
if (GTK_WIDGET_CLASS (backup_window_parent_class)->realize) {
GTK_WIDGET_CLASS (backup_window_parent_class)->realize (widget);
}

root_window_size_changed (BACKUP_WINDOW (widget)->event_filter, (gpointer) widget);
BackupWindow *window = BACKUP_WINDOW (widget);

cs_screen_set_net_wm_name (gtk_widget_get_window (widget),
"backup-locker");

cs_gdk_event_filter_start (BACKUP_WINDOW (widget)->event_filter, FALSE, debug);
root_window_size_changed (window->event_filter, (gpointer) widget);

cs_gdk_event_filter_stop (window->event_filter);
cs_gdk_event_filter_start (window->event_filter, FALSE, debug);
}

static void
Expand Down Expand Up @@ -323,15 +349,13 @@ backup_window_new (gulong pretty_xid)

window->event_filter = cs_gdk_event_filter_new (GTK_WIDGET (window), pretty_xid);
g_signal_connect (window->event_filter, "xscreen-size", G_CALLBACK (root_window_size_changed), window);
g_signal_connect (window->event_filter, "screensaver-window-changed", G_CALLBACK (screensaver_window_changed), window);

window->pretty_xid = pretty_xid;

return GTK_WIDGET (result);
}

static GCancellable *window_monitor_cancellable = NULL;
static guint sigterm_src_id;

static void
window_monitor_thread (GTask *task,
gpointer source_object,
Expand All @@ -342,7 +366,7 @@ window_monitor_thread (GTask *task,
GError *error;

gulong xid = GDK_POINTER_TO_XID (task_data);
gchar *xid_str = g_strdup_printf ("%lu", xid);
gchar *xid_str = g_strdup_printf ("0x%lx", xid);
error = NULL;

xprop_proc = g_subprocess_new (G_SUBPROCESS_FLAGS_STDOUT_SILENCE,
Expand All @@ -360,7 +384,7 @@ window_monitor_thread (GTask *task,
}
else
{
g_debug ("Monitoring screensaver window (%#lx)", xid);
g_debug ("Moncitoring screensaver window (0x%lx)", xid);
g_subprocess_wait (xprop_proc, cancellable, &error);
if (error != NULL && error->code != G_IO_ERROR_CANCELLED)
{
Expand All @@ -377,26 +401,39 @@ screensaver_window_gone (GObject *source,
gpointer user_data)
{
BackupWindow *window = BACKUP_WINDOW (user_data);
GCancellable *task_cancellable = g_task_get_cancellable (G_TASK (result));
gulong xid = GDK_POINTER_TO_XID (g_task_get_task_data (G_TASK (result)));

g_task_propagate_boolean (G_TASK (result), NULL);

// The normal screensaver window is gone - either thru a crash or normal unlocking.
// The main process will kill us, or the user will have to. Either way, grab everything.
if (!g_cancellable_is_cancelled (g_task_get_cancellable (G_TASK (result))))
if (!g_cancellable_is_cancelled (task_cancellable))
{
g_debug ("Screensaver window gone");
activate_backup_window (window);
}
g_debug ("Screensaver window gone: 0x%lx (pretty_xid now 0x%lx)", xid, window->pretty_xid);
g_mutex_lock (&pretty_xid_mutex);

g_clear_object (&window_monitor_cancellable);
if (xid == window->pretty_xid)
{
activate_backup_window (window);
}

g_mutex_unlock (&pretty_xid_mutex);
}

g_clear_object (&task_cancellable);
}

static void
setup_window_monitor (BackupWindow *window, gulong xid)
{
GTask *task;

g_debug ("Beginning to monitor screensaver window 0x%lx", xid);

g_mutex_lock (&pretty_xid_mutex);
window->pretty_xid = xid;

window_monitor_cancellable = g_cancellable_new ();
task = g_task_new (NULL, window_monitor_cancellable, screensaver_window_gone, window);

Expand All @@ -405,6 +442,7 @@ setup_window_monitor (BackupWindow *window, gulong xid)

g_task_run_in_thread (task, window_monitor_thread);
g_object_unref (task);
g_mutex_unlock (&pretty_xid_mutex);
}

static gboolean
Expand Down
37 changes: 21 additions & 16 deletions libcscreensaver/cs-event-grabber.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#endif /* HAVE_XF86MISCSETGRABKEYSSTATE */

#include "cs-event-grabber.h"
#include "cs-screen.h"

static void cs_event_grabber_class_init (CsEventGrabberClass *klass);
static void cs_event_grabber_init (CsEventGrabber *grab);
Expand Down Expand Up @@ -169,7 +170,7 @@ cs_event_grabber_get_keyboard (CsEventGrabber *grab,
g_return_val_if_fail (window != NULL, FALSE);
g_return_val_if_fail (screen != NULL, FALSE);

DEBUG ("Grabbing keyboard widget=0x%X\n", (guint32) GDK_WINDOW_XID (window));
DEBUG ("Grabbing keyboard widget=0x%lx\n", (gulong) GDK_WINDOW_XID (window));
status = gdk_keyboard_grab (window, FALSE, GDK_CURRENT_TIME);

if (status == GDK_GRAB_SUCCESS) {
Expand Down Expand Up @@ -204,7 +205,7 @@ cs_event_grabber_get_mouse (CsEventGrabber *grab,

cursor = gdk_cursor_new (GDK_BLANK_CURSOR);

DEBUG ("Grabbing mouse widget=0x%X\n", (guint32) GDK_WINDOW_XID (window));
DEBUG ("Grabbing mouse widget=0x%lx\n", (gulong) GDK_WINDOW_XID (window));
status = gdk_pointer_grab (window, TRUE, 0, NULL,
(hide_cursor ? cursor : NULL),
GDK_CURRENT_TIME);
Expand Down Expand Up @@ -291,8 +292,8 @@ cs_event_grabber_move_mouse (CsEventGrabber *grab,
}

if (grab->priv->mouse_grab_window == window) {
DEBUG ("Window %X is already grabbed, skipping\n",
(guint32) GDK_WINDOW_XID (grab->priv->mouse_grab_window));
DEBUG ("Window 0x%lx is already grabbed, skipping\n",
(gulong) GDK_WINDOW_XID (grab->priv->mouse_grab_window));
return TRUE;
}

Expand All @@ -302,12 +303,12 @@ cs_event_grabber_move_mouse (CsEventGrabber *grab,
return TRUE;
#else
if (grab->priv->mouse_grab_window) {
DEBUG ("Moving pointer grab from 0x%X to 0x%X\n",
(guint32) GDK_WINDOW_XID (grab->priv->mouse_grab_window),
(guint32) GDK_WINDOW_XID (window));
DEBUG ("Moving pointer grab from 0x%lx to 0x%lx\n",
(gulong) GDK_WINDOW_XID (grab->priv->mouse_grab_window),
(gulong) GDK_WINDOW_XID (window));
} else {
DEBUG ("Getting pointer grab on 0x%X\n",
(guint32) GDK_WINDOW_XID (window));
DEBUG ("Getting pointer grab on 0x%lx\n",
(gulong) GDK_WINDOW_XID (window));
}
#endif

Expand Down Expand Up @@ -351,18 +352,18 @@ cs_event_grabber_move_keyboard (CsEventGrabber *grab,
GdkScreen *old_screen;

if (grab->priv->keyboard_grab_window == window) {
DEBUG ("Window %X is already grabbed, skipping\n",
(guint32) GDK_WINDOW_XID (grab->priv->keyboard_grab_window));
DEBUG ("Window 0x%lx is already grabbed, skipping\n",
(gulong) GDK_WINDOW_XID (grab->priv->keyboard_grab_window));
return TRUE;
}

if (grab->priv->keyboard_grab_window != NULL) {
DEBUG ("Moving keyboard grab from 0x%X to 0x%X\n",
(guint32) GDK_WINDOW_XID (grab->priv->keyboard_grab_window),
(guint32) GDK_WINDOW_XID (window));
DEBUG ("Moving keyboard grab from 0x%lx to 0x%lx\n",
(gulong) GDK_WINDOW_XID (grab->priv->keyboard_grab_window),
(gulong) GDK_WINDOW_XID (window));
} else {
DEBUG ("Getting keyboard grab on 0x%X\n",
(guint32) GDK_WINDOW_XID (window));
DEBUG ("Getting keyboard grab on 0x%lx\n",
(gulong) GDK_WINDOW_XID (window));

}

Expand Down Expand Up @@ -646,6 +647,10 @@ cs_event_grabber_init (CsEventGrabber *grab)

grab->priv->mouse_hide_cursor = FALSE;
grab->priv->invisible = gtk_invisible_new ();

cs_screen_set_net_wm_name (gtk_widget_get_window (grab->priv->invisible),
"event-grabber-window");

gtk_widget_show (grab->priv->invisible);
}

Expand Down
Loading

0 comments on commit 3d0c406

Please sign in to comment.