-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUser32Window.lua
368 lines (278 loc) · 7.86 KB
/
User32Window.lua
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
--
-- User32Window.lua
--
local bit = require "bit"
local bor = bit.bor
local ffi = require "ffi"
local C = ffi.C
require "win_gdi32"
require "win_user32"
require "win_kernel32"
require "StopWatch"
local user32 = ffi.load("User32")
local kernel32 = ffi.load("Kernel32")
local gdi32 = ffi.load("gdi32")
local User32Window = {
WindowMap = {},
Defaults = {
ClassName = "User32Window",
Title = "Window",
Origin = {10,10},
Extent = {320, 240},
FrameRate = 30,
},
}
local User32Window_mt = {
__index = User32Window,
}
function User32Window.new(params)
params = params or User32Window.Defaults
params.ClassName = params.ClassName or User32Window.Defaults.ClassName
params.Title = params.Title or User32Window.Defaults.Title
params.Origin = params.Origin or User32Window.Defaults.Origin
params.Extent = params.Extent or User32Window.Defaults.Extent
params.FrameRate = params.FrameRate or User32Window.Defaults.FrameRate
local self = {
Registration = nil;
IsReady = false;
IsValid = false;
IsRunning = false;
MessageDelegate = params.MessageDelegate;
OnSetFocusDelegate = params.OnSetFocusDelegate;
OnTickDelegate = params.OnTickDelegate;
-- Interactor routines
KeyboardInteractor = params.KeyboardInteractor;
MouseInteractor = params.MouseInteractor;
GestureInteractor = params.GestureInteractor;
}
self.FrameRate = params.FrameRate
self.Interval =1/ self.FrameRate
setmetatable(self, User32Window_mt);
self:Register(params)
self:CreateWindow(params)
return self;
end
function User32Window:GetClientSize()
local csize = ffi.new( "RECT[1]" )
user32.GetClientRect(self.WindowHandle, csize);
csize = csize[0]
local width = csize.right-csize.left
local height = csize.bottom-csize.top
return width, height
end
function User32Window:SetFrameRate(rate)
self.FrameRate = rate
self.Interval = 1/self.FrameRate
end
function User32Window:Register(params)
self.AppInstance = kernel32.GetModuleHandleA(nil)
self.ClassName = params.ClassName
local classStyle = bit.bor(C.CS_HREDRAW, C.CS_VREDRAW, C.CS_OWNDC);
local aClass = ffi.new('WNDCLASSEXA', {
cbSize = ffi.sizeof("WNDCLASSEXA");
style = classStyle;
lpfnWndProc = WindowProc;
cbClsExtra = 0;
cbWndExtra = 0;
hInstance = self.AppInstance;
hIcon = nil;
hCursor = nil;
hbrBackground = nil;
lpszMenuName = nil;
lpszClassName = self.ClassName;
hIconSm = nil;
})
self.Registration = user32.RegisterClassExA(aClass)
assert(self.Registration ~= 0, "Registration error"..tostring(C.GetLastError()))
end
function User32Window:CreateWindow(params)
self.ClassName = params.ClassName
self.Title = params.Title
self.Width = params.Extent[1]
self.Height = params.Extent[2]
local dwExStyle = bit.bor(C.WS_EX_APPWINDOW, C.WS_EX_WINDOWEDGE)
local dwStyle = bit.bor(C.WS_SYSMENU, C.WS_VISIBLE, C.WS_POPUP)
--print("User32Window:CreateWindow - 1.0")
local hwnd = user32.CreateWindowExA(
0,
self.ClassName,
self.Title,
C.WS_OVERLAPPEDWINDOW,
C.CW_USEDEFAULT,
C.CW_USEDEFAULT,
params.Extent[1], params.Extent[2],
nil,
nil,
self.AppInstance,
nil)
--print("User32Window:CreateWindow - 2.0")
assert(hwnd,"unable to create window"..tostring(C.GetLastError()))
self:OnCreated(hwnd)
end
function User32Window:Show()
user32.ShowWindow(self.WindowHandle, C.SW_SHOW)
end
function User32Window:Hide()
end
function User32Window:Update()
user32.UpdateWindow(self.WindowHandle)
end
function User32Window:SwapBuffers()
gdi32.SwapBuffers(self.GDIContext.Handle);
end
User32Window.GetHandle = function(self)
return self.WindowHandle;
end
function User32Window:OnCreated(hwnd)
--print("User32Window:OnCreated")
local winptr = ffi.cast("intptr_t", hwnd)
local winnum = tonumber(winptr)
self.WindowHandle = hwnd
User32Window.WindowMap[winnum] = self
local hdc = C.GetDC(self.WindowHandle)
self.GDIContext = GDIContext(hdc)
self.GDIContext:UseDCPen()
self.GDIContext:UseDCBrush()
self.IsValid = true
end
function User32Window:OnDestroy()
-- print("User32Window:OnDestroy")
C.PostQuitMessage(0)
return 0
end
function User32Window:OnQuit()
--print("User32Window:OnQuit")
self.IsRunning = false
-- delete glcontext
if self.GLContext then
self.GLContext:Destroy()
end
end
function User32Window:OnTick(tickCount)
if (self.OnTickDelegate) then
self.OnTickDelegate(self, tickCount)
end
end
function User32Window:OnFocusMessage(msg)
--print("OnFocusMessage")
if (self.OnSetFocusDelegate) then
self.OnSetFocusDelegate(self, msg)
end
end
function User32Window:OnKeyboardMessage(msg)
if self.KeyboardInteractor then
self.KeyboardInteractor(msg)
end
end
function User32Window:OnMouseMessage(msg)
if self.MouseInteractor then
self.MouseInteractor(msg)
end
end
--[[
for window creation, we should see the
following sequence
WM_GETMINMAXINFO = 0x0024
WM_NCCREATE = 0x0081
WM_NCCALCSIZE = 0x0083
WM_CREATE = 0x0001
Then, after ShowWindow is called
WM_SHOWWINDOW = 0x0018,
WM_WINDOWPOSCHANGING = 0x0046,
WM_ACTIVATEAPP = 0x001C,
Closing Sequence
WM_CLOSE = 0x0010,
...
WM_ACTIVATEAPP = 0x001C,
WM_KILLFOCUS = 0x0008,
WM_IME_SETCONTEXT = 0x0281,
WM_IME_NOTIFY = 0x0282,
WM_DESTROY = 0x0002,
WM_NCDESTROY = 0x0082,
--]]
function WindowProc(hwnd, msg, wparam, lparam)
-- lookup which window object is associated with the
-- window handle
local winptr = ffi.cast("intptr_t", hwnd)
local winnum = tonumber(winptr)
local self = User32Window.WindowMap[winnum]
--print(string.format("WindowProc: 0x%x, Window: 0x%x, self: %s", msg, winnum, tostring(self)))
-- if we have a self, then the window is capable
-- of handling the message
if self then
if (self.MessageDelegate) then
result = self.MessageDelegate(hwnd, msg, wparam, lparam)
return result
end
if (msg == C.WM_DESTROY) then
return self:OnDestroy()
end
if (msg >= C.WM_MOUSEFIRST and msg <= C.WM_MOUSELAST) or
(msg >= C.WM_NCMOUSEMOVE and msg <= C.WM_NCMBUTTONDBLCLK) then
self:OnMouseMessage(msg, wparam, lparam)
end
if (msg >= C.WM_KEYDOWN and msg <= C.WM_SYSCOMMAND) then
self:OnKeyboardMessage(msg, wparam, lparam)
end
end
-- otherwise, it's not associated with a window that we know
-- so do default processing
return user32.DefWindowProcA(hwnd, msg, wparam, lparam);
end
function User32Window:Run()
if not self.IsValid then
print('Window Handle is NULL')
return
end
self.IsRunning = true
self:Show()
self:Update()
return Loop(self)
end
-- The following 'jit.off(Loop)' is here because LuaJit
-- can't quite fix-up the case where a callback is being
-- called from LuaJit'd code
-- http://lua-users.org/lists/lua-l/2011-12/msg00712.html
--
-- I found the proper way to do this is to put the jit.off
-- call before the function body.
--
jit.off(Loop)
function Loop(win)
local timerEvent = C.CreateEventA(nil, false, false, nil)
-- If the timer event was not created
-- just return
if timerEvent == nil then
error("unable to create timer")
return
end
local handleCount = 1
local handles = ffi.new('void*[1]', {timerEvent})
local msg = ffi.new("MSG")
local sw = StopWatch.new()
local tickCount = 1
local timeleft = 0
local lastTime = sw:Milliseconds()
local nextTime = lastTime + win.Interval * 1000
local dwFlags = bor(C.MWMO_ALERTABLE,C.MWMO_INPUTAVAILABLE)
while (win.IsRunning) do
while (user32.PeekMessageA(msg, nil, 0, 0, C.PM_REMOVE) ~= 0) do
user32.TranslateMessage(msg)
user32.DispatchMessageA(msg)
if msg.message == C.WM_QUIT then
return win:OnQuit()
end
end
timeleft = nextTime - sw:Milliseconds();
if (timeleft <= 0) then
win:OnTick(tickCount);
tickCount = tickCount + 1
nextTime = nextTime + win.Interval * 1000
timeleft = nextTime - sw:Milliseconds();
end
if timeleft < 0 then timeleft = 0 end
-- use an alertable wait
C.MsgWaitForMultipleObjectsEx(handleCount, handles, timeleft, C.QS_ALLEVENTS, dwFlags)
end
end
return User32Window