4 #include <X11/extensions/Xrandr.h>
6 #include <msp/io/print.h>
7 #include <msp/strings/format.h>
8 #include <msp/strings/lexicalcast.h>
10 #include "display_private.h"
16 bool error_flag = false;
17 std::string error_msg;
19 int x_error_handler(Display *display, XErrorEvent *event)
22 XGetErrorText(display, event->error_code, err, sizeof(err));
24 string request_code = Msp::lexical_cast<string, int>(event->request_code);
26 XGetErrorDatabaseText(display, "XRequest", request_code.c_str(), request_code.c_str(), req, sizeof(req));
28 string msg = Msp::format("Request %s failed with %s [%08X]", req, err, event->resourceid);
30 Msp::IO::print(Msp::IO::cerr, "Discarding error: %s\n", msg);
33 Msp::IO::print(Msp::IO::cerr, "%s\n", msg);
42 inline Msp::Graphics::VideoRotation rotation_from_sys(Rotation r)
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;
53 inline Rotation rotation_to_sys(Msp::Graphics::VideoRotation r)
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;
64 inline bool monitor_x_compare(const Msp::Graphics::Monitor &m1, const Msp::Graphics::Monitor &m2)
66 if(m1.desktop_mode && !m2.desktop_mode)
72 inline unsigned mode_width(const Msp::Graphics::VideoMode &m, Msp::Graphics::VideoRotation r)
74 if(r==Msp::Graphics::ROTATE_RIGHT || r==Msp::Graphics::ROTATE_LEFT)
86 Display::Display(const string &disp_name):
91 priv->display = XOpenDisplay(0);
93 priv->display = XOpenDisplay(disp_name.c_str());
95 throw runtime_error("XOpenDisplay");
97 XSetErrorHandler(x_error_handler);
99 err_dialog = new ErrorDialog(this);
104 if(XRRQueryExtension(priv->display, &event_base, &error_base))
107 XRRQueryVersion(priv->display, &major, &minor);
108 if(major>1 || (major==1 && minor>=2))
110 WindowHandle root = DefaultRootWindow(priv->display);
111 XRRScreenResources *res = XRRGetScreenResources(priv->display, root);
112 RROutput primary = XRRGetOutputPrimary(priv->display, root);
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];
118 for(int i=0; i<res->noutput; ++i)
120 XRROutputInfo *output = XRRGetOutputInfo(priv->display, res, res->outputs[i]);
121 XRRCrtcInfo *crtc = (output->crtc ? XRRGetCrtcInfo(priv->display, res, output->crtc) : 0);
123 monitors.push_back(Monitor());
124 Monitor &monitor = monitors.back();
125 monitor.index = monitors.size()-1;
126 priv->monitors.push_back(res->outputs[i]);
130 monitor.desktop_rotation = rotation_from_sys(crtc->rotation);
131 monitor.current_rotation = monitor.desktop_rotation;
136 if(res->outputs[i]==primary)
137 primary_monitor = &monitor;
139 for(int j=0; j<output->nmode; ++j)
141 map<RRMode, XRRModeInfo *>::iterator k = modes_by_id.find(output->modes[j]);
142 if(k==modes_by_id.end())
145 XRRModeInfo *info = k->second;
147 VideoMode mode(info->width, info->height);
148 mode.index = modes.size();
149 mode.monitor = &monitor;
150 mode.rate = info->dotClock/(info->hTotal*info->vTotal);
154 modes.push_back(mode);
155 priv->modes.push_back(info->id);
156 monitor.video_modes.push_back(&modes.back());
158 if(crtc && info->id==crtc->mode)
160 monitor.desktop_mode = &modes.back();
161 monitor.current_mode = monitor.desktop_mode;
165 XRRFreeOutputInfo(output);
167 XRRFreeCrtcInfo(crtc);
170 XRRFreeScreenResources(res);
172 monitors.sort(monitor_x_compare);
173 Monitor *prev_enabled = 0;
174 for(list<Monitor>::iterator i=monitors.begin(); i!=monitors.end(); ++i)
177 i->next_left = prev_enabled;
179 prev_enabled->next_right = &*i;
183 if(!primary_monitor || !primary_monitor->desktop_mode)
185 // XRandR didn't give a sensible primary monitor. Try to guess one.
186 unsigned largest = 0;
187 for(list<Monitor>::iterator i=monitors.begin(); i!=monitors.end(); ++i)
190 unsigned size = i->desktop_mode->width*i->desktop_mode->height;
194 primary_monitor = &*i;
205 XCloseDisplay(priv->display);
210 void Display::set_mode(const VideoMode &requested_mode, bool exclusive)
213 const VideoMode *mode = find_mode(requested_mode);
215 throw unsupported_video_mode(requested_mode);
217 VideoRotation requested_rotation = requested_mode.rotation;
218 if(requested_rotation==ROTATE_ANY)
219 requested_rotation = mode->monitor->desktop_rotation;
221 WindowHandle root = DefaultRootWindow(priv->display);
222 XRRScreenResources *res = XRRGetScreenResources(priv->display, root);
223 RROutput output = priv->monitors[mode->monitor->index];
224 XRROutputInfo *output_info = XRRGetOutputInfo(priv->display, res, output);
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;
230 crtc_info = XRRGetCrtcInfo(priv->display, res, crtc);
233 for(int i=0; i<res->ncrtc; ++i)
235 crtc_info = XRRGetCrtcInfo(priv->display, res, res->crtcs[i]);
236 if(!crtc_info->noutput)
238 crtc = res->crtcs[i];
241 XRRFreeCrtcInfo(crtc_info);
246 XRRFreeOutputInfo(output_info);
247 throw unsupported_video_mode(requested_mode);
256 // Disable other outputs for exclusive mode
257 for(list<Monitor>::iterator i=monitors.begin(); i!=monitors.end(); ++i)
258 if(&*i!=mode->monitor)
260 XRROutputInfo *o = XRRGetOutputInfo(priv->display, res, priv->monitors[i->index]);
262 XRRSetCrtcConfig(priv->display, res, o->crtc, CurrentTime, 0, 0, 0, RR_Rotate_0, 0, 0);
263 XRRFreeOutputInfo(o);
266 i->current_rotation = ROTATE_NORMAL;
273 const Monitor *left = mode->monitor->next_left;
274 while(left && !left->current_mode)
275 left = left->next_left;
279 x = left->x+mode_width(*left->current_mode, left->current_rotation);
284 XRRSetCrtcConfig(priv->display, res, crtc, CurrentTime, x, y, priv->modes[mode->index], rotation_to_sys(requested_rotation), &output, 1);
286 list<Monitor>::iterator i;
287 for(i=monitors.begin(); i!=monitors.end(); ++i)
288 if(&*i==mode->monitor)
290 i->current_mode = mode;
291 i->current_rotation = requested_rotation;
295 x += mode_width(*mode, requested_rotation);
300 for(; i!=monitors.end(); ++i)
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);
307 panning.timestamp = CurrentTime;
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);
322 XRRFreeOutputInfo(o);
327 x += mode_width(*i->current_mode, i->current_rotation);
330 XRRFreeOutputInfo(output_info);
331 XRRFreeCrtcInfo(crtc_info);
332 XRRFreeScreenResources(res);
334 (void)requested_mode;
336 throw runtime_error("no xrandr support");
340 bool Display::process_events()
342 int pending = XPending(priv->display);
349 XNextEvent(priv->display, &event.xevent);
353 map<WindowHandle, Window *>::iterator j = priv->windows.find(event.xevent.xany.window);
354 if(j!=priv->windows.end())
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)
361 XKeyEvent &kev = event.xevent.xkey;
363 XPeekEvent(priv->display, &ev2);
364 if(ev2.type==KeyPress)
366 XKeyEvent &kev2 = ev2.xkey;
367 if(kev2.window==kev.window && kev2.time==kev.time && kev2.keycode==kev.keycode)
369 XNextEvent(priv->display, &ev2);
376 j->second->event(event);
383 void Display::check_error()
388 throw runtime_error(error_msg);
393 } // namespace Graphics