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