Skip to content
This repository has been archived by the owner on Oct 26, 2023. It is now read-only.

Commit

Permalink
win_watcher utilizes threads to be faster.
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Bäck authored and Richard Bäck committed Jan 3, 2022
1 parent bb64452 commit 09b095a
Show file tree
Hide file tree
Showing 6 changed files with 318 additions and 44 deletions.
30 changes: 26 additions & 4 deletions src/director.c
Original file line number Diff line number Diff line change
Expand Up @@ -1027,20 +1027,42 @@ int
b3_director_w32_set_active_window(HWND window_handler, char generate_lag)
{
int error ;
WINDOWPLACEMENT window_placement;
RECT window_rect;
POINT point_bak;
POINT point;
INPUT input = { 0 };

error = 0;

if (generate_lag) {
Sleep(500);
/**
* Nothing to do if the window is already active.
*/
if (GetActiveWindow() == window_handler) {
return error;
}

/**
* Only perform the lag if the window is not shown at all
*/
GetWindowPlacement(window_handler, &window_placement);
switch (window_placement.showCmd) {
case SW_HIDE:
case SW_SHOWMINIMIZED:
case SW_MINIMIZE:
case SW_FORCEMINIMIZE:
if (generate_lag) {
Sleep(500);
}
break;
}

/**
* Active the window by clicking on it
*/
GetWindowRect(window_handler, &window_rect);
point.x = window_rect.left + 1;
point.y = window_rect.top + 1;
point.x = window_rect.left;
point.y = window_rect.top;

GetCursorPos(&point_bak);
SetCursorPos(point.x, point.y);
Expand Down
3 changes: 3 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ parameterized_main(void)
* Start win watcher
*/
if (!error) {
b3_win_watcher_set_threaded(win_watcher, 1);
b3_win_watcher_start(win_watcher);
}

Expand All @@ -266,6 +267,8 @@ parameterized_main(void)
* Start main loops
*/
if (!error) {
b3_win_watcher_set_threaded(win_watcher, 0);

main_loop();

b3_win_watcher_stop(win_watcher);
Expand Down
60 changes: 54 additions & 6 deletions src/win_factory.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,61 @@

#include <stdlib.h>

static int
b3_win_factory_free_impl(b3_win_factory_t *win_factory);

static b3_win_t *
b3_win_factory_win_create_impl(b3_win_factory_t *win_factory, HWND window_handler);

static int
b3_win_factory_win_free_impl(b3_win_factory_t *win_factory, b3_win_t *win);

b3_win_factory_t *
b3_win_factory_new(void)
{
b3_win_factory_t *win_factory;

win_factory = malloc(sizeof(win_factory));
if (win_factory) {
win_factory->b3_win_factory_free = b3_win_factory_free_impl;
win_factory->b3_win_factory_win_create = b3_win_factory_win_create_impl;
win_factory->b3_win_factory_win_free = b3_win_factory_win_free_impl;

win_factory->global_mutex = CreateMutex(NULL, FALSE, NULL);
array_new(&(win_factory->win_arr));
}

array_new(&(win_factory->win_arr));

return win_factory;
}

int
b3_win_factory_free(b3_win_factory_t *win_factory)
{
return win_factory->b3_win_factory_free(win_factory);
}

b3_win_t *
b3_win_factory_win_create(b3_win_factory_t *win_factory, HWND window_handler)
{
return win_factory->b3_win_factory_win_create(win_factory, window_handler);
}

int
b3_win_factory_win_free(b3_win_factory_t *win_factory, b3_win_t *win)
{
return win_factory->b3_win_factory_win_free(win_factory, win);
}

int
b3_win_factory_free_impl(b3_win_factory_t *win_factory)
{
ArrayIter iter;
b3_win_t *win_iter;

ReleaseMutex(win_factory->global_mutex);
CloseHandle(win_factory->global_mutex);

array_iter_init(&iter, win_factory->win_arr);
while (array_iter_next(&iter, (void *) &win_iter) != CC_ITER_END) {
array_iter_remove(&iter, NULL);
Expand All @@ -64,13 +101,15 @@ b3_win_factory_free(b3_win_factory_t *win_factory)
}

b3_win_t *
b3_win_factory_win_create(b3_win_factory_t *win_factory, HWND window_handler)
b3_win_factory_win_create_impl(b3_win_factory_t *win_factory, HWND window_handler)
{
ArrayIter iter;
b3_win_t *win_iter;
b3_win_t *win_new;
b3_win_t *win;

WaitForSingleObject(win_factory->global_mutex, INFINITE);

win_new = b3_win_new(window_handler, 0);
win = NULL;
array_iter_init(&iter, win_factory->win_arr);
Expand All @@ -87,16 +126,23 @@ b3_win_factory_win_create(b3_win_factory_t *win_factory, HWND window_handler)
array_add(win_factory->win_arr, win);
}

ReleaseMutex(win_factory->global_mutex);

return win;
}

int
b3_win_factory_win_free(b3_win_factory_t *win_factory, b3_win_t *win)
b3_win_factory_win_free_impl(b3_win_factory_t *win_factory, b3_win_t *win)
{
int error;
ArrayIter iter;
b3_win_t *win_iter;
char found;

error = 1;

WaitForSingleObject(win_factory->global_mutex, INFINITE);

found = 0;
array_iter_init(&iter, win_factory->win_arr);
while (!found && array_iter_next(&iter, (void *) &win_iter) != CC_ITER_END) {
Expand All @@ -110,8 +156,10 @@ b3_win_factory_win_free(b3_win_factory_t *win_factory, b3_win_t *win)
if (win_iter != win) {
b3_win_free(win_iter);
}
return b3_win_free(win);
} else {
return 1;
error = b3_win_free(win);
}

ReleaseMutex(win_factory->global_mutex);

return error;
}
12 changes: 10 additions & 2 deletions src/win_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,21 @@
#ifndef B3_WIN_FACTORY_H
#define B3_WIN_FACTORY_H

typedef struct b3_win_factory_s
typedef struct b3_win_factory_s b3_win_factory_t;

struct b3_win_factory_s
{
int (* b3_win_factory_free)(b3_win_factory_t *win_factory);
b3_win_t *(* b3_win_factory_win_create)(b3_win_factory_t *win_factory, HWND window_handler);
int (* b3_win_factory_win_free)(b3_win_factory_t *win_factory, b3_win_t *win);

HANDLE global_mutex;

/**
* Array of b3_win_t *
*/
Array *win_arr;
} b3_win_factory_t;
};

/**
* @brief Creates a new window factory
Expand Down
Loading

0 comments on commit 09b095a

Please sign in to comment.