main.rs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. mod display;
  6. #[cfg(target_os = "linux")]
  7. fn configure_display_backend() -> Option<String> {
  8. use std::env;
  9. let set_env_if_absent = |key: &str, value: &str| {
  10. if env::var_os(key).is_none() {
  11. // Safety: called during startup before any threads are spawned, so mutating the
  12. // process environment is safe.
  13. unsafe { env::set_var(key, value) };
  14. }
  15. };
  16. let on_wayland = env::var_os("WAYLAND_DISPLAY").is_some()
  17. || matches!(
  18. env::var("XDG_SESSION_TYPE"),
  19. Ok(v) if v.eq_ignore_ascii_case("wayland")
  20. );
  21. if !on_wayland {
  22. return None;
  23. }
  24. let prefer_wayland = display::read_wayland().unwrap_or(false);
  25. let allow_wayland = prefer_wayland
  26. || matches!(
  27. env::var("OC_ALLOW_WAYLAND"),
  28. Ok(v) if matches!(v.to_ascii_lowercase().as_str(), "1" | "true" | "yes")
  29. );
  30. if allow_wayland {
  31. if prefer_wayland {
  32. return Some("Wayland session detected; using native Wayland from settings".into());
  33. }
  34. return Some("Wayland session detected; respecting OC_ALLOW_WAYLAND=1".into());
  35. }
  36. // Prefer XWayland when available to avoid Wayland protocol errors seen during startup.
  37. if env::var_os("DISPLAY").is_some() {
  38. set_env_if_absent("WINIT_UNIX_BACKEND", "x11");
  39. set_env_if_absent("GDK_BACKEND", "x11");
  40. set_env_if_absent("WEBKIT_DISABLE_DMABUF_RENDERER", "1");
  41. return Some(
  42. "Wayland session detected; forcing X11 backend to avoid compositor protocol errors. \
  43. Set OC_ALLOW_WAYLAND=1 to keep native Wayland."
  44. .into(),
  45. );
  46. }
  47. set_env_if_absent("WEBKIT_DISABLE_DMABUF_RENDERER", "1");
  48. Some(
  49. "Wayland session detected without X11; leaving Wayland enabled (set WINIT_UNIX_BACKEND/GDK_BACKEND manually if needed)."
  50. .into(),
  51. )
  52. }
  53. fn main() {
  54. // Ensure loopback connections are never sent through proxy settings.
  55. // Some VPNs/proxies set HTTP_PROXY/HTTPS_PROXY/ALL_PROXY without excluding localhost.
  56. const LOOPBACK: [&str; 3] = ["127.0.0.1", "localhost", "::1"];
  57. let upsert = |key: &str| {
  58. let mut items = std::env::var(key)
  59. .unwrap_or_default()
  60. .split(',')
  61. .map(|v| v.trim())
  62. .filter(|v| !v.is_empty())
  63. .map(|v| v.to_string())
  64. .collect::<Vec<_>>();
  65. for host in LOOPBACK {
  66. if items.iter().any(|v| v.eq_ignore_ascii_case(host)) {
  67. continue;
  68. }
  69. items.push(host.to_string());
  70. }
  71. // Safety: called during startup before any threads are spawned.
  72. unsafe { std::env::set_var(key, items.join(",")) };
  73. };
  74. upsert("NO_PROXY");
  75. upsert("no_proxy");
  76. #[cfg(target_os = "linux")]
  77. {
  78. if let Some(backend_note) = configure_display_backend() {
  79. eprintln!("{backend_note:?}");
  80. }
  81. }
  82. opencode_lib::run()
  83. }