summarylogtreecommitdiffstats
path: root/taskers-build-runtime.patch
blob: 69b6c4c072fd2691ab26c93c51eaa96bc5a81490 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
diff --git a/crates/taskers-app/src/main.rs b/crates/taskers-app/src/main.rs
index dcc7d7972e..b8e839dfcc 100644
--- a/crates/taskers-app/src/main.rs
+++ b/crates/taskers-app/src/main.rs
@@ -16,6 +16,7 @@
     future::pending,
     io::{self, Write},
     net::TcpListener,
+    os::unix::fs::MetadataExt,
     path::{Path, PathBuf},
     process::{Command, Stdio},
     rc::Rc,
@@ -34,7 +35,7 @@
 use taskers_domain::{AppModel, NotificationDeliveryState, NotificationId, SignalKind};
 use taskers_ghostty::{
     BackendChoice, EmbeddedTerminalAppearance, GhosttyHost, GhosttyHostOptions,
-    ensure_runtime_installed,
+    ensure_runtime_installed, runtime_terminfo_dir,
 };
 use taskers_host::{DiagnosticCategory, DiagnosticRecord, DiagnosticsSink, TaskersHost};
 use taskers_runtime::{
@@ -756,12 +757,28 @@
         return;
     }
 
+    #[cfg(feature = "dev-trace")]
+    safe_eprintln(format!(
+        "[dev-trace] process_pending_notifications: {} pending",
+        pending.len()
+    ));
+
     for notification in pending {
-        let delivery = if prefs.suppress_when_visible
-            && notification_target_visible(&model, window, &notification)
-        {
+        let visible = prefs.suppress_when_visible
+            && notification_target_visible(&model, window, &notification);
+        let delivery = if visible {
+            #[cfg(feature = "dev-trace")]
+            safe_eprintln(format!(
+                "[dev-trace] notification SUPPRESSED (pane visible): id={} title={:?}",
+                notification.id, notification.title
+            ));
             NotificationDeliveryState::Suppressed
         } else {
+            #[cfg(feature = "dev-trace")]
+            safe_eprintln(format!(
+                "[dev-trace] notification SHOWN: id={} title={:?} body={:?}",
+                notification.id, notification.title, notification.body
+            ));
             let desktop = gio::Notification::new(&notification.title);
             if let Some(body) = &notification.body {
                 desktop.set_body(Some(body));
@@ -1360,6 +1377,15 @@
             },
         ),
     };
+    maybe_export_bundled_terminfo(&mut shell_launch);
+    publish_shell_environment(&shell_launch);
+    let root_x11_guest_lane = should_force_software_gl(
+        std::env::var_os("DISPLAY").is_some(),
+        std::env::var_os("WAYLAND_DISPLAY").is_some(),
+        is_running_as_root(),
+        std::env::var_os("LIBGL_ALWAYS_SOFTWARE").is_some(),
+    );
+    maybe_enable_software_gl(&mut startup_notes, root_x11_guest_lane);
     shell_launch
         .env
         .insert("TASKERS_SOCKET".into(), socket_path.display().to_string());
@@ -1368,6 +1394,12 @@
             "Smoke mode with mock terminal backend skips the terminal session sidecar.".into(),
         );
         None
+    } else if root_x11_guest_lane {
+        startup_notes.push(
+            "Root-launched X11 guest session skips terminal sidecar attach and uses direct shells because the attach CLI exits immediately in this environment."
+                .into(),
+        );
+        None
     } else {
         match ensure_terminal_session_daemon(&terminal_socket_path) {
             Ok(()) => {
@@ -1410,6 +1442,59 @@
     }
 }
 
+fn maybe_export_bundled_terminfo(shell_launch: &mut ShellLaunchSpec) {
+    if let Some(path) = runtime_terminfo_dir() {
+        // Startup scrubs inherited terminal env so the app doesn't inherit the
+        // parent emulator's terminfo. Re-inject Taskers' own bundled terminfo
+        // so embedded Ghostty panes still boot on systems without a global
+        // xterm-ghostty entry.
+        shell_launch
+            .env
+            .insert("TERMINFO".into(), path.display().to_string());
+    }
+}
+
+fn publish_shell_environment(shell_launch: &ShellLaunchSpec) {
+    let shell = shell_launch.program.display().to_string();
+    if std::env::var_os("TASKERS_REAL_SHELL").is_none() {
+        unsafe {
+            std::env::set_var("TASKERS_REAL_SHELL", &shell);
+        }
+    }
+    if std::env::var_os("SHELL").is_none() {
+        unsafe {
+            std::env::set_var("SHELL", &shell);
+        }
+    }
+}
+
+fn maybe_enable_software_gl(startup_notes: &mut Vec<String>, should_force: bool) {
+    if should_force {
+        unsafe {
+            std::env::set_var("LIBGL_ALWAYS_SOFTWARE", "1");
+        }
+        startup_notes.push(
+            "Enabled LIBGL_ALWAYS_SOFTWARE=1 for a root-launched X11 session to keep Ghostty rendering in virtualized desktops."
+                .into(),
+        );
+    }
+}
+
+fn should_force_software_gl(
+    display_present: bool,
+    wayland_present: bool,
+    running_as_root: bool,
+    libgl_already_set: bool,
+) -> bool {
+    display_present && !wayland_present && running_as_root && !libgl_already_set
+}
+
+fn is_running_as_root() -> bool {
+    std::fs::metadata("/proc/self")
+        .map(|metadata| metadata.uid() == 0)
+        .unwrap_or(false)
+}
+
 fn smoke_runtime_path_overrides() -> Result<RuntimePathOverrides> {
     let timestamp = SystemTime::now()
         .duration_since(UNIX_EPOCH)
@@ -2472,10 +2557,13 @@
 #[cfg(test)]
 mod startup_tests {
     use super::{
-        RuntimePathOverrides, looks_like_dev_install, should_defer_initial_sync,
+        RuntimePathOverrides, looks_like_dev_install, maybe_export_bundled_terminfo,
+        should_defer_initial_sync,
         should_skip_terminal_sidecar_in_smoke, smoke_runtime_path_overrides,
+        should_force_software_gl,
     };
-    use std::path::Path;
+    use std::{collections::BTreeMap, path::Path, path::PathBuf};
+    use taskers_runtime::ShellLaunchSpec;
 
     #[test]
     fn initial_sync_waits_for_real_allocation() {
@@ -2542,4 +2630,71 @@
         assert!(!should_skip_terminal_sidecar_in_smoke(None));
         unsafe { std::env::remove_var("TASKERS_TERMINAL_BACKEND") };
     }
+
+    #[test]
+    fn software_gl_defaults_only_for_root_x11_when_unset() {
+        assert!(should_force_software_gl(true, false, true, false));
+        assert!(!should_force_software_gl(true, true, true, false));
+        assert!(!should_force_software_gl(false, false, true, false));
+        assert!(!should_force_software_gl(true, false, false, false));
+        assert!(!should_force_software_gl(true, false, true, true));
+    }
+
+    #[test]
+    fn packaged_runtime_terminfo_is_reinjected_after_scrub() {
+        let temp = tempfile::tempdir().expect("tempdir");
+        let runtime_dir = temp.path().join("taskers").join("ghostty");
+        let terminfo_dir = temp.path().join("taskers").join("terminfo");
+        std::fs::create_dir_all(&runtime_dir).expect("runtime dir");
+        std::fs::create_dir_all(terminfo_dir.join("x")).expect("terminfo dir");
+        std::fs::write(terminfo_dir.join("x").join("xterm-ghostty"), b"fake terminfo")
+            .expect("write terminfo");
+
+        let _guard = EnvGuard::set([
+            ("TASKERS_GHOSTTY_RUNTIME_DIR", Some(runtime_dir.clone())),
+            ("TERMINFO", None),
+            ("XDG_DATA_HOME", None),
+        ]);
+
+        let mut shell_launch = ShellLaunchSpec {
+            program: PathBuf::from("/bin/sh"),
+            args: Vec::new(),
+            env: BTreeMap::new(),
+        };
+        maybe_export_bundled_terminfo(&mut shell_launch);
+
+        assert_eq!(
+            shell_launch.env.get("TERMINFO").map(String::as_str),
+            Some(terminfo_dir.to_string_lossy().as_ref())
+        );
+    }
+
+    struct EnvGuard {
+        saved: Vec<(&'static str, Option<std::ffi::OsString>)>,
+    }
+
+    impl EnvGuard {
+        fn set<const N: usize>(entries: [(&'static str, Option<PathBuf>); N]) -> Self {
+            let mut saved = Vec::with_capacity(N);
+            for (key, value) in entries {
+                saved.push((key, std::env::var_os(key)));
+                match value {
+                    Some(value) => unsafe { std::env::set_var(key, value) },
+                    None => unsafe { std::env::remove_var(key) },
+                }
+            }
+            Self { saved }
+        }
+    }
+
+    impl Drop for EnvGuard {
+        fn drop(&mut self) {
+            for (key, value) in self.saved.drain(..).rev() {
+                match value {
+                    Some(value) => unsafe { std::env::set_var(key, value) },
+                    None => unsafe { std::env::remove_var(key) },
+                }
+            }
+        }
+    }
 }
diff --git a/crates/taskers-ghostty/build.rs b/crates/taskers-ghostty/build.rs
index 3ad289e058..b17837f036 100644
--- a/crates/taskers-ghostty/build.rs
+++ b/crates/taskers-ghostty/build.rs
@@ -4,11 +4,14 @@
     process::Command,
 };
 
+const SKIP_BUILD_RUNTIME_EMBED_ENV: &str = "TASKERS_GHOSTTY_SKIP_BUILD_RUNTIME_EMBED";
+
 fn main() {
     println!("cargo:rustc-check-cfg=cfg(taskers_ghostty_bridge)");
     println!("cargo:rerun-if-changed=build.rs");
     println!("cargo:rerun-if-changed=../../vendor/ghostty/build.zig");
     println!("cargo:rerun-if-changed=../../vendor/ghostty/src/taskers_bridge.zig");
+    println!("cargo:rerun-if-changed=../../vendor/ghostty/src/taskers_bridge_glibc_compat.c");
     println!("cargo:rerun-if-changed=../../vendor/ghostty/src/taskers_bridge_build_info.zig");
     println!("cargo:rerun-if-changed=../../vendor/ghostty/include/taskers_ghostty_bridge.h");
     println!("cargo:rerun-if-changed=../../vendor/ghostty/src/apprt.zig");
@@ -25,6 +28,13 @@
         println!("cargo:rustc-env=TASKERS_BUILD_TARGET={target}");
     }
 
+    if env::var_os(SKIP_BUILD_RUNTIME_EMBED_ENV).is_some() {
+        println!(
+            "cargo:warning=skipping build-time Ghostty runtime embedding because {SKIP_BUILD_RUNTIME_EMBED_ENV} is set"
+        );
+        return;
+    }
+
     let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("manifest dir"));
     let workspace_root = manifest_dir
         .ancestors()
@@ -54,6 +64,10 @@
             .join("libtaskers_ghostty_bridge.so")
             .display()
     );
+    println!(
+        "cargo:rustc-env=TASKERS_GHOSTTY_BUILD_TERMINFO_DIR={}",
+        install_dir.join("share").join("terminfo").display()
+    );
     println!("cargo:rustc-cfg=taskers_ghostty_bridge");
 }
 
diff --git a/crates/taskers-ghostty/src/lib.rs b/crates/taskers-ghostty/src/lib.rs
index c105811153..9de0717ee5 100644
--- a/crates/taskers-ghostty/src/lib.rs
+++ b/crates/taskers-ghostty/src/lib.rs
@@ -11,5 +11,5 @@
 pub use bridge::{GhosttyBridgeInfo, GhosttyError, GhosttyHost};
 pub use runtime::{
     RuntimeBootstrap, RuntimeBootstrapError, configure_runtime_environment,
-    ensure_runtime_installed,
+    ensure_runtime_installed, runtime_terminfo_dir,
 };
diff --git a/crates/taskers-ghostty/src/runtime.rs b/crates/taskers-ghostty/src/runtime.rs
index 03a716dc7d..361687c359 100644
--- a/crates/taskers-ghostty/src/runtime.rs
+++ b/crates/taskers-ghostty/src/runtime.rs
@@ -195,6 +195,30 @@
         .filter(|path| path.exists())
 }
 
+pub fn runtime_terminfo_dir() -> Option<PathBuf> {
+    if let Some(path) = env::var_os("TERMINFO")
+        .map(PathBuf::from)
+        .filter(|path| terminfo_dir_is_usable(path))
+    {
+        return Some(path);
+    }
+
+    if let Some(path) = explicit_runtime_dir()
+        .and_then(|root| sibling_terminfo_dir(&root))
+        .filter(|path| terminfo_dir_is_usable(path))
+    {
+        return Some(path);
+    }
+
+    if let Some(path) = build_runtime_terminfo_dir() {
+        return Some(path);
+    }
+
+    default_installed_runtime_dir()
+        .and_then(|root| sibling_terminfo_dir(&root))
+        .filter(|path| terminfo_dir_is_usable(path))
+}
+
 fn unpack_bundle<R: Read>(reader: R, staging_root: &Path) -> Result<(), RuntimeBootstrapError> {
     let decoder = XzDecoder::new(reader);
     let mut archive = Archive::new(decoder);
@@ -259,6 +283,12 @@
         .filter(|path| path.exists())
 }
 
+fn build_runtime_terminfo_dir() -> Option<PathBuf> {
+    option_env!("TASKERS_GHOSTTY_BUILD_TERMINFO_DIR")
+        .map(PathBuf::from)
+        .filter(|path| terminfo_dir_is_usable(path))
+}
+
 fn set_runtime_environment_vars(path: &Path) {
     unsafe {
         env::set_var("GHOSTTY_RESOURCES_DIR", path);
@@ -286,6 +316,14 @@
     )
 }
 
+fn sibling_terminfo_dir(runtime_dir: &Path) -> Option<PathBuf> {
+    runtime_dir.parent().map(|root| root.join("terminfo"))
+}
+
+fn terminfo_dir_is_usable(path: &Path) -> bool {
+    path.join(TERMINFO_GHOSTTY_PATH).exists() || path.join(TERMINFO_XTERM_GHOSTTY_PATH).exists()
+}
+
 fn replace_directory(source: &Path, destination: &Path) -> Result<(), RuntimeBootstrapError> {
     if let Some(parent) = destination.parent() {
         fs::create_dir_all(parent).map_err(|error| RuntimeBootstrapError::CreateDir {
@@ -322,7 +360,7 @@
 mod tests {
     use super::{
         RUNTIME_VERSION_FILE, RuntimeBootstrap, ensure_runtime_installed, runtime_bridge_path,
-        runtime_resources_dir,
+        runtime_resources_dir, runtime_terminfo_dir,
     };
     use std::{env, fs, path::Path};
     use tar::Builder;
@@ -412,6 +450,7 @@
             Some(runtime_dir.join("lib").join("libtaskers_ghostty_bridge.so"))
         );
         assert_eq!(runtime_resources_dir(), Some(runtime_dir));
+        assert_eq!(runtime_terminfo_dir(), Some(terminfo_dir));
     }
 
     #[test]
@@ -437,6 +476,25 @@
         );
     }
 
+    #[test]
+    fn runtime_terminfo_dir_follows_explicit_runtime_dir() {
+        let temp = tempdir().expect("tempdir");
+        let runtime_dir = temp.path().join("taskers").join("ghostty");
+        let terminfo_dir = temp.path().join("taskers").join("terminfo");
+        fs::create_dir_all(&runtime_dir).expect("runtime dir");
+        fs::create_dir_all(terminfo_dir.join("x")).expect("terminfo dir");
+        fs::write(terminfo_dir.join("x").join("xterm-ghostty"), b"fake terminfo")
+            .expect("write fake terminfo");
+
+        let _guard = EnvGuard::set([
+            ("TASKERS_GHOSTTY_RUNTIME_DIR", Some(runtime_dir.as_os_str())),
+            ("TERMINFO", None),
+            ("XDG_DATA_HOME", None),
+        ]);
+
+        assert_eq!(runtime_terminfo_dir(), Some(terminfo_dir));
+    }
+
     fn write_bundle(source_dir: &Path, bundle_path: &Path) {
         let file = fs::File::create(bundle_path).expect("create bundle");
         let encoder = XzEncoder::new(file, 9);
diff --git a/scripts/build_linux_bundle.sh b/scripts/build_linux_bundle.sh
index 674983299d..12ef06748a 100755
--- a/scripts/build_linux_bundle.sh
+++ b/scripts/build_linux_bundle.sh
@@ -10,6 +10,8 @@
 stage_dir="$(mktemp -d "${TMPDIR:-/tmp}/taskers-linux-bundle.XXXXXX")"
 prefix_dir="$stage_dir/prefix"
 bundle_dir="$stage_dir/bundle"
+bundle_rustflags="${RUSTFLAGS:-}"
+bundle_rustflags="${bundle_rustflags:+$bundle_rustflags }--remap-path-prefix=${repo_root}=."
 
 cleanup() {
   rm -rf "$stage_dir"
@@ -18,7 +20,9 @@
 
 (
   cd "$repo_root"
-  cargo build --release -p taskers --bin taskers --bin taskers-gtk --bin taskersctl --bin taskers-terminald
+  TASKERS_GHOSTTY_SKIP_BUILD_RUNTIME_EMBED=1 \
+    RUSTFLAGS="$bundle_rustflags" \
+    cargo build --release -p taskers --bin taskers --bin taskers-gtk --bin taskersctl --bin taskers-terminald
 )
 
 (
@@ -29,6 +33,7 @@
     -Dgtk-wayland=false \
     -Dstrip=true \
     -Di18n=false \
+    -Dcpu=baseline \
     "-Dversion-string=$ghostty_version" \
     --summary none \
     --prefix "$prefix_dir"