├── .rustfmt.toml ├── docs ├── img │ ├── 2x2.png │ ├── 3x3-crosshair.png │ └── 6x6-crosshair.png └── crosshair-alignment.md ├── screenshots ├── cross.png └── custom.png ├── src-lib ├── private │ ├── mod.rs │ ├── util │ │ ├── mod.rs │ │ ├── custom_serializer.rs │ │ ├── image │ │ │ ├── naive.rs │ │ │ ├── precise.rs │ │ │ └── mod.rs │ │ ├── numeric.rs │ │ └── dialog.rs │ ├── hotkey │ │ ├── mod.rs │ │ ├── keycode.rs │ │ └── hotkey_manager.rs │ ├── platform │ │ ├── mod.rs │ │ ├── windows.rs │ │ └── generic.rs │ └── settings.rs └── lib.rs ├── tests └── resources │ ├── test.png │ ├── test_config_old.toml │ ├── test_config.toml │ └── test_config_image.toml ├── .gitignore ├── benches ├── README.md ├── benches.rs ├── hotkey.rs └── color_picker.rs ├── .editorconfig ├── .github ├── dependabot.yml └── workflows │ ├── publish.yml │ ├── ci.yml │ └── build.yml ├── Cargo.toml ├── MAINTENANCE.md ├── src ├── main.rs ├── tray.rs └── window.rs ├── README.md ├── deny.toml └── LICENSE /.rustfmt.toml: -------------------------------------------------------------------------------- 1 | max_width = 120 2 | -------------------------------------------------------------------------------- /docs/img/2x2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zkxs/simple-crosshair-overlay/HEAD/docs/img/2x2.png -------------------------------------------------------------------------------- /screenshots/cross.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zkxs/simple-crosshair-overlay/HEAD/screenshots/cross.png -------------------------------------------------------------------------------- /src-lib/private/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod hotkey; 2 | pub mod platform; 3 | pub mod settings; 4 | pub mod util; 5 | -------------------------------------------------------------------------------- /screenshots/custom.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zkxs/simple-crosshair-overlay/HEAD/screenshots/custom.png -------------------------------------------------------------------------------- /tests/resources/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zkxs/simple-crosshair-overlay/HEAD/tests/resources/test.png -------------------------------------------------------------------------------- /docs/img/3x3-crosshair.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zkxs/simple-crosshair-overlay/HEAD/docs/img/3x3-crosshair.png -------------------------------------------------------------------------------- /docs/img/6x6-crosshair.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zkxs/simple-crosshair-overlay/HEAD/docs/img/6x6-crosshair.png -------------------------------------------------------------------------------- /tests/resources/test_config_old.toml: -------------------------------------------------------------------------------- 1 | window_dx = 0 2 | window_dy = 0 3 | window_width = 16 4 | window_height = 16 5 | color = "FFFF0005" 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Build artifacts 2 | /target 3 | 4 | # IDE: IntelliJ 5 | *.iml 6 | .idea/ 7 | 8 | # cargo-flamegraph output 9 | flamegraph.svg 10 | -------------------------------------------------------------------------------- /benches/README.md: -------------------------------------------------------------------------------- 1 | This directory contains some benchmarks comparing different implementations of various functions. 2 | This is the whole reason the application is structured as a thin wrapper around a library implementing all 3 | functionality: it's not possible to benchmark a binary with criterion. You _must_ benchmark a library. 4 | -------------------------------------------------------------------------------- /src-lib/private/util/mod.rs: -------------------------------------------------------------------------------- 1 | // This file is part of simple-crosshair-overlay and is licenced under the GNU GPL v3.0. 2 | // See LICENSE file for full text. 3 | // Copyright © 2023 Michael Ripley 4 | 5 | //! Various utilities 6 | 7 | pub mod custom_serializer; 8 | pub mod dialog; 9 | pub mod image; 10 | pub mod numeric; 11 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # https://EditorConfig.org 2 | root = true 3 | 4 | [*] 5 | end_of_line = lf 6 | trim_trailing_whitespace = true 7 | insert_final_newline = true 8 | charset = utf-8 9 | 10 | [*.rs] 11 | indent_style = space 12 | indent_size = 4 13 | 14 | [*.yml] 15 | indent_style = space 16 | indent_size = 2 17 | 18 | [*.md] 19 | indent_style = space 20 | indent_size = 2 21 | trim_trailing_whitespace = false 22 | -------------------------------------------------------------------------------- /tests/resources/test_config.toml: -------------------------------------------------------------------------------- 1 | window_dx = 0 2 | window_dy = 0 3 | window_width = 16 4 | window_height = 16 5 | color = "FFFF0005" 6 | fps = 60 7 | monitor = 1 8 | 9 | [key_bindings] 10 | up = ["Up"] 11 | down = ["Down"] 12 | left = ["Left"] 13 | right = ["Right"] 14 | cycle_monitor = ["LControl", "M"] 15 | scale_increase = ["PageUp"] 16 | scale_decrease = ["PageDown"] 17 | toggle_hidden = ["LControl", "H"] 18 | toggle_adjust = ["LControl", "J"] 19 | toggle_color_picker = ["LControl", "K"] 20 | -------------------------------------------------------------------------------- /tests/resources/test_config_image.toml: -------------------------------------------------------------------------------- 1 | window_dx = 0 2 | window_dy = 0 3 | window_width = 16 4 | window_height = 16 5 | color = "FFFF0005" 6 | fps = 60 7 | image_path = "tests/resources/test.png" 8 | monitor = 1 9 | 10 | [key_bindings] 11 | up = ["Up"] 12 | down = ["Down"] 13 | left = ["Left"] 14 | right = ["Right"] 15 | cycle_monitor = ["LControl", "M"] 16 | scale_increase = ["PageUp"] 17 | scale_decrease = ["PageDown"] 18 | toggle_hidden = ["LControl", "H"] 19 | toggle_adjust = ["LControl", "J"] 20 | toggle_color_picker = ["LControl", "K"] 21 | -------------------------------------------------------------------------------- /src-lib/private/hotkey/mod.rs: -------------------------------------------------------------------------------- 1 | // This file is part of simple-crosshair-overlay and is licenced under the GNU GPL v3.0. 2 | // See LICENSE file for full text. 3 | // Copyright © 2023 Michael Ripley 4 | 5 | //! Keyboard reading system built to read hotkeys without a focused window. 6 | 7 | pub use hotkey_manager::HotkeyManager; 8 | pub use hotkey_manager::KeyBindings; 9 | pub(crate) use keycode::Keycode; // needs to be pub(crate) so the platform-specific implementations can implement From conversions 10 | 11 | mod hotkey_manager; 12 | mod keycode; 13 | -------------------------------------------------------------------------------- /benches/benches.rs: -------------------------------------------------------------------------------- 1 | // This file is part of simple-crosshair-overlay and is licenced under the GNU GPL v3.0. 2 | // See LICENSE file for full text. 3 | // Copyright © 2023 Michael Ripley 4 | 5 | //! Benchmarks for various functions 6 | 7 | use criterion::{criterion_group, criterion_main}; 8 | 9 | use color_picker::*; 10 | use hotkey::*; 11 | 12 | mod color_picker; 13 | mod hotkey; 14 | 15 | criterion_group!( 16 | benches, 17 | bench_color_picker, 18 | bench_hsv_argb, 19 | bench_multiply_color_channel, 20 | bench_key_poll, 21 | bench_key_process 22 | ); 23 | criterion_main!(benches); 24 | -------------------------------------------------------------------------------- /src-lib/lib.rs: -------------------------------------------------------------------------------- 1 | // This file is part of simple-crosshair-overlay and is licenced under the GNU GPL v3.0. 2 | // See LICENSE file for full text. 3 | // Copyright © 2023 Michael Ripley 4 | 5 | //! This library is used by the simple-crosshair-overlay application and is not intended for public 6 | //! use. Due to limitations of criterion, I can only benchmark functions in the public library. Due 7 | //! to limitations of crates.io, all used libraries must be published. The result is I'm forced to 8 | //! publish my internal API publicly. 9 | //! 10 | //! **This library will not be following semantic-versioning** as again, it is not intended to be 11 | //! public API. 12 | 13 | pub mod private; 14 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # To get started with Dependabot version updates, you'll need to specify which 2 | # package ecosystems to update and where the package manifests are located. 3 | # Please see the documentation for all configuration options: 4 | # https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates 5 | 6 | version: 2 7 | updates: 8 | - package-ecosystem: "cargo" 9 | directory: "/" 10 | schedule: 11 | interval: "weekly" 12 | open-pull-requests-limit: 15 13 | groups: 14 | minor-and-patch: 15 | applies-to: version-updates 16 | update-types: 17 | - "minor" 18 | - "patch" 19 | - package-ecosystem: "github-actions" 20 | directory: "/" 21 | schedule: 22 | interval: "weekly" 23 | -------------------------------------------------------------------------------- /src-lib/private/util/custom_serializer.rs: -------------------------------------------------------------------------------- 1 | // This file is part of simple-crosshair-overlay and is licenced under the GNU GPL v3.0. 2 | // See LICENSE file for full text. 3 | // Copyright © 2023 Michael Ripley 4 | 5 | /// Serialize a u32-packed ARGB color as a hex string, because editing a decimal u32 by hand is fucked. 6 | pub mod argb_color { 7 | use serde::{Deserialize, Deserializer, Serializer}; 8 | 9 | pub fn serialize(color: &u32, serializer: S) -> Result 10 | where 11 | S: Serializer, 12 | { 13 | serializer.serialize_str(&format!("{color:08X}")) 14 | } 15 | 16 | pub fn deserialize<'de, D>(deserializer: D) -> Result 17 | where 18 | D: Deserializer<'de>, 19 | { 20 | let s = String::deserialize(deserializer)?; 21 | u32::from_str_radix(&s, 16).map_err(serde::de::Error::custom) 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | # This file is part of simple-crosshair-overlay and is licenced under the GNU GPL v3.0. 2 | # See LICENSE file for full text. 3 | # Copyright © 2025 Michael Ripley 4 | 5 | name: Publish 6 | on: 7 | workflow_dispatch: 8 | secrets: 9 | CARGO_REGISTRY_TOKEN: 10 | required: true 11 | jobs: 12 | publish: 13 | runs-on: windows-latest 14 | steps: 15 | - name: git checkout 16 | uses: actions/checkout@v5 17 | - name: Setup workflow cache 18 | uses: actions/cache@v4 19 | with: 20 | path: | 21 | ~/.cargo/bin/ 22 | ~/.cargo/registry/index/ 23 | ~/.cargo/registry/cache/ 24 | ~/.cargo/git/db/ 25 | target/ 26 | key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} 27 | - name: Update Rust Toolchain 28 | run: rustup update 29 | - name: Publish 30 | run: cargo publish 31 | env: 32 | CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} 33 | -------------------------------------------------------------------------------- /docs/crosshair-alignment.md: -------------------------------------------------------------------------------- 1 | # Crosshair Alignment 2 | 3 | To properly support all games, we need to be able to align the crosshair down to 0.5 pixel increments. Let me explain. 4 | 5 | Many games fire from the center of the screen, but there isn't a pixel at the center of the screen. Take a look at this small, 2x2 pixel "screen" below: 6 | 7 | ![A 2x2 grid of pixels](img/2x2.png) 8 | 9 | As you can see, there isn't a single pixel at the center. Instead, there's an intersection of four pixels. This exact same thing happens on larger displays. 1920x1080, 1440x2560, 3840x2160, you get the idea: all of those are even numbers. 10 | 11 | Here's an example 3x3 crosshair which can't possibly be centered on your screen. 12 | 13 | ![A 3x3 "odd alignment" crosshair](img/3x3-crosshair.png) 14 | 15 | 16 | The solution is to make a crosshair that's an even size. Here's a larger 6x6 crosshair which *can* be centered on your screen. 17 | 18 | ![A 6x6 "even alignment" crosshair](img/6x6-crosshair.png) 19 | 20 | Both of the above crosshair examples are possible in Simple Crosshair Overlay: as you scale up the default crosshair you will notice the lines alternate between 1 pixel and 2 pixel thickness. It's up to you to use an even or odd size based on your personal preference and what's correct for the game you're playing. 21 | -------------------------------------------------------------------------------- /src-lib/private/platform/mod.rs: -------------------------------------------------------------------------------- 1 | // This file is part of simple-crosshair-overlay and is licenced under the GNU GPL v3.0. 2 | // See LICENSE file for full text. 3 | // Copyright © 2023 Michael Ripley 4 | 5 | //! Platform-specific implementations 6 | 7 | use std::fmt::Debug; 8 | 9 | pub use generic::HotkeyManager; 10 | #[cfg(not(target_os = "windows"))] 11 | pub use generic::{WindowHandle, get_foreground_window, set_foreground_window}; 12 | #[cfg(target_os = "windows")] 13 | pub use windows::{WindowHandle, get_foreground_window, set_foreground_window}; 14 | 15 | use crate::private::hotkey::Keycode; 16 | 17 | pub mod generic; // pub so benchmarking can access 18 | 19 | #[cfg(target_os = "windows")] 20 | pub mod windows; // pub so benchmarking can access 21 | 22 | /// `T` is the type used to represent keycodes internally 23 | pub trait KeyboardState: Default 24 | where 25 | T: KeycodeType, 26 | { 27 | /// update internal keyboard state from keyboard 28 | fn poll(&mut self); 29 | 30 | fn get_state(&self) -> &[T]; 31 | } 32 | 33 | pub trait KeycodeType: From + TryInto + Debug { 34 | /// maximum possible number of distinct keycode variants 35 | fn num_variants() -> usize; 36 | 37 | /// Convert a keycode into an index for a lookup table 38 | fn index(&self) -> usize; 39 | } 40 | -------------------------------------------------------------------------------- /src-lib/private/util/image/naive.rs: -------------------------------------------------------------------------------- 1 | // This file is part of simple-crosshair-overlay and is licenced under the GNU GPL v3.0. 2 | // See LICENSE file for full text. 3 | // Copyright © 2023 Michael Ripley 4 | 5 | //! Naive implementations of various functions that are less performant than their optimized 6 | //! alternatives. 7 | //! 8 | //! These are retained for: 9 | //! 10 | //! 1. benchmarking comparisons 11 | //! 2. unit testing known good output 12 | 13 | use crate::private::util::image::hue_value_to_argb; 14 | 15 | #[inline(always)] 16 | pub fn draw_color_picker(buffer: &mut [u32]) { 17 | const EXPECTED_SIZE: usize = 256; 18 | const BUFFER_SIZE: usize = EXPECTED_SIZE * EXPECTED_SIZE; 19 | debug_assert_eq!( 20 | buffer.len(), 21 | BUFFER_SIZE, 22 | "draw_color_picker() passed buffer of wrong size" 23 | ); 24 | 25 | for y in 0..EXPECTED_SIZE { 26 | for x in 0..EXPECTED_SIZE { 27 | buffer[y * EXPECTED_SIZE + x] = hue_value_color_from_coordinates(x, y); 28 | } 29 | } 30 | } 31 | 32 | /// calculate an ARGB color from picked coordinates from a color picker. 33 | /// this color does NOT have premultiplied alpha. 34 | /// `x` and `y` must be within 0..255 35 | fn hue_value_color_from_coordinates(x: usize, y: usize) -> u32 { 36 | hue_value_to_argb(x as u8, 255 - (y as u8)) 37 | } 38 | -------------------------------------------------------------------------------- /benches/hotkey.rs: -------------------------------------------------------------------------------- 1 | // This file is part of simple-crosshair-overlay and is licenced under the GNU GPL v3.0. 2 | // See LICENSE file for full text. 3 | // Copyright © 2023 Michael Ripley 4 | 5 | //! Benchmarks for the hotkey manager 6 | 7 | use std::hint::black_box; 8 | use std::time::{Duration, Instant}; 9 | 10 | use criterion::Criterion; 11 | 12 | use simple_crosshair_overlay::private::hotkey::KeyBindings; 13 | use simple_crosshair_overlay::private::platform; 14 | use simple_crosshair_overlay::private::platform::KeyboardState; 15 | 16 | pub fn bench_key_poll(c: &mut Criterion) { 17 | let mut group = c.benchmark_group("Key poll"); 18 | 19 | let mut keyboard_state = platform::generic::DeviceQueryKeyboardState::default(); 20 | group.bench_function("device_query", |bencher| bencher.iter(|| keyboard_state.poll())); 21 | 22 | group.finish(); 23 | } 24 | 25 | pub fn bench_key_process(c: &mut Criterion) { 26 | let mut group = c.benchmark_group("Key process"); 27 | 28 | let mut hotkey_manager = platform::generic::HotkeyManager::new(&KeyBindings::default()).unwrap(); 29 | 30 | group.bench_function("bitmask", |bencher| { 31 | bencher.iter_custom(|iters| { 32 | let mut duration = Duration::ZERO; 33 | for _i in 0..iters { 34 | hotkey_manager.poll_keys(); 35 | let start = Instant::now(); 36 | platform::generic::HotkeyManager::process_keys(black_box(&mut hotkey_manager)); 37 | duration += start.elapsed(); 38 | } 39 | duration 40 | }); 41 | }); 42 | 43 | group.finish(); 44 | } 45 | -------------------------------------------------------------------------------- /src-lib/private/platform/windows.rs: -------------------------------------------------------------------------------- 1 | // This file is part of simple-crosshair-overlay and is licenced under the GNU GPL v3.0. 2 | // See LICENSE file for full text. 3 | // Copyright © 2023 Michael Ripley 4 | 5 | //! Windows-specific implementations. 6 | //! This is only in the module tree on Windows targets. 7 | 8 | use winapi::shared::windef::HWND; 9 | use winapi::um::winuser; 10 | 11 | /// null-safe window handle 12 | #[derive(Copy, Clone, Debug)] 13 | pub struct WindowHandle { 14 | hwnd: HWND, 15 | } 16 | 17 | impl WindowHandle { 18 | /// must not be called with a null pointer 19 | fn new(hwnd: HWND) -> WindowHandle { 20 | debug_assert!(!hwnd.is_null()); 21 | WindowHandle { hwnd } 22 | } 23 | 24 | /// will never return null pointer 25 | fn hwnd(self) -> HWND { 26 | debug_assert!(!self.hwnd.is_null()); 27 | self.hwnd 28 | } 29 | } 30 | 31 | /// wrapper around https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getforegroundwindow 32 | /// 33 | /// this converts null pointers into None 34 | pub fn get_foreground_window() -> Option { 35 | unsafe { 36 | match winuser::GetForegroundWindow() { 37 | hwnd if hwnd.is_null() => None, 38 | hwnd => Some(WindowHandle::new(hwnd)), 39 | } 40 | } 41 | } 42 | 43 | /// wrapper around https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow 44 | /// 45 | /// this does not handle null pointers, as it shouldn't be possible to get a null inside a `WindowHandle`. 46 | /// `true` is returned if the foreground window was set successfully. 47 | pub fn set_foreground_window(window_handle: WindowHandle) -> bool { 48 | unsafe { winuser::SetForegroundWindow(window_handle.hwnd()) != 0 } 49 | } 50 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | version = "1.2.2" 3 | authors = ["Michael Ripley RGB conversion 16 | pub fn hsv_to_argb(hue: u8, saturation: u8, value: u8) -> u32 { 17 | const HUE_RATIO: f64 = 360.0 / 255.0; 18 | let hue = hue as f64 * HUE_RATIO; 19 | let saturation = saturation as f64 / 255.0; 20 | let value = value as f64 / 255.0; 21 | 22 | let hue_over_60 = hue / 60.0; 23 | let chroma = value * saturation; 24 | let intermediate_color = chroma * (1.0 - (hue_over_60 % 2.0 - 1.0).abs()); 25 | 26 | let [r, g, b] = match hue_over_60 { 27 | h if h < 1.0 => [chroma, intermediate_color, 0.0], 28 | h if h < 2.0 => [intermediate_color, chroma, 0.0], 29 | h if h < 3.0 => [0.0, chroma, intermediate_color], 30 | h if h < 4.0 => [0.0, intermediate_color, chroma], 31 | h if h < 5.0 => [intermediate_color, 0.0, chroma], 32 | _ => [chroma, 0.0, intermediate_color], 33 | }; 34 | 35 | let r = (r * 255.0).round() as u8; 36 | let g = (g * 255.0).round() as u8; 37 | let b = (b * 255.0).round() as u8; 38 | 39 | u32::from_le_bytes([b, g, r, 255]) 40 | } 41 | 42 | /// alpha premultiply implemented with f64 precision and rounding to nearest int 43 | pub fn multiply_color_channels_u8(c: u8, a: u8) -> u8 { 44 | (c as f64 * a as f64 / 255f64).round() as u8 45 | } 46 | -------------------------------------------------------------------------------- /benches/color_picker.rs: -------------------------------------------------------------------------------- 1 | // This file is part of simple-crosshair-overlay and is licenced under the GNU GPL v3.0. 2 | // See LICENSE file for full text. 3 | // Copyright © 2023 Michael Ripley 4 | 5 | //! Color picker benchmarks. 6 | 7 | use criterion::{BatchSize, Criterion}; 8 | use std::hint::black_box; 9 | 10 | use simple_crosshair_overlay::private::util::image; 11 | 12 | pub fn bench_color_picker(c: &mut Criterion) { 13 | let mut group = c.benchmark_group("Color Picker Implementations"); 14 | 15 | group.bench_function("Naive", |bencher| { 16 | bencher.iter_batched_ref( 17 | || vec![0; 256 * 256], 18 | |buffer| image::naive::draw_color_picker(black_box(buffer.as_mut_slice())), 19 | BatchSize::SmallInput, 20 | ) 21 | }); 22 | 23 | group.bench_function("Optimized", |bencher| { 24 | bencher.iter_batched_ref( 25 | || vec![0; 252 * 252], 26 | |buffer| image::draw_color_picker(black_box(buffer.as_mut_slice())), 27 | BatchSize::SmallInput, 28 | ) 29 | }); 30 | 31 | group.finish(); 32 | } 33 | 34 | pub fn bench_hsv_argb(c: &mut Criterion) { 35 | let mut group = c.benchmark_group("HSV -> ARGB conversion implementations"); 36 | 37 | group.bench_function("Precise HSV", |bencher| { 38 | bencher.iter(|| image::precise::hsv_to_argb(black_box(0xFF), black_box(0xFF), black_box(0xFF))); 39 | }); 40 | 41 | group.bench_function("Optimized HV", |bencher| { 42 | bencher.iter(|| image::hue_value_to_argb(black_box(0xFF), black_box(0xFF))); 43 | }); 44 | 45 | group.bench_function("Optimized HA", |bencher| { 46 | bencher.iter(|| image::hue_alpha_to_argb(black_box(0xFF), black_box(0xFF))); 47 | }); 48 | 49 | group.finish(); 50 | } 51 | 52 | pub fn bench_multiply_color_channel(c: &mut Criterion) { 53 | let mut group = c.benchmark_group("Color channel multiply implementations"); 54 | 55 | group.bench_function("Precise", |bencher| { 56 | bencher.iter(|| image::precise::multiply_color_channels_u8(black_box(0xFF), black_box(0x7F))); 57 | }); 58 | 59 | group.bench_function("Optimized", |bencher| { 60 | bencher.iter(|| image::multiply_color_channels_u8(black_box(0xFF), black_box(0x7F))); 61 | }); 62 | 63 | group.finish(); 64 | } 65 | -------------------------------------------------------------------------------- /src-lib/private/hotkey/keycode.rs: -------------------------------------------------------------------------------- 1 | // This file is part of simple-crosshair-overlay and is licenced under the GNU GPL v3.0. 2 | // See LICENSE file for full text. 3 | // Copyright © 2023 Michael Ripley 4 | 5 | //! Listing of all keycodes and conversions between other keycode types 6 | 7 | use serde::{Deserialize, Serialize}; 8 | 9 | /// Our own Keycode type, which *should* be a 1:1 mapping with `device_query::Keycode`. 10 | /// You may be wondering why I don't just use `device_query::Keycode`. Well, I can't 11 | /// `#[derive(Serialize, Deserialize)]` for a type I don't own, so alas I had to make this 12 | /// incredibly verbose file to allow serde to handle the Keycode enum. 13 | #[derive(Serialize, Deserialize, Debug, Clone, Copy)] 14 | pub enum Keycode { 15 | Key0, 16 | Key1, 17 | Key2, 18 | Key3, 19 | Key4, 20 | Key5, 21 | Key6, 22 | Key7, 23 | Key8, 24 | Key9, 25 | A, 26 | B, 27 | C, 28 | D, 29 | E, 30 | F, 31 | G, 32 | H, 33 | I, 34 | J, 35 | K, 36 | L, 37 | M, 38 | N, 39 | O, 40 | P, 41 | Q, 42 | R, 43 | S, 44 | T, 45 | U, 46 | V, 47 | W, 48 | X, 49 | Y, 50 | Z, 51 | F1, 52 | F2, 53 | F3, 54 | F4, 55 | F5, 56 | F6, 57 | F7, 58 | F8, 59 | F9, 60 | F10, 61 | F11, 62 | F12, 63 | Escape, 64 | Space, 65 | LControl, 66 | RControl, 67 | LShift, 68 | RShift, 69 | LAlt, 70 | RAlt, 71 | #[serde(alias = "Meta")] // for backwards compatibility 72 | LMeta, 73 | RMeta, 74 | Enter, 75 | Up, 76 | Down, 77 | Left, 78 | Right, 79 | Backspace, 80 | CapsLock, 81 | Tab, 82 | Home, 83 | End, 84 | PageUp, 85 | PageDown, 86 | Insert, 87 | Delete, 88 | Numpad0, 89 | Numpad1, 90 | Numpad2, 91 | Numpad3, 92 | Numpad4, 93 | Numpad5, 94 | Numpad6, 95 | Numpad7, 96 | Numpad8, 97 | Numpad9, 98 | NumpadSubtract, 99 | NumpadAdd, 100 | NumpadDivide, 101 | NumpadMultiply, 102 | Grave, 103 | Minus, 104 | Equal, 105 | LeftBracket, 106 | RightBracket, 107 | BackSlash, 108 | Semicolon, 109 | Apostrophe, 110 | Comma, 111 | Dot, 112 | Slash, 113 | F13, 114 | F14, 115 | F15, 116 | F16, 117 | F17, 118 | F18, 119 | F19, 120 | F20, 121 | Command, 122 | LOption, 123 | ROption, 124 | NumpadEquals, 125 | NumpadEnter, 126 | NumpadDecimal, 127 | } 128 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | # This file is part of simple-crosshair-overlay and is licenced under the GNU GPL v3.0. 2 | # See LICENSE file for full text. 3 | # Copyright © 2025 Michael Ripley 4 | 5 | name: CI 6 | on: 7 | pull_request: 8 | branches: # run for pull requests that target master or prerelease 9 | - master 10 | - prerelease 11 | paths-ignore: # ignore files that can't alter build output 12 | - '**.md' 13 | - .github/dependabot.yml 14 | - .github/workflows/build.yml 15 | - .github/workflows/publish.yml 16 | - .gitignore 17 | - docs/** 18 | - LICENSE 19 | - screenshots/** 20 | jobs: 21 | cargo-deny: 22 | runs-on: ubuntu-latest 23 | steps: 24 | - uses: actions/checkout@v5 25 | - uses: EmbarkStudios/cargo-deny-action@v2 26 | cargo-fmt: 27 | runs-on: ubuntu-latest 28 | steps: 29 | - name: Update Rust Toolchain 30 | run: rustup update 31 | - name: Install Cargo 32 | run: rustup component add cargo 33 | - name: Install Clippy 34 | run: rustup component add rustfmt 35 | - uses: actions/checkout@v5 36 | - name: Format 37 | run: cargo fmt --check 38 | test: 39 | strategy: 40 | matrix: 41 | target: 42 | - runs-on: windows-latest 43 | triple: x86_64-pc-windows-msvc 44 | build-name: Windows 45 | artifact-suffix: '' 46 | suffix: .exe 47 | path-separator: '\' 48 | runner-can-execute: true 49 | # - runs-on: ubuntu-latest 50 | # triple: x86_64-unknown-linux-gnu 51 | # build-name: Linux 52 | # artifact-suffix: -linux 53 | # suffix: '' 54 | # path-separator: '/' 55 | # runner-can-execute: true 56 | - runs-on: macos-latest 57 | triple: x86_64-apple-darwin 58 | build-name: macOS x86 59 | artifact-suffix: -mac-x86 60 | suffix: '' 61 | path-separator: '/' 62 | runner-can-execute: true 63 | - runs-on: macos-latest 64 | triple: aarch64-apple-darwin 65 | build-name: macOS ARM 66 | artifact-suffix: -mac-arm 67 | suffix: '' 68 | path-separator: '/' 69 | runner-can-execute: false 70 | fail-fast: false 71 | name: Test ${{ matrix.target.build-name }} 72 | runs-on: ${{ matrix.target.runs-on }} 73 | steps: 74 | - name: Install Rust target 75 | run: rustup target add ${{ matrix.target.triple }} 76 | - name: Install Cargo 77 | run: rustup component add cargo 78 | - name: Install Clippy 79 | run: rustup component add clippy 80 | - name: git checkout 81 | uses: actions/checkout@v5 82 | - name: Setup workflow cache 83 | uses: actions/cache@v4 84 | with: 85 | path: | 86 | ~/.cargo/bin/ 87 | ~/.cargo/registry/index/ 88 | ~/.cargo/registry/cache/ 89 | ~/.cargo/git/db/ 90 | target/ 91 | key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} 92 | - name: Install extra Linux dependencies 93 | if: matrix.target.runs-on == 'ubuntu-latest' 94 | run: | # gdk-sys needs {libgtk-3-dev}. 95 | sudo apt-get update 96 | sudo apt-get install -y libgtk-3-dev 97 | - name: Check 98 | run: cargo clippy --target ${{ matrix.target.triple }} --all-features --all-targets 99 | - name: Test 100 | if: matrix.target.runner-can-execute 101 | run: cargo test --target ${{ matrix.target.triple }} 102 | -------------------------------------------------------------------------------- /src-lib/private/util/numeric.rs: -------------------------------------------------------------------------------- 1 | // This file is part of simple-crosshair-overlay and is licenced under the GNU GPL v3.0. 2 | // See LICENSE file for full text. 3 | // Copyright © 2023 Michael Ripley 4 | 5 | //! Numeric utilities 6 | 7 | use std::time::Duration; 8 | 9 | pub fn fps_to_tick_interval(fps: u32) -> Duration { 10 | let millis = 1000.div_ceil_placeholder(fps); 11 | Duration::from_millis(millis as u64) 12 | } 13 | 14 | pub trait DivCeil { 15 | /// Intentionally _not_ named `div_ceil` to avoid name conflicts with an 16 | /// [unstable feature I can't use](https://github.com/rust-lang/rust/issues/88581). Thanks Rust. 17 | /// Very cool that **unstable** features can conflict with stable names and win. 18 | /// 19 | /// This does an integer ceiling division. 20 | fn div_ceil_placeholder(&self, rhs: Self) -> Self; 21 | } 22 | 23 | impl DivCeil for usize { 24 | // implementation comes from https://github.com/rust-lang/rust/pull/88582/files 25 | fn div_ceil_placeholder(&self, rhs: Self) -> Self { 26 | let quotient = self / rhs; 27 | let remainder = self % rhs; 28 | if remainder > 0 { quotient + 1 } else { quotient } 29 | } 30 | } 31 | 32 | impl DivCeil for u32 { 33 | // implementation comes from https://github.com/rust-lang/rust/pull/88582/files 34 | fn div_ceil_placeholder(&self, rhs: Self) -> Self { 35 | let quotient = self / rhs; 36 | let remainder = self % rhs; 37 | if remainder > 0 { quotient + 1 } else { quotient } 38 | } 39 | } 40 | 41 | impl DivCeil for u64 { 42 | // implementation comes from https://github.com/rust-lang/rust/pull/88582/files 43 | fn div_ceil_placeholder(&self, rhs: u64) -> u64 { 44 | let quotient = self / rhs; 45 | let remainder = self % rhs; 46 | if remainder > 0 { quotient + 1 } else { quotient } 47 | } 48 | } 49 | 50 | pub trait DivFloor { 51 | /// Intentionally _not_ named `div_floor` to avoid name conflicts with an 52 | /// [unstable feature I can't use](https://github.com/rust-lang/rust/issues/88581). Thanks Rust. 53 | /// Very cool that **unstable** features can conflict with stable names and win. 54 | /// 55 | /// This does an integer floor division. 56 | fn div_floor_placeholder(&self, rhs: Self) -> Self; 57 | } 58 | 59 | impl DivFloor for i32 { 60 | // implementation comes from https://github.com/rust-lang/rust/pull/88582/files 61 | fn div_floor_placeholder(&self, rhs: Self) -> Self { 62 | let d = self / rhs; 63 | let r = self % rhs; 64 | if (r > 0 && rhs < 0) || (r < 0 && rhs > 0) { 65 | d - 1 66 | } else { 67 | d 68 | } 69 | } 70 | } 71 | 72 | #[cfg(test)] 73 | mod test_div_rounding { 74 | use super::*; 75 | 76 | /// this is obvious, but I included it for completeness with the following test 77 | #[test] 78 | fn positive_div_rounds_down() { 79 | assert_eq!(101 / 2, 50); 80 | } 81 | 82 | /// rust integer division always rounds towards zero, this test is just to document that because we actually care about rounding towards -Infinity for some pixel math 83 | #[test] 84 | fn negative_div_rounds_up() { 85 | assert_eq!(-101 / 2, -50); 86 | } 87 | 88 | #[test] 89 | fn div_ceil_usize_no_round() { 90 | assert_eq!(100usize.div_ceil_placeholder(2), 50); 91 | } 92 | 93 | #[test] 94 | fn div_ceil_u64_no_round() { 95 | assert_eq!(100u64.div_ceil_placeholder(2), 50); 96 | } 97 | 98 | #[test] 99 | fn div_ceil_usize_rounds_up() { 100 | assert_eq!(101usize.div_ceil_placeholder(2), 51); 101 | } 102 | 103 | #[test] 104 | fn div_ceil_u64_rounds_up() { 105 | assert_eq!(101u64.div_ceil_placeholder(2), 51); 106 | } 107 | 108 | #[test] 109 | fn positive_div_floor_rounds_down() { 110 | assert_eq!(101.div_floor_placeholder(2), 50); 111 | } 112 | 113 | #[test] 114 | fn positive_div_floor_no_round() { 115 | assert_eq!(100.div_floor_placeholder(2), 50); 116 | } 117 | 118 | #[test] 119 | fn negative_div_floor_rounds_down() { 120 | assert_eq!((-101).div_floor_placeholder(2), -51); 121 | } 122 | 123 | #[test] 124 | fn negative_div_floor_no_round() { 125 | assert_eq!((-100).div_floor_placeholder(2), -50); 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | // This file is part of simple-crosshair-overlay and is licenced under the GNU GPL v3.0. 2 | // See LICENSE file for full text. 3 | // Copyright © 2023-2024 Michael Ripley 4 | 5 | #![windows_subsystem = "windows"] // necessary to remove the console window on Windows 6 | 7 | use std::io; 8 | 9 | use debug_print::debug_println; 10 | use winit::event_loop::{DeviceEvents, EventLoop}; 11 | use winit::window::{CursorGrabMode, Window}; 12 | 13 | use simple_crosshair_overlay::private::platform; 14 | use simple_crosshair_overlay::private::settings::CONFIG_PATH; 15 | use simple_crosshair_overlay::private::settings::Settings; 16 | use simple_crosshair_overlay::private::util::dialog; 17 | 18 | mod tray; 19 | mod window; 20 | 21 | static ICON_TOOLTIP: &str = "Simple Crosshair Overlay"; 22 | 23 | /// constants generated in build.rs 24 | mod build_constants { 25 | include!(env!("CONSTANTS_PATH")); 26 | } 27 | 28 | fn main() { 29 | // Initialize Eventloop before everything 30 | let event_loop: EventLoop = EventLoop::new().unwrap(); 31 | // in theory Wait is now the default ControlFlow, so the following isn't needed: 32 | // event_loop.set_control_flow(ControlFlow::Wait); 33 | 34 | // settings has a decent quantity of data in it, but it never really gets moved so we can just leave it on the stack 35 | // the image buffer is internally boxed so don't worry about that 36 | let settings = match Settings::load() { 37 | Ok(settings) => settings, 38 | Err(e) if e.kind() == io::ErrorKind::NotFound => Settings::default(), // generate new settings file when it doesn't exist 39 | Err(e) => { 40 | dialog::show_warning(format!( 41 | "Error loading settings file \"{}\". Resetting to default settings.\n\n{}", 42 | CONFIG_PATH.display(), 43 | e 44 | )); 45 | Settings::default() 46 | } 47 | }; 48 | 49 | // only functional on Linux targets 50 | event_loop.listen_device_events(DeviceEvents::Never); 51 | 52 | // start sending tick events 53 | start_tick_sender(&settings, &event_loop); 54 | 55 | // create the winit application 56 | let mut window_state = window::State::new(settings, &event_loop); 57 | 58 | // pass control to the event loop 59 | event_loop.run_app(&mut window_state).unwrap(); 60 | } 61 | 62 | fn start_tick_sender(settings: &Settings, event_loop: &EventLoop) { 63 | let user_event_sender = event_loop.create_proxy(); 64 | let key_process_interval = settings.tick_interval; 65 | std::thread::Builder::new() 66 | .name("tick-sender".to_string()) 67 | .spawn(move || { 68 | loop { 69 | let _ = user_event_sender.send_event(()); 70 | std::thread::sleep(key_process_interval); 71 | } 72 | }) 73 | .unwrap(); // if we fail to spawn a thread something is super wrong and we ought to panic 74 | } 75 | 76 | /// Updates the window state after entering or exiting color picker mode 77 | /// 78 | /// If `save_focused` is `true`, this will make a best-effort to restore the previously focused window next time we exit color pick mode. 79 | fn handle_color_pick( 80 | color_pick: bool, 81 | window: &Window, 82 | last_focused_window: &mut Option, 83 | save_focused: bool, 84 | ) { 85 | if color_pick { 86 | *last_focused_window = if save_focused { 87 | // back up the last-focused window right before we focus ourself 88 | platform::get_foreground_window() 89 | } else { 90 | // make sure we don't have some weird old window handle saved if we shouldn't be saving focus 91 | None 92 | }; 93 | window.set_cursor_hittest(true).unwrap(); // fails on non Windows/Mac/Linux platforms 94 | window.focus_window(); 95 | window.set_cursor_grab(CursorGrabMode::Confined).unwrap(); // if we do this after the window is focused, it'll move the cursor to the window for us. 96 | } else { 97 | window.set_cursor_grab(CursorGrabMode::None).unwrap(); 98 | window.set_cursor_hittest(false).unwrap(); 99 | if let Some(last_focused_window) = *last_focused_window { 100 | let _success = platform::set_foreground_window(last_focused_window); 101 | debug_println!("focus previous window {last_focused_window:?} {_success}"); 102 | } 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src-lib/private/util/dialog.rs: -------------------------------------------------------------------------------- 1 | // This file is part of simple-crosshair-overlay and is licenced under the GNU GPL v3.0. 2 | // See LICENSE file for full text. 3 | // Copyright © 2023-2025 Michael Ripley 4 | 5 | use std::path::PathBuf; 6 | use std::sync::Mutex; 7 | use std::sync::mpsc; 8 | use std::thread::JoinHandle; 9 | 10 | use lazy_static::lazy_static; 11 | use native_dialog::{DialogBuilder, MessageLevel}; 12 | 13 | lazy_static! { 14 | 15 | // this is some arcane bullshit to get a global mpsc 16 | // the sender can be cloned, and we'll do that via a thread_local later 17 | // the receiver can't be cloned, so just shove it in an Option so we can take() it later. 18 | static ref DIALOG_REQUEST_CHANNEL: (Mutex>, Mutex>>) = { 19 | let (sender, receiver) = mpsc::channel(); 20 | let sender = Mutex::new(sender); 21 | let receiver = Mutex::new(Some(receiver)); 22 | (sender, receiver) 23 | }; 24 | } 25 | 26 | thread_local! { 27 | // We only need one of these per thread. As we don't use any thread pools this should be a one-time cost on application startup. 28 | static DIALOG_REQUEST_SENDER: mpsc::Sender = DIALOG_REQUEST_CHANNEL.0.lock().unwrap().clone(); 29 | } 30 | 31 | /// The different types of requests the dialog worker thread can process 32 | enum DialogRequest { 33 | /// Show a file browser for the user to select a PNG image 34 | PngPath, 35 | /// Show an informational popup with the provided text 36 | Info(String), 37 | /// Show a warning popup with the provided text 38 | Warning(String), 39 | /// Stop the dialog worker thread 40 | Terminate, 41 | } 42 | 43 | pub struct DialogWorker { 44 | join_handle: Option>, 45 | file_path_receiver: mpsc::Receiver>, 46 | } 47 | 48 | impl DialogWorker { 49 | /// try to get a file path from the dialog worker's internal queue 50 | pub fn try_recv_file_path(&self) -> Result, mpsc::TryRecvError> { 51 | self.file_path_receiver.try_recv() 52 | } 53 | 54 | /// signal the dialog worker thread to shut down once it's done processing its queue 55 | pub fn shutdown(&mut self) -> Option<()> { 56 | let _ = DIALOG_REQUEST_SENDER.with(|sender| sender.send(DialogRequest::Terminate)); 57 | self.join_handle.take()?.join().ok() 58 | } 59 | } 60 | 61 | /// show a native popup with an info icon + sound 62 | pub fn show_info(text: String) { 63 | let _ = DIALOG_REQUEST_SENDER.with(|sender| sender.send(DialogRequest::Info(text))); 64 | } 65 | 66 | /// show a native popup with a warning icon + sound 67 | pub fn show_warning(text: String) { 68 | let _ = DIALOG_REQUEST_SENDER.with(|sender| sender.send(DialogRequest::Warning(text))); 69 | } 70 | 71 | /// show a native popup requesting a path to a PNG 72 | pub fn request_png() { 73 | let _ = DIALOG_REQUEST_SENDER.with(|sender| sender.send(DialogRequest::PngPath)); 74 | } 75 | 76 | pub fn spawn_worker() -> DialogWorker { 77 | let (file_path_sender, file_path_receiver) = mpsc::channel(); 78 | let dialog_request_receiver = DIALOG_REQUEST_CHANNEL.1.lock().unwrap().take().unwrap(); 79 | 80 | // native dialogs block a thread, so we'll spin up a single thread to loop through queued dialogs. 81 | // If we ever need to show multiple dialogs, they just get queued. 82 | let join_handle = std::thread::Builder::new() 83 | .name("dialog-worker".to_string()) 84 | .spawn(move || { 85 | loop { 86 | // block waiting for a file read request 87 | match dialog_request_receiver.recv().unwrap() { 88 | DialogRequest::PngPath => { 89 | let path = DialogBuilder::file() 90 | .add_filter("PNG Image", ["png"]) 91 | .open_single_file() 92 | .show() 93 | .ok() 94 | .flatten(); 95 | 96 | let _ = file_path_sender.send(path); 97 | } 98 | DialogRequest::Info(text) => { 99 | DialogBuilder::message() 100 | .set_level(MessageLevel::Info) 101 | .set_title("Simple Crosshair Overlay") 102 | .set_text(&text) 103 | .alert() 104 | .show() 105 | .unwrap(); 106 | } 107 | DialogRequest::Warning(text) => { 108 | DialogBuilder::message() 109 | .set_level(MessageLevel::Warning) 110 | .set_title("Simple Crosshair Overlay") 111 | .set_text(&text) 112 | .alert() 113 | .show() 114 | .unwrap(); 115 | } 116 | DialogRequest::Terminate => break, 117 | } 118 | } 119 | }) 120 | .unwrap(); 121 | 122 | DialogWorker { 123 | join_handle: Some(join_handle), // we take() from this later 124 | file_path_receiver, 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Simple Crosshair Overlay 2 | 3 | A performant native crosshair overlay without unnecessary bloat. Free and open-source software. 4 | 5 | ![screenshot of the default, simple crosshair in action](screenshots/cross.png) 6 | 7 |
8 | Click here to expand another screenshot demoing a custom PNG crosshair 9 | 10 | ![screenshot of a custom PNG crosshair](screenshots/custom.png) 11 | 12 |
13 | 14 | ## Features 15 | 16 | - Works on any application that's not fullscreen exclusive. You **must** use windowed or borderless-windowed mode on your game. This was an intentional design choice, as rendering into a fullscreen-exclusive game is not anti-cheat-compatible. 17 | - Performant: the overlay is only redrawn when you change the crosshair. CPU, GPU, and memory usage are minimal. 18 | - Minimal UI: managed via a tray icon and hotkeys. 19 | - Comes with a simple default crosshair that can be scaled and recolored to your preference. 20 | - Can use custom PNG images as crosshairs. 21 | - No installer. The only file this program creates is small configuration saved in `%appdata%\simple-crosshair-overlay`. 22 | 23 | ## Installation 24 | 25 | 1. Download simple-crosshair-overlay.exe from the [latest release](https://github.com/zkxs/simple-crosshair-overlay/releases/latest), and save it to a location of your choice 26 | 2. Run simple-crosshair-overlay.exe 27 | 3. Optionally, if you want a start menu shortcut you can make one yourself! Simply right-click simple-crosshair-overlay.exe and select "Pin to Start". This will automatically create a shortcut in `%appdata%\Microsoft\Windows\Start Menu\Programs`. 28 | 29 | **macOS** binaries are available, but I lack hardware to test against so I do not know if they work. If you're using simple-crosshair-overlay on macOS please let me know in the [macOS support tracking issue](https://github.com/zkxs/simple-crosshair-overlay/issues/3). 30 | 31 | **Linux** is presently unsupported, pending resolution of a few issues. See the [Linux support tracking issue](https://github.com/zkxs/simple-crosshair-overlay/issues/6) for details. 32 | 36 | 37 | ## Usage 38 | 39 | Use the tray icon to: 40 | 41 | - Toggle crosshair visibility (you can also use Ctrl+H) 42 | - Toggle **Adjust Mode** (you can also use Ctrl+J) 43 | - Pick a color for the default crosshair (you can also use Ctrl+K if you are in Adjust Mode). 44 | - Load a PNG image as your crosshair 45 | - Reset crosshair to default settings 46 | - Safely exit the application and save your settings 47 | 48 | In **Adjust Mode**: 49 | 50 | - Arrow keys to move the crosshair 51 | - PageUp/PageDown to increase/decrease the crosshair scale 52 | - Ctrl+M to cycle through your monitors 53 | - Ctrl+K to pick a color for the default crosshair 54 | 55 | ### Custom PNG Crosshairs 56 | 57 | Your PNG file must use RGBA pixel format. Most PNGs are already saved this way, but you may need to specifically save 58 | it with an alpha channel if Simple Crosshair Overlay is giving you an error. 59 | 60 | Also note that changing the color of the built-in crosshair has no effect on custom PNG crosshairs. If you want your custom 61 | crosshair in a different color you'll have to make that change in an image editor. 62 | 63 | ### Changing Hotkeys 64 | 65 | Hotkeys cannot currently be changed in-application. To edit your hotkeys, do the following: 66 | 67 | 1. Open the config file `%appdata%\simple-crosshair-overlay\config\config.toml`. If this file does not exist, starting 68 | and exiting the program once will create it. 69 | 2. Change hotkeys in the `key_bindings` section by referencing the Keycode values defined in [keycode.rs](src-lib/private/hotkey/keycode.rs) 70 | 71 | ## Notes 72 | 73 | Simple Crosshair Overlay supports 0.5 pixel alignment with the default crosshair by scaling it to an even or odd size. If this sounds nonsensical, I've written [a quick explanation of this concept](docs/crosshair-alignment.md). If you're using a custom PNG crosshair, then providing the correct even/odd size is up to you. 74 | 75 | ## Installing from Source 76 | 77 | 1. [Install Rust](https://www.rust-lang.org/tools/install) 78 | 2. `cargo install simple-crosshair-overlay` 79 | 80 | ## Building from Source 81 | 82 | 1. [Install Rust](https://www.rust-lang.org/tools/install) 83 | 2. Clone the project 84 | 3. `cargo build --release` 85 | 86 | ## Feedback 87 | 88 | If you have bugs to report please let me know by opening an [issue](https://github.com/zkxs/simple-crosshair-overlay/issues). 89 | 90 | For suggestions, questions, or even just to say hey, feel free to start a [discussion](https://github.com/zkxs/simple-crosshair-overlay/discussions). 91 | 92 | ## License 93 | 94 | Simple Crosshair Overlay is free software: you can redistribute it and/or modify it under the terms of the 95 | [GNU General Public License](LICENSE) as published by the Free Software Foundation, under version 3 of the 96 | License. 97 | 98 | Simple Crosshair Overlay is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 99 | even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 100 | [GNU General Public License](LICENSE) for more details. 101 | 102 | A full list of dependencies is available in [Cargo.toml](Cargo.toml), or a breakdown of dependencies by license can be 103 | generated with `cargo deny list`. 104 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | # This file is part of simple-crosshair-overlay and is licenced under the GNU GPL v3.0. 2 | # See LICENSE file for full text. 3 | # Copyright © 2025 Michael Ripley 4 | 5 | name: Build 6 | on: 7 | push: 8 | paths-ignore: # ignore files that can't alter build output 9 | - '**.md' 10 | - .github/dependabot.yml 11 | - .github/workflows/ci.yml 12 | - .github/workflows/publish.yml 13 | - .gitignore 14 | - docs/** 15 | - LICENSE 16 | - screenshots/** 17 | jobs: 18 | cargo-deny: 19 | # only run for pushes to tags or non-dependabot branches 20 | if: startsWith(github.ref, 'refs/tags/') || (startsWith(github.ref, 'refs/heads/') && !startsWith(github.ref, 'refs/heads/dependabot/')) 21 | runs-on: ubuntu-latest 22 | steps: 23 | - uses: actions/checkout@v5 24 | - uses: EmbarkStudios/cargo-deny-action@v2 25 | cargo-fmt: 26 | # only run for pushes to tags or non-dependabot branches 27 | if: startsWith(github.ref, 'refs/tags/') || (startsWith(github.ref, 'refs/heads/') && !startsWith(github.ref, 'refs/heads/dependabot/')) 28 | runs-on: ubuntu-latest 29 | steps: 30 | - name: Update Rust Toolchain 31 | run: rustup update 32 | - name: Install Cargo 33 | run: rustup component add cargo 34 | - name: Install Clippy 35 | run: rustup component add rustfmt 36 | - uses: actions/checkout@v5 37 | - name: Format 38 | run: cargo fmt --check 39 | build: 40 | # only run for pushes to tags or non-dependabot branches 41 | if: startsWith(github.ref, 'refs/tags/') || (startsWith(github.ref, 'refs/heads/') && !startsWith(github.ref, 'refs/heads/dependabot/')) 42 | strategy: 43 | matrix: 44 | target: 45 | - runs-on: windows-latest 46 | triple: x86_64-pc-windows-msvc 47 | build-name: Windows 48 | artifact-suffix: '' 49 | suffix: .exe 50 | path-separator: '\' 51 | runner-can-execute: true 52 | # - runs-on: ubuntu-latest 53 | # triple: x86_64-unknown-linux-gnu 54 | # build-name: Linux 55 | # artifact-suffix: -linux 56 | # suffix: '' 57 | # path-separator: '/' 58 | # runner-can-execute: true 59 | - runs-on: macos-latest 60 | triple: x86_64-apple-darwin 61 | build-name: macOS x86 62 | artifact-suffix: -mac-x86 63 | suffix: '' 64 | path-separator: '/' 65 | runner-can-execute: true 66 | - runs-on: macos-latest 67 | triple: aarch64-apple-darwin 68 | build-name: macOS ARM 69 | artifact-suffix: -mac-arm 70 | suffix: '' 71 | path-separator: '/' 72 | runner-can-execute: false 73 | fail-fast: false 74 | name: Build ${{ matrix.target.build-name }} 75 | runs-on: ${{ matrix.target.runs-on }} 76 | steps: 77 | - name: Update Rust Toolchain 78 | run: rustup update 79 | - name: Install Rust target 80 | run: rustup target add ${{ matrix.target.triple }} 81 | - name: Install nightly Rust 82 | run: rustup toolchain install nightly 83 | - name: Install Cargo 84 | run: rustup component add cargo 85 | - name: Install Clippy 86 | run: rustup component add clippy 87 | - name: Install nightly rust-src 88 | run: rustup component add rust-src --toolchain nightly 89 | - name: git checkout 90 | uses: actions/checkout@v5 91 | - name: Setup workflow cache 92 | uses: actions/cache@v4 93 | with: 94 | path: | 95 | ~/.cargo/bin/ 96 | ~/.cargo/registry/index/ 97 | ~/.cargo/registry/cache/ 98 | ~/.cargo/git/db/ 99 | target/ 100 | key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} 101 | - name: Install extra Linux dependencies 102 | if: matrix.target.runs-on == 'ubuntu-latest' 103 | run: | # gdk-sys needs {libgtk-3-dev}. 104 | sudo apt-get update 105 | sudo apt-get install -y libgtk-3-dev 106 | - name: Check 107 | run: cargo clippy --target ${{ matrix.target.triple }} --all-features --all-targets 108 | - name: Test 109 | if: matrix.target.runner-can-execute 110 | run: cargo test --target ${{ matrix.target.triple }} 111 | - name: Build 112 | run: cargo +nightly build -Z build-std=std --release --target ${{ matrix.target.triple }} 113 | - name: Upload workflow artifact 114 | uses: actions/upload-artifact@v4 115 | with: 116 | name: simple-crosshair-overlay-${{ matrix.target.triple }} 117 | path: ./target/${{ matrix.target.triple }}/release/simple-crosshair-overlay${{ matrix.target.suffix }} 118 | if-no-files-found: error 119 | - name: Rename artifact for release # action-gh-release is incapable of renaming files, so I have to do it manually 120 | if: startsWith(github.ref, 'refs/tags/') # only run for pushes to tags 121 | run: | 122 | cp "./target/${{ matrix.target.triple }}/release/simple-crosshair-overlay${{ matrix.target.suffix }}" "${{ runner.temp }}/simple-crosshair-overlay${{ matrix.target.artifact-suffix }}${{ matrix.target.suffix }}" 123 | ls "${{ runner.temp }}" 124 | file "${{ runner.temp }}${{ matrix.target.path-separator }}simple-crosshair-overlay${{ matrix.target.artifact-suffix }}${{ matrix.target.suffix }}" 125 | shell: bash 126 | - name: Upload release artifact 127 | uses: softprops/action-gh-release@v2.3.4 128 | if: startsWith(github.ref, 'refs/tags/') # only run for pushes to tags 129 | with: 130 | draft: true 131 | files: ${{ runner.temp }}${{ matrix.target.path-separator }}simple-crosshair-overlay${{ matrix.target.artifact-suffix }}${{ matrix.target.suffix }} 132 | fail_on_unmatched_files: true 133 | -------------------------------------------------------------------------------- /src/tray.rs: -------------------------------------------------------------------------------- 1 | // This file is part of simple-crosshair-overlay and is licenced under the GNU GPL v3.0. 2 | // See LICENSE file for full text. 3 | // Copyright © 2023-2024 Michael Ripley 4 | 5 | #[cfg(target_os = "linux")] 6 | use debug_print::debug_println; 7 | use tray_icon::menu::{CheckMenuItem, IsMenuItem, MenuItem, Result as MenuResult, Submenu}; 8 | use tray_icon::{TrayIcon, TrayIconBuilder, menu::Menu}; 9 | 10 | use crate::{ICON_TOOLTIP, build_constants}; 11 | 12 | pub fn build_tray_icon() -> (MenuItems, TrayIcon) { 13 | // on linux we have to do this in a completely different way 14 | #[cfg(not(target_os = "linux"))] 15 | let tray_menu = Menu::new(); 16 | 17 | let menu_items = MenuItems::default(); 18 | 19 | // windows: do not use a submenu 20 | #[cfg(target_os = "windows")] 21 | { 22 | menu_items.add_to_menu(&tray_menu); 23 | } 24 | 25 | // mac: there are special submenu requirements 26 | #[cfg(target_os = "macos")] 27 | { 28 | // on mac all menu items must be in a submenu, so just make one with no name. Hope that doesn't cause problems... 29 | let submenu = tray_icon::menu::Submenu::new("", true); 30 | tray_menu.append(&submenu).unwrap(); 31 | menu_items.add_to_menu(&submenu); 32 | } 33 | 34 | // on Linux this MUST be called on the GTK thread, so we have to do some weird hijinks to pass things around 35 | #[cfg(not(target_os = "linux"))] 36 | let tray_icon: TrayIcon = { 37 | let tray_icon_builder = TrayIconBuilder::new() 38 | .with_menu(Box::new(tray_menu)) 39 | .with_tooltip(ICON_TOOLTIP) 40 | .with_icon(get_icon()); 41 | tray_icon_builder.build().unwrap() 42 | }; 43 | 44 | #[cfg(target_os = "linux")] 45 | { 46 | use std::sync::{Arc, Condvar, Mutex}; 47 | use std::time::Duration; 48 | 49 | let condvar_pair = Arc::new((Mutex::new(false), Condvar::new())); 50 | 51 | // start GTK background thread 52 | let condvar_pair_clone = condvar_pair.clone(); 53 | std::thread::Builder::new() 54 | .name("gtk-main".to_string()) 55 | .spawn(move || { 56 | debug_println!("starting GTK background thread"); 57 | gtk::init().unwrap(); 58 | debug_println!("GTK init complete"); 59 | 60 | // initialize the tray icon 61 | let tray_menu = Menu::new(); 62 | menu_items.add_to_menu(&tray_menu); 63 | 64 | let tray_icon_builder = TrayIconBuilder::new() 65 | .with_menu(Box::new(tray_menu)) 66 | .with_tooltip(ICON_TOOLTIP) 67 | .with_icon(get_icon()); 68 | let mut tray_icon = Some(tray_icon_builder.build().unwrap()); 69 | 70 | // signal that GTK init is complete 71 | { 72 | let (lock, condvar) = &*condvar_pair_clone; 73 | let mut gtk_started = lock.lock().unwrap(); 74 | *gtk_started = true; 75 | condvar.notify_one(); 76 | } // this block is actually necessary so that the lock gets released! 77 | 78 | debug_println!("GTK init signal sent. Starting GTK main loop."); 79 | loop { 80 | gtk::main_iteration_do(false); 81 | //TODO: channel MenuItem state around? 82 | std::thread::yield_now(); 83 | } 84 | debug_println!("GTK main loop returned!? Weird."); 85 | }) 86 | .unwrap(); 87 | debug_println!("spawned GTK background thread"); 88 | 89 | // wait for GTK to init 90 | let (lock, condvar) = &*condvar_pair; 91 | let gtk_started = lock.lock().unwrap(); 92 | debug_println!("acquired GTK lock"); 93 | if !*gtk_started { 94 | debug_println!("waiting for GTK init signal"); 95 | let (gtk_started, timeout_result) = condvar.wait_timeout(gtk_started, Duration::from_secs(5)).unwrap(); 96 | if !*gtk_started { 97 | panic!("GTK startup timed out = {}", timeout_result.timed_out()); 98 | } 99 | } 100 | 101 | debug_println!("GTK startup complete"); 102 | } 103 | 104 | (menu_items, tray_icon) 105 | } 106 | 107 | /// Load a tray icon graphic. 108 | fn get_icon() -> tray_icon::Icon { 109 | // simply grab the static byte array that's embedded in the application, which was generated in build.rs 110 | tray_icon::Icon::from_rgba( 111 | include_bytes!(env!("TRAY_ICON_PATH")).to_vec(), 112 | build_constants::TRAY_ICON_DIMENSION, 113 | build_constants::TRAY_ICON_DIMENSION, 114 | ) 115 | .unwrap() 116 | } 117 | 118 | /// Contains the menu items in our tray menu 119 | #[derive(Clone)] 120 | pub struct MenuItems { 121 | pub visible_button: CheckMenuItem, 122 | pub adjust_button: CheckMenuItem, 123 | pub color_pick_button: CheckMenuItem, 124 | pub image_pick_button: MenuItem, 125 | pub reset_button: MenuItem, 126 | pub about_button: MenuItem, 127 | pub exit_button: MenuItem, 128 | } 129 | 130 | impl Default for MenuItems { 131 | fn default() -> Self { 132 | let visible_button = CheckMenuItem::new("Visible", true, true, None); 133 | let adjust_button = CheckMenuItem::new("Adjust", true, false, None); 134 | let color_pick_button = CheckMenuItem::new("Pick Color", true, false, None); 135 | let image_pick_button = MenuItem::new("Load Image", true, None); 136 | let reset_button = MenuItem::new("Reset Overlay", true, None); 137 | let about_button = MenuItem::new("About", true, None); 138 | let exit_button = MenuItem::new("Exit", true, None); 139 | 140 | MenuItems { 141 | visible_button, 142 | adjust_button, 143 | color_pick_button, 144 | image_pick_button, 145 | reset_button, 146 | about_button, 147 | exit_button, 148 | } 149 | } 150 | } 151 | 152 | impl MenuItems { 153 | /// Append all the menu items into the provided `menu`. 154 | fn add_to_menu(&self, menu: &T) 155 | where 156 | T: AppendableMenu, 157 | { 158 | menu.append(&self.visible_button).unwrap(); 159 | menu.append(&self.adjust_button).unwrap(); 160 | menu.append(&self.color_pick_button).unwrap(); 161 | menu.append(&self.image_pick_button).unwrap(); 162 | menu.append(&self.reset_button).unwrap(); 163 | menu.append(&self.about_button).unwrap(); 164 | menu.append(&self.exit_button).unwrap(); 165 | } 166 | } 167 | 168 | /// Surprisingly tray-icon doesn't provide a trait for the Menu.append() behavior several structs 169 | /// have, so I have to build it myself for the structs I'm actually using. 170 | trait AppendableMenu { 171 | /// Add a menu item to the end of this menu. 172 | fn append(&self, item: &dyn IsMenuItem) -> MenuResult<()>; 173 | } 174 | 175 | impl AppendableMenu for Menu { 176 | fn append(&self, item: &dyn IsMenuItem) -> MenuResult<()> { 177 | self.append(item) 178 | } 179 | } 180 | 181 | impl AppendableMenu for Submenu { 182 | fn append(&self, item: &dyn IsMenuItem) -> MenuResult<()> { 183 | self.append(item) 184 | } 185 | } 186 | -------------------------------------------------------------------------------- /src-lib/private/settings.rs: -------------------------------------------------------------------------------- 1 | // This file is part of simple-crosshair-overlay and is licenced under the GNU GPL v3.0. 2 | // See LICENSE file for full text. 3 | // Copyright © 2023 Michael Ripley 4 | 5 | //! Relating to the settings file loaded on app start and persisted on app close 6 | 7 | use std::path::{Path, PathBuf}; 8 | use std::time::Duration; 9 | use std::{fs, io}; 10 | 11 | use debug_print::debug_println; 12 | use lazy_static::lazy_static; 13 | use serde::{Deserialize, Serialize}; 14 | use winit::dpi::{PhysicalPosition, PhysicalSize}; 15 | use winit::window::Window; 16 | 17 | use crate::private::hotkey::KeyBindings; 18 | use crate::private::util::dialog::show_warning; 19 | use crate::private::util::image::{self, Image}; 20 | use crate::private::util::numeric::fps_to_tick_interval; 21 | 22 | const DEFAULT_OFFSET_X: i32 = 0; 23 | const DEFAULT_OFFSET_Y: i32 = 0; 24 | const DEFAULT_SIZE: u32 = 16; 25 | const DEFAULT_FPS: u32 = 60; 26 | const DEFAULT_MONITOR_INDEX: usize = 0; 27 | const DEFAULT_MONITOR: u32 = (DEFAULT_MONITOR_INDEX as u32) + 1; 28 | const DEFAULT_COLOR: u32 = 0xB2FF0000; // 70% alpha red; 29 | 30 | // needed for serde, as it can't read constants directly 31 | const fn default_fps() -> u32 { 32 | DEFAULT_FPS 33 | } 34 | 35 | const fn default_monitor() -> u32 { 36 | DEFAULT_MONITOR 37 | } 38 | 39 | lazy_static! { 40 | pub static ref CONFIG_PATH: PathBuf = directories::ProjectDirs::from("dev.zkxs", "", "simple-crosshair-overlay") 41 | .unwrap() 42 | .config_dir() 43 | .join("config.toml"); 44 | } 45 | 46 | /// The actual persisted settings struct 47 | #[derive(Deserialize, Serialize)] 48 | pub struct PersistedSettings { 49 | pub window_dx: i32, 50 | pub window_dy: i32, 51 | pub window_width: u32, 52 | pub window_height: u32, 53 | #[serde(with = "crate::private::util::custom_serializer::argb_color")] 54 | color: u32, 55 | #[serde(default = "default_fps")] 56 | fps: u32, 57 | image_path: Option, 58 | #[serde(default)] 59 | pub key_bindings: KeyBindings, 60 | /// 1-indexed monitor to render the overlay to 61 | #[serde(default = "default_monitor")] 62 | monitor: u32, 63 | } 64 | 65 | impl PersistedSettings { 66 | fn load(self) -> Settings { 67 | let color = image::premultiply_alpha(self.color); 68 | 69 | // make sure that if the user manually put an empty string in their config we don't explode 70 | let filtered_image_path = self.image_path.as_ref().filter(|path| !path.as_os_str().is_empty()); 71 | 72 | let image = if let Some(image_path) = filtered_image_path { 73 | match image::load_png(image_path.as_path()) { 74 | Ok(image) => Some(image), 75 | Err(e) => { 76 | show_warning(format!( 77 | "Failed loading saved image_path \"{}\".\n\n{}", 78 | image_path.display(), 79 | e 80 | )); 81 | None 82 | } 83 | } 84 | } else { 85 | None 86 | }; 87 | 88 | let tick_interval = fps_to_tick_interval(self.fps); 89 | let monitor_index = usize::try_from(self.monitor.checked_sub(1).unwrap()).unwrap(); 90 | let render_mode = RenderMode::from(&image); 91 | 92 | Settings { 93 | persisted: self, 94 | color, 95 | image, 96 | tick_interval, 97 | monitor_index, 98 | desired_window_position: PhysicalPosition::default(), 99 | desired_window_size: PhysicalSize::default(), 100 | render_mode, 101 | } 102 | } 103 | } 104 | 105 | impl Default for PersistedSettings { 106 | fn default() -> Self { 107 | PersistedSettings { 108 | window_dx: DEFAULT_OFFSET_X, 109 | window_dy: DEFAULT_OFFSET_Y, 110 | window_width: DEFAULT_SIZE, 111 | window_height: DEFAULT_SIZE, 112 | color: DEFAULT_COLOR, 113 | fps: DEFAULT_FPS, 114 | image_path: None, 115 | key_bindings: KeyBindings::default(), 116 | monitor: DEFAULT_MONITOR, 117 | } 118 | } 119 | } 120 | 121 | /// A wrapper around the persisted settings providing additional derived values 122 | pub struct Settings { 123 | pub persisted: PersistedSettings, 124 | pub color: u32, 125 | image: Option>, 126 | pub tick_interval: Duration, 127 | /// 0-indexed monitor to render the overlay to 128 | pub monitor_index: usize, 129 | pub desired_window_position: PhysicalPosition, 130 | pub desired_window_size: PhysicalSize, 131 | pub render_mode: RenderMode, 132 | } 133 | 134 | impl Settings { 135 | pub fn size(&self) -> PhysicalSize { 136 | match self.render_mode { 137 | RenderMode::Image => { 138 | let image = self.image.as_ref().unwrap(); 139 | PhysicalSize::new(image.width, image.height) 140 | } 141 | RenderMode::Crosshair => PhysicalSize::new(self.persisted.window_width, self.persisted.window_height), 142 | RenderMode::ColorPicker => { 143 | PhysicalSize::new(image::COLOR_PICKER_SIZE as u32, image::COLOR_PICKER_SIZE as u32) 144 | } 145 | } 146 | } 147 | 148 | pub fn image(&self) -> Option<&Image> { 149 | self.image.as_ref().map(|b| b.as_ref()) 150 | } 151 | 152 | /// Toggle color picker mode on or off. Returns `true` if color picker mode is now enabled, `false` otherwise. 153 | pub fn toggle_pick_color(&mut self) -> bool { 154 | let (render_mode, enabled) = if self.render_mode == RenderMode::ColorPicker { 155 | (RenderMode::from(&self.image), false) 156 | } else { 157 | (RenderMode::ColorPicker, true) 158 | }; 159 | self.render_mode = render_mode; 160 | enabled 161 | } 162 | 163 | pub fn set_pick_color(&mut self, pick_color: bool) { 164 | self.render_mode = if pick_color { 165 | RenderMode::ColorPicker 166 | } else { 167 | RenderMode::from(&self.image) 168 | } 169 | } 170 | 171 | /// Returns `true` if color picker mode is now enabled, `false` otherwise. 172 | pub fn get_pick_color(&self) -> bool { 173 | self.render_mode == RenderMode::ColorPicker 174 | } 175 | 176 | /// Set the color of the generated crosshair. The provided `color` must not have premultiplied alpha (yet) 177 | pub fn set_color(&mut self, color: u32) { 178 | debug_println!("set color to {color:08X}"); 179 | self.persisted.color = color; 180 | self.color = image::premultiply_alpha(color); 181 | self.image = None; // unload image 182 | self.persisted.image_path = None; 183 | self.render_mode = RenderMode::Crosshair; 184 | } 185 | 186 | pub fn is_scalable(&self) -> bool { 187 | self.image.is_none() 188 | } 189 | 190 | /// only reset the settings the user can actually edit in-app. If they've manually edited "secret settings" in their config that should stick. 191 | pub fn reset(&mut self) { 192 | self.persisted.window_dx = DEFAULT_OFFSET_X; 193 | self.persisted.window_dy = DEFAULT_OFFSET_Y; 194 | self.persisted.window_width = DEFAULT_SIZE; 195 | self.persisted.window_height = DEFAULT_SIZE; 196 | self.persisted.color = DEFAULT_COLOR; 197 | self.color = image::premultiply_alpha(DEFAULT_COLOR); 198 | self.persisted.image_path = None; 199 | if self.render_mode == RenderMode::Image { 200 | self.render_mode = RenderMode::Crosshair; 201 | } 202 | self.image = None; 203 | } 204 | 205 | /// load a new PNG at runtime 206 | pub fn load_png(&mut self, path: PathBuf) -> io::Result<()> { 207 | let image = image::load_png(path.as_path())?; 208 | self.persisted.image_path = Some(path); 209 | self.image = Some(image); 210 | self.render_mode = RenderMode::Image; 211 | Ok(()) 212 | } 213 | 214 | pub fn load() -> io::Result { 215 | fs::create_dir_all(CONFIG_PATH.as_path().parent().unwrap())?; 216 | Settings::load_from_path(CONFIG_PATH.as_path()) 217 | } 218 | 219 | #[inline(always)] 220 | fn load_from_path(path: T) -> io::Result 221 | where 222 | T: AsRef, 223 | { 224 | fs::read_to_string(path) 225 | .and_then(|string| { 226 | toml::from_str::(&string).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)) 227 | }) 228 | .map(|settings| settings.load()) 229 | } 230 | 231 | pub fn save(&self) -> Result<(), String> { 232 | self.save_to_path(CONFIG_PATH.as_path()) 233 | } 234 | 235 | #[inline(always)] 236 | fn save_to_path(&self, path: T) -> Result<(), String> 237 | where 238 | T: AsRef, 239 | { 240 | let serialized_config = toml::to_string(&self.persisted).expect("failed to serialize settings"); 241 | fs::write(path, serialized_config).map_err(|e| format!("{e:?}")) 242 | } 243 | 244 | pub fn set_window_position(&mut self, window: &Window) { 245 | let position = self.compute_window_coordinates(window); 246 | self.desired_window_position = position; 247 | window.set_outer_position(position); 248 | } 249 | 250 | fn reset_window_position(&self, window: &Window) { 251 | window.set_outer_position(self.desired_window_position); 252 | } 253 | 254 | pub fn validate_window_position(&self, window: &Window, position: PhysicalPosition) { 255 | if position != self.desired_window_position { 256 | debug_println!("resetting window position"); 257 | self.reset_window_position(window); 258 | } 259 | } 260 | 261 | pub fn set_window_size(&self, window: &Window) { 262 | let _ = window.request_inner_size(self.size()); 263 | } 264 | 265 | pub fn validate_window_size(&self, window: &Window, size: PhysicalSize) { 266 | if size != self.size() { 267 | debug_println!("resetting window size"); 268 | self.set_window_size(window); 269 | } 270 | } 271 | 272 | /// Compute the correct coordinates of the top-left of the window in order to center the crosshair in the selected monitor 273 | fn compute_window_coordinates(&self, window: &Window) -> PhysicalPosition { 274 | // fall back to primary monitor if the desired monitor index is invalid 275 | let monitor = window 276 | .available_monitors() 277 | .nth(self.monitor_index) 278 | .unwrap_or_else(|| window.primary_monitor().unwrap()); 279 | 280 | // grab a bunch of coordinates/sizes and convert them to i32s, as we have some signed math to do 281 | let PhysicalPosition { 282 | x: monitor_x, 283 | y: monitor_y, 284 | } = monitor.position(); 285 | let PhysicalSize { 286 | width: monitor_width, 287 | height: monitor_height, 288 | } = monitor.size(); 289 | let monitor_width = i32::try_from(monitor_width).unwrap(); 290 | let monitor_height = i32::try_from(monitor_height).unwrap(); 291 | let PhysicalSize { 292 | width: window_width, 293 | height: window_height, 294 | } = self.size(); 295 | let window_width = i32::try_from(window_width).unwrap(); 296 | let window_height = i32::try_from(window_height).unwrap(); 297 | 298 | // calculate the coordinates of the center of the monitor, rounding down 299 | let (monitor_center_x, monitor_center_y) = 300 | image::rectangle_center(monitor_x, monitor_y, monitor_width, monitor_height); 301 | 302 | // adjust by half our window size, as we want the coordinates at which to place the top-left corner of the window 303 | let window_x = monitor_center_x - (window_width / 2) + self.persisted.window_dx; 304 | let window_y = monitor_center_y - (window_height / 2) + self.persisted.window_dy; 305 | 306 | debug_println!("placing window at {}, {}", window_x, window_y); 307 | PhysicalPosition::new(window_x, window_y) 308 | } 309 | 310 | pub fn set_monitor(&mut self, monitor_index: usize) { 311 | self.monitor_index = monitor_index; 312 | self.persisted.monitor = (monitor_index as u32) + 1; 313 | } 314 | } 315 | 316 | impl Default for Settings { 317 | fn default() -> Self { 318 | let savable = PersistedSettings::default(); 319 | let color = image::premultiply_alpha(savable.color); 320 | Settings { 321 | persisted: savable, 322 | color, 323 | image: None, 324 | tick_interval: fps_to_tick_interval(DEFAULT_FPS), 325 | monitor_index: DEFAULT_MONITOR_INDEX, 326 | desired_window_position: PhysicalPosition::default(), 327 | desired_window_size: PhysicalSize::default(), 328 | render_mode: RenderMode::Crosshair, 329 | } 330 | } 331 | } 332 | 333 | #[derive(Eq, PartialEq)] 334 | pub enum RenderMode { 335 | Image, 336 | Crosshair, 337 | ColorPicker, 338 | } 339 | 340 | impl From<&Option> for RenderMode 341 | where 342 | T: AsRef, 343 | { 344 | fn from(value: &Option) -> Self { 345 | if value.is_some() { 346 | RenderMode::Image 347 | } else { 348 | RenderMode::Crosshair 349 | } 350 | } 351 | } 352 | 353 | #[cfg(test)] 354 | mod test_config_load { 355 | use super::*; 356 | 357 | /// typical config 358 | #[test] 359 | fn test_load_settings() { 360 | Settings::load_from_path("tests/resources/test_config.toml").unwrap(); 361 | } 362 | 363 | /// config with an image set 364 | #[test] 365 | fn test_load_settings_with_image() { 366 | Settings::load_from_path("tests/resources/test_config_image.toml").unwrap(); 367 | } 368 | 369 | /// config with minimum possible values set 370 | #[test] 371 | fn test_load_settings_old() { 372 | Settings::load_from_path("tests/resources/test_config_old.toml").unwrap(); 373 | } 374 | 375 | /// load a PNG into a config 376 | #[test] 377 | fn test_load_png() { 378 | let mut settings = Settings::load_from_path("tests/resources/test_config.toml").unwrap(); 379 | settings.load_png("tests/resources/test.png".into()).unwrap(); 380 | } 381 | 382 | /// save config to disk 383 | #[test] 384 | fn test_save_config() { 385 | let settings = Settings::load_from_path("tests/resources/test_config.toml").unwrap(); 386 | 387 | let mut path = std::env::temp_dir(); 388 | path.push("DELETEME_simple-crosshair-overlay-test-config.toml"); 389 | 390 | settings.save_to_path(&path).expect("save failed"); 391 | fs::remove_file(&path).expect("cleanup failed"); 392 | } 393 | } 394 | -------------------------------------------------------------------------------- /deny.toml: -------------------------------------------------------------------------------- 1 | # This template contains all of the possible sections and their default values 2 | 3 | # Note that all fields that take a lint level have these possible values: 4 | # * deny - An error will be produced and the check will fail 5 | # * warn - A warning will be produced, but the check will not fail 6 | # * allow - No warning or error will be produced, though in some cases a note 7 | # will be 8 | 9 | # The values provided in this template are the default values that will be used 10 | # when any section or field is not specified in your own configuration 11 | 12 | # Root options 13 | 14 | # The graph table configures how the dependency graph is constructed and thus 15 | # which crates the checks are performed against 16 | [graph] 17 | # If 1 or more target triples (and optionally, target_features) are specified, 18 | # only the specified targets will be checked when running `cargo deny check`. 19 | # This means, if a particular package is only ever used as a target specific 20 | # dependency, such as, for example, the `nix` crate only being used via the 21 | # `target_family = "unix"` configuration, that only having windows targets in 22 | # this list would mean the nix crate, as well as any of its exclusive 23 | # dependencies not shared by any other crates, would be ignored, as the target 24 | # list here is effectively saying which targets you are building for. 25 | targets = [ 26 | # The triple can be any string, but only the target triples built in to 27 | # rustc (as of 1.40) can be checked against actual config expressions 28 | #"x86_64-unknown-linux-musl", 29 | # You can also specify which target_features you promise are enabled for a 30 | # particular target. target_features are currently not validated against 31 | # the actual valid features supported by the target architecture. 32 | #{ triple = "wasm32-unknown-unknown", features = ["atomics"] }, 33 | ] 34 | # When creating the dependency graph used as the source of truth when checks are 35 | # executed, this field can be used to prune crates from the graph, removing them 36 | # from the view of cargo-deny. This is an extremely heavy hammer, as if a crate 37 | # is pruned from the graph, all of its dependencies will also be pruned unless 38 | # they are connected to another crate in the graph that hasn't been pruned, 39 | # so it should be used with care. The identifiers are [Package ID Specifications] 40 | # (https://doc.rust-lang.org/cargo/reference/pkgid-spec.html) 41 | #exclude = [] 42 | # If true, metadata will be collected with `--all-features`. Note that this can't 43 | # be toggled off if true, if you want to conditionally enable `--all-features` it 44 | # is recommended to pass `--all-features` on the cmd line instead 45 | all-features = false 46 | # If true, metadata will be collected with `--no-default-features`. The same 47 | # caveat with `all-features` applies 48 | no-default-features = false 49 | # If set, these feature will be enabled when collecting metadata. If `--features` 50 | # is specified on the cmd line they will take precedence over this option. 51 | #features = [] 52 | 53 | # The output table provides options for how/if diagnostics are outputted 54 | [output] 55 | # When outputting inclusion graphs in diagnostics that include features, this 56 | # option can be used to specify the depth at which feature edges will be added. 57 | # This option is included since the graphs can be quite large and the addition 58 | # of features from the crate(s) to all of the graph roots can be far too verbose. 59 | # This option can be overridden via `--feature-depth` on the cmd line 60 | feature-depth = 1 61 | 62 | # This section is considered when running `cargo deny check advisories` 63 | # More documentation for the advisories section can be found here: 64 | # https://embarkstudios.github.io/cargo-deny/checks/advisories/cfg.html 65 | [advisories] 66 | # The path where the advisory databases are cloned/fetched into 67 | #db-path = "$CARGO_HOME/advisory-dbs" 68 | # The url(s) of the advisory databases to use 69 | #db-urls = ["https://github.com/rustsec/advisory-db"] 70 | # A list of advisory IDs to ignore. Note that ignored advisories will still 71 | # output a note when they are encountered. 72 | ignore = [ 73 | #"RUSTSEC-0000-0000", 74 | #{ id = "RUSTSEC-0000-0000", reason = "you can specify a reason the advisory is ignored" }, 75 | { id = "RUSTSEC-2024-0413", reason = "atk: The gtk-rs GTK3 bindings are no longer maintained. tray-icon needs to migrate to GTK4 to resolve this." }, 76 | { id = "RUSTSEC-2024-0416", reason = "atk-sys: The gtk-rs GTK3 bindings are no longer maintained. tray-icon needs to migrate to GTK4 to resolve this." }, 77 | { id = "RUSTSEC-2024-0412", reason = "gdk: The gtk-rs GTK3 bindings are no longer maintained. tray-icon needs to migrate to GTK4 to resolve this." }, 78 | { id = "RUSTSEC-2024-0418", reason = "gdk-sys: The gtk-rs GTK3 bindings are no longer maintained. tray-icon needs to migrate to GTK4 to resolve this." }, 79 | { id = "RUSTSEC-2024-0415", reason = "gtk: The gtk-rs GTK3 bindings are no longer maintained. tray-icon needs to migrate to GTK4 to resolve this." }, 80 | { id = "RUSTSEC-2024-0420", reason = "gtk-sys: The gtk-rs GTK3 bindings are no longer maintained. tray-icon needs to migrate to GTK4 to resolve this." }, 81 | { id = "RUSTSEC-2024-0419", reason = "gtk-macros: The gtk-rs GTK3 bindings are no longer maintained. tray-icon needs to migrate to GTK4 to resolve this." }, 82 | { id = "RUSTSEC-2024-0429", reason = "glib: Unsoundness in `Iterator` and `DoubleEndedIterator` impls for `glib::VariantStrIter`. tray-icon needs to migrate to GTK4 to resolve this. This is not an impactful security issue, as simple-crosshair-overlay does not operate on untrusted input." }, 83 | { id = "RUSTSEC-2024-0370", reason = "proc-macro-error is unmaintained. tray-icon needs to migrate to GTK4 to resolve this." }, 84 | #"a-crate-that-is-yanked@0.1.1", # you can also ignore yanked crate versions if you wish 85 | #{ crate = "a-crate-that-is-yanked@0.1.1", reason = "you can specify why you are ignoring the yanked crate" }, 86 | ] 87 | # If this is true, then cargo deny will use the git executable to fetch advisory database. 88 | # If this is false, then it uses a built-in git library. 89 | # Setting this to true can be helpful if you have special authentication requirements that cargo-deny does not support. 90 | # See Git Authentication for more information about setting up git authentication. 91 | #git-fetch-with-cli = true 92 | 93 | # This section is considered when running `cargo deny check licenses` 94 | # More documentation for the licenses section can be found here: 95 | # https://embarkstudios.github.io/cargo-deny/checks/licenses/cfg.html 96 | [licenses] 97 | # List of explicitly allowed licenses 98 | # See https://spdx.org/licenses/ for list of possible licenses 99 | # [possible values: any SPDX 3.11 short identifier (+ optional exception)]. 100 | allow = [ 101 | # Bog standard stuff 102 | "Apache-2.0", 103 | "BSD-2-Clause", 104 | "BSD-3-Clause", 105 | "GPL-3.0-only", 106 | "ISC", 107 | "MIT", 108 | "Zlib", 109 | # Weirder stuff 110 | "Apache-2.0 WITH LLVM-exception", # Needed by target-lexicon 111 | "MPL-2.0", # OSI and FSF approved. Needed by option-ext 112 | "Unicode-3.0", # OSI but not FSF approved. Needed by unicode-ident. 113 | ] 114 | # The confidence threshold for detecting a license from license text. 115 | # The higher the value, the more closely the license text must be to the 116 | # canonical license text of a valid SPDX license file. 117 | # [possible values: any between 0.0 and 1.0]. 118 | confidence-threshold = 0.8 119 | # Allow 1 or more licenses on a per-crate basis, so that particular licenses 120 | # aren't accepted for every possible crate as with the normal allow list 121 | exceptions = [ 122 | # Each entry is the crate and version constraint, and its specific allow 123 | # list 124 | #{ allow = ["Zlib"], crate = "adler32" }, 125 | ] 126 | 127 | # Some crates don't have (easily) machine readable licensing information, 128 | # adding a clarification entry for it allows you to manually specify the 129 | # licensing information 130 | #[[licenses.clarify]] 131 | # The package spec the clarification applies to 132 | #crate = "ring" 133 | # The SPDX expression for the license requirements of the crate 134 | #expression = "MIT AND ISC AND OpenSSL" 135 | # One or more files in the crate's source used as the "source of truth" for 136 | # the license expression. If the contents match, the clarification will be used 137 | # when running the license check, otherwise the clarification will be ignored 138 | # and the crate will be checked normally, which may produce warnings or errors 139 | # depending on the rest of your configuration 140 | #license-files = [ 141 | # Each entry is a crate relative path, and the (opaque) hash of its contents 142 | #{ path = "LICENSE", hash = 0xbd0eed23 } 143 | #] 144 | 145 | [licenses.private] 146 | # If true, ignores workspace crates that aren't published, or are only 147 | # published to private registries. 148 | # To see how to mark a crate as unpublished (to the official registry), 149 | # visit https://doc.rust-lang.org/cargo/reference/manifest.html#the-publish-field. 150 | ignore = false 151 | # One or more private registries that you might publish crates to, if a crate 152 | # is only published to private registries, and ignore is true, the crate will 153 | # not have its license(s) checked 154 | registries = [ 155 | #"https://sekretz.com/registry 156 | ] 157 | 158 | # This section is considered when running `cargo deny check bans`. 159 | # More documentation about the 'bans' section can be found here: 160 | # https://embarkstudios.github.io/cargo-deny/checks/bans/cfg.html 161 | [bans] 162 | # Lint level for when multiple versions of the same crate are detected 163 | multiple-versions = "warn" 164 | # Lint level for when a crate version requirement is `*` 165 | wildcards = "allow" 166 | # The graph highlighting used when creating dotgraphs for crates 167 | # with multiple versions 168 | # * lowest-version - The path to the lowest versioned duplicate is highlighted 169 | # * simplest-path - The path to the version with the fewest edges is highlighted 170 | # * all - Both lowest-version and simplest-path are used 171 | highlight = "all" 172 | # The default lint level for `default` features for crates that are members of 173 | # the workspace that is being checked. This can be overridden by allowing/denying 174 | # `default` on a crate-by-crate basis if desired. 175 | workspace-default-features = "allow" 176 | # The default lint level for `default` features for external crates that are not 177 | # members of the workspace. This can be overridden by allowing/denying `default` 178 | # on a crate-by-crate basis if desired. 179 | external-default-features = "allow" 180 | # List of crates that are allowed. Use with care! 181 | allow = [ 182 | #"ansi_term@0.11.0", 183 | #{ crate = "ansi_term@0.11.0", reason = "you can specify a reason it is allowed" }, 184 | ] 185 | # List of crates to deny 186 | deny = [ 187 | #"ansi_term@0.11.0", 188 | #{ crate = "ansi_term@0.11.0", reason = "you can specify a reason it is banned" }, 189 | # Wrapper crates can optionally be specified to allow the crate when it 190 | # is a direct dependency of the otherwise banned crate 191 | #{ crate = "ansi_term@0.11.0", wrappers = ["this-crate-directly-depends-on-ansi_term"] }, 192 | ] 193 | 194 | # List of features to allow/deny 195 | # Each entry the name of a crate and a version range. If version is 196 | # not specified, all versions will be matched. 197 | #[[bans.features]] 198 | #crate = "reqwest" 199 | # Features to not allow 200 | #deny = ["json"] 201 | # Features to allow 202 | #allow = [ 203 | # "rustls", 204 | # "__rustls", 205 | # "__tls", 206 | # "hyper-rustls", 207 | # "rustls", 208 | # "rustls-pemfile", 209 | # "rustls-tls-webpki-roots", 210 | # "tokio-rustls", 211 | # "webpki-roots", 212 | #] 213 | # If true, the allowed features must exactly match the enabled feature set. If 214 | # this is set there is no point setting `deny` 215 | #exact = true 216 | 217 | # Certain crates/versions that will be skipped when doing duplicate detection. 218 | skip = [ 219 | #"ansi_term@0.11.0", 220 | #{ crate = "ansi_term@0.11.0", reason = "you can specify a reason why it can't be updated/removed" }, 221 | 222 | { crate = "core-foundation@0.9.4", reason = "Wrapper lib. winit & native-dialog use old version" }, 223 | { crate = "core-graphics@0.23.2", reason = "Wrapper lib. winit & native-dialog use old version" }, 224 | { crate = "core-graphics-types@0.1.3", reason = "Wrapper lib. winit & native-dialog use old version" }, 225 | { crate = "heck@0.4.1", reason = "Case conversion lib. gtk uses old version" }, 226 | { crate = "libloading@0.7.4", reason = "Wrapper lib. tray-icon uses old version" }, 227 | { crate = "linux-raw-sys@0.4", reason = "Wrapper lib. winit uses old version" }, 228 | { crate = "linux-raw-sys@0.6", reason = "Wrapper lib. softbuffer uses old version" }, 229 | { crate = "redox_syscall@0.4.1", reason = "Wrapper lib. winit uses old version" }, 230 | { crate = "thiserror@1.0.69", reason = "A derive macro for Error. A LOT of stuff uses old version" }, 231 | { crate = "thiserror-impl@1.0.69", reason = "A derive macro for Error. A LOT of stuff uses old version" }, 232 | { crate = "getrandom@0.2", reason = "used by dirs" }, 233 | { crate = "rustix@0.38", reason = "used by winit, softbuffer, native-dialog" }, 234 | ] 235 | # Similarly to `skip` allows you to skip certain crates during duplicate 236 | # detection. Unlike skip, it also includes the entire tree of transitive 237 | # dependencies starting at the specified crate, up to a certain depth, which is 238 | # by default infinite. 239 | skip-tree = [ 240 | #"ansi_term@0.11.0", # will be skipped along with _all_ of its direct and transitive dependencies 241 | #{ crate = "ansi_term@0.11.0", depth = 20 }, 242 | 243 | # Windows bindings have giant obnoxious trees of custom dependencies 244 | { crate = "windows-sys@0.45", reason = "used by winit" }, 245 | { crate = "windows-sys@0.52", reason = "used by winit" }, 246 | { crate = "windows-sys@0.59", reason = "used by winit, native-dialog" }, 247 | { crate = "windows-sys@0.60", reason = "used by tray-icon" }, 248 | { crate = "windows-targets@0.48", reason = "used by winit, softbuffer" }, 249 | 250 | # block2/objc2 also has a big tree of custom deps 251 | { crate = "block2@0.5", reason = "used by winit" }, 252 | { crate = "objc2-app-kit@0.2", reason = "used by winit" }, 253 | { crate = "objc2-quartz-core@0.2", reason = "used by winit" }, 254 | 255 | # Allow build-only dependencies. Who cares if they're duped? 256 | { crate = "winres" }, 257 | { crate = "ico" }, 258 | 259 | # Allow build-time-eval'd macros 260 | { crate = "proc-macro-crate" }, 261 | ] 262 | 263 | # This section is considered when running `cargo deny check sources`. 264 | # More documentation about the 'sources' section can be found here: 265 | # https://embarkstudios.github.io/cargo-deny/checks/sources/cfg.html 266 | [sources] 267 | # Lint level for what to happen when a crate from a crate registry that is not 268 | # in the allow list is encountered 269 | unknown-registry = "deny" 270 | # Lint level for what to happen when a crate from a git repository that is not 271 | # in the allow list is encountered 272 | unknown-git = "deny" 273 | # List of URLs for allowed crate registries. Defaults to the crates.io index 274 | # if not specified. If it is specified but empty, no registries are allowed. 275 | allow-registry = ["https://github.com/rust-lang/crates.io-index"] 276 | # List of URLs for allowed Git repositories 277 | allow-git = [] 278 | 279 | [sources.allow-org] 280 | # 1 or more github.com organizations to allow git sources for 281 | #github = [""] 282 | # 1 or more gitlab.com organizations to allow git sources for 283 | #gitlab = [""] 284 | # 1 or more bitbucket.org organizations to allow git sources for 285 | #bitbucket = [""] 286 | -------------------------------------------------------------------------------- /src-lib/private/hotkey/hotkey_manager.rs: -------------------------------------------------------------------------------- 1 | // This file is part of simple-crosshair-overlay and is licenced under the GNU GPL v3.0. 2 | // See LICENSE file for full text. 3 | // Copyright © 2023 Michael Ripley 4 | 5 | //! Hotkey input system. 6 | //! 7 | //! The idea here is to do as much work as possible up front once, thereby minimizing 8 | //! the hot part of it: polling the keyboard state and extracting what we care about. 9 | //! 10 | //! We care about if certain key combinations are pressed. To make this really fast, I make 11 | //! heavy use of bitmasks. 12 | 13 | use std::marker::PhantomData; 14 | 15 | use serde::{Deserialize, Serialize}; 16 | 17 | use crate::private::platform::{KeyboardState, KeycodeType}; 18 | 19 | use super::Keycode; 20 | 21 | /// the number of bits in this mask is the number of distinct keys that can be used across all keybinds 22 | type Bitmask = u32; 23 | type KeyBinding = Vec; 24 | 25 | // serde defaults for new keybinds 26 | fn default_cycle_monitor_keybind() -> KeyBinding { 27 | KeyBindings::default().cycle_monitor 28 | } 29 | 30 | fn default_toggle_color_picker_keybind() -> KeyBinding { 31 | KeyBindings::default().toggle_color_picker 32 | } 33 | 34 | /// format user can specify keybindings with 35 | #[derive(Serialize, Deserialize)] 36 | pub struct KeyBindings { 37 | up: KeyBinding, 38 | down: KeyBinding, 39 | left: KeyBinding, 40 | right: KeyBinding, 41 | #[serde(default = "default_cycle_monitor_keybind")] 42 | cycle_monitor: KeyBinding, 43 | scale_increase: KeyBinding, 44 | scale_decrease: KeyBinding, 45 | toggle_hidden: KeyBinding, 46 | toggle_adjust: KeyBinding, 47 | #[serde(default = "default_toggle_color_picker_keybind")] 48 | toggle_color_picker: KeyBinding, 49 | } 50 | 51 | impl Default for KeyBindings { 52 | fn default() -> Self { 53 | KeyBindings { 54 | up: vec![Keycode::Up], 55 | down: vec![Keycode::Down], 56 | left: vec![Keycode::Left], 57 | right: vec![Keycode::Right], 58 | cycle_monitor: vec![Keycode::LControl, Keycode::M], 59 | scale_increase: vec![Keycode::PageUp], 60 | scale_decrease: vec![Keycode::PageDown], 61 | toggle_hidden: vec![Keycode::LControl, Keycode::H], 62 | toggle_adjust: vec![Keycode::LControl, Keycode::J], 63 | toggle_color_picker: vec![Keycode::LControl, Keycode::K], 64 | } 65 | } 66 | } 67 | 68 | struct KeyBuffer 69 | where 70 | K: KeycodeType, 71 | { 72 | lookup_table: Vec, 73 | up_mask: Bitmask, 74 | down_mask: Bitmask, 75 | left_mask: Bitmask, 76 | right_mask: Bitmask, 77 | cycle_monitor_mask: Bitmask, 78 | scale_increase_mask: Bitmask, 79 | scale_decrease_mask: Bitmask, 80 | toggle_hidden_mask: Bitmask, 81 | toggle_adjust_mask: Bitmask, 82 | toggle_color_picker_mask: Bitmask, 83 | any_movement_mask: Bitmask, 84 | any_scale_mask: Bitmask, 85 | _keycode_type_marker: PhantomData, 86 | } 87 | 88 | impl KeyBuffer 89 | where 90 | K: KeycodeType, 91 | { 92 | fn new(key_bindings: &KeyBindings) -> Result, &'static str> { 93 | // build the lookup table and compute each hotkeys bitmask combination 94 | let mut bit = 1; 95 | let mut lookup_table = vec![0; K::num_variants()]; 96 | let up_mask = Self::update_key_buffer_values(&key_bindings.up, &mut bit, &mut lookup_table)?; 97 | let down_mask = Self::update_key_buffer_values(&key_bindings.down, &mut bit, &mut lookup_table)?; 98 | let left_mask = Self::update_key_buffer_values(&key_bindings.left, &mut bit, &mut lookup_table)?; 99 | let right_mask = Self::update_key_buffer_values(&key_bindings.right, &mut bit, &mut lookup_table)?; 100 | let cycle_monitor_mask = 101 | Self::update_key_buffer_values(&key_bindings.cycle_monitor, &mut bit, &mut lookup_table)?; 102 | let scale_increase_mask = 103 | Self::update_key_buffer_values(&key_bindings.scale_increase, &mut bit, &mut lookup_table)?; 104 | let scale_decrease_mask = 105 | Self::update_key_buffer_values(&key_bindings.scale_decrease, &mut bit, &mut lookup_table)?; 106 | let toggle_hidden_mask = 107 | Self::update_key_buffer_values(&key_bindings.toggle_hidden, &mut bit, &mut lookup_table)?; 108 | let toggle_adjust_mask = 109 | Self::update_key_buffer_values(&key_bindings.toggle_adjust, &mut bit, &mut lookup_table)?; 110 | let toggle_color_picker_mask = 111 | Self::update_key_buffer_values(&key_bindings.toggle_color_picker, &mut bit, &mut lookup_table)?; 112 | let any_movement_mask = up_mask | down_mask | left_mask | right_mask; 113 | let any_scale_mask = scale_increase_mask | scale_decrease_mask; 114 | 115 | Ok(KeyBuffer { 116 | lookup_table, 117 | up_mask, 118 | down_mask, 119 | left_mask, 120 | right_mask, 121 | cycle_monitor_mask, 122 | scale_increase_mask, 123 | scale_decrease_mask, 124 | toggle_hidden_mask, 125 | toggle_adjust_mask, 126 | toggle_color_picker_mask, 127 | any_movement_mask, 128 | any_scale_mask, 129 | _keycode_type_marker: Default::default(), 130 | }) 131 | } 132 | 133 | /// - `key_combination`: a set of keys to use for a specific hotkey action 134 | /// - `bit`: a bitmask with a single bit set which is used to represent a single key. For example, 135 | /// Ctrl might end up as 0b1. This bit is shifted for each distinct key we use. 136 | /// - `lookup_table`: a lookup table where each item is a key. A value of zero indicates no hotkey 137 | /// uses this key. A nonzero value indicates at least one hotkey uses this key. 138 | /// 139 | /// This function is called for each hotkey you want to register, and it returns bitmask 140 | /// representing which keys must be pressed for that hotkey. Each key used as part of the hotkey 141 | /// system is assigned a unique bit in this masking scheme. This means if a u32 is used as the 142 | /// bitmask type then only 32 distinct keys may be used across all hotkeys. 143 | fn update_key_buffer_values( 144 | key_combination: &[Keycode], 145 | bit: &mut Bitmask, 146 | lookup_table: &mut [Bitmask], 147 | ) -> Result { 148 | let mut mask: Bitmask = 0; 149 | for keycode in key_combination { 150 | let lookup_table_mask = &mut lookup_table[K::from(*keycode).index()]; 151 | if *lookup_table_mask == 0 { 152 | // if the previous shift overflowed the mask will be zero 153 | if *bit == 0 { 154 | return Err( 155 | "Only 32 distinct keys may be used for hotkeys at this time. Congratulations if you're seeing this, as I didn't think anyone would be crazy enough to use that many keys.", 156 | ); 157 | } 158 | 159 | // generate a new mask and add to the table 160 | *lookup_table_mask = *bit; 161 | *bit <<= 1; 162 | } 163 | mask |= *lookup_table_mask; 164 | } 165 | Ok(mask) 166 | } 167 | 168 | /// Get the bitmask that corresponds to this specific key. This returns a mask with a single bit 169 | /// set for keys used in any hotkey, and returns zero for keys not used in any hotkey. 170 | #[inline(always)] 171 | fn keycode_to_mask(&self, keycode: &K) -> Bitmask { 172 | self.lookup_table[keycode.index()] 173 | } 174 | 175 | /// Generate the bitmask that corresponds to the currently pressed key combination. 176 | fn update(&self, buf: &mut Bitmask, keys: &[K]) { 177 | *buf = 0; 178 | for keycode in keys { 179 | *buf |= self.keycode_to_mask(keycode); 180 | } 181 | } 182 | 183 | /// Check if the currently pressed keys contain the "up" key combination 184 | fn up(&self, buf: Bitmask) -> bool { 185 | buf & self.up_mask == self.up_mask 186 | } 187 | 188 | /// Check if the currently pressed keys contain the "down" key combination 189 | fn down(&self, buf: Bitmask) -> bool { 190 | buf & self.down_mask == self.down_mask 191 | } 192 | 193 | /// Check if the currently pressed keys contain the "left" key combination 194 | fn left(&self, buf: Bitmask) -> bool { 195 | buf & self.left_mask == self.left_mask 196 | } 197 | 198 | /// Check if the currently pressed keys contain the "right" key combination 199 | fn right(&self, buf: Bitmask) -> bool { 200 | buf & self.right_mask == self.right_mask 201 | } 202 | 203 | /// Check if the currently pressed keys contain the "cycle_monitor" key combination 204 | fn cycle_monitor(&self, buf: Bitmask) -> bool { 205 | buf & self.cycle_monitor_mask == self.cycle_monitor_mask 206 | } 207 | 208 | /// Check if the currently pressed keys contain the "scale_increase" key combination 209 | fn scale_increase(&self, buf: Bitmask) -> bool { 210 | buf & self.scale_increase_mask == self.scale_increase_mask 211 | } 212 | 213 | /// Check if the currently pressed keys contain the "scale_decrease" key combination 214 | fn scale_decrease(&self, buf: Bitmask) -> bool { 215 | buf & self.scale_decrease_mask == self.scale_decrease_mask 216 | } 217 | 218 | /// Check if the currently pressed keys contain the "toggle_hidden" key combination 219 | fn toggle_hidden(&self, buf: Bitmask) -> bool { 220 | buf & self.toggle_hidden_mask == self.toggle_hidden_mask 221 | } 222 | 223 | /// Check if the currently pressed keys contain the "toggle_adjust" key combination 224 | fn toggle_adjust(&self, buf: Bitmask) -> bool { 225 | buf & self.toggle_adjust_mask == self.toggle_adjust_mask 226 | } 227 | 228 | /// Check if the currently pressed keys contain the "toggle_color_picker" key combination 229 | fn toggle_color_picker(&self, buf: Bitmask) -> bool { 230 | buf & self.toggle_color_picker_mask == self.toggle_color_picker_mask 231 | } 232 | 233 | //TODO: this is not strictly correct: if a movement keybind uses multiple keys it breaks, as it will return `true` for partial binding presses 234 | /// Check if the currently pressed keys contain any movement keys 235 | fn any_movement(&self, buf: Bitmask) -> bool { 236 | buf & self.any_movement_mask != 0 237 | } 238 | 239 | //TODO: this is not strictly correct: if a scale keybind uses multiple keys it breaks, as it will return `true` for partial binding presses 240 | /// Check if the currently pressed keys contain any scaling keys 241 | fn any_scale(&self, buf: Bitmask) -> bool { 242 | buf & self.any_scale_mask != 0 243 | } 244 | } 245 | 246 | pub struct HotkeyManager 247 | where 248 | KS: KeyboardState, 249 | K: KeycodeType, 250 | { 251 | previous_state: Bitmask, 252 | current_state: Bitmask, 253 | movement_key_held_frames: u32, 254 | scale_key_held_frames: u32, 255 | key_buffer: KeyBuffer, 256 | keyboard_state: KS, 257 | } 258 | 259 | impl HotkeyManager 260 | where 261 | KS: KeyboardState, 262 | K: KeycodeType, 263 | { 264 | pub(crate) fn new_generic(key_bindings: &KeyBindings) -> Result, &'static str> { 265 | Ok(HotkeyManager { 266 | previous_state: 0, 267 | current_state: 0, 268 | movement_key_held_frames: 0, 269 | scale_key_held_frames: 0, 270 | key_buffer: KeyBuffer::new(key_bindings)?, 271 | keyboard_state: KS::default(), 272 | }) 273 | } 274 | 275 | pub fn poll_keys(&mut self) { 276 | self.keyboard_state.poll(); 277 | } 278 | 279 | /// updates state with current key data 280 | pub fn process_keys(&mut self) { 281 | self.previous_state = self.current_state; 282 | 283 | // calculate state 284 | let key_buffer = &self.key_buffer; 285 | key_buffer.update(&mut self.current_state, self.keyboard_state.get_state()); 286 | 287 | self.movement_key_held_frames = if key_buffer.any_movement(self.current_state) { 288 | self.movement_key_held_frames + 1 289 | } else { 290 | 0 291 | }; 292 | 293 | self.scale_key_held_frames = if key_buffer.any_scale(self.current_state) { 294 | self.scale_key_held_frames + 1 295 | } else { 296 | 0 297 | }; 298 | } 299 | 300 | /// check if "toggle_hidden" key combination was just pressed 301 | pub fn toggle_hidden(&self) -> bool { 302 | let key_buffer = &self.key_buffer; 303 | !key_buffer.toggle_hidden(self.previous_state) && key_buffer.toggle_hidden(self.current_state) 304 | } 305 | 306 | /// check if "toggle_adjust" key combination was just pressed 307 | pub fn toggle_adjust(&self) -> bool { 308 | let key_buffer = &self.key_buffer; 309 | !key_buffer.toggle_adjust(self.previous_state) && key_buffer.toggle_adjust(self.current_state) 310 | } 311 | 312 | /// check if "toggle_color_picker" key combination was just pressed 313 | pub fn toggle_color_picker(&self) -> bool { 314 | let key_buffer = &self.key_buffer; 315 | !key_buffer.toggle_color_picker(self.previous_state) && key_buffer.toggle_color_picker(self.current_state) 316 | } 317 | 318 | /// check if "cycle_monitor" key combination was just pressed 319 | pub fn cycle_monitor(&self) -> bool { 320 | let key_buffer = &self.key_buffer; 321 | !key_buffer.cycle_monitor(self.previous_state) && key_buffer.cycle_monitor(self.current_state) 322 | } 323 | 324 | /// calculate the move up speed based on how long movement keys have been held 325 | pub fn move_up(&self) -> u32 { 326 | if self.key_buffer.up(self.current_state) { 327 | move_ramp(self.movement_key_held_frames) 328 | } else { 329 | 0 330 | } 331 | } 332 | 333 | /// calculate the move down speed based on how long movement keys have been held 334 | pub fn move_down(&self) -> u32 { 335 | if self.key_buffer.down(self.current_state) { 336 | move_ramp(self.movement_key_held_frames) 337 | } else { 338 | 0 339 | } 340 | } 341 | 342 | /// calculate the move left speed based on how long movement keys have been held 343 | pub fn move_left(&self) -> u32 { 344 | if self.key_buffer.left(self.current_state) { 345 | move_ramp(self.movement_key_held_frames) 346 | } else { 347 | 0 348 | } 349 | } 350 | 351 | /// calculate the move right speed based on how long movement keys have been held 352 | pub fn move_right(&self) -> u32 { 353 | if self.key_buffer.right(self.current_state) { 354 | move_ramp(self.movement_key_held_frames) 355 | } else { 356 | 0 357 | } 358 | } 359 | 360 | /// calculate the scale increase speed based on how long scaling keys have been held 361 | pub fn scale_increase(&self) -> u32 { 362 | if self.key_buffer.scale_increase(self.current_state) { 363 | scale_ramp(self.scale_key_held_frames) 364 | } else { 365 | 0 366 | } 367 | } 368 | 369 | /// calculate the scale decrease speed based on how long scaling keys have been held 370 | pub fn scale_decrease(&self) -> u32 { 371 | if self.key_buffer.scale_decrease(self.current_state) { 372 | scale_ramp(self.scale_key_held_frames) 373 | } else { 374 | 0 375 | } 376 | } 377 | } 378 | 379 | // TODO: this should probably be fps-aware 380 | fn move_ramp(frames: u32) -> u32 { 381 | if frames < 2 { 382 | 1 383 | } else if frames < 10 { 384 | 0 385 | } else if frames < 25 { 386 | 1 387 | } else if frames < 35 { 388 | 4 389 | } else if frames < 55 { 390 | 16 391 | } else if frames < 75 { 392 | 32 393 | } else { 394 | 64 395 | } 396 | } 397 | 398 | // TODO: this should probably be fps-aware 399 | fn scale_ramp(frames: u32) -> u32 { 400 | if frames < 2 { 401 | 1 402 | } else if frames < 10 { 403 | 0 404 | } else if frames < 25 { 405 | 1 406 | } else if frames < 35 { 407 | 4 408 | } else if frames < 55 { 409 | 16 410 | } else if frames < 75 { 411 | 32 412 | } else { 413 | 64 414 | } 415 | } 416 | -------------------------------------------------------------------------------- /src-lib/private/platform/generic.rs: -------------------------------------------------------------------------------- 1 | // This file is part of simple-crosshair-overlay and is licenced under the GNU GPL v3.0. 2 | // See LICENSE file for full text. 3 | // Copyright © 2023 Michael Ripley 4 | 5 | //! Platform-agnostic implementations. 6 | //! This is only in the module tree on targets lacking a platform-specific implementation. 7 | //! On platforms that do not support the operation they will no-op and indicate that the action failed. 8 | 9 | use device_query::{DeviceQuery, DeviceState, Keycode as DeviceQueryKeycode}; 10 | 11 | use crate::private::hotkey; 12 | use crate::private::hotkey::{KeyBindings, Keycode}; 13 | use crate::private::platform::{KeyboardState, KeycodeType}; 14 | 15 | /// platform-independent window handle (it's nothing) 16 | #[derive(Copy, Clone, Debug)] 17 | pub struct WindowHandle; 18 | 19 | /// Always returns `None`, as this requires a platform-specific implementation. 20 | pub fn get_foreground_window() -> Option { 21 | None 22 | } 23 | 24 | /// Always no-ops and returns `false` for the result (indicating failure), as this requires a platform-specific implementation. 25 | pub fn set_foreground_window(_window_handle: WindowHandle) -> bool { 26 | false 27 | } 28 | 29 | pub struct DeviceQueryKeyboardState { 30 | device_state: DeviceState, 31 | keys: Vec, 32 | } 33 | 34 | impl Default for DeviceQueryKeyboardState { 35 | fn default() -> Self { 36 | Self { 37 | device_state: DeviceState::new(), 38 | keys: Vec::new(), 39 | } 40 | } 41 | } 42 | 43 | impl KeyboardState for DeviceQueryKeyboardState { 44 | fn poll(&mut self) { 45 | self.keys = self.device_state.get_keys(); 46 | } 47 | 48 | fn get_state(&self) -> &[DeviceQueryKeycode] { 49 | &self.keys 50 | } 51 | } 52 | 53 | impl From for Keycode { 54 | fn from(value: DeviceQueryKeycode) -> Self { 55 | match value { 56 | DeviceQueryKeycode::Key0 => Keycode::Key0, 57 | DeviceQueryKeycode::Key1 => Keycode::Key1, 58 | DeviceQueryKeycode::Key2 => Keycode::Key2, 59 | DeviceQueryKeycode::Key3 => Keycode::Key3, 60 | DeviceQueryKeycode::Key4 => Keycode::Key4, 61 | DeviceQueryKeycode::Key5 => Keycode::Key5, 62 | DeviceQueryKeycode::Key6 => Keycode::Key6, 63 | DeviceQueryKeycode::Key7 => Keycode::Key7, 64 | DeviceQueryKeycode::Key8 => Keycode::Key8, 65 | DeviceQueryKeycode::Key9 => Keycode::Key9, 66 | DeviceQueryKeycode::A => Keycode::A, 67 | DeviceQueryKeycode::B => Keycode::B, 68 | DeviceQueryKeycode::C => Keycode::C, 69 | DeviceQueryKeycode::D => Keycode::D, 70 | DeviceQueryKeycode::E => Keycode::E, 71 | DeviceQueryKeycode::F => Keycode::F, 72 | DeviceQueryKeycode::G => Keycode::G, 73 | DeviceQueryKeycode::H => Keycode::H, 74 | DeviceQueryKeycode::I => Keycode::I, 75 | DeviceQueryKeycode::J => Keycode::J, 76 | DeviceQueryKeycode::K => Keycode::K, 77 | DeviceQueryKeycode::L => Keycode::L, 78 | DeviceQueryKeycode::M => Keycode::M, 79 | DeviceQueryKeycode::N => Keycode::N, 80 | DeviceQueryKeycode::O => Keycode::O, 81 | DeviceQueryKeycode::P => Keycode::P, 82 | DeviceQueryKeycode::Q => Keycode::Q, 83 | DeviceQueryKeycode::R => Keycode::R, 84 | DeviceQueryKeycode::S => Keycode::S, 85 | DeviceQueryKeycode::T => Keycode::T, 86 | DeviceQueryKeycode::U => Keycode::U, 87 | DeviceQueryKeycode::V => Keycode::V, 88 | DeviceQueryKeycode::W => Keycode::W, 89 | DeviceQueryKeycode::X => Keycode::X, 90 | DeviceQueryKeycode::Y => Keycode::Y, 91 | DeviceQueryKeycode::Z => Keycode::Z, 92 | DeviceQueryKeycode::F1 => Keycode::F1, 93 | DeviceQueryKeycode::F2 => Keycode::F2, 94 | DeviceQueryKeycode::F3 => Keycode::F3, 95 | DeviceQueryKeycode::F4 => Keycode::F4, 96 | DeviceQueryKeycode::F5 => Keycode::F5, 97 | DeviceQueryKeycode::F6 => Keycode::F6, 98 | DeviceQueryKeycode::F7 => Keycode::F7, 99 | DeviceQueryKeycode::F8 => Keycode::F8, 100 | DeviceQueryKeycode::F9 => Keycode::F9, 101 | DeviceQueryKeycode::F10 => Keycode::F10, 102 | DeviceQueryKeycode::F11 => Keycode::F11, 103 | DeviceQueryKeycode::F12 => Keycode::F12, 104 | DeviceQueryKeycode::Escape => Keycode::Escape, 105 | DeviceQueryKeycode::Space => Keycode::Space, 106 | DeviceQueryKeycode::LControl => Keycode::LControl, 107 | DeviceQueryKeycode::RControl => Keycode::RControl, 108 | DeviceQueryKeycode::LShift => Keycode::LShift, 109 | DeviceQueryKeycode::RShift => Keycode::RShift, 110 | DeviceQueryKeycode::LAlt => Keycode::LAlt, 111 | DeviceQueryKeycode::RAlt => Keycode::RAlt, 112 | DeviceQueryKeycode::LMeta => Keycode::LMeta, 113 | DeviceQueryKeycode::RMeta => Keycode::RMeta, 114 | DeviceQueryKeycode::Enter => Keycode::Enter, 115 | DeviceQueryKeycode::Up => Keycode::Up, 116 | DeviceQueryKeycode::Down => Keycode::Down, 117 | DeviceQueryKeycode::Left => Keycode::Left, 118 | DeviceQueryKeycode::Right => Keycode::Right, 119 | DeviceQueryKeycode::Backspace => Keycode::Backspace, 120 | DeviceQueryKeycode::CapsLock => Keycode::CapsLock, 121 | DeviceQueryKeycode::Tab => Keycode::Tab, 122 | DeviceQueryKeycode::Home => Keycode::Home, 123 | DeviceQueryKeycode::End => Keycode::End, 124 | DeviceQueryKeycode::PageUp => Keycode::PageUp, 125 | DeviceQueryKeycode::PageDown => Keycode::PageDown, 126 | DeviceQueryKeycode::Insert => Keycode::Insert, 127 | DeviceQueryKeycode::Delete => Keycode::Delete, 128 | DeviceQueryKeycode::Numpad0 => Keycode::Numpad0, 129 | DeviceQueryKeycode::Numpad1 => Keycode::Numpad1, 130 | DeviceQueryKeycode::Numpad2 => Keycode::Numpad2, 131 | DeviceQueryKeycode::Numpad3 => Keycode::Numpad3, 132 | DeviceQueryKeycode::Numpad4 => Keycode::Numpad4, 133 | DeviceQueryKeycode::Numpad5 => Keycode::Numpad5, 134 | DeviceQueryKeycode::Numpad6 => Keycode::Numpad6, 135 | DeviceQueryKeycode::Numpad7 => Keycode::Numpad7, 136 | DeviceQueryKeycode::Numpad8 => Keycode::Numpad8, 137 | DeviceQueryKeycode::Numpad9 => Keycode::Numpad9, 138 | DeviceQueryKeycode::NumpadSubtract => Keycode::NumpadSubtract, 139 | DeviceQueryKeycode::NumpadAdd => Keycode::NumpadAdd, 140 | DeviceQueryKeycode::NumpadDivide => Keycode::NumpadDivide, 141 | DeviceQueryKeycode::NumpadMultiply => Keycode::NumpadMultiply, 142 | DeviceQueryKeycode::Grave => Keycode::Grave, 143 | DeviceQueryKeycode::Minus => Keycode::Minus, 144 | DeviceQueryKeycode::Equal => Keycode::Equal, 145 | DeviceQueryKeycode::LeftBracket => Keycode::LeftBracket, 146 | DeviceQueryKeycode::RightBracket => Keycode::RightBracket, 147 | DeviceQueryKeycode::BackSlash => Keycode::BackSlash, 148 | DeviceQueryKeycode::Semicolon => Keycode::Semicolon, 149 | DeviceQueryKeycode::Apostrophe => Keycode::Apostrophe, 150 | DeviceQueryKeycode::Comma => Keycode::Comma, 151 | DeviceQueryKeycode::Dot => Keycode::Dot, 152 | DeviceQueryKeycode::Slash => Keycode::Slash, 153 | DeviceQueryKeycode::F13 => Keycode::F13, 154 | DeviceQueryKeycode::F14 => Keycode::F14, 155 | DeviceQueryKeycode::F15 => Keycode::F15, 156 | DeviceQueryKeycode::F16 => Keycode::F16, 157 | DeviceQueryKeycode::F17 => Keycode::F17, 158 | DeviceQueryKeycode::F18 => Keycode::F18, 159 | DeviceQueryKeycode::F19 => Keycode::F19, 160 | DeviceQueryKeycode::F20 => Keycode::F20, 161 | DeviceQueryKeycode::Command => Keycode::Command, 162 | DeviceQueryKeycode::LOption => Keycode::LOption, 163 | DeviceQueryKeycode::ROption => Keycode::ROption, 164 | DeviceQueryKeycode::NumpadEquals => Keycode::NumpadEquals, 165 | DeviceQueryKeycode::NumpadEnter => Keycode::NumpadEnter, 166 | DeviceQueryKeycode::NumpadDecimal => Keycode::NumpadDecimal, 167 | } 168 | } 169 | } 170 | 171 | impl From for DeviceQueryKeycode { 172 | fn from(value: Keycode) -> Self { 173 | match value { 174 | Keycode::Key0 => DeviceQueryKeycode::Key0, 175 | Keycode::Key1 => DeviceQueryKeycode::Key1, 176 | Keycode::Key2 => DeviceQueryKeycode::Key2, 177 | Keycode::Key3 => DeviceQueryKeycode::Key3, 178 | Keycode::Key4 => DeviceQueryKeycode::Key4, 179 | Keycode::Key5 => DeviceQueryKeycode::Key5, 180 | Keycode::Key6 => DeviceQueryKeycode::Key6, 181 | Keycode::Key7 => DeviceQueryKeycode::Key7, 182 | Keycode::Key8 => DeviceQueryKeycode::Key8, 183 | Keycode::Key9 => DeviceQueryKeycode::Key9, 184 | Keycode::A => DeviceQueryKeycode::A, 185 | Keycode::B => DeviceQueryKeycode::B, 186 | Keycode::C => DeviceQueryKeycode::C, 187 | Keycode::D => DeviceQueryKeycode::D, 188 | Keycode::E => DeviceQueryKeycode::E, 189 | Keycode::F => DeviceQueryKeycode::F, 190 | Keycode::G => DeviceQueryKeycode::G, 191 | Keycode::H => DeviceQueryKeycode::H, 192 | Keycode::I => DeviceQueryKeycode::I, 193 | Keycode::J => DeviceQueryKeycode::J, 194 | Keycode::K => DeviceQueryKeycode::K, 195 | Keycode::L => DeviceQueryKeycode::L, 196 | Keycode::M => DeviceQueryKeycode::M, 197 | Keycode::N => DeviceQueryKeycode::N, 198 | Keycode::O => DeviceQueryKeycode::O, 199 | Keycode::P => DeviceQueryKeycode::P, 200 | Keycode::Q => DeviceQueryKeycode::Q, 201 | Keycode::R => DeviceQueryKeycode::R, 202 | Keycode::S => DeviceQueryKeycode::S, 203 | Keycode::T => DeviceQueryKeycode::T, 204 | Keycode::U => DeviceQueryKeycode::U, 205 | Keycode::V => DeviceQueryKeycode::V, 206 | Keycode::W => DeviceQueryKeycode::W, 207 | Keycode::X => DeviceQueryKeycode::X, 208 | Keycode::Y => DeviceQueryKeycode::Y, 209 | Keycode::Z => DeviceQueryKeycode::Z, 210 | Keycode::F1 => DeviceQueryKeycode::F1, 211 | Keycode::F2 => DeviceQueryKeycode::F2, 212 | Keycode::F3 => DeviceQueryKeycode::F3, 213 | Keycode::F4 => DeviceQueryKeycode::F4, 214 | Keycode::F5 => DeviceQueryKeycode::F5, 215 | Keycode::F6 => DeviceQueryKeycode::F6, 216 | Keycode::F7 => DeviceQueryKeycode::F7, 217 | Keycode::F8 => DeviceQueryKeycode::F8, 218 | Keycode::F9 => DeviceQueryKeycode::F9, 219 | Keycode::F10 => DeviceQueryKeycode::F10, 220 | Keycode::F11 => DeviceQueryKeycode::F11, 221 | Keycode::F12 => DeviceQueryKeycode::F12, 222 | Keycode::Escape => DeviceQueryKeycode::Escape, 223 | Keycode::Space => DeviceQueryKeycode::Space, 224 | Keycode::LControl => DeviceQueryKeycode::LControl, 225 | Keycode::RControl => DeviceQueryKeycode::RControl, 226 | Keycode::LShift => DeviceQueryKeycode::LShift, 227 | Keycode::RShift => DeviceQueryKeycode::RShift, 228 | Keycode::LAlt => DeviceQueryKeycode::LAlt, 229 | Keycode::RAlt => DeviceQueryKeycode::RAlt, 230 | Keycode::LMeta => DeviceQueryKeycode::LMeta, 231 | Keycode::RMeta => DeviceQueryKeycode::RMeta, 232 | Keycode::Enter => DeviceQueryKeycode::Enter, 233 | Keycode::Up => DeviceQueryKeycode::Up, 234 | Keycode::Down => DeviceQueryKeycode::Down, 235 | Keycode::Left => DeviceQueryKeycode::Left, 236 | Keycode::Right => DeviceQueryKeycode::Right, 237 | Keycode::Backspace => DeviceQueryKeycode::Backspace, 238 | Keycode::CapsLock => DeviceQueryKeycode::CapsLock, 239 | Keycode::Tab => DeviceQueryKeycode::Tab, 240 | Keycode::Home => DeviceQueryKeycode::Home, 241 | Keycode::End => DeviceQueryKeycode::End, 242 | Keycode::PageUp => DeviceQueryKeycode::PageUp, 243 | Keycode::PageDown => DeviceQueryKeycode::PageDown, 244 | Keycode::Insert => DeviceQueryKeycode::Insert, 245 | Keycode::Delete => DeviceQueryKeycode::Delete, 246 | Keycode::Numpad0 => DeviceQueryKeycode::Numpad0, 247 | Keycode::Numpad1 => DeviceQueryKeycode::Numpad1, 248 | Keycode::Numpad2 => DeviceQueryKeycode::Numpad2, 249 | Keycode::Numpad3 => DeviceQueryKeycode::Numpad3, 250 | Keycode::Numpad4 => DeviceQueryKeycode::Numpad4, 251 | Keycode::Numpad5 => DeviceQueryKeycode::Numpad5, 252 | Keycode::Numpad6 => DeviceQueryKeycode::Numpad6, 253 | Keycode::Numpad7 => DeviceQueryKeycode::Numpad7, 254 | Keycode::Numpad8 => DeviceQueryKeycode::Numpad8, 255 | Keycode::Numpad9 => DeviceQueryKeycode::Numpad9, 256 | Keycode::NumpadSubtract => DeviceQueryKeycode::NumpadSubtract, 257 | Keycode::NumpadAdd => DeviceQueryKeycode::NumpadAdd, 258 | Keycode::NumpadDivide => DeviceQueryKeycode::NumpadDivide, 259 | Keycode::NumpadMultiply => DeviceQueryKeycode::NumpadMultiply, 260 | Keycode::Grave => DeviceQueryKeycode::Grave, 261 | Keycode::Minus => DeviceQueryKeycode::Minus, 262 | Keycode::Equal => DeviceQueryKeycode::Equal, 263 | Keycode::LeftBracket => DeviceQueryKeycode::LeftBracket, 264 | Keycode::RightBracket => DeviceQueryKeycode::RightBracket, 265 | Keycode::BackSlash => DeviceQueryKeycode::BackSlash, 266 | Keycode::Semicolon => DeviceQueryKeycode::Semicolon, 267 | Keycode::Apostrophe => DeviceQueryKeycode::Apostrophe, 268 | Keycode::Comma => DeviceQueryKeycode::Comma, 269 | Keycode::Dot => DeviceQueryKeycode::Dot, 270 | Keycode::Slash => DeviceQueryKeycode::Slash, 271 | Keycode::F13 => DeviceQueryKeycode::F13, 272 | Keycode::F14 => DeviceQueryKeycode::F14, 273 | Keycode::F15 => DeviceQueryKeycode::F15, 274 | Keycode::F16 => DeviceQueryKeycode::F16, 275 | Keycode::F17 => DeviceQueryKeycode::F17, 276 | Keycode::F18 => DeviceQueryKeycode::F18, 277 | Keycode::F19 => DeviceQueryKeycode::F19, 278 | Keycode::F20 => DeviceQueryKeycode::F20, 279 | Keycode::Command => DeviceQueryKeycode::Command, 280 | Keycode::LOption => DeviceQueryKeycode::LOption, 281 | Keycode::ROption => DeviceQueryKeycode::ROption, 282 | Keycode::NumpadEquals => DeviceQueryKeycode::NumpadEquals, 283 | Keycode::NumpadEnter => DeviceQueryKeycode::NumpadEnter, 284 | Keycode::NumpadDecimal => DeviceQueryKeycode::NumpadDecimal, 285 | } 286 | } 287 | } 288 | 289 | impl KeycodeType for DeviceQueryKeycode { 290 | #[inline(always)] 291 | fn num_variants() -> usize { 292 | // MUST be the number of variants returned by `index()` 293 | 111 294 | } 295 | 296 | fn index(&self) -> usize { 297 | match &self { 298 | DeviceQueryKeycode::Key0 => 0, 299 | DeviceQueryKeycode::Key1 => 1, 300 | DeviceQueryKeycode::Key2 => 2, 301 | DeviceQueryKeycode::Key3 => 3, 302 | DeviceQueryKeycode::Key4 => 4, 303 | DeviceQueryKeycode::Key5 => 5, 304 | DeviceQueryKeycode::Key6 => 6, 305 | DeviceQueryKeycode::Key7 => 7, 306 | DeviceQueryKeycode::Key8 => 8, 307 | DeviceQueryKeycode::Key9 => 9, 308 | DeviceQueryKeycode::A => 10, 309 | DeviceQueryKeycode::B => 11, 310 | DeviceQueryKeycode::C => 12, 311 | DeviceQueryKeycode::D => 13, 312 | DeviceQueryKeycode::E => 14, 313 | DeviceQueryKeycode::F => 15, 314 | DeviceQueryKeycode::G => 16, 315 | DeviceQueryKeycode::H => 17, 316 | DeviceQueryKeycode::I => 18, 317 | DeviceQueryKeycode::J => 19, 318 | DeviceQueryKeycode::K => 20, 319 | DeviceQueryKeycode::L => 21, 320 | DeviceQueryKeycode::M => 22, 321 | DeviceQueryKeycode::N => 23, 322 | DeviceQueryKeycode::O => 24, 323 | DeviceQueryKeycode::P => 25, 324 | DeviceQueryKeycode::Q => 26, 325 | DeviceQueryKeycode::R => 27, 326 | DeviceQueryKeycode::S => 28, 327 | DeviceQueryKeycode::T => 29, 328 | DeviceQueryKeycode::U => 30, 329 | DeviceQueryKeycode::V => 31, 330 | DeviceQueryKeycode::W => 32, 331 | DeviceQueryKeycode::X => 33, 332 | DeviceQueryKeycode::Y => 34, 333 | DeviceQueryKeycode::Z => 35, 334 | DeviceQueryKeycode::F1 => 36, 335 | DeviceQueryKeycode::F2 => 37, 336 | DeviceQueryKeycode::F3 => 38, 337 | DeviceQueryKeycode::F4 => 39, 338 | DeviceQueryKeycode::F5 => 40, 339 | DeviceQueryKeycode::F6 => 41, 340 | DeviceQueryKeycode::F7 => 42, 341 | DeviceQueryKeycode::F8 => 43, 342 | DeviceQueryKeycode::F9 => 44, 343 | DeviceQueryKeycode::F10 => 45, 344 | DeviceQueryKeycode::F11 => 46, 345 | DeviceQueryKeycode::F12 => 47, 346 | DeviceQueryKeycode::Escape => 48, 347 | DeviceQueryKeycode::Space => 49, 348 | DeviceQueryKeycode::LControl => 50, 349 | DeviceQueryKeycode::RControl => 51, 350 | DeviceQueryKeycode::LShift => 52, 351 | DeviceQueryKeycode::RShift => 53, 352 | DeviceQueryKeycode::LAlt => 54, 353 | DeviceQueryKeycode::RAlt => 55, 354 | DeviceQueryKeycode::LMeta => 56, 355 | DeviceQueryKeycode::RMeta => 57, 356 | DeviceQueryKeycode::Enter => 58, 357 | DeviceQueryKeycode::Up => 59, 358 | DeviceQueryKeycode::Down => 60, 359 | DeviceQueryKeycode::Left => 61, 360 | DeviceQueryKeycode::Right => 62, 361 | DeviceQueryKeycode::Backspace => 63, 362 | DeviceQueryKeycode::CapsLock => 64, 363 | DeviceQueryKeycode::Tab => 65, 364 | DeviceQueryKeycode::Home => 66, 365 | DeviceQueryKeycode::End => 67, 366 | DeviceQueryKeycode::PageUp => 68, 367 | DeviceQueryKeycode::PageDown => 69, 368 | DeviceQueryKeycode::Insert => 70, 369 | DeviceQueryKeycode::Delete => 71, 370 | DeviceQueryKeycode::Numpad0 => 72, 371 | DeviceQueryKeycode::Numpad1 => 73, 372 | DeviceQueryKeycode::Numpad2 => 74, 373 | DeviceQueryKeycode::Numpad3 => 75, 374 | DeviceQueryKeycode::Numpad4 => 76, 375 | DeviceQueryKeycode::Numpad5 => 77, 376 | DeviceQueryKeycode::Numpad6 => 78, 377 | DeviceQueryKeycode::Numpad7 => 79, 378 | DeviceQueryKeycode::Numpad8 => 80, 379 | DeviceQueryKeycode::Numpad9 => 81, 380 | DeviceQueryKeycode::NumpadSubtract => 82, 381 | DeviceQueryKeycode::NumpadAdd => 83, 382 | DeviceQueryKeycode::NumpadDivide => 84, 383 | DeviceQueryKeycode::NumpadMultiply => 85, 384 | DeviceQueryKeycode::Grave => 86, 385 | DeviceQueryKeycode::Minus => 87, 386 | DeviceQueryKeycode::Equal => 88, 387 | DeviceQueryKeycode::LeftBracket => 89, 388 | DeviceQueryKeycode::RightBracket => 90, 389 | DeviceQueryKeycode::BackSlash => 91, 390 | DeviceQueryKeycode::Semicolon => 92, 391 | DeviceQueryKeycode::Apostrophe => 93, 392 | DeviceQueryKeycode::Comma => 94, 393 | DeviceQueryKeycode::Dot => 95, 394 | DeviceQueryKeycode::Slash => 96, 395 | DeviceQueryKeycode::F13 => 97, 396 | DeviceQueryKeycode::F14 => 98, 397 | DeviceQueryKeycode::F15 => 99, 398 | DeviceQueryKeycode::F16 => 100, 399 | DeviceQueryKeycode::F17 => 101, 400 | DeviceQueryKeycode::F18 => 102, 401 | DeviceQueryKeycode::F19 => 103, 402 | DeviceQueryKeycode::F20 => 104, 403 | DeviceQueryKeycode::Command => 105, 404 | DeviceQueryKeycode::LOption => 106, 405 | DeviceQueryKeycode::ROption => 107, 406 | DeviceQueryKeycode::NumpadEquals => 108, 407 | DeviceQueryKeycode::NumpadEnter => 109, 408 | DeviceQueryKeycode::NumpadDecimal => 110, 409 | } 410 | } 411 | } 412 | 413 | pub type HotkeyManager = hotkey::HotkeyManager; 414 | 415 | impl HotkeyManager { 416 | pub fn new(key_bindings: &KeyBindings) -> Result { 417 | HotkeyManager::new_generic(key_bindings) 418 | } 419 | } 420 | 421 | impl Default for HotkeyManager { 422 | fn default() -> Self { 423 | HotkeyManager::new(&KeyBindings::default()).expect("default keybindings were invalid") 424 | } 425 | } 426 | -------------------------------------------------------------------------------- /src/window.rs: -------------------------------------------------------------------------------- 1 | // This file is part of simple-crosshair-overlay and is licenced under the GNU GPL v3.0. 2 | // See LICENSE file for full text. 3 | // Copyright © 2023-2025 Michael Ripley 4 | 5 | use std::num::NonZeroU32; 6 | use std::rc::Rc; 7 | 8 | use debug_print::debug_println; 9 | use tray_icon::TrayIcon; 10 | use tray_icon::dpi::{PhysicalPosition, PhysicalSize}; 11 | use tray_icon::menu::{MenuEvent, MenuEventReceiver}; 12 | use winit::application::ApplicationHandler; 13 | use winit::event::{DeviceEvent, DeviceId, ElementState, MouseButton, StartCause, WindowEvent}; 14 | use winit::event_loop::{ActiveEventLoop, EventLoop}; 15 | use winit::window::{CursorIcon, Window, WindowId, WindowLevel}; 16 | 17 | use simple_crosshair_overlay::private::platform; 18 | use simple_crosshair_overlay::private::platform::HotkeyManager; 19 | use simple_crosshair_overlay::private::settings::{CONFIG_PATH, RenderMode, Settings}; 20 | use simple_crosshair_overlay::private::util::dialog::DialogWorker; 21 | use simple_crosshair_overlay::private::util::{dialog, image}; 22 | 23 | use crate::tray::MenuItems; 24 | use crate::{build_constants, handle_color_pick, tray}; 25 | 26 | pub type UserEvent = (); 27 | type Surface = softbuffer::Surface, Rc>; 28 | 29 | pub struct State<'a> { 30 | context: Option, 31 | settings: Settings, 32 | hotkey_manager: HotkeyManager, 33 | /// native dialogs block a thread, so we'll spin up a single thread to loop through queued dialogs. 34 | /// If we ever need to show multiple dialogs, they just get queued. 35 | dialog_worker: DialogWorker, 36 | /// we keep the tray icon in an Option so that we can take() it later to drop 37 | tray_icon: Option, 38 | menu_items: MenuItems, 39 | last_focused_window: Option, 40 | last_mouse_position: PhysicalPosition, 41 | menu_channel: &'a MenuEventReceiver, 42 | /// if set to true, the next redraw will be forced even for known buffer contents 43 | force_redraw: bool, 44 | window_position_dirty: bool, 45 | window_scale_dirty: bool, 46 | window_visible: bool, 47 | } 48 | 49 | /// Window context 50 | struct Context { 51 | window: Rc, 52 | surface: Surface, 53 | } 54 | 55 | impl Context { 56 | fn new(active_event_loop: &ActiveEventLoop, settings: &mut Settings) -> Self { 57 | // unsafe note: these three structs MUST live and die together. 58 | // It is highly illegal to use the context or surface after the window is dropped. 59 | // The context only gets used right here, so that's fine. 60 | // As of this writing, none of these get moved out of this struct. Therefore, they all get dropped at the same time, which is safe. 61 | let window = Rc::new(init_window(active_event_loop, settings)); 62 | let context = softbuffer::Context::new(window.clone()).unwrap(); 63 | let surface: Surface = Surface::new(&context, window.clone()).unwrap(); 64 | Context { window, surface } 65 | } 66 | } 67 | 68 | impl<'a> State<'a> { 69 | pub fn new(settings: Settings, _event_loop: &EventLoop) -> Self { 70 | // HotkeyManager has a decent quantity of data in it, but again it never really gets moved so we can just leave it on the stack 71 | let hotkey_manager: HotkeyManager = HotkeyManager::new(&settings.persisted.key_bindings).unwrap_or_else(|e| { 72 | dialog::show_warning(format!("{e}\n\nUsing default hotkeys.")); 73 | HotkeyManager::default() 74 | }); 75 | 76 | let (menu_items, tray_icon) = tray::build_tray_icon(); 77 | State { 78 | context: None, 79 | settings, 80 | hotkey_manager, 81 | dialog_worker: dialog::spawn_worker(), 82 | tray_icon: Some(tray_icon), 83 | menu_items, 84 | last_focused_window: None, 85 | last_mouse_position: Default::default(), 86 | menu_channel: MenuEvent::receiver(), 87 | force_redraw: false, 88 | window_position_dirty: false, 89 | window_scale_dirty: false, 90 | window_visible: true, 91 | } 92 | } 93 | 94 | fn post_event_work(&mut self, active_event_loop: &ActiveEventLoop) { 95 | let window: &Window = &self.context.as_ref().unwrap().window; 96 | 97 | if let Ok(path) = self.dialog_worker.try_recv_file_path() { 98 | self.menu_items.image_pick_button.set_enabled(true); 99 | 100 | if let Some(path) = path { 101 | match self.settings.load_png(path) { 102 | Ok(()) => { 103 | self.force_redraw = true; 104 | self.window_scale_dirty = true; 105 | } 106 | Err(e) => dialog::show_warning(format!("Error loading PNG.\n\n{e}")), 107 | } 108 | } 109 | } 110 | 111 | while let Ok(event) = self.menu_channel.try_recv() { 112 | match event.id { 113 | id if id == self.menu_items.exit_button.id() => { 114 | // drop the tray icon, solving the funny Windows issue where it lingers after application close 115 | #[cfg(not(target_os = "linux"))] 116 | self.tray_icon.take(); 117 | window.set_visible(false); 118 | if let Err(e) = self.settings.save() { 119 | dialog::show_warning(format!( 120 | "Error saving settings to \"{}\".\n\n{}", 121 | CONFIG_PATH.display(), 122 | e 123 | )); 124 | } 125 | 126 | // kill the dialog worker and wait for it to finish 127 | // this makes the application remain open until the user has clicked through any queued dialogs 128 | self.dialog_worker 129 | .shutdown() 130 | .expect("failed to shut down dialog worker"); 131 | 132 | active_event_loop.exit(); 133 | break; 134 | } 135 | id if id == self.menu_items.visible_button.id() => { 136 | window.set_visible(self.menu_items.visible_button.is_checked()); 137 | } 138 | id if id == self.menu_items.reset_button.id() => { 139 | self.settings.reset(); 140 | self.force_redraw = true; 141 | self.window_scale_dirty = true; 142 | } 143 | id if id == self.menu_items.color_pick_button.id() => { 144 | let pick_color = self.menu_items.color_pick_button.is_checked(); 145 | self.settings.set_pick_color(pick_color); 146 | handle_color_pick(pick_color, window, &mut self.last_focused_window, false); 147 | self.window_scale_dirty = true; 148 | } 149 | id if id == self.menu_items.image_pick_button.id() => { 150 | self.menu_items.image_pick_button.set_enabled(false); 151 | dialog::request_png(); 152 | } 153 | id if id == self.menu_items.about_button.id() => { 154 | dialog::show_info(format!( 155 | "{}\nversion {} {}", 156 | build_constants::APPLICATION_NAME, 157 | env!("CARGO_PKG_VERSION"), 158 | env!("GIT_COMMIT_HASH") 159 | )); 160 | } 161 | _ => (), 162 | } 163 | } 164 | 165 | if self.window_scale_dirty { 166 | on_window_size_or_position_change(window, &mut self.settings); 167 | self.window_scale_dirty = false; 168 | self.window_position_dirty = false; 169 | } else if self.window_position_dirty { 170 | on_window_position_change(window, &mut self.settings); 171 | self.window_position_dirty = false; 172 | } 173 | } 174 | } 175 | 176 | impl<'a> ApplicationHandler for State<'a> { 177 | fn new_events(&mut self, event_loop: &ActiveEventLoop, cause: StartCause) { 178 | if matches!(cause, StartCause::Init) { 179 | self.context = Some(Context::new(event_loop, &mut self.settings)) 180 | } 181 | } 182 | 183 | fn resumed(&mut self, _event_loop: &ActiveEventLoop) { 184 | // only used on iOS/Android/Web 185 | } 186 | 187 | fn user_event(&mut self, event_loop: &ActiveEventLoop, _event: UserEvent) { 188 | let window: &Window = &self.context.as_ref().unwrap().window; 189 | 190 | self.hotkey_manager.poll_keys(); 191 | self.hotkey_manager.process_keys(); 192 | 193 | let adjust_mode = self.menu_items.adjust_button.is_checked(); 194 | if adjust_mode { 195 | if self.hotkey_manager.move_up() != 0 { 196 | self.settings.persisted.window_dy -= self.hotkey_manager.move_up() as i32; 197 | self.window_position_dirty = true; 198 | } 199 | 200 | if self.hotkey_manager.move_down() != 0 { 201 | self.settings.persisted.window_dy += self.hotkey_manager.move_down() as i32; 202 | self.window_position_dirty = true; 203 | } 204 | 205 | if self.hotkey_manager.move_left() != 0 { 206 | self.settings.persisted.window_dx -= self.hotkey_manager.move_left() as i32; 207 | self.window_position_dirty = true; 208 | } 209 | 210 | if self.hotkey_manager.move_right() != 0 { 211 | self.settings.persisted.window_dx += self.hotkey_manager.move_right() as i32; 212 | self.window_position_dirty = true; 213 | } 214 | 215 | if self.hotkey_manager.cycle_monitor() { 216 | let next_monitor = (self.settings.monitor_index + 1) % window.available_monitors().count(); 217 | self.settings.set_monitor(next_monitor); 218 | self.window_scale_dirty = true; 219 | } 220 | 221 | if self.settings.is_scalable() && self.hotkey_manager.scale_increase() != 0 { 222 | self.settings.persisted.window_height += self.hotkey_manager.scale_increase(); 223 | self.settings.persisted.window_width = self.settings.persisted.window_height; 224 | self.window_scale_dirty = true; 225 | } 226 | 227 | if self.settings.is_scalable() && self.hotkey_manager.scale_decrease() != 0 { 228 | self.settings.persisted.window_height = self 229 | .settings 230 | .persisted 231 | .window_height 232 | .checked_sub(self.hotkey_manager.scale_decrease()) 233 | .unwrap_or(1) 234 | .max(1); 235 | self.settings.persisted.window_width = self.settings.persisted.window_height; 236 | self.window_scale_dirty = true; 237 | } 238 | 239 | // adjust button is already checked 240 | if self.hotkey_manager.toggle_adjust() { 241 | self.menu_items.adjust_button.set_checked(false) 242 | } 243 | } else if self.hotkey_manager.toggle_adjust() { 244 | // adjust button is NOT checked 245 | self.menu_items.adjust_button.set_checked(true) 246 | } 247 | 248 | if self.hotkey_manager.toggle_hidden() { 249 | self.window_visible = !self.window_visible; 250 | window.set_visible(self.window_visible); 251 | if !self.window_visible { 252 | self.menu_items.adjust_button.set_checked(false) 253 | } 254 | } 255 | 256 | // only enable this hotkey if the color picker is already visible OR if adjust mode is on 257 | if self.hotkey_manager.toggle_color_picker() && (adjust_mode || self.settings.get_pick_color()) { 258 | let color_pick = self.settings.toggle_pick_color(); 259 | self.menu_items.color_pick_button.set_checked(color_pick); 260 | handle_color_pick(color_pick, window, &mut self.last_focused_window, true); 261 | self.window_scale_dirty = true; 262 | } 263 | 264 | self.post_event_work(event_loop); 265 | } 266 | 267 | fn window_event(&mut self, event_loop: &ActiveEventLoop, _window_id: WindowId, event: WindowEvent) { 268 | let context: &mut Context = self.context.as_mut().unwrap(); 269 | 270 | match event { 271 | WindowEvent::RedrawRequested => { 272 | // failsafe to resize the window before a redraw if necessary 273 | // ...and of course it's fucking necessary 274 | self.settings 275 | .validate_window_size(&context.window, context.window.inner_size()); 276 | draw_window(&mut context.surface, &self.settings, self.force_redraw); 277 | self.force_redraw = false; 278 | } 279 | WindowEvent::Moved(position) => { 280 | // incredibly, if the taskbar is at the top or left of the screen Windows will 281 | // (un)helpfully shift the window over by the taskbar's size. I have no idea why 282 | // this happens and it's terrible, but luckily Windows tells me it's done this so 283 | // that I can immediately detect and undo it. 284 | debug_println!("window position changed to {:?}", position); 285 | self.settings.validate_window_position(&context.window, position); 286 | } 287 | WindowEvent::Resized(size) => { 288 | // See above nightmare scenario with the window position. I figure I might as well 289 | // do the same thing for size just in case Windows also has some arcane, evil 290 | // involuntary resizing behavior. 291 | debug_println!("window size changed to {:?}", size); 292 | self.settings.validate_window_size(&context.window, size); 293 | } 294 | WindowEvent::CursorMoved { position, .. } => { 295 | self.last_mouse_position = position; 296 | } 297 | WindowEvent::MouseInput { 298 | state: ElementState::Pressed, 299 | button: MouseButton::Left, 300 | .. 301 | } => { 302 | let PhysicalPosition { x, y } = self.last_mouse_position; 303 | let x = x as usize; 304 | let y = y as usize; 305 | 306 | let PhysicalSize { width, height } = self.settings.size(); 307 | let width = width as usize; 308 | let height = height as usize; 309 | 310 | self.settings 311 | .set_color(image::hue_alpha_color_from_coordinates(x, y, width, height)); 312 | self.menu_items.color_pick_button.set_checked(false); 313 | handle_color_pick(false, &context.window, &mut self.last_focused_window, false); 314 | self.window_scale_dirty = true; 315 | } 316 | _ => {} 317 | } 318 | 319 | self.post_event_work(event_loop); 320 | } 321 | 322 | fn device_event(&mut self, _event_loop: &ActiveEventLoop, _device_id: DeviceId, _event: DeviceEvent) {} 323 | 324 | fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) {} 325 | 326 | fn suspended(&mut self, _event_loop: &ActiveEventLoop) { 327 | // only used on iOS/Android/Web 328 | } 329 | 330 | fn exiting(&mut self, _event_loop: &ActiveEventLoop) {} 331 | 332 | fn memory_warning(&mut self, _event_loop: &ActiveEventLoop) { 333 | // only used on iOS/Android 334 | } 335 | } 336 | 337 | /// Handles both window size and position change side effects. 338 | fn on_window_size_or_position_change(window: &Window, settings: &mut Settings) { 339 | settings.set_window_size(window); 340 | settings.set_window_position(window); 341 | window.request_redraw(); // needed in case the window size didn't change but the image was replaced 342 | 343 | /* 344 | TODO: scaling jitter problem 345 | When the application is scaled really quickly via key-repeat spam it struggles to scale, move, and redraw the window in perfect sync. 346 | To fix this I'd have to completely rearchitect how scaling works. Ideas: 347 | 1. Temporarily size the window to full screen, thereby eliminating all but the redraws 348 | 2. Stop relying on key repeat and instead remember key state and use ticks for your update intervals 349 | */ 350 | } 351 | 352 | /// Slightly cheaper special case that can only handle window position changes. Do not use this if the window size may have changed. 353 | fn on_window_position_change(window: &Window, settings: &mut Settings) { 354 | settings.set_window_position(window); 355 | } 356 | 357 | /// Draws a crosshair image, or a simple red crosshair if no image is set. Normally this only 358 | /// redraws the buffer if it's uninitialized, but redraw can be forced by setting the `force` 359 | /// parameter to `true`. 360 | fn draw_window(surface: &mut Surface, settings: &Settings, force: bool) { 361 | let PhysicalSize { 362 | width: window_width, 363 | height: window_height, 364 | } = settings.size(); 365 | surface 366 | .resize( 367 | NonZeroU32::new(window_width).unwrap(), 368 | NonZeroU32::new(window_height).unwrap(), 369 | ) 370 | .unwrap(); 371 | 372 | let width = window_width as usize; 373 | let height = window_height as usize; 374 | 375 | let mut buffer = surface.buffer_mut().unwrap(); 376 | 377 | if force || buffer.age() == 0 { 378 | // only redraw if the buffer is uninitialized OR redraw is being forced 379 | match settings.render_mode { 380 | RenderMode::Image => { 381 | // draw our image 382 | buffer.copy_from_slice(settings.image().unwrap().data.as_slice()); 383 | } 384 | RenderMode::Crosshair => { 385 | // draw a generated crosshair 386 | 387 | const FULL_ALPHA: u32 = 0x00000000; 388 | 389 | if width <= 2 || height <= 2 { 390 | // edge case where there simply aren't enough pixels to draw a crosshair, so we just fall back to a dot 391 | buffer.fill(settings.color); 392 | } else { 393 | // draw a simple crosshair. Think a `+` shape. 394 | buffer.fill(FULL_ALPHA); 395 | 396 | // horizontal line 397 | let start = width * (height / 2); 398 | for x in start..start + width { 399 | buffer[x] = settings.color; 400 | } 401 | 402 | // second horizontal line (if size is even we need this for centering) 403 | if height.is_multiple_of(2) { 404 | let start = start - width; 405 | for x in start..start + width { 406 | buffer[x] = settings.color; 407 | } 408 | } 409 | 410 | // vertical line 411 | for y in 0..height { 412 | buffer[width * y + width / 2] = settings.color; 413 | } 414 | 415 | // second vertical line (if size is even we need this for centering) 416 | if width.is_multiple_of(2) { 417 | for y in 0..height { 418 | buffer[width * y + width / 2 - 1] = settings.color; 419 | } 420 | } 421 | } 422 | } 423 | RenderMode::ColorPicker => { 424 | image::draw_color_picker(&mut buffer); 425 | } 426 | } 427 | } 428 | 429 | buffer.present().unwrap(); 430 | } 431 | 432 | /// Initialize the window. This gives a transparent, borderless window that's always on top and can be clicked through. 433 | fn init_window(active_event_loop: &ActiveEventLoop, settings: &mut Settings) -> Window { 434 | let window_attributes = Window::default_attributes() 435 | .with_visible(false) // things get very buggy on Windows if you default the window to invisible... 436 | .with_transparent(true) 437 | .with_decorations(false) 438 | .with_resizable(false) 439 | .with_title("Simple Crosshair Overlay") 440 | .with_position(PhysicalPosition::new(0, 0)) // can't determine monitor size until the window is created, so just use some dummy values 441 | .with_inner_size(PhysicalSize::new(1, 1)) // this might flicker so make it very tiny 442 | .with_active(false); 443 | 444 | #[cfg(target_os = "windows")] 445 | let window_attributes = { 446 | use winit::platform::windows::WindowAttributesExtWindows; 447 | window_attributes.with_drag_and_drop(false).with_skip_taskbar(true) 448 | }; 449 | 450 | #[cfg(target_os = "macos")] 451 | let window_attributes = { 452 | use winit::platform::macos::WindowAttributesExtMacOS; 453 | window_attributes 454 | .with_title_hidden(true) 455 | .with_titlebar_hidden(true) 456 | .with_has_shadow(false) 457 | }; 458 | 459 | let window = active_event_loop.create_window(window_attributes).unwrap(); 460 | 461 | // contrary to all my expectations this call appears to work reliably 462 | settings.set_window_position(&window); 463 | 464 | // this call is very fragile (read: shit) and sometimes simply doesn't do anything. 465 | // There's a fallback call up in the event loop that saves us when this fails. 466 | settings.set_window_size(&window); 467 | 468 | // once the window is ready, show it 469 | window.set_visible(true); 470 | 471 | // set these weirder settings AFTER the window is visible to avoid even more buggy Windows behavior 472 | // Windows particularly hates if you unset cursor_hittest while the window is hidden 473 | window.set_cursor_hittest(false).unwrap(); 474 | window.set_window_level(WindowLevel::AlwaysOnTop); 475 | window.set_cursor(CursorIcon::Crosshair); // Yo Dawg, I herd you like crosshairs so I put a crosshair in your crosshair so you can aim while you aim. 476 | 477 | window 478 | } 479 | -------------------------------------------------------------------------------- /src-lib/private/util/image/mod.rs: -------------------------------------------------------------------------------- 1 | // This file is part of simple-crosshair-overlay and is licenced under the GNU GPL v3.0. 2 | // See LICENSE file for full text. 3 | // Copyright © 2023-2025 Michael Ripley 4 | 5 | //! Image processing and color utilities 6 | 7 | use std::fs::File; 8 | use std::io; 9 | use std::io::BufReader; 10 | use std::path::Path; 11 | 12 | use png::ColorType; 13 | 14 | use crate::private::util::numeric::{DivCeil, DivFloor}; 15 | 16 | #[cfg(any(test, feature = "benchmark"))] 17 | pub mod precise; 18 | 19 | #[cfg(any(test, feature = "benchmark"))] 20 | pub mod naive; 21 | 22 | /// in-memory image representation 23 | pub struct Image { 24 | /// image width 25 | pub width: u32, 26 | /// image height 27 | pub height: u32, 28 | /// ARGB pixel color data 29 | pub data: Vec, 30 | } 31 | 32 | const COLOR_PICKER_NUM_SECTIONS: u8 = 6; 33 | /// floor(256/6) 34 | const COLOR_PICKER_SECTION_WIDTH: usize = 42; 35 | /// side-length of the color picker box 36 | pub const COLOR_PICKER_SIZE: usize = COLOR_PICKER_SECTION_WIDTH * (COLOR_PICKER_NUM_SECTIONS as usize); 37 | 38 | #[inline(always)] 39 | pub fn draw_color_picker(buffer: &mut [u32]) { 40 | const BUFFER_SIZE: usize = COLOR_PICKER_SIZE * COLOR_PICKER_SIZE; 41 | debug_assert_eq!( 42 | buffer.len(), 43 | BUFFER_SIZE, 44 | "draw_color_picker() passed buffer of wrong size" 45 | ); 46 | const MAX_VALUE: u8 = 255; 47 | 48 | const SECTION_0: usize = 0; 49 | const SECTION_1: usize = SECTION_0 + COLOR_PICKER_SECTION_WIDTH; 50 | const SECTION_2: usize = SECTION_1 + COLOR_PICKER_SECTION_WIDTH; 51 | const SECTION_3: usize = SECTION_2 + COLOR_PICKER_SECTION_WIDTH; 52 | const SECTION_4: usize = SECTION_3 + COLOR_PICKER_SECTION_WIDTH; 53 | const SECTION_5: usize = SECTION_4 + COLOR_PICKER_SECTION_WIDTH; 54 | 55 | let mut value = MAX_VALUE; 56 | for row in 0..COLOR_PICKER_SIZE { 57 | let mut ramp_up = 0; 58 | let mut ramp_down = 255; 59 | let row_offset = row * COLOR_PICKER_SIZE; 60 | for column_offset in 0..COLOR_PICKER_SECTION_WIDTH { 61 | // the old implementation calls `multiply_color_channels_u8` 3x more (once per pixel) 62 | let ramp_up_times_value = multiply_color_channels_u8(ramp_up, value); 63 | let ramp_down_times_value = multiply_color_channels_u8(ramp_down, value); 64 | 65 | // write six pixels at once 66 | buffer[row_offset + SECTION_0 + column_offset] = u32::from_le_bytes([0, ramp_up_times_value, value, 255]); 67 | buffer[row_offset + SECTION_1 + column_offset] = u32::from_le_bytes([0, value, ramp_down_times_value, 255]); 68 | buffer[row_offset + SECTION_2 + column_offset] = u32::from_le_bytes([ramp_up_times_value, value, 0, 255]); 69 | buffer[row_offset + SECTION_3 + column_offset] = u32::from_le_bytes([value, ramp_down_times_value, 0, 255]); 70 | buffer[row_offset + SECTION_4 + column_offset] = u32::from_le_bytes([value, 0, ramp_up_times_value, 255]); 71 | buffer[row_offset + SECTION_5 + column_offset] = u32::from_le_bytes([ramp_down_times_value, 0, value, 255]); 72 | 73 | ramp_up = ramp_up.wrapping_add(COLOR_PICKER_NUM_SECTIONS); 74 | ramp_down = ramp_down.wrapping_sub(COLOR_PICKER_NUM_SECTIONS); 75 | } 76 | value = value.wrapping_sub(1); 77 | } 78 | } 79 | 80 | /// calculate an ARGB color from picked coordinates from the color picker 81 | /// this color does NOT have premultiplied alpha 82 | pub fn hue_alpha_color_from_coordinates(x: usize, y: usize, width: usize, height: usize) -> u32 { 83 | debug_assert_eq!(width, COLOR_PICKER_SIZE); 84 | debug_assert_eq!(height, COLOR_PICKER_SIZE); 85 | x_y_to_argb_252(x as u8, y as u8) 86 | } 87 | 88 | /// see https://en.wikipedia.org/wiki/HSL_and_HSV#Color_conversion_formulae 89 | /// this is a HSV -> RGB conversion, except S is always set to 100%, which simplifies things 90 | pub fn hue_value_to_argb(hue: u8, value: u8) -> u32 { 91 | const MAX_COLOR: u8 = 255; 92 | // we need the ceiling of each of the 5 boundaries between the 6 sections 93 | const SECTION_1: u8 = 43; // 256/6*1 = 42.667 94 | const SECTION_2: u8 = 86; // 256/6*2 = 85.333 95 | const SECTION_3: u8 = 128; // 256/6*3 = 128.000 96 | const SECTION_4: u8 = 171; // 256/6*4 = 170.667 97 | const SECTION_5: u8 = 214; // 256/6*5 = 213.333 98 | 99 | // convert the hue into a nice sawtooth line going from 0->255 in each of the 6 sections 100 | let raw_hue = hue.wrapping_mul(6); 101 | 102 | let [r, g, b] = match hue { 103 | hue if hue < SECTION_1 => [value, multiply_color_channels_u8(raw_hue, value), 0], 104 | hue if hue < SECTION_2 => [multiply_color_channels_u8(MAX_COLOR - raw_hue, value), value, 0], 105 | hue if hue < SECTION_3 => [0, value, multiply_color_channels_u8(raw_hue, value)], 106 | hue if hue < SECTION_4 => [0, multiply_color_channels_u8(MAX_COLOR - raw_hue, value), value], 107 | hue if hue < SECTION_5 => [multiply_color_channels_u8(raw_hue, value), 0, value], 108 | _ => [value, 0, multiply_color_channels_u8(MAX_COLOR - raw_hue, value)], 109 | }; 110 | 111 | u32::from_le_bytes([b, g, r, MAX_COLOR]) 112 | } 113 | 114 | /// this is a HSV -> RGB conversion, except S and V are always set to 100%, which simplifies things 115 | pub fn hue_alpha_to_argb(hue: u8, alpha: u8) -> u32 { 116 | const MAX_COLOR: u8 = 255; 117 | // we need the ceiling of each of the 5 boundaries between the 6 sections 118 | const SECTION_1: u8 = 43; // 256/6*1 = 42.667 119 | const SECTION_2: u8 = 86; // 256/6*2 = 85.333 120 | const SECTION_3: u8 = 128; // 256/6*3 = 128.000 121 | const SECTION_4: u8 = 171; // 256/6*4 = 170.667 122 | const SECTION_5: u8 = 214; // 256/6*5 = 213.333 123 | 124 | // convert the hue into a nice sawtooth line going from 0->255 in each of the 6 sections 125 | let raw_hue = hue.wrapping_mul(6); 126 | 127 | let [r, g, b] = match hue { 128 | hue if hue < SECTION_1 => [MAX_COLOR, raw_hue, 0], 129 | hue if hue < SECTION_2 => [MAX_COLOR - raw_hue, MAX_COLOR, 0], 130 | hue if hue < SECTION_3 => [0, MAX_COLOR, raw_hue], 131 | hue if hue < SECTION_4 => [0, MAX_COLOR - raw_hue, MAX_COLOR], 132 | hue if hue < SECTION_5 => [raw_hue, 0, MAX_COLOR], 133 | _ => [MAX_COLOR, 0, MAX_COLOR - raw_hue], 134 | }; 135 | 136 | u32::from_le_bytes([b, g, r, alpha]) 137 | } 138 | 139 | /// Given color picker coordinates, get a crosshair color 140 | fn x_y_to_argb_252(x: u8, y: u8) -> u32 { 141 | const MAX_COLOR: u8 = 255; 142 | 143 | // we need the ceiling of each of the 5 boundaries between the 6 sections 144 | const SECTION_0: u8 = 0; 145 | const SECTION_1: u8 = SECTION_0 + COLOR_PICKER_SECTION_WIDTH as u8; 146 | const SECTION_2: u8 = SECTION_1 + COLOR_PICKER_SECTION_WIDTH as u8; 147 | const SECTION_3: u8 = SECTION_2 + COLOR_PICKER_SECTION_WIDTH as u8; 148 | const SECTION_4: u8 = SECTION_3 + COLOR_PICKER_SECTION_WIDTH as u8; 149 | const SECTION_5: u8 = SECTION_4 + COLOR_PICKER_SECTION_WIDTH as u8; 150 | 151 | // convert the hue into a nice sawtooth line going from 0->255 in each of the 6 sections 152 | let raw_hue = x.wrapping_mul(6); 153 | 154 | let [r, g, b] = match x { 155 | hue if hue < SECTION_1 => [MAX_COLOR, raw_hue, 0], 156 | hue if hue < SECTION_2 => [MAX_COLOR - raw_hue, MAX_COLOR, 0], 157 | hue if hue < SECTION_3 => [0, MAX_COLOR, raw_hue], 158 | hue if hue < SECTION_4 => [0, MAX_COLOR - raw_hue, MAX_COLOR], 159 | hue if hue < SECTION_5 => [raw_hue, 0, MAX_COLOR], 160 | _ => [MAX_COLOR, 0, MAX_COLOR - raw_hue], 161 | }; 162 | 163 | u32::from_le_bytes([b, g, r, MAX_COLOR - y]) 164 | } 165 | 166 | /// Convert BE RGBA to LE ARGB, premultiplying alpha where required by the target platform. 167 | #[inline(always)] 168 | #[cfg(target_os = "windows")] 169 | fn rgba_to_argb(rgba_color: u32) -> u32 { 170 | // OPTIMIZATION NOTE: this could benefit from SIMD. However, it only happens when the user loads 171 | // a PNG from disk. So not only is this infrequent, the latency of doing all the number crunching 172 | // is going to be completely overshadowed by the incredible slowness of reading from disk. Not 173 | // worth shaving microseconds off a millisecond-latency operation. 174 | 175 | // The PNG data is currently laid out as RGBA in BE order. 176 | // From a LE perspective, this means the actual data in the u32 is ABGR 177 | // Therefore, if we read this in LE order the bytes go RGBA. 178 | let [r, g, b, a] = rgba_color.to_le_bytes(); 179 | 180 | // We want to pack the data back into ARGB. Provided in LE order that's BGRA. 181 | u32::from_le_bytes([ 182 | multiply_color_channels_u8(b, a), 183 | multiply_color_channels_u8(g, a), 184 | multiply_color_channels_u8(r, a), 185 | a, 186 | ]) 187 | } 188 | 189 | /// Convert BE RGBA to LE ARGB, premultiplying alpha where required by the target platform. 190 | #[inline(always)] 191 | #[cfg(not(target_os = "windows"))] 192 | fn rgba_to_argb(rgba_color: u32) -> u32 { 193 | // The PNG data is currently laid out as RGBA in BE order. 194 | // From a LE perspective, this means the actual data in the u32 is ABGR 195 | // Therefore, if we read this in LE order the bytes go RGBA. 196 | let [r, g, b, a] = rgba_color.to_le_bytes(); 197 | 198 | // We want to pack the data back into ARGB. Provided in LE order that's BGRA. 199 | u32::from_le_bytes([b, g, r, a]) 200 | } 201 | 202 | /// Premultiply alpha if required by current platform. On this platform this performs the premultiplication. 203 | #[cfg(target_os = "windows")] 204 | pub fn premultiply_alpha(color: u32) -> u32 { 205 | let [b, g, r, a] = color.to_le_bytes(); 206 | u32::from_le_bytes([ 207 | multiply_color_channels_u8(b, a), 208 | multiply_color_channels_u8(g, a), 209 | multiply_color_channels_u8(r, a), 210 | a, 211 | ]) 212 | } 213 | 214 | /// Premultiply alpha if required by current platform. On this platform this is a no-op. 215 | #[cfg(not(target_os = "windows"))] 216 | pub fn premultiply_alpha(color: u32) -> u32 { 217 | color 218 | } 219 | 220 | /// calculates `a * b / 255` 221 | /// 222 | /// Note that this cannot be done with u8 precision alone, an intermediate step in the math can be 223 | /// up to 255 * 255 == 65025 inclusive. Example code on how to do this conversion casts to floats 224 | /// for the intermediate step, but that seems excessive when a u16 would do perfectly well and will 225 | /// even truncate towards zero just like a float -> u8 conversion. It's possible that using a wider 226 | /// type (like u32) might give more optimal assembly, but that's really the compiler's problem to 227 | /// worry about. 228 | /// 229 | /// - "Integer division rounds towards zero" [source](https://doc.rust-lang.org/reference/expressions/operator-expr.html#arithmetic-and-logical-binary-operators) 230 | /// - "Casting from a float to an integer will round the float towards zero" [source](https://doc.rust-lang.org/reference/expressions/operator-expr.html#numeric-cast) 231 | /// 232 | /// Finally, we can round to nearest int by simply adding 255 / 2 ~= 127 to the dividend 233 | #[inline(always)] 234 | pub fn multiply_color_channels_u8(a: u8, b: u8) -> u8 { 235 | const MAX_COLOR: u16 = 255; 236 | const HALF_COLOR: u16 = 127; 237 | 238 | ((a as u16 * b as u16 + HALF_COLOR) / MAX_COLOR) as u8 239 | } 240 | 241 | /// load a png file into an in-memory image 242 | pub fn load_png(path: T) -> io::Result> 243 | where 244 | T: AsRef, 245 | { 246 | static HUMONGOUS_PNG_MESSAGE: &str = "PNG does not fit into the memory space of the machine"; 247 | let file = File::open(path)?; 248 | let bufread = BufReader::new(file); 249 | let decoder = png::Decoder::new(bufread); 250 | let mut reader = decoder.read_info()?; 251 | 252 | // The PNG decoder wants a u8 buffer to store its RGBA data... but winit wants ARGB u32 data. 253 | // Here I make a buffer of the correct size to hold the reader's data, but as u32's instead of u8's. 254 | // This is done because it's not safe to cast a &[u8] into a &[u32] due to possible u32 misalignment, 255 | // however it is completely safe to cast a &[u32] into a &[u8]. 256 | const RATIO: usize = size_of::() / size_of::(); // this is going to be 4 always, but it's good practice to not use a magic number here 257 | let mut buf_as_u32: Vec = Vec::with_capacity( 258 | reader 259 | .output_buffer_size() 260 | .expect(HUMONGOUS_PNG_MESSAGE) 261 | .div_ceil_placeholder(RATIO), 262 | ); 263 | #[allow(clippy::uninit_vec)] 264 | unsafe { 265 | // there is no requirement I send a zeroed buffer to the PNG decoding library. 266 | buf_as_u32.set_len(buf_as_u32.capacity()); 267 | } 268 | 269 | // a little check to make sure div_ceil isn't fucked up. Which it's definitely not, because I eyeballed it really sternly. 270 | debug_assert!( 271 | buf_as_u32.len() * RATIO >= reader.output_buffer_size().expect(HUMONGOUS_PNG_MESSAGE), 272 | "buffer was unexpectedly not large enough for image decode" 273 | ); 274 | 275 | // I'm just transmuting color data between u32 and [u8; 4] packing. No risk. 276 | let buf_as_u8: &mut [u8] = unsafe { 277 | if let ([], aligned, []) = buf_as_u32.align_to_mut() { 278 | aligned 279 | } else { 280 | panic!("couldn't align u32 buf to u8") 281 | } 282 | }; 283 | 284 | let info = reader.next_frame(buf_as_u8)?; 285 | 286 | if info.color_type != ColorType::Rgba { 287 | Err(io::Error::new( 288 | io::ErrorKind::InvalidInput, 289 | format!( 290 | "PNG was in {:?} format. Only {:?} format is supported. Please re-save your PNG in the required format.", 291 | info.color_type, 292 | ColorType::Rgba 293 | ), 294 | ))?; 295 | } 296 | 297 | // post-process color layout in each pixel 298 | buf_as_u32 299 | .iter_mut() 300 | .for_each(|pixel| *pixel = rgba_to_argb(pixel.to_owned())); 301 | 302 | let image = Image { 303 | width: info.width, 304 | height: info.height, 305 | data: buf_as_u32, 306 | }; 307 | 308 | Ok(Box::new(image)) 309 | } 310 | 311 | /// calculate the coordinates of the center of a rectangle. 312 | /// `x` and `y` are the coordinates of the top left corner. 313 | /// `width` and `height` are the dimensions of the rectangle. 314 | /// Rounding is done towards -Infinity. 315 | /// I haven't thought about what happens if `width` or `height` are negative, so you'd better keep them positive. 316 | #[inline(always)] 317 | pub fn rectangle_center(x: i32, y: i32, width: i32, height: i32) -> (i32, i32) { 318 | (x + width.div_floor_placeholder(2), y + height.div_floor_placeholder(2)) 319 | } 320 | 321 | #[cfg(test)] 322 | mod test_pixel_format { 323 | use super::*; 324 | 325 | /// simply confirm that to_le_bytes does what I expect, as the documentation is slightly vague 326 | #[test] 327 | fn test_le() { 328 | let b0 = 0u8; 329 | let b1 = 1u8; 330 | let b2 = 2u8; 331 | let b3 = 3u8; 332 | 333 | let u0 = b0 as u32; 334 | let u1 = b1 as u32; 335 | let u2 = b2 as u32; 336 | let u3 = b3 as u32; 337 | 338 | // a u32 made up of [b3, b2, b1, b0] 339 | let packed_u32 = (u3 << 24) + (u2 << 16) + (u1 << 8) + u0; 340 | 341 | let bytes = packed_u32.to_le_bytes(); 342 | assert_eq!(&bytes, &[b0, b1, b2, b3]); 343 | } 344 | 345 | #[test] 346 | fn test_pixel_format_conversion() { 347 | let alpha = 255u8; 348 | let red = 20u8; 349 | let green = 40u8; 350 | let blue = 60u8; 351 | let png_data = u32::from_le_bytes([red, green, blue, alpha]); // laid out backwards in memory, so we write it forwards in LE 352 | let argb_data = rgba_to_argb(png_data); 353 | assert_eq!(argb_data.to_le_bytes(), [blue, green, red, alpha]); // laid out properly in memory, so we write it backwards in LE 354 | } 355 | 356 | /// This should be a no-op. 357 | #[test] 358 | fn test_premultiply_alpha_noop() { 359 | assert_eq!(multiply_color_channels_u8(255, 255), 255); 360 | assert_eq!(multiply_color_channels_u8(127, 255), 127); 361 | assert_eq!(multiply_color_channels_u8(0, 255), 0); 362 | } 363 | 364 | /// This should half the value of each color. 365 | #[test] 366 | fn test_premultiply_alpha_half() { 367 | assert_eq!(multiply_color_channels_u8(255, 127), 127); 368 | assert_eq!(multiply_color_channels_u8(127, 127), 63); 369 | assert_eq!(multiply_color_channels_u8(0, 127), 0); 370 | } 371 | 372 | /// This should zero all the color data. 373 | #[test] 374 | fn test_premultiply_alpha_zero() { 375 | assert_eq!(multiply_color_channels_u8(255, 0), 0); 376 | assert_eq!(multiply_color_channels_u8(127, 0), 0); 377 | assert_eq!(multiply_color_channels_u8(0, 0), 0); 378 | } 379 | 380 | /// make sure our alpha premultiplication always rounds to the nearest u8 381 | #[test] 382 | fn premultiply_alpha_rounding() { 383 | // test for some every `c` for various predefined `a` 384 | // what's important here is to contrive c*a/255 for results that will round in different ways while avoiding an exhaustive test, as that'd be slow 385 | for c in 0..=255 { 386 | for a in [ 387 | 0, 1, 2, 3, 4, 20, 30, 40, 50, 60, 61, 62, 63, 64, 77, 127, 128, 254, 255, 388 | ] { 389 | let precise_result = precise::multiply_color_channels_u8(c, a); 390 | let actual_result = multiply_color_channels_u8(c, a); 391 | assert_eq!(actual_result, precise_result, "mismatch for c={c} a={a}") 392 | } 393 | } 394 | } 395 | } 396 | 397 | #[cfg(test)] 398 | mod test_rectangle_center { 399 | use super::*; 400 | 401 | #[test] 402 | fn test_rectangle_center_0_corner() { 403 | assert_eq!(rectangle_center(0, 0, 100, 100), (50, 50)); 404 | } 405 | 406 | #[test] 407 | fn test_rectangle_center_0_corner_odd_size() { 408 | assert_eq!(rectangle_center(0, 0, 101, 101), (50, 50)); 409 | } 410 | 411 | #[test] 412 | fn test_rectangle_center_even_corner() { 413 | assert_eq!(rectangle_center(2, 2, 96, 96), (50, 50)); 414 | } 415 | 416 | #[test] 417 | fn test_rectangle_center_even_corner_odd_size() { 418 | assert_eq!(rectangle_center(2, 2, 97, 97), (50, 50)); 419 | } 420 | 421 | #[test] 422 | fn test_rectangle_center_negative_corner() { 423 | assert_eq!(rectangle_center(-2, -2, 104, 104), (50, 50)); 424 | } 425 | 426 | #[test] 427 | fn test_rectangle_center_negative_corner_odd_size() { 428 | assert_eq!(rectangle_center(-2, -2, 105, 105), (50, 50)); 429 | } 430 | 431 | /// my actual 1080p monitor setup 432 | #[test] 433 | fn test_1080p_top_centered() { 434 | assert_eq!(rectangle_center(397, -1080, 1920, 1080), (397 + 960, -1080 + 540)); 435 | } 436 | } 437 | 438 | #[cfg(test)] 439 | mod test_color_picker { 440 | use super::*; 441 | 442 | fn color_error(actual: u32, expected: u32) -> f64 { 443 | if actual == expected { 444 | return 0.0; 445 | } 446 | 447 | let [b1, g1, r1, a1] = actual.to_le_bytes(); 448 | let [b2, g2, r2, a2] = expected.to_le_bytes(); 449 | 450 | // calculate deltas 451 | let b = b1 as f64 - b2 as f64; 452 | let g = g1 as f64 - g2 as f64; 453 | let r = r1 as f64 - r2 as f64; 454 | let a = a1 as f64 - a2 as f64; 455 | 456 | // square the components 457 | let b = b * b; 458 | let g = g * g; 459 | let r = r * r; 460 | let a = a * a; 461 | 462 | // norm the components 463 | (b + g + r + a).sqrt() 464 | } 465 | 466 | #[test] 467 | fn test_hv_to_argb_hue_only() { 468 | let max_error = 5f64; 469 | 470 | for hue in 0..=255 { 471 | let actual_argb = hue_value_to_argb(hue, 255); 472 | let expected_argb = precise::hsv_to_argb(hue, 255, 255); 473 | let error = color_error(actual_argb, expected_argb); 474 | assert!( 475 | error <= max_error, 476 | "precise and optimized hv->argb differ: @ hue {hue}, {actual_argb:08X} != {expected_argb:08X}, error={error}" 477 | ); 478 | } 479 | } 480 | 481 | #[test] 482 | fn test_ha_to_argb_hue_only() { 483 | let max_error = 5f64; 484 | 485 | for hue in 0..=255 { 486 | let actual_argb = hue_alpha_to_argb(hue, 255); 487 | let expected_argb = precise::hsv_to_argb(hue, 255, 255); 488 | let error = color_error(actual_argb, expected_argb); 489 | assert!( 490 | error <= max_error, 491 | "precise and optimized ha->argb differ: @ hue {hue}, {actual_argb:08X} != {expected_argb:08X}, error={error}" 492 | ); 493 | } 494 | } 495 | 496 | #[test] 497 | fn test_hv_to_argb_value_only() { 498 | let max_error = 5f64; 499 | 500 | for value in 0..=255 { 501 | let actual_argb = hue_value_to_argb(255, value); 502 | let expected_argb = precise::hsv_to_argb(255, 255, value); 503 | let error = color_error(actual_argb, expected_argb); 504 | assert!( 505 | error <= max_error, 506 | "precise and optimized hv->argb differ: @ value {value}, {actual_argb:08X} != {expected_argb:08X}, error={error}" 507 | ); 508 | } 509 | } 510 | 511 | /// make sure the optimized color picker behaves generally as expected 512 | #[test] 513 | fn test_optimized_color_picker() { 514 | const BUFFER_DIMENSION: usize = 252; 515 | const BUFFER_SIZE: usize = BUFFER_DIMENSION * BUFFER_DIMENSION; 516 | 517 | let mut buffer = vec![0; BUFFER_SIZE]; 518 | draw_color_picker(&mut buffer); 519 | 520 | // make sure various pixels are nonzero 521 | assert_ne!(buffer[0], 0, "first pixel should be set"); 522 | assert_ne!(buffer[buffer.len() - 1], 0, "last pixel should be set"); 523 | 524 | check_picked_color(&buffer, 0, 0); 525 | check_picked_color(&buffer, 0, 252 - 1); 526 | check_picked_color(&buffer, 252 - 1, 0); 527 | check_picked_color(&buffer, 252 - 1, 252 - 1); 528 | } 529 | 530 | #[derive(Debug)] 531 | struct HsvColor { 532 | h: f64, 533 | s: f64, 534 | v: f64, 535 | } 536 | 537 | impl PartialEq for HsvColor { 538 | fn eq(&self, other: &Self) -> bool { 539 | // values range from 0 to 1, but ultimately they come from u8 precision, so allow only a u8's worth of rounding error 540 | const MAX_ERROR: f64 = 0.49 / 255.0; 541 | (self.h - other.h).abs() < MAX_ERROR 542 | && (self.s - other.s).abs() < MAX_ERROR 543 | && (self.v - other.v).abs() < MAX_ERROR 544 | } 545 | } 546 | 547 | impl Eq for HsvColor {} 548 | 549 | fn rgb_to_hsv_precise(color: u32) -> HsvColor { 550 | const MAX_COLOR: f64 = 255.0; 551 | let [b, g, r, _a] = color.to_le_bytes(); 552 | let r = r as f64 / MAX_COLOR; 553 | let g = g as f64 / MAX_COLOR; 554 | let b = b as f64 / MAX_COLOR; 555 | 556 | let x_max = r.max(g.max(b)); // value 557 | let x_min = r.min(g.min(b)); 558 | let c = x_max - x_min; 559 | 560 | let h = if c == 0.0 { 561 | 0.0 562 | } else if x_max == r { 563 | (((g - b) / c) % 6.0) / 60.0 564 | } else if x_max == g { 565 | (((b - r) / c) + 2.0) / 60.0 566 | } else { 567 | // x_max must therefore equal b 568 | (((r - g) / c) + 4.0) / 60.0 569 | }; 570 | 571 | let s = if x_max == 0.0 { 0.0 } else { c / x_max }; 572 | 573 | HsvColor { h, s, v: x_max } 574 | } 575 | 576 | fn check_picked_color(buffer: &[u32], x: usize, y: usize) { 577 | const BUFFER_DIMENSION: usize = 252; 578 | 579 | let picker_color = rgb_to_hsv_precise(buffer[y * BUFFER_DIMENSION + x]); 580 | let HsvColor { h, s: _, v } = picker_color; 581 | let expected_color = HsvColor { h, s: 1.0, v: 1.0 }; 582 | let expected_alpha = (v * 255.0).round() as u8; 583 | 584 | let calculated_color = x_y_to_argb_252(x as u8, y as u8); 585 | let actual_color = rgb_to_hsv_precise(calculated_color); 586 | let [_, _, _, actual_alpha] = calculated_color.to_le_bytes(); 587 | assert_eq!(expected_color, actual_color, "color did not match at ({x}, {y})"); 588 | assert_eq!(expected_alpha, actual_alpha, "alpha did not match at ({x}, {y})"); 589 | } 590 | } 591 | 592 | #[cfg(test)] 593 | mod test_png { 594 | use super::*; 595 | 596 | #[test] 597 | fn test_load_png() { 598 | load_png("tests/resources/test.png").unwrap(); 599 | } 600 | } 601 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | --------------------------------------------------------------------------------