Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
64 changes: 58 additions & 6 deletions herbe.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <X11/Xlib.h>
#include <X11/Xft/Xft.h>
#include <X11/extensions/Xinerama.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down