2000-02-17 20:38:33 +01:00
|
|
|
/* window.cc: hidden windows for signals/itimer support
|
|
|
|
|
2002-06-05 06:01:43 +02:00
|
|
|
Copyright 1997, 1998, 2000, 2001, 2002 Red Hat, Inc.
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
Written by Sergey Okhapkin <sos@prospect.com.ru>
|
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
2000-08-02 18:28:18 +02:00
|
|
|
#include "winsup.h"
|
2000-02-17 20:38:33 +01:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
2000-09-08 04:56:55 +02:00
|
|
|
#include <signal.h>
|
2000-05-09 15:28:11 +02:00
|
|
|
#include <limits.h>
|
2000-07-27 19:30:51 +02:00
|
|
|
#include <wingdi.h>
|
|
|
|
#include <winuser.h>
|
2002-01-06 10:28:13 +01:00
|
|
|
#define USE_SYS_TYPES_FD_SET
|
|
|
|
#include <winsock2.h>
|
2001-12-06 23:36:08 +01:00
|
|
|
#include <unistd.h>
|
2000-08-22 05:58:47 +02:00
|
|
|
#include "cygerrno.h"
|
2000-09-08 04:56:55 +02:00
|
|
|
#include "perprocess.h"
|
|
|
|
#include "security.h"
|
2002-08-01 18:20:31 +02:00
|
|
|
#include "cygthread.h"
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
static NO_COPY UINT timer_active = 0;
|
|
|
|
static NO_COPY struct itimerval itv;
|
|
|
|
static NO_COPY DWORD start_time;
|
|
|
|
static NO_COPY HWND ourhwnd = NULL;
|
|
|
|
|
|
|
|
static LRESULT CALLBACK
|
|
|
|
WndProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
|
|
|
#ifndef NOSTRACE
|
2000-03-07 06:33:09 +01:00
|
|
|
strace.wm (uMsg, wParam, lParam);
|
2000-02-17 20:38:33 +01:00
|
|
|
#endif
|
|
|
|
switch (uMsg)
|
|
|
|
{
|
|
|
|
case WM_PAINT:
|
|
|
|
return 0;
|
|
|
|
case WM_DESTROY:
|
|
|
|
PostQuitMessage (0);
|
|
|
|
return 0;
|
|
|
|
case WM_TIMER:
|
|
|
|
if (wParam == timer_active)
|
|
|
|
{
|
|
|
|
UINT elapse = itv.it_interval.tv_sec * 1000 +
|
|
|
|
itv.it_interval.tv_usec / 1000;
|
|
|
|
KillTimer (hwnd, timer_active);
|
|
|
|
if (!elapse)
|
|
|
|
{
|
|
|
|
timer_active = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
timer_active = SetTimer (hwnd, 1, elapse, NULL);
|
|
|
|
start_time = GetTickCount ();
|
|
|
|
itv.it_value = itv.it_interval;
|
|
|
|
}
|
2001-03-18 22:11:25 +01:00
|
|
|
raise (SIGALRM);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
case WM_ASYNCIO:
|
2002-09-23 02:31:31 +02:00
|
|
|
if (WSAGETSELECTEVENT (lParam) == FD_OOB)
|
2002-05-28 03:55:40 +02:00
|
|
|
raise (SIGURG);
|
2002-01-06 10:28:13 +01:00
|
|
|
else
|
|
|
|
raise (SIGIO);
|
2000-02-17 20:38:33 +01:00
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return DefWindowProc (hwnd, uMsg, wParam, lParam);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static HANDLE window_started;
|
|
|
|
|
|
|
|
static DWORD WINAPI
|
2000-02-21 06:20:38 +01:00
|
|
|
Winmain (VOID *)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
MSG msg;
|
|
|
|
WNDCLASS wc;
|
2001-10-13 03:35:15 +02:00
|
|
|
static NO_COPY char classname[] = "CygwinWndClass";
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* Register the window class for the main window. */
|
|
|
|
|
|
|
|
wc.style = 0;
|
|
|
|
wc.lpfnWndProc = (WNDPROC) WndProc;
|
|
|
|
wc.cbClsExtra = 0;
|
|
|
|
wc.cbWndExtra = 0;
|
|
|
|
wc.hInstance = user_data->hmodule;
|
|
|
|
wc.hIcon = NULL;
|
|
|
|
wc.hCursor = NULL;
|
|
|
|
wc.hbrBackground = NULL;
|
|
|
|
wc.lpszMenuName = NULL;
|
|
|
|
wc.lpszClassName = classname;
|
|
|
|
|
|
|
|
if (!RegisterClass (&wc))
|
|
|
|
{
|
|
|
|
system_printf ("Cannot register window class");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create hidden window. */
|
|
|
|
ourhwnd = CreateWindow (classname, classname,
|
|
|
|
WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT,
|
|
|
|
CW_USEDEFAULT, CW_USEDEFAULT, (HWND) NULL,
|
|
|
|
(HMENU) NULL, user_data->hmodule, (LPVOID) NULL);
|
|
|
|
|
|
|
|
SetEvent (window_started);
|
|
|
|
|
|
|
|
if (!ourhwnd)
|
|
|
|
{
|
|
|
|
system_printf ("Cannot create window");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start the message loop. */
|
|
|
|
|
|
|
|
while (GetMessage (&msg, ourhwnd, 0, 0) == TRUE)
|
|
|
|
{
|
|
|
|
DispatchMessage (&msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg.wParam;
|
|
|
|
}
|
|
|
|
|
|
|
|
HWND __stdcall
|
|
|
|
gethwnd ()
|
|
|
|
{
|
|
|
|
if (ourhwnd != NULL)
|
|
|
|
return ourhwnd;
|
|
|
|
|
2002-08-01 18:20:31 +02:00
|
|
|
cygthread *h;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
window_started = CreateEvent (&sec_none_nih, TRUE, FALSE, NULL);
|
2002-08-01 18:20:31 +02:00
|
|
|
h = new cygthread (Winmain, NULL, "win");
|
|
|
|
SetThreadPriority (*h, THREAD_PRIORITY_HIGHEST);
|
2000-02-17 20:38:33 +01:00
|
|
|
WaitForSingleObject (window_started, INFINITE);
|
|
|
|
CloseHandle (window_started);
|
|
|
|
return ourhwnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void __stdcall
|
|
|
|
window_terminate ()
|
|
|
|
{
|
|
|
|
if (ourhwnd)
|
|
|
|
SendMessage (ourhwnd, WM_DESTROY, 0, 0);
|
|
|
|
}
|
|
|
|
|
2001-11-28 01:06:35 +01:00
|
|
|
extern "C" int
|
2000-02-17 20:38:33 +01:00
|
|
|
setitimer (int which, const struct itimerval *value, struct itimerval *oldvalue)
|
|
|
|
{
|
|
|
|
UINT elapse;
|
|
|
|
|
|
|
|
if (which != ITIMER_REAL)
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
2000-05-09 15:28:11 +02:00
|
|
|
/* Check if we will wrap */
|
|
|
|
if (itv.it_value.tv_sec >= (long) (UINT_MAX / 1000))
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
if (timer_active)
|
|
|
|
{
|
2001-03-18 22:11:25 +01:00
|
|
|
KillTimer (gethwnd (), timer_active);
|
2000-02-17 20:38:33 +01:00
|
|
|
timer_active = 0;
|
|
|
|
}
|
|
|
|
if (oldvalue)
|
|
|
|
*oldvalue = itv;
|
|
|
|
if (value == NULL)
|
|
|
|
{
|
|
|
|
set_errno (EFAULT);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
itv = *value;
|
|
|
|
elapse = itv.it_value.tv_sec * 1000 + itv.it_value.tv_usec / 1000;
|
|
|
|
if (elapse == 0)
|
2000-06-27 00:33:54 +02:00
|
|
|
if (itv.it_value.tv_usec)
|
|
|
|
elapse = 1;
|
|
|
|
else
|
|
|
|
return 0;
|
2001-03-18 22:11:25 +01:00
|
|
|
if (!(timer_active = SetTimer (gethwnd (), 1, elapse, NULL)))
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
start_time = GetTickCount ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-11-28 01:06:35 +01:00
|
|
|
extern "C" int
|
2000-02-17 20:38:33 +01:00
|
|
|
getitimer (int which, struct itimerval *value)
|
|
|
|
{
|
|
|
|
UINT elapse, val;
|
|
|
|
|
|
|
|
if (which != ITIMER_REAL)
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (value == NULL)
|
|
|
|
{
|
|
|
|
set_errno (EFAULT);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*value = itv;
|
|
|
|
if (!timer_active)
|
|
|
|
{
|
|
|
|
value->it_value.tv_sec = 0;
|
|
|
|
value->it_value.tv_usec = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
elapse = GetTickCount () - start_time;
|
|
|
|
val = itv.it_value.tv_sec * 1000 + itv.it_value.tv_usec / 1000;
|
|
|
|
val -= elapse;
|
2001-11-28 01:06:35 +01:00
|
|
|
value->it_value.tv_sec = val / 1000;
|
|
|
|
value->it_value.tv_usec = val % 1000;
|
2000-02-17 20:38:33 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-11-28 01:06:35 +01:00
|
|
|
extern "C" unsigned int
|
2000-02-17 20:38:33 +01:00
|
|
|
alarm (unsigned int seconds)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct itimerval newt, oldt;
|
|
|
|
|
|
|
|
newt.it_value.tv_sec = seconds;
|
|
|
|
newt.it_value.tv_usec = 0;
|
|
|
|
newt.it_interval.tv_sec = 0;
|
|
|
|
newt.it_interval.tv_usec = 0;
|
2001-11-28 01:06:35 +01:00
|
|
|
setitimer (ITIMER_REAL, &newt, &oldt);
|
2000-02-17 20:38:33 +01:00
|
|
|
ret = oldt.it_value.tv_sec;
|
|
|
|
if (ret == 0 && oldt.it_value.tv_usec)
|
|
|
|
ret = 1;
|
|
|
|
return ret;
|
|
|
|
}
|
2001-11-28 01:06:35 +01:00
|
|
|
|
|
|
|
extern "C" useconds_t
|
|
|
|
ualarm (useconds_t value, useconds_t interval)
|
|
|
|
{
|
|
|
|
struct itimerval timer, otimer;
|
|
|
|
|
|
|
|
timer.it_value.tv_sec = 0;
|
|
|
|
timer.it_value.tv_usec = value;
|
|
|
|
timer.it_interval.tv_sec = 0;
|
|
|
|
timer.it_interval.tv_usec = interval;
|
|
|
|
|
|
|
|
if (setitimer (ITIMER_REAL, &timer, &otimer) < 0)
|
|
|
|
return (u_int)-1;
|
|
|
|
|
|
|
|
return (otimer.it_value.tv_sec * 1000000) + otimer.it_value.tv_usec;
|
|
|
|
}
|
|
|
|
|