From 43b3026b0744d54287eada71c84ea8be174950c3 Mon Sep 17 00:00:00 2001
From: Shringe <dashingkoso@gmail.com>
Date: Sat, 12 Jul 2025 01:00:46 -0500
Subject: [PATCH] basic implementation

---
 dwl.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/dwl.c b/dwl.c
index c717c1d..4ba134d 100644
--- a/dwl.c
+++ b/dwl.c
@@ -680,6 +680,15 @@ cleanup(void)
 	wlr_xwayland_destroy(xwayland);
 	xwayland = NULL;
 #endif
+	/* Stop systemd target */
+	if (fork() == 0) {
+		setsid();
+		execvp("systemctl", (char *const[]) {
+			"systemctl", "--user", "stop", "dwl-session.target", NULL
+		});
+		exit(1);
+	}
+
 	wl_display_destroy_clients(dpy);
 	if (child_pid > 0) {
 		kill(-child_pid, SIGTERM);
@@ -2222,6 +2231,31 @@ run(char *startup_cmd)
 		die("startup: display_add_socket_auto");
 	setenv("WAYLAND_DISPLAY", socket, 1);
 
+	/* Import environment variables then start systemd target */
+	if (fork() == 0) {
+		setsid();
+		
+		/* First: import environment variables */
+		pid_t import_pid = fork();
+		if (import_pid == 0) {
+			execvp("systemctl", (char *const[]) {
+				"systemctl", "--user", "import-environment", 
+				"DISPLAY", "WAYLAND_DISPLAY", NULL
+			});
+			exit(1);
+		}
+		
+		/* Wait for import to complete */
+		waitpid(import_pid, NULL, 0);
+		
+		/* Second: start target */
+		execvp("systemctl", (char *const[]) {
+			"systemctl", "--user", "start", "dwl-session.target", NULL
+		});
+		
+		exit(1);
+	}
+
 	/* Start the backend. This will enumerate outputs and inputs, become the DRM
 	 * master, etc */
 	if (!wlr_backend_start(backend))
-- 
2.49.0

