]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/x11/display.cpp
368b67cecdc5b6953db9e113664e16f30bed9889
[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         }
180 #endif
181 }
182
183 Display::~Display()
184 {
185         XCloseDisplay(priv->display);
186         delete priv;
187 }
188
189 void Display::set_mode(const VideoMode &requested_mode, bool exclusive)
190 {
191 #ifdef WITH_XRANDR
192         const VideoMode *mode = find_matching_mode(requested_mode);
193         if(!mode)
194                 throw unsupported_video_mode(requested_mode);
195
196         VideoRotation requested_rotation = requested_mode.rotation;
197         if(requested_rotation==ROTATE_ANY)
198                 requested_rotation = mode->monitor->desktop_rotation;
199
200         WindowHandle root = DefaultRootWindow(priv->display);
201         XRRScreenResources *res = XRRGetScreenResources(priv->display, root);
202         RROutput output = priv->monitors[mode->monitor->index];
203         XRROutputInfo *output_info = XRRGetOutputInfo(priv->display, res, output);
204
205         // Check if the output already has a CRTC and find a free one if it doesn't
206         RRCrtc crtc = output_info->crtc;
207         XRRCrtcInfo *crtc_info = 0;
208         if(crtc)
209                 crtc_info = XRRGetCrtcInfo(priv->display, res, crtc);
210         else
211         {
212                 for(int i=0; i<res->ncrtc; ++i)
213                 {
214                         crtc_info = XRRGetCrtcInfo(priv->display, res, res->crtcs[i]);
215                         if(!crtc_info->noutput)
216                         {
217                                 crtc = res->crtcs[i];
218                                 break;
219                         }
220                         XRRFreeCrtcInfo(crtc_info);
221                 }
222
223                 if(!crtc)
224                 {
225                         XRRFreeOutputInfo(output_info);
226                         throw unsupported_video_mode(requested_mode);
227                 }
228         }
229
230         int x = 0;
231         int y = 0;
232
233         if(exclusive)
234         {
235                 // Disable other outputs for exclusive mode
236                 for(list<Monitor>::iterator i=monitors.begin(); i!=monitors.end(); ++i)
237                         if(&*i!=mode->monitor)
238                         {
239                                 XRROutputInfo *o = XRRGetOutputInfo(priv->display, res, priv->monitors[i->index]);
240                                 if(o->crtc)
241                                         XRRSetCrtcConfig(priv->display, res, o->crtc, CurrentTime, 0, 0, 0, RR_Rotate_0, 0, 0);
242                                 XRRFreeOutputInfo(o);
243
244                                 i->current_mode = 0;
245                                 i->current_rotation = ROTATE_NORMAL;
246                                 i->x = 0;
247                                 i->y = 0;
248                         }
249         }
250         else
251         {
252                 const Monitor *left = mode->monitor->next_left;
253                 while(left && !left->current_mode)
254                         left = left->next_left;
255
256                 if(left)
257                 {
258                         x = left->x+mode_width(*left->current_mode, left->current_rotation);
259                         y = left->y;
260                 }
261         }
262
263         XRRSetCrtcConfig(priv->display, res, crtc, CurrentTime, x, y, priv->modes[mode->index], rotation_to_sys(requested_rotation), &output, 1);
264
265         list<Monitor>::iterator i;
266         for(i=monitors.begin(); i!=monitors.end(); ++i)
267                 if(&*i==mode->monitor)
268                 {
269                         i->current_mode = mode;
270                         i->current_rotation = requested_rotation;
271                         i->x = x;
272                         i->y = y;
273
274                         x += mode_width(*mode, requested_rotation);
275                         ++i;
276                         break;
277                 }
278
279         for(; i!=monitors.end(); ++i)
280                 if(i->current_mode)
281                 {
282                         XRROutputInfo *o = XRRGetOutputInfo(priv->display, res, priv->monitors[i->index]);
283                         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);
284                         XRRFreeOutputInfo(o);
285
286                         i->x = x;
287                         i->y = y;
288
289                         x += mode_width(*i->current_mode, i->current_rotation);
290                 }
291
292         XRRFreeOutputInfo(output_info);
293         XRRFreeCrtcInfo(crtc_info);
294         XRRFreeScreenResources(res);
295 #else
296         (void)requested_mode;
297         (void)exclusive;
298         throw runtime_error("no xrandr support");
299 #endif
300 }
301
302 bool Display::process_events()
303 {
304         int pending = XPending(priv->display);
305         if(pending==0)
306                 return false;
307
308         for(; pending--;)
309         {
310                 Window::Event event;
311                 XNextEvent(priv->display, &event.xevent);
312
313                 check_error();
314
315                 map<WindowHandle, Window *>::iterator j = priv->windows.find(event.xevent.xany.window);
316                 if(j!=priv->windows.end())
317                 {
318                         /* Filter keyboard autorepeat.  If this packet is a KeyRelease and
319                         the next one is a KeyPress with the exact same parameters, they
320                         indicate autorepeat and must be dropped. */
321                         if(event.xevent.type==KeyRelease && !j->second->get_keyboard_autorepeat() && pending>0)
322                         {
323                                 XKeyEvent &kev = event.xevent.xkey;
324                                 XEvent ev2;
325                                 XPeekEvent(priv->display, &ev2);
326                                 if(ev2.type==KeyPress)
327                                 {
328                                         XKeyEvent &kev2 = ev2.xkey;
329                                         if(kev2.window==kev.window && kev2.time==kev.time && kev2.keycode==kev.keycode)
330                                         {
331                                                 XNextEvent(priv->display, &ev2);
332                                                 --pending;
333                                                 continue;
334                                         }
335                                 }
336                         }
337
338                         j->second->event(event);
339                 }
340         }
341
342         return true;
343 }
344
345 void Display::check_error()
346 {
347         if(error_flag)
348         {
349                 error_flag = false;
350                 throw runtime_error(error_msg);
351         }
352 }
353
354 } // namespace Msp
355 } // namespace Graphics