Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
280 changes: 129 additions & 151 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ simple-logging = "2.0.2"
xml = "0.8.10"

[dependencies.windows]
version = "0.58.0"
version = "0.61.3"
features = [
"Win32_Foundation",
"Win32_UI_WindowsAndMessaging",
Expand Down
6 changes: 3 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl App {
fn eventloop() -> Result<()> {
let mut message = MSG::default();
loop {
let ret = unsafe { GetMessageW(&mut message, HWND::default(), 0, 0) };
let ret = unsafe { GetMessageW(&mut message, None, 0, 0) };
match ret.0 {
-1 => {
unsafe { GetLastError() }.ok()?;
Expand Down Expand Up @@ -152,7 +152,7 @@ impl App {
CW_USEDEFAULT,
None,
None,
hinstance,
Some(hinstance.into()),
None,
)
}
Expand All @@ -178,7 +178,7 @@ impl App {
std::thread::sleep(std::time::Duration::from_secs(3));
let _ = unsafe {
PostMessageW(
HWND(hwnd as _),
Some(HWND(hwnd as _)),
WM_USER_REGISTER_TRAYICON,
WPARAM(0),
LPARAM(0),
Expand Down
28 changes: 16 additions & 12 deletions src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ impl KeyboardListener {
let hook = unsafe {
let hinstance = { GetModuleHandleW(None) }
.map_err(|err| anyhow!("Failed to get module handle, {err}"))?;
SetWindowsHookExW(WH_KEYBOARD_LL, Some(keyboard_proc), hinstance, 0)
SetWindowsHookExW(
WH_KEYBOARD_LL,
Some(keyboard_proc),
Some(hinstance.into()),
0,
)
}
.map_err(|err| anyhow!("Failed to set windows hook, {err}"))?;
info!("keyboard listener start");
Expand Down Expand Up @@ -91,13 +96,9 @@ unsafe extern "system" fn keyboard_proc(code: i32, w_param: WPARAM, l_param: LPA
if PREVIOUS_KEYCODE == state.hotkey.code {
let id = state.hotkey.id;
if id == SWITCH_APPS_HOTKEY_ID {
unsafe {
SendMessageW(WINDOW, WM_USER_SWITCH_APPS_DONE, WPARAM(0), LPARAM(0))
};
unsafe { SendMessageW(WINDOW, WM_USER_SWITCH_APPS_DONE, None, None) };
} else if id == SWITCH_WINDOWS_HOTKEY_ID {
unsafe {
SendMessageW(WINDOW, WM_USER_SWITCH_WINDOWS_DONE, WPARAM(0), LPARAM(0))
};
unsafe { SendMessageW(WINDOW, WM_USER_SWITCH_WINDOWS_DONE, None, None) };
}
}
}
Expand All @@ -111,21 +112,24 @@ unsafe extern "system" fn keyboard_proc(code: i32, w_param: WPARAM, l_param: LPA
let reverse = if IS_SHIFT_PRESSED { 1 } else { 0 };
if id == SWITCH_APPS_HOTKEY_ID {
unsafe {
SendMessageW(WINDOW, WM_USER_SWITCH_APPS, WPARAM(0), LPARAM(reverse))
SendMessageW(WINDOW, WM_USER_SWITCH_APPS, None, Some(LPARAM(reverse)))
};
PREVIOUS_KEYCODE = scan_code;
return LRESULT(1);
} else if id == SWITCH_WINDOWS_HOTKEY_ID && !IS_FOREGROUND_IN_BLACKLIST {
unsafe {
SendMessageW(WINDOW, WM_USER_SWITCH_WINDOWS, WPARAM(0), LPARAM(reverse))
SendMessageW(
WINDOW,
WM_USER_SWITCH_WINDOWS,
None,
Some(LPARAM(reverse)),
)
};
PREVIOUS_KEYCODE = scan_code;
return LRESULT(1);
}
} else if scan_code == 0x01 && id == SWITCH_APPS_HOTKEY_ID {
unsafe {
SendMessageW(WINDOW, WM_USER_SWITCH_APPS_CANCEL, WPARAM(0), LPARAM(0))
};
unsafe { SendMessageW(WINDOW, WM_USER_SWITCH_APPS_CANCEL, None, None) };
PREVIOUS_KEYCODE = scan_code;
return LRESULT(1);
}
Expand Down
40 changes: 20 additions & 20 deletions src/painter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use windows::Win32::{
CreateCompatibleBitmap, CreateCompatibleDC, CreateRoundRectRgn, CreateSolidBrush,
DeleteDC, DeleteObject, FillRect, FillRgn, GetDC, ReleaseDC, SelectObject,
SetStretchBltMode, StretchBlt, AC_SRC_ALPHA, AC_SRC_OVER, BLENDFUNCTION, HALFTONE,
HBITMAP, HBRUSH, HDC, HPALETTE, SRCCOPY,
HBITMAP, HDC, HPALETTE, SRCCOPY,
},
GdiPlus::{
FillModeAlternate, GdipAddPathArc, GdipClosePathFigure, GdipCreateBitmapFromHBITMAP,
Expand Down Expand Up @@ -59,7 +59,7 @@ impl GdiAAPainter {
check_error(|| unsafe { GdiplusStartup(&mut token, &startup_input, std::ptr::null_mut()) })
.context("Failed to initialize GDI+")?;

let hdc_screen = unsafe { GetDC(hwnd) };
let hdc_screen = unsafe { GetDC(Some(hwnd)) };
let rounded_corner = is_win11();

Ok(Self {
Expand Down Expand Up @@ -93,9 +93,9 @@ impl GdiAAPainter {
let (fg_color, bg_color) = theme_color(is_light_theme());

unsafe {
let hdc_mem = CreateCompatibleDC(hdc_screen);
let hdc_mem = CreateCompatibleDC(Some(hdc_screen));
let bitmap_mem = CreateCompatibleBitmap(hdc_screen, width, height);
SelectObject(hdc_mem, bitmap_mem);
SelectObject(hdc_mem, bitmap_mem.into());

let mut graphics = GpGraphics::default();
let mut graphics_ptr: *mut GpGraphics = &mut graphics;
Expand Down Expand Up @@ -167,13 +167,13 @@ impl GdiAAPainter {
};
let _ = UpdateLayeredWindow(
hwnd,
hdc_screen,
Some(hdc_screen),
Some(&POINT { x, y }),
Some(&SIZE {
cx: width,
cy: height,
}),
hdc_mem,
Some(hdc_mem),
Some(&POINT::default()),
COLORREF(0),
Some(&blend),
Expand All @@ -185,8 +185,8 @@ impl GdiAAPainter {
GdipDeletePen(bg_pen_ptr);
GdipDeleteGraphics(graphics_ptr);

let _ = DeleteObject(bitmap_icons);
let _ = DeleteObject(bitmap_mem);
let _ = DeleteObject(bitmap_icons.into());
let _ = DeleteObject(bitmap_mem.into());
let _ = DeleteDC(hdc_mem);
}

Expand All @@ -195,7 +195,7 @@ impl GdiAAPainter {
}
unsafe {
let _ = ShowWindow(self.hwnd, SW_SHOW);
let _ = SetFocus(self.hwnd);
let _ = SetFocus(Some(self.hwnd));
}
self.show = true;
}
Expand All @@ -211,7 +211,7 @@ impl GdiAAPainter {
impl Drop for GdiAAPainter {
fn drop(&mut self) {
unsafe {
ReleaseDC(self.hwnd, self.hdc_screen);
ReleaseDC(Some(self.hwnd), self.hdc_screen);
GdiplusShutdown(self.token);
}
}
Expand Down Expand Up @@ -319,13 +319,13 @@ fn draw_icons(
let scaled_icon_outer_size = scaled_icon_inner_size + scaled_border_size * 2;

unsafe {
let hdc_tmp = CreateCompatibleDC(hdc_screen);
let hdc_tmp = CreateCompatibleDC(Some(hdc_screen));
let bitmap_tmp = CreateCompatibleBitmap(hdc_screen, width, height);
SelectObject(hdc_tmp, bitmap_tmp);
SelectObject(hdc_tmp, bitmap_tmp.into());

let hdc_scaled = CreateCompatibleDC(hdc_screen);
let hdc_scaled = CreateCompatibleDC(Some(hdc_screen));
let bitmap_scaled = CreateCompatibleBitmap(hdc_screen, scaled_width, scaled_height);
SelectObject(hdc_scaled, bitmap_scaled);
SelectObject(hdc_scaled, bitmap_scaled.into());

let fg_brush = CreateSolidBrush(COLORREF(fg_color));
let bg_brush = CreateSolidBrush(COLORREF(bg_color));
Expand Down Expand Up @@ -355,7 +355,7 @@ fn draw_icons(
scaled_corner_radius,
);
let _ = FillRgn(hdc_scaled, rgn, fg_brush);
let _ = DeleteObject(rgn);
let _ = DeleteObject(rgn.into());
}

let cx = scaled_border_size + scaled_icon_outer_size * (i as i32);
Expand All @@ -367,7 +367,7 @@ fn draw_icons(
scaled_icon_inner_size,
scaled_icon_inner_size,
0,
HBRUSH::default(),
None,
DI_NORMAL,
);
}
Expand All @@ -379,17 +379,17 @@ fn draw_icons(
0,
width,
height,
hdc_scaled,
Some(hdc_scaled),
0,
0,
scaled_width,
scaled_height,
SRCCOPY,
);

let _ = DeleteObject(fg_brush);
let _ = DeleteObject(bg_brush);
let _ = DeleteObject(bitmap_scaled);
let _ = DeleteObject(fg_brush.into());
let _ = DeleteObject(bg_brush.into());
let _ = DeleteObject(bitmap_scaled.into());
let _ = DeleteDC(hdc_scaled);
let _ = DeleteDC(hdc_tmp);

Expand Down
2 changes: 1 addition & 1 deletion src/trayicon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl TrayIcon {
TPM_LEFTALIGN | TPM_BOTTOMALIGN,
cursor.x,
cursor.y,
0,
None,
hwnd,
None,
)
Expand Down
2 changes: 1 addition & 1 deletion src/utils/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn is_running_as_admin() -> Result<bool> {
}

pub fn is_process_elevated(pid: u32) -> Option<bool> {
let handle = unsafe { OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, None, pid) }.ok()?;
let handle = unsafe { OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, pid) }.ok()?;
is_elevated(handle).ok()
}

Expand Down
18 changes: 9 additions & 9 deletions src/utils/app_icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use indexmap::IndexMap;
use windows::{
core::PCWSTR,
Win32::{
Foundation::{COLORREF, HWND, TRUE, WPARAM},
Foundation::{COLORREF, HWND, WPARAM},
Graphics::Gdi::{
CreateCompatibleDC, DeleteDC, DeleteObject, GetDC, GetObjectW, GetPixel, ReleaseDC,
SelectObject, BITMAP, HGDIOBJ,
Expand Down Expand Up @@ -154,7 +154,7 @@ pub fn load_image_as_hicon<T: AsRef<Path>>(image_path: T) -> Option<HICON> {
let mut logo_file = File::open(image_path).ok()?;
let mut buffer = vec![];
logo_file.read_to_end(&mut buffer).ok()?;
unsafe { CreateIconFromResourceEx(&buffer, TRUE, 0x30000, 100, 100, LR_DEFAULTCOLOR) }.ok()
unsafe { CreateIconFromResourceEx(&buffer, true, 0x30000, 100, 100, LR_DEFAULTCOLOR) }.ok()
}
}

Expand All @@ -163,7 +163,7 @@ fn fallback_icon() -> HICON {
}

pub fn get_window_icon(hwnd: HWND) -> Option<HICON> {
let ret = unsafe { SendMessageW(hwnd, WM_GETICON, WPARAM(ICON_BIG as _), None) };
let ret = unsafe { SendMessageW(hwnd, WM_GETICON, Some(WPARAM(ICON_BIG as _)), None) };
if ret.0 != 0 {
return Some(HICON(ret.0 as _));
}
Expand Down Expand Up @@ -239,18 +239,18 @@ fn get_icon_size(hicon: HICON) -> Option<(i32, i32)> {

let mut bmp = BITMAP::default();
if 0 == GetObjectW(
icon_info.hbmColor,
icon_info.hbmColor.into(),
std::mem::size_of::<BITMAP>() as i32,
Some(&mut bmp as *mut _ as *mut _),
) {
let _ = DeleteObject(icon_info.hbmColor);
let _ = DeleteObject(icon_info.hbmMask);
let _ = DeleteObject(icon_info.hbmColor.into());
let _ = DeleteObject(icon_info.hbmMask.into());
return None;
}

let (width, height) = (bmp.bmWidth, bmp.bmHeight);
let hdc = GetDC(None);
let hmemdc = CreateCompatibleDC(hdc);
let hmemdc = CreateCompatibleDC(Some(hdc));
let old_bitmap = SelectObject(hmemdc, HGDIOBJ(icon_info.hbmColor.0 as _));

let (mut min_x, mut min_y, mut max_x, mut max_y) = (width, height, 0, 0);
Expand Down Expand Up @@ -278,8 +278,8 @@ fn get_icon_size(hicon: HICON) -> Option<(i32, i32)> {
SelectObject(hmemdc, old_bitmap);
let _ = DeleteDC(hmemdc);
ReleaseDC(None, hdc);
let _ = DeleteObject(icon_info.hbmColor);
let _ = DeleteObject(icon_info.hbmMask);
let _ = DeleteObject(icon_info.hbmColor.into());
let _ = DeleteObject(icon_info.hbmMask.into());

Some((max_x - min_x + 1, max_y - min_y + 1))
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/regedit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl RegKey {
RegOpenKeyExW(
HKEY_CURRENT_USER,
subkey,
0,
None,
KEY_ALL_ACCESS,
&mut hkey as *mut _,
)
Expand Down Expand Up @@ -90,7 +90,7 @@ impl RegKey {
}

pub fn set_value(&self, value: &[u8]) -> Result<()> {
unsafe { RegSetValueExW(self.hkey, self.name, 0, REG_SZ, Some(value)) }
unsafe { RegSetValueExW(self.hkey, self.name, None, REG_SZ, Some(value)) }
.ok()
.map_err(|err| anyhow!("Fail to write reg value, {:?}", err))?;
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions src/utils/scheduled_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ fn get_author_and_userid() -> WindowsResult<(String, String)> {
LookupAccountSidW(
None,
user_sid,
PWSTR(name.as_mut_ptr()),
Some(PWSTR(name.as_mut_ptr())),
&mut name_len,
PWSTR(domain.as_mut_ptr()),
Some(PWSTR(domain.as_mut_ptr())),
&mut domain_len,
&mut sid_name_use,
)?
Expand Down
4 changes: 2 additions & 2 deletions src/utils/single_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::to_wstring;
use anyhow::{anyhow, Result};
use windows::core::PCWSTR;
use windows::Win32::{
Foundation::{CloseHandle, BOOL, ERROR_ALREADY_EXISTS, HANDLE},
Foundation::{CloseHandle, ERROR_ALREADY_EXISTS, HANDLE},
System::Threading::{CreateMutexW, ReleaseMutex},
};

Expand All @@ -19,7 +19,7 @@ impl SingleInstance {
/// Returns a new SingleInstance object.
pub fn create(name: &str) -> Result<Self> {
let name = to_wstring(name);
let handle = unsafe { CreateMutexW(None, BOOL(1), PCWSTR(name.as_ptr())) }
let handle = unsafe { CreateMutexW(None, true, PCWSTR(name.as_ptr())) }
.map_err(|err| anyhow!("Fail to setup single instance, {err}"))?;
let handle =
if windows::core::Error::from_win32().code() == ERROR_ALREADY_EXISTS.to_hresult() {
Expand Down
6 changes: 3 additions & 3 deletions src/utils/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use crate::utils::is_process_elevated;
use anyhow::{anyhow, Result};
use indexmap::IndexMap;
use std::{ffi::c_void, mem::size_of, path::PathBuf};
use windows::core::PWSTR;
use windows::core::{BOOL, PWSTR};
use windows::Win32::{
Foundation::{BOOL, HWND, LPARAM, MAX_PATH, POINT, RECT},
Foundation::{HWND, LPARAM, MAX_PATH, POINT, RECT},
Graphics::{
Dwm::{DwmGetWindowAttribute, DWMWA_CLOAKED, DWM_CLOAKED_SHELL},
Gdi::{GetMonitorInfoW, MonitorFromPoint, MONITORINFO, MONITOR_DEFAULTTONEAREST},
Expand Down Expand Up @@ -118,7 +118,7 @@ pub fn get_window_pid(hwnd: HWND) -> u32 {
}

pub fn get_module_path(pid: u32) -> Option<String> {
let handle = unsafe { OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, None, pid) }.ok()?;
let handle = unsafe { OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, pid) }.ok()?;
let mut len: u32 = MAX_PATH;
let mut name = vec![0u16; len as usize];
let ret = unsafe {
Expand Down
2 changes: 1 addition & 1 deletion tools/inspect-windows/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ indexmap = "2.2.3"
window-switcher = { path = "../.."}

[dependencies.windows]
version = "0.58.0"
version = "0.61.3"
features = [
"Win32_Foundation",
"Win32_UI_WindowsAndMessaging",
Expand Down
3 changes: 2 additions & 1 deletion tools/inspect-windows/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use anyhow::{Context, Result};
use window_switcher::utils::*;

use windows::Win32::Foundation::{BOOL, HWND, LPARAM};
use windows::core::BOOL;
use windows::Win32::Foundation::{HWND, LPARAM};
use windows::Win32::Graphics::Dwm::{DWM_CLOAKED_APP, DWM_CLOAKED_INHERITED, DWM_CLOAKED_SHELL};
use windows::Win32::UI::WindowsAndMessaging::{EnumWindows, GetWindow, GW_OWNER};

Expand Down
Loading