commit f983e20c6046e83fb13b2628d523bd8a4828411d
parent 941ab3d0f212701eab992f0390279d75425589d4
Author: lumidify <nobody@lumidify.org>
Date:   Tue, 29 Dec 2020 20:36:00 +0100
Clean up window even on fatal exit; add -t option for setting title of window
Diffstat:
2 files changed, 24 insertions(+), 5 deletions(-)
diff --git a/ltkd.c b/ltkd.c
@@ -109,15 +109,32 @@ static int listenfd = -1;
 static char *ltk_dir = NULL;
 static FILE *ltk_logfile = NULL;
 static char *sock_path = NULL;
+/* Note: Most functions still take this explicitly because it wasn't
+   global originally, but that's just the way it is. */
+static ltk_window *main_window = NULL;
 
 int main(int argc, char *argv[]) {
-	ltk_window *window = ltk_create_window("theme.ini", "Demo", 0, 0, 500, 500);
+	int ch;
+	char *title = "LTK Window";
+	while ((ch = getopt(argc, argv, "t:")) != -1) {
+		switch (ch) {
+			case 't':
+				title = optarg;
+				break;
+			default:
+				ltk_fatal("USAGE: ltkd [-t title]\n");
+		}
+	}
+
+	/* FIXME: set window size properly - I only run it in a tiling WM
+	   anyways, so it doesn't matter, but still... */
+	main_window = ltk_create_window("theme.ini", title, 0, 0, 500, 500);
 
 	ltk_dir = ltk_setup_directory();
 	if (!ltk_dir) ltk_fatal("Unable to setup ltk directory.\n");
 	ltk_logfile = open_log(ltk_dir);
 	if (!ltk_logfile) ltk_fatal("Unable to open log file.\n");
-	sock_path = get_sock_path(ltk_dir, window->xwindow);
+	sock_path = get_sock_path(ltk_dir, main_window->xwindow);
 	if (!sock_path) ltk_fatal("Unable to allocate memory for socket path.\n");
 
 	/* Note: sockets should be initialized to 0 because it is static */
@@ -129,7 +146,7 @@ int main(int argc, char *argv[]) {
 		sockets[i].tokens.tokens = NULL;
 	}
 
-	return ltk_mainloop(window);
+	return ltk_mainloop(main_window);
 }
 
 static int
@@ -337,7 +354,6 @@ ltk_cleanup_nongui(void) {
 
 static void
 ltk_cleanup_gui(ltk_window *window) {
-	ltk_destroy_theme(window->theme);
 	ltk_destroy_window(window);
 }
 
@@ -396,6 +412,8 @@ ltk_fatal(const char *msg) {
 	(void)fprintf(errfile, msg);
 	/* FIXME: cleanup gui stuff too (need window for that) */
 	ltk_cleanup_nongui();
+	if (main_window)
+		ltk_cleanup_gui(main_window);
 	exit(1);
 };
 
@@ -543,6 +561,7 @@ ltk_destroy_window(ltk_window *window) {
 		}
 	}
 	kh_destroy(widget, window->widget_hash);
+	ltk_destroy_theme(window->theme);
 	ltk_cleanup_text();
 	XCloseDisplay(window->dpy);
 	free(window);
diff --git a/test.sh b/test.sh
@@ -7,7 +7,7 @@
 # supported yet.
 
 export LTKDIR="`pwd`/.ltk"
-ltk_id=`./ltkd`
+ltk_id=`./ltkd -t "Cool Window"`
 
 cat test.gui | ./ltkc $ltk_id | while read cmd
 do