jim.c: properly free cached callframes
[jimtcl.git] / jim-eventloop.c
blob22ca5ff31787b850c06daf896968c515c3a9a623
1 /* Jim - A small embeddable Tcl interpreter
3 * Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
4 * Copyright 2005 Clemens Hintze <c.hintze@gmx.net>
5 * Copyright 2005 patthoyts - Pat Thoyts <patthoyts@users.sf.net>
6 * Copyright 2008 oharboe - Øyvind Harboe - oyvind.harboe@zylin.com
7 * Copyright 2008 Andrew Lunn <andrew@lunn.ch>
8 * Copyright 2008 Duane Ellis <openocd@duaneellis.com>
9 * Copyright 2008 Uwe Klein <uklein@klein-messgeraete.de>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials
20 * provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE JIM TCL PROJECT ``AS IS'' AND ANY
23 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * The views and conclusions contained in the software and documentation
36 * are those of the authors and should not be interpreted as representing
37 * official policies, either expressed or implied, of the Jim Tcl Project.
38 **/
40 #include "jimautoconf.h"
41 #include <jim.h>
42 #include <jim-eventloop.h>
44 /* POSIX includes */
45 #include <sys/time.h>
46 #include <sys/types.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <errno.h>
51 #if defined(__MINGW32__)
52 #include <windows.h>
53 #include <winsock.h>
54 #define msleep Sleep
55 #else
56 #ifdef HAVE_SYS_SELECT_H
57 #include <sys/select.h>
58 #endif
60 #ifndef HAVE_USLEEP
61 /* XXX: Implement this in terms of select() or nanosleep() */
62 #define msleep(MS) sleep((MS) / 1000)
63 #warning "sub-second sleep not supported"
64 #else
65 #define msleep(MS) sleep((MS) / 1000); usleep(((MS) % 1000) * 1000);
66 #endif
67 #endif
69 /* --- */
71 /* File event structure */
72 typedef struct Jim_FileEvent
74 FILE *handle;
75 int mask; /* one of JIM_EVENT_(READABLE|WRITABLE|EXCEPTION) */
76 Jim_FileProc *fileProc;
77 Jim_EventFinalizerProc *finalizerProc;
78 void *clientData;
79 struct Jim_FileEvent *next;
80 } Jim_FileEvent;
82 /* Time event structure */
83 typedef struct Jim_TimeEvent
85 jim_wide id; /* time event identifier. */
86 long initialms; /* initial relative timer value */
87 jim_wide when; /* milliseconds */
88 Jim_TimeProc *timeProc;
89 Jim_EventFinalizerProc *finalizerProc;
90 void *clientData;
91 struct Jim_TimeEvent *next;
92 } Jim_TimeEvent;
94 /* Per-interp stucture containing the state of the event loop */
95 typedef struct Jim_EventLoop
97 Jim_FileEvent *fileEventHead;
98 Jim_TimeEvent *timeEventHead;
99 jim_wide timeEventNextId; /* highest event id created, starting at 1 */
100 time_t timeBase;
101 int suppress_bgerror; /* bgerror returned break, so don't call it again */
102 } Jim_EventLoop;
104 static void JimAfterTimeHandler(Jim_Interp *interp, void *clientData);
105 static void JimAfterTimeEventFinalizer(Jim_Interp *interp, void *clientData);
107 int Jim_EvalObjBackground(Jim_Interp *interp, Jim_Obj *scriptObjPtr)
109 Jim_EventLoop *eventLoop = Jim_GetAssocData(interp, "eventloop");
110 Jim_CallFrame *savedFramePtr;
111 int retval;
113 savedFramePtr = interp->framePtr;
114 interp->framePtr = interp->topFramePtr;
115 retval = Jim_EvalObj(interp, scriptObjPtr);
116 interp->framePtr = savedFramePtr;
117 /* Try to report the error (if any) via the bgerror proc */
118 if (retval != JIM_OK && !eventLoop->suppress_bgerror) {
119 Jim_Obj *objv[2];
120 int rc = JIM_ERR;
122 objv[0] = Jim_NewStringObj(interp, "bgerror", -1);
123 objv[1] = Jim_GetResult(interp);
124 Jim_IncrRefCount(objv[0]);
125 Jim_IncrRefCount(objv[1]);
126 if (Jim_GetCommand(interp, objv[0], JIM_NONE) == NULL || (rc = Jim_EvalObjVector(interp, 2, objv)) != JIM_OK) {
127 if (rc == JIM_BREAK) {
128 /* No more bgerror calls */
129 eventLoop->suppress_bgerror++;
131 else {
132 /* Report the error to stderr. */
133 Jim_MakeErrorMessage(interp);
134 fprintf(stderr, "%s\n", Jim_String(Jim_GetResult(interp)));
135 /* And reset the result */
136 Jim_SetResultString(interp, "", -1);
139 Jim_DecrRefCount(interp, objv[0]);
140 Jim_DecrRefCount(interp, objv[1]);
142 return retval;
146 void Jim_CreateFileHandler(Jim_Interp *interp, FILE * handle, int mask,
147 Jim_FileProc * proc, void *clientData, Jim_EventFinalizerProc * finalizerProc)
149 Jim_FileEvent *fe;
150 Jim_EventLoop *eventLoop = Jim_GetAssocData(interp, "eventloop");
152 fe = Jim_Alloc(sizeof(*fe));
153 fe->handle = handle;
154 fe->mask = mask;
155 fe->fileProc = proc;
156 fe->finalizerProc = finalizerProc;
157 fe->clientData = clientData;
158 fe->next = eventLoop->fileEventHead;
159 eventLoop->fileEventHead = fe;
163 * Removes all event handlers for 'handle' that match 'mask'.
165 void Jim_DeleteFileHandler(Jim_Interp *interp, FILE * handle, int mask)
167 Jim_FileEvent *fe, *next, *prev = NULL;
168 Jim_EventLoop *eventLoop = Jim_GetAssocData(interp, "eventloop");
170 for (fe = eventLoop->fileEventHead; fe; fe = next) {
171 next = fe->next;
172 if (fe->handle == handle && (fe->mask & mask)) {
173 /* Remove this entry from the list */
174 if (prev == NULL)
175 eventLoop->fileEventHead = next;
176 else
177 prev->next = next;
178 if (fe->finalizerProc)
179 fe->finalizerProc(interp, fe->clientData);
180 Jim_Free(fe);
181 continue;
183 prev = fe;
188 * Returns the time since interp creation in milliseconds.
190 static jim_wide JimGetTime(Jim_EventLoop *eventLoop)
192 struct timeval tv;
194 gettimeofday(&tv, NULL);
196 return (jim_wide)(tv.tv_sec - eventLoop->timeBase) * 1000 + tv.tv_usec / 1000;
199 jim_wide Jim_CreateTimeHandler(Jim_Interp *interp, jim_wide milliseconds,
200 Jim_TimeProc * proc, void *clientData, Jim_EventFinalizerProc * finalizerProc)
202 Jim_EventLoop *eventLoop = Jim_GetAssocData(interp, "eventloop");
203 jim_wide id = ++eventLoop->timeEventNextId;
204 Jim_TimeEvent *te, *e, *prev;
206 te = Jim_Alloc(sizeof(*te));
207 te->id = id;
208 te->initialms = milliseconds;
209 te->when = JimGetTime(eventLoop) + milliseconds;
210 te->timeProc = proc;
211 te->finalizerProc = finalizerProc;
212 te->clientData = clientData;
214 /* Add to the appropriate place in the list */
215 prev = NULL;
216 for (e = eventLoop->timeEventHead; e; e = e->next) {
217 if (te->when < e->when) {
218 break;
220 prev = e;
222 if (prev) {
223 te->next = prev->next;
224 prev->next = te;
226 else {
227 te->next = eventLoop->timeEventHead;
228 eventLoop->timeEventHead = te;
231 return id;
234 static jim_wide JimParseAfterId(Jim_Obj *idObj)
236 const char *tok = Jim_String(idObj);
237 jim_wide id;
239 if (strncmp(tok, "after#", 6) == 0 && Jim_StringToWide(tok + 6, &id, 10) == JIM_OK) {
240 /* Got an event by id */
241 return id;
243 return -1;
246 static jim_wide JimFindAfterByScript(Jim_EventLoop *eventLoop, Jim_Obj *scriptObj)
248 Jim_TimeEvent *te;
250 for (te = eventLoop->timeEventHead; te; te = te->next) {
251 /* Is this an 'after' event? */
252 if (te->timeProc == JimAfterTimeHandler) {
253 if (Jim_StringEqObj(scriptObj, te->clientData)) {
254 return te->id;
258 return -1; /* NO event with the specified ID found */
261 static Jim_TimeEvent *JimFindTimeHandlerById(Jim_EventLoop *eventLoop, jim_wide id)
263 Jim_TimeEvent *te;
265 for (te = eventLoop->timeEventHead; te; te = te->next) {
266 if (te->id == id) {
267 return te;
270 return NULL;
273 static Jim_TimeEvent *Jim_RemoveTimeHandler(Jim_EventLoop *eventLoop, jim_wide id)
275 Jim_TimeEvent *te, *prev = NULL;
277 for (te = eventLoop->timeEventHead; te; te = te->next) {
278 if (te->id == id) {
279 if (prev == NULL)
280 eventLoop->timeEventHead = te->next;
281 else
282 prev->next = te->next;
283 return te;
285 prev = te;
287 return NULL;
290 static void Jim_FreeTimeHandler(Jim_Interp *interp, Jim_TimeEvent *te)
292 if (te->finalizerProc)
293 te->finalizerProc(interp, te->clientData);
294 Jim_Free(te);
297 jim_wide Jim_DeleteTimeHandler(Jim_Interp *interp, jim_wide id)
299 Jim_TimeEvent *te;
300 Jim_EventLoop *eventLoop = Jim_GetAssocData(interp, "eventloop");
302 if (id > eventLoop->timeEventNextId) {
303 return -2; /* wrong event ID */
306 te = Jim_RemoveTimeHandler(eventLoop, id);
307 if (te) {
308 jim_wide remain;
310 remain = te->when - JimGetTime(eventLoop);
311 remain = (remain < 0) ? 0 : remain;
313 Jim_FreeTimeHandler(interp, te);
314 return remain;
316 return -1; /* NO event with the specified ID found */
319 /* --- POSIX version of Jim_ProcessEvents, for now the only available --- */
321 /* Process every pending time event, then every pending file event
322 * (that may be registered by time event callbacks just processed).
323 * The behaviour depends upon the setting of flags:
325 * If flags is 0, the function does nothing and returns.
326 * if flags has JIM_ALL_EVENTS set, all event types are processed.
327 * if flags has JIM_FILE_EVENTS set, file events are processed.
328 * if flags has JIM_TIME_EVENTS set, time events are processed.
329 * if flags has JIM_DONT_WAIT set, the function returns as soon as all
330 * the events that are possible to process without waiting are processed.
332 * Returns the number of events processed or -1 if
333 * there are no matching handlers, or -2 on error.
335 int Jim_ProcessEvents(Jim_Interp *interp, int flags)
337 jim_wide sleep_ms = -1;
338 int processed = 0;
339 Jim_EventLoop *eventLoop = Jim_GetAssocData(interp, "eventloop");
340 Jim_FileEvent *fe = eventLoop->fileEventHead;
341 Jim_TimeEvent *te;
342 jim_wide maxId;
344 if ((flags & JIM_FILE_EVENTS) == 0 || fe == NULL) {
345 /* No file events */
346 if ((flags & JIM_TIME_EVENTS) == 0 || eventLoop->timeEventHead == NULL) {
347 /* No time events */
348 return -1;
352 /* Note that we want call select() even if there are no
353 * file events to process as long as we want to process time
354 * events, in order to sleep until the next time event is ready
355 * to fire. */
357 if (flags & JIM_DONT_WAIT) {
358 /* Wait no time */
359 sleep_ms = 0;
361 else if (flags & JIM_TIME_EVENTS) {
362 /* The nearest timer is always at the head of the list */
363 if (eventLoop->timeEventHead) {
364 Jim_TimeEvent *shortest = eventLoop->timeEventHead;
366 /* Calculate the time missing for the nearest
367 * timer to fire. */
368 sleep_ms = shortest->when - JimGetTime(eventLoop);
369 if (sleep_ms < 0) {
370 sleep_ms = 0;
373 else {
374 /* Wait forever */
375 sleep_ms = -1;
379 #ifdef HAVE_SELECT
380 if (flags & JIM_FILE_EVENTS) {
381 int retval;
382 struct timeval tv, *tvp = NULL;
383 fd_set rfds, wfds, efds;
384 int maxfd = -1;
386 FD_ZERO(&rfds);
387 FD_ZERO(&wfds);
388 FD_ZERO(&efds);
390 /* Check file events */
391 while (fe != NULL) {
392 int fd = fileno(fe->handle);
394 if (fe->mask & JIM_EVENT_READABLE)
395 FD_SET(fd, &rfds);
396 if (fe->mask & JIM_EVENT_WRITABLE)
397 FD_SET(fd, &wfds);
398 if (fe->mask & JIM_EVENT_EXCEPTION)
399 FD_SET(fd, &efds);
400 if (maxfd < fd)
401 maxfd = fd;
402 fe = fe->next;
405 if (sleep_ms >= 0) {
406 tvp = &tv;
407 tvp->tv_sec = sleep_ms / 1000;
408 tvp->tv_usec = 1000 * (sleep_ms % 1000);
411 retval = select(maxfd + 1, &rfds, &wfds, &efds, tvp);
413 if (retval < 0) {
414 if (errno == EINVAL) {
415 /* This can happen on mingw32 if a non-socket filehandle is passed */
416 Jim_SetResultString(interp, "non-waitable filehandle", -1);
417 return -2;
420 else if (retval > 0) {
421 fe = eventLoop->fileEventHead;
422 while (fe != NULL) {
423 int fd = fileno(fe->handle);
424 int mask = 0;
426 if ((fe->mask & JIM_EVENT_READABLE) && FD_ISSET(fd, &rfds))
427 mask |= JIM_EVENT_READABLE;
428 if (fe->mask & JIM_EVENT_WRITABLE && FD_ISSET(fd, &wfds))
429 mask |= JIM_EVENT_WRITABLE;
430 if (fe->mask & JIM_EVENT_EXCEPTION && FD_ISSET(fd, &efds))
431 mask |= JIM_EVENT_EXCEPTION;
433 if (mask) {
434 if (fe->fileProc(interp, fe->clientData, mask) != JIM_OK) {
435 /* Remove the element on handler error */
436 Jim_DeleteFileHandler(interp, fe->handle, mask);
438 processed++;
439 /* After an event is processed our file event list
440 * may no longer be the same, so what we do
441 * is to clear the bit for this file descriptor and
442 * restart again from the head. */
443 fe = eventLoop->fileEventHead;
444 FD_CLR(fd, &rfds);
445 FD_CLR(fd, &wfds);
446 FD_CLR(fd, &efds);
448 else {
449 fe = fe->next;
454 #else
455 if (sleep_ms > 0) {
456 msleep(sleep_ms);
458 #endif
460 /* Check time events */
461 te = eventLoop->timeEventHead;
462 maxId = eventLoop->timeEventNextId;
463 while (te) {
464 jim_wide id;
466 if (te->id > maxId) {
467 te = te->next;
468 continue;
470 if (JimGetTime(eventLoop) >= te->when) {
471 id = te->id;
472 /* Remove from the list before executing */
473 Jim_RemoveTimeHandler(eventLoop, id);
474 te->timeProc(interp, te->clientData);
475 /* After an event is processed our time event list may
476 * no longer be the same, so we restart from head.
477 * Still we make sure to don't process events registered
478 * by event handlers itself in order to don't loop forever
479 * even in case an [after 0] that continuously register
480 * itself. To do so we saved the max ID we want to handle. */
481 Jim_FreeTimeHandler(interp, te);
483 te = eventLoop->timeEventHead;
484 processed++;
486 else {
487 te = te->next;
491 return processed;
494 /* ---------------------------------------------------------------------- */
496 static void JimELAssocDataDeleProc(Jim_Interp *interp, void *data)
498 void *next;
499 Jim_FileEvent *fe;
500 Jim_TimeEvent *te;
501 Jim_EventLoop *eventLoop = data;
503 fe = eventLoop->fileEventHead;
504 while (fe) {
505 next = fe->next;
506 if (fe->finalizerProc)
507 fe->finalizerProc(interp, fe->clientData);
508 Jim_Free(fe);
509 fe = next;
512 te = eventLoop->timeEventHead;
513 while (te) {
514 next = te->next;
515 if (te->finalizerProc)
516 te->finalizerProc(interp, te->clientData);
517 Jim_Free(te);
518 te = next;
520 Jim_Free(data);
523 static int JimELVwaitCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
525 Jim_EventLoop *eventLoop = Jim_CmdPrivData(interp);
526 Jim_Obj *oldValue;
527 int rc;
529 if (argc != 2) {
530 Jim_WrongNumArgs(interp, 1, argv, "name");
531 return JIM_ERR;
534 oldValue = Jim_GetGlobalVariable(interp, argv[1], JIM_NONE);
535 if (oldValue) {
536 Jim_IncrRefCount(oldValue);
538 else {
539 /* If a result was left, it is an error */
540 if (Jim_Length(Jim_GetResult(interp))) {
541 return JIM_ERR;
545 eventLoop->suppress_bgerror = 0;
547 while ((rc = Jim_ProcessEvents(interp, JIM_ALL_EVENTS)) >= 0) {
548 Jim_Obj *currValue;
549 currValue = Jim_GetGlobalVariable(interp, argv[1], JIM_NONE);
550 /* Stop the loop if the vwait-ed variable changed value,
551 * or if was unset and now is set (or the contrary)
552 * or if a signal was caught
554 if ((oldValue && !currValue) ||
555 (!oldValue && currValue) ||
556 (oldValue && currValue && !Jim_StringEqObj(oldValue, currValue)) ||
557 Jim_CheckSignal(interp)) {
558 break;
561 if (oldValue)
562 Jim_DecrRefCount(interp, oldValue);
564 if (rc == -2) {
565 return JIM_ERR;
568 Jim_SetEmptyResult(interp);
569 return JIM_OK;
572 static int JimELUpdateCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
574 Jim_EventLoop *eventLoop = Jim_CmdPrivData(interp);
575 static const char * const options[] = {
576 "idletasks", NULL
578 enum { UPDATE_IDLE, UPDATE_NONE };
579 int option = UPDATE_NONE;
580 int flags = JIM_TIME_EVENTS;
582 if (argc == 1) {
583 flags = JIM_ALL_EVENTS;
585 else if (argc > 2 || Jim_GetEnum(interp, argv[1], options, &option, NULL, JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) {
586 Jim_WrongNumArgs(interp, 1, argv, "?idletasks?");
587 return JIM_ERR;
590 eventLoop->suppress_bgerror = 0;
592 while (Jim_ProcessEvents(interp, flags | JIM_DONT_WAIT) > 0) {
595 return JIM_OK;
598 static void JimAfterTimeHandler(Jim_Interp *interp, void *clientData)
600 Jim_Obj *objPtr = clientData;
602 Jim_EvalObjBackground(interp, objPtr);
605 static void JimAfterTimeEventFinalizer(Jim_Interp *interp, void *clientData)
607 Jim_Obj *objPtr = clientData;
609 Jim_DecrRefCount(interp, objPtr);
612 static int JimELAfterCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
614 Jim_EventLoop *eventLoop = Jim_CmdPrivData(interp);
615 jim_wide ms = 0, id;
616 Jim_Obj *objPtr, *idObjPtr;
617 static const char * const options[] = {
618 "cancel", "info", "idle", NULL
620 enum
621 { AFTER_CANCEL, AFTER_INFO, AFTER_IDLE, AFTER_RESTART, AFTER_EXPIRE, AFTER_CREATE };
622 int option = AFTER_CREATE;
624 if (argc < 2) {
625 Jim_WrongNumArgs(interp, 1, argv, "option ?arg ...?");
626 return JIM_ERR;
628 if (Jim_GetWide(interp, argv[1], &ms) != JIM_OK) {
629 if (Jim_GetEnum(interp, argv[1], options, &option, "argument", JIM_ERRMSG) != JIM_OK) {
630 return JIM_ERR;
632 Jim_SetEmptyResult(interp);
634 else if (argc == 2) {
635 /* Simply a sleep */
636 msleep(ms);
637 return JIM_OK;
640 switch (option) {
641 case AFTER_IDLE:
642 if (argc < 3) {
643 Jim_WrongNumArgs(interp, 2, argv, "script ?script ...?");
644 return JIM_ERR;
646 /* fall through */
647 case AFTER_CREATE: {
648 Jim_Obj *scriptObj = Jim_ConcatObj(interp, argc - 2, argv + 2);
649 Jim_IncrRefCount(scriptObj);
650 id = Jim_CreateTimeHandler(interp, ms, JimAfterTimeHandler, scriptObj,
651 JimAfterTimeEventFinalizer);
652 objPtr = Jim_NewStringObj(interp, NULL, 0);
653 Jim_AppendString(interp, objPtr, "after#", -1);
654 idObjPtr = Jim_NewIntObj(interp, id);
655 Jim_IncrRefCount(idObjPtr);
656 Jim_AppendObj(interp, objPtr, idObjPtr);
657 Jim_DecrRefCount(interp, idObjPtr);
658 Jim_SetResult(interp, objPtr);
659 return JIM_OK;
661 case AFTER_CANCEL:
662 if (argc < 3) {
663 Jim_WrongNumArgs(interp, 2, argv, "id|command");
664 return JIM_ERR;
666 else {
667 jim_wide remain = 0;
669 id = JimParseAfterId(argv[2]);
670 if (id <= 0) {
671 /* Not an event id, so search by script */
672 Jim_Obj *scriptObj = Jim_ConcatObj(interp, argc - 2, argv + 2);
673 id = JimFindAfterByScript(eventLoop, scriptObj);
674 Jim_FreeNewObj(interp, scriptObj);
675 if (id <= 0) {
676 /* Not found */
677 break;
680 remain = Jim_DeleteTimeHandler(interp, id);
681 if (remain >= 0) {
682 Jim_SetResultInt(interp, remain);
685 break;
687 case AFTER_INFO:
688 if (argc == 2) {
689 Jim_TimeEvent *te = eventLoop->timeEventHead;
690 Jim_Obj *listObj = Jim_NewListObj(interp, NULL, 0);
691 char buf[30];
692 const char *fmt = "after#%" JIM_WIDE_MODIFIER;
694 while (te) {
695 snprintf(buf, sizeof(buf), fmt, te->id);
696 Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, buf, -1));
697 te = te->next;
699 Jim_SetResult(interp, listObj);
701 else if (argc == 3) {
702 id = JimParseAfterId(argv[2]);
703 if (id >= 0) {
704 Jim_TimeEvent *e = JimFindTimeHandlerById(eventLoop, id);
705 if (e && e->timeProc == JimAfterTimeHandler) {
706 Jim_Obj *listObj = Jim_NewListObj(interp, NULL, 0);
707 Jim_ListAppendElement(interp, listObj, e->clientData);
708 Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, e->initialms ? "timer" : "idle", -1));
709 Jim_SetResult(interp, listObj);
710 return JIM_OK;
713 Jim_SetResultFormatted(interp, "event \"%#s\" doesn't exist", argv[2]);
714 return JIM_ERR;
716 else {
717 Jim_WrongNumArgs(interp, 2, argv, "?id?");
718 return JIM_ERR;
720 break;
722 return JIM_OK;
725 int Jim_eventloopInit(Jim_Interp *interp)
727 Jim_EventLoop *eventLoop;
729 if (Jim_PackageProvide(interp, "eventloop", "1.0", JIM_ERRMSG))
730 return JIM_ERR;
732 eventLoop = Jim_Alloc(sizeof(*eventLoop));
733 memset(eventLoop, 0, sizeof(*eventLoop));
735 Jim_SetAssocData(interp, "eventloop", JimELAssocDataDeleProc, eventLoop);
737 Jim_CreateCommand(interp, "vwait", JimELVwaitCommand, eventLoop, NULL);
738 Jim_CreateCommand(interp, "update", JimELUpdateCommand, eventLoop, NULL);
739 Jim_CreateCommand(interp, "after", JimELAfterCommand, eventLoop, NULL);
741 return JIM_OK;