3 This file is part of libmspgbase
4 Copyright © 2007-2008 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
11 #include <X11/extensions/xf86vmode.h>
13 #include <msp/core/except.h>
14 #include <msp/strings/formatter.h>
15 #include <msp/strings/lexicalcast.h>
18 #include "display_priv.h"
24 bool error_flag=false;
25 std::string error_msg;
28 int x_error_handler(Display *display, XErrorEvent *event)
31 XGetErrorText(display, event->error_code, err, sizeof(err));
33 string request_code=Msp::lexical_cast(static_cast<int>(event->request_code));
35 XGetErrorDatabaseText(display, "XRequest", request_code.c_str(), request_code.c_str(), req, sizeof(req));
37 string msg=Msp::format("Request %s failed with %s [%08X]", req, err, event->resourceid);
39 cerr<<"Discarding error: "<<msg<<'\n';
56 Display::Display(const string &disp_name):
62 for(unsigned i=0;; ++i)
65 if(!EnumDisplaySettings(0, i, &info))
68 VideoMode mode(info.dmPelsWidth, info.dmPelsHeight);
69 mode.rate=info.dmDisplayFrequency;
70 modes.push_back(mode);
74 if(EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS, &info))
76 orig_mode=VideoMode(info.dmPelsWidth, info.dmPelsHeight);
77 orig_mode.rate=info.dmDisplayFrequency;
81 priv->display=XOpenDisplay(0);
83 priv->display=XOpenDisplay(disp_name.c_str());
85 throw Exception("Couldn't open X display");
87 XSetErrorHandler(x_error_handler);
89 int screen=DefaultScreen(priv->display);
92 XF86VidModeModeInfo **infos;
93 XF86VidModeGetAllModeLines(priv->display, screen, &nmodes, &infos);
94 for(int i=0; i<nmodes; ++i)
96 XF86VidModeModeInfo &info=*infos[i];
98 VideoMode mode(info.hdisplay, info.vdisplay);
99 if(info.htotal && info.vtotal)
100 mode.rate=info.dotclock/(info.htotal*info.vtotal);
101 modes.push_back(mode);
106 XF86VidModeModeLine modeline;
108 XF86VidModeGetModeLine(priv->display, screen, &dotclock, &modeline);
109 orig_mode=VideoMode(modeline.hdisplay, modeline.vdisplay);
110 if(modeline.htotal && modeline.vtotal)
111 orig_mode.rate=dotclock/(modeline.htotal*modeline.vtotal);
118 XCloseDisplay(priv->display);
123 void Display::add_window(Window &wnd)
125 priv->windows[wnd.get_private().window]=&wnd;
128 void Display::remove_window(Window &wnd)
130 priv->windows.erase(wnd.get_private().window);
133 void Display::set_mode(const VideoMode &mode)
137 info.dmSize=sizeof(DEVMODE);
138 info.dmFields=DM_PELSWIDTH|DM_PELSHEIGHT;
139 info.dmPelsWidth=mode.width;
140 info.dmPelsHeight=mode.height;
143 info.dmFields|=DM_DISPLAYFREQUENCY;
144 info.dmDisplayFrequency=mode.rate;
147 ChangeDisplaySettings(&info, CDS_FULLSCREEN);
149 int screen=DefaultScreen(priv->display);
152 XF86VidModeModeInfo **infos;
153 XF86VidModeGetAllModeLines(priv->display, screen, &nmodes, &infos);
154 for(int i=0; i<nmodes; ++i)
156 XF86VidModeModeInfo &info=*infos[i];
159 if(info.htotal && info.vtotal)
160 rate=info.dotclock/(info.htotal*info.vtotal);
161 if(info.hdisplay==mode.width && info.vdisplay==mode.height && (mode.rate==0 || rate==mode.rate))
163 XF86VidModeSwitchToMode(priv->display, screen, &info);
164 XF86VidModeSetViewPort(priv->display, screen, 0, 0);
169 throw InvalidParameterValue("Requested mode 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