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 Visual *v = XDefaultVisual(_display, DefaultScreen(_display));
22 _blue_mask = v->blue_mask;
23 _green_mask = v->green_mask;
24 _red_mask = v->red_mask;
26 if(_blue_mask == 0 || _green_mask == 0 || _red_mask == 0) {
27 cerr << "Can not deal with the colors on that display.\n";
31 while(!(_red_mask & 1)) {
32 _red_mask = _red_mask >> 1;
33 _red_shift = _red_shift << 1;
37 while(!(_green_mask & 1)) {
38 _green_mask = _green_mask >> 1;
39 _green_shift = _green_shift << 1;
43 while(!(_blue_mask & 1)) {
44 _blue_mask = _blue_mask >> 1;
45 _blue_shift = _blue_shift << 1;
48 _gc = DefaultGC(_display, DefaultScreen(_display));
50 XSetWindowAttributes xswa;
52 _pixmap = XCreatePixmap(_display, DefaultRootWindow(_display),
53 _width, _height, DisplayPlanes(_display, DefaultScreen(_display)));
55 xswa.background_pixmap = _pixmap;
56 xswa.event_mask = ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | KeyPressMask | KeyReleaseMask;
57 xswa.backing_store = Always;
59 _window = XCreateWindow(_display, DefaultRootWindow(_display), x, y, _width, _height,
60 0, 0, InputOutput, CopyFromParent,
61 CWBackPixmap | CWEventMask | CWBackingStore,
64 XSizeHints size_hints;
65 size_hints.flags = PMinSize | PMaxSize | USPosition;
66 size_hints.x = x; // These two lines do not seem to be required
67 size_hints.y = y; // ...
68 size_hints.min_width = _width;
69 size_hints.min_height = _height;
70 size_hints.max_width = _width;
71 size_hints.max_height = _height;
72 XSetNormalHints(_display, _window, &size_hints);
74 XStoreName(_display, _window, name);
76 XSetState(_display, _gc, 0, 0, GXcopy, AllPlanes);
77 XFillRectangle(_display, _pixmap, _gc, 0, 0, _width, _height);
83 SimpleWindow::~SimpleWindow() {
84 XUnmapWindow(_display, _window);
85 XDestroyWindow(_display, _window);
86 XCloseDisplay(_display);
89 int SimpleWindow::width() {
93 int SimpleWindow::height() {
97 void SimpleWindow::map() {
98 XMapWindow(_display, _window);
102 void SimpleWindow::unmap() {
103 XUnmapWindow(_display, _window);
107 void SimpleWindow::color(float red, float green, float blue) {
108 XSetState(_display, _gc,
109 ((unsigned int) ( red * _red_mask)) * _red_shift
110 + ((unsigned int) (green * _green_mask)) * _green_shift
111 + ((unsigned int) ( blue * _blue_mask)) * _blue_shift,
112 0, GXcopy, AllPlanes);
115 void SimpleWindow::draw_point(int x, int y) {
116 XDrawPoint(_display, _pixmap, _gc, x, y);
119 void SimpleWindow::draw_line(int x1, int y1, int x2, int y2) {
120 XDrawLine(_display, _pixmap, _gc, x1, y1, x2, y2);
123 void SimpleWindow::draw_circle(int x, int y, int r) {
124 XDrawArc(_display, _pixmap, _gc, x-r, y-r, 2*r, 2*r, 0, 360*64);
127 void SimpleWindow::draw_text(const char *s, int x, int y) {
128 XDrawString(_display, _pixmap, _gc, x, y, s, strlen(s));
131 void SimpleWindow::fill_rectangle(int x, int y, int w, int h) {
132 XFillRectangle(_display, _pixmap, _gc, x, y, w, h);
135 void SimpleWindow::fill_polygon(int nb, int *x, int *y) {
137 for(int n = 0; n < nb; n++) {
141 XFillPolygon(_display, _pixmap, _gc, points, nb, Nonconvex, CoordModeOrigin);
144 void SimpleWindow::show() {
145 XCopyArea(_display, _pixmap, _window, _gc, 0, 0, _width, _height, 0, 0);
149 void SimpleWindow::fill() {
150 XFillRectangle(_display, _pixmap, _gc, 0, 0, _width, _height);
153 int SimpleWindow::file_descriptor() {
154 return XConnectionNumber(_display);
157 SimpleEvent SimpleWindow::event() {
161 if(XPending(_display) > 0) {
164 XNextEvent(_display, &event);
166 switch (event.type) {
169 se.type = SimpleEvent::MOUSE_CLICK_PRESS;
170 se.button = event.xbutton.button;
171 se.x = event.xbutton.x;
172 se.y = event.xbutton.y;
176 se.type = SimpleEvent::MOUSE_CLICK_RELEASE;
177 se.button = event.xbutton.button;
178 se.x = event.xbutton.x;
179 se.y = event.xbutton.y;
183 se.type = SimpleEvent::MOUSE_MOTION;
184 se.button = event.xbutton.button;
185 se.x = event.xbutton.x;
186 se.y = event.xbutton.y;
190 se.type = SimpleEvent::KEY_PRESS;
191 mykey = XkbKeycodeToKeysym(_display,
192 event.xkey.keycode, 0,
193 event.xkey.state & ShiftMask ? 1 : 0);
194 strncpy(se.key, XKeysymToString(mykey), (sizeof(se.key)/sizeof(char) - 1));
198 se.type = SimpleEvent::KEY_RELEASE;
199 mykey = XkbKeycodeToKeysym(_display,
200 event.xkey.keycode, 0,
201 event.xkey.state & ShiftMask ? 1 : 0);
202 strncpy(se.key, XKeysymToString(mykey), (sizeof(se.key)/sizeof(char) - 1));
206 se.type = SimpleEvent::UNDEFINED;
210 } else se.type = SimpleEvent::NO_EVENT;