main.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. // Ensure loopback connections are never sent through proxy settings.
  49. // Some VPNs/proxies set HTTP_PROXY/HTTPS_PROXY/ALL_PROXY without excluding localhost.
  50. const LOOPBACK: [&str; 3] = ["127.0.0.1", "localhost", "::1"];
  51. let upsert = |key: &str| {
  52. let mut items = std::env::var(key)
  53. .unwrap_or_default()
  54. .split(',')
  55. .map(|v| v.trim())
  56. .filter(|v| !v.is_empty())
  57. .map(|v| v.to_string())
  58. .collect::<Vec<_>>();
  59. for host in LOOPBACK {
  60. if items.iter().any(|v| v.eq_ignore_ascii_case(host)) {
  61. continue;
  62. }
  63. items.push(host.to_string());
  64. }
  65. // Safety: called during startup before any threads are spawned.
  66. unsafe { std::env::set_var(key, items.join(",")) };
  67. };
  68. upsert("NO_PROXY");
  69. upsert("no_proxy");
  70. #[cfg(target_os = "linux")]
  71. {
  72. if let Some(backend_note) = configure_display_backend() {
  73. eprintln!("{backend_note:?}");
  74. }
  75. }
  76. opencode_lib::run()
  77. }