1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 09:27:59 +00:00
putty-source/windows/utils/gui-timing.c
Simon Tatham c674b2da4f Windows: move GUI timer handling into a utils module.
Previously, the timing.c subsystem worked in Windows PuTTY by means of
scheduling WM_TIMER messages to be sent to the terminal window. Now it
uses a separate hidden window instead, and all the machinery for
handling that window lives on its own in windows/utils/gui-timing.c.

Most immediately, this removes a use of wgs.term_hwnd that will become
awkward when I move that structure in a following commit. But also, it
will make it easier to add the same timing subsystem to unrelated GUI
programs, such as Windows Pageant: if we ever decide to implement
automatic deletion or re-encryption of unused keys after a timeout,
this will help make that easier.
2022-09-13 11:26:57 +01:00

57 lines
1.5 KiB
C

#include "putty.h"
#define TIMING_CLASS_NAME "PuTTYTimerWindow"
#define TIMING_TIMER_ID 1234
static long timing_next_time;
static HWND timing_hwnd;
static LRESULT CALLBACK TimingWndProc(HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch (message) {
case WM_TIMER:
if ((UINT_PTR)wParam == TIMING_TIMER_ID) {
unsigned long next;
KillTimer(hwnd, TIMING_TIMER_ID);
if (run_timers(timing_next_time, &next)) {
timer_change_notify(next);
} else {
}
}
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
void setup_gui_timing(void)
{
WNDCLASS wndclass;
memset(&wndclass, 0, sizeof(wndclass));
wndclass.lpfnWndProc = TimingWndProc;
wndclass.hInstance = hinst;
wndclass.lpszClassName = TIMING_CLASS_NAME;
RegisterClass(&wndclass);
timing_hwnd = CreateWindow(
TIMING_CLASS_NAME, "PuTTY: hidden timing window",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
100, 100, NULL, NULL, hinst, NULL);
ShowWindow(timing_hwnd, SW_HIDE);
}
void timer_change_notify(unsigned long next)
{
unsigned long now = GETTICKCOUNT();
long ticks;
if (now - next < INT_MAX)
ticks = 0;
else
ticks = next - now;
KillTimer(timing_hwnd, TIMING_TIMER_ID);
SetTimer(timing_hwnd, TIMING_TIMER_ID, ticks, NULL);
timing_next_time = next;
}