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"
36 #define DCLICK_INTERVAL 400
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);
44 bool init_window_manager()
47 if(!(wm_mem = sh_malloc(sizeof *wm))) {
51 wm = new (wm_mem) WindowManager;
53 get_subsys()->wm_offset = (int)((char*)wm - (char*)get_pool());
58 void destroy_window_manager()
64 void WindowManager::create_frame(Window *win)
66 Window *frame = new Window;
67 Window *parent = win->get_parent();
69 frame->set_display_callback(display);
70 frame->set_mouse_button_callback(mouse);
71 frame->set_mouse_motion_callback(motion);
72 frame->set_focusable(false);
73 frame->add_child(win);
75 windows.push_back(frame);
77 Rect win_rect = win->get_rect();
78 frame->move(win_rect.x - frame_thickness,
79 win_rect.y - frame_thickness - titlebar_thickness);
80 frame->resize(win_rect.width + frame_thickness * 2,
81 win_rect.height + frame_thickness * 2 + titlebar_thickness);
83 win->move(frame_thickness, frame_thickness + titlebar_thickness);
84 parent->add_child(frame);
87 void WindowManager::destroy_frame(Window *win)
89 Window *frame = win->parent;
98 std::list<Window*>::iterator it;
99 it = std::find(windows.begin(), windows.end(), frame);
100 if(it != windows.end()) {
101 root_win->add_child(win);
107 WindowManager::WindowManager()
112 throw std::runtime_error("Trying to create a second instance of WindowManager!\n");
115 root_win = new Window;
116 root_win->resize(get_screen_size().width, get_screen_size().height);
117 root_win->move(0, 0);
118 root_win->set_managed(false);
129 titlebar_thickness = 16;
131 set_focused_frame_color(0, 0, 0);
132 set_unfocused_frame_color(200, 200, 200);
134 mouse_cursor.set_image(mouse_cursor_width, mouse_cursor_height);
135 unsigned char *pixels = mouse_cursor.get_image();
137 for(int i=0; i<mouse_cursor_height; i++) {
138 for(int j=0; j<mouse_cursor_width; j++) {
139 int val = mouse_cursor_bw[i * mouse_cursor_width + j];
148 WindowManager::~WindowManager()
153 void WindowManager::invalidate_region(const Rect &rect)
155 dirty_rects.push_back(rect);
158 void WindowManager::process_windows()
160 if(dirty_rects.empty()) {
164 std::list<Rect>::iterator drit = dirty_rects.begin();
166 while(drit != dirty_rects.end()) {
167 uni = rect_union(uni, *drit++);
174 fill_rect(uni, bg_color[0], bg_color[1], bg_color[2]);
177 blit(background->pixels, Rect(0, 0, background->width, background->height),
178 get_framebuffer(), get_screen_size(), 0, 0);
181 root_win->draw_children(uni);
184 int mouse_x, mouse_y;
185 get_pointer_pos(&mouse_x, &mouse_y);
187 blit_key(mouse_cursor.get_image(), mouse_cursor.get_rect(),
188 get_framebuffer(), get_screen_size(), mouse_x, mouse_y,
191 Rect mouse_rect(mouse_x, mouse_y, mouse_cursor.get_width(), mouse_cursor.get_height());
192 invalidate_region(mouse_rect);
197 void WindowManager::add_window(Window *win)
199 if(!win || win == root_win) {
203 root_win->add_child(win);
205 if(windows.empty()) {
209 if(win->get_managed()) {
213 windows.push_back(win);
216 void WindowManager::remove_window(Window *win)
218 std::list<Window*>::iterator it;
219 it = std::find(windows.begin(), windows.end(), win);
221 if(it != windows.end()) {
226 void WindowManager::set_focused_window(Window *win)
228 if(win && win == focused_win) {
234 // invalidate the frame (if any)
235 parent = focused_win->get_parent();
236 if(parent && parent != root_win) {
237 parent->invalidate();
238 fill_rect(parent->get_absolute_rect(), frame_ucolor[0], frame_ucolor[1], frame_ucolor[2]);
247 if(win->get_focusable()) {
249 parent = focused_win->get_parent();
250 fill_rect(parent->get_absolute_rect(), frame_fcolor[0], frame_fcolor[1], frame_fcolor[2]);
254 Window **children = win->get_children();
255 for(int i=0; i<win->get_children_count(); i++) {
256 if(children[0]->get_focusable()) {
257 set_focused_window(children[0]);
258 fill_rect(win->get_absolute_rect(), frame_fcolor[0], frame_fcolor[1], frame_fcolor[2]);
266 const Window *WindowManager::get_focused_window() const
271 Window *WindowManager::get_focused_window()
276 Window *WindowManager::get_window_at_pos(int pointer_x, int pointer_y)
278 Window *root_win = wm->get_root_window();
279 Window **children = root_win->get_children();
280 for(int i=root_win->get_children_count() - 1; i>=0; i--) {
281 if(children[i]->contains_point(pointer_x, pointer_y)) {
289 Window *WindowManager::get_root_window() const
294 void WindowManager::set_focused_frame_color(int r, int g, int b)
301 void WindowManager::get_focused_frame_color(int *r, int *g, int *b) const
303 *r = frame_fcolor[0];
304 *g = frame_fcolor[1];
305 *b = frame_fcolor[2];
308 void WindowManager::set_unfocused_frame_color(int r, int g, int b)
315 void WindowManager::get_unfocused_frame_color(int *r, int *g, int *b) const
317 *r = frame_ucolor[0];
318 *g = frame_ucolor[1];
319 *b = frame_ucolor[2];
322 void WindowManager::set_background(const Pixmap *pixmap)
329 background = new Pixmap(*pixmap);
336 const Pixmap *WindowManager::get_background() const
341 Window *WindowManager::get_grab_window() const
346 void WindowManager::grab_mouse(Window *win)
351 void WindowManager::release_mouse()
356 void WindowManager::raise_window(Window *win)
362 Window *parent = win->get_parent();
363 if(parent != root_win) {
364 if(parent->get_parent() == root_win) {
372 root_win->remove_child(win);
373 root_win->add_child(win);
376 void WindowManager::sink_window(Window *win)
382 std::list<Window*>::iterator it;
383 it = std::find(windows.begin(), windows.end(), win);
384 if(it != windows.end()) {
386 windows.push_front(win);
390 void WindowManager::maximize_window(Window *win)
392 win->normal_rect = win->rect;
394 Rect rect = get_screen_size();
397 if((frame = win->get_parent())) {
398 frame->normal_rect = frame->rect;
399 frame->resize(rect.width, rect.height);
400 frame->move(rect.x, rect.y);
402 rect.width -= frame_thickness * 2;
403 rect.height -= frame_thickness * 2 + titlebar_thickness;
409 win->resize(rect.width, rect.height);
410 win->set_state(Window::STATE_MAXIMIZED);
413 void WindowManager::unmaximize_window(Window *win)
415 win->resize(win->normal_rect.width, win->normal_rect.height);
416 win->move(win->normal_rect.x, win->normal_rect.y);
419 if((frame = win->get_parent())) {
420 frame->resize(frame->normal_rect.width, frame->normal_rect.height);
421 frame->move(frame->normal_rect.x, frame->normal_rect.y);
424 win->set_state(Window::STATE_NORMAL);
427 static void display(Window *win)
430 Window *child = win->get_children()[0];
432 Rect abs_rect = win->get_absolute_rect();
434 //TODO 5 not hardcoded
435 set_text_position(abs_rect.x + 5, abs_rect.y + 15);
436 set_text_color(255, 255, 255);
438 if(child == wm->get_focused_window()) {
439 wm->get_focused_frame_color(&r, &g, &b);
440 fill_rect(abs_rect, r, g, b);
443 wm->get_unfocused_frame_color(&r, &g, &b);
444 fill_rect(win->get_absolute_rect(), r, g, b);
447 draw_text(child->get_title());
450 static int prev_x, prev_y;
452 static void mouse(Window *win, int bn, bool pressed, int x, int y)
454 static long last_click = 0;
459 wm->raise_window(win);
464 long time = winnie_get_time();
465 if((time - last_click) < DCLICK_INTERVAL) {
466 Window *child = win->get_children()[0];
467 Window::State state = child->get_state();
468 if(state == Window::STATE_MAXIMIZED) {
469 wm->unmaximize_window(child);
471 else if(state == Window::STATE_NORMAL) {
472 wm->maximize_window(child);
482 static void motion(Window *win, int x, int y)
484 int left_bn = get_button(0);
492 if(win->get_children()[0]->get_state() != Window::STATE_MAXIMIZED) {
493 Rect rect = win->get_rect();
494 win->move(rect.x + dx, rect.y + dy);