Skip to content

Commit

Permalink
Using only wintimers.
Browse files Browse the repository at this point in the history
  • Loading branch information
PerryWerneck committed Feb 23, 2025
1 parent cd0985a commit aba9f27
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 74 deletions.
1 change: 0 additions & 1 deletion src/include/private/mainloop.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include <windows.h>

enum MessageTypes {
WM_CHECK_TIMERS = WM_USER+101,
WM_ADD_TIMER = WM_USER+102,
WM_REMOVE_TIMER = WM_USER+103,
};
Expand Down
95 changes: 22 additions & 73 deletions src/library/os/windows/mainloop.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,18 @@
static size_t instances = 0;
static ATOM identifier;

#define IDT_CHECK_TIMERS 1

static LRESULT WINAPI hwndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

struct _lib3270_poll_context {
LIB3270_LINKED_LIST
};

typedef struct timeout {

LIB3270_LINKED_LIST_HEAD

/// @brief The timestamp, in getCurrentTime() units, for next activation.
unsigned long activation_time;
LPARAM id;
unsigned long interval_ms;

/// @brief The timer callback.
int (*call)(H3270 *, void *);
Expand All @@ -73,10 +72,13 @@

static void * win32_timer_add(H3270 *hSession, unsigned long interval_ms, int (*proc)(H3270 *session, void *userdata), void *userdata) {

static LPARAM id = 0;

timeout_t *t_new = lib3270_new(timeout_t);

t_new->call = proc;
t_new->activation_time = getCurrentTime() + interval_ms;
t_new->id = ++id;
t_new->interval_ms = interval_ms;
t_new->userdata = userdata;

PostMessage(hSession->hwnd,WM_ADD_TIMER,0,(LPARAM) t_new);
Expand Down Expand Up @@ -172,94 +174,41 @@

switch(uMsg) {
case WM_CREATE:
if(hSession->timer.context->interval) {
SetTimer(hwnd,IDT_CHECK_TIMERS,hSession->timer.context->interval,(TIMERPROC) NULL);
}
hSession->hwnd = hwnd;
break;

case WM_DESTROY:
if(hSession->timer.context->interval) {
KillTimer(hwnd, IDT_CHECK_TIMERS);
{
timeout_t *t;
for (t = (timeout_t *) hSession->timer.context->timers.first; t != NULL; t = (timeout_t *) t->next) {
KillTimer(hwnd, t->id);
}
hSession->hwnd = NULL;
}
hSession->hwnd = NULL;
break;

case WM_TIMER:
if(wParam == IDT_CHECK_TIMERS) {
PostMessage(hwnd,WM_CHECK_TIMERS,0,0);
}
return 0;

case WM_CHECK_TIMERS:
{
debug("%s","Checking timers");
unsigned long now = getCurrentTime();
unsigned long wait = 0;

timeout_t *t;
for (t = (timeout_t *) hSession->timer.context->timers.first; t != NULL; t = (timeout_t *) t->next) {
if (t->activation_time <= now) {
if(t->id == wParam) {
t->call(hSession,t->userdata);
} else {
wait = t->activation_time - now;
SendMessage(hwnd,WM_REMOVE_TIMER,0,(LPARAM) t);
break;
}
}

if(!wait) {

// Empty list.
KillTimer(hwnd,IDT_CHECK_TIMERS);
hSession->timer.context->interval = 0;

} if(!hSession->timer.context->interval) {

// New list.
SetTimer(hwnd,IDT_CHECK_TIMERS,wait,(TIMERPROC) NULL);
hSession->timer.context->interval = wait;

} else if(wait != hSession->timer.context->interval) {

// Update list.
SetTimer(hwnd,IDT_CHECK_TIMERS,wait,(TIMERPROC) NULL);
hSession->timer.context->interval = wait;

}

}
return 0;

case WM_ADD_TIMER:
{
timeout_t *t_new = (timeout_t *) lParam;
timeout_t *t = NULL;
timeout_t *prev = NULL;

// Find where to insert this item.
for (t = (timeout_t *) hSession->timer.context->timers.first; t != NULL; t = (timeout_t *) t->next) {
if (t->activation_time > t_new->activation_time)
break;
prev = t;
}

// Insert it.
if (prev == NULL) {
// t_new is Front.
t_new->next = hSession->timer.context->timers.first;
hSession->timer.context->timers.first = (struct lib3270_linked_list_node *) t_new;
} else if (t == NULL) {
// t_new is Rear.
t_new->next = NULL;
prev->next = (struct lib3270_linked_list_node *) t_new;
hSession->timer.context->timers.last = (struct lib3270_linked_list_node *) t_new;
} else {
// t_new is Middle.
t_new->next = (struct lib3270_linked_list_node *) t;
prev->next = (struct lib3270_linked_list_node *) t_new;
}
timeout_t *t = (timeout_t *) lParam;

SendMessage(hwnd,WM_CHECK_TIMERS,0,0);
t->prev = hSession->timer.context->timers.last;
t->next = NULL;
hSession->timer.context->timers.last = (struct lib3270_linked_list_node *) t;

SetTimer(hwnd,t->id,t->interval_ms,(TIMERPROC) NULL);

}
return 0;
Expand All @@ -268,12 +217,12 @@
{
timeout_t *timer = (timeout_t *) lParam;
if(timer) {
KillTimer(hwnd,timer->id);
lib3270_linked_list_delete_node(
(lib3270_linked_list *) &hSession->timer.context->timers,
timer
);
}
SendMessage(hwnd,WM_CHECK_TIMERS,0,0);
}
return 0;

Expand Down

0 comments on commit aba9f27

Please sign in to comment.