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()
65 bool client_open_wm(void *smem_start, int offset)
67 wm = (WindowManager*) ((unsigned char*)smem_start + offset);
71 void client_close_wm()
75 void WindowManager::create_frame(Window *win)
77 Window *frame = new Window;
78 Window *parent = win->get_parent();
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);
86 windows.push_back(frame);
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);
94 win->move(frame_thickness, frame_thickness + titlebar_thickness);
95 parent->add_child(frame);
98 void WindowManager::destroy_frame(Window *win)
100 Window *frame = win->parent;
105 if(grab_win == win) {
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);
118 WindowManager::WindowManager()
123 throw std::runtime_error("Trying to create a second instance of WindowManager!\n");
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);
140 titlebar_thickness = 16;
142 set_focused_frame_color(0, 0, 0);
143 set_unfocused_frame_color(200, 200, 200);
145 mouse_cursor.set_image(mouse_cursor_width, mouse_cursor_height);
146 unsigned char *pixels = mouse_cursor.get_image();
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];
159 WindowManager::~WindowManager()
164 void WindowManager::invalidate_region(const Rect &rect)
166 dirty_rects.push_back(rect);
169 void WindowManager::process_windows()
171 if(dirty_rects.empty()) {
175 std::list<Rect>::iterator drit = dirty_rects.begin();
177 while(drit != dirty_rects.end()) {
178 uni = rect_union(uni, *drit++);
185 fill_rect(uni, bg_color[0], bg_color[1], bg_color[2]);
188 blit(background->pixels, Rect(0, 0, background->width, background->height),
189 get_framebuffer(), get_screen_size(), 0, 0);
192 root_win->draw_children(uni);
195 int mouse_x, mouse_y;
196 get_pointer_pos(&mouse_x, &mouse_y);
198 blit_key(mouse_cursor.get_image(), mouse_cursor.get_rect(),
199 get_framebuffer(), get_screen_size(), mouse_x, mouse_y,
202 Rect mouse_rect(mouse_x, mouse_y, mouse_cursor.get_width(), mouse_cursor.get_height());
203 invalidate_region(mouse_rect);
208 void WindowManager::add_window(Window *win)
210 if(!win || win == root_win) {
214 root_win->add_child(win);
216 if(windows.empty()) {
220 if(win->get_managed()) {
224 windows.push_back(win);
227 void WindowManager::remove_window(Window *win)
229 std::list<Window*>::iterator it;
230 it = std::find(windows.begin(), windows.end(), win);
232 if(it != windows.end()) {
237 void WindowManager::set_focused_window(Window *win)
239 if(win && win == 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]);
258 if(win->get_focusable()) {
260 parent = focused_win->get_parent();
261 fill_rect(parent->get_absolute_rect(), frame_fcolor[0], frame_fcolor[1], frame_fcolor[2]);
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]);
277 const Window *WindowManager::get_focused_window() const
282 Window *WindowManager::get_focused_window()
287 Window *WindowManager::get_window_at_pos(int pointer_x, int pointer_y)
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)) {
300 Window *WindowManager::get_root_window() const
305 void WindowManager::set_focused_frame_color(int r, int g, int b)
312 void WindowManager::get_focused_frame_color(int *r, int *g, int *b) const
314 *r = frame_fcolor[0];
315 *g = frame_fcolor[1];
316 *b = frame_fcolor[2];
319 void WindowManager::set_unfocused_frame_color(int r, int g, int b)
326 void WindowManager::get_unfocused_frame_color(int *r, int *g, int *b) const
328 *r = frame_ucolor[0];
329 *g = frame_ucolor[1];
330 *b = frame_ucolor[2];
333 void WindowManager::set_background(const Pixmap *pixmap)
340 background = new Pixmap(*pixmap);
347 const Pixmap *WindowManager::get_background() const
352 Window *WindowManager::get_grab_window() const
357 void WindowManager::grab_mouse(Window *win)
362 void WindowManager::release_mouse()
367 void WindowManager::raise_window(Window *win)
373 Window *parent = win->get_parent();
374 if(parent != root_win) {
375 if(parent->get_parent() == root_win) {
383 root_win->remove_child(win);
384 root_win->add_child(win);
387 void WindowManager::sink_window(Window *win)
393 std::list<Window*>::iterator it;
394 it = std::find(windows.begin(), windows.end(), win);
395 if(it != windows.end()) {
397 windows.push_front(win);
401 void WindowManager::maximize_window(Window *win)
403 win->normal_rect = win->rect;
405 Rect rect = get_screen_size();
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);
413 rect.width -= frame_thickness * 2;
414 rect.height -= frame_thickness * 2 + titlebar_thickness;
420 win->resize(rect.width, rect.height);
421 win->set_state(Window::STATE_MAXIMIZED);
424 void WindowManager::unmaximize_window(Window *win)
426 win->resize(win->normal_rect.width, win->normal_rect.height);
427 win->move(win->normal_rect.x, win->normal_rect.y);
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);
435 win->set_state(Window::STATE_NORMAL);
438 static void display(Window *win)
441 Window *child = win->get_children()[0];
443 Rect abs_rect = win->get_absolute_rect();
445 //TODO 5 not hardcoded
446 set_text_position(abs_rect.x + 5, abs_rect.y + 15);
447 set_text_color(255, 255, 255);
449 if(child == wm->get_focused_window()) {
450 wm->get_focused_frame_color(&r, &g, &b);
451 fill_rect(abs_rect, r, g, b);
454 wm->get_unfocused_frame_color(&r, &g, &b);
455 fill_rect(win->get_absolute_rect(), r, g, b);
458 draw_text(child->get_title());
461 static int prev_x, prev_y;
463 static void mouse(Window *win, int bn, bool pressed, int x, int y)
465 static long last_click = 0;
470 wm->raise_window(win);
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);
482 else if(state == Window::STATE_NORMAL) {
483 wm->maximize_window(child);
493 static void motion(Window *win, int x, int y)
495 int left_bn = get_button(0);
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);