newlib-cygwin/winsup/cygwin/window.cc

270 lines
5.9 KiB
C++
Raw Normal View History

2000-02-18 03:38:33 +08:00
/* window.cc: hidden windows for signals/itimer support
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. */
#include "winsup.h"
2000-02-18 03:38:33 +08:00
#include <sys/time.h>
#include <stdlib.h>
#include <signal.h>
#include <limits.h>
#include <wingdi.h>
#include <winuser.h>
#define USE_SYS_TYPES_FD_SET
#include <winsock2.h>
#include <unistd.h>
#include "cygerrno.h"
#include "perprocess.h"
#include "security.h"
#include "cygthread.h"
2000-02-18 03:38:33 +08: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
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)
{
timer_active = 0;
}
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:
if (WSAGETSELECTEVENT (lParam) == FD_OOB)
* autoload.cc (LoadFuncEx): Define via new LoadFuncEx2 macro. (LoadFuncEx2): Adapted from LoadFuncEx. Provides control of return value for nonexistent function. (NtQueryObject): Declare. (IsDebuggerPresent): Declare via LoadFuncEx2 and always return true if not available. * debug.h (being_debugged): Just rely on IsDebuggerPresent return value. * dtable.cc (handle_to_fn): New function. (dtable::init_std_file_from_handle): Attempt to derive std handle's name via handle_to_fn. (dtable::build_fhandler_from_name): Fill in what we can in path_conv structure when given a handle and path doesn't exist. * fhandler.cc (fhandler_base::open): Don't set the file pointer here. Use pc->exists () to determine if file exists rather than calling GetFileAttributes again. * fhandler.h (fhandler_base::exec_state_isknown): New method. (fhandler_base::fstat_helper): Add extra arguments to declaration. (fhandler_base::fstat_by_handle): Declare new method. (fhandler_base::fstat_by_name): Declare new method. * fhandler_disk_file (num_entries): Make __stdcall. (fhandler_base::fstat_by_handle): Define new method. (fhandler_base::fstat_by_name): Define new method. (fhandler_base:fstat): Call fstat_by_{handle,name} as appropriate. (fhandler_disk_file::fstat_helper): Accept extra arguments for filling out stat structure. Move handle or name specific stuff to new methods above. (fhandler_disk_file::open): Use real_path->exists rather than calling GetFileAttributes again. * ntdll.h (FILE_NAME_INFORMATION): Define new structure. (OBJECT_INFORMATION_CLASS): Partially define new enum. (OBJECT_NAME_INFORMATION): Define new structure. (NtQueryInformationFile): New declaration. (NtQueryObject): New declaration. * path.cc (path_conv::fillin): Define new method. * path.h (path_conv::fillin): Declare new method. (path_conv::drive_thpe): Rename from 'get_drive_type'. (path_conv::volser): Declare new method. (path_conv::volname): Declare new method. (path_conv::root_dir): Declare new method. * syscalls.cc (fstat64): Send real path_conv to fstat as second argument.
2002-05-28 09:55:40 +08:00
raise (SIGURG);
else
raise (SIGIO);
2000-02-18 03:38:33 +08:00
return 0;
default:
return DefWindowProc (hwnd, uMsg, wParam, lParam);
}
}
static HANDLE window_started;
static DWORD WINAPI
Winmain (VOID *)
2000-02-18 03:38:33 +08:00
{
MSG msg;
WNDCLASS wc;
static NO_COPY char classname[] = "CygwinWndClass";
2000-02-18 03:38:33 +08: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, %E");
2000-02-18 03:38:33 +08:00
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);
2000-02-18 03:38:33 +08:00
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);
2000-02-18 03:38:33 +08:00
* cygthread.cc (cygthread::stub): Revert previous change and again subsume cygthread::stub2. Just return from function now since ExitThread is guaranteed by automatic _threadinfo wrapper. Define as per ThreadProc convention. (cygthread::stub2): Remove. (cygthread::simplestub): Perform similar operations to cygthread::stub. (cygthread::simplestub2): Remove. * cygthread.h (cygthread::stub): Declare as per ThreadProc convention. (cygthread::stub2): Remove declaration. (cygthread::simplestub): Declare as per ThreadProc convention. (cygthread::simplestub2): Remove declaration. * cygtls.h (_threadinfo::call): Define first argument as per ThreadProc convention. (_threadinfo::call2): Ditto. (_tlsbase): Define as char * pointer. (_tlstop): New definition. (_main_tls): Define here. * dcrt0.cc (alloc_stack): Revert previous change which called init_thread since information would be overwritten by fork later anyway. (dll_crt0_1): Use _tlsbase and _tlstop for setting stack bottom, top. * exceptions.cc: Move _main_tls declaration to cygtls.h. (_threadinfo::call): Define first argument as per ThreadProc convention. (_threadinfo::call2): Call ExitThread with thread function return value. (_threadinfo::init_thread): Don't initialize cygtls to zero if input arg is NULL. * fork.cc (fork_child): Reset _main_tls here. Reinitialize parts of _my_tls after copying data from parent. * init.cc (threadfunc_fe): New function. Front-end for all threads created in cygwin processes. (munge_threadfunc): New function. (dll_entry): Call munge_threadfunc to force the call of a thread wrapper. * thread.cc (pthread::thread_init_wrapper): Perform similar operations to cygthread::stub. (pthread::thread_init_wrapper2): Remove. * thread.h (pthread::thread_init_wrapper): Declare as per ThreadProc convention. (pthread::thread_init_wrapper2): Remove declaration. * window.cc (Winmain): Just return from function now since ExitThread is guaranteed by automatic _threadinfo wrapper.
2003-12-14 15:09:22 +08:00
return 0;
2000-02-18 03:38:33 +08:00
}
HWND __stdcall
gethwnd ()
{
if (ourhwnd != NULL)
return ourhwnd;
cygthread *h;
2000-02-18 03:38:33 +08:00
window_started = CreateEvent (&sec_none_nih, TRUE, FALSE, NULL);
h = new cygthread (Winmain, NULL, "win");
h->SetThreadPriority (THREAD_PRIORITY_HIGHEST);
2000-02-18 03:38:33 +08:00
WaitForSingleObject (window_started, INFINITE);
CloseHandle (window_started);
h->zap_h ();
2000-02-18 03:38:33 +08:00
return ourhwnd;
}
extern "C" int
2000-02-18 03:38:33 +08:00
setitimer (int which, const struct itimerval *value, struct itimerval *oldvalue)
{
UINT elapse;
if (which != ITIMER_REAL)
{
set_errno (ENOSYS);
2000-02-18 03:38:33 +08:00
return -1;
}
/* 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)
{
2001-03-19 05:11:25 +08:00
KillTimer (gethwnd (), 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;
elapse = itv.it_value.tv_sec * 1000 + itv.it_value.tv_usec / 1000;
if (elapse == 0)
if (itv.it_value.tv_usec)
elapse = 1;
else
return 0;
2001-03-19 05:11:25 +08:00
if (!(timer_active = SetTimer (gethwnd (), 1, elapse, NULL)))
2000-02-18 03:38:33 +08:00
{
__seterrno ();
return -1;
}
start_time = GetTickCount ();
return 0;
}
extern "C" int
2000-02-18 03:38:33 +08: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;
value->it_value.tv_sec = val / 1000;
value->it_value.tv_usec = val % 1000;
2000-02-18 03:38:33 +08:00
return 0;
}
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;
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;
}
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;
}
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;
}