2022-02-27 17:18:17 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#include <X11/extensions/Xrandr.h>
|
|
|
|
#include "dat.h"
|
|
|
|
#include "fns.h"
|
2022-03-01 06:40:32 +00:00
|
|
|
#include "config.h"
|
2022-02-27 17:18:17 +00:00
|
|
|
|
|
|
|
int nmonitors = 0;
|
|
|
|
XRRMonitorInfo* monitorinfo;
|
2024-11-29 16:52:32 +00:00
|
|
|
int* xstep;
|
|
|
|
int* ystep;
|
2022-02-27 17:18:17 +00:00
|
|
|
|
|
|
|
void fetchmonitorinfo() {
|
|
|
|
if (monitorinfo)
|
|
|
|
XRRFreeMonitors(monitorinfo);
|
2024-11-29 16:52:32 +00:00
|
|
|
if (xstep)
|
|
|
|
free(xstep);
|
|
|
|
if (ystep)
|
|
|
|
free(ystep);
|
2022-02-27 17:18:17 +00:00
|
|
|
|
|
|
|
monitorinfo = XRRGetMonitors(dpy, DefaultRootWindow(dpy), 1, &nmonitors);
|
2024-11-29 16:52:32 +00:00
|
|
|
xstep = malloc(nmonitors * sizeof(int));
|
|
|
|
ystep = malloc(nmonitors * sizeof(int));
|
|
|
|
for (int i = 0; i < nmonitors; i++) {
|
|
|
|
xstep[i] = monitorinfo[i].width / CASCADE_DENSITY;
|
|
|
|
ystep[i] = monitorinfo[i].height / CASCADE_DENSITY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void getcascadecoords(Window w, int monitor, int* x, int* y) {
|
|
|
|
int mappedclients = 0;
|
|
|
|
Client* c;
|
|
|
|
int ox = monitorinfo[monitor].x;
|
|
|
|
int oy = monitorinfo[monitor].y;
|
|
|
|
|
|
|
|
if (w == 0)
|
|
|
|
return;
|
|
|
|
for (c = clients; c; c = c->next) {
|
|
|
|
if (
|
|
|
|
c->window != w && c->state == NormalState &&
|
|
|
|
getmonitorbyclient(c) == monitor) {
|
|
|
|
mappedclients++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
if (mappedclients < CASCADE_DENSITY) {
|
|
|
|
*x = ox + mappedclients * xstep[monitor];
|
|
|
|
*y = oy + mappedclients * ystep[monitor];
|
|
|
|
return;
|
|
|
|
} else if (mappedclients < 1.5 * CASCADE_DENSITY) {
|
|
|
|
*x = ox + (mappedclients - CASCADE_DENSITY + 1) * xstep[monitor];
|
|
|
|
*y = oy + (mappedclients - CASCADE_DENSITY) * ystep[monitor];
|
|
|
|
return;
|
|
|
|
} else if (mappedclients < 2 * CASCADE_DENSITY) {
|
|
|
|
*x = ox + (mappedclients - 1.5 * CASCADE_DENSITY) * xstep[monitor];
|
|
|
|
*y = oy + (mappedclients - 1.5 * CASCADE_DENSITY + 1) * ystep[monitor];
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
mappedclients -= 2 * CASCADE_DENSITY;
|
|
|
|
}
|
|
|
|
}
|
2022-02-27 17:18:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int getmonitorbyclient(Client* c) {
|
|
|
|
XRRMonitorInfo m;
|
|
|
|
int cx, cy, i, p;
|
2022-03-01 06:40:32 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-02-27 17:18:17 +00:00
|
|
|
|
|
|
|
p = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < nmonitors; i++) {
|
|
|
|
m = monitorinfo[i];
|
2022-03-01 06:40:32 +00:00
|
|
|
if (
|
|
|
|
c && cx >= m.x && cx < m.x + m.width && cy >= m.y &&
|
|
|
|
cy < m.y + m.height) {
|
2022-02-27 17:18:17 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m.primary) {
|
|
|
|
p = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-01 06:40:32 +00:00
|
|
|
/* if center is not within any monitor, return primary */
|
2022-02-27 17:18:17 +00:00
|
|
|
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;
|
2022-03-01 06:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|