2 // Written and (C) by Francois Fleuret
3 // Contact <francois.fleuret@idiap.ch> for comments & bug reports
8 #include "simple_window.h"
10 SimpleEvent::SimpleEvent() : type(UNDEFINED) {}
12 SimpleWindow::SimpleWindow(const char *name, int x, int y, int w, int h) {
14 _width = w; _height = h;
16 _display = XOpenDisplay(0);
20 _screen = DefaultScreen(_display);
21 _visual = XDefaultVisual(_display, _screen);
23 _blue_mask = _visual->blue_mask;
24 _green_mask = _visual->green_mask;
25 _red_mask = _visual->red_mask;
27 if(_blue_mask == 0 || _green_mask == 0 || _red_mask == 0) {
28 cerr << "Can not deal with the colors on that display.\n";
32 while(!(_red_mask & 1)) {
33 _red_mask = _red_mask >> 1;
34 _red_shift = _red_shift << 1;
38 while(!(_green_mask & 1)) {
39 _green_mask = _green_mask >> 1;
40 _green_shift = _green_shift << 1;
44 while(!(_blue_mask & 1)) {
45 _blue_mask = _blue_mask >> 1;
46 _blue_shift = _blue_shift << 1;
49 _gc = DefaultGC(_display, DefaultScreen(_display));
51 XSetWindowAttributes xswa;
53 _pixmap = XCreatePixmap(_display, DefaultRootWindow(_display),
54 _width, _height, DisplayPlanes(_display, DefaultScreen(_display)));
56 xswa.background_pixmap = _pixmap;
57 xswa.event_mask = ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | KeyPressMask | KeyReleaseMask;
58 xswa.backing_store = Always;
60 _window = XCreateWindow(_display, DefaultRootWindow(_display), x, y, _width, _height,
61 0, 0, InputOutput, CopyFromParent,
62 CWBackPixmap | CWEventMask | CWBackingStore,
65 XSizeHints size_hints;
66 size_hints.flags = PMinSize | PMaxSize | USPosition;
67 size_hints.x = x; // These two lines do not seem to be required
68 size_hints.y = y; // ...
69 size_hints.min_width = _width;
70 size_hints.min_height = _height;
71 size_hints.max_width = _width;
72 size_hints.max_height = _height;
73 XSetNormalHints(_display, _window, &size_hints);
75 XStoreName(_display, _window, name);
77 XSetState(_display, _gc, 0, 0, GXcopy, AllPlanes);
78 XFillRectangle(_display, _pixmap, _gc, 0, 0, _width, _height);
84 SimpleWindow::~SimpleWindow() {
85 XUnmapWindow(_display, _window);
86 XDestroyWindow(_display, _window);
87 XCloseDisplay(_display);
90 int SimpleWindow::width() {
94 int SimpleWindow::height() {
98 void SimpleWindow::map() {
99 XMapWindow(_display, _window);
103 void SimpleWindow::unmap() {
104 XUnmapWindow(_display, _window);
108 void SimpleWindow::color(float red, float green, float blue) {
109 XSetState(_display, _gc,
110 ((unsigned int) ( red * _red_mask)) * _red_shift
111 + ((unsigned int) (green * _green_mask)) * _green_shift
112 + ((unsigned int) ( blue * _blue_mask)) * _blue_shift,
113 0, GXcopy, AllPlanes);
116 void SimpleWindow::draw_point(int x, int y) {
117 XDrawPoint(_display, _pixmap, _gc, x, y);
120 void SimpleWindow::draw_line(int x1, int y1, int x2, int y2) {
121 XDrawLine(_display, _pixmap, _gc, x1, y1, x2, y2);
124 void SimpleWindow::draw_circle(int x, int y, int r) {
125 XDrawArc(_display, _pixmap, _gc, x-r, y-r, 2*r, 2*r, 0, 360*64);
128 void SimpleWindow::draw_text(const char *s, int x, int y) {
129 XDrawString(_display, _pixmap, _gc, x, y, s, strlen(s));
132 void SimpleWindow::fill_rectangle(int x, int y, int w, int h) {
133 XFillRectangle(_display, _pixmap, _gc, x, y, w, h);
136 void SimpleWindow::fill_polygon(int nb, int *x, int *y) {
138 for(int n = 0; n < nb; n++) {
142 XFillPolygon(_display, _pixmap, _gc, points, nb, Nonconvex, CoordModeOrigin);
145 void SimpleWindow::show() {
146 XCopyArea(_display, _pixmap, _window, _gc, 0, 0, _width, _height, 0, 0);
150 void SimpleWindow::fill() {
151 XFillRectangle(_display, _pixmap, _gc, 0, 0, _width, _height);
154 int SimpleWindow::file_descriptor() {
155 return XConnectionNumber(_display);
158 SimpleEvent SimpleWindow::event() {
162 if(XPending(_display) > 0) {
165 XNextEvent(_display, &event);
167 switch (event.type) {
170 se.type = SimpleEvent::MOUSE_CLICK_PRESS;
171 se.button = event.xbutton.button;
172 se.x = event.xbutton.x;
173 se.y = event.xbutton.y;
177 se.type = SimpleEvent::MOUSE_CLICK_RELEASE;
178 se.button = event.xbutton.button;
179 se.x = event.xbutton.x;
180 se.y = event.xbutton.y;
184 se.type = SimpleEvent::MOUSE_MOTION;
185 se.button = event.xbutton.button;
186 se.x = event.xbutton.x;
187 se.y = event.xbutton.y;
191 se.type = SimpleEvent::KEY_PRESS;
192 mykey = XkbKeycodeToKeysym(_display,
193 event.xkey.keycode, 0,
194 event.xkey.state & ShiftMask ? 1 : 0);
195 strncpy(se.key, XKeysymToString(mykey), (sizeof(se.key)/sizeof(char) - 1));
199 se.type = SimpleEvent::KEY_RELEASE;
200 mykey = XkbKeycodeToKeysym(_display,
201 event.xkey.keycode, 0,
202 event.xkey.state & ShiftMask ? 1 : 0);
203 strncpy(se.key, XKeysymToString(mykey), (sizeof(se.key)/sizeof(char) - 1));
207 se.type = SimpleEvent::UNDEFINED;
211 } else se.type = SimpleEvent::NO_EVENT;
216 cairo_t *SimpleWindow::get_cairo_context_resource() {
217 cairo_surface_t *surface;
219 surface = cairo_xlib_surface_create(_display, _pixmap, _visual, _width, _height);
221 cairo_xlib_surface_set_size(surface, _width, _height);
223 return cairo_create(surface);