-
Notifications
You must be signed in to change notification settings - Fork 0
/
vwHook.c
152 lines (136 loc) · 4.4 KB
/
vwHook.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// VirtuaWin - Virtual Desktop Manager (virtuawin.sourceforge.net)
// vwHook.c - Windows Hook for sending window activation events.
//
// Copyright (c) 2007-2021 VirtuaWin (VirtuaWin@home.se)
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
//
#define vwHOOK_BUILD
#define vwHOOK_TEST 0
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "Messages.h"
#include "vwHook.h"
#ifdef _MSC_VER
#pragma data_seg(".VWSHR")
#define SHARED
#else
#define SHARED __attribute__((section(".VWSHR"), shared))
#endif
static int vwHookUse SHARED = 0 ;
static HWND vwHWnd SHARED = NULL ;
#ifdef _MSC_VER
#pragma data_seg()
#pragma comment(linker, "/SECTION:.VWSHR,RWS")
#endif
#if vwHOOK_TEST
FILE *logfp ;
#endif
HINSTANCE hookHInst ;
HHOOK hookCallWndProc ;
clock_t vwShowHideTime ;
/* allow up to 0.25secs for VW to change the visibility state of a window */
#define vwCHANGE_STATE_TIME (CLOCKS_PER_SEC >> 2)
BOOL WINAPI
DllMain(HINSTANCE hInst, DWORD fdwReason, LPVOID lpvReserved)
{
if(fdwReason == DLL_PROCESS_ATTACH)
{
hookHInst = hInst ;
#if vwHOOK_TEST
if(logfp == NULL)
logfp = fopen("c:\\vwHook.log","w+") ;
fprintf(logfp,"DllMain: %p\n",hookHInst) ;
fflush(logfp) ;
#endif
}
return TRUE ;
}
LRESULT CALLBACK
vwHookCallWndProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if((nCode == HC_ACTION) && vwHookUse)
{
CWPSTRUCT *cwp = (CWPSTRUCT *) lParam ;
HWND vwh = vwHWnd ;
clock_t cc ;
#if vwHOOK_TEST
{
SYSTEMTIME stime;
GetLocalTime (&stime);
fprintf(logfp,"[%04d-%02d-%02d %02d:%02d:%02d:%03d] vwHookCallWndProc %x: %d %d %d (%x %x %x) - %x\n",
stime.wYear, stime.wMonth, stime.wDay, stime.wHour, stime.wMinute, stime.wSecond, stime.wMilliseconds,
cwp->hwnd,cwp->message,cwp->wParam,cwp->lParam,cwp->message,cwp->wParam,cwp->lParam,
GetWindowLong(cwp->hwnd, GWL_STYLE)) ;
fflush(logfp) ;
}
#endif
if(cwp->hwnd == vwh)
/* dont report any VW window events back to VW */
;
else if((cwp->message == WM_ACTIVATE) && (LOWORD(cwp->wParam) != WA_INACTIVE) &&
(((cc=clock()) - vwShowHideTime) > vwCHANGE_STATE_TIME))
{
#if vwHOOK_TEST
fprintf(logfp," Activate: %d %d\n",vwShowHideTime,cc) ;
fflush(logfp) ;
#endif
/* set time to this time to avoid sending more than one message at a time */
vwShowHideTime = cc ;
PostMessage(vwh,VW_ACCESSWIN,(WPARAM) cwp->hwnd,-1) ;
}
else if((cwp->message == WM_NULL) && (cwp->wParam == 0x51842145) && (cwp->lParam == 0x5e7bdeba))
vwShowHideTime = clock() ;
}
return CallNextHookEx(hookCallWndProc,nCode,wParam,lParam) ;
}
vwHOOK_EXPORT void
vwHookSetup(HWND ivwHWnd, int ivwHookUse)
{
vwHWnd = ivwHWnd ;
vwHookUse = ivwHookUse ;
}
vwHOOK_EXPORT int
vwHookInstall(void)
{
#if vwHOOK_TEST
/* only hook the vwTestApp */
DWORD vwHookThreadId ;
HWND hWnd ;
if(logfp == NULL)
logfp = fopen("c:\\vwHook.log","w+") ;
fprintf(logfp,"vwHookInstall: %p\n",hookHInst) ;
fflush(logfp) ;
if(((hWnd = FindWindow("#32770","vwTestApp")) == NULL) ||
((vwHookThreadId = GetWindowThreadProcessId(hWnd,NULL)) == 0))
return 1 ;
fprintf(logfp,"vwHookInstall: %p: %x %d\n",hookHInst,hWnd,vwHookThreadId) ;
fflush(logfp) ;
#else
#define vwHookThreadId 0
#endif
if((hookCallWndProc = SetWindowsHookEx(WH_CALLWNDPROC,vwHookCallWndProc,hookHInst,vwHookThreadId)) == NULL)
return 2 ;
return 0 ;
}
vwHOOK_EXPORT void
vwHookUninstall(void)
{
UnhookWindowsHookEx(hookCallWndProc) ;
}