Receive a signal when the window in focus is maximized and when it leaves maximized mode

StackOverflow https://stackoverflow.com/questions/22723192

  •  23-06-2023
  •  | 
  •  

Question

I'm writing a script that will run as a daemon. I need to check when there's a maximized window in focus on the screen, and when the window in focus isn't maximized and run my script (bash) in both situations. Could anybody help me?

Était-ce utile?

La solution

I'm not aware of any solution ready to be used out of the box but you can easily accomplish what requested by leveraging libwnck. In the following a quite basic example that catches any maximization on the current screen:

/* gcc $(pkg-config --cflags --libs libwnck-1.0) test.c -o test */

#include <gdk/gdk.h>

#define WNCK_I_KNOW_THIS_IS_UNSTABLE
#include <libwnck/libwnck.h>


static void
geometry_changed(WnckWindow *window)
{
    if (wnck_window_is_maximized(window)) {
        g_print("A window has been maximized\n");
    }
}

static void
window_opened(WnckScreen *screen, WnckWindow *window)
{
    g_signal_connect(window, "geometry-changed",
                     G_CALLBACK(geometry_changed), NULL);

    /* Force a geometry-changed emission on already opened windows:
     * remove it if you need to catch only *new* maximizations */
    geometry_changed(window);
}

gint
main(gint argc, gchar *argv[])
{
    GMainLoop *loop;
    WnckScreen *screen;

    gdk_init(&argc, &argv);

    screen = wnck_screen_get(0);
    g_signal_connect(screen, "window-opened",
                     G_CALLBACK(window_opened), NULL);

    loop = g_main_loop_new(NULL, FALSE);
    g_main_loop_run(loop);
    g_main_loop_unref(loop);

    return 0;
}

Addendum

If you want to know when the window focus changes, you can connect a callback to the active-window-changed signal of the relevant WnckScreen, such as in the following untested snippet:

static void
active_window_changed(WnckScreen *screen)
{
    WnckWindow *active_window = wnck_screen_get_active_window(screen);
    if (wnck_window_is_maximized(active_window)) {
        g_print("The active window is maximized\n");
    }
}

...
g_signal_connect(screen, "active-window-changed",
                 G_CALLBACK(active_window_changed), NULL);
...
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top