From 1a99700287749211aec38cb58ea2664585154794 Mon Sep 17 00:00:00 2001
From: Marvin Borner
Date: Tue, 15 Sep 2020 19:17:49 +0200
Subject: Some code improvements.

I know, my commit messages are getting worse...
---
 apps/wm.c | 52 ++++++++++++++++++++++++++++++----------------------
 1 file changed, 30 insertions(+), 22 deletions(-)

(limited to 'apps/wm.c')

diff --git a/apps/wm.c b/apps/wm.c
index df58e10..ca032e7 100644
--- a/apps/wm.c
+++ b/apps/wm.c
@@ -12,7 +12,8 @@
 #include <sys.h>
 #include <vesa.h>
 
-#define MOUSE_SKIP 2 // => Every move % n != 0 gets skipped
+static int MOUSE_SKIP = 0; // => Every move % n != 0 gets skipped
+static int window_count;
 
 static struct vbe vbe;
 static struct window direct; // Direct video memory window
@@ -34,40 +35,47 @@ static struct window *new_window(struct window *win, int x, int y, u16 width, u1
 	win->bpp = vbe.bpp;
 	win->pitch = win->width * (win->bpp >> 3);
 	win->fb = malloc(height * win->pitch);
+	memset(win->fb, 0, height * win->pitch);
 	win->flags = flags;
+	window_count++;
+	if (window_count % 2 == 1)
+		MOUSE_SKIP++;
 	return win;
 }
 
 static struct window *window_at(int x, int y)
 {
-	if (windows->head && windows->head->data) {
-		struct node *iterator = windows->head;
-		do {
-			struct window *win = iterator->data;
-			if (win != &root && x >= win->x && x <= win->x + (int)win->width &&
-			    y >= win->y && y <= win->y + (int)win->height) {
-				return win;
-			}
-		} while ((iterator = iterator->next) != NULL);
+	if (!windows->head || !windows->head->data)
+		return NULL;
+
+	struct node *iterator = windows->head;
+	while (iterator != NULL) {
+		struct window *win = iterator->data;
+		if (win != &root && x >= win->x && x <= win->x + (int)win->width && y >= win->y &&
+		    y <= win->y + (int)win->height)
+			return win;
+		iterator = iterator->next;
 	}
 	return NULL;
 }
 
 static void redraw_all()
 {
-	if (windows->head && windows->head->data) {
-		struct node *iterator = windows->head;
-		do {
-			struct window *win = iterator->data;
-			if (win != focused)
-				gui_win_on_win(&exchange, win, win->x, win->y);
-		} while ((iterator = iterator->next) != NULL);
-
-		if (focused)
-			gui_win_on_win(&exchange, focused, focused->x, focused->y);
-
-		memcpy(direct.fb, exchange.fb, exchange.pitch * exchange.height);
+	if (!windows->head || !windows->head->data)
+		return;
+
+	struct node *iterator = windows->head;
+	while (iterator != NULL) {
+		struct window *win = iterator->data;
+		if (win != focused)
+			gui_win_on_win(&exchange, win, win->x, win->y);
+		iterator = iterator->next;
 	}
+
+	if (focused)
+		gui_win_on_win(&exchange, focused, focused->x, focused->y);
+
+	memcpy(direct.fb, exchange.fb, exchange.pitch * exchange.height);
 }
 
 static int mouse_skip = 0;
-- 
cgit v1.2.3