summaryrefslogtreecommitdiff
path: root/src/xrandr.c
blob: 5ddf082f070367b23aafdf9979b1117e3c5651f1 (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* functions for grabbing information about xrandr screens
 * Copyright (C) 2016 Mathieu OTHACEHE <m.othacehe@gmail.com>
 *
 * This file is part of ratpoison.
 *
 * ratpoison is free software; you can redistribute it and/or moify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * ratpoison is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this software; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307 USA
 */

#include "ratpoison.h"

#ifdef HAVE_XRANDR

#include <X11/extensions/Xrandr.h>

static int xrandr_evbase;

#define XRANDR_MAJOR 1
#define XRANDR_MINOR 3

void
init_xrandr (void)
{
  int errbase, major, minor;

  if (!XRRQueryExtension (dpy, &xrandr_evbase, &errbase)) {
    return;
  }

  if (XRRQueryVersion (dpy, &major, &minor) == 0) {
    return;
  }

  if (major != XRANDR_MAJOR ||
      (major == XRANDR_MAJOR && minor < XRANDR_MINOR)) {
    PRINT_ERROR (("Xrandr version %d.%d is not supported\n", major, minor));
    return;
  }

  XRRSelectInput (dpy, RootWindow (dpy, DefaultScreen(dpy)),
                  RRCrtcChangeNotifyMask | RROutputChangeNotifyMask);

  rp_have_xrandr = 1;
}

int
xrandr_query_screen (int **outputs)
{
  XRRScreenResources *res;
  XRROutputInfo *outinfo;
  int *output_array;
  int count = 0;
  int i;

  res = XRRGetScreenResources (dpy, RootWindow (dpy, DefaultScreen (dpy)));
  output_array = xmalloc (res->noutput * sizeof(int));

  for (i = 0; i < res->noutput; i++) {
    outinfo = XRRGetOutputInfo (dpy, res, res->outputs[i]);
    if (!outinfo->crtc)
      continue;

    output_array[count++] = res->outputs[i];

    XRRFreeOutputInfo (outinfo);
  }

  XRRFreeScreenResources (res);

  *outputs = output_array;
  return count;
}

static rp_screen *
xrandr_screen_output (int rr_output)
{
  rp_screen *cur;

  list_for_each_entry (cur, &rp_screens, node)
    {
      if (cur->xrandr.output == rr_output)
        return cur;
    }

  return NULL;
}

static rp_screen *
xrandr_screen_crtc (int rr_crtc)
{
  rp_screen *cur;

  list_for_each_entry (cur, &rp_screens, node)
    {
      if (cur->xrandr.crtc == rr_crtc)
        return cur;
    }

  return NULL;
}

int
xrandr_is_primary (rp_screen *screen)
{
  return screen->xrandr.primary;
}

void
xrandr_fill_screen (int rr_output, rp_screen *screen)
{
  XRRScreenResources *res;
  XRROutputInfo *outinfo;
  XRRCrtcInfo *crtinfo;
  RROutput primary;

  res = XRRGetScreenResourcesCurrent (dpy, RootWindow (dpy, DefaultScreen (dpy)));
  outinfo = XRRGetOutputInfo (dpy, res, rr_output);
  if (!outinfo->crtc)
    goto free_res;

  crtinfo = XRRGetCrtcInfo (dpy, res, outinfo->crtc);
  if (!crtinfo)
    goto free_out;

  primary = XRRGetOutputPrimary (dpy, RootWindow (dpy, DefaultScreen (dpy)));
  if (rr_output == primary)
    screen->xrandr.primary = 1;
  else
    screen->xrandr.primary = 0;

  screen->xrandr.name = xstrdup (outinfo->name);
  screen->xrandr.output  = rr_output;
  screen->xrandr.crtc    = outinfo->crtc;

  screen->left   = crtinfo->x;
  screen->top    = crtinfo->y;
  screen->width  = crtinfo->width;
  screen->height = crtinfo->height;

  XRRFreeCrtcInfo (crtinfo);
 free_out:
  XRRFreeOutputInfo (outinfo);
 free_res:
  XRRFreeScreenResources (res);
}

static void
xrandr_output_change (XRROutputChangeNotifyEvent *ev)
{
  XRRScreenResources *res;
  XRROutputInfo *outinfo;
  rp_screen *screen;

  res = XRRGetScreenResourcesCurrent (dpy, RootWindow (dpy, DefaultScreen (dpy)));
  outinfo = XRRGetOutputInfo (dpy, res, ev->output);

  screen = xrandr_screen_output (ev->output);

  if (!screen && outinfo->crtc) {
    screen = screen_add (ev->output);
    screen_sort ();
    PRINT_DEBUG (("%s: Added screen %s with crtc %lu\n", __func__,
                  screen->xrandr.name,
                  (unsigned long)outinfo->crtc));
  } else if (screen && !outinfo->crtc) {
    PRINT_DEBUG (("%s: Removing screen %s\n", __func__,
                  screen->xrandr.name));
    screen_del (screen);
  }

  XRRFreeOutputInfo (outinfo);
  XRRFreeScreenResources (res);
}

#ifdef DEBUG
static const char *
xrandr_rotation_string (Rotation r)
{
  static char buf[64];

#define CASE(c) case c : return #c
  switch (r)
    {
      CASE(RR_Rotate_0);
      CASE(RR_Rotate_90);
      CASE(RR_Rotate_180);
      CASE(RR_Rotate_270);
#undef CASE
    default:
      snprintf(buf, sizeof buf, "Unknown rotation %hu", (unsigned short)r);
      return buf;
  }
}
#endif

static void
xrandr_crtc_change (XRRCrtcChangeNotifyEvent *ev)
{
  rp_screen *screen;

  if (!ev->crtc || !ev->width || !ev->height)
    return;

  screen = xrandr_screen_crtc (ev->crtc);

  PRINT_DEBUG (("%s: crtc %s, rotation %s "
                "ev->x %d, ev->y %d, ev->width %d, ev->height %d\n",
                __func__, screen ? "found" : "not found",
                xrandr_rotation_string (ev->rotation),
                ev->x, ev->y, ev->width, ev->height));

  if (!screen)
    return;

  if (ev->rotation == RR_Rotate_90 || ev->rotation == RR_Rotate_270)
    screen_update (screen, ev->x, ev->y, ev->height, ev->width);
  else
    screen_update (screen, ev->x, ev->y, ev->width, ev->height);
}

void
xrandr_notify (XEvent *ev)
{
  XRRNotifyEvent *n_event;
  XRROutputChangeNotifyEvent *o_event;
  XRRCrtcChangeNotifyEvent *c_event;

  if (ev->type != xrandr_evbase + RRNotify)
    return;

  PRINT_DEBUG (("--- Handling RRNotify ---\n"));

  n_event = (XRRNotifyEvent *)ev;
  switch (n_event->subtype) {
  case RRNotify_OutputChange:
    PRINT_DEBUG (("---          XRROutputChangeNotifyEvent ---\n"));
    o_event = (XRROutputChangeNotifyEvent *)ev;
    xrandr_output_change (o_event);
    break;
  case RRNotify_CrtcChange:
    PRINT_DEBUG (("---          XRRCrtcChangeNotifyEvent ---\n"));
    c_event = (XRRCrtcChangeNotifyEvent *)ev;
    xrandr_crtc_change (c_event);
    break;
  case RRNotify_OutputProperty:
    PRINT_DEBUG (("---          RRNotify_OutputProperty ---\n"));
    break;
  default:
    PRINT_DEBUG (("---          Unknown subtype %d ---\n", n_event->subtype));
    break;
  }
}

#else /* HAVE_XRANDR */

void
init_xrandr(void)
{
  /* Nothing */
}

int
xrandr_query_screen (int **outputs)
{
  (void)outputs;
  fatal("xrandr_query_screen shouldn't be called.  This is a BUG.");
  return 0;
}

int
xrandr_is_primary (rp_screen *screen)
{
  (void)screen;
  fatal("xrandr_is_primary shouldn't be called.  This is a BUG.");
  return 0;
}

void
xrandr_fill_screen (int rr_output, rp_screen *screen)
{
  (void)rr_output;
  memset(&screen->xrandr, 0, sizeof(screen->xrandr));
  screen->xrandr.primary = (rr_output == 0);
  screen->xrandr.output  = rr_output;
  screen->xrandr.name = xstrdup ("N/A");
}

void
xrandr_notify (XEvent *ev)
{
  (void)ev;
  fatal("xrandr_notify shouldn't be called.  This is a BUG.");
}

#endif /* HAVE_XRANDR */