marlersoft / zigwin32gen

Generates Complete Zig bindings for Win32. See https://github.com/marlersoft/zigwin32 for the bindings themselves.
108 stars 17 forks source link

Ability to define alignment/union_pointer constants similar to function params #30

Open Tired-Fox opened 6 months ago

Tired-Fox commented 6 months ago

Right now function parameters can be defined to be union_pointer in unionpointers.json and this would make their alignment align(1). This should also be added to constants as functions taking these constants don't currently except all of the variants. Not all constants variants are generated as the alignment is invalid for align(2) pointers. I am referencing IDC_* and IDI_* constants primarily.

Example

The following example isn't implemented in the current generation but could fix some problems of missing constants and usability. Suppose a user would want to load a cursor in the wide unicode format. They can only load the default IDC_ARROW constant as that is the only constant that is able to be loaded without align(1).

const win32 = @import("win32");
const windows_and_messaging = win32.ui.windows_and_messaging;
const HINSTANCE = win32.foundation.HINSTANCE;
const LoadCursorW = windows_and_messaging.LoadCursorW;
const IDC_ARROW = windows_and_messaging.IDC_ARROW;

pub fn example(instance: HINSTANCE) void {
    _ = LoadCursorW(instance, IDC_ARROW);
}

However, the user cannot load any other cursor as they are not generated. The change would be to mark lpCursorName param in LoadCursorW as a union pointer. Then the constant IDC_HAND, and all other IDC_* constants be marked as union pointers. With the constants being loaded in and managed like the function union pointers, they can all be marked as align(1).

Example Generated Code

pub extern "user32" fn LoadCursorW(
    hInstance: ?HINSTANCE,
    lpCursorName: ?[*:0]align(1) const u16,
) callconv(@import("std").os.windows.WINAPI) ?HCURSOR;

// ...

pub const IDC_ARROW = @import("../zig.zig").typedConst([*:0]align(1) const u16, @as(i32, 32512));
pub const IDC_HAND = @import("../zig.zig").typedConst([*:0]align(1) const u16, @as(i32, 32649));
// ... rest of constants

This would allow the user to use other constants like this

// ...
const IDC_HAND = windows_and_messaging.IDC_HAND;

pub fn example(instance: HINSTANCE) void {
    _ = LoadCursorW(instance, IDC_HAND);
}

This would also apply to LoadIconW and other functions that expect a align(1) string. Most functions that us MakeIntResourceW.

I don't mind implementing a PR for this as I am planning to fork and do this myself for use with a library I am writing.