Initial commit of newLISP.
[newlisp.git] / win32-util.c
blobf36537ebc8f92aa151b5fc7ddddef632f00d962b
1 /*
2 win32-util.c, utitity routines for native win32 port of newLISP
4 Copyright (C) 2008 Lutz Mueller
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 // for function: gettimeofday()
22 // Copyright (C) 1992-2002 Steve Adams
23 //
24 // This program is free software; you can redistribute it and/or modify
25 // it under the terms of the GNU General Public License version 2, 1991,
26 // as published by the Free Software Foundation.
28 // This program is distributed in the hope that it will be useful,
29 // but WITHOUT ANY WARRANTY; without even the implied warranty of
30 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 // GNU General Public License for more details.
32 //
33 // You should have received a copy of the GNU General Public License
34 // along with this program; if not, write to the Free Software
35 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
41 // Copyright (c) 1992-2007 Lutz Mueller
42 //
43 // This program is free software; you can redistribute it and/or modify
44 // it under the terms of the GNU General Public License version 2, 1991,
45 // as published by the Free Software Foundation.
47 // This program is distributed in the hope that it will be useful,
48 // but WITHOUT ANY WARRANTY; without even the implied warranty of
49 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50 // GNU General Public License for more details.
51 //
52 // You should have received a copy of the GNU General Public License
53 // along with this program; if not, write to the Free Software
54 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <errno.h>
63 #include <time.h>
64 #include <windows.h>
65 #include <io.h>
67 /* ------------------------ gettimeofday() ------------------------ */
68 /* not needed with MinGW gcc 3.4.5
70 struct timezone {
71 int tz_minuteswest;
72 int tz_dsttime;
75 int gettimeofday( struct timeval *tp, struct timezone *tzp )
77 SYSTEMTIME lpt;
78 TIME_ZONE_INFORMATION tzi;
79 int result = 0;
81 time_t Now;
82 Now = time( NULL );
83 tp->tv_sec = Now;
85 GetSystemTime(&lpt);
86 tp->tv_usec = lpt.wMilliseconds * 1000;
88 if(tzp != NULL)
90 result = GetTimeZoneInformation(&tzi);
91 tzp->tz_minuteswest = tzi.Bias;
92 tzp->tz_dsttime = tzi.DaylightBias;
95 return(result);
100 /* ---------------------------- pipes -------------------------------------- */
102 UINT winPipedProcess(char * cmd, int inpipe, int outpipe, int option)
104 STARTUPINFO si = { 0 };
105 PROCESS_INFORMATION process;
106 int result;
107 long fin, fout;
109 /* GetStartupInfo(&si); */
110 si.cb = sizeof(si);
111 si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
112 si.wShowWindow = option; /* SW_SHOW, SW_HIDE get additional user option in Win32 versions */
114 fin = _get_osfhandle(inpipe);
115 fout = _get_osfhandle(outpipe);
117 si.hStdInput = (inpipe) ? (HANDLE)fin : GetStdHandle(STD_INPUT_HANDLE);
118 si.hStdOutput = (outpipe) ? (HANDLE)fout : GetStdHandle(STD_OUTPUT_HANDLE);
119 si.hStdError = (outpipe) ? (HANDLE)fout : GetStdHandle(STD_OUTPUT_HANDLE);
121 if((result = CreateProcess(NULL,cmd,NULL,NULL,TRUE,DETACHED_PROCESS,NULL,NULL,&si, &process)) == 0)
122 return(0);
124 /* let this be handled by parent process
125 CloseHandle((HANDLE)fin);
126 CloseHandle((HANDLE)fout);
129 return((UINT)process.hProcess);
133 typedef struct _PROCESS_INFORMATION { // pi
134 HANDLE hProcess;
135 HANDLE hThread;
136 DWORD dwProcessId;
137 DWORD dwThreadId;
138 } PROCESS_INFORMATION;
142 int winPipe(UINT * inpipe, UINT * outpipe)
144 SECURITY_ATTRIBUTES sa = { 0 };
145 HANDLE pipe_r, pipe_w;
147 sa.nLength = sizeof(sa);
148 sa.bInheritHandle = TRUE;
149 sa.lpSecurityDescriptor = NULL;
151 if(!CreatePipe(&pipe_r, &pipe_w, &sa, 0))
152 return(0);
154 *inpipe = _open_osfhandle((long) pipe_r, 0);
155 *outpipe = _open_osfhandle((long) pipe_w, 0);
158 return(1);
162 /* ---------------------------------- semaphores ---------------------------- */
164 UINT winCreateSemaphore(void)
166 SECURITY_ATTRIBUTES security = { 0 };
167 HANDLE hSemaphore;
169 security.nLength = sizeof(security);
170 security.bInheritHandle = TRUE;
171 security.lpSecurityDescriptor = NULL; /* default of caller */
173 hSemaphore = CreateSemaphore(&security, 0, 65536, NULL);
175 return((UINT)hSemaphore);
178 UINT winWaitSemaphore(UINT hSemaphore)
180 DWORD dwWaitResult;
182 dwWaitResult = WaitForSingleObject((HANDLE)hSemaphore, INFINITE);
184 if(dwWaitResult == WAIT_FAILED)
185 return(FALSE);
187 return(TRUE);
190 UINT winSignalSemaphore(UINT hSemaphore, int count)
192 return(ReleaseSemaphore((HANDLE)hSemaphore, count, NULL));
195 int winDeleteSemaphore(UINT hSemaphore)
197 return (CloseHandle((HANDLE)hSemaphore));
201 See: http://www.codeguru.com/Cpp/W-P/win32/article.php/c1423/
202 QuerySemaphore() call
203 int winGetSemaphoreCount(UINT hSemaphore) {}
206 /* ---------------------------- shared memory interface -------------------- */
208 UINT winSharedMemory(int size)
210 SECURITY_ATTRIBUTES sa = { 0 };
211 HANDLE hMemory;
213 sa.nLength = sizeof(sa);
214 sa.bInheritHandle = TRUE;
215 sa.lpSecurityDescriptor = NULL; /* default of caller */
217 hMemory = CreateFileMapping((HANDLE)0xFFFFFFFF, &sa, PAGE_READWRITE, 0, size, NULL);
219 return((UINT)hMemory);
222 UINT * winMapView(UINT hMemory, int size)
224 return((UINT*)MapViewOfFile((HANDLE)hMemory, FILE_MAP_WRITE, 0, 0, size));
227 /* ---------------------------- timer -------------------------------------- */
230 #include "newlisp.h"
231 #include "protos.h"
233 extern SYMBOL * timerEvent;
234 extern int milliSecTime(void);
237 UINT_PTR SetTimer(HWND hWnd, UINT_PTR nIDEvent, UINT uElapse, TIMERPROC lpTimerFunc);
238 BOOL KillTimer(HWND hWnd, UINT_PTR uIDEvent);
241 void timerFunc(void * dummy);
242 int timerDuration = 0;
243 int rollover = 0;
244 int start;
246 CELL * p_timerEvent(CELL * params)
248 CELL * symCell;
249 double seconds;
250 int now;
252 if(params != nilCell)
254 params = getSymbol(params, &timerEvent);
256 if(params != nilCell)
258 getFloat(params, &seconds);
259 timerDuration = 1000 * seconds;
260 if(timerDuration > 0)
262 start = milliSecTime();
263 _beginthread(timerFunc, 0, 0);
264 seconds = timerDuration/1000.0;
267 else
269 /* return the elapsed time */
270 seconds = ((now = milliSecTime()) < start ?
271 86400000 - start + now :
272 now - start)/1000.0;
274 return(stuffFloat(&seconds));
277 symCell = getCell(CELL_SYMBOL);
278 symCell->contents = (UINT)timerEvent;
279 return(symCell);
283 void timerFunc(void * dummy)
285 if(timerDuration == 0) return;
287 /* take care of midnight rollover */
288 if((rollover = start + timerDuration - 86400000) > 0)
290 while(milliSecTime() > start) mySleep(10); /* wait for rollover */
291 while(milliSecTime() < rollover) mySleep(10);
293 else
294 while( milliSecTime() < (start + timerDuration) ) mySleep(10);
296 if(recursionCount)
297 traceFlag |= TRACE_TIMER;
298 else /* if idle */
299 executeSymbol(timerEvent, NULL);
302 /* eof */