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