Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
typedef void (* Tk_ExtEventLoop)(void);
#if defined BUILD_tk
__declspec(dllexport) int Tk_RegisterEventLoop(Tk_ExtEventLoop func);
#else
__declspec(dllimport) int Tk_RegisterEventLoop(Tk_ExtEventLoop func);
#endif
static Tk_ExtEventLoop event_loop = NULL;
int
Tk_RegisterEventLoop(Tk_ExtEventLoop func)
{
if (event_loop != NULL || func == NULL)
return 0;
event_loop = func;
return 1;
}
void
Tk_MainLoop(void)
{
if (event_loop != NULL)
(* event_loop)(); // preempt standard event loop
while (Tk_GetNumMainWindows() > 0) {
Tcl_DoOneEvent(0);
}
event_loop = NULL; // paranoia
}
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <tcl.h>
#include <tk.h>
static Tcl_Interp *interp = NULL;
static int time_init = 0;
static unsigned long time_base;
static void reset_timer_period(void)
{
timeEndPeriod(1);
}
static int init_systime(void)
{
MMRESULT res;
if (time_init)
return 1;
res = timeBeginPeriod(1);
if (res != TIMERR_NOERROR)
return 0;
time_base = timeGetTime();
time_init = 1;
atexit(reset_timer_period);
return 1;
}
static unsigned long get_systime(void)
{
if (! time_init)
return 0;
return timeGetTime() - time_base;
}
int tcl_app_init(Tcl_Interp *i)
{
interp = i;
/*
Code must be added here to tell Tcl/Tk where to find
the necessary libraries and scripts. Removed from
this example, for clarity.
*/
if (Tcl_Init(i) == TCL_ERROR)
return TCL_ERROR;
if (Tk_Init(i) == TCL_ERROR)
return TCL_ERROR;
/*
Code to register callbacks for the GUI must be added
here. Removed from this example, for clarity.
*/
return TCL_OK;
}
#define TCL_CALL_INTERVAL 5
#define OUTPUT_INTERVAL 1000
void event_loop(void)
{
unsigned long curr_time, last_tcl_call, last_output;
last_tcl_call = last_output = curr_time = get_systime();
while (1)
{
curr_time = get_systime();
// check for time wrap-around, it is unlikely but possible
if (curr_time < last_tcl_call)
{
last_tcl_call = last_output = curr_time;
continue;
}
else if (curr_time < last_output)
{
last_tcl_call = last_output = curr_time;
continue;
}
// check timeouts and call functions as needed
if (curr_time - last_tcl_call >= TCL_CALL_INTERVAL)
{
last_tcl_call = curr_time;
Tcl_DoOneEvent(TCL_ALL_EVENTS | TCL_DONT_WAIT);
if (Tk_GetNumMainWindows() == 0)
break;
}
else if (curr_time - last_output >= OUTPUT_INTERVAL)
{
last_output = curr_time;
printf("time = %d\n", (int) curr_time);
}
}
}
int main(int argc, char *argv[])
{
init_systime();
if (! Tk_RegisterEventLoop(event_loop))
exit(1);
Tk_Main(argc, argv, tcl_app_init);
return 0;
}