Skip to content

Commit

Permalink
monitor: Attempt to fix lock-bypassing
Browse files Browse the repository at this point in the history
This is a by-hand application of
mate-desktop/mate-screensaver#167. It cannot be
done automatically because the two codebases are quite diverged at this
point.

The idea is that instead of listening to the `monitor-changed` on
GdkScreen, we listen for the `monitor-added` and `monitor-removed`
signals on GdkDisplay, making dynamic monitor changes more reliable.

Signed-off-by: Evan Maddock <[email protected]>
  • Loading branch information
EbonJaeger committed Apr 16, 2024
1 parent a19afe5 commit bdeda0b
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 83 deletions.
175 changes: 96 additions & 79 deletions src/gs-manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -977,19 +977,17 @@ connect_window_signals (GSManager *manager,

static void
gs_manager_create_window_for_monitor (GSManager *manager,
GdkScreen *screen,
int monitor_index)
GdkMonitor *monitor)
{
GSWindow *window;
GdkRectangle rect;

GdkMonitor* monitor = gdk_display_get_monitor(gdk_display_get_default(), monitor_index);
gdk_monitor_get_geometry (monitor, &rect);

gs_debug ("Creating window for monitor %d [%d,%d] (%dx%d)",
monitor_index, rect.x, rect.y, rect.width, rect.height);
gs_debug ("Creating a window [%d,%d] (%dx%d)",
rect.x, rect.y, rect.width, rect.height);

window = gs_window_new (screen, monitor_index, manager->priv->lock_active);
window = gs_window_new (monitor, manager->priv->lock_active);

gs_window_set_user_switch_enabled (window, manager->priv->user_switch_enabled);
gs_window_set_logout_enabled (window, manager->priv->logout_enabled);
Expand All @@ -1009,68 +1007,80 @@ gs_manager_create_window_for_monitor (GSManager *manager,
}

static void
on_screen_monitors_changed (GdkScreen *screen,
GSManager *manager)
on_display_monitor_added (GdkDisplay *display,
GdkMonitor *monitor,
GSManager *manager)
{
GSList *l;
int n_monitors;
int n_windows;
int i;
GSList *l;
int n_monitors;

n_monitors = gdk_display_get_n_monitors (gdk_display_get_default ());
n_windows = g_slist_length (manager->priv->windows);
n_monitors = gdk_display_get_n_monitors (display);

gs_debug ("Monitors changed for screen 0: num=%d", n_monitors);
gs_debug ("Monitor added on display %s, now there are %d",
gdk_display_get_name (display), n_monitors);

if (n_monitors > n_windows) {
/* Tear down the unlock dialog in case we want to move it
* to the new monitor
*/
l = manager->priv->windows;
while (l != NULL)
{
gs_window_cancel_unlock_request (GS_WINDOW (l->data));
l = l->next;
}

/* Tear down unlock dialog in case we want to move it
* to a new monitor
*/
l = manager->priv->windows;
while (l != NULL) {
gs_window_cancel_unlock_request (GS_WINDOW (l->data));
l = l->next;
}
/* add a new window */
gs_manager_create_window_for_monitor (manager, monitor);

/* add more windows */
for (i = n_windows; i < n_monitors; i++) {
gs_manager_create_window_for_monitor (manager, screen, i);
}

/* And put unlock dialog up where ever it's supposed to be
*/
gs_manager_request_unlock (manager);
} else {

gdk_x11_grab_server ();

/* remove the extra windows */
l = manager->priv->windows;
while (l != NULL) {
GdkScreen *this_screen;
int this_monitor;
GSList *next = l->next;

this_screen = gs_window_get_screen (GS_WINDOW (l->data));
this_monitor = gs_window_get_monitor (GS_WINDOW (l->data));
if (this_screen == screen && this_monitor >= n_monitors) {
gs_window_destroy (GS_WINDOW (l->data));
manager->priv->windows = g_slist_delete_link (manager->priv->windows, l);
}
l = next;
}
/* and put unlock dialog up whereever it's supposed to be */
gs_manager_request_unlock (manager);
}

/* make sure there is a lock dialog on a connected monitor,
static void
on_display_monitor_removed (GdkDisplay *display,
GdkMonitor *monitor,
GSManager *manager)
{
GSList *l;
int n_monitors;
int n_windows;

n_monitors = gdk_display_get_n_monitors (display);
n_windows = g_slist_length (manager->priv->windows);

gs_debug ("Monitor removed on display %s, now there are %d",
gdk_display_get_name (display), n_monitors);

gdk_x11_grab_server ();

/* remove the now extra window */
l = manager->priv->windows;
while (l != NULL)
{
GdkDisplay *this_display;
GdkMonitor *this_monitor;
GSList *next = l->next;

this_display = gs_window_get_display (GS_WINDOW (l->data));
this_monitor = gdk_display_get_monitor (this_display, gs_window_get_monitor (GS_WINDOW (l->data)));

if (this_display == display && this_monitor == monitor)
{
gs_window_destroy (GS_WINDOW (l->data));
manager->priv->windows = g_slist_delete_link (manager->priv->windows, l);
}
l = next;
}

/* make sure there is a lock dialog on a connected monitor,
* and that the keyboard is still properly grabbed after all
* the windows above got destroyed*/
if (n_windows > n_monitors) {
gs_manager_request_unlock (manager);
}
if (n_windows > n_monitors) {
gs_manager_request_unlock (manager);
}

gdk_display_flush (gdk_display_get_default ());
gdk_x11_ungrab_server ();
}
gdk_display_flush (display);
gdk_x11_ungrab_server ();
}

static void
Expand All @@ -1088,11 +1098,15 @@ gs_manager_destroy_windows (GSManager *manager)

display = gdk_display_get_default ();

g_signal_handlers_disconnect_by_func (gdk_display_get_default_screen (display),
on_screen_monitors_changed,
manager);
g_signal_handlers_disconnect_by_func (display,
on_display_monitor_removed,
manager);
g_signal_handlers_disconnect_by_func (display,
on_display_monitor_added,
manager);

for (l = manager->priv->windows; l; l = l->next) {

for (l = manager->priv->windows; l; l = l->next) {
gs_window_destroy (l->data);
}
g_slist_free (manager->priv->windows);
Expand Down Expand Up @@ -1136,51 +1150,54 @@ gs_manager_finalize (GObject *object)
}

static void
gs_manager_create_windows_for_screen (GSManager *manager,
GdkScreen *screen)
gs_manager_create_windows_for_display (GSManager *manager,
GdkDisplay *display)
{
int n_monitors;
int i;
int n_monitors;
int i;

g_return_if_fail (manager != NULL);
g_return_if_fail (GS_IS_MANAGER (manager));
g_return_if_fail (GDK_IS_SCREEN (screen));
g_return_if_fail (GDK_IS_DISPLAY (display));

g_object_ref (manager);
g_object_ref (screen);
g_object_ref (display);

n_monitors = gdk_display_get_n_monitors (gdk_display_get_default ());
n_monitors = gdk_display_get_n_monitors (display);

gs_debug ("Creating %d windows for screen 0", n_monitors);
gs_debug ("Creating %d windows for display %s", n_monitors, gdk_display_get_name (display));

for (i = 0; i < n_monitors; i++) {
gs_manager_create_window_for_monitor (manager, screen, i);
GdkMonitor *mon = gdk_display_get_monitor (display, i);
gs_manager_create_window_for_monitor (manager, mon);
}

g_object_unref (screen);
g_object_unref (display);
g_object_unref (manager);
}

static void
gs_manager_create_windows (GSManager *manager)
{
GdkDisplay *display;
GdkScreen *screen;

g_return_if_fail (manager != NULL);
g_return_if_fail (GS_IS_MANAGER (manager));

g_assert (manager->priv->windows == NULL);

display = gdk_display_get_default ();
screen = gdk_display_get_default_screen (display);

g_signal_connect (screen,
"monitors-changed",
G_CALLBACK (on_screen_monitors_changed),
manager);
g_signal_connect (display,
"monitor-added",
G_CALLBACK (on_display_monitor_added),
manager);
g_signal_connect (display,
"monitor-removed",
G_CALLBACK (on_display_monitor_removed),
manager);

gs_manager_create_windows_for_screen (manager, screen);
gs_manager_create_windows_for_display (manager, display);
}

GSManager *
Expand Down
14 changes: 11 additions & 3 deletions src/gs-window-x11.c
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,13 @@ gs_window_set_lock_enabled (GSWindow *window,
g_object_notify (G_OBJECT (window), "lock-enabled");
}

GdkDisplay *gs_window_get_display (GSWindow *window)
{
g_return_val_if_fail (GS_IS_WINDOW (window), NULL);

return gdk_window_get_display (GTK_WIDGET (window));
}

void
gs_window_set_screen (GSWindow *window,
GdkScreen *screen)
Expand Down Expand Up @@ -2410,11 +2417,12 @@ gs_window_finalize (GObject *object)
}

GSWindow *
gs_window_new (GdkScreen *screen,
int monitor,
gs_window_new (GdkMonitor *monitor,
gboolean lock_enabled)
{
GObject *result;
GObject *result;
GdkDisplay *display = gdk_monitor_get_display (monitor);
GdkScreen *screen = gdk_display_get_default_screen (display);

result = g_object_new (GS_TYPE_WINDOW,
"type", GTK_WINDOW_POPUP,
Expand Down
3 changes: 2 additions & 1 deletion src/gs-window.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ GType gs_window_get_type(void);
gboolean gs_window_is_obscured(GSWindow* window);
gboolean gs_window_is_dialog_up(GSWindow* window);

GdkDisplay *gs_window_get_display(GSWindow *window);
void gs_window_set_screen(GSWindow* window, GdkScreen* screen);
GdkScreen* gs_window_get_screen(GSWindow* window);
void gs_window_set_monitor(GSWindow* window, int monitor);
Expand All @@ -76,7 +77,7 @@ void gs_window_show_message(GSWindow* window, const char* summary, const char* b
void gs_window_request_unlock(GSWindow* window);
void gs_window_cancel_unlock_request(GSWindow* window);

GSWindow* gs_window_new(GdkScreen* screen, int monitor, gboolean lock_enabled);
GSWindow* gs_window_new(GdkMonitor *monitor, gboolean lock_enabled);
void gs_window_show(GSWindow* window);
void gs_window_destroy(GSWindow* window);
GdkWindow* gs_window_get_gdk_window(GSWindow* window);
Expand Down

0 comments on commit bdeda0b

Please sign in to comment.