shared memory: TODO fix client bug
[winnie] / libwinnie / src / wm.cc
1 /*
2 winnie - an experimental window system
3
4 Copyright (C) 2013 Eleni Maria Stea
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 Author: Eleni Maria Stea <elene.mst@gmail.com>
20 */
21
22 #include <algorithm>
23 #include <limits.h>
24 #include <stdexcept>
25 #include <stdio.h>      // TODO
26
27 #include "gfx.h"
28 #include "mouse.h"
29 #include "mouse_cursor.h"
30 #include "shalloc.h"
31 #include "text.h"
32 #include "window.h"
33 #include "winnie.h"
34 #include "wm.h"
35
36 #define DCLICK_INTERVAL 400
37
38 WindowManager *wm;
39
40 static void display(Window *win);
41 static void mouse(Window *win, int bn, bool pressed, int x, int y);
42 static void motion(Window *win, int x, int y);
43
44 bool init_window_manager()
45 {
46         void *wm_mem;
47         if(!(wm_mem = sh_malloc(sizeof *wm))) {
48                 return false;
49         }
50
51         wm = new (wm_mem) WindowManager; 
52
53         get_subsys()->wm_offset = (int)((char*)wm - (char*)get_pool());
54
55         return true;
56 }
57
58 void destroy_window_manager()
59 {
60         wm->~WindowManager();
61         sh_free(wm);
62 }
63
64
65 bool client_open_wm(void *smem_start, int offset)
66 {
67         wm = (WindowManager*) ((unsigned char*)smem_start + offset);
68         return true;
69 }
70
71 void client_close_wm()
72 {
73 }
74
75 void WindowManager::create_frame(Window *win)
76 {
77         Window *frame = new Window;
78         Window *parent = win->get_parent();
79
80         frame->set_display_callback(display);
81         frame->set_mouse_button_callback(mouse);
82         frame->set_mouse_motion_callback(motion);
83         frame->set_focusable(false);
84         frame->add_child(win);
85
86         windows.push_back(frame);
87
88         Rect win_rect = win->get_rect();
89         frame->move(win_rect.x - frame_thickness,
90                         win_rect.y - frame_thickness - titlebar_thickness);
91         frame->resize(win_rect.width + frame_thickness * 2,
92                         win_rect.height + frame_thickness * 2 + titlebar_thickness);
93
94         win->move(frame_thickness, frame_thickness + titlebar_thickness);
95         parent->add_child(frame);
96 }
97
98 void WindowManager::destroy_frame(Window *win)
99 {
100         Window *frame = win->parent;
101         if(!frame) {
102                 return;
103         }
104
105         if(grab_win == win) {
106                 release_mouse();
107         }
108
109         std::list<Window*>::iterator it;
110         it = std::find(windows.begin(), windows.end(), frame);
111         if(it != windows.end()) {
112                 root_win->add_child(win);
113                 windows.erase(it);
114                 delete frame;
115         }
116 }
117
118 WindowManager::WindowManager()
119 {
120         if(!wm) {
121                 wm = this;
122         } else {
123                 throw std::runtime_error("Trying to create a second instance of WindowManager!\n");
124         }
125
126         root_win = new Window;
127         root_win->resize(get_screen_size().width, get_screen_size().height);
128         root_win->move(0, 0);
129         root_win->set_managed(false);
130
131         grab_win = 0;
132         focused_win = 0;
133         background = 0;
134
135         bg_color[0] = 210;
136         bg_color[1] = 106;
137         bg_color[2] = 106;
138
139         frame_thickness = 8;
140         titlebar_thickness = 16;
141
142         set_focused_frame_color(0, 0, 0);
143         set_unfocused_frame_color(200, 200, 200);
144
145         mouse_cursor.set_image(mouse_cursor_width, mouse_cursor_height);
146         unsigned char *pixels = mouse_cursor.get_image();
147
148         for(int i=0; i<mouse_cursor_height; i++) {
149                 for(int j=0; j<mouse_cursor_width; j++) {
150                         int val = mouse_cursor_bw[i * mouse_cursor_width + j];
151                         *pixels++ = val;
152                         *pixels++ = val;
153                         *pixels++ = val;
154                         *pixels++ = 255;
155                 }
156         }
157 }
158
159 WindowManager::~WindowManager()
160 {
161         delete root_win;
162 }
163
164 void WindowManager::invalidate_region(const Rect &rect)
165 {
166         dirty_rects.push_back(rect);
167 }
168
169 void WindowManager::process_windows()
170 {
171         if(dirty_rects.empty()) {
172                 return;
173         }
174
175         std::list<Rect>::iterator drit = dirty_rects.begin();
176         Rect uni = *drit++;
177         while(drit != dirty_rects.end()) {
178                 uni = rect_union(uni, *drit++);
179         }
180         dirty_rects.clear();
181
182         wait_vsync();
183
184         if(!background) {
185                 fill_rect(uni, bg_color[0], bg_color[1], bg_color[2]);
186         }
187         else {
188                 blit(background->pixels, Rect(0, 0, background->width, background->height),
189                                 get_framebuffer(), get_screen_size(), 0, 0);
190         }
191
192         root_win->draw_children(uni);
193
194         // draw mouse cursor
195         int mouse_x, mouse_y;
196         get_pointer_pos(&mouse_x, &mouse_y);
197
198         blit_key(mouse_cursor.get_image(), mouse_cursor.get_rect(),
199                         get_framebuffer(), get_screen_size(), mouse_x, mouse_y,
200                         0, 0, 0);
201
202         Rect mouse_rect(mouse_x, mouse_y, mouse_cursor.get_width(), mouse_cursor.get_height());
203         invalidate_region(mouse_rect);
204
205         gfx_update(uni);
206 }
207
208 void WindowManager::add_window(Window *win)
209 {
210         if(!win || win == root_win) {
211                 return;
212         }
213
214         root_win->add_child(win);
215
216         if(windows.empty()) {
217                 focused_win = win;
218         }
219
220         if(win->get_managed()) {
221                 create_frame(win);
222         }
223
224         windows.push_back(win);
225 }
226
227 void WindowManager::remove_window(Window *win)
228 {
229         std::list<Window*>::iterator it;
230         it = std::find(windows.begin(), windows.end(), win);
231
232         if(it != windows.end()) {
233                 windows.erase(it);
234         }
235 }
236
237 void WindowManager::set_focused_window(Window *win)
238 {
239         if(win && win == focused_win) {
240                 return;
241         }
242
243         Window *parent;
244         if(focused_win) {
245                 // invalidate the frame (if any)
246                 parent = focused_win->get_parent();
247                 if(parent && parent != root_win) {
248                         parent->invalidate();
249                         fill_rect(parent->get_absolute_rect(), frame_ucolor[0], frame_ucolor[1], frame_ucolor[2]);
250                 }
251         }
252
253         if(!win) {
254                 focused_win = 0;
255                 return;
256         }
257
258         if(win->get_focusable()) {
259                 focused_win = win;
260                 parent = focused_win->get_parent();
261                 fill_rect(parent->get_absolute_rect(), frame_fcolor[0], frame_fcolor[1], frame_fcolor[2]);
262                 return;
263         }
264
265         Window **children = win->get_children();
266         for(int i=0; i<win->get_children_count(); i++) {
267                 if(children[0]->get_focusable()) {
268                         set_focused_window(children[0]);
269                         fill_rect(win->get_absolute_rect(), frame_fcolor[0], frame_fcolor[1], frame_fcolor[2]);
270                         return;
271                 }
272         }
273
274         focused_win = 0;
275 }
276
277 const Window *WindowManager::get_focused_window() const
278 {
279         return focused_win;
280 }
281
282 Window *WindowManager::get_focused_window()
283 {
284         return focused_win;
285 }
286
287 Window *WindowManager::get_window_at_pos(int pointer_x, int pointer_y)
288 {
289         Window *root_win = wm->get_root_window();
290         Window **children = root_win->get_children();
291         for(int i=root_win->get_children_count() - 1; i>=0; i--) {
292                 if(children[i]->contains_point(pointer_x, pointer_y)) {
293                         return children[i];
294                 }
295         }
296
297         return 0;
298 }
299
300 Window *WindowManager::get_root_window() const
301 {
302         return root_win;
303 }
304
305 void WindowManager::set_focused_frame_color(int r, int g, int b)
306 {
307         frame_fcolor[0] = r;
308         frame_fcolor[1] = g;
309         frame_fcolor[2] = b;
310 }
311
312 void WindowManager::get_focused_frame_color(int *r, int *g, int *b) const
313 {
314         *r = frame_fcolor[0];
315         *g = frame_fcolor[1];
316         *b = frame_fcolor[2];
317 }
318
319 void WindowManager::set_unfocused_frame_color(int r, int g, int b)
320 {
321         frame_ucolor[0] = r;
322         frame_ucolor[1] = g;
323         frame_ucolor[2] = b;
324 }
325
326 void WindowManager::get_unfocused_frame_color(int *r, int *g, int *b) const
327 {
328         *r = frame_ucolor[0];
329         *g = frame_ucolor[1];
330         *b = frame_ucolor[2];
331 }
332
333 void WindowManager::set_background(const Pixmap *pixmap)
334 {
335         if(background) {
336                 delete background;
337         }
338
339         if(pixmap) {
340                 background = new Pixmap(*pixmap);
341         }
342         else {
343                 background = 0;
344         }
345 }
346
347 const Pixmap *WindowManager::get_background() const
348 {
349         return background;
350 }
351
352 Window *WindowManager::get_grab_window() const
353 {
354         return grab_win;
355 }
356
357 void WindowManager::grab_mouse(Window *win)
358 {
359         grab_win = win;
360 }
361
362 void WindowManager::release_mouse()
363 {
364         grab_win = 0;
365 }
366
367 void WindowManager::raise_window(Window *win)
368 {
369         if(!win) {
370                 return;
371         }
372
373         Window *parent = win->get_parent();
374         if(parent != root_win) {
375                 if(parent->get_parent() == root_win) {
376                         win = parent;
377                 }
378                 else {
379                         return;
380                 }
381         }
382
383         root_win->remove_child(win);
384         root_win->add_child(win);
385 }
386
387 void WindowManager::sink_window(Window *win)
388 {
389         if(!win) {
390                 return;
391         }
392
393         std::list<Window*>::iterator it;
394         it = std::find(windows.begin(), windows.end(), win);
395         if(it != windows.end()) {
396                 windows.erase(it);
397                 windows.push_front(win);
398         }
399 }
400
401 void WindowManager::maximize_window(Window *win)
402 {
403         win->normal_rect = win->rect;
404         
405         Rect rect = get_screen_size();
406
407         Window *frame;
408         if((frame = win->get_parent())) {
409                 frame->normal_rect = frame->rect;
410                 frame->resize(rect.width, rect.height);
411                 frame->move(rect.x, rect.y);
412
413                 rect.width -= frame_thickness * 2;
414                 rect.height -= frame_thickness * 2 + titlebar_thickness;
415         }
416         else {
417                 win->move(0, 0);
418         }
419
420         win->resize(rect.width, rect.height);
421         win->set_state(Window::STATE_MAXIMIZED);
422 }
423
424 void WindowManager::unmaximize_window(Window *win)
425 {
426         win->resize(win->normal_rect.width, win->normal_rect.height);
427         win->move(win->normal_rect.x, win->normal_rect.y);
428
429         Window *frame;
430         if((frame = win->get_parent())) {
431                 frame->resize(frame->normal_rect.width, frame->normal_rect.height);
432                 frame->move(frame->normal_rect.x, frame->normal_rect.y);
433         }
434
435         win->set_state(Window::STATE_NORMAL);
436 }
437
438 static void display(Window *win)
439 {
440         //frame display:
441         Window *child = win->get_children()[0];
442         int r, g, b;
443         Rect abs_rect = win->get_absolute_rect();
444
445         //TODO 5 not hardcoded
446         set_text_position(abs_rect.x + 5, abs_rect.y + 15);
447         set_text_color(255, 255, 255);
448
449         if(child == wm->get_focused_window()) {
450                 wm->get_focused_frame_color(&r, &g, &b);
451                 fill_rect(abs_rect, r, g, b);
452         }
453         else {
454                 wm->get_unfocused_frame_color(&r, &g, &b);
455                 fill_rect(win->get_absolute_rect(), r, g, b);
456         }
457
458         draw_text(child->get_title());
459 }
460
461 static int prev_x, prev_y;
462
463 static void mouse(Window *win, int bn, bool pressed, int x, int y)
464 {
465         static long last_click = 0;
466
467         if(bn == 0) {
468                 if(pressed) {   
469                         wm->grab_mouse(win);
470                         wm->raise_window(win);
471                         prev_x = x;
472                         prev_y = y;
473                 }
474                 else {
475                         long time = winnie_get_time();
476                         if((time - last_click) < DCLICK_INTERVAL) {
477                                 Window *child = win->get_children()[0];
478                                 Window::State state = child->get_state();
479                                 if(state == Window::STATE_MAXIMIZED) {
480                                         wm->unmaximize_window(child);
481                                 }
482                                 else if(state == Window::STATE_NORMAL) {
483                                         wm->maximize_window(child);
484                                 }
485                         }
486                         last_click = time;
487
488                         wm->release_mouse();
489                 }
490         }
491 }
492
493 static void motion(Window *win, int x, int y)
494 {
495         int left_bn = get_button(0);
496
497         if(left_bn) {
498                 int dx = x - prev_x;
499                 int dy = y - prev_y;
500                 prev_x = x - dx;
501                 prev_y = y - dy;
502
503                 if(win->get_children()[0]->get_state() != Window::STATE_MAXIMIZED) {
504                         Rect rect = win->get_rect();
505                         win->move(rect.x + dx, rect.y + dy);
506                 }
507         }
508 }