diff --git a/app.c b/app.c index 56de75d..fbec35c 100644 --- a/app.c +++ b/app.c @@ -7,6 +7,7 @@ #include #include +#include int appInit(App *app) { @@ -118,6 +119,16 @@ int appInit(App *app) { // subscribe to keyboard state and layout changes XkbSelectEvents(app->dpy, XkbUseCoreKbd, XkbMapNotifyMask | XkbStateNotifyMask, XkbMapNotifyMask | XkbStateNotifyMask); + #ifdef SCREEN_EVENTS + // subscribe to xrandr events + if (!XRRQueryExtension(app->dpy, &app->xrev, &app->xrerr)) { + LOGWRN("app: init: XRRQueryExtension error"); + } else { + app->xron = 1; + XRRSelectInput(app->dpy, app->win, RRScreenChangeNotifyMask); + } + #endif + // init submodules if (graphInit(&app->graph, app)) { graphResize(&app->graph); @@ -273,8 +284,13 @@ int appRun(App *app) { } break; } - default: - break; + default: + if (app->xron && event.type == app->xrev + RRScreenChangeNotify) { + LOGDBG("app: screen change event"); + XRRScreenChangeNotifyEvent *ev = (XRRScreenChangeNotifyEvent*)&event; + appUpdateScreenSize(app, ev->width, ev->height); + } + break; } } @@ -321,6 +337,21 @@ void appStop(App *app, int err) { } +void appUpdateScreenSize(App *app, int sw, int sh) { + if (sw <= 0 || sh <= 0) return; + if (sw == app->sw && sh == app->sh) return; + LOGDBG("app: update screen size: w=%d, h=%d", sw, sh); + int hw = app->sw/2, hh = app->sh/2; + int x0 = (app->x*sw + hw)/app->sw; + int y0 = (app->y*sh + hh)/app->sh; + int x1 = ((app->x + app->w)*sw + hw)/app->sw; + int y1 = ((app->y + app->h)*sh + hh)/app->sh; + app->sw = sw; + app->sh = sh; + appMove(app, x0, y0, x1 - x0, y1 - y0); +} + + void appMove(App *app, int x, int y, int w, int h) { LOGDBG("app: move: x=%d, y=%d, w=%d, h=%d", x, y, w, h); if (w < MIN_WIDTH) w = MIN_WIDTH; diff --git a/app.h b/app.h index 2d79676..bf5345a 100644 --- a/app.h +++ b/app.h @@ -20,13 +20,14 @@ struct App { Window root; Window win; Atom aWmDel; - int sw, sh; int stopFd; // dynamic fields int run; + int sw, sh; int x, y, w, h; int irx, iry, irw, irh; + int xron, xrev, xrerr; }; @@ -34,6 +35,7 @@ int appInit(App *app); void appDeinit(App *app); int appRun(App *app); void appStop(App *app, int err); +void appUpdateScreenSize(App *app, int sw, int sh); void appMove(App *app, int x, int y, int w, int h); void appInvalidateRect(App *app, int x, int y, int w, int h); diff --git a/build.sh b/build.sh index 339f818..cfe21e4 100755 --- a/build.sh +++ b/build.sh @@ -4,7 +4,7 @@ set -e TARGET="coolkbd" -FLAGS="$(pkg-config --cflags --libs x11 xft xtst)" +FLAGS="$(pkg-config --cflags --libs x11 xft xtst xrandr)" FLAGS="$FLAGS -Wall" MODE_FLAGS="-O3 -DNDEBUG" diff --git a/config.h.example b/config.h.example index 5e7cc6d..c6319cb 100644 --- a/config.h.example +++ b/config.h.example @@ -9,6 +9,7 @@ //#define DOCK //#define TOP_RESIZE //#define TOP +//#define SCREEN_EVENTS #define WIDTH_SCALE 1/1 // (nominator)/(denomitator) #define HEIGHT_SCALE 1/3