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