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
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.
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
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.
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.
67 /* ------------------------ gettimeofday() ------------------------ */
68 /* not needed with MinGW gcc 3.4.5
75 int gettimeofday( struct timeval *tp, struct timezone *tzp )
78 TIME_ZONE_INFORMATION tzi;
86 tp->tv_usec = lpt.wMilliseconds * 1000;
90 result = GetTimeZoneInformation(&tzi);
91 tzp->tz_minuteswest = tzi.Bias;
92 tzp->tz_dsttime = tzi.DaylightBias;
100 /* ---------------------------- pipes -------------------------------------- */
102 UINT
winPipedProcess(char * cmd
, int inpipe
, int outpipe
, int option
)
104 STARTUPINFO si
= { 0 };
105 PROCESS_INFORMATION process
;
109 /* GetStartupInfo(&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)
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
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))
154 *inpipe
= _open_osfhandle((long) pipe_r
, 0);
155 *outpipe
= _open_osfhandle((long) pipe_w
, 0);
162 /* ---------------------------------- semaphores ---------------------------- */
164 UINT
winCreateSemaphore(void)
166 SECURITY_ATTRIBUTES security
= { 0 };
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
)
182 dwWaitResult
= WaitForSingleObject((HANDLE
)hSemaphore
, INFINITE
);
184 if(dwWaitResult
== WAIT_FAILED
)
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 };
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 -------------------------------------- */
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;
246 CELL
* p_timerEvent(CELL
* params
)
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;
269 /* return the elapsed time */
270 seconds
= ((now
= milliSecTime()) < start
?
271 86400000 - start
+ now
:
274 return(stuffFloat(&seconds
));
277 symCell
= getCell(CELL_SYMBOL
);
278 symCell
->contents
= (UINT
)timerEvent
;
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);
294 while( milliSecTime() < (start
+ timerDuration
) ) mySleep(10);
297 traceFlag
|= TRACE_TIMER
;
299 executeSymbol(timerEvent
, NULL
);