2
0

main.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Prevents additional console window on Windows in release, DO NOT REMOVE!!
  2. #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
  3. // borrowed from https://github.com/skyline69/balatro-mod-manager
  4. #[cfg(target_os = "linux")]
  5. fn configure_display_backend() -> Option<String> {
  6. use std::env;
  7. let set_env_if_absent = |key: &str, value: &str| {
  8. if env::var_os(key).is_none() {
  9. // Safety: called during startup before any threads are spawned, so mutating the
  10. // process environment is safe.
  11. unsafe { env::set_var(key, value) };
  12. }
  13. };
  14. let on_wayland = env::var_os("WAYLAND_DISPLAY").is_some()
  15. || matches!(
  16. env::var("XDG_SESSION_TYPE"),
  17. Ok(v) if v.eq_ignore_ascii_case("wayland")
  18. );
  19. if !on_wayland {
  20. return None;
  21. }
  22. // Allow users to explicitly keep Wayland if they know their setup is stable.
  23. let allow_wayland = matches!(
  24. env::var("OC_ALLOW_WAYLAND"),
  25. Ok(v) if matches!(v.to_ascii_lowercase().as_str(), "1" | "true" | "yes")
  26. );
  27. if allow_wayland {
  28. return Some("Wayland session detected; respecting OC_ALLOW_WAYLAND=1".into());
  29. }
  30. // Prefer XWayland when available to avoid Wayland protocol errors seen during startup.
  31. if env::var_os("DISPLAY").is_some() {
  32. set_env_if_absent("WINIT_UNIX_BACKEND", "x11");
  33. set_env_if_absent("GDK_BACKEND", "x11");
  34. set_env_if_absent("WEBKIT_DISABLE_DMABUF_RENDERER", "1");
  35. return Some(
  36. "Wayland session detected; forcing X11 backend to avoid compositor protocol errors. \
  37. Set OC_ALLOW_WAYLAND=1 to keep native Wayland."
  38. .into(),
  39. );
  40. }
  41. set_env_if_absent("WEBKIT_DISABLE_DMABUF_RENDERER", "1");
  42. Some(
  43. "Wayland session detected without X11; leaving Wayland enabled (set WINIT_UNIX_BACKEND/GDK_BACKEND manually if needed)."
  44. .into(),
  45. )
  46. }
  47. fn main() {
  48. #[cfg(target_os = "linux")]
  49. {
  50. if let Some(backend_note) = configure_display_backend() {
  51. eprintln!("{backend_note:?}");
  52. }
  53. }
  54. opencode_lib::run()
  55. }