static int Win32_GetVersion(Jim_Interp *interp, int objc, Jim_Obj *const *objv) { JIM_NOTUSED(objc); JIM_NOTUSED(objv); Jim_SetResult(interp, Jim_NewIntObj(interp, GetVersion())); return JIM_OK; }
static int Win32_GetActiveWindow(Jim_Interp *interp, int objc, Jim_Obj *const objv[]) { JIM_NOTUSED(objc); JIM_NOTUSED(objv); Jim_SetResult(interp, Jim_NewIntObj(interp, (DWORD)GetActiveWindow())); return JIM_OK; }
static int Win32_GetCursor(Jim_Interp *interp, int objc, Jim_Obj *const objv[]) { HCURSOR hCursor; JIM_NOTUSED(objc); JIM_NOTUSED(objv); hCursor = GetCursor(); Jim_SetResult(interp, Jim_NewIntObj(interp, (DWORD)hCursor)); return JIM_OK; }
static int Win32_GetSystemInfo(Jim_Interp *interp, int objc, Jim_Obj *const *objv) { Jim_Obj *a[20]; SYSTEM_INFO si; int n = 0; struct pa_map { int arch; const char *name; }; struct pa_map *p, map[] = { { PROCESSOR_ARCHITECTURE_INTEL, "intel" }, { PROCESSOR_ARCHITECTURE_MIPS, "mips" }, { PROCESSOR_ARCHITECTURE_ALPHA, "alpha" }, { PROCESSOR_ARCHITECTURE_PPC, "ppc" }, { PROCESSOR_ARCHITECTURE_SHX, "shx" }, { PROCESSOR_ARCHITECTURE_ARM, "arm" }, { PROCESSOR_ARCHITECTURE_IA64, "ia64" }, { PROCESSOR_ARCHITECTURE_ALPHA64,"alpha64" }, { PROCESSOR_ARCHITECTURE_MSIL, "msil" }, { PROCESSOR_ARCHITECTURE_AMD64, "amd64"}, { PROCESSOR_ARCHITECTURE_IA32_ON_WIN64, "ia32onwin64" }, { PROCESSOR_ARCHITECTURE_UNKNOWN,"unknown" } }; JIM_NOTUSED(objc); JIM_NOTUSED(objv); GetSystemInfo(&si); a[n++] = Jim_NewStringObj(interp, "ProcessorArchitecture", -1); for (p = map; p->arch != PROCESSOR_ARCHITECTURE_UNKNOWN; ++p) { if (p->arch == si.wProcessorArchitecture) { break; } } a[n++] = Jim_NewStringObj(interp, p->name, -1); #define JIMADD(name,element) \ a[n++] = Jim_NewStringObj(interp, #name, -1); \ a[n++] = Jim_NewIntObj(interp, (jim_wide)si. ## element ) JIMADD(PageSize, dwPageSize); JIMADD(MinimumApplicationAddress, lpMinimumApplicationAddress); JIMADD(MaximumApplicationAddress, lpMaximumApplicationAddress); JIMADD(ActiveProcessorMask, dwActiveProcessorMask); JIMADD(NumberOfProcessors, dwNumberOfProcessors); JIMADD(ProcessorType, dwProcessorType); JIMADD(AllocationGranularity, dwAllocationGranularity); JIMADD(ProcessorLevel, wProcessorLevel); JIMADD(ProcessorRevision, wProcessorRevision); #undef JIMADD Jim_SetResult(interp, Jim_NewListObj(interp, a, n)); return JIM_OK; }
static int Win32_GetPerformanceInfo(Jim_Interp *interp, int objc, Jim_Obj *const *objv) { Jim_Obj *a[26]; size_t n = 0; PERFORMANCE_INFORMATION pi; LPFNGETPERFORMANCEINFO lpfnGetPerformanceInfo = NULL; HMODULE hLib = (HMODULE)Jim_CmdPrivData(interp); JIM_NOTUSED(objc); JIM_NOTUSED(objv); if (hLib != NULL) lpfnGetPerformanceInfo = (LPFNGETPERFORMANCEINFO)GetProcAddress(hLib, "GetPerformanceInfo"); if (lpfnGetPerformanceInfo == NULL) { /* should never happen */ Jim_SetResultString(interp, "argh!", -1); return JIM_ERR; } pi.cb = sizeof(pi); if (!lpfnGetPerformanceInfo(&pi, sizeof(pi))) { Jim_SetResult(interp, Win32ErrorObj(interp, "GetPerformanceInfo", GetLastError())); return JIM_ERR; } #define JIMADD(name) \ a[n++] = Jim_NewStringObj(interp, #name, -1); \ a[n++] = Jim_NewIntObj(interp, pi.name ) JIMADD(CommitTotal); JIMADD(CommitLimit); JIMADD(CommitPeak); JIMADD(PhysicalTotal); JIMADD(PhysicalAvailable); JIMADD(SystemCache); JIMADD(KernelTotal); JIMADD(KernelPaged); JIMADD(KernelNonpaged); JIMADD(PageSize); JIMADD(HandleCount); JIMADD(ProcessCount); JIMADD(ThreadCount); #undef JIMADD Jim_SetResult(interp, Jim_NewListObj(interp, a, n)); return JIM_OK; }
static void JimAioDelProc(Jim_Interp *interp, void *privData) { AioFile *af = privData; JIM_NOTUSED(interp); if (!(af->OpenFlags & AIO_KEEPOPEN)) fclose(af->fp); if (!af->OpenFlags == AIO_FDOPEN) // fp = fdopen(fd) !! close(af->fd); if (af->rEvent) { // remove existing EventHandlers fprintf(stderr,"deleting ReadEvent\n"); Jim_DeleteFileHandler(interp,af->fp); Jim_DecrRefCount(interp,af->rEvent); } if (af->wEvent) { fprintf(stderr,"deleting WriteEvent\n"); Jim_DeleteFileHandler(interp,af->fp); Jim_DecrRefCount(interp,af->wEvent); } if (af->eEvent) { fprintf(stderr,"deleting ExceptionEvent\n"); Jim_DeleteFileHandler(interp,af->fp); Jim_DecrRefCount(interp,af->eEvent); } Jim_Free(af); }
static void JimAioDelProc(Jim_Interp *interp, void *privData) { AioFile *af = privData; JIM_NOTUSED(interp); if (!(af->OpenFlags & AIO_KEEPOPEN)) { fclose(af->fp); } Jim_DecrRefCount(interp, af->filename); #ifdef jim_ext_eventloop /* remove existing EventHandlers */ if (af->rEvent) { Jim_DeleteFileHandler(interp, af->fp); } if (af->wEvent) { Jim_DeleteFileHandler(interp, af->fp); } if (af->eEvent) { Jim_DeleteFileHandler(interp, af->fp); } #endif Jim_Free(af); }
static void JimSdlDelProc(Jim_Interp *interp, void *privData) { JimSdlSurface *jss = privData; JIM_NOTUSED(interp); SDL_FreeSurface(jss->screen); Jim_Free(jss); }
static void Win32_ReleasePrivLib(Jim_Interp *interp, void *clientData) { HMODULE hLib = (HMODULE)clientData; JIM_NOTUSED(interp); if (hLib) FreeLibrary(hLib); }
static void JimSqliteDelProc(Jim_Interp *interp, void *privData) { sqlite3 *db = privData; JIM_NOTUSED(interp); sqlite3_close(db); }
static void JimAioDelProc(Jim_Interp *interp, void *privData) { AioFile *af = privData; JIM_NOTUSED(interp); if (!af->keepOpen) fclose(af->fp); Jim_Free(af); }
static int Win32_GetCursorPos(Jim_Interp *interp, int objc, Jim_Obj *const objv[]) { Jim_Obj *a[2]; POINT pt; JIM_NOTUSED(objc); JIM_NOTUSED(objv); if (!GetCursorPos(&pt)) { Jim_SetResult(interp, Win32ErrorObj(interp, "GetCursorPos", GetLastError())); return JIM_ERR; } a[0] = Jim_NewIntObj(interp, pt.x); a[1] = Jim_NewIntObj(interp, pt.y); Jim_SetResult(interp, Jim_NewListObj(interp, a, 2)); return JIM_OK; }
BOOL APIENTRY DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID reserved) { JIM_NOTUSED(reserved); if (dwReason == DLL_PROCESS_ATTACH) { g_hInstance = hInstance; } return TRUE; }
static int Jim_PosixForkCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { pid_t pid; JIM_NOTUSED(argv); if (argc != 1) { Jim_WrongNumArgs(interp, 1, argv, ""); return JIM_ERR; } if ((pid = fork()) == -1) { Jim_SetResultString(interp, strerror(errno), -1); return JIM_ERR; } Jim_SetResult(interp, Jim_NewIntObj(interp, (jim_wide)pid)); return JIM_OK; }
static int Jim_PosixForkCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { pid_t pid; JIM_NOTUSED(argv); if (argc != 1) { Jim_WrongNumArgs(interp, 1, argv, ""); return JIM_ERR; } if ((pid = fork()) == -1) { Jim_PosixSetError(interp); return JIM_ERR; } Jim_SetResultInt(interp, (jim_wide) pid); return JIM_OK; }
/* Process every pending time event, then every pending file event * (that may be registered by time event callbacks just processed). * Without special flags the function sleeps until some file event * fires, or when the next time event occurrs (if any). * * If flags is 0, the function does nothing and returns. * if flags has JIM_ALL_EVENTS set, all the kind of events are processed. * if flags has JIM_FILE_EVENTS set, file events are processed. * if flags has JIM_TIME_EVENTS set, time events are processed. * if flags has JIM_DONT_WAIT set the function returns ASAP until all * the events that's possible to process without to wait are processed. * * The function returns the number of events processed. */ int Jim_ProcessEvents(Jim_Interp *interp, int flags) { int maxfd = 0, numfd = 0, processed = 0; fd_set rfds, wfds, efds; Jim_EventLoop *eventLoop = Jim_GetAssocData(interp, "eventloop"); Jim_FileEvent *fe = eventLoop->fileEventHead; Jim_TimeEvent *te; jim_wide maxId; JIM_NOTUSED(flags); FD_ZERO(&rfds); FD_ZERO(&wfds); FD_ZERO(&efds); /* Check file events */ while (fe != NULL) { int fd = fileno((FILE*)fe->handle); if (fe->mask & JIM_EVENT_READABLE) FD_SET(fd, &rfds); if (fe->mask & JIM_EVENT_WRITABLE) FD_SET(fd, &wfds); if (fe->mask & JIM_EVENT_EXCEPTION) FD_SET(fd, &efds); if (maxfd < fd) maxfd = fd; numfd++; fe = fe->next; } /* Note that we want call select() even if there are no * file events to process as long as we want to process time * events, in order to sleep until the next time event is ready * to fire. */ if (numfd || ((flags & JIM_TIME_EVENTS) && !(flags & JIM_DONT_WAIT))) { int retval; Jim_TimeEvent *shortest; struct timeval tv, *tvp; jim_wide dt; shortest = JimSearchNearestTimer(eventLoop); if (shortest) { long now_sec, now_ms; /* Calculate the time missing for the nearest * timer to fire. */ JimGetTime(&now_sec, &now_ms); tvp = &tv; dt = 1000 * (shortest->when_sec - now_sec); dt += ( shortest->when_ms - now_ms); if (dt < 0) { dt = 1; } tvp->tv_sec = dt / 1000; tvp->tv_usec = dt % 1000; // fprintf(stderr,"Next %d.% 8d\n",(int)tvp->tv_sec,(int)tvp->tv_usec); } else { tvp = NULL; /* wait forever */ // fprintf(stderr,"No Event\n"); } retval = select(maxfd+1, &rfds, &wfds, &efds, tvp); if (retval < 0) { switch (errno) { case EINTR: fprintf(stderr,"select EINTR\n"); break; case EINVAL: fprintf(stderr,"select EINVAL\n"); break; case ENOMEM: fprintf(stderr,"select ENOMEM\n"); break; } } else if (retval > 0) { fe = eventLoop->fileEventHead; while(fe != NULL) { int fd = fileno((FILE*)fe->handle); // fprintf(stderr,"fd: %d mask: %02x \n",fd,fe->mask); if ((fe->mask & JIM_EVENT_READABLE && FD_ISSET(fd, &rfds)) || (fe->mask & JIM_EVENT_WRITABLE && FD_ISSET(fd, &wfds)) || (fe->mask & JIM_EVENT_EXCEPTION && FD_ISSET(fd, &efds))) { int mask = 0; if (fe->mask & JIM_EVENT_READABLE && FD_ISSET(fd, &rfds)) { mask |= JIM_EVENT_READABLE; if ((fe->mask & JIM_EVENT_FEOF) && feof((FILE *)fe->handle)) mask |= JIM_EVENT_FEOF; } if (fe->mask & JIM_EVENT_WRITABLE && FD_ISSET(fd, &wfds)) mask |= JIM_EVENT_WRITABLE; if (fe->mask & JIM_EVENT_EXCEPTION && FD_ISSET(fd, &efds)) mask |= JIM_EVENT_EXCEPTION; if (fe->fileProc(interp, fe->clientData, mask) == JIM_ERR) { /* Remove the element on handler error */ Jim_DeleteFileHandler(interp, fe->handle); } processed++; /* After an event is processed our file event list * may no longer be the same, so what we do * is to clear the bit for this file descriptor and * restart again from the head. */ fe = eventLoop->fileEventHead; FD_CLR(fd, &rfds); FD_CLR(fd, &wfds); FD_CLR(fd, &efds); } else { fe = fe->next; } } } } /* Check time events */ te = eventLoop->timeEventHead; maxId = eventLoop->timeEventNextId-1; while(te) { long now_sec, now_ms; jim_wide id; if (te->id > maxId) { te = te->next; continue; } JimGetTime(&now_sec, &now_ms); if (now_sec > te->when_sec || (now_sec == te->when_sec && now_ms >= te->when_ms)) { id = te->id; te->timeProc(interp, te->clientData); /* After an event is processed our time event list may * no longer be the same, so we restart from head. * Still we make sure to don't process events registered * by event handlers itself in order to don't loop forever * even in case an [after 0] that continuously register * itself. To do so we saved the max ID we want to handle. */ Jim_DeleteTimeHandler(interp, id); te = eventLoop->timeEventHead; } else { te = te->next; } } return processed; }