4 #ifdef WITH_XF86VIDMODE
5 #include <X11/extensions/xf86vmode.h>
8 #include <msp/core/except.h>
9 #include <msp/strings/formatter.h>
10 #include <msp/strings/lexicalcast.h>
13 #include "display_priv.h"
19 bool error_flag = false;
20 std::string error_msg;
23 int x_error_handler(Display *display, XErrorEvent *event)
26 XGetErrorText(display, event->error_code, err, sizeof(err));
28 string request_code = Msp::lexical_cast(static_cast<int>(event->request_code));
30 XGetErrorDatabaseText(display, "XRequest", request_code.c_str(), request_code.c_str(), req, sizeof(req));
32 string msg = Msp::format("Request %s failed with %s [%08X]", req, err, event->resourceid);
34 cerr<<"Discarding error: "<<msg<<'\n';
51 Display::Display(const string &disp_name):
57 for(unsigned i=0;; ++i)
60 if(!EnumDisplaySettings(0, i, &info))
63 VideoMode mode(info.dmPelsWidth, info.dmPelsHeight);
64 mode.rate = info.dmDisplayFrequency;
65 modes.push_back(mode);
69 if(EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS, &info))
71 orig_mode = VideoMode(info.dmPelsWidth, info.dmPelsHeight);
72 orig_mode.rate = info.dmDisplayFrequency;
76 priv->display = XOpenDisplay(0);
78 priv->display = XOpenDisplay(disp_name.c_str());
80 throw Exception("Couldn't open X display");
82 XSetErrorHandler(x_error_handler);
84 #ifdef WITH_XF86VIDMODE
85 int screen = DefaultScreen(priv->display);
88 XF86VidModeModeInfo **infos;
89 XF86VidModeGetAllModeLines(priv->display, screen, &nmodes, &infos);
90 for(int i=0; i<nmodes; ++i)
92 XF86VidModeModeInfo &info = *infos[i];
94 VideoMode mode(info.hdisplay, info.vdisplay);
95 if(info.htotal && info.vtotal)
96 mode.rate = info.dotclock/(info.htotal*info.vtotal);
97 modes.push_back(mode);
102 XF86VidModeModeLine modeline;
104 XF86VidModeGetModeLine(priv->display, screen, &dotclock, &modeline);
105 orig_mode = VideoMode(modeline.hdisplay, modeline.vdisplay);
106 if(modeline.htotal && modeline.vtotal)
107 orig_mode.rate = dotclock/(modeline.htotal*modeline.vtotal);
115 XCloseDisplay(priv->display);
120 void Display::add_window(Window &wnd)
122 priv->windows[wnd.get_private().window] = &wnd;
125 void Display::remove_window(Window &wnd)
127 priv->windows.erase(wnd.get_private().window);
130 void Display::set_mode(const VideoMode &mode)
134 info.dmSize = sizeof(DEVMODE);
135 info.dmFields = DM_PELSWIDTH|DM_PELSHEIGHT;
136 info.dmPelsWidth = mode.width;
137 info.dmPelsHeight = mode.height;
140 info.dmFields |= DM_DISPLAYFREQUENCY;
141 info.dmDisplayFrequency = mode.rate;
144 ChangeDisplaySettings(&info, CDS_FULLSCREEN);
145 #elif defined(WITH_XF86VIDMODE)
146 int screen = DefaultScreen(priv->display);
149 XF86VidModeModeInfo **infos;
150 XF86VidModeGetAllModeLines(priv->display, screen, &nmodes, &infos);
151 for(int i=0; i<nmodes; ++i)
153 XF86VidModeModeInfo &info = *infos[i];
156 if(info.htotal && info.vtotal)
157 rate = info.dotclock/(info.htotal*info.vtotal);
158 if(info.hdisplay==mode.width && info.vdisplay==mode.height && (mode.rate==0 || rate==mode.rate))
160 XF86VidModeSwitchToMode(priv->display, screen, &info);
161 XF86VidModeSetViewPort(priv->display, screen, 0, 0);
166 throw InvalidParameterValue("Requested mode not supported");
169 throw Exception("Video mode switching not supported");
181 if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
182 DispatchMessage(&msg);
186 int pending = XPending(priv->display);
193 XNextEvent(priv->display, &event.xevent);
197 map<WindowHandle, Window *>::iterator j = priv->windows.find(event.xevent.xany.window);
198 if(j!=priv->windows.end())
200 /* Filter keyboard autorepeat. If this packet is a KeyRelease and
201 the next one is a KeyPress with the exact same parameters, they
202 indicate autorepeat and must be dropped. */
203 if(event.xevent.type==KeyRelease && !j->second->get_keyboard_autorepeat() && pending>0)
205 XKeyEvent &kev = event.xevent.xkey;
207 XPeekEvent(priv->display, &ev2);
208 if(ev2.type==KeyPress)
210 XKeyEvent &kev2 = ev2.xkey;
211 if(kev2.window==kev.window && kev2.time==kev.time && kev2.keycode==kev.keycode)
213 XNextEvent(priv->display, &ev2);
220 j->second->event(event);
227 void Display::check_error()
232 throw Exception(error_msg);
236 } // namespace Graphics