all repos — ryudo @ de0bc0397ac6302cb4e11c53097c77abcfb30eb5

the floatiling window manager that flows; fork of rio from plan9port

monitor.c (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#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);
    }
  }
}