3 This file is part of libmspgbase
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
10 #include <msp/core/error.h>
15 #include <msp/strings/lexicalcast.h>
19 DisplayOptions::DisplayOptions():
27 Initializes the class but does not open the window. Intended for use by
35 Window::Window(unsigned w, unsigned h)
43 Window::Window(const DisplayOptions &dopt):
52 XDestroyWindow(display, window);
54 XCloseDisplay(display);
59 XMapRaised(display, window);
64 XUnmapWindow(display, window);
71 int pending=XPending(display);
75 for(int i=0; i<pending; ++i)
78 XNextEvent(display, &event);
84 void Window::prepare()
86 if(options.display.empty())
87 display=XOpenDisplay(0);
89 display=XOpenDisplay(options.display.c_str());
91 throw Exception("Couldn't open X display");
93 //XSetErrorHandler(x_error_handler);
100 window=XCreateWindow(display, DefaultRootWindow(display), 0, 0, options.width, options.height, 0, CopyFromParent, InputOutput, CopyFromParent, 0, 0);
102 throw Exception("Couldn't create a window");
104 XSelectInput(display, window, ButtonPressMask|ButtonReleaseMask|PointerMotionMask|KeyPressMask|KeyReleaseMask|StructureNotifyMask);
107 void Window::process_event(const XEvent &event)
112 signal_button_press.emit(event.xbutton.x, event.xbutton.y, event.xbutton.button, event.xbutton.state);
115 signal_button_release.emit(event.xbutton.x, event.xbutton.y, event.xbutton.button, event.xbutton.state);
118 signal_pointer_motion.emit(event.xmotion.x, event.xmotion.y);
123 XLookupString(const_cast<XKeyEvent *>(&event.xkey), buf, sizeof(buf), 0, 0);
124 // XXX Handle the result according to locale
125 signal_key_press.emit(event.xkey.keycode, event.xkey.state, buf[0]);
129 signal_key_release.emit(event.xkey.keycode, event.xkey.state);
131 case ConfigureNotify:
132 options.width=event.xconfigure.width;
133 options.height=event.xconfigure.height;
139 int Window::x_error_handler(Display *display, XErrorEvent *error)
142 XGetErrorText(display, error->error_code, buf, sizeof(buf));
143 /*string request_code=lexical_cast(error->request_code);
145 XGetErrorDatabaseText(display, "XRequest", request_code.c_str(), buf, buf2, sizeof(buf2));*/
146 throw Exception(buf);