3 #ifdef WITH_XF86VIDMODE
4 #include <X11/extensions/xf86vmode.h>
6 #include <msp/strings/format.h>
7 #include <msp/strings/lexicalcast.h>
9 #include "display_private.h"
15 bool error_flag = false;
16 std::string error_msg;
18 int x_error_handler(Display *display, XErrorEvent *event)
21 XGetErrorText(display, event->error_code, err, sizeof(err));
23 string request_code = Msp::lexical_cast<string, int>(event->request_code);
25 XGetErrorDatabaseText(display, "XRequest", request_code.c_str(), request_code.c_str(), req, sizeof(req));
27 string msg = Msp::format("Request %s failed with %s [%08X]", req, err, event->resourceid);
29 cerr<<"Discarding error: "<<msg<<'\n';
46 Display::Display(const string &disp_name):
50 priv->display = XOpenDisplay(0);
52 priv->display = XOpenDisplay(disp_name.c_str());
54 throw runtime_error("XOpenDisplay");
56 XSetErrorHandler(x_error_handler);
58 #ifdef WITH_XF86VIDMODE
59 int screen = DefaultScreen(priv->display);
62 XF86VidModeModeInfo **infos;
63 XF86VidModeGetAllModeLines(priv->display, screen, &nmodes, &infos);
64 for(int i=0; i<nmodes; ++i)
66 XF86VidModeModeInfo &info = *infos[i];
68 VideoMode mode(info.hdisplay, info.vdisplay);
69 if(info.htotal && info.vtotal)
70 mode.rate = info.dotclock/(info.htotal*info.vtotal);
71 modes.push_back(mode);
76 XF86VidModeModeLine modeline;
78 XF86VidModeGetModeLine(priv->display, screen, &dotclock, &modeline);
79 orig_mode = VideoMode(modeline.hdisplay, modeline.vdisplay);
80 if(modeline.htotal && modeline.vtotal)
81 orig_mode.rate = dotclock/(modeline.htotal*modeline.vtotal);
87 XCloseDisplay(priv->display);
91 void Display::set_mode(const VideoMode &mode)
93 #ifdef WITH_XF86VIDMODE
94 int screen = DefaultScreen(priv->display);
97 XF86VidModeModeInfo **infos;
98 XF86VidModeGetAllModeLines(priv->display, screen, &nmodes, &infos);
99 for(int i=0; i<nmodes; ++i)
101 XF86VidModeModeInfo &info = *infos[i];
104 if(info.htotal && info.vtotal)
105 rate = info.dotclock/(info.htotal*info.vtotal);
106 if(info.hdisplay==mode.width && info.vdisplay==mode.height && (mode.rate==0 || rate==mode.rate))
108 XF86VidModeSwitchToMode(priv->display, screen, &info);
109 XF86VidModeSetViewPort(priv->display, screen, 0, 0);
114 throw unsupported_video_mode(mode);
117 throw runtime_error("no xf86vidmode support");
121 bool Display::process_events()
123 int pending = XPending(priv->display);
130 XNextEvent(priv->display, &event.xevent);
134 map<WindowHandle, Window *>::iterator j = priv->windows.find(event.xevent.xany.window);
135 if(j!=priv->windows.end())
137 /* Filter keyboard autorepeat. If this packet is a KeyRelease and
138 the next one is a KeyPress with the exact same parameters, they
139 indicate autorepeat and must be dropped. */
140 if(event.xevent.type==KeyRelease && !j->second->get_keyboard_autorepeat() && pending>0)
142 XKeyEvent &kev = event.xevent.xkey;
144 XPeekEvent(priv->display, &ev2);
145 if(ev2.type==KeyPress)
147 XKeyEvent &kev2 = ev2.xkey;
148 if(kev2.window==kev.window && kev2.time==kev.time && kev2.keycode==kev.keycode)
150 XNextEvent(priv->display, &ev2);
157 j->second->event(event);
164 void Display::check_error()
169 throw runtime_error(error_msg);
174 } // namespace Graphics