main.rs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 opencode_lib::linux_windowing::{Backend, SessionEnv, select_backend};
  7. use std::env;
  8. let set_env_if_absent = |key: &str, value: &str| {
  9. if env::var_os(key).is_none() {
  10. // Safety: called during startup before any threads are spawned, so mutating the
  11. // process environment is safe.
  12. unsafe { env::set_var(key, value) };
  13. }
  14. };
  15. let session = SessionEnv::capture();
  16. let prefer_wayland = opencode_lib::linux_display::read_wayland().unwrap_or(false);
  17. let decision = select_backend(&session, prefer_wayland)?;
  18. match decision.backend {
  19. Backend::X11 => {
  20. set_env_if_absent("WINIT_UNIX_BACKEND", "x11");
  21. set_env_if_absent("GDK_BACKEND", "x11");
  22. set_env_if_absent("WEBKIT_DISABLE_DMABUF_RENDERER", "1");
  23. }
  24. Backend::Wayland => {
  25. set_env_if_absent("WINIT_UNIX_BACKEND", "wayland");
  26. set_env_if_absent("GDK_BACKEND", "wayland");
  27. set_env_if_absent("WEBKIT_DISABLE_DMABUF_RENDERER", "1");
  28. }
  29. Backend::Auto => {
  30. set_env_if_absent("GDK_BACKEND", "wayland,x11");
  31. set_env_if_absent("WEBKIT_DISABLE_DMABUF_RENDERER", "1");
  32. }
  33. }
  34. Some(decision.note)
  35. }
  36. fn main() {
  37. // Ensure loopback connections are never sent through proxy settings.
  38. // Some VPNs/proxies set HTTP_PROXY/HTTPS_PROXY/ALL_PROXY without excluding localhost.
  39. const LOOPBACK: [&str; 3] = ["127.0.0.1", "localhost", "::1"];
  40. let upsert = |key: &str| {
  41. let mut items = std::env::var(key)
  42. .unwrap_or_default()
  43. .split(',')
  44. .map(|v| v.trim())
  45. .filter(|v| !v.is_empty())
  46. .map(|v| v.to_string())
  47. .collect::<Vec<_>>();
  48. for host in LOOPBACK {
  49. if items.iter().any(|v| v.eq_ignore_ascii_case(host)) {
  50. continue;
  51. }
  52. items.push(host.to_string());
  53. }
  54. // Safety: called during startup before any threads are spawned.
  55. unsafe { std::env::set_var(key, items.join(",")) };
  56. };
  57. upsert("NO_PROXY");
  58. upsert("no_proxy");
  59. #[cfg(target_os = "linux")]
  60. {
  61. if let Some(backend_note) = configure_display_backend() {
  62. eprintln!("{backend_note}");
  63. }
  64. }
  65. opencode_lib::run()
  66. }