-
Notifications
You must be signed in to change notification settings - Fork 574
/
Copy pathWeaselServerImpl.cpp
397 lines (333 loc) · 9.29 KB
/
WeaselServerImpl.cpp
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#include "stdafx.h"
#include "WeaselServerImpl.h"
#include <Windows.h>
#include <boost/thread.hpp>
#include <VersionHelpers.hpp>
namespace weasel {
class PipeServer : public PipeChannel<DWORD, PipeMessage>
{
public:
using ServerRunner = std::function<void()>;
using Respond = std::function<void(Msg)>;
using ServerHandler = std::function<void(PipeMessage, Respond)>;
PipeServer(std::wstring &pn_cmd, SECURITY_ATTRIBUTES *s);
public:
void Listen(ServerHandler const &handler);
/* Get a server runner */
ServerRunner GetServerRunner(ServerHandler const &handler);
private:
void _ProcessPipeThread(HANDLE pipe, ServerHandler const &handler);
};
}
using namespace weasel;
extern CAppModule _Module;
ServerImpl::ServerImpl()
: m_pRequestHandler(NULL),
channel(std::make_unique<PipeServer>(GetPipeName(), sa.get_attr()))
{
m_hUser32Module = GetModuleHandle(_T("user32.dll"));
}
ServerImpl::~ServerImpl()
{
_Finailize();
}
void ServerImpl::_Finailize()
{
if (pipeThread != nullptr) {
pipeThread->interrupt();
}
if (IsWindow())
{
DestroyWindow();
}
m_pRequestHandler->Finalize();
}
LRESULT ServerImpl::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
// not neccessary...
::SetWindowText(m_hWnd, WEASEL_IPC_WINDOW);
return 0;
}
LRESULT ServerImpl::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
Stop();
return 0;
}
LRESULT ServerImpl::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
bHandled = FALSE;
return 1;
}
LRESULT ServerImpl::OnQueryEndSystemSession(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
return TRUE;
}
LRESULT ServerImpl::OnEndSystemSession(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
if (m_pRequestHandler)
{
m_pRequestHandler->Finalize();
}
return 0;
}
LRESULT ServerImpl::OnCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
UINT uID = LOWORD(wParam);
std::map<UINT, CommandHandler>::iterator it = m_MenuHandlers.find(uID);
if (it == m_MenuHandlers.end())
{
bHandled = FALSE;
return 0;
}
it->second(); // execute command
return 0;
}
int ServerImpl::Start()
{
// assure single instance
if (FindWindow(WEASEL_IPC_WINDOW, NULL) != NULL)
{
return 0;
}
HWND hwnd = Create(NULL);
return (int)hwnd;
}
int ServerImpl::Stop()
{
_Finailize();
// quit the server
::ExitProcess(0);
return 0;
}
int ServerImpl::Run()
{
// This workaround causes a VC internal error:
// void PipeServer::Listen(ServerHandler handler);
//
// auto handler = boost::bind(&ServerImpl::HandlePipeMessage, this);
// auto listener = boost::bind(&PipeServer::Listen, channel.get(), handler);
//
auto listener = [this](PipeMessage msg, PipeServer::Respond resp) -> void {
HandlePipeMessage(msg, resp);
};
pipeThread = std::make_unique<boost::thread>([this, &listener]() {
channel->Listen(listener);
});
CMessageLoop theLoop;
_Module.AddMessageLoop(&theLoop);
int nRet = theLoop.Run();
_Module.RemoveMessageLoop();
return nRet;
}
DWORD ServerImpl::OnEcho(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam)
{
if (!m_pRequestHandler)
return 0;
return m_pRequestHandler->FindSession(lParam);
}
DWORD ServerImpl::OnStartSession(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam)
{
if (!m_pRequestHandler)
return 0;
return m_pRequestHandler->AddSession(reinterpret_cast<LPWSTR>(channel->ReceiveBuffer()));
}
DWORD ServerImpl::OnEndSession(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam)
{
if (!m_pRequestHandler)
return 0;
return m_pRequestHandler->RemoveSession(lParam);
}
DWORD ServerImpl::OnKeyEvent(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam)
{
if (!m_pRequestHandler/* || !m_pSharedMemory*/)
return 0;
auto eat = [this](std::wstring &msg) -> bool {
*channel << msg;
return true;
};
return m_pRequestHandler->ProcessKeyEvent(KeyEvent(wParam), lParam, eat);
}
DWORD ServerImpl::OnShutdownServer(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam)
{
Stop();
return 0;
}
DWORD ServerImpl::OnFocusIn(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam)
{
if (!m_pRequestHandler)
return 0;
m_pRequestHandler->FocusIn(wParam, lParam);
return 0;
}
DWORD ServerImpl::OnFocusOut(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam)
{
if (!m_pRequestHandler)
return 0;
m_pRequestHandler->FocusOut(wParam, lParam);
return 0;
}
DWORD ServerImpl::OnUpdateInputPosition(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam)
{
if (!m_pRequestHandler)
return 0;
/*
* 移位标志 = 1bit == 0
* height: 0~127 = 7bit
* top:-2048~2047 = 12bit(有符号)
* left:-2048~2047 = 12bit(有符号)
*
* 高解析度下:
* 移位标志 = 1bit == 1
* height: 0~254 = 7bit(舍弃低1位)
* top: -4096~4094 = 12bit(有符号,舍弃低1位)
* left: -4096~4094 = 12bit(有符号,舍弃低1位)
*/
RECT rc;
int hi_res = (wParam >> 31) & 0x01;
rc.left = ((wParam & 0x7ff) - (wParam & 0x800)) << hi_res;
rc.top = (((wParam >> 12) & 0x7ff) - ((wParam >> 12) & 0x800)) << hi_res;
const int width = 6;
int height = ((wParam >> 24) & 0x7f) << hi_res;
rc.right = rc.left + width;
rc.bottom = rc.top + height;
if (IsWindows8Point1OrGreater())
{
using PPTLPFPMDPI = BOOL (WINAPI *)(HWND, LPPOINT);
PPTLPFPMDPI PhysicalToLogicalPointForPerMonitorDPI = (PPTLPFPMDPI)::GetProcAddress(m_hUser32Module, "PhysicalToLogicalPointForPerMonitorDPI");
POINT lt = { rc.left, rc.top };
POINT rb = { rc.right, rc.bottom };
PhysicalToLogicalPointForPerMonitorDPI(NULL, <);
PhysicalToLogicalPointForPerMonitorDPI(NULL, &rb);
rc = { lt.x, lt.y, rb.x, rb.y };
}
m_pRequestHandler->UpdateInputPosition(rc, lParam);
return 0;
}
DWORD ServerImpl::OnStartMaintenance(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam)
{
if (m_pRequestHandler)
m_pRequestHandler->StartMaintenance();
return 0;
}
DWORD ServerImpl::OnEndMaintenance(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam)
{
if (m_pRequestHandler)
m_pRequestHandler->EndMaintenance();
return 0;
}
DWORD ServerImpl::OnCommitComposition(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam)
{
if (m_pRequestHandler)
m_pRequestHandler->CommitComposition(lParam);
return 0;
}
DWORD ServerImpl::OnClearComposition(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam)
{
if (m_pRequestHandler)
m_pRequestHandler->ClearComposition(lParam);
return 0;
}
#define MAP_PIPE_MSG_HANDLE(__msg, __wParam, __lParam) {\
auto lParam = __lParam;\
auto wParam = __wParam;\
LRESULT _result = 0;\
switch (__msg) {\
#define PIPE_MSG_HANDLE(__msg, __func) \
case __msg:\
_result = __func(__msg, wParam, lParam);\
break;\
#define END_MAP_PIPE_MSG_HANDLE(__result) }__result = _result; }
template<typename _Resp>
void ServerImpl::HandlePipeMessage(PipeMessage pipe_msg, _Resp resp)
{
DWORD result;
MAP_PIPE_MSG_HANDLE(pipe_msg.Msg, pipe_msg.wParam, pipe_msg.lParam)
PIPE_MSG_HANDLE(WEASEL_IPC_ECHO, OnEcho)
PIPE_MSG_HANDLE(WEASEL_IPC_START_SESSION, OnStartSession)
PIPE_MSG_HANDLE(WEASEL_IPC_END_SESSION, OnEndSession)
PIPE_MSG_HANDLE(WEASEL_IPC_PROCESS_KEY_EVENT, OnKeyEvent)
PIPE_MSG_HANDLE(WEASEL_IPC_SHUTDOWN_SERVER, OnShutdownServer)
PIPE_MSG_HANDLE(WEASEL_IPC_FOCUS_IN, OnFocusIn)
PIPE_MSG_HANDLE(WEASEL_IPC_FOCUS_OUT, OnFocusOut)
PIPE_MSG_HANDLE(WEASEL_IPC_UPDATE_INPUT_POS, OnUpdateInputPosition)
PIPE_MSG_HANDLE(WEASEL_IPC_START_MAINTENANCE, OnStartMaintenance)
PIPE_MSG_HANDLE(WEASEL_IPC_END_MAINTENANCE, OnEndMaintenance)
PIPE_MSG_HANDLE(WEASEL_IPC_COMMIT_COMPOSITION, OnCommitComposition)
PIPE_MSG_HANDLE(WEASEL_IPC_CLEAR_COMPOSITION, OnClearComposition);
END_MAP_PIPE_MSG_HANDLE(result);
resp(result);
}
PipeServer::PipeServer(std::wstring &pn_cmd, SECURITY_ATTRIBUTES *s)
: PipeChannel(pn_cmd, s)
{}
void PipeServer::Listen(ServerHandler const &handler)
{
for (;;) {
HANDLE pipe = INVALID_HANDLE_VALUE;
try {
boost::this_thread::interruption_point();
pipe = _ConnectServerPipe(pname);
boost::thread th([&handler, pipe, this] {
_ProcessPipeThread(pipe, handler);
});
}
catch (DWORD ex) {
_FinalizePipe(pipe);
}
boost::this_thread::interruption_point();
}
}
PipeServer::ServerRunner PipeServer::GetServerRunner(ServerHandler const &handler)
{
return [&handler, this]() {
Listen(handler);
};
}
void PipeServer::_ProcessPipeThread(HANDLE pipe, ServerHandler const &handler)
{
try {
for (;;) {
Res msg;
_Receive(pipe, &msg, sizeof(msg));
handler(msg, [this, pipe](Msg resp) {
_Send(pipe, resp);
});
}
}
catch (...) {
_FinalizePipe(pipe);
}
}
// weasel::Server
Server::Server()
: m_pImpl(new ServerImpl)
{}
Server::~Server()
{
if (m_pImpl)
delete m_pImpl;
}
int Server::Start()
{
return m_pImpl->Start();
}
int Server::Stop()
{
return m_pImpl->Stop();
}
int Server::Run()
{
return m_pImpl->Run();
}
void Server::SetRequestHandler(RequestHandler* pHandler)
{
m_pImpl->SetRequestHandler(pHandler);
}
void Server::AddMenuHandler(UINT uID, CommandHandler handler)
{
m_pImpl->AddMenuHandler(uID, handler);
}
HWND Server::GetHWnd()
{
return m_pImpl->m_hWnd;
}