7791e7989b0208054d539d83476628c536750ff2
[winnie] / src / wm.cc
1 #include <algorithm>
2 #include <limits.h>
3 #include <stdexcept>
4 #include <stdio.h>      // TODO
5
6 #include "gfx.h"
7 #include "mouse.h"
8 #include "mouse_cursor.h"
9 #include "text.h"
10 #include "wm.h"
11 #include "window.h"
12 #include "winnie.h"
13
14 #define DCLICK_INTERVAL 400
15
16 WindowManager *wm;
17
18 static void display(Window *win);
19 static void mouse(Window *win, int bn, bool pressed, int x, int y);
20 static void motion(Window *win, int x, int y);
21
22 bool init_window_manager()
23 {
24         if(!(wm = new WindowManager)) {
25                 return false;
26         }
27
28         return true;
29 }
30
31 void destroy_window_manager()
32 {
33         delete wm;
34 }
35
36 void WindowManager::create_frame(Window *win)
37 {
38         Window *frame = new Window;
39         Window *parent = win->get_parent();
40
41         frame->set_display_callback(display);
42         frame->set_mouse_button_callback(mouse);
43         frame->set_mouse_motion_callback(motion);
44         frame->set_focusable(false);
45         frame->add_child(win);
46
47         windows.push_back(frame);
48
49         Rect win_rect = win->get_rect();
50         frame->move(win_rect.x - frame_thickness,
51                         win_rect.y - frame_thickness - titlebar_thickness);
52         frame->resize(win_rect.width + frame_thickness * 2,
53                         win_rect.height + frame_thickness * 2 + titlebar_thickness);
54
55         win->move(frame_thickness, frame_thickness + titlebar_thickness);
56         parent->add_child(frame);
57 }
58
59 void WindowManager::destroy_frame(Window *win)
60 {
61         Window *frame = win->parent;
62         if(!frame) {
63                 return;
64         }
65
66         if(grab_win == win) {
67                 release_mouse();
68         }
69
70         std::list<Window*>::iterator it;
71         it = std::find(windows.begin(), windows.end(), frame);
72         if(it != windows.end()) {
73                 root_win->add_child(win);
74                 windows.erase(it);
75                 delete frame;
76         }
77 }
78
79 WindowManager::WindowManager()
80 {
81         if(!wm) {
82                 wm = this;
83         } else {
84                 throw std::runtime_error("Trying to create a second instance of WindowManager!\n");
85         }
86
87         root_win = new Window;
88         root_win->resize(get_screen_size().width, get_screen_size().height);
89         root_win->move(0, 0);
90         root_win->set_managed(false);
91
92         grab_win = 0;
93         focused_win = 0;
94
95         bg_color[0] = 210;
96         bg_color[1] = 106;
97         bg_color[2] = 106;
98
99         frame_thickness = 8;
100         titlebar_thickness = 16;
101
102         set_focused_frame_color(0, 0, 0);
103         set_unfocused_frame_color(200, 200, 200);
104
105         mouse_cursor.set_image(mouse_cursor_width, mouse_cursor_height);
106         unsigned char *pixels = mouse_cursor.get_image();
107
108         for(int i=0; i<mouse_cursor_height; i++) {
109                 for(int j=0; j<mouse_cursor_width; j++) {
110                         int val = mouse_cursor_bw[i * mouse_cursor_width + j];
111                         *pixels++ = val;
112                         *pixels++ = val;
113                         *pixels++ = val;
114                         *pixels++ = 255;
115                 }
116         }
117 }
118
119 WindowManager::~WindowManager()
120 {
121         delete root_win;
122 }
123
124 void WindowManager::invalidate_region(const Rect &rect)
125 {
126         dirty_rects.push_back(rect);
127 }
128
129 void WindowManager::process_windows()
130 {
131         if(dirty_rects.empty()) {
132                 return;
133         }
134
135         std::list<Rect>::iterator drit = dirty_rects.begin();
136         Rect uni = *drit++;
137         while(drit != dirty_rects.end()) {
138                 uni = rect_union(uni, *drit++);
139         }
140         dirty_rects.clear();
141
142         wait_vsync();
143
144         fill_rect(uni, bg_color[0], bg_color[1], bg_color[2]);
145
146         root_win->draw_children(uni);
147
148         // draw mouse cursor
149         int mouse_x, mouse_y;
150         get_pointer_pos(&mouse_x, &mouse_y);
151
152         blit_key(mouse_cursor.get_image(), mouse_cursor.get_rect(),
153                         get_framebuffer(), get_screen_size(), mouse_x, mouse_y,
154                         0, 0, 0);
155
156         Rect mouse_rect = {mouse_x, mouse_y, mouse_cursor.get_width(), mouse_cursor.get_height()};
157         invalidate_region(mouse_rect);
158
159         gfx_update();
160 }
161
162 void WindowManager::add_window(Window *win)
163 {
164         if(!win || win == root_win) {
165                 return;
166         }
167
168         root_win->add_child(win);
169
170         if(windows.empty()) {
171                 focused_win = win;
172         }
173
174         if(win->get_managed()) {
175                 create_frame(win);
176         }
177
178         windows.push_back(win);
179 }
180
181 void WindowManager::remove_window(Window *win)
182 {
183         std::list<Window*>::iterator it;
184         it = std::find(windows.begin(), windows.end(), win);
185
186         if(it != windows.end()) {
187                 windows.erase(it);
188         }
189 }
190
191 void WindowManager::set_focused_window(Window *win)
192 {
193         if(win && win == focused_win) {
194                 return;
195         }
196
197         Window *parent;
198         if(focused_win) {
199                 // invalidate the frame (if any)
200                 parent = focused_win->get_parent();
201                 if(parent && parent != root_win) {
202                         parent->invalidate();
203                         fill_rect(parent->get_absolute_rect(), frame_ucolor[0], frame_ucolor[1], frame_ucolor[2]);
204                 }
205         }
206
207         if(!win) {
208                 focused_win = 0;
209                 return;
210         }
211
212         if(win->get_focusable()) {
213                 focused_win = win;
214                 parent = focused_win->get_parent();
215                 fill_rect(parent->get_absolute_rect(), frame_fcolor[0], frame_fcolor[1], frame_fcolor[2]);
216                 return;
217         }
218
219         Window **children = win->get_children();
220         for(int i=0; i<win->get_children_count(); i++) {
221                 if(children[0]->get_focusable()) {
222                         set_focused_window(children[0]);
223                         fill_rect(win->get_absolute_rect(), frame_fcolor[0], frame_fcolor[1], frame_fcolor[2]);
224                         return;
225                 }
226         }
227
228         focused_win = 0;
229 }
230
231 const Window *WindowManager::get_focused_window() const
232 {
233         return focused_win;
234 }
235
236 Window *WindowManager::get_focused_window()
237 {
238         return focused_win;
239 }
240
241 Window *WindowManager::get_window_at_pos(int pointer_x, int pointer_y)
242 {
243         Window *root_win = wm->get_root_window();
244         Window **children = root_win->get_children();
245         for(int i=root_win->get_children_count() - 1; i>=0; i--) {
246                 if(children[i]->contains_point(pointer_x, pointer_y)) {
247                         return children[i];
248                 }
249         }
250
251         return 0;
252 }
253
254 Window *WindowManager::get_root_window() const
255 {
256         return root_win;
257 }
258
259 void WindowManager::set_focused_frame_color(int r, int g, int b)
260 {
261         frame_fcolor[0] = r;
262         frame_fcolor[1] = g;
263         frame_fcolor[2] = b;
264 }
265
266 void WindowManager::get_focused_frame_color(int *r, int *g, int *b) const
267 {
268         *r = frame_fcolor[0];
269         *g = frame_fcolor[1];
270         *b = frame_fcolor[2];
271 }
272
273 void WindowManager::set_unfocused_frame_color(int r, int g, int b)
274 {
275         frame_ucolor[0] = r;
276         frame_ucolor[1] = g;
277         frame_ucolor[2] = b;
278 }
279
280 void WindowManager::get_unfocused_frame_color(int *r, int *g, int *b) const
281 {
282         *r = frame_ucolor[0];
283         *g = frame_ucolor[1];
284         *b = frame_ucolor[2];
285 }
286
287 Window *WindowManager::get_grab_window() const
288 {
289         return grab_win;
290 }
291
292 void WindowManager::grab_mouse(Window *win)
293 {
294         grab_win = win;
295 }
296
297 void WindowManager::release_mouse()
298 {
299         grab_win = 0;
300 }
301
302 void WindowManager::raise_window(Window *win)
303 {
304         if(!win) {
305                 return;
306         }
307
308         Window *parent = win->get_parent();
309         if(parent != root_win) {
310                 if(parent->get_parent() == root_win) {
311                         win = parent;
312                 }
313                 else {
314                         return;
315                 }
316         }
317
318         root_win->remove_child(win);
319         root_win->add_child(win);
320 }
321
322 void WindowManager::sink_window(Window *win)
323 {
324         if(!win) {
325                 return;
326         }
327
328         std::list<Window*>::iterator it;
329         it = std::find(windows.begin(), windows.end(), win);
330         if(it != windows.end()) {
331                 windows.erase(it);
332                 windows.push_front(win);
333         }
334 }
335
336 void WindowManager::maximize_window(Window *win)
337 {
338         win->normal_rect = win->rect;
339         
340         Rect rect = get_screen_size();
341
342         Window *frame;
343         if((frame = win->get_parent())) {
344                 frame->normal_rect = frame->rect;
345                 frame->resize(rect.width, rect.height);
346                 frame->move(rect.x, rect.y);
347
348                 rect.width -= frame_thickness * 2;
349                 rect.height -= frame_thickness * 2 + titlebar_thickness;
350         }
351         else {
352                 rect.x = 0;
353                 rect.y = 0;
354                 win->move(rect.x, rect.y);
355         }
356
357         win->resize(rect.width, rect.height);
358         win->set_state(Window::STATE_MAXIMIZED);
359 }
360
361 void WindowManager::unmaximize_window(Window *win)
362 {
363         win->resize(win->normal_rect.width, win->normal_rect.height);
364         win->move(win->normal_rect.x, win->normal_rect.y);
365
366         Window *frame;
367         if((frame = win->get_parent())) {
368                 frame->resize(frame->normal_rect.width, frame->normal_rect.height);
369                 frame->move(frame->normal_rect.x, frame->normal_rect.y);
370         }
371
372         win->set_state(Window::STATE_NORMAL);
373 }
374
375 static void display(Window *win)
376 {
377         //frame display:
378         Window *child = win->get_children()[0];
379         int r, g, b;
380         Rect abs_rect = win->get_absolute_rect();
381
382         //TODO 5 not hardcoded
383         set_text_position(abs_rect.x + 5, abs_rect.y + 15);
384         set_text_color(255, 255, 255);
385
386         if(child == wm->get_focused_window()) {
387                 wm->get_focused_frame_color(&r, &g, &b);
388                 fill_rect(abs_rect, r, g, b);
389         }
390         else {
391                 wm->get_unfocused_frame_color(&r, &g, &b);
392                 fill_rect(win->get_absolute_rect(), r, g, b);
393         }
394
395         draw_text(child->get_title());
396 }
397
398 static int prev_x, prev_y;
399
400 static void mouse(Window *win, int bn, bool pressed, int x, int y)
401 {
402         static long last_click = 0;
403
404         if(bn == 0) {
405                 if(pressed) {
406                         long time = winnie_get_time();
407                         if((time - last_click) < DCLICK_INTERVAL) {
408                                 Window *child = win->get_children()[0];
409                                 Window::State state = child->get_state();
410                                 if(state == Window::STATE_MAXIMIZED) {
411                                         child->set_state(Window::STATE_NORMAL);
412                                         wm->unmaximize_window(child);
413                                 }
414                                 else if(state == Window::STATE_NORMAL) {
415                                         wm->maximize_window(child);
416                                 }
417                         }
418
419                         wm->grab_mouse(win);
420                         wm->raise_window(win);
421                         prev_x = x;
422                         prev_y = y;
423
424                         last_click = time;
425                 }
426                 else {
427                         wm->release_mouse();
428                 }
429         }
430 }
431
432 static void motion(Window *win, int x, int y)
433 {
434         int left_bn = get_button(0);
435
436         if(left_bn) {
437                 int dx = x - prev_x;
438                 int dy = y - prev_y;
439                 prev_x = x - dx;
440                 prev_y = y - dy;
441
442                 Rect rect = win->get_rect();
443                 win->move(rect.x + dx, rect.y + dy);
444         }
445 }