commit 3619a1877bfda43ab9e4cdbcd8599d8cfc82fcdf
parent b6c58f80c87f37dab6010278715ac3b6ce7105b0
Author: hhvn <dev@hhvn.uk>
Date: Tue, 23 May 2023 10:34:02 +0100
Draw on monitor with currently focused window
Diffstat:
M | Makefile | | | 7 | ++++++- |
M | herbe.c | | | 70 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- |
2 files changed, 67 insertions(+), 10 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,5 +1,10 @@
CFLAGS = -Wall -Wextra -pedantic -lX11 -lXft -I/usr/include/freetype2 -pthread
+CFLAGS += -g3 -O0
+
+CFLAGS += -DXINERAMA
+LDFLAGS += -lXinerama
+
PREFIX ?= /usr/local
CC ?= cc
@@ -9,7 +14,7 @@ config.h:
cp config.def.h config.h
herbe: herbe.c config.h
- $(CC) herbe.c $(CFLAGS) -o herbe
+ $(CC) herbe.c $(LDFLAGS) $(CFLAGS) -o herbe
install: herbe
mkdir -p ${DESTDIR}${PREFIX}/bin
diff --git a/herbe.c b/herbe.c
@@ -11,6 +11,10 @@
#include <fcntl.h>
#include <semaphore.h>
+#ifdef XINERAMA
+#include <X11/extensions/Xinerama.h>
+#endif /* XINERAMA */
+
#define EXIT_ACTION EXIT_SUCCESS
#define EXIT_DISMISS 2
@@ -116,11 +120,64 @@ expire(int sig) {
XFlush(display);
}
+void
+getpos(Display *dpy, int scr, int h, int *rx, int *ry) {
+#ifdef XINERAMA
+ XineramaScreenInfo *info;
+ XWindowAttributes win;
+ Window focus;
+ int mx, my, mw, mh;
+ int revert;
+ int i;
+ int n;
+
+ info = XineramaQueryScreens(dpy, &n);
+
+ if (info) {
+ XGetInputFocus(dpy, &focus, &revert);
+ XGetWindowAttributes(dpy, focus, &win);
+
+ for (i = 0; i < n; i++) {
+ if (win.x >= info[i].x_org && win.x <= info[i].x_org + info[i].width &&
+ win.y >= info[i].y_org && win.y <= info[i].y_org + info[i].height) {
+ mx = info[i].x_org;
+ my = info[i].y_org;
+ mw = info[i].width;
+ mh = info[i].height;
+ goto end;
+ }
+ }
+ }
+#endif /* XINERAMA */
+
+ mx = 0;
+ my = 0;
+ mw = DisplayWidth(dpy, scr);
+ mh = DisplayHeight(dpy, scr);
+
+end:
+ if (corner == TOP_RIGHT || corner == BOTTOM_RIGHT)
+ *rx = mw - w - border * 2 - x;
+ else
+ *rx = x;
+
+ if (corner == BOTTOM_LEFT || corner == BOTTOM_RIGHT)
+ *ry = mh - h - border * 2 - y;
+ else
+ *ry = y;
+
+ *rx += mx;
+ *ry += my;
+
+ XFree(info);
+}
+
int
main(int argc, char *argv[]) {
struct sigaction act_expire, act_ignore;
- int screen, sw, sh;
+ int screen;
int h, th;
+ int wx, wy;
size_t eol;
size_t i;
XSetWindowAttributes attributes;
@@ -129,6 +186,7 @@ main(int argc, char *argv[]) {
snprintf(spath, sizeof(spath), "/herbe-%d", getuid());
if (argc == 1) {
+ sem_unlink(spath);
fprintf(stderr, "usage: herbe body...\n");
return EXIT_FAILURE;
}
@@ -152,8 +210,6 @@ main(int argc, char *argv[]) {
die("XOpenDisplay()");
screen = DefaultScreen(display);
- sw = DisplayWidth(display, screen);
- sh = DisplayHeight(display, screen);
visual = DefaultVisual(display, screen);
colormap = DefaultColormap(display, screen);
@@ -194,13 +250,9 @@ main(int argc, char *argv[]) {
th = font->ascent - font->descent;
h = (body.len - 1) * lpad + body.len * th + 2 * pad;
- if (corner == TOP_RIGHT || corner == BOTTOM_RIGHT)
- x = sw - w - border * 2 - x;
-
- if (corner == BOTTOM_LEFT || corner == BOTTOM_RIGHT)
- y = sh - h - border * 2 - y;
+ getpos(display, screen, h, &wx, &wy);
- window = XCreateWindow(display, RootWindow(display, screen), x, y, w, h, border, DefaultDepth(display, screen),
+ window = XCreateWindow(display, RootWindow(display, screen), wx, wy, w, h, border, DefaultDepth(display, screen),
CopyFromParent, visual, CWOverrideRedirect | CWBackPixel | CWBorderPixel, &attributes);
draw = XftDrawCreate(display, window, visual, colormap);