+ if(dirty_rects.empty()) {
+ return;
+ }
+
+ std::list<Rect>::iterator drit = dirty_rects.begin();
+ Rect uni = *drit++;
+ while(drit != dirty_rects.end()) {
+ uni = rect_union(uni, *drit++);
+ }
+ dirty_rects.clear();
+
+ wait_vsync();
+
+ fill_rect(uni, bg_color[0], bg_color[1], bg_color[2]);
+
+ root_win->draw_children(uni);
+
+ // draw mouse cursor
+ int mouse_x, mouse_y;
+ get_pointer_pos(&mouse_x, &mouse_y);
+
+ blit_key(mouse_cursor.get_image(), mouse_cursor.get_rect(),
+ get_framebuffer(), get_screen_size(), mouse_x, mouse_y,
+ 0, 0, 0);
+
+ Rect mouse_rect = {mouse_x, mouse_y, mouse_cursor.get_width(), mouse_cursor.get_height()};
+ invalidate_region(mouse_rect);
+
+ gfx_update();
+}
+
+void WindowManager::add_window(Window *win)
+{
+ if(!win || win == root_win) {
+ return;
+ }
+
+ root_win->add_child(win);
+
+ if(windows.empty()) {
+ focused_win = win;
+ }
+
+ if(win->get_managed()) {
+ create_frame(win);
+ }
+
+ windows.push_back(win);
+}
+
+void WindowManager::remove_window(Window *win)
+{
+ std::list<Window*>::iterator it;
+ it = std::find(windows.begin(), windows.end(), win);
+
+ if(it != windows.end()) {
+ windows.erase(it);
+ }
+}
+
+void WindowManager::set_focused_window(Window *win)
+{
+ if(win && win == focused_win) {
+ return;
+ }
+
+ Window *parent;
+ if(focused_win) {
+ // invalidate the frame (if any)
+ parent = focused_win->get_parent();
+ if(parent && parent != root_win) {
+ parent->invalidate();
+ fill_rect(parent->get_absolute_rect(), frame_ucolor[0], frame_ucolor[1], frame_ucolor[2]);
+ }
+ }
+
+ if(!win) {
+ focused_win = 0;
+ return;
+ }
+
+ if(win->get_focusable()) {
+ focused_win = win;
+ parent = focused_win->get_parent();
+ fill_rect(parent->get_absolute_rect(), frame_fcolor[0], frame_fcolor[1], frame_fcolor[2]);
+ return;
+ }
+
+ Window **children = win->get_children();
+ for(int i=0; i<win->get_children_count(); i++) {
+ if(children[0]->get_focusable()) {
+ set_focused_window(children[0]);
+ fill_rect(win->get_absolute_rect(), frame_fcolor[0], frame_fcolor[1], frame_fcolor[2]);
+ return;
+ }
+ }
+
+ focused_win = 0;
+}
+
+const Window *WindowManager::get_focused_window() const
+{
+ return focused_win;
+}
+
+Window *WindowManager::get_focused_window()
+{
+ return focused_win;
+}
+
+Window *WindowManager::get_window_at_pos(int pointer_x, int pointer_y)
+{
+ Window *root_win = wm->get_root_window();
+ Window **children = root_win->get_children();
+ for(int i=root_win->get_children_count() - 1; i>=0; i--) {
+ if(children[i]->contains_point(pointer_x, pointer_y)) {
+ return children[i];
+ }
+ }
+
+ return 0;
+}
+
+Window *WindowManager::get_root_window() const
+{
+ return root_win;
+}
+
+void WindowManager::set_focused_frame_color(int r, int g, int b)
+{
+ frame_fcolor[0] = r;
+ frame_fcolor[1] = g;
+ frame_fcolor[2] = b;
+}
+
+void WindowManager::get_focused_frame_color(int *r, int *g, int *b) const
+{
+ *r = frame_fcolor[0];
+ *g = frame_fcolor[1];
+ *b = frame_fcolor[2];
+}
+
+void WindowManager::set_unfocused_frame_color(int r, int g, int b)
+{
+ frame_ucolor[0] = r;
+ frame_ucolor[1] = g;
+ frame_ucolor[2] = b;
+}
+
+void WindowManager::get_unfocused_frame_color(int *r, int *g, int *b) const
+{
+ *r = frame_ucolor[0];
+ *g = frame_ucolor[1];
+ *b = frame_ucolor[2];
+}
+
+Window *WindowManager::get_grab_window() const
+{
+ return grab_win;
+}
+
+void WindowManager::grab_mouse(Window *win)
+{
+ grab_win = win;
+}
+
+void WindowManager::release_mouse()
+{
+ grab_win = 0;
+}
+
+void WindowManager::raise_window(Window *win)
+{
+ if(!win) {
+ return;
+ }
+
+ Window *parent = win->get_parent();
+ if(parent != root_win) {
+ if(parent->get_parent() == root_win) {
+ win = parent;