2 winnie - an experimental window system
4 Copyright (C) 2013 Eleni Maria Stea
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.
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.
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/>.
19 Author: Eleni Maria Stea <elene.mst@gmail.com>
25 #include <stdio.h> // TODO
29 #include "mouse_cursor.h"
35 #define DCLICK_INTERVAL 400
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);
43 bool init_window_manager()
45 if(!(wm = new WindowManager)) {
52 void destroy_window_manager()
57 void WindowManager::create_frame(Window *win)
59 Window *frame = new Window;
60 Window *parent = win->get_parent();
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);
68 windows.push_back(frame);
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);
76 win->move(frame_thickness, frame_thickness + titlebar_thickness);
77 parent->add_child(frame);
80 void WindowManager::destroy_frame(Window *win)
82 Window *frame = win->parent;
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);
100 WindowManager::WindowManager()
105 throw std::runtime_error("Trying to create a second instance of WindowManager!\n");
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);
122 titlebar_thickness = 16;
124 set_focused_frame_color(0, 0, 0);
125 set_unfocused_frame_color(200, 200, 200);
127 mouse_cursor.set_image(mouse_cursor_width, mouse_cursor_height);
128 unsigned char *pixels = mouse_cursor.get_image();
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];
141 WindowManager::~WindowManager()
146 void WindowManager::invalidate_region(const Rect &rect)
148 dirty_rects.push_back(rect);
151 void WindowManager::process_windows()
153 if(dirty_rects.empty()) {
157 std::list<Rect>::iterator drit = dirty_rects.begin();
159 while(drit != dirty_rects.end()) {
160 uni = rect_union(uni, *drit++);
167 fill_rect(uni, bg_color[0], bg_color[1], bg_color[2]);
170 blit(background->pixels, Rect(0, 0, background->width, background->height),
171 get_framebuffer(), get_screen_size(), 0, 0);
174 root_win->draw_children(uni);
177 int mouse_x, mouse_y;
178 get_pointer_pos(&mouse_x, &mouse_y);
180 blit_key(mouse_cursor.get_image(), mouse_cursor.get_rect(),
181 get_framebuffer(), get_screen_size(), mouse_x, mouse_y,
184 Rect mouse_rect(mouse_x, mouse_y, mouse_cursor.get_width(), mouse_cursor.get_height());
185 invalidate_region(mouse_rect);
190 void WindowManager::add_window(Window *win)
192 if(!win || win == root_win) {
196 root_win->add_child(win);
198 if(windows.empty()) {
202 if(win->get_managed()) {
206 windows.push_back(win);
209 void WindowManager::remove_window(Window *win)
211 std::list<Window*>::iterator it;
212 it = std::find(windows.begin(), windows.end(), win);
214 if(it != windows.end()) {
219 void WindowManager::set_focused_window(Window *win)
221 if(win && win == 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]);
240 if(win->get_focusable()) {
242 parent = focused_win->get_parent();
243 fill_rect(parent->get_absolute_rect(), frame_fcolor[0], frame_fcolor[1], frame_fcolor[2]);
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]);
259 const Window *WindowManager::get_focused_window() const
264 Window *WindowManager::get_focused_window()
269 Window *WindowManager::get_window_at_pos(int pointer_x, int pointer_y)
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)) {
282 Window *WindowManager::get_root_window() const
287 void WindowManager::set_focused_frame_color(int r, int g, int b)
294 void WindowManager::get_focused_frame_color(int *r, int *g, int *b) const
296 *r = frame_fcolor[0];
297 *g = frame_fcolor[1];
298 *b = frame_fcolor[2];
301 void WindowManager::set_unfocused_frame_color(int r, int g, int b)
308 void WindowManager::get_unfocused_frame_color(int *r, int *g, int *b) const
310 *r = frame_ucolor[0];
311 *g = frame_ucolor[1];
312 *b = frame_ucolor[2];
315 void WindowManager::set_background(const Pixmap *pixmap)
322 background = new Pixmap(*pixmap);
329 const Pixmap *WindowManager::get_background() const
334 Window *WindowManager::get_grab_window() const
339 void WindowManager::grab_mouse(Window *win)
344 void WindowManager::release_mouse()
349 void WindowManager::raise_window(Window *win)
355 Window *parent = win->get_parent();
356 if(parent != root_win) {
357 if(parent->get_parent() == root_win) {
365 root_win->remove_child(win);
366 root_win->add_child(win);
369 void WindowManager::sink_window(Window *win)
375 std::list<Window*>::iterator it;
376 it = std::find(windows.begin(), windows.end(), win);
377 if(it != windows.end()) {
379 windows.push_front(win);
383 void WindowManager::maximize_window(Window *win)
385 win->normal_rect = win->rect;
387 Rect rect = get_screen_size();
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);
395 rect.width -= frame_thickness * 2;
396 rect.height -= frame_thickness * 2 + titlebar_thickness;
402 win->resize(rect.width, rect.height);
403 win->set_state(Window::STATE_MAXIMIZED);
406 void WindowManager::unmaximize_window(Window *win)
408 win->resize(win->normal_rect.width, win->normal_rect.height);
409 win->move(win->normal_rect.x, win->normal_rect.y);
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);
417 win->set_state(Window::STATE_NORMAL);
420 static void display(Window *win)
423 Window *child = win->get_children()[0];
425 Rect abs_rect = win->get_absolute_rect();
427 //TODO 5 not hardcoded
428 set_text_position(abs_rect.x + 5, abs_rect.y + 15);
429 set_text_color(255, 255, 255);
431 if(child == wm->get_focused_window()) {
432 wm->get_focused_frame_color(&r, &g, &b);
433 fill_rect(abs_rect, r, g, b);
436 wm->get_unfocused_frame_color(&r, &g, &b);
437 fill_rect(win->get_absolute_rect(), r, g, b);
440 draw_text(child->get_title());
443 static int prev_x, prev_y;
445 static void mouse(Window *win, int bn, bool pressed, int x, int y)
447 static long last_click = 0;
452 wm->raise_window(win);
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);
464 else if(state == Window::STATE_NORMAL) {
465 wm->maximize_window(child);
475 static void motion(Window *win, int x, int y)
477 int left_bn = get_button(0);
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);