diff options
author | Colomban Wendling <[email protected]> | 2024-09-10 18:39:01 +0200 |
---|---|---|
committer | Luke from DC <[email protected]> | 2024-09-22 06:13:18 +0000 |
commit | 90b39ec6035110936e24b8095b7128e1330806f1 (patch) | |
tree | 996c699e7e10c025e3c1fb06bb347acb7c897a3b /src/daemon/daemon.c | |
parent | 10455f6dc15a5d41d2e06e2f7c1c09276a0bbf4f (diff) | |
download | mate-notification-daemon-90b39ec6035110936e24b8095b7128e1330806f1.tar.bz2 mate-notification-daemon-90b39ec6035110936e24b8095b7128e1330806f1.tar.xz |
daemon: Properly update the set of monitors when it changes
Fixes #218.
Diffstat (limited to 'src/daemon/daemon.c')
-rw-r--r-- | src/daemon/daemon.c | 70 |
1 files changed, 42 insertions, 28 deletions
diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c index 59b4ec6..a513efd 100644 --- a/src/daemon/daemon.c +++ b/src/daemon/daemon.c @@ -323,54 +323,68 @@ static void on_screen_monitors_changed(GdkScreen* screen, NotifyDaemon* daemon) GdkDisplay *display; NotifyScreen* nscreen; int n_monitors; - int i; nscreen = daemon->screen; display = gdk_screen_get_display (screen); n_monitors = gdk_display_get_n_monitors(display); - if (n_monitors > (int) nscreen->n_stacks) + if (n_monitors != (int) nscreen->n_stacks) { - /* grow */ - nscreen->stacks = g_renew(NotifyStack *, nscreen->stacks, (gsize) n_monitors); + NotifyStack **new_stacks = g_malloc (sizeof *new_stacks * n_monitors); - /* add more stacks */ - for (i = (int) nscreen->n_stacks; i < n_monitors; i++) + /* create the new list, either moving over from the existing stacks, + * or creating new ones as needed */ + for (int i = 0; i < n_monitors; i++) { - create_stack_for_monitor(daemon, screen, gdk_display_get_monitor (display, i)); - } - - nscreen->n_stacks = (gsize) n_monitors; - } - else if (n_monitors < (int) nscreen->n_stacks) - { - NotifyStack* last_stack; + GdkMonitor *monitor = gdk_display_get_monitor (display, i); + gsize j; - last_stack = nscreen->stacks[n_monitors - 1]; - - /* transfer items before removing stacks */ - for (i = n_monitors; i < (int) nscreen->n_stacks; i++) + for (j = 0; j < nscreen->n_stacks; j++) + { + if (nscreen->stacks[j] != NULL && + notify_stack_get_monitor (nscreen->stacks[j]) == monitor) + { + new_stacks[i] = nscreen->stacks[j]; + nscreen->stacks[j] = NULL; + break; + } + } + /* not found, create it */ + if (j >= nscreen->n_stacks) + new_stacks[i] = notify_stack_new (daemon, screen, monitor, daemon->stack_location); + } + /* transfer items and remove old stacks */ + for (gsize i = 0; i < nscreen->n_stacks; i++) { NotifyStack* stack = nscreen->stacks[i]; - GList* windows = g_list_copy(notify_stack_get_windows(stack)); - GList* l; - for (l = windows; l != NULL; l = l->next) + /* if it has been moved over to the new ones, ignore it */ + if (stack == NULL) + continue; + + if (n_monitors > 0) /* always true, but just to be safe */ { - /* skip removing the window from the old stack since it will try - * to unrealize the window. - * And the stack is going away anyhow. */ - notify_stack_add_window(last_stack, l->data, TRUE); + int target_idx = n_monitors - 1; + GList* windows = g_list_copy (notify_stack_get_windows (stack)); + + for (GList *l = windows; l != NULL; l = l->next) + { + /* skip removing the window from the old stack since it will try + * to unrealize the window. + * And the stack is going away anyhow. */ + notify_stack_add_window (new_stacks[target_idx], l->data, TRUE); + } + + g_list_free(windows); } - g_list_free(windows); notify_stack_destroy(stack); nscreen->stacks[i] = NULL; } - /* remove the extra stacks */ - nscreen->stacks = g_renew(NotifyStack*, nscreen->stacks, (gsize) n_monitors); + g_free (nscreen->stacks); + nscreen->stacks = new_stacks; nscreen->n_stacks = (gsize) n_monitors; } } |