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);
41 inline Msp::Graphics::VideoRotation rotation_from_sys(Rotation r)
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;
52 inline Rotation rotation_to_sys(Msp::Graphics::VideoRotation r)
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;
63 bool monitor_x_compare(const Msp::Graphics::Monitor &m1, const Msp::Graphics::Monitor &m2)
65 if(m1.desktop_mode && !m2.desktop_mode)
70 inline unsigned mode_width(const Msp::Graphics::VideoMode &m, Msp::Graphics::VideoRotation r)
72 if(r==Msp::Graphics::ROTATE_RIGHT || r==Msp::Graphics::ROTATE_LEFT)
84 Display::Display(const string &disp_name):
89 priv->display = XOpenDisplay(0);
91 priv->display = XOpenDisplay(disp_name.c_str());
93 throw runtime_error("XOpenDisplay");
95 XSetErrorHandler(x_error_handler);
97 err_dialog = new ErrorDialog(this);
102 if(XRRQueryExtension(priv->display, &event_base, &error_base))
105 XRRQueryVersion(priv->display, &major, &minor);
106 if(major>1 || (major==1 && minor>=2))
108 WindowHandle root = DefaultRootWindow(priv->display);
109 XRRScreenResources *res = XRRGetScreenResources(priv->display, root);
110 RROutput primary = XRRGetOutputPrimary(priv->display, root);
112 map<RRMode, XRRModeInfo *> modes_by_id;
113 for(int i=0; i<res->nmode; ++i)
114 modes_by_id[res->modes[i].id] = &res->modes[i];
116 for(int i=0; i<res->noutput; ++i)
118 XRROutputInfo *output = XRRGetOutputInfo(priv->display, res, res->outputs[i]);
119 XRRCrtcInfo *crtc = (output->crtc ? XRRGetCrtcInfo(priv->display, res, output->crtc) : 0);
121 monitors.push_back(Monitor());
122 Monitor &monitor = monitors.back();
123 monitor.index = monitors.size()-1;
124 priv->monitors.push_back(res->outputs[i]);
128 monitor.desktop_rotation = rotation_from_sys(crtc->rotation);
129 monitor.current_rotation = monitor.desktop_rotation;
134 if(res->outputs[i]==primary)
135 primary_monitor = &monitor;
137 for(int j=0; j<output->nmode; ++j)
139 map<RRMode, XRRModeInfo *>::iterator k = modes_by_id.find(output->modes[j]);
140 if(k==modes_by_id.end())
143 XRRModeInfo *info = k->second;
145 VideoMode mode(info->width, info->height);
146 mode.index = modes.size();
147 mode.monitor = &monitor;
148 mode.rate = info->dotClock/(info->hTotal*info->vTotal);
149 if(find_matching_mode(mode))
152 modes.push_back(mode);
153 priv->modes.push_back(info->id);
154 monitor.video_modes.push_back(&modes.back());
156 if(crtc && info->id==crtc->mode)
158 monitor.desktop_mode = &modes.back();
159 monitor.current_mode = monitor.desktop_mode;
163 XRRFreeOutputInfo(output);
165 XRRFreeCrtcInfo(crtc);
168 XRRFreeScreenResources(res);
170 monitors.sort(monitor_x_compare);
171 Monitor *prev_enabled = 0;
172 for(list<Monitor>::iterator i=monitors.begin(); i!=monitors.end(); ++i)
175 i->next_left = prev_enabled;
177 prev_enabled->next_right = &*i;
181 if(!primary_monitor || !primary_monitor->desktop_mode)
183 // XRandR didn't give a sensible primary monitor. Try to guess one.
184 unsigned largest = 0;
185 for(list<Monitor>::iterator i=monitors.begin(); i!=monitors.end(); ++i)
188 unsigned size = i->desktop_mode->width*i->desktop_mode->height;
192 primary_monitor = &*i;
203 XCloseDisplay(priv->display);
208 void Display::set_mode(const VideoMode &requested_mode, bool exclusive)
211 const VideoMode *mode = find_matching_mode(requested_mode);
213 throw unsupported_video_mode(requested_mode);
215 VideoRotation requested_rotation = requested_mode.rotation;
216 if(requested_rotation==ROTATE_ANY)
217 requested_rotation = mode->monitor->desktop_rotation;
219 WindowHandle root = DefaultRootWindow(priv->display);
220 XRRScreenResources *res = XRRGetScreenResources(priv->display, root);
221 RROutput output = priv->monitors[mode->monitor->index];
222 XRROutputInfo *output_info = XRRGetOutputInfo(priv->display, res, output);
224 // Check if the output already has a CRTC and find a free one if it doesn't
225 RRCrtc crtc = output_info->crtc;
226 XRRCrtcInfo *crtc_info = 0;
228 crtc_info = XRRGetCrtcInfo(priv->display, res, crtc);
231 for(int i=0; i<res->ncrtc; ++i)
233 crtc_info = XRRGetCrtcInfo(priv->display, res, res->crtcs[i]);
234 if(!crtc_info->noutput)
236 crtc = res->crtcs[i];
239 XRRFreeCrtcInfo(crtc_info);
244 XRRFreeOutputInfo(output_info);
245 throw unsupported_video_mode(requested_mode);
254 // Disable other outputs for exclusive mode
255 for(list<Monitor>::iterator i=monitors.begin(); i!=monitors.end(); ++i)
256 if(&*i!=mode->monitor)
258 XRROutputInfo *o = XRRGetOutputInfo(priv->display, res, priv->monitors[i->index]);
260 XRRSetCrtcConfig(priv->display, res, o->crtc, CurrentTime, 0, 0, 0, RR_Rotate_0, 0, 0);
261 XRRFreeOutputInfo(o);
264 i->current_rotation = ROTATE_NORMAL;
271 const Monitor *left = mode->monitor->next_left;
272 while(left && !left->current_mode)
273 left = left->next_left;
277 x = left->x+mode_width(*left->current_mode, left->current_rotation);
282 XRRSetCrtcConfig(priv->display, res, crtc, CurrentTime, x, y, priv->modes[mode->index], rotation_to_sys(requested_rotation), &output, 1);
284 list<Monitor>::iterator i;
285 for(i=monitors.begin(); i!=monitors.end(); ++i)
286 if(&*i==mode->monitor)
288 i->current_mode = mode;
289 i->current_rotation = requested_rotation;
293 x += mode_width(*mode, requested_rotation);
298 for(; i!=monitors.end(); ++i)
301 XRROutputInfo *o = XRRGetOutputInfo(priv->display, res, priv->monitors[i->index]);
302 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);
303 XRRFreeOutputInfo(o);
308 x += mode_width(*i->current_mode, i->current_rotation);
311 XRRFreeOutputInfo(output_info);
312 XRRFreeCrtcInfo(crtc_info);
313 XRRFreeScreenResources(res);
315 (void)requested_mode;
317 throw runtime_error("no xrandr support");
321 bool Display::process_events()
323 int pending = XPending(priv->display);
330 XNextEvent(priv->display, &event.xevent);
334 map<WindowHandle, Window *>::iterator j = priv->windows.find(event.xevent.xany.window);
335 if(j!=priv->windows.end())
337 /* Filter keyboard autorepeat. If this packet is a KeyRelease and
338 the next one is a KeyPress with the exact same parameters, they
339 indicate autorepeat and must be dropped. */
340 if(event.xevent.type==KeyRelease && !j->second->get_keyboard_autorepeat() && pending>0)
342 XKeyEvent &kev = event.xevent.xkey;
344 XPeekEvent(priv->display, &ev2);
345 if(ev2.type==KeyPress)
347 XKeyEvent &kev2 = ev2.xkey;
348 if(kev2.window==kev.window && kev2.time==kev.time && kev2.keycode==kev.keycode)
350 XNextEvent(priv->display, &ev2);
357 j->second->event(event);
364 void Display::check_error()
369 throw runtime_error(error_msg);
374 } // namespace Graphics