Cairo support works, both to draw in the window and to write as a png image in a...
[universe.git] / simple_window.cc
1
2 // Written and (C) by Francois Fleuret
3 // Contact <francois.fleuret@idiap.ch> for comments & bug reports
4
5 #include <string.h>
6 #include <stdlib.h>
7
8 #include "simple_window.h"
9
10 SimpleEvent::SimpleEvent() : type(UNDEFINED) {}
11
12 SimpleWindow::SimpleWindow(const char *name, int x, int y, int w, int h) {
13
14   _width = w; _height = h;
15
16   _display = XOpenDisplay(0);
17
18   if (_display) {
19
20     _screen = DefaultScreen(_display);
21     _visual = XDefaultVisual(_display, _screen);
22
23     _blue_mask = _visual->blue_mask;
24     _green_mask = _visual->green_mask;
25     _red_mask = _visual->red_mask;
26
27     if(_blue_mask == 0 || _green_mask == 0 || _red_mask == 0) {
28       cerr << "Can not deal with the colors on that display.\n";
29     }
30
31     _red_shift = 1;
32     while(!(_red_mask & 1)) {
33       _red_mask = _red_mask >> 1;
34       _red_shift = _red_shift << 1;
35     }
36
37     _green_shift = 1;
38     while(!(_green_mask & 1)) {
39       _green_mask = _green_mask >> 1;
40       _green_shift = _green_shift << 1;
41     }
42
43     _blue_shift = 1;
44     while(!(_blue_mask & 1)) {
45       _blue_mask = _blue_mask >> 1;
46       _blue_shift = _blue_shift << 1;
47     }
48
49     _gc = DefaultGC(_display, DefaultScreen(_display));
50
51     XSetWindowAttributes xswa;
52
53     _pixmap = XCreatePixmap(_display, DefaultRootWindow(_display),
54                             _width, _height, DisplayPlanes(_display, DefaultScreen(_display)));
55
56     xswa.background_pixmap = _pixmap;
57     xswa.event_mask = ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | KeyPressMask | KeyReleaseMask;
58     xswa.backing_store = Always;
59
60     _window = XCreateWindow(_display, DefaultRootWindow(_display), x, y, _width, _height,
61                             0, 0, InputOutput, CopyFromParent,
62                             CWBackPixmap | CWEventMask | CWBackingStore,
63                             &xswa);
64
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);
74
75     XStoreName(_display, _window, name);
76
77     XSetState(_display, _gc, 0, 0, GXcopy, AllPlanes);
78     XFillRectangle(_display, _pixmap, _gc, 0, 0, _width, _height);
79     XFlush(_display);
80   } else abort();
81 }
82
83
84 SimpleWindow::~SimpleWindow() {
85   XUnmapWindow(_display, _window);
86   XDestroyWindow(_display, _window);
87   XCloseDisplay(_display);
88 }
89
90 int SimpleWindow::width() {
91   return _width;
92 }
93
94 int SimpleWindow::height() {
95   return _height;
96 }
97
98 void SimpleWindow::map() {
99   XMapWindow(_display, _window);
100   XFlush(_display);
101 }
102
103 void SimpleWindow::unmap() {
104   XUnmapWindow(_display, _window);
105   XFlush(_display);
106 }
107
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);
114 }
115
116 void SimpleWindow::draw_point(int x, int y) {
117   XDrawPoint(_display, _pixmap, _gc, x, y);
118 }
119
120 void SimpleWindow::draw_line(int x1, int y1, int x2, int y2) {
121   XDrawLine(_display, _pixmap, _gc, x1, y1, x2, y2);
122 }
123
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);
126 }
127
128 void SimpleWindow::draw_text(const char *s, int x, int y) {
129   XDrawString(_display, _pixmap, _gc, x, y, s, strlen(s));
130 }
131
132 void SimpleWindow::fill_rectangle(int x, int y, int w, int h) {
133   XFillRectangle(_display, _pixmap, _gc, x, y, w, h);
134 }
135
136 void SimpleWindow::fill_polygon(int nb, int *x, int *y) {
137   XPoint points[nb];
138   for(int n = 0; n < nb; n++) {
139     points[n].x = x[n];
140     points[n].y = y[n];
141   }
142   XFillPolygon(_display, _pixmap, _gc, points, nb, Nonconvex, CoordModeOrigin);
143 }
144
145 void SimpleWindow::show() {
146   XCopyArea(_display, _pixmap, _window, _gc, 0, 0, _width, _height, 0, 0);
147   XFlush(_display);
148 }
149
150 void SimpleWindow::fill() {
151   XFillRectangle(_display, _pixmap, _gc, 0, 0, _width, _height);
152 }
153
154 int  SimpleWindow::file_descriptor() {
155   return XConnectionNumber(_display);
156 }
157
158 SimpleEvent SimpleWindow::event() {
159   SimpleEvent se;
160   KeySym mykey;
161
162   if(XPending(_display) > 0) {
163
164     XEvent event;
165     XNextEvent(_display, &event);
166
167     switch (event.type) {
168
169     case ButtonPress:
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;
174       break;
175
176     case ButtonRelease:
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;
181       break;
182
183     case MotionNotify:
184       se.type = SimpleEvent::MOUSE_MOTION;
185       se.button = event.xbutton.button;
186       se.x = event.xbutton.x;
187       se.y = event.xbutton.y;
188       break;
189
190     case KeyPress:
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));
196       break;
197
198     case KeyRelease:
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));
204       break;
205
206     default:
207       se.type = SimpleEvent::UNDEFINED;
208       break;
209
210     }
211   } else se.type = SimpleEvent::NO_EVENT;
212   return se;
213 }
214
215 #ifdef CAIRO_SUPPORT
216 cairo_t *SimpleWindow::get_cairo_context_resource() {
217   cairo_surface_t *surface;
218
219   surface = cairo_xlib_surface_create(_display, _pixmap, _visual, _width, _height);
220
221   cairo_xlib_surface_set_size(surface, _width, _height);
222
223   return cairo_create(surface);
224 }
225 #endif