add okay-ish files

This commit is contained in:
Mylloon 2024-04-03 13:40:32 +02:00
parent 5dbcc498ef
commit 7ce2a36b45
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
4 changed files with 108 additions and 0 deletions

6
includes/battery.h Normal file
View file

@ -0,0 +1,6 @@
#pragma once
#include <stdbool.h>
// Get the current battery level
int get_battery_level(const char *device_path, bool *is_charging);

7
includes/helper.h Normal file
View file

@ -0,0 +1,7 @@
#pragma once
#define PID_WIRED 0x007A
#define PID_WIRELESS 0x007B
// Enumerate through HID devices and find device path
void find_device_path(int pid, char *device_path);

60
src/helper.c Normal file
View file

@ -0,0 +1,60 @@
#include "../includes/helper.h"
#include <windows.h>
#include <hidsdi.h>
#include <setupapi.h>
#include <stdio.h>
#define VID 0x1532
void find_device_path(int pid, char *device_path) {
GUID hidGuid;
HidD_GetHidGuid(&hidGuid);
HDEVINFO hDevInfo = SetupDiGetClassDevs(
&hidGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (hDevInfo == INVALID_HANDLE_VALUE) {
fprintf_s(stderr, "Failed to get device information.\n");
return;
}
SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
for (DWORD i = 0; SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &hidGuid, i,
&deviceInterfaceData);
i++) {
DWORD requiredSize;
SetupDiGetDeviceInterfaceDetail(hDevInfo, &deviceInterfaceData, NULL, 0,
&requiredSize, NULL);
PSP_DEVICE_INTERFACE_DETAIL_DATA detailData =
(PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(requiredSize);
detailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
if (SetupDiGetDeviceInterfaceDetail(hDevInfo, &deviceInterfaceData,
detailData, requiredSize, NULL, NULL)) {
HANDLE hDevice = CreateFile(
detailData->DevicePath, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (hDevice != INVALID_HANDLE_VALUE) {
// Get device attributes
HIDD_ATTRIBUTES attributes;
attributes.Size = sizeof(HIDD_ATTRIBUTES);
if (HidD_GetAttributes(hDevice, &attributes)) {
// Check VID and PID
if (attributes.VendorID == VID && attributes.ProductID == pid) {
strcpy(device_path, detailData->DevicePath);
CloseHandle(hDevice);
break;
}
}
CloseHandle(hDevice);
}
}
free(detailData);
}
SetupDiDestroyDeviceInfoList(hDevInfo);
}

35
src/main.c Normal file
View file

@ -0,0 +1,35 @@
#include "../includes/battery.h"
#include "../includes/helper.h"
#include <stdio.h>
#include <string.h>
int main(void) {
char device_path[2048];
// Try wired
find_device_path(PID_WIRED, device_path);
if (strlen(device_path) < 10) {
// Try wireless
find_device_path(PID_WIRELESS, device_path);
if (strlen(device_path) < 10) {
fprintf_s(stderr, "Can't find any compatible device.\n");
return 1;
}
}
printf("Device: %s\n", device_path);
bool is_charging;
int battery_level;
if ((battery_level = get_battery_level(device_path, &is_charging)) < 0) {
fprintf_s(stderr, "Failed to get battery level.\n");
return 1;
}
printf("Battery Level: %d%%\n", battery_level);
printf("Charging: %s\n", is_charging ? "Yes" : "No");
return 0;
}