~ubuntu-branches/ubuntu/maverick/psi/maverick

« back to all changes in this revision

Viewing changes to cutestuff/idle/win32/idleui.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2006-01-20 00:20:36 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060120002036-7nw6yo6totip0ee5
Tags: 0.10-2
* Added upstream changelog (Closes: Bug#327748)
* Mention --no-gpg and --no-gpg-agent in manpage (Closes: Bug#204416)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
////////////////////////////////////////////////////////////////
2
 
// 2000 Microsoft Systems Journal.
3
 
// If this program works, it was written by Paul DiLascia.
4
 
// If not, I don't know who wrote it.
5
 
// This program compiles with Visual C++ 6.0 on Windows 98
6
 
//
7
 
// See IdleUI.h
8
 
//
9
 
 
10
 
#include<windows.h>
11
 
#include<winuser.h>
12
 
#include<assert.h>
13
 
 
14
 
#define DLLEXPORT __declspec(dllexport)
15
 
 
16
 
#ifdef _DEBUG
17
 
#define new DEBUG_NEW
18
 
#undef THIS_FILE
19
 
static char THIS_FILE[] = __FILE__;
20
 
#endif
21
 
 
22
 
////////////////
23
 
// The following global data is SHARED among all instances of the DLL
24
 
// (processes); i.e., these are system-wide globals.
25
 
//
26
 
#pragma data_seg (".IdleUI")  // you must define as SHARED in .def
27
 
 
28
 
HHOOK g_keyboardHook = NULL;      // one instance for all processes
29
 
HHOOK g_mouseHook = NULL;    // one instance for all processes
30
 
DWORD g_lastInputTick = 0;  // tick time of last input event
31
 
 
32
 
/**
33
 
  Last mouse position.
34
 
*/
35
 
POINT g_lastMousePos;
36
 
 
37
 
#pragma data_seg ()
38
 
 
39
 
/**
40
 
  Flag indicating whether the DLL's owning process is the loading DLL.
41
 
*/
42
 
bool g_isHandleOwner = false;
43
 
 
44
 
//
45
 
// Public interface
46
 
//
47
 
 
48
 
//////////////////
49
 
// Initialize DLL: install kbd/mouse hooks.
50
 
//
51
 
DLLEXPORT BOOL IdleUIInit()
52
 
{
53
 
  return TRUE;
54
 
}
55
 
 
56
 
//////////////////
57
 
// Terminate DLL: remove hooks.
58
 
//
59
 
DLLEXPORT void IdleUITerm()
60
 
{
61
 
}
62
 
 
63
 
/////////////////
64
 
// Get tick count of last keyboard or mouse event
65
 
//
66
 
DLLEXPORT DWORD IdleUIGetLastInputTime()
67
 
{
68
 
  return g_lastInputTick;
69
 
}
70
 
 
71
 
//
72
 
// Internals
73
 
//
74
 
 
75
 
/////////////////
76
 
// Keyboard hook: record tick count
77
 
//
78
 
LRESULT CALLBACK keyboardHookCallback(int code, WPARAM wParam, LPARAM lParam)
79
 
{
80
 
        if (code == HC_ACTION)
81
 
  {
82
 
    g_lastInputTick = GetTickCount();
83
 
        }
84
 
  return ::CallNextHookEx(g_keyboardHook, code, wParam, lParam);
85
 
}
86
 
 
87
 
/////////////////
88
 
// Mouse hook: record tick count
89
 
//
90
 
LRESULT CALLBACK mouseHookCallback(int code, WPARAM wParam, LPARAM lParam)
91
 
{
92
 
        if (code == HC_ACTION)
93
 
  {
94
 
    // Update timestamp if event indicates mouse action
95
 
    bool change = false;
96
 
    if (wParam == WM_MOUSEMOVE && lParam != 0)
97
 
    {
98
 
      PMOUSEHOOKSTRUCT mhs = (PMOUSEHOOKSTRUCT) lParam;
99
 
      if (mhs->pt.x != g_lastMousePos.x ||
100
 
        mhs->pt.y != g_lastMousePos.y)
101
 
      {
102
 
        change = true;
103
 
        g_lastMousePos = mhs->pt;
104
 
      }
105
 
    }
106
 
    else
107
 
    {
108
 
      change = true;
109
 
    }
110
 
    if (change)
111
 
    {
112
 
      g_lastInputTick = GetTickCount();
113
 
    }
114
 
        }
115
 
  return ::CallNextHookEx(g_mouseHook, code, wParam, lParam);
116
 
}
117
 
 
118
 
void initialize(HINSTANCE module)
119
 
{
120
 
  if (g_keyboardHook == 0)
121
 
  {
122
 
    g_keyboardHook = SetWindowsHookEx(WH_KEYBOARD, keyboardHookCallback, module, 0);
123
 
    g_mouseHook = SetWindowsHookEx(WH_MOUSE, mouseHookCallback, module, 0);
124
 
    g_lastInputTick = GetTickCount();
125
 
    g_isHandleOwner = true;
126
 
  }
127
 
  assert(g_keyboardHook);
128
 
  assert(g_mouseHook);
129
 
}
130
 
 
131
 
void shutdown()
132
 
{
133
 
  // Only handle-owning process may unhook
134
 
  if (g_isHandleOwner)
135
 
  {
136
 
    if (g_keyboardHook != 0)
137
 
    {
138
 
      UnhookWindowsHookEx(g_keyboardHook);
139
 
      g_keyboardHook = 0;
140
 
    }
141
 
    if (g_mouseHook != 0)
142
 
    {
143
 
      UnhookWindowsHookEx(g_mouseHook);
144
 
      g_mouseHook = 0;
145
 
    }
146
 
  }
147
 
}
148
 
 
149
 
//
150
 
// DLL entry point
151
 
//
152
 
 
153
 
BOOL WINAPI DllMain(HINSTANCE module, DWORD reason, LPVOID reserved)
154
 
{
155
 
  switch (reason)
156
 
  {
157
 
    case DLL_PROCESS_ATTACH:
158
 
    {
159
 
      initialize(module);
160
 
      break;
161
 
    }
162
 
    case DLL_PROCESS_DETACH:
163
 
    {
164
 
      shutdown();
165
 
      break;
166
 
    }
167
 
    case DLL_THREAD_ATTACH:
168
 
    case DLL_THREAD_DETACH:
169
 
    {
170
 
      // Ignore
171
 
      break;
172
 
    }
173
 
  }
174
 
  return TRUE;
175
 
}