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);
100 if(XRRQueryExtension(priv->display, &event_base, &error_base))
103 XRRQueryVersion(priv->display, &major, &minor);
104 if(major>1 || (major==1 && minor>=2))
106 WindowHandle root = DefaultRootWindow(priv->display);
107 XRRScreenResources *res = XRRGetScreenResources(priv->display, root);
108 RROutput primary = XRRGetOutputPrimary(priv->display, root);
110 map<RRMode, XRRModeInfo *> modes_by_id;
111 for(int i=0; i<res->nmode; ++i)
112 modes_by_id[res->modes[i].id] = &res->modes[i];
114 for(int i=0; i<res->noutput; ++i)
116 XRROutputInfo *output = XRRGetOutputInfo(priv->display, res, res->outputs[i]);
117 XRRCrtcInfo *crtc = (output->crtc ? XRRGetCrtcInfo(priv->display, res, output->crtc) : 0);
119 monitors.push_back(Monitor());
120 Monitor &monitor = monitors.back();
121 monitor.index = monitors.size()-1;
122 priv->monitors.push_back(res->outputs[i]);
126 monitor.desktop_rotation = rotation_from_sys(crtc->rotation);
127 monitor.current_rotation = monitor.desktop_rotation;
132 if(res->outputs[i]==primary)
133 primary_monitor = &monitor;
135 for(int j=0; j<output->nmode; ++j)
137 map<RRMode, XRRModeInfo *>::iterator k = modes_by_id.find(output->modes[j]);
138 if(k==modes_by_id.end())
141 XRRModeInfo *info = k->second;
143 VideoMode mode(info->width, info->height);
144 mode.index = modes.size();
145 mode.monitor = &monitor;
146 mode.rate = info->dotClock/(info->hTotal*info->vTotal);
147 if(find_matching_mode(mode))
150 modes.push_back(mode);
151 priv->modes.push_back(info->id);
152 monitor.video_modes.push_back(&modes.back());
154 if(crtc && info->id==crtc->mode)
156 monitor.desktop_mode = &modes.back();
157 monitor.current_mode = monitor.desktop_mode;
161 XRRFreeOutputInfo(output);
163 XRRFreeCrtcInfo(crtc);
166 XRRFreeScreenResources(res);
168 monitors.sort(monitor_x_compare);
169 Monitor *prev_enabled = 0;
170 for(list<Monitor>::iterator i=monitors.begin(); i!=monitors.end(); ++i)
173 i->next_left = prev_enabled;
175 prev_enabled->next_right = &*i;
185 XCloseDisplay(priv->display);
189 void Display::set_mode(const VideoMode &requested_mode, bool exclusive)
192 const VideoMode *mode = find_matching_mode(requested_mode);
194 throw unsupported_video_mode(requested_mode);
196 VideoRotation requested_rotation = requested_mode.rotation;
197 if(requested_rotation==ROTATE_ANY)
198 requested_rotation = mode->monitor->desktop_rotation;
200 WindowHandle root = DefaultRootWindow(priv->display);
201 XRRScreenResources *res = XRRGetScreenResources(priv->display, root);
202 RROutput output = priv->monitors[mode->monitor->index];
203 XRROutputInfo *output_info = XRRGetOutputInfo(priv->display, res, output);
205 // Check if the output already has a CRTC and find a free one if it doesn't
206 RRCrtc crtc = output_info->crtc;
207 XRRCrtcInfo *crtc_info = 0;
209 crtc_info = XRRGetCrtcInfo(priv->display, res, crtc);
212 for(int i=0; i<res->ncrtc; ++i)
214 crtc_info = XRRGetCrtcInfo(priv->display, res, res->crtcs[i]);
215 if(!crtc_info->noutput)
217 crtc = res->crtcs[i];
220 XRRFreeCrtcInfo(crtc_info);
225 XRRFreeOutputInfo(output_info);
226 throw unsupported_video_mode(requested_mode);
235 // Disable other outputs for exclusive mode
236 for(list<Monitor>::iterator i=monitors.begin(); i!=monitors.end(); ++i)
237 if(&*i!=mode->monitor)
239 XRROutputInfo *o = XRRGetOutputInfo(priv->display, res, priv->monitors[i->index]);
241 XRRSetCrtcConfig(priv->display, res, o->crtc, CurrentTime, 0, 0, 0, RR_Rotate_0, 0, 0);
242 XRRFreeOutputInfo(o);
245 i->current_rotation = ROTATE_NORMAL;
252 const Monitor *left = mode->monitor->next_left;
253 while(left && !left->current_mode)
254 left = left->next_left;
258 x = left->x+mode_width(*left->current_mode, left->current_rotation);
263 XRRSetCrtcConfig(priv->display, res, crtc, CurrentTime, x, y, priv->modes[mode->index], rotation_to_sys(requested_rotation), &output, 1);
265 list<Monitor>::iterator i;
266 for(i=monitors.begin(); i!=monitors.end(); ++i)
267 if(&*i==mode->monitor)
269 i->current_mode = mode;
270 i->current_rotation = requested_rotation;
274 x += mode_width(*mode, requested_rotation);
279 for(; i!=monitors.end(); ++i)
282 XRROutputInfo *o = XRRGetOutputInfo(priv->display, res, priv->monitors[i->index]);
283 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);
284 XRRFreeOutputInfo(o);
289 x += mode_width(*i->current_mode, i->current_rotation);
292 XRRFreeOutputInfo(output_info);
293 XRRFreeCrtcInfo(crtc_info);
294 XRRFreeScreenResources(res);
296 (void)requested_mode;
298 throw runtime_error("no xrandr support");
302 bool Display::process_events()
304 int pending = XPending(priv->display);
311 XNextEvent(priv->display, &event.xevent);
315 map<WindowHandle, Window *>::iterator j = priv->windows.find(event.xevent.xany.window);
316 if(j!=priv->windows.end())
318 /* Filter keyboard autorepeat. If this packet is a KeyRelease and
319 the next one is a KeyPress with the exact same parameters, they
320 indicate autorepeat and must be dropped. */
321 if(event.xevent.type==KeyRelease && !j->second->get_keyboard_autorepeat() && pending>0)
323 XKeyEvent &kev = event.xevent.xkey;
325 XPeekEvent(priv->display, &ev2);
326 if(ev2.type==KeyPress)
328 XKeyEvent &kev2 = ev2.xkey;
329 if(kev2.window==kev.window && kev2.time==kev.time && kev2.keycode==kev.keycode)
331 XNextEvent(priv->display, &ev2);
338 j->second->event(event);
345 void Display::check_error()
350 throw runtime_error(error_msg);
355 } // namespace Graphics