3 #ifdef WITH_XF86VIDMODE
4 #include <X11/extensions/xf86vmode.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);
47 Display::Display(const string &disp_name):
51 priv->display = XOpenDisplay(0);
53 priv->display = XOpenDisplay(disp_name.c_str());
55 throw runtime_error("XOpenDisplay");
57 XSetErrorHandler(x_error_handler);
59 #ifdef WITH_XF86VIDMODE
60 int screen = DefaultScreen(priv->display);
63 XF86VidModeModeInfo **infos;
64 XF86VidModeGetAllModeLines(priv->display, screen, &nmodes, &infos);
65 for(int i=0; i<nmodes; ++i)
67 XF86VidModeModeInfo &info = *infos[i];
69 VideoMode mode(info.hdisplay, info.vdisplay);
70 if(info.htotal && info.vtotal)
71 mode.rate = info.dotclock/(info.htotal*info.vtotal);
72 modes.push_back(mode);
77 XF86VidModeModeLine modeline;
79 XF86VidModeGetModeLine(priv->display, screen, &dotclock, &modeline);
80 orig_mode = VideoMode(modeline.hdisplay, modeline.vdisplay);
81 if(modeline.htotal && modeline.vtotal)
82 orig_mode.rate = dotclock/(modeline.htotal*modeline.vtotal);
88 XCloseDisplay(priv->display);
92 void Display::set_mode(const VideoMode &mode)
94 #ifdef WITH_XF86VIDMODE
95 int screen = DefaultScreen(priv->display);
98 XF86VidModeModeInfo **infos;
99 XF86VidModeGetAllModeLines(priv->display, screen, &nmodes, &infos);
100 for(int i=0; i<nmodes; ++i)
102 XF86VidModeModeInfo &info = *infos[i];
105 if(info.htotal && info.vtotal)
106 rate = info.dotclock/(info.htotal*info.vtotal);
107 if(info.hdisplay==mode.width && info.vdisplay==mode.height && (mode.rate==0 || rate==mode.rate))
109 XF86VidModeSwitchToMode(priv->display, screen, &info);
110 XF86VidModeSetViewPort(priv->display, screen, 0, 0);
115 throw unsupported_video_mode(mode);
118 throw runtime_error("no xf86vidmode support");
122 bool Display::process_events()
124 int pending = XPending(priv->display);
131 XNextEvent(priv->display, &event.xevent);
135 map<WindowHandle, Window *>::iterator j = priv->windows.find(event.xevent.xany.window);
136 if(j!=priv->windows.end())
138 /* Filter keyboard autorepeat. If this packet is a KeyRelease and
139 the next one is a KeyPress with the exact same parameters, they
140 indicate autorepeat and must be dropped. */
141 if(event.xevent.type==KeyRelease && !j->second->get_keyboard_autorepeat() && pending>0)
143 XKeyEvent &kev = event.xevent.xkey;
145 XPeekEvent(priv->display, &ev2);
146 if(ev2.type==KeyPress)
148 XKeyEvent &kev2 = ev2.xkey;
149 if(kev2.window==kev.window && kev2.time==kev.time && kev2.keycode==kev.keycode)
151 XNextEvent(priv->display, &ev2);
158 j->second->event(event);
165 void Display::check_error()
170 throw runtime_error(error_msg);
175 } // namespace Graphics