]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/x11/display.cpp
Get monitor names from EDID if available
[libs/gui.git] / source / graphics / x11 / display.cpp
1 #include <X11/Xlib.h>
2 #include <X11/Xatom.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 #ifdef WITH_XRANDR
42 inline Msp::Graphics::VideoRotation rotation_from_sys(Rotation r)
43 {
44         switch(r)
45         {
46         case RR_Rotate_90: return Msp::Graphics::ROTATE_RIGHT;
47         case RR_Rotate_180: return Msp::Graphics::ROTATE_INVERTED;
48         case RR_Rotate_270: return Msp::Graphics::ROTATE_LEFT;
49         default: return Msp::Graphics::ROTATE_NORMAL;
50         }
51 }
52
53 inline Rotation rotation_to_sys(Msp::Graphics::VideoRotation r)
54 {
55         switch(r)
56         {
57         case Msp::Graphics::ROTATE_RIGHT: return RR_Rotate_90;
58         case Msp::Graphics::ROTATE_INVERTED: return RR_Rotate_180;
59         case Msp::Graphics::ROTATE_LEFT: return RR_Rotate_270;
60         default: return RR_Rotate_0;
61         }
62 }
63
64 inline bool monitor_x_compare(const Msp::Graphics::Monitor &m1, const Msp::Graphics::Monitor &m2)
65 {
66         if(m1.desktop_mode && !m2.desktop_mode)
67                 return true;
68         return m1.x<m2.x;
69 }
70 #endif
71
72 inline unsigned mode_width(const Msp::Graphics::VideoMode &m, Msp::Graphics::VideoRotation r)
73 {
74         if(r==Msp::Graphics::ROTATE_RIGHT || r==Msp::Graphics::ROTATE_LEFT)
75                 return m.height;
76         else
77                 return m.width;
78 }
79
80 }
81
82
83 namespace Msp {
84 namespace Graphics {
85
86 Display::Display(const string &disp_name):
87         primary_monitor(0),
88         priv(new Private),
89         focus_window(0)
90 {
91         if(disp_name.empty())
92                 priv->display = XOpenDisplay(0);
93         else
94                 priv->display = XOpenDisplay(disp_name.c_str());
95         if(!priv->display)
96                 throw runtime_error("XOpenDisplay");
97
98         XSetErrorHandler(x_error_handler);
99
100         priv->root_window = DefaultRootWindow(priv->display);
101
102         err_dialog = new ErrorDialog(this);
103
104 #ifdef WITH_XRANDR
105         int event_base;
106         int error_base;
107         if(XRRQueryExtension(priv->display, &event_base, &error_base))
108         {
109                 int major, minor;
110                 XRRQueryVersion(priv->display, &major, &minor);
111                 if(major>1 || (major==1 && minor>=2))
112                 {
113                         XRRScreenResources *res = XRRGetScreenResources(priv->display, priv->root_window);
114                         RROutput primary = XRRGetOutputPrimary(priv->display, priv->root_window);
115                         Atom edid_prop = XInternAtom(priv->display, RR_PROPERTY_RANDR_EDID, true);
116
117                         map<RRMode, XRRModeInfo *> modes_by_id;
118                         for(int i=0; i<res->nmode; ++i)
119                                 modes_by_id[res->modes[i].id] = &res->modes[i];
120
121                         for(int i=0; i<res->noutput; ++i)
122                         {
123                                 XRROutputInfo *output = XRRGetOutputInfo(priv->display, res, res->outputs[i]);
124                                 XRRCrtcInfo *crtc = (output->crtc ? XRRGetCrtcInfo(priv->display, res, output->crtc) : 0);
125
126                                 monitors.push_back(Monitor());
127                                 Monitor &monitor = monitors.back();
128                                 monitor.index = monitors.size()-1;
129                                 monitor.name.assign(output->name, output->nameLen);
130                                 priv->monitors.push_back(res->outputs[i]);
131
132                                 if(edid_prop)
133                                 {
134                                         Atom prop_type;
135                                         int prop_format;
136                                         unsigned long length;
137                                         unsigned long overflow;
138                                         unsigned char *edid = 0;
139                                         XRRGetOutputProperty(priv->display, res->outputs[i], edid_prop, 0, 32, false, false, XA_INTEGER, &prop_type, &prop_format, &length, &overflow, &edid);
140                                         if(prop_type==XA_INTEGER && prop_format==8)
141                                         {
142                                                 for(unsigned j=0; j<4; ++j)
143                                                 {
144                                                         unsigned offset = 54+j*18;
145                                                         if(edid[offset]==0 && edid[offset+1]==0 && edid[offset+3]==0xFC)
146                                                         {
147                                                                 unsigned k;
148                                                                 for(k=0; (k<13 && edid[offset+5+k]!=0x0A); ++k) ;
149                                                                 monitor.name.assign(reinterpret_cast<char *>(edid+offset+5), k);
150                                                         }
151                                                 }
152                                         }
153                                         XFree(edid);
154                                 }
155
156                                 if(crtc)
157                                 {
158                                         monitor.desktop_rotation = rotation_from_sys(crtc->rotation);
159                                         monitor.current_rotation = monitor.desktop_rotation;
160                                         monitor.x = crtc->x;
161                                         monitor.y = crtc->y;
162                                 }
163
164                                 if(res->outputs[i]==primary)
165                                         primary_monitor = &monitor;
166
167                                 for(int j=0; j<output->nmode; ++j)
168                                 {
169                                         map<RRMode, XRRModeInfo *>::iterator k = modes_by_id.find(output->modes[j]);
170                                         if(k==modes_by_id.end())
171                                                 continue;
172
173                                         XRRModeInfo *info = k->second;
174
175                                         VideoMode mode(info->width, info->height);
176                                         mode.index = modes.size();
177                                         mode.monitor = &monitor;
178                                         mode.rate = static_cast<float>(info->dotClock)/(info->hTotal*info->vTotal);
179                                         if(find_mode(mode, 0.01f))
180                                                 continue;
181
182                                         modes.push_back(mode);
183                                         priv->modes.push_back(info->id);
184                                         monitor.video_modes.push_back(&modes.back());
185
186                                         if(crtc && info->id==crtc->mode)
187                                         {
188                                                 monitor.desktop_mode = &modes.back();
189                                                 monitor.current_mode = monitor.desktop_mode;
190                                         }
191                                 }
192
193                                 XRRFreeOutputInfo(output);
194                                 if(crtc)
195                                         XRRFreeCrtcInfo(crtc);
196                         }
197
198                         XRRFreeScreenResources(res);
199
200                         monitors.sort(monitor_x_compare);
201                         Monitor *prev_enabled = 0;
202                         for(list<Monitor>::iterator i=monitors.begin(); i!=monitors.end(); ++i)
203                                 if(i->desktop_mode)
204                                 {
205                                         i->next_left = prev_enabled;
206                                         if(prev_enabled)
207                                                 prev_enabled->next_right = &*i;
208                                         prev_enabled = &*i;
209                                 }
210
211                         if(!primary_monitor || !primary_monitor->desktop_mode)
212                         {
213                                 // XRandR didn't give a sensible primary monitor.  Try to guess one.
214                                 unsigned largest = 0;
215                                 for(list<Monitor>::iterator i=monitors.begin(); i!=monitors.end(); ++i)
216                                         if(i->desktop_mode)
217                                         {
218                                                 unsigned size = i->desktop_mode->width*i->desktop_mode->height;
219                                                 if(size>largest)
220                                                 {
221                                                         largest = size;
222                                                         primary_monitor = &*i;
223                                                 }
224                                         }
225                         }
226                 }
227         }
228 #endif
229 }
230
231 Display::~Display()
232 {
233         XCloseDisplay(priv->display);
234         delete priv;
235         delete err_dialog;
236 }
237
238 void Display::set_mode(const VideoMode &requested_mode, bool exclusive)
239 {
240 #ifdef WITH_XRANDR
241         const VideoMode *mode = find_mode(requested_mode);
242         if(!mode)
243                 throw unsupported_video_mode(requested_mode);
244
245         VideoRotation requested_rotation = requested_mode.rotation;
246         if(requested_rotation==ROTATE_ANY)
247                 requested_rotation = mode->monitor->desktop_rotation;
248
249         XRRScreenResources *res = XRRGetScreenResources(priv->display, priv->root_window);
250         RROutput output = priv->monitors[mode->monitor->index];
251         XRROutputInfo *output_info = XRRGetOutputInfo(priv->display, res, output);
252
253         // Check if the output already has a CRTC and find a free one if it doesn't
254         RRCrtc crtc = output_info->crtc;
255         XRRCrtcInfo *crtc_info = 0;
256         if(crtc)
257                 crtc_info = XRRGetCrtcInfo(priv->display, res, crtc);
258         else
259         {
260                 for(int i=0; i<res->ncrtc; ++i)
261                 {
262                         crtc_info = XRRGetCrtcInfo(priv->display, res, res->crtcs[i]);
263                         if(!crtc_info->noutput)
264                         {
265                                 crtc = res->crtcs[i];
266                                 break;
267                         }
268                         XRRFreeCrtcInfo(crtc_info);
269                 }
270
271                 if(!crtc)
272                 {
273                         XRRFreeOutputInfo(output_info);
274                         throw unsupported_video_mode(requested_mode);
275                 }
276         }
277
278         int x = 0;
279         int y = 0;
280
281         if(exclusive)
282         {
283                 // Disable other outputs for exclusive mode
284                 for(list<Monitor>::iterator i=monitors.begin(); i!=monitors.end(); ++i)
285                         if(&*i!=mode->monitor)
286                         {
287                                 XRROutputInfo *o = XRRGetOutputInfo(priv->display, res, priv->monitors[i->index]);
288                                 if(o->crtc)
289                                         XRRSetCrtcConfig(priv->display, res, o->crtc, CurrentTime, 0, 0, 0, RR_Rotate_0, 0, 0);
290                                 XRRFreeOutputInfo(o);
291
292                                 i->current_mode = 0;
293                                 i->current_rotation = ROTATE_NORMAL;
294                                 i->x = 0;
295                                 i->y = 0;
296                         }
297         }
298         else
299         {
300                 const Monitor *left = mode->monitor->next_left;
301                 while(left && !left->current_mode)
302                         left = left->next_left;
303
304                 if(left)
305                 {
306                         x = left->x+mode_width(*left->current_mode, left->current_rotation);
307                         y = left->y;
308                 }
309         }
310
311         XRRSetCrtcConfig(priv->display, res, crtc, CurrentTime, x, y, priv->modes[mode->index], rotation_to_sys(requested_rotation), &output, 1);
312
313         list<Monitor>::iterator i;
314         for(i=monitors.begin(); i!=monitors.end(); ++i)
315                 if(&*i==mode->monitor)
316                 {
317                         i->current_mode = mode;
318                         i->current_rotation = requested_rotation;
319                         i->x = x;
320                         i->y = y;
321
322                         x += mode_width(*mode, requested_rotation);
323                         ++i;
324                         break;
325                 }
326
327         for(; i!=monitors.end(); ++i)
328                 if(i->current_mode)
329                 {
330                         XRROutputInfo *o = XRRGetOutputInfo(priv->display, res, priv->monitors[i->index]);
331                         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);
332
333                         XRRPanning panning;
334                         panning.timestamp = CurrentTime;
335                         panning.left = x;
336                         panning.top = y;
337                         panning.width = i->current_mode->width;
338                         panning.height = i->current_mode->height;
339                         panning.track_left = panning.left;
340                         panning.track_top = panning.top;
341                         panning.track_width = panning.width;
342                         panning.track_height = panning.height;
343                         panning.border_left = 0;
344                         panning.border_top = 0;
345                         panning.border_right = 0;
346                         panning.border_bottom = 0;
347                         XRRSetPanning(priv->display, res, o->crtc, &panning);
348
349                         XRRFreeOutputInfo(o);
350
351                         i->x = x;
352                         i->y = y;
353
354                         x += mode_width(*i->current_mode, i->current_rotation);
355                 }
356
357         XRRFreeOutputInfo(output_info);
358         XRRFreeCrtcInfo(crtc_info);
359         XRRFreeScreenResources(res);
360 #else
361         (void)requested_mode;
362         (void)exclusive;
363         throw runtime_error("no xrandr support");
364 #endif
365 }
366
367 bool Display::process_events()
368 {
369         int pending = XPending(priv->display);
370         if(pending==0)
371                 return false;
372
373         for(; pending--;)
374         {
375                 Window::Event event;
376                 XNextEvent(priv->display, &event.xevent);
377
378                 check_error();
379
380                 map<WindowHandle, Window *>::iterator j = priv->windows.find(event.xevent.xany.window);
381                 if(j!=priv->windows.end())
382                 {
383                         /* Filter keyboard autorepeat.  If this packet is a KeyRelease and
384                         the next one is a KeyPress with the exact same parameters, they
385                         indicate autorepeat and must be dropped. */
386                         if(event.xevent.type==KeyRelease && !j->second->get_keyboard_autorepeat() && pending>0)
387                         {
388                                 XKeyEvent &kev = event.xevent.xkey;
389                                 XEvent ev2;
390                                 XPeekEvent(priv->display, &ev2);
391                                 if(ev2.type==KeyPress)
392                                 {
393                                         XKeyEvent &kev2 = ev2.xkey;
394                                         if(kev2.window==kev.window && kev2.time==kev.time && kev2.keycode==kev.keycode)
395                                         {
396                                                 XNextEvent(priv->display, &ev2);
397                                                 --pending;
398                                                 continue;
399                                         }
400                                 }
401                         }
402
403                         j->second->event(event);
404                 }
405         }
406
407         return true;
408 }
409
410 void Display::check_error()
411 {
412         if(error_flag)
413         {
414                 error_flag = false;
415                 throw runtime_error(error_msg);
416         }
417 }
418
419 } // namespace Msp
420 } // namespace Graphics