Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
d3xMachina committed Aug 21, 2024
1 parent cc4ce4c commit e417c3b
Showing 1 changed file with 31 additions and 62 deletions.
93 changes: 31 additions & 62 deletions windows/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,25 +192,22 @@ static int lookup_functions()

typedef void (*tls_destructor)(void *data, hid_device *dev, BOOLEAN all);

struct tls_allocation
{
struct tls_allocation {
void *data;
DWORD thread_id;
tls_destructor destructor;

struct tls_allocation *next;
};

struct device_error
{
struct device_error {
HANDLE device_handle;
wchar_t *last_error_str;

struct device_error *next;
};

struct tls_context
{
struct tls_context {
struct tls_allocation *allocated;
CRITICAL_SECTION critical_section;
BOOLEAN critical_section_ready;
Expand Down Expand Up @@ -267,17 +264,15 @@ static hid_device *new_hid_device()

static void tls_init()
{
if (!tls_context.critical_section_ready)
{
if (!tls_context.critical_section_ready) {
InitializeCriticalSection(&tls_context.critical_section);
tls_context.critical_section_ready = TRUE;
}
}

static void tls_register(void* data, tls_destructor destructor)
{
if (!tls_context.critical_section_ready)
{
if (!tls_context.critical_section_ready) {
return;
}

Expand All @@ -288,8 +283,7 @@ static void tls_register(void* data, tls_destructor destructor)
struct tls_allocation *current = tls_context.allocated;
struct tls_allocation *prev = NULL;

while (current)
{
while (current) {
prev = current;
current = current->next;
}
Expand All @@ -300,12 +294,10 @@ static void tls_register(void* data, tls_destructor destructor)
tls->destructor = destructor;
tls->next = NULL;

if (prev)
{
if (prev) {
prev->next = tls;
}
else
{
else {
tls_context.allocated = tls;
}

Expand All @@ -314,8 +306,7 @@ static void tls_register(void* data, tls_destructor destructor)

static void tls_free(DWORD thread_id, hid_device *dev, BOOLEAN all_devices)
{
if (!tls_context.critical_section_ready)
{
if (!tls_context.critical_section_ready) {
return;
}

Expand All @@ -324,34 +315,28 @@ static void tls_free(DWORD thread_id, hid_device *dev, BOOLEAN all_devices)
struct tls_allocation *current = tls_context.allocated;
struct tls_allocation *prev = NULL;

while (current)
{
if (thread_id != 0 && thread_id != current->thread_id)
{
while (current) {
if (thread_id != 0 && thread_id != current->thread_id) {
prev = current;
current = current->next;
continue;
}

current->destructor(&current->data, dev, all_devices);

if (current->data == NULL)
{
if (prev)
{
if (current->data == NULL) {
if (prev) {
prev->next = current->next;
}
else
{
else {
tls_context.allocated = current->next;
}

struct tls_allocation *current_tmp = current;
current = current->next;
free(current_tmp);
}
else
{
else {
prev = current;
current = current->next;
}
Expand All @@ -367,8 +352,7 @@ static void tls_free_all_threads(hid_device *dev, BOOLEAN all_devices)

static void tls_exit()
{
if (!tls_context.critical_section_ready)
{
if (!tls_context.critical_section_ready) {
return;
}

Expand All @@ -379,17 +363,14 @@ static void tls_exit()

static void free_error_buffer(struct device_error **error, hid_device *dev, BOOLEAN all_devices)
{
if (error == NULL)
{
if (error == NULL) {
return;
}

struct device_error *current = *error;

if (all_devices)
{
while (current)
{
if (all_devices) {
while (current) {
struct device_error *current_tmp = current;
current = current->next;
free(current_tmp->last_error_str);
Expand All @@ -402,17 +383,13 @@ static void free_error_buffer(struct device_error **error, hid_device *dev, BOOL
{
struct device_error *prev = NULL;

while (current)
{
while (current) {
if ((dev == NULL && current->device_handle == NULL) ||
(dev != NULL && dev->device_handle == current->device_handle))
{
if (prev)
{
(dev != NULL && dev->device_handle == current->device_handle)) {
if (prev) {
prev->next = current->next;
}
else
{
else {
*error = current->next;
}

Expand Down Expand Up @@ -524,11 +501,9 @@ static wchar_t** get_error_buffer(hid_device *dev)
struct device_error *current = device_error;
struct device_error *prev = NULL;

while (current)
{
while (current) {
if ((dev == NULL && current->device_handle == NULL) ||
(dev != NULL && dev->device_handle == current->device_handle))
{
(dev != NULL && dev->device_handle == current->device_handle)) {
return &current->last_error_str;
}

Expand All @@ -541,12 +516,10 @@ static wchar_t** get_error_buffer(hid_device *dev)
error->last_error_str = NULL;
error->next = NULL;

if (prev)
{
if (prev) {
prev->next = error;
}
else
{
else {
device_error = error;
tls_register(device_error, (tls_destructor)&free_error_buffer);
}
Expand All @@ -558,11 +531,9 @@ static wchar_t* get_error_str(hid_device *dev)
{
struct device_error *current = device_error;

while (current)
{
while (current) {
if ((dev == NULL && current->device_handle == NULL) ||
(dev != NULL && dev->device_handle == current->device_handle))
{
(dev != NULL && dev->device_handle == current->device_handle)) {
return current->last_error_str;
}

Expand Down Expand Up @@ -625,10 +596,8 @@ HID_API_EXPORT const char* HID_API_CALL hid_version_str(void)

BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
{
switch (reason)
{
case DLL_THREAD_DETACH:
{
switch (reason) {
case DLL_THREAD_DETACH: {
DWORD thread_id = GetCurrentThreadId();
tls_free(thread_id, NULL, TRUE);
break;
Expand Down

0 comments on commit e417c3b

Please sign in to comment.