format: fix format %hd on some platforms
[jimtcl.git] / jim-eventloop.c
blob936103bcb0728bb4cff2b5ae9e1462e579912f06
2 /* Jim - A small embeddable Tcl interpreter
4 * Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
5 * Copyright 2005 Clemens Hintze <c.hintze@gmx.net>
6 * Copyright 2005 patthoyts - Pat Thoyts <patthoyts@users.sf.net>
7 * Copyright 2008 oharboe - Øyvind Harboe - oyvind.harboe@zylin.com
8 * Copyright 2008 Andrew Lunn <andrew@lunn.ch>
9 * Copyright 2008 Duane Ellis <openocd@duaneellis.com>
10 * Copyright 2008 Uwe Klein <uklein@klein-messgeraete.de>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE JIM TCL PROJECT ``AS IS'' AND ANY
24 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
26 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * The views and conclusions contained in the software and documentation
37 * are those of the authors and should not be interpreted as representing
38 * official policies, either expressed or implied, of the Jim Tcl Project.
39 **/
41 #include "jimautoconf.h"
42 #include <jim.h>
43 #include <jim-eventloop.h>
45 /* POSIX includes */
46 #include <sys/time.h>
47 #include <sys/types.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <errno.h>
52 #if defined(__MINGW32__)
53 #include <windows.h>
54 #include <winsock.h>
55 #define msleep Sleep
56 #else
57 #ifdef HAVE_SYS_SELECT_H
58 #include <sys/select.h>
59 #endif
61 #ifndef HAVE_USLEEP
62 /* XXX: Implement this in terms of select() or nanosleep() */
63 #define msleep(MS) sleep((MS) / 1000)
64 #warning "sub-second sleep not supported"
65 #else
66 #define msleep(MS) sleep((MS) / 1000); usleep(((MS) % 1000) * 1000);
67 #endif
68 #endif
70 /* --- */
72 /* File event structure */
73 typedef struct Jim_FileEvent
75 FILE *handle;
76 int mask; /* one of JIM_EVENT_(READABLE|WRITABLE|EXCEPTION) */
77 Jim_FileProc *fileProc;
78 Jim_EventFinalizerProc *finalizerProc;
79 void *clientData;
80 struct Jim_FileEvent *next;
81 } Jim_FileEvent;
83 /* Time event structure */
84 typedef struct Jim_TimeEvent
86 jim_wide id; /* time event identifier. */
87 int mode; /* restart, repetitive .. UK */
88 long initialms; /* initial relativ timer value UK */
89 jim_wide when; /* milliseconds */
90 Jim_TimeProc *timeProc;
91 Jim_EventFinalizerProc *finalizerProc;
92 void *clientData;
93 struct Jim_TimeEvent *next;
94 } Jim_TimeEvent;
96 /* Per-interp stucture containing the state of the event loop */
97 typedef struct Jim_EventLoop
99 Jim_FileEvent *fileEventHead;
100 Jim_TimeEvent *timeEventHead;
101 jim_wide timeEventNextId; /* highest event id created, starting at 1 */
102 time_t timeBase;
103 int suppress_bgerror; /* bgerror returned break, so don't call it again */
104 } Jim_EventLoop;
106 static void JimAfterTimeHandler(Jim_Interp *interp, void *clientData);
107 static void JimAfterTimeEventFinalizer(Jim_Interp *interp, void *clientData);
109 int Jim_EvalObjBackground(Jim_Interp *interp, Jim_Obj *scriptObjPtr)
111 Jim_EventLoop *eventLoop = Jim_GetAssocData(interp, "eventloop");
112 Jim_CallFrame *savedFramePtr;
113 int retval;
115 savedFramePtr = interp->framePtr;
116 interp->framePtr = interp->topFramePtr;
117 retval = Jim_EvalObj(interp, scriptObjPtr);
118 interp->framePtr = savedFramePtr;
119 /* Try to report the error (if any) via the bgerror proc */
120 if (retval != JIM_OK && !eventLoop->suppress_bgerror) {
121 Jim_Obj *objv[2];
122 int rc = JIM_ERR;
124 objv[0] = Jim_NewStringObj(interp, "bgerror", -1);
125 objv[1] = Jim_GetResult(interp);
126 Jim_IncrRefCount(objv[0]);
127 Jim_IncrRefCount(objv[1]);
128 if (Jim_GetCommand(interp, objv[0], JIM_NONE) == NULL || (rc = Jim_EvalObjVector(interp, 2, objv)) != JIM_OK) {
129 if (rc == JIM_BREAK) {
130 /* No more bgerror calls */
131 eventLoop->suppress_bgerror++;
133 else {
134 /* Report the error to stderr. */
135 Jim_MakeErrorMessage(interp);
136 fprintf(stderr, "%s\n", Jim_String(Jim_GetResult(interp)));
137 /* And reset the result */
138 Jim_SetResultString(interp, "", -1);
141 Jim_DecrRefCount(interp, objv[0]);
142 Jim_DecrRefCount(interp, objv[1]);
144 return retval;
148 void Jim_CreateFileHandler(Jim_Interp *interp, FILE * handle, int mask,
149 Jim_FileProc * proc, void *clientData, Jim_EventFinalizerProc * finalizerProc)
151 Jim_FileEvent *fe;
152 Jim_EventLoop *eventLoop = Jim_GetAssocData(interp, "eventloop");
154 fe = Jim_Alloc(sizeof(*fe));
155 fe->handle = handle;
156 fe->mask = mask;
157 fe->fileProc = proc;
158 fe->finalizerProc = finalizerProc;
159 fe->clientData = clientData;
160 fe->next = eventLoop->fileEventHead;
161 eventLoop->fileEventHead = fe;
165 * Removes all event handlers for 'handle' that match 'mask'.
167 void Jim_DeleteFileHandler(Jim_Interp *interp, FILE * handle, int mask)
169 Jim_FileEvent *fe, *next, *prev = NULL;
170 Jim_EventLoop *eventLoop = Jim_GetAssocData(interp, "eventloop");
172 for (fe = eventLoop->fileEventHead; fe; fe = next) {
173 next = fe->next;
174 if (fe->handle == handle && (fe->mask & mask)) {
175 /* Remove this entry from the list */
176 if (prev == NULL)
177 eventLoop->fileEventHead = next;
178 else
179 prev->next = next;
180 if (fe->finalizerProc)
181 fe->finalizerProc(interp, fe->clientData);
182 Jim_Free(fe);
183 continue;
185 prev = fe;
190 * Returns the time since interp creation in milliseconds.
192 static jim_wide JimGetTime(Jim_EventLoop *eventLoop)
194 struct timeval tv;
196 gettimeofday(&tv, NULL);
198 return (jim_wide)(tv.tv_sec - eventLoop->timeBase) * 1000 + tv.tv_usec / 1000;
201 jim_wide Jim_CreateTimeHandler(Jim_Interp *interp, jim_wide milliseconds,
202 Jim_TimeProc * proc, void *clientData, Jim_EventFinalizerProc * finalizerProc)
204 Jim_EventLoop *eventLoop = Jim_GetAssocData(interp, "eventloop");
205 jim_wide id = ++eventLoop->timeEventNextId;
206 Jim_TimeEvent *te, *e, *prev;
208 te = Jim_Alloc(sizeof(*te));
209 te->id = id;
210 te->mode = 0;
211 te->initialms = milliseconds;
212 te->when = JimGetTime(eventLoop) + milliseconds;
213 te->timeProc = proc;
214 te->finalizerProc = finalizerProc;
215 te->clientData = clientData;
217 /* Add to the appropriate place in the list */
218 prev = NULL;
219 for (e = eventLoop->timeEventHead; e; e = e->next) {
220 if (te->when < e->when) {
221 break;
223 prev = e;
225 if (prev) {
226 te->next = prev->next;
227 prev->next = te;
229 else {
230 te->next = eventLoop->timeEventHead;
231 eventLoop->timeEventHead = te;
234 return id;
237 static jim_wide JimParseAfterId(Jim_Obj *idObj)
239 const char *tok = Jim_String(idObj);
240 jim_wide id;
242 if (strncmp(tok, "after#", 6) == 0 && Jim_StringToWide(tok + 6, &id, 10) == JIM_OK) {
243 /* Got an event by id */
244 return id;
246 return -1;
249 static jim_wide JimFindAfterByScript(Jim_EventLoop *eventLoop, Jim_Obj *scriptObj)
251 Jim_TimeEvent *te;
253 for (te = eventLoop->timeEventHead; te; te = te->next) {
254 /* Is this an 'after' event? */
255 if (te->timeProc == JimAfterTimeHandler) {
256 if (Jim_StringEqObj(scriptObj, te->clientData)) {
257 return te->id;
261 return -1; /* NO event with the specified ID found */
264 static Jim_TimeEvent *JimFindTimeHandlerById(Jim_EventLoop *eventLoop, jim_wide id)
266 Jim_TimeEvent *te;
268 for (te = eventLoop->timeEventHead; te; te = te->next) {
269 if (te->id == id) {
270 return te;
273 return NULL;
276 static Jim_TimeEvent *Jim_RemoveTimeHandler(Jim_EventLoop *eventLoop, jim_wide id)
278 Jim_TimeEvent *te, *prev = NULL;
280 for (te = eventLoop->timeEventHead; te; te = te->next) {
281 if (te->id == id) {
282 if (prev == NULL)
283 eventLoop->timeEventHead = te->next;
284 else
285 prev->next = te->next;
286 return te;
288 prev = te;
290 return NULL;
293 static void Jim_FreeTimeHandler(Jim_Interp *interp, Jim_TimeEvent *te)
295 if (te->finalizerProc)
296 te->finalizerProc(interp, te->clientData);
297 Jim_Free(te);
300 jim_wide Jim_DeleteTimeHandler(Jim_Interp *interp, jim_wide id)
302 Jim_TimeEvent *te;
303 Jim_EventLoop *eventLoop = Jim_GetAssocData(interp, "eventloop");
305 if (id > eventLoop->timeEventNextId) {
306 return -2; /* wrong event ID */
309 te = Jim_RemoveTimeHandler(eventLoop, id);
310 if (te) {
311 jim_wide remain;
313 remain = te->when - JimGetTime(eventLoop);
314 remain = (remain < 0) ? 0 : remain;
316 Jim_FreeTimeHandler(interp, te);
317 return remain;
319 return -1; /* NO event with the specified ID found */
322 /* --- POSIX version of Jim_ProcessEvents, for now the only available --- */
324 /* Process every pending time event, then every pending file event
325 * (that may be registered by time event callbacks just processed).
326 * Without special flags the function sleeps until some file event
327 * fires, or when the next time event occurrs (if any).
329 * If flags is 0, the function does nothing and returns.
330 * if flags has JIM_ALL_EVENTS set, all the kind of events are processed.
331 * if flags has JIM_FILE_EVENTS set, file events are processed.
332 * if flags has JIM_TIME_EVENTS set, time events are processed.
333 * if flags has JIM_DONT_WAIT set the function returns ASAP until all
334 * the events that's possible to process without to wait are processed.
336 * The function returns the number of events processed or -1 if
337 * there are no matching handlers, or -2 on error.
339 int Jim_ProcessEvents(Jim_Interp *interp, int flags)
341 jim_wide sleep_ms = -1;
342 int processed = 0;
343 Jim_EventLoop *eventLoop = Jim_GetAssocData(interp, "eventloop");
344 Jim_FileEvent *fe = eventLoop->fileEventHead;
345 Jim_TimeEvent *te;
346 jim_wide maxId;
348 if ((flags & JIM_FILE_EVENTS) == 0 || fe == NULL) {
349 /* No file events */
350 if ((flags & JIM_TIME_EVENTS) == 0 || eventLoop->timeEventHead == NULL) {
351 /* No time events */
352 return -1;
356 /* Note that we want call select() even if there are no
357 * file events to process as long as we want to process time
358 * events, in order to sleep until the next time event is ready
359 * to fire. */
361 if (flags & JIM_DONT_WAIT) {
362 /* Wait no time */
363 sleep_ms = 0;
365 else if (flags & JIM_TIME_EVENTS) {
366 /* The nearest timer is always at the head of the list */
367 if (eventLoop->timeEventHead) {
368 Jim_TimeEvent *shortest = eventLoop->timeEventHead;
370 /* Calculate the time missing for the nearest
371 * timer to fire. */
372 sleep_ms = shortest->when - JimGetTime(eventLoop);
373 if (sleep_ms < 0) {
374 sleep_ms = 0;
377 else {
378 /* Wait forever */
379 sleep_ms = -1;
383 #ifdef HAVE_SELECT
384 if (flags & JIM_FILE_EVENTS) {
385 int retval;
386 struct timeval tv, *tvp = NULL;
387 fd_set rfds, wfds, efds;
388 int maxfd = -1;
390 FD_ZERO(&rfds);
391 FD_ZERO(&wfds);
392 FD_ZERO(&efds);
394 /* Check file events */
395 while (fe != NULL) {
396 int fd = fileno(fe->handle);
398 if (fe->mask & JIM_EVENT_READABLE)
399 FD_SET(fd, &rfds);
400 if (fe->mask & JIM_EVENT_WRITABLE)
401 FD_SET(fd, &wfds);
402 if (fe->mask & JIM_EVENT_EXCEPTION)
403 FD_SET(fd, &efds);
404 if (maxfd < fd)
405 maxfd = fd;
406 fe = fe->next;
409 if (sleep_ms >= 0) {
410 tvp = &tv;
411 tvp->tv_sec = sleep_ms / 1000;
412 tvp->tv_usec = 1000 * (sleep_ms % 1000);
415 retval = select(maxfd + 1, &rfds, &wfds, &efds, tvp);
417 if (retval < 0) {
418 if (errno == EINVAL) {
419 /* This can happen on mingw32 if a non-socket filehandle is passed */
420 Jim_SetResultString(interp, "non-waitable filehandle", -1);
421 return -2;
424 else if (retval > 0) {
425 fe = eventLoop->fileEventHead;
426 while (fe != NULL) {
427 int fd = fileno(fe->handle);
428 int mask = 0;
430 if ((fe->mask & JIM_EVENT_READABLE) && FD_ISSET(fd, &rfds))
431 mask |= JIM_EVENT_READABLE;
432 if (fe->mask & JIM_EVENT_WRITABLE && FD_ISSET(fd, &wfds))
433 mask |= JIM_EVENT_WRITABLE;
434 if (fe->mask & JIM_EVENT_EXCEPTION && FD_ISSET(fd, &efds))
435 mask |= JIM_EVENT_EXCEPTION;
437 if (mask) {
438 if (fe->fileProc(interp, fe->clientData, mask) != JIM_OK) {
439 /* Remove the element on handler error */
440 Jim_DeleteFileHandler(interp, fe->handle, mask);
442 processed++;
443 /* After an event is processed our file event list
444 * may no longer be the same, so what we do
445 * is to clear the bit for this file descriptor and
446 * restart again from the head. */
447 fe = eventLoop->fileEventHead;
448 FD_CLR(fd, &rfds);
449 FD_CLR(fd, &wfds);
450 FD_CLR(fd, &efds);
452 else {
453 fe = fe->next;
458 #else
459 if (sleep_ms > 0) {
460 msleep(sleep_ms);
462 #endif
464 /* Check time events */
465 te = eventLoop->timeEventHead;
466 maxId = eventLoop->timeEventNextId;
467 while (te) {
468 jim_wide id;
470 if (te->id > maxId) {
471 te = te->next;
472 continue;
474 if (JimGetTime(eventLoop) >= te->when) {
475 id = te->id;
476 /* Remove from the list before executing */
477 Jim_RemoveTimeHandler(eventLoop, id);
478 te->timeProc(interp, te->clientData);
479 /* After an event is processed our time event list may
480 * no longer be the same, so we restart from head.
481 * Still we make sure to don't process events registered
482 * by event handlers itself in order to don't loop forever
483 * even in case an [after 0] that continuously register
484 * itself. To do so we saved the max ID we want to handle. */
485 Jim_FreeTimeHandler(interp, te);
487 te = eventLoop->timeEventHead;
488 processed++;
490 else {
491 te = te->next;
495 return processed;
498 /* ---------------------------------------------------------------------- */
500 static void JimELAssocDataDeleProc(Jim_Interp *interp, void *data)
502 void *next;
503 Jim_FileEvent *fe;
504 Jim_TimeEvent *te;
505 Jim_EventLoop *eventLoop = data;
507 fe = eventLoop->fileEventHead;
508 while (fe) {
509 next = fe->next;
510 if (fe->finalizerProc)
511 fe->finalizerProc(interp, fe->clientData);
512 Jim_Free(fe);
513 fe = next;
516 te = eventLoop->timeEventHead;
517 while (te) {
518 next = te->next;
519 if (te->finalizerProc)
520 te->finalizerProc(interp, te->clientData);
521 Jim_Free(te);
522 te = next;
524 Jim_Free(data);
527 static int JimELVwaitCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
529 Jim_EventLoop *eventLoop = Jim_CmdPrivData(interp);
530 Jim_Obj *oldValue;
531 int rc;
533 if (argc != 2) {
534 Jim_WrongNumArgs(interp, 1, argv, "name");
535 return JIM_ERR;
538 oldValue = Jim_GetGlobalVariable(interp, argv[1], JIM_NONE);
539 if (oldValue) {
540 Jim_IncrRefCount(oldValue);
542 else {
543 /* If a result was left, it is an error */
544 if (Jim_Length(Jim_GetResult(interp))) {
545 return JIM_ERR;
549 eventLoop->suppress_bgerror = 0;
551 while ((rc = Jim_ProcessEvents(interp, JIM_ALL_EVENTS)) >= 0) {
552 Jim_Obj *currValue;
553 currValue = Jim_GetGlobalVariable(interp, argv[1], JIM_NONE);
554 /* Stop the loop if the vwait-ed variable changed value,
555 * or if was unset and now is set (or the contrary)
556 * or if a signal was caught
558 if ((oldValue && !currValue) ||
559 (!oldValue && currValue) ||
560 (oldValue && currValue && !Jim_StringEqObj(oldValue, currValue)) ||
561 Jim_CheckSignal(interp)) {
562 break;
565 if (oldValue)
566 Jim_DecrRefCount(interp, oldValue);
568 if (rc == -2) {
569 return JIM_ERR;
572 Jim_SetEmptyResult(interp);
573 return JIM_OK;
576 static int JimELUpdateCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
578 Jim_EventLoop *eventLoop = Jim_CmdPrivData(interp);
579 static const char * const options[] = {
580 "idletasks", NULL
582 enum { UPDATE_IDLE, UPDATE_NONE };
583 int option = UPDATE_NONE;
584 int flags = JIM_TIME_EVENTS;
586 if (argc == 1) {
587 flags = JIM_ALL_EVENTS;
589 else if (argc > 2 || Jim_GetEnum(interp, argv[1], options, &option, NULL, JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) {
590 Jim_WrongNumArgs(interp, 1, argv, "?idletasks?");
591 return JIM_ERR;
594 eventLoop->suppress_bgerror = 0;
596 while (Jim_ProcessEvents(interp, flags | JIM_DONT_WAIT) > 0) {
599 return JIM_OK;
602 static void JimAfterTimeHandler(Jim_Interp *interp, void *clientData)
604 Jim_Obj *objPtr = clientData;
606 Jim_EvalObjBackground(interp, objPtr);
609 static void JimAfterTimeEventFinalizer(Jim_Interp *interp, void *clientData)
611 Jim_Obj *objPtr = clientData;
613 Jim_DecrRefCount(interp, objPtr);
616 static int JimELAfterCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
618 Jim_EventLoop *eventLoop = Jim_CmdPrivData(interp);
619 jim_wide ms = 0, id;
620 Jim_Obj *objPtr, *idObjPtr;
621 static const char * const options[] = {
622 "cancel", "info", "idle", NULL
624 enum
625 { AFTER_CANCEL, AFTER_INFO, AFTER_IDLE, AFTER_RESTART, AFTER_EXPIRE, AFTER_CREATE };
626 int option = AFTER_CREATE;
628 if (argc < 2) {
629 Jim_WrongNumArgs(interp, 1, argv, "option ?arg ...?");
630 return JIM_ERR;
632 if (Jim_GetWide(interp, argv[1], &ms) != JIM_OK) {
633 if (Jim_GetEnum(interp, argv[1], options, &option, "argument", JIM_ERRMSG) != JIM_OK) {
634 return JIM_ERR;
636 Jim_SetEmptyResult(interp);
638 else if (argc == 2) {
639 /* Simply a sleep */
640 msleep(ms);
641 return JIM_OK;
644 switch (option) {
645 case AFTER_IDLE:
646 if (argc < 3) {
647 Jim_WrongNumArgs(interp, 2, argv, "script ?script ...?");
648 return JIM_ERR;
650 /* fall through */
651 case AFTER_CREATE: {
652 Jim_Obj *scriptObj = Jim_ConcatObj(interp, argc - 2, argv + 2);
653 Jim_IncrRefCount(scriptObj);
654 id = Jim_CreateTimeHandler(interp, ms, JimAfterTimeHandler, scriptObj,
655 JimAfterTimeEventFinalizer);
656 objPtr = Jim_NewStringObj(interp, NULL, 0);
657 Jim_AppendString(interp, objPtr, "after#", -1);
658 idObjPtr = Jim_NewIntObj(interp, id);
659 Jim_IncrRefCount(idObjPtr);
660 Jim_AppendObj(interp, objPtr, idObjPtr);
661 Jim_DecrRefCount(interp, idObjPtr);
662 Jim_SetResult(interp, objPtr);
663 return JIM_OK;
665 case AFTER_CANCEL:
666 if (argc < 3) {
667 Jim_WrongNumArgs(interp, 2, argv, "id|command");
668 return JIM_ERR;
670 else {
671 jim_wide remain = 0;
673 id = JimParseAfterId(argv[2]);
674 if (id <= 0) {
675 /* Not an event id, so search by script */
676 Jim_Obj *scriptObj = Jim_ConcatObj(interp, argc - 2, argv + 2);
677 id = JimFindAfterByScript(eventLoop, scriptObj);
678 Jim_FreeNewObj(interp, scriptObj);
679 if (id <= 0) {
680 /* Not found */
681 break;
684 remain = Jim_DeleteTimeHandler(interp, id);
685 if (remain >= 0) {
686 Jim_SetResultInt(interp, remain);
689 break;
691 case AFTER_INFO:
692 if (argc == 2) {
693 Jim_TimeEvent *te = eventLoop->timeEventHead;
694 Jim_Obj *listObj = Jim_NewListObj(interp, NULL, 0);
695 char buf[30];
696 const char *fmt = "after#%" JIM_WIDE_MODIFIER;
698 while (te) {
699 snprintf(buf, sizeof(buf), fmt, te->id);
700 Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, buf, -1));
701 te = te->next;
703 Jim_SetResult(interp, listObj);
705 else if (argc == 3) {
706 id = JimParseAfterId(argv[2]);
707 if (id >= 0) {
708 Jim_TimeEvent *e = JimFindTimeHandlerById(eventLoop, id);
709 if (e && e->timeProc == JimAfterTimeHandler) {
710 Jim_Obj *listObj = Jim_NewListObj(interp, NULL, 0);
711 Jim_ListAppendElement(interp, listObj, e->clientData);
712 Jim_ListAppendElement(interp, listObj, Jim_NewStringObj(interp, e->initialms ? "timer" : "idle", -1));
713 Jim_SetResult(interp, listObj);
714 return JIM_OK;
717 Jim_SetResultFormatted(interp, "event \"%#s\" doesn't exist", argv[2]);
718 return JIM_ERR;
720 else {
721 Jim_WrongNumArgs(interp, 2, argv, "?id?");
722 return JIM_ERR;
724 break;
726 return JIM_OK;
729 int Jim_eventloopInit(Jim_Interp *interp)
731 Jim_EventLoop *eventLoop;
733 if (Jim_PackageProvide(interp, "eventloop", "1.0", JIM_ERRMSG))
734 return JIM_ERR;
736 eventLoop = Jim_Alloc(sizeof(*eventLoop));
737 memset(eventLoop, 0, sizeof(*eventLoop));
739 Jim_SetAssocData(interp, "eventloop", JimELAssocDataDeleProc, eventLoop);
741 Jim_CreateCommand(interp, "vwait", JimELVwaitCommand, eventLoop, NULL);
742 Jim_CreateCommand(interp, "update", JimELUpdateCommand, eventLoop, NULL);
743 Jim_CreateCommand(interp, "after", JimELAfterCommand, eventLoop, NULL);
745 return JIM_OK;