2000-02-18 03:38:33 +08:00
|
|
|
/* window.cc: hidden windows for signals/itimer support
|
|
|
|
|
2004-02-09 12:04:24 +08:00
|
|
|
Copyright 1997, 1998, 2000, 2001, 2002, 2003, 2004 Red Hat, Inc.
|
2000-02-18 03:38:33 +08: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-03 00:28:18 +08:00
|
|
|
#include "winsup.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <stdlib.h>
|
2000-09-08 10:56:55 +08:00
|
|
|
#include <signal.h>
|
2000-05-09 21:28:11 +08:00
|
|
|
#include <limits.h>
|
2000-07-28 01:30:51 +08:00
|
|
|
#include <wingdi.h>
|
|
|
|
#include <winuser.h>
|
2002-01-06 17:28:13 +08:00
|
|
|
#define USE_SYS_TYPES_FD_SET
|
|
|
|
#include <winsock2.h>
|
2001-12-07 06:36:08 +08:00
|
|
|
#include <unistd.h>
|
2000-08-22 11:58:47 +08:00
|
|
|
#include "cygerrno.h"
|
2000-09-08 10:56:55 +08:00
|
|
|
#include "perprocess.h"
|
|
|
|
#include "security.h"
|
2004-05-16 12:18:50 +08:00
|
|
|
#include "thread.h"
|
|
|
|
#include "cygtls.h"
|
|
|
|
#include "sync.h"
|
|
|
|
#include "wininfo.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2004-05-16 12:18:50 +08:00
|
|
|
wininfo NO_COPY winmsg;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2004-05-16 12:18:50 +08:00
|
|
|
muto NO_COPY *wininfo::lock;
|
|
|
|
|
|
|
|
wininfo::wininfo ()
|
|
|
|
{
|
|
|
|
new_muto_name (lock, "!winlock");
|
|
|
|
}
|
|
|
|
|
|
|
|
int __stdcall
|
|
|
|
wininfo::process (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
#ifndef NOSTRACE
|
2000-03-07 13:33:09 +08:00
|
|
|
strace.wm (uMsg, wParam, lParam);
|
2000-02-18 03:38:33 +08: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)
|
2004-05-16 12:18:50 +08:00
|
|
|
timer_active = 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
timer_active = SetTimer (hwnd, 1, elapse, NULL);
|
|
|
|
start_time = GetTickCount ();
|
|
|
|
itv.it_value = itv.it_interval;
|
|
|
|
}
|
2001-03-19 05:11:25 +08:00
|
|
|
raise (SIGALRM);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
case WM_ASYNCIO:
|
2002-09-23 08:31:31 +08:00
|
|
|
if (WSAGETSELECTEVENT (lParam) == FD_OOB)
|
2002-05-28 09:55:40 +08:00
|
|
|
raise (SIGURG);
|
2002-01-06 17:28:13 +08:00
|
|
|
else
|
|
|
|
raise (SIGIO);
|
2000-02-18 03:38:33 +08:00
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return DefWindowProc (hwnd, uMsg, wParam, lParam);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-16 12:18:50 +08:00
|
|
|
static LRESULT CALLBACK
|
|
|
|
process_window_events (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
|
|
|
return winmsg.process (hwnd, uMsg, wParam, lParam);
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2004-05-16 12:18:50 +08:00
|
|
|
/* Handle windows events. Inherits ownership of the wininfo lock */
|
|
|
|
DWORD WINAPI
|
|
|
|
wininfo::winthread ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
MSG msg;
|
|
|
|
WNDCLASS wc;
|
2001-10-13 09:35:15 +08:00
|
|
|
static NO_COPY char classname[] = "CygwinWndClass";
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2004-05-16 12:18:50 +08:00
|
|
|
lock->grab ();
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Register the window class for the main window. */
|
|
|
|
|
|
|
|
wc.style = 0;
|
2004-05-16 12:18:50 +08:00
|
|
|
wc.lpfnWndProc = (WNDPROC) process_window_events;
|
2000-02-18 03:38:33 +08:00
|
|
|
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))
|
2004-05-16 12:18:50 +08:00
|
|
|
api_fatal ("cannot register window class, %E");
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* Create hidden window. */
|
2004-05-16 12:18:50 +08:00
|
|
|
hwnd = CreateWindow (classname, classname, WS_POPUP, CW_USEDEFAULT,
|
|
|
|
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
|
|
|
|
(HWND) NULL, (HMENU) NULL, user_data->hmodule,
|
|
|
|
(LPVOID) NULL);
|
|
|
|
if (!hwnd)
|
|
|
|
api_fatal ("couldn't create window, %E");
|
|
|
|
lock->release ();
|
2004-05-16 00:10:41 +08:00
|
|
|
|
2004-05-16 12:18:50 +08:00
|
|
|
while (GetMessage (&msg, hwnd, 0, 0) == TRUE)
|
2002-10-10 13:31:43 +08:00
|
|
|
DispatchMessage (&msg);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2003-12-14 15:09:22 +08:00
|
|
|
return 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2004-05-16 12:18:50 +08:00
|
|
|
static DWORD WINAPI
|
|
|
|
winthread (VOID *arg)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2004-05-16 12:18:50 +08:00
|
|
|
return ((wininfo *) arg)->winthread ();
|
|
|
|
}
|
|
|
|
|
|
|
|
wininfo::operator
|
|
|
|
HWND ()
|
|
|
|
{
|
|
|
|
if (hwnd)
|
|
|
|
return hwnd;
|
|
|
|
|
|
|
|
lock->acquire ();
|
|
|
|
if (!hwnd)
|
|
|
|
{
|
|
|
|
lock->upforgrabs ();
|
|
|
|
cygthread *h = new cygthread (::winthread, this, "win");
|
|
|
|
h->SetThreadPriority (THREAD_PRIORITY_HIGHEST);
|
|
|
|
h->zap_h ();
|
|
|
|
lock->acquire ();
|
|
|
|
}
|
|
|
|
lock->release ();
|
|
|
|
return hwnd;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2001-11-28 08:06:35 +08:00
|
|
|
extern "C" int
|
2000-02-18 03:38:33 +08:00
|
|
|
setitimer (int which, const struct itimerval *value, struct itimerval *oldvalue)
|
|
|
|
{
|
|
|
|
if (which != ITIMER_REAL)
|
|
|
|
{
|
2002-12-31 06:38:13 +08:00
|
|
|
set_errno (ENOSYS);
|
2000-02-18 03:38:33 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2004-05-16 12:18:50 +08:00
|
|
|
return winmsg.setitimer (value, oldvalue);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: Very racy */
|
|
|
|
int __stdcall
|
|
|
|
wininfo::setitimer (const struct itimerval *value, struct itimerval *oldvalue)
|
|
|
|
{
|
2000-05-09 21:28:11 +08:00
|
|
|
/* Check if we will wrap */
|
|
|
|
if (itv.it_value.tv_sec >= (long) (UINT_MAX / 1000))
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
if (timer_active)
|
|
|
|
{
|
2004-05-16 12:18:50 +08:00
|
|
|
KillTimer (winmsg, timer_active);
|
2000-02-18 03:38:33 +08:00
|
|
|
timer_active = 0;
|
|
|
|
}
|
|
|
|
if (oldvalue)
|
|
|
|
*oldvalue = itv;
|
|
|
|
if (value == NULL)
|
|
|
|
{
|
|
|
|
set_errno (EFAULT);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
itv = *value;
|
2004-05-16 12:18:50 +08:00
|
|
|
UINT elapse = itv.it_value.tv_sec * 1000 + itv.it_value.tv_usec / 1000;
|
2000-02-18 03:38:33 +08:00
|
|
|
if (elapse == 0)
|
2000-06-27 06:33:54 +08:00
|
|
|
if (itv.it_value.tv_usec)
|
|
|
|
elapse = 1;
|
|
|
|
else
|
|
|
|
return 0;
|
2004-05-16 12:18:50 +08:00
|
|
|
if (!(timer_active = SetTimer (winmsg, 1, elapse, NULL)))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
start_time = GetTickCount ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-11-28 08:06:35 +08:00
|
|
|
extern "C" int
|
2000-02-18 03:38:33 +08:00
|
|
|
getitimer (int which, struct itimerval *value)
|
|
|
|
{
|
|
|
|
if (which != ITIMER_REAL)
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (value == NULL)
|
|
|
|
{
|
|
|
|
set_errno (EFAULT);
|
|
|
|
return -1;
|
|
|
|
}
|
2004-05-16 12:18:50 +08:00
|
|
|
return winmsg.getitimer (value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: racy */
|
|
|
|
int __stdcall
|
|
|
|
wininfo::getitimer (struct itimerval *value)
|
|
|
|
{
|
2000-02-18 03:38:33 +08:00
|
|
|
*value = itv;
|
|
|
|
if (!timer_active)
|
|
|
|
{
|
|
|
|
value->it_value.tv_sec = 0;
|
|
|
|
value->it_value.tv_usec = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
2004-05-16 12:18:50 +08:00
|
|
|
|
|
|
|
UINT elapse, val;
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
elapse = GetTickCount () - start_time;
|
|
|
|
val = itv.it_value.tv_sec * 1000 + itv.it_value.tv_usec / 1000;
|
|
|
|
val -= elapse;
|
2001-11-28 08:06:35 +08:00
|
|
|
value->it_value.tv_sec = val / 1000;
|
|
|
|
value->it_value.tv_usec = val % 1000;
|
2000-02-18 03:38:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-11-28 08:06:35 +08:00
|
|
|
extern "C" unsigned int
|
2000-02-18 03:38:33 +08: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 08:06:35 +08:00
|
|
|
setitimer (ITIMER_REAL, &newt, &oldt);
|
2000-02-18 03:38:33 +08:00
|
|
|
ret = oldt.it_value.tv_sec;
|
|
|
|
if (ret == 0 && oldt.it_value.tv_usec)
|
|
|
|
ret = 1;
|
|
|
|
return ret;
|
|
|
|
}
|
2001-11-28 08:06:35 +08: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;
|
|
|
|
}
|
|
|
|
|
2003-10-14 17:21:55 +08:00
|
|
|
bool
|
|
|
|
has_visible_window_station (void)
|
|
|
|
{
|
|
|
|
HWINSTA station_hdl;
|
|
|
|
USEROBJECTFLAGS uof;
|
|
|
|
DWORD len;
|
|
|
|
|
|
|
|
/* Check if the process is associated with a visible window station.
|
|
|
|
These are processes running on the local desktop as well as processes
|
|
|
|
running in terminal server sessions.
|
|
|
|
Processes running in a service session not explicitely associated
|
|
|
|
with the desktop (using the "Allow service to interact with desktop"
|
|
|
|
property) are running in an invisible window station. */
|
|
|
|
if ((station_hdl = GetProcessWindowStation ())
|
|
|
|
&& GetUserObjectInformationA (station_hdl, UOI_FLAGS, &uof,
|
|
|
|
sizeof uof, &len)
|
|
|
|
&& (uof.dwFlags & WSF_VISIBLE))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|