diff --git a/Makefile b/Makefile index 3225e36..2a2e3ec 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CFLAGS = -Wall -Wextra -pedantic -lX11 -lXft -I/usr/include/freetype2 -pthread +CFLAGS = -Wall -Wextra -pedantic -lX11 -lXft -lXinerama -I/usr/include/freetype2 -pthread PREFIX ?= /usr/local CC ?= cc diff --git a/herbe.c b/herbe.c index 51d3990..d0aae01 100644 --- a/herbe.c +++ b/herbe.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -19,6 +20,9 @@ Display *display; Window window; int exit_code = EXIT_DISMISS; +#define max(a, b) (((a) > (b)) ? (a) : (b)) +#define min(a, b) (((a) < (b)) ? (a) : (b)) + static void die(const char *format, ...) { va_list ap; @@ -110,10 +114,58 @@ int main(int argc, char *argv[]) int screen = DefaultScreen(display); Visual *visual = DefaultVisual(display, screen); Colormap colormap = DefaultColormap(display, screen); - - int screen_width = DisplayWidth(display, screen); - int screen_height = DisplayHeight(display, screen); - + + Window focused_window; + { + int ignored; + XGetInputFocus(display, &focused_window, &ignored); + } + + XWindowAttributes attr; + XGetWindowAttributes(display, focused_window, &attr); + + int num_screens; + XineramaScreenInfo *info = XineramaQueryScreens(display, &num_screens); + + // Find the monitor the focused window is on. + int mon = 0; + int max_area = 0; + for (int idx = 0; idx < num_screens; idx += 1) + { + XineramaScreenInfo *at = info + idx; + + int r0x0 = at->x_org; + int r0y0 = at->y_org; + int r0x1 = at->x_org + at->width; + int r0y1 = at->y_org + at->height; + + int r1x0 = attr.x; + int r1y0 = attr.y; + int r1x1 = attr.x + (attr.width + attr.border_width*2); + int r1y1 = attr.y + (attr.height + attr.border_width*2); + + // Intersect + int x0 = max(r0x0, r1x0); + int x1 = min(r0x1, r1x1); + int y0 = max(r0y0, r1y0); + int y1 = min(r0y1, r1y1); + + int area = (x1 - x0)*(y1 - y0); + if(area > max_area) + { + max_area = area; + mon = idx; + } + } + + int mon_x = info[mon].x_org; + int mon_y = info[mon].y_org; + + int screen_width = info[mon].width; + int screen_height = info[mon].height; + + XFree(info); + XSetWindowAttributes attributes; attributes.override_redirect = True; XftColor color; @@ -161,8 +213,8 @@ int main(int argc, char *argv[]) if (corner == BOTTOM_LEFT || corner == BOTTOM_RIGHT) y = screen_height - height - border_size * 2 - pos_y; - - window = XCreateWindow(display, RootWindow(display, screen), x, y, width, height, border_size, DefaultDepth(display, screen), + + window = XCreateWindow(display, RootWindow(display, screen), mon_x + x, mon_y + y, width, height, border_size, DefaultDepth(display, screen), CopyFromParent, visual, CWOverrideRedirect | CWBackPixel | CWBorderPixel, &attributes); XftDraw *draw = XftDrawCreate(display, window, visual, colormap);