code: drawterm

Download patch

ref: 31e15655e642339a90344e6d97d5a788154000e6
parent: c636d0954ba47fbd8883a01d1167c9c92f47acea
author: Jacob Moody <moody@posixcafe.org>
date: Sun Mar 10 00:15:02 EDT 2024

gui-wl: misc cleanup

* Make includes match everyone else and tidy
* Clean up all warnings
* Fix wrong argument in decoration setup
* Remove posted handling from clipboard.
  I think this was causing a rare issue where the clipboard would stop working,
  where it would get stuck posted and stop offering new data.
  This whole posted code was solving a problem I am now not sure ever existed.

--- a/Make.linux
+++ b/Make.linux
@@ -3,7 +3,7 @@
 AR=ar
 AS=as
 RANLIB=ranlib
-CC?=cc
+CC=cc
 CFLAGS=-Wall -Wno-missing-braces -Wno-parentheses -ggdb -I$(ROOT) -I$(ROOT)/include -I$(ROOT)/kern -c -D_THREAD_SAFE -DPTHREAD $(PTHREAD) `pkg-config --cflags libpipewire-0.3` -D_REENTRANT -O2
 O=o
 OS=posix
--- a/gui-wl/wl-cb.c
+++ b/gui-wl/wl-cb.c
@@ -1,13 +1,19 @@
-#define _POSIX_C_SOURCE 200809L
+#define _GNU_SOURCE
+
+#include "u.h"
+#include "lib.h"
+#include "dat.h"
+#include "fns.h"
+#include <draw.h>
+#include <memdraw.h>
+#include <keyboard.h>
+
+#undef up
+
 #include <sys/mman.h>
 #include <wayland-client.h>
 #include <wayland-client-protocol.h>
 #include <linux/input-event-codes.h>
-#include <sys/types.h>
-#include <sys/mman.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <time.h>
 #include <xkbcommon/xkbcommon.h>
 #include "xdg-shell-protocol.h"
 #include "xdg-decoration-protocol.h"
@@ -14,24 +20,9 @@
 #include "xdg-primary-selection-protocol.h"
 #include "wlr-virtual-pointer.h"
 
-#include "u.h"
-#include "lib.h"
-#include "kern/dat.h"
-#include "kern/fns.h"
-#include "error.h"
-#include "user.h"
-#include <draw.h>
-#include <memdraw.h>
-#include <keyboard.h>
 #include "screen.h"
 #include "wl-inc.h"
 
-#undef close
-#undef send
-#undef pipe
-#undef write
-#undef read
-#undef time
 
 static void
 xdg_surface_handle_configure(void *data, struct xdg_surface *xdg_surface, uint32_t serial)
@@ -68,11 +59,8 @@
 	wl->maximized = 0;
 	for(i = 0; i < states->size; i++){
 		state = ((enum xdg_toplevel_state *)states->data)[i];
-		switch (state){
-		case XDG_TOPLEVEL_STATE_MAXIMIZED:
+		if(state == XDG_TOPLEVEL_STATE_MAXIMIZED)
 			wl->maximized = 1;
-			return;
-		}
 	}
 }
 
@@ -259,6 +247,9 @@
 	case XKB_KEY_Insert:
 		utf32 = Kins;
 		break;
+	case XKB_KEY_Scroll_Lock:
+		utf32 = Kscroll;
+		break;
 	case XKB_KEY_F1:
 	case XKB_KEY_F2:
 	case XKB_KEY_F3:
@@ -529,7 +520,6 @@
 	len = strlen(wl->clip.content);
 	for(pos = 0; (n = write(fd, wl->clip.content+pos, len-pos)) > 0 && pos < len; pos += n)
 		;
-	wl->clip.posted = 0;
 	close(fd);
 	qunlock(&wl->clip.lk);
 }
@@ -537,12 +527,6 @@
 static void
 data_source_handle_cancelled(void *data, struct wl_data_source *source)
 {
-	Wlwin *wl;
-
-	wl = data;
-	qlock(&wl->clip.lk);
-	wl->clip.posted = 0;
-	qunlock(&wl->clip.lk);
 	wl_data_source_destroy(source);
 }
 
@@ -567,7 +551,6 @@
 	len = strlen(wl->clip.content);
 	for(pos = 0; (n = write(fd, wl->clip.content+pos, len-pos)) > 0 && pos < len; pos += n)
 		;
-	wl->clip.primsel_posted = 0;
 	close(fd);
 	qunlock(&wl->clip.lk);
 }
@@ -575,12 +558,6 @@
 static void
 primsel_source_handle_cancelled(void *data, struct zwp_primary_selection_source_v1 *source)
 {
-	Wlwin *wl;
-
-	wl = data;
-	qlock(&wl->clip.lk);
-	wl->clip.primsel_posted = 0;
-	qunlock(&wl->clip.lk);
 	zwp_primary_selection_source_v1_destroy(source);
 }
 
@@ -793,7 +770,7 @@
 	wl->client_side_deco = wl->decoman == nil;
 	if(wl->decoman != nil){
 		deco = zxdg_decoration_manager_v1_get_toplevel_decoration(wl->decoman, wl->xdg_toplevel);
-		zxdg_toplevel_decoration_v1_add_listener(wl->decoman, &zxdg_toplevel_decoration_v1_listener, wl);
+		zxdg_toplevel_decoration_v1_add_listener(deco, &zxdg_toplevel_decoration_v1_listener, wl);
 		zxdg_toplevel_decoration_v1_set_mode(deco, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
 	}
 
@@ -831,23 +808,15 @@
 	free(wl->clip.content);
 	wl->clip.content = strdup(s);
 
-	/* Do we still own the clipboard? */
-	if(wl->clip.posted == 0){
-		source = wl_data_device_manager_create_data_source(wl->data_device_manager);
-		wl_data_source_add_listener(source, &data_source_listener, wl);
-		wl_data_source_offer(source, "text/plain;charset=utf-8");
-		wl_data_device_set_selection(wl->data_device, source, wl->clip.serial);
-		wl->clip.posted = 1;
-	}
+	source = wl_data_device_manager_create_data_source(wl->data_device_manager);
+	wl_data_source_add_listener(source, &data_source_listener, wl);
+	wl_data_source_offer(source, "text/plain;charset=utf-8");
+	wl_data_device_set_selection(wl->data_device, source, wl->clip.serial);
 
-	/* Primary selection */
-	if(wl->clip.primsel_posted == 0){
-		psource = zwp_primary_selection_device_manager_v1_create_source(wl->primsel);
-		zwp_primary_selection_source_v1_add_listener(psource, &primsel_source_listener, wl);
-		zwp_primary_selection_source_v1_offer(psource, "text/plain;charset=utf-8");
-		zwp_primary_selection_device_v1_set_selection(wl->primsel_device, psource, wl->clip.serial);
-		wl->clip.primsel_posted = 1;
-	}
+	psource = zwp_primary_selection_device_manager_v1_create_source(wl->primsel);
+	zwp_primary_selection_source_v1_add_listener(psource, &primsel_source_listener, wl);
+	zwp_primary_selection_source_v1_offer(psource, "text/plain;charset=utf-8");
+	zwp_primary_selection_device_v1_set_selection(wl->primsel_device, psource, wl->clip.serial);
 
 	qunlock(&wl->clip.lk);
 }
--- a/gui-wl/wl-inc.h
+++ b/gui-wl/wl-inc.h
@@ -18,15 +18,6 @@
 	 * So we must provide the serial we get
 	 * on keyboard.enter. */
 	u32int serial;
-
-	/* Because we dont actually cough
-	 * up the buffer until someone else
-	 * asks, we can change the contents
-	 * locally without a round trip.
-	 * Posted stores if we already made
-	 * our round trip */
-	int posted;
-	int primsel_posted;
 };
 
 struct Mouse {
--- a/gui-wl/wl-util.c
+++ b/gui-wl/wl-util.c
@@ -1,31 +1,26 @@
-#define _POSIX_C_SOURCE 200809L
-#include <sys/mman.h>
-#include <wayland-client.h>
-#include <wayland-client-protocol.h>
-#include <linux/input-event-codes.h>
-#include <sys/types.h>
-#include <sys/mman.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <xkbcommon/xkbcommon.h>
-#include "xdg-shell-protocol.h"
+#define _GNU_SOURCE
 
 #include "u.h"
 #include "lib.h"
-#include "kern/dat.h"
-#include "kern/fns.h"
-#include "error.h"
-#include "user.h"
+#include "dat.h"
+#include "fns.h"
 #include <draw.h>
 #include <memdraw.h>
 #include <keyboard.h>
 #include <cursor.h>
+
+#undef up
+
+#include <sys/mman.h>
+#include <wayland-client.h>
+#include <wayland-client-protocol.h>
+#include <linux/input-event-codes.h>
+#include <xkbcommon/xkbcommon.h>
+#include "xdg-shell-protocol.h"
+
 #include "screen.h"
 #include "wl-inc.h"
 
-#undef getenv
-#undef close
-
 static int
 wlcreateshm(off_t size)
 {
@@ -64,7 +59,8 @@
 	fd = wlcreateshm(screensize+cursorsize);
 	if(fd < 0)
 		panic("could not mk_shm_fd");
-	ftruncate(fd, screensize+cursorsize);
+	if(ftruncate(fd, screensize+cursorsize) < 0)
+		panic("could not ftruncate");
 
 	wl->shm_data = mmap(nil, screensize+cursorsize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
 	if(wl->shm_data == MAP_FAILED)