56 lines
No EOL
1.1 KiB
C
56 lines
No EOL
1.1 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <X11/Xlib.h>
|
|
#include <X11/extensions/Xrandr.h>
|
|
#include "dat.h"
|
|
#include "fns.h"
|
|
|
|
int nmonitors = 0;
|
|
XRRMonitorInfo* monitorinfo;
|
|
|
|
void fetchmonitorinfo() {
|
|
if (monitorinfo)
|
|
XRRFreeMonitors(monitorinfo);
|
|
|
|
monitorinfo = XRRGetMonitors(dpy, DefaultRootWindow(dpy), 1, &nmonitors);
|
|
}
|
|
|
|
int getmonitorbyclient(Client* c) {
|
|
XRRMonitorInfo m;
|
|
int cx, cy, i, p;
|
|
cx = c->x + c->dx / 2;
|
|
cy = c->y + c->dy / 2;
|
|
|
|
p = 0;
|
|
|
|
for (i = 0; i < nmonitors; i++) {
|
|
m = monitorinfo[i];
|
|
if (cx >= m.x && cx < m.x + m.width && cy >= m.y && cy < m.y + m.height) {
|
|
return i;
|
|
}
|
|
|
|
if (m.primary) {
|
|
p = i;
|
|
}
|
|
}
|
|
|
|
/* if center is not within any window, return primary */
|
|
return p;
|
|
}
|
|
|
|
int getmonitorbymouse() {
|
|
Window w;
|
|
int x, y, i;
|
|
unsigned int mask;
|
|
XRRMonitorInfo m;
|
|
|
|
XQueryPointer(dpy, DefaultRootWindow(dpy), &w, &w, &x, &y, &x, &y, &mask);
|
|
for (i = 0; i < nmonitors; i++) {
|
|
m = monitorinfo[i];
|
|
if (x >= m.x && x < m.x + m.width && y >= m.y && y < m.y + m.height) {
|
|
return i;
|
|
}
|
|
}
|
|
/* should never reach here, but return first monitor if we do */
|
|
return 0;
|
|
} |