]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/x11/display.cpp
Try to guess the primary monitor if xrandr isn't making any sense
[libs/gui.git] / source / graphics / x11 / display.cpp
1 #include <iostream>
2 #include <X11/Xlib.h>
3 #ifdef WITH_XRANDR
4 #include <X11/extensions/Xrandr.h>
5 #endif
6 #include <msp/io/print.h>
7 #include <msp/strings/format.h>
8 #include <msp/strings/lexicalcast.h>
9 #include "display.h"
10 #include "display_private.h"
11
12 using namespace std;
13
14 namespace {
15
16 bool error_flag = false;
17 std::string error_msg;
18
19 int x_error_handler(Display *display, XErrorEvent *event)
20 {
21         char err[128];
22         XGetErrorText(display, event->error_code, err, sizeof(err));
23
24         string request_code = Msp::lexical_cast<string, int>(event->request_code);
25         char req[128];
26         XGetErrorDatabaseText(display, "XRequest", request_code.c_str(), request_code.c_str(), req, sizeof(req));
27
28         string msg = Msp::format("Request %s failed with %s [%08X]", req, err, event->resourceid);
29         if(error_flag)
30                 Msp::IO::print(Msp::IO::cerr, "Discarding error: %s\n", msg);
31         else
32         {
33                 Msp::IO::print(Msp::IO::cerr, "%s\n", msg);
34                 error_msg = msg;
35                 error_flag = true;
36         }
37
38         return 0;
39 }
40
41 inline Msp::Graphics::VideoRotation rotation_from_sys(Rotation r)
42 {
43         switch(r)
44         {
45         case RR_Rotate_90: return Msp::Graphics::ROTATE_RIGHT;
46         case RR_Rotate_180: return Msp::Graphics::ROTATE_INVERTED;
47         case RR_Rotate_270: return Msp::Graphics::ROTATE_LEFT;
48         default: return Msp::Graphics::ROTATE_NORMAL;
49         }
50 }
51
52 inline Rotation rotation_to_sys(Msp::Graphics::VideoRotation r)
53 {
54         switch(r)
55         {
56         case Msp::Graphics::ROTATE_RIGHT: return RR_Rotate_90;
57         case Msp::Graphics::ROTATE_INVERTED: return RR_Rotate_180;
58         case Msp::Graphics::ROTATE_LEFT: return RR_Rotate_270;
59         default: return RR_Rotate_0;
60         }
61 }
62
63 bool monitor_x_compare(const Msp::Graphics::Monitor &m1, const Msp::Graphics::Monitor &m2)
64 {
65         if(m1.desktop_mode && !m2.desktop_mode)
66                 return true;
67         return m1.x<m2.x;
68 }
69
70 inline unsigned mode_width(const Msp::Graphics::VideoMode &m, Msp::Graphics::VideoRotation r)
71 {
72         if(r==Msp::Graphics::ROTATE_RIGHT || r==Msp::Graphics::ROTATE_LEFT)
73                 return m.height;
74         else
75                 return m.width;
76 }
77
78 }
79
80
81 namespace Msp {
82 namespace Graphics {
83
84 Display::Display(const string &disp_name):
85         primary_monitor(0),
86         priv(new Private)
87 {
88         if(disp_name.empty())
89                 priv->display = XOpenDisplay(0);
90         else
91                 priv->display = XOpenDisplay(disp_name.c_str());
92         if(!priv->display)
93                 throw runtime_error("XOpenDisplay");
94
95         XSetErrorHandler(x_error_handler);
96
97 #ifdef WITH_XRANDR
98         int event_base;
99         int error_base;
100         if(XRRQueryExtension(priv->display, &event_base, &error_base))
101         {
102                 int major, minor;
103                 XRRQueryVersion(priv->display, &major, &minor);
104                 if(major>1 || (major==1 && minor>=2))
105                 {
106                         WindowHandle root = DefaultRootWindow(priv->display);
107                         XRRScreenResources *res = XRRGetScreenResources(priv->display, root);
108                         RROutput primary = XRRGetOutputPrimary(priv->display, root);
109
110                         map<RRMode, XRRModeInfo *> modes_by_id;
111                         for(int i=0; i<res->nmode; ++i)
112                                 modes_by_id[res->modes[i].id] = &res->modes[i];
113
114                         for(int i=0; i<res->noutput; ++i)
115                         {
116                                 XRROutputInfo *output = XRRGetOutputInfo(priv->display, res, res->outputs[i]);
117                                 XRRCrtcInfo *crtc = (output->crtc ? XRRGetCrtcInfo(priv->display, res, output->crtc) : 0);
118
119                                 monitors.push_back(Monitor());
120                                 Monitor &monitor = monitors.back();
121                                 monitor.index = monitors.size()-1;
122                                 priv->monitors.push_back(res->outputs[i]);
123
124                                 if(crtc)
125                                 {
126                                         monitor.desktop_rotation = rotation_from_sys(crtc->rotation);
127                                         monitor.current_rotation = monitor.desktop_rotation;
128                                         monitor.x = crtc->x;
129                                         monitor.y = crtc->y;
130                                 }
131
132                                 if(res->outputs[i]==primary)
133                                         primary_monitor = &monitor;
134
135                                 for(int j=0; j<output->nmode; ++j)
136                                 {
137                                         map<RRMode, XRRModeInfo *>::iterator k = modes_by_id.find(output->modes[j]);
138                                         if(k==modes_by_id.end())
139                                                 continue;
140
141                                         XRRModeInfo *info = k->second;
142
143                                         VideoMode mode(info->width, info->height);
144                                         mode.index = modes.size();
145                                         mode.monitor = &monitor;
146                                         mode.rate = info->dotClock/(info->hTotal*info->vTotal);
147                                         if(find_matching_mode(mode))
148                                                 continue;
149
150                                         modes.push_back(mode);
151                                         priv->modes.push_back(info->id);
152                                         monitor.video_modes.push_back(&modes.back());
153
154                                         if(crtc && info->id==crtc->mode)
155                                         {
156                                                 monitor.desktop_mode = &modes.back();
157                                                 monitor.current_mode = monitor.desktop_mode;
158                                         }
159                                 }
160
161                                 XRRFreeOutputInfo(output);
162                                 if(crtc)
163                                         XRRFreeCrtcInfo(crtc);
164                         }
165
166                         XRRFreeScreenResources(res);
167
168                         monitors.sort(monitor_x_compare);
169                         Monitor *prev_enabled = 0;
170                         for(list<Monitor>::iterator i=monitors.begin(); i!=monitors.end(); ++i)
171                                 if(i->desktop_mode)
172                                 {
173                                         i->next_left = prev_enabled;
174                                         if(prev_enabled)
175                                                 prev_enabled->next_right = &*i;
176                                         prev_enabled = &*i;
177                                 }
178
179                         if(!primary_monitor || !primary_monitor->desktop_mode)
180                         {
181                                 // XRandR didn't give a sensible primary monitor.  Try to guess one.
182                                 unsigned largest = 0;
183                                 for(list<Monitor>::iterator i=monitors.begin(); i!=monitors.end(); ++i)
184                                         if(i->desktop_mode)
185                                         {
186                                                 unsigned size = i->desktop_mode->width*i->desktop_mode->height;
187                                                 if(size>largest)
188                                                 {
189                                                         largest = size;
190                                                         primary_monitor = &*i;
191                                                 }
192                                         }
193                         }
194                 }
195         }
196 #endif
197 }
198
199 Display::~Display()
200 {
201         XCloseDisplay(priv->display);
202         delete priv;
203 }
204
205 void Display::set_mode(const VideoMode &requested_mode, bool exclusive)
206 {
207 #ifdef WITH_XRANDR
208         const VideoMode *mode = find_matching_mode(requested_mode);
209         if(!mode)
210                 throw unsupported_video_mode(requested_mode);
211
212         VideoRotation requested_rotation = requested_mode.rotation;
213         if(requested_rotation==ROTATE_ANY)
214                 requested_rotation = mode->monitor->desktop_rotation;
215
216         WindowHandle root = DefaultRootWindow(priv->display);
217         XRRScreenResources *res = XRRGetScreenResources(priv->display, root);
218         RROutput output = priv->monitors[mode->monitor->index];
219         XRROutputInfo *output_info = XRRGetOutputInfo(priv->display, res, output);
220
221         // Check if the output already has a CRTC and find a free one if it doesn't
222         RRCrtc crtc = output_info->crtc;
223         XRRCrtcInfo *crtc_info = 0;
224         if(crtc)
225                 crtc_info = XRRGetCrtcInfo(priv->display, res, crtc);
226         else
227         {
228                 for(int i=0; i<res->ncrtc; ++i)
229                 {
230                         crtc_info = XRRGetCrtcInfo(priv->display, res, res->crtcs[i]);
231                         if(!crtc_info->noutput)
232                         {
233                                 crtc = res->crtcs[i];
234                                 break;
235                         }
236                         XRRFreeCrtcInfo(crtc_info);
237                 }
238
239                 if(!crtc)
240                 {
241                         XRRFreeOutputInfo(output_info);
242                         throw unsupported_video_mode(requested_mode);
243                 }
244         }
245
246         int x = 0;
247         int y = 0;
248
249         if(exclusive)
250         {
251                 // Disable other outputs for exclusive mode
252                 for(list<Monitor>::iterator i=monitors.begin(); i!=monitors.end(); ++i)
253                         if(&*i!=mode->monitor)
254                         {
255                                 XRROutputInfo *o = XRRGetOutputInfo(priv->display, res, priv->monitors[i->index]);
256                                 if(o->crtc)
257                                         XRRSetCrtcConfig(priv->display, res, o->crtc, CurrentTime, 0, 0, 0, RR_Rotate_0, 0, 0);
258                                 XRRFreeOutputInfo(o);
259
260                                 i->current_mode = 0;
261                                 i->current_rotation = ROTATE_NORMAL;
262                                 i->x = 0;
263                                 i->y = 0;
264                         }
265         }
266         else
267         {
268                 const Monitor *left = mode->monitor->next_left;
269                 while(left && !left->current_mode)
270                         left = left->next_left;
271
272                 if(left)
273                 {
274                         x = left->x+mode_width(*left->current_mode, left->current_rotation);
275                         y = left->y;
276                 }
277         }
278
279         XRRSetCrtcConfig(priv->display, res, crtc, CurrentTime, x, y, priv->modes[mode->index], rotation_to_sys(requested_rotation), &output, 1);
280
281         list<Monitor>::iterator i;
282         for(i=monitors.begin(); i!=monitors.end(); ++i)
283                 if(&*i==mode->monitor)
284                 {
285                         i->current_mode = mode;
286                         i->current_rotation = requested_rotation;
287                         i->x = x;
288                         i->y = y;
289
290                         x += mode_width(*mode, requested_rotation);
291                         ++i;
292                         break;
293                 }
294
295         for(; i!=monitors.end(); ++i)
296                 if(i->current_mode)
297                 {
298                         XRROutputInfo *o = XRRGetOutputInfo(priv->display, res, priv->monitors[i->index]);
299                         XRRSetCrtcConfig(priv->display, res, o->crtc, CurrentTime, x, y, priv->modes[i->current_mode->index], rotation_to_sys(i->current_rotation), &priv->monitors[i->index], 1);
300                         XRRFreeOutputInfo(o);
301
302                         i->x = x;
303                         i->y = y;
304
305                         x += mode_width(*i->current_mode, i->current_rotation);
306                 }
307
308         XRRFreeOutputInfo(output_info);
309         XRRFreeCrtcInfo(crtc_info);
310         XRRFreeScreenResources(res);
311 #else
312         (void)requested_mode;
313         (void)exclusive;
314         throw runtime_error("no xrandr support");
315 #endif
316 }
317
318 bool Display::process_events()
319 {
320         int pending = XPending(priv->display);
321         if(pending==0)
322                 return false;
323
324         for(; pending--;)
325         {
326                 Window::Event event;
327                 XNextEvent(priv->display, &event.xevent);
328
329                 check_error();
330
331                 map<WindowHandle, Window *>::iterator j = priv->windows.find(event.xevent.xany.window);
332                 if(j!=priv->windows.end())
333                 {
334                         /* Filter keyboard autorepeat.  If this packet is a KeyRelease and
335                         the next one is a KeyPress with the exact same parameters, they
336                         indicate autorepeat and must be dropped. */
337                         if(event.xevent.type==KeyRelease && !j->second->get_keyboard_autorepeat() && pending>0)
338                         {
339                                 XKeyEvent &kev = event.xevent.xkey;
340                                 XEvent ev2;
341                                 XPeekEvent(priv->display, &ev2);
342                                 if(ev2.type==KeyPress)
343                                 {
344                                         XKeyEvent &kev2 = ev2.xkey;
345                                         if(kev2.window==kev.window && kev2.time==kev.time && kev2.keycode==kev.keycode)
346                                         {
347                                                 XNextEvent(priv->display, &ev2);
348                                                 --pending;
349                                                 continue;
350                                         }
351                                 }
352                         }
353
354                         j->second->event(event);
355                 }
356         }
357
358         return true;
359 }
360
361 void Display::check_error()
362 {
363         if(error_flag)
364         {
365                 error_flag = false;
366                 throw runtime_error(error_msg);
367         }
368 }
369
370 } // namespace Msp
371 } // namespace Graphics