ryudo/monitor.c

115 lines
2.5 KiB
C
Raw Permalink Normal View History

#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h>
#include "dat.h"
#include "fns.h"
#include "config.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 = cy = 0;
/* we check which monitor the center of the window is in */
if (c) {
cx = c->x + c->dx / 2;
cy = c->y + c->dy / 2;
}
p = 0;
for (i = 0; i < nmonitors; i++) {
m = monitorinfo[i];
if (
c && 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 monitor, 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;
}
void wrangle(Client* c, XRRMonitorInfo monitor) {
if (!c)
return;
/* If it's bigger than the screen, try to set it maximized */
if (c->dx >= monitor.width && c->dy >= monitor.height) {
quickreshape(c, monitor.x, monitor.y, monitor.width, monitor.height, 1);
/* and if it's got an edge out of bounds, nudge it into bounds */
} else {
if (c->x < monitor.x + BORDER) {
quickreshape(
c,
monitor.x,
c->y - BORDER,
c->dx + 2 * BORDER,
c->dy + 2 * BORDER,
0);
}
if (c->y < monitor.y + BORDER) {
quickreshape(
c,
c->x - BORDER,
monitor.y,
c->dx + 2 * BORDER,
c->dy + 2 * BORDER,
0);
}
if (c->x + c->dx + BORDER > monitor.x + monitor.width) {
quickreshape(
c,
monitor.x + monitor.width - (c->dx + 2 * BORDER),
c->y - BORDER,
c->dx + 2 * BORDER,
c->dy + 2 * BORDER,
0);
}
if (c->y + c->dy + BORDER > monitor.y + monitor.height) {
quickreshape(
c,
c->x - BORDER,
monitor.y + monitor.height - (c->dy + 2 * BORDER),
c->dx + 2 * BORDER,
c->dy + 2 * BORDER,
0);
}
}
}