From 39d1a00803a51e7f61dfc18b123448c477a61623 Mon Sep 17 00:00:00 2001 From: user726687 <98432183+user726687@users.noreply.github.com> Date: Fri, 14 Jun 2024 06:05:10 -0700 Subject: [PATCH] update build options and add mutex and gitignore --- .gitignore | 2 ++ build.zig | 64 +++--------------------------------------------------- finger.zig | 36 +++++++++++++++--------------- 3 files changed, 24 insertions(+), 78 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9c10773 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +zig-cache +zig-out \ No newline at end of file diff --git a/build.zig b/build.zig index 2f1e398..4842bfc 100644 --- a/build.zig +++ b/build.zig @@ -1,70 +1,12 @@ const std = @import("std"); - -// Although this function looks imperative, note that its job is to -// declaratively construct a build graph that will be executed by an external -// runner. pub fn build(b: *std.Build) void { - // Standard target options allows the person running `zig build` to choose - // what target to build for. Here we do not override the defaults, which - // means any target is allowed, and the default is native. Other options - // for restricting supported target set are available. - const target = b.standardTargetOptions(.{}); - - // Standard optimization options allow the person running `zig build` to select - // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not - // set a preferred release mode, allowing the user to decide how to optimize. - const optimize = b.standardOptimizeOption(.{.preferred_optimize_mode=.ReleaseFast}); - const exe = b.addExecutable(.{ .name = "finger", - // In this case the main source file is merely a path, however, in more - // complicated build scripts, this could be a generated file. .root_source_file = .{ .path = "finger.zig" }, - .target = target, - .optimize = optimize, + .target = b.standardTargetOptions(.{}), + .optimize = .ReleaseFast, + .strip = true, }); exe.subsystem = .Windows; - // This declares intent for the executable to be installed into the - // standard location when the user invokes the "install" step (the default - // step when running `zig build`). b.installArtifact(exe); - - // This *creates* a Run step in the build graph, to be executed when another - // step is evaluated that depends on it. The next line below will establish - // such a dependency. - const run_cmd = b.addRunArtifact(exe); - - // By making the run step depend on the install step, it will be run from the - // installation directory rather than directly from within the cache directory. - // This is not necessary, however, if the application depends on other installed - // files, this ensures they will be present and in the expected location. - run_cmd.step.dependOn(b.getInstallStep()); - - // This allows the user to pass arguments to the application in the build - // command itself, like this: `zig build run -- arg1 arg2 etc` - if (b.args) |args| { - run_cmd.addArgs(args); - } - - // This creates a build step. It will be visible in the `zig build --help` menu, - // and can be selected like this: `zig build run` - // This will evaluate the `run` step rather than the default, which is "install". - const run_step = b.step("run", "Run the app"); - run_step.dependOn(&run_cmd.step); - - // Creates a step for unit testing. This only builds the test executable - // but does not run it. - const unit_tests = b.addTest(.{ - .root_source_file = .{ .path = "src/main.zig" }, - .target = target, - .optimize = optimize, - }); - - const run_unit_tests = b.addRunArtifact(unit_tests); - - // Similar to creating the run step earlier, this exposes a `test` step to - // the `zig build --help` menu, providing a way for the user to request - // running the unit tests. - const test_step = b.step("test", "Run unit tests"); - test_step.dependOn(&run_unit_tests.step); } \ No newline at end of file diff --git a/finger.zig b/finger.zig index 113730f..8e067c1 100644 --- a/finger.zig +++ b/finger.zig @@ -1,7 +1,11 @@ const std = @import("std"); const win = std.os.windows; const DELTA_THRESHOLD = 48; -pub fn main() void { + +pub fn main() void { + _ = CreateMutexA(null, 1, "ThreeFingerDrag") orelse return; + if (.SUCCESS != win.kernel32.GetLastError()) return; + const wnd_class = RegisterClassA( &WNDCLASSA { .lpfnWndProc = wndProc, @@ -33,7 +37,7 @@ pub fn main() void { } } -fn wndProc(hwnd: HWND, uMsg: u32, wParam: usize, lParam: isize) callconv(WINAPI) isize { +fn wndProc(hwnd: win.HWND, uMsg: u32, wParam: usize, lParam: isize) callconv(WINAPI) isize { if (0x00ff==uMsg) { var data: RAWINPUT.HID(30) = undefined; var size: u32 = @sizeOf(RAWINPUT.HID(30)); @@ -118,36 +122,34 @@ fn mb1(state: bool) u32 { fn send(input: INPUT) u32 { return SendInput(1, @ptrCast(&input), @sizeOf(INPUT)); } - +extern "kernel32" fn CreateMutexA(?*const win.SECURITY_ATTRIBUTES, i32, [*:0]const u8) callconv(WINAPI) ?*anyopaque; extern "user32" fn RegisterClassA(*const WNDCLASSA) callconv(WINAPI) u16; -extern "user32" fn DefWindowProcA(HWND, u32, usize, isize) callconv(WINAPI) isize; -extern "user32" fn CreateWindowExA(u32, *anyopaque, [*:0]const u8, u32, i32, i32, i32, i32, ?HWND, ?*const anyopaque, ?HINSTANCE, ?*const anyopaque) callconv(WINAPI) ?HWND; +extern "user32" fn DefWindowProcA(win.HWND, u32, usize, isize) callconv(WINAPI) isize; +extern "user32" fn CreateWindowExA(u32, *anyopaque, [*:0]const u8, u32, i32, i32, i32, i32, ?win.HWND, ?*const anyopaque, ?win.HINSTANCE, ?*const anyopaque) callconv(WINAPI) ?win.HWND; extern "user32" fn PostQuitMessage(i32) callconv(WINAPI) void; -extern "user32" fn GetMessageA(*MSG, ?HWND, u32, u32) callconv(WINAPI) i32; +extern "user32" fn GetMessageA(*MSG, ?win.HWND, u32, u32) callconv(WINAPI) i32; extern "user32" fn DispatchMessageA(*const MSG) callconv(WINAPI) isize; extern "user32" fn RegisterRawInputDevices([*]const RAWINPUTDEVICE,u32,u32) i32; extern "user32" fn GetRawInputData(isize, u32, ?*anyopaque, *u32, u32) callconv(WINAPI) i32; extern "user32" fn SendInput(cInputs: u32, pInputs: [*]const INPUT, cbSize: i32) callconv(WINAPI) u32; const WINAPI = win.WINAPI; -const HWND = *opaque{}; -const HINSTANCE = *opaque{}; -const WNDPROC = *const fn (HWND, u32, usize, isize) callconv(WINAPI) isize; +const WNDPROC = *const fn (win.HWND, u32, usize, isize) callconv(WINAPI) isize; const WNDCLASSA = extern struct { style: u32 = 0, lpfnWndProc: WNDPROC, cbClsExtra: i32 = 0, cbWndExtr: i32 = 0, - hInstance: ?HINSTANCE = null, - hIcon: ?*opaque{} = null, - hCursor: ?*opaque{} = null, - hbrBackground: ?*opaque{} = null, - lpszMenuName: ?*anyopaque = null, - lpszClassName: *const anyopaque, + hInstance: ?win.HINSTANCE = null, + hIcon: ?win.HICON = null, + hCursor: ?win.HCURSOR = null, + hbrBackground: ?win.HBRUSH = null, + lpszMenuName: ?win.LPCSTR = null, + lpszClassName: win.LPCSTR, }; const MSG = extern struct { - hWnd: ?HWND, + hWnd: ?win.HWND, message: u32, wParam: usize, lParam: isize, @@ -163,7 +165,7 @@ const RAWINPUTDEVICE = extern struct { usUsagePage : u16, usUsage : u16, dwFlags : u32, - hwndTarget : ?HWND, + hwndTarget : ?win.HWND, }; const RAWINPUTHEADER = extern struct {