2 #include "display_private.h"
6 #include <X11/extensions/Xrandr.h>
8 #include <msp/core/algorithm.h>
9 #include <msp/io/print.h>
10 #include <msp/strings/format.h>
11 #include <msp/strings/lexicalcast.h>
17 bool error_flag = false;
20 int x_error_handler(Display *display, XErrorEvent *event)
23 XGetErrorText(display, event->error_code, err, sizeof(err));
25 string request_code = Msp::lexical_cast<string, int>(event->request_code);
27 XGetErrorDatabaseText(display, "XRequest", request_code.c_str(), request_code.c_str(), req, sizeof(req));
29 string msg = Msp::format("Request %s failed with %s [%08X]", req, err, event->resourceid);
31 Msp::IO::print(Msp::IO::cerr, "Discarding error: %s\n", msg);
34 Msp::IO::print(Msp::IO::cerr, "%s\n", msg);
43 inline Msp::Graphics::VideoRotation rotation_from_sys(Rotation r)
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;
54 inline Rotation rotation_to_sys(Msp::Graphics::VideoRotation r)
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;
65 inline bool monitor_x_compare(const Msp::Graphics::Monitor &m1, const Msp::Graphics::Monitor &m2)
67 if(m1.desktop_settings.mode && !m2.desktop_settings.mode)
69 return m1.desktop_settings.x<m2.desktop_settings.x;
73 inline unsigned mode_width(const Msp::Graphics::VideoMode &m, Msp::Graphics::VideoRotation r)
75 if(r==Msp::Graphics::ROTATE_RIGHT || r==Msp::Graphics::ROTATE_LEFT)
87 Display::Display(const string &disp_name):
91 priv->display = XOpenDisplay(nullptr);
93 priv->display = XOpenDisplay(disp_name.c_str());
95 throw runtime_error("XOpenDisplay");
97 XSetErrorHandler(x_error_handler);
99 priv->root_window = DefaultRootWindow(priv->display);
101 err_dialog = new ErrorDialog(this);
106 if(XRRQueryExtension(priv->display, &event_base, &error_base))
109 XRRQueryVersion(priv->display, &major, &minor);
110 if(major>1 || (major==1 && minor>=2))
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);
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];
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)
125 XRROutputInfo *output = XRRGetOutputInfo(priv->display, res, res->outputs[i]);
126 XRRCrtcInfo *crtc = (output->crtc ? XRRGetCrtcInfo(priv->display, res, output->crtc) : nullptr);
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]);
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)
144 for(unsigned j=0; j<4; ++j)
146 unsigned offset = 54+j*18;
147 if(edid[offset]==0 && edid[offset+1]==0 && edid[offset+3]==0xFC)
150 for(k=0; (k<13 && edid[offset+5+k]!=0x0A); ++k) ;
151 monitor.name.assign(reinterpret_cast<char *>(edid+offset+5), k);
160 monitor.desktop_settings.rotation = rotation_from_sys(crtc->rotation);
161 monitor.desktop_settings.x = crtc->x;
162 monitor.desktop_settings.y = crtc->y;
165 if(res->outputs[i]==primary)
166 primary_index = monitor.index;
168 unsigned first_mode = modes.size();
169 int desktop_mode_index = -1;
170 for(int j=0; j<output->nmode; ++j)
172 auto k = modes_by_id.find(output->modes[j]);
173 if(k==modes_by_id.end())
176 XRRModeInfo *info = k->second;
177 float rate = static_cast<float>(info->dotClock)/(info->hTotal*info->vTotal);
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);
184 VideoMode mode(info->width, info->height);
185 mode.index = modes.size();
188 modes.push_back(mode);
189 priv->modes.push_back(info->id);
190 mode_monitor_indices.push_back(monitor.index);
192 if(crtc && info->id==crtc->mode)
193 desktop_mode_index = mode.index;
195 desktop_mode_indices.push_back(desktop_mode_index);
197 XRRFreeOutputInfo(output);
199 XRRFreeCrtcInfo(crtc);
202 XRRFreeScreenResources(res);
204 for(unsigned i=0; i<monitors.size(); ++i)
206 int j = desktop_mode_indices[i];
208 monitors[i].desktop_settings.mode = &modes[j];
209 monitors[i].current_settings = monitors[i].desktop_settings;
212 sort(monitors, monitor_x_compare);
214 for(unsigned i=0; i<modes.size(); ++i)
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]);
221 Monitor *prev_enabled = nullptr;
222 for(Monitor &m: monitors)
223 if(m.desktop_settings.mode)
225 m.next_left = prev_enabled;
227 prev_enabled->next_right = &m;
232 primary_monitor = &*find_member(monitors, static_cast<unsigned>(primary_index), &Monitor::index);
234 if(!primary_monitor || !primary_monitor->desktop_settings.mode)
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)
241 unsigned size = desktop_mode->width*desktop_mode->height;
245 primary_monitor = &m;
256 XCloseDisplay(priv->display);
261 void Display::set_mode(const VideoMode &requested_mode, bool exclusive)
264 const VideoMode *mode = find_mode(requested_mode);
266 throw unsupported_video_mode(requested_mode);
268 VideoRotation requested_rotation = requested_mode.rotation;
269 if(requested_rotation==ROTATE_ANY)
270 requested_rotation = mode->monitor->desktop_settings.rotation;
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);
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;
280 crtc_info = XRRGetCrtcInfo(priv->display, res, crtc);
283 for(int i=0; i<res->ncrtc; ++i)
285 crtc_info = XRRGetCrtcInfo(priv->display, res, res->crtcs[i]);
286 if(!crtc_info->noutput)
288 crtc = res->crtcs[i];
291 XRRFreeCrtcInfo(crtc_info);
296 XRRFreeOutputInfo(output_info);
297 throw unsupported_video_mode(requested_mode);
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);
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;
312 // Disable other outputs for exclusive mode
313 for(Monitor &m: monitors)
314 if(&m!=mode->monitor)
316 XRROutputInfo *o = XRRGetOutputInfo(priv->display, res, priv->monitors[m.index]);
318 XRRSetCrtcConfig(priv->display, res, o->crtc, CurrentTime, 0, 0, 0, RR_Rotate_0, nullptr, 0);
319 XRRFreeOutputInfo(o);
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;
329 monitor->current_settings.x = monitor->desktop_settings.x;
330 monitor->current_settings.y = monitor->desktop_settings.y;
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);
339 XRRFreeOutputInfo(output_info);
340 XRRFreeCrtcInfo(crtc_info);
341 XRRFreeScreenResources(res);
343 (void)requested_mode;
345 throw runtime_error("no xrandr support");
349 bool Display::process_events()
351 int pending = XPending(priv->display);
358 XNextEvent(priv->display, &event.xevent);
362 auto j = priv->windows.find(event.xevent.xany.window);
363 if(j!=priv->windows.end())
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)
370 XKeyEvent &kev = event.xevent.xkey;
372 XPeekEvent(priv->display, &ev2);
373 if(ev2.type==KeyPress)
375 XKeyEvent &kev2 = ev2.xkey;
376 if(kev2.window==kev.window && kev2.time==kev.time && kev2.keycode==kev.keycode)
378 XNextEvent(priv->display, &ev2);
385 j->second->event(event);
392 void Display::check_error()
397 throw runtime_error(error_msg);
402 } // namespace Graphics