shithub: cstory

Download patch

ref: d1a4203e8983629646e1a85949429be10483dced
parent: b2eae2b262371db954fcc1e6d77486a94021b324
author: Jacob Moody <moody@posixcafe.org>
date: Thu Dec 21 17:32:02 EST 2023

fix janky window resizes

As a side effect move all graphical initialization
and mouse stuff to renderer implementation.

--- a/src/Backends/Platform/9front.cpp
+++ b/src/Backends/Platform/9front.cpp
@@ -9,10 +9,8 @@
 #include <draw.h>
 #include <memdraw.h>
 #include <keyboard.h>
-#include <mouse.h>
 
 static int keyboard_state[BACKEND_KEYBOARD_TOTAL];
-static Mousectl *mctl;
 
 int Mark_Rune(int *kbd, Rune r)
 {
@@ -128,43 +126,12 @@
 	}
 }
 
-void Backend_Proc(void*)
-{
-	enum { Aresize, Amouse, Aend };
-	Mouse m;
-
-	Alt a[] = {
-		[Amouse] { nil, &m, CHANRCV },
-		[Aresize] { nil, nil, CHANRCV },
-		[Aend] { nil, nil, CHANEND },
-	};
-	threadsetname("resizeproc");
-	a[Amouse].c = mctl->c;
-	a[Aresize].c = mctl->resizec;
-
-	for(;;){
-		switch(alt(a)){
-		case Aresize:
-			getwindow(display, Refnone);
-			break;
-		}
-	}
-}
-
 int Backend_Init(void (*drag_and_drop_callback)(const char *path), void (*window_focus_callback)(int focus))
 {
 	(void)drag_and_drop_callback;
 	(void)window_focus_callback;
 
-	memimageinit();
-	if(initdraw(nil, nil, "cstory") < 0)
-		sysfatal("initdraw: %r");
-	mctl = initmouse(nil, screen);
-	if(mctl == nil)
-		sysfatal("initmouse: %r");
-
 	proccreate(Key_Proc, nil, 8192);
-	proccreate(Backend_Proc, nil, 8192);
 
 	return 1;
 }
--- a/src/Backends/Rendering/9front.cpp
+++ b/src/Backends/Rendering/9front.cpp
@@ -8,6 +8,8 @@
 #include <string.h>
 #include <draw.h>
 #include <memdraw.h>
+#include <thread.h>
+#include <mouse.h>
 
 #include "../Misc.h"
 #include "Window/Software.h"
@@ -16,6 +18,9 @@
 #define MIN(a, b) ((a) < (b) ? (a) : (b))
 #define MAX(a, b) ((a) > (b) ? (a) : (b))
 
+static Channel *resizec;
+static Mousectl *mctl;
+
 typedef struct RenderBackend_Surface
 {
 	Image *i;
@@ -40,8 +45,42 @@
 	scale = 2,
 };
 
+static void RenderBackend_Mouseproc(void*)
+{
+	enum { Amouse, Aresize, Aout, Aend };
+	static int needgetwin;
+
+	Alt a[] = {
+		[Amouse] {  mctl->c, nil, CHANRCV },
+		[Aresize] { mctl->resizec, nil, CHANRCV },
+		[Aout] { resizec, &needgetwin, CHANSND },
+		[Aend] { nil, nil, CHANEND },
+	};
+
+	threadsetname("mouseproc");
+	for(;;){
+		switch(alt(a)){
+		case Aresize:
+			needgetwin = 1;
+			break;
+		case Aout:
+			needgetwin = 0;
+			break;
+		}
+	}
+}
+
 RenderBackend_Surface* RenderBackend_Init(const char *window_title, size_t width, size_t height, int fullscreen)
 {
+	memimageinit();
+	if(initdraw(nil, nil, "cstory") < 0)
+		sysfatal("initdraw: %r");
+	resizec = chancreate(sizeof(int), 1);
+	mctl = initmouse(nil, screen);
+	if(mctl == nil)
+		sysfatal("initmouse: %r");
+	proccreate(RenderBackend_Mouseproc, nil, 8192);
+
 	framebuffer.i = allocimage(display, Rect(0, 0, width*scale, height*scale), screen->chan, 0, DBlue);
 	if(framebuffer.i == nil)
 		sysfatal("could not alloc screen");
@@ -56,6 +95,11 @@
 
 void RenderBackend_DrawScreen(void)
 {
+	int needgetwin;
+
+	recv(resizec, &needgetwin);
+	if(needgetwin)
+		getwindow(display, Refnone);
 	draw(screen, screen->r, framebuffer.i, nil, ZP);
 	flushimage(display, 1);
 }