nefarius / ViGEmClient

ViGEm Client SDK for feeder development.
https://docs.nefarius.at/projects/ViGEm/
MIT License
138 stars 66 forks source link

Make the client library compilable with zig #33

Closed makl11 closed 1 year ago

makl11 commented 1 year ago

While trying to use the library with zigs c-interop feature, I ran into some compiler errors, mostly because of the using keyword in Client.h. This happens, because while zig can compile c++ source code just fine, it can not work with c++ header files. Since I did not like the idea of writing a whole c only wrapper, I decided to try and patch the header file. Afterwards I ran into some additional issues with the DBGPRINT makro, because __FUNCTIONW__ is undefined when compiling with zig and tI could not find another way to get the functionname as a wide string directly.

The changes I made allow me to successfully run the following demo code:

const std = @import("std");
const C = @cImport({
    @cDefine("WIN32_LEAN_AND_MEAN", "1");
    @cInclude("windows.h");
    @cInclude("ViGEm/Client.h");
});

pub fn main() !void {
    const stdin = std.io.getStdIn().reader();

    const client = C.vigem_alloc();
    defer C.vigem_free(client);

    if (client == null) {
        return error.ClientIsNull;
    }

    if (!C.VIGEM_SUCCESS(C.vigem_connect(client))) {
        return error.ViGemBusConnectionFailed;
    }

    const gamepad = C.vigem_target_x360_alloc();
    defer C.vigem_target_free(gamepad);

    if (!C.VIGEM_SUCCESS(C.vigem_target_add(client, gamepad))) {
        return error.TargetPluginFailed;
    }

    _ = try stdin.readByte(); // pause until enter key is pressed

    _ = C.vigem_target_remove(client, gamepad);
}

As expected a xbox 360 controller shows up and disappears after exiting the program, but I did not do any further testing so far! Please review my changes and feel free to point out all the ways I messed up, as I do not have much experience with c++ and this is my first PR. 😅 I'm willing to do some further work on this topic, if compatibility with zig is in your intrest aswell!

nefarius commented 1 year ago

Funny enough those stupid C++-only features at one point just crept in without any good need and simply nobody noticed until now 🤣

nefarius commented 1 year ago

See my remarks, thanks.