-#include <vector>
-#include <GL/glx.h>
-#include <msp/core/error.h>
-#include "window.h"
-
-using namespace std;
-
-namespace Msp {
-namespace GLtk {
-
-DisplayOptions::DisplayOptions():
- width(640),
- height(480),
- depth(24),
- alpha(false),
- doublebuffer(false),
- multisample(0),
- fullscreen(false)
-{ }
-
-
-Window::Window(unsigned w, unsigned h)
-{
- DisplayOptions dopt;
- dopt.width=w;
- dopt.height=h;
-
- init(dopt);
-}
-
-Window::Window(const DisplayOptions &dopt)
-{
- init(dopt);
-}
-
-Window::~Window()
-{
- XCloseDisplay(display);
-}
-
-void Window::show()
-{
- XMapWindow(display, window);
-}
-
-void Window::tick()
-{
- while(1)
- {
- int pending=XPending(display);
- if(pending==0)
- break;
-
- for(int i=0; i<pending; ++i)
- {
- XEvent event;
- XNextEvent(display, &event);
- process_event(event);
- }
- }
-}
-
-void Window::init(const DisplayOptions &dopt)
-{
- options=dopt;
-
- display=XOpenDisplay(0);
- if(!display)
- throw Exception("Couldn't open X display");
-
- vector<int> attribs;
- attribs.push_back(GLX_BUFFER_SIZE);
- attribs.push_back(dopt.depth);
- attribs.push_back(GLX_DOUBLEBUFFER);
- attribs.push_back(1);
- if(dopt.multisample>0)
- {
- attribs.push_back(GLX_SAMPLE_BUFFERS_ARB);
- attribs.push_back(dopt.multisample);
- }
-
- XVisualInfo *visual=glXChooseVisual(display, DefaultScreen(display), &attribs.front());
- if(!visual)
- throw Exception("Couldn't get a matching visual");
-
- window=XCreateWindow(display, DefaultRootWindow(display), 0, 0, dopt.width, dopt.height, 0, CopyFromParent, InputOutput, visual->visual, 0, 0);
-
- XSelectInput(display, window, ButtonPressMask|ButtonReleaseMask|MotionMask|KeyPressMask|KeyReleaseMask);
-}
-
-void Window::process_event(const XEvent &event)
-{
- switch(event.type)
- {
- case ButtonPress:
- signal_button_press.emit(event.button.x, event.button.y, event.button.button, event.button.state);
- break;
- case ButtonRelease:
- signal_button_release.emit(event.button.x, event.button.y, event.button.button, event.button.state);
- break;
- case PointerMotion:
- signal_pointer_motion.emit(event.motion.x, event.motion.y);
- break;
- case KeyPress:
- {
- char buf[16];
- XLookupString(event.key, buf, sizeof(buf), 0, 0);
- // XXX Handle the result according to locale
- signal_key_press.emit(event.key.keycode, event.key.state, buf[0]);
- }
- break;
- case KeyRelease:
- signal_key_release.emit(event.key.keycode, event.key.state);
- break;
- default:;
- }
-}
-
-} // namespace GLtk
-} // namespace Msp