main.rs 3.2 KB

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