-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlovr-window.lua
More file actions
401 lines (317 loc) · 11.3 KB
/
lovr-window.lua
File metadata and controls
401 lines (317 loc) · 11.3 KB
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
398
399
400
local ffi = require 'ffi'
local C = ffi.os == 'Windows' and ffi.load('glfw3') or ffi.C
local C_str = ffi.string
ffi.cdef [[
enum {
GLFW_RESIZABLE = 0x00020003,
GLFW_VISIBLE = 0x00020004,
GLFW_DECORATED = 0x00020005,
GLFW_FLOATING = 0x00020007
};
typedef struct {
int width;
int height;
unsigned char* pixels;
} GLFWimage;
typedef struct GLFWvidmode {
int width;
int height;
int refreshRate;
} GLFWvidmode;
typedef struct GLFWwindow GLFWwindow;
GLFWwindow* glfwGetCurrentContext(void);
typedef struct GLFWmonitor GLFWmonitor;
GLFWmonitor** glfwGetMonitors(int *count);
GLFWmonitor* glfwGetWindowMonitor(GLFWwindow* window);
GLFWmonitor* glfwGetPrimaryMonitor(void);
void glfwGetMonitorPos(GLFWmonitor* monitor, int *xpos, int *ypos);
const char* glfwGetMonitorName(GLFWmonitor* monitor);
const GLFWvidmode* glfwGetVideoMode(GLFWmonitor* monitor);
void glfwSetWindowMonitor(GLFWwindow* window, GLFWmonitor* monitor, int xpos, int ypos, int width, int height, int refreshRate);
void glfwGetMonitorWorkarea(GLFWmonitor *monitor, int *xpos, int *ypos, int *width, int *height);
// icon
void glfwSetWindowIcon(GLFWwindow* window, int count, const GLFWimage *images);
// attributes
void glfwSetWindowAttrib(GLFWwindow* window, int attrib, int value); //+
int glfwGetWindowAttrib(GLFWwindow* window, int attrib); //+
// size & limits
void glfwSetWindowSize(GLFWwindow* window, int width, int height); //-
void glfwGetWindowSize(GLFWwindow* window, int *width, int *height); //-
void glfwSetWindowSizeLimits(GLFWwindow* window, int minwidth, int minheight, int maxwidth, int maxheight); //-
// position
void glfwSetWindowPos(GLFWwindow* window, int xpos, int ypos);
void glfwGetWindowPos(GLFWwindow* window, int *xpos, int *ypos);
// minimize maximize restore
void glfwMaximizeWindow(GLFWwindow* window);
void glfwIconifyWindow(GLFWwindow *window);
void glfwRestoreWindow(GLFWwindow *window);
// title
void glfwSetWindowTitle(GLFWwindow* window, const char* title);
// visible
void glfwShowWindow(GLFWwindow* window);
void glfwHideWindow(GLFWwindow* window);
// focus
void glfwFocusWindow(GLFWwindow* window);
// attention
void glfwRequestWindowAttention(GLFWwindow* window);
// opacity
void glfwSetWindowOpacity(GLFWwindow* window, float opacity);
float glfwGetWindowOpacity(GLFWwindow* window);
// callbacks
typedef void(*GLFWwindowsizefun) (GLFWwindow*, int, int); // size changed
GLFWwindowsizefun glfwSetWindowSizeCallback(GLFWwindow* window, GLFWwindowsizefun callback );
typedef void(*GLFWwindowmaximizefun) (GLFWwindow*, int); // maximize
GLFWwindowmaximizefun glfwSetWindowMaximizeCallback(GLFWwindow* window, GLFWwindowmaximizefun callback);
typedef void(*GLFWwindowposfun) (GLFWwindow*, int, int); // position changed
GLFWwindowposfun glfwSetWindowPosCallback(GLFWwindow* window, GLFWwindowposfun callback);
typedef void(* GLFWdropfun) (GLFWwindow*, int, const char *[]);
GLFWdropfun glfwSetDropCallback(GLFWwindow* window, GLFWdropfun callback);
]]
local W = C.glfwGetCurrentContext()
local window = {}
local __monitors
---------------------------------------------------------------------------------------------------------------
local __params = { -- default parameters list
title = 'LÖVR',
icon = nil,
fullscreen = false,
fullscreentype = "desktop",
width = 1080,
height = 600,
minwidth = 320,
minheight = 240,
x = nil,
y = nil,
display = 1,
centered = false,
topmost = false,
borderless = false,
resizable = false,
opacity = 1,
vsync = 1,
msaa = 0
}
if conf then
for k,v in pairs(conf) do
__params[k] = v
end
if type(__params.icon) == 'string' then
__params.icon = lovr.data.newImage(__params.icon, false)
end
conf = nil
end
---------------------------------------------------------------------------------------------------------------
function window.getDisplayCount()
local count = ffi.new('int[1]')
__monitors = C.glfwGetMonitors(count)
return count[0]
end
local function check_monitor( index, throwerr )
if type(index) ~= 'number' then
if throwerr then
error('Bad argument #1: number expected got ' .. type(index), 3)
else
return false
end
end
local dcnt = window.getDisplayCount()
if index < 1 or index > dcnt then
if throwerr then
error('Invalid display index: ' .. tostring(index), 3)
else
return false
end
end
return true
end
function window.getDisplayName( index )
check_monitor( index, true )
return C_str(C.glfwGetMonitorName( __monitors[index-1] ))
end
function window.getDisplayDimensions( index )
check_monitor( index, true )
local screenmode = C.glfwGetVideoMode( __monitors[index-1] )
return screenmode.width, screenmode.height
end
---------------------------------------------------------------------------------------------------------------
function window.setIcon( source )
if not source then
C.glfwSetWindowIcon(W, 0, nil)
__params.icon = nil
return
end
if type(source) == 'string' then
source = lovr.data.newImage(source, false)
elseif tostring(source) ~= 'Image' then
error('Bad argument #1 to setIcon (Image expected)', 2)
end
__params.icon = source
local icon = ffi.new('GLFWimage', source:getWidth(), source:getHeight(), source:getBlob():getPointer())
C.glfwSetWindowIcon(W, 1, icon)
end
function window.getIcon()
return tostring(__params.icon) == 'Image' and __params.icon or nil
end
---------------------------------------------------------------------------------------------------------------
function window.setOpacity( value )
value = math.max(0, math.min(value, 1))
C.glfwSetWindowOpacity(W, value)
end
function window.getOpacity()
return C.glfwGetWindowOpacity(W)
end
---------------------------------------------------------------------------------------------------------------
function window.setPosition( x,y )
C.glfwSetWindowPos(W, x or 0, y or 0)
end
function window.getPosition()
local x, y = ffi.new('int[1]'), ffi.new('int[1]')
C.glfwGetWindowPos(W, x, y)
return x[0], y[0]
end
---------------------------------------------------------------------------------------------------------------
function window.maximize()
C.glfwMaximizeWindow(W)
end
function window.minimize()
C.glfwIconifyWindow(W)
end
function window.restore()
C.glfwRestoreWindow(W)
end
function window.focus()
C.glfwFocusWindow(W)
end
function window.requestAttention()
C.glfwRequestWindowAttention(W)
end
---------------------------------------------------------------------------------------------------------------
function window.setTitle( title )
C.glfwSetWindowTitle(W, title)
__params.title = title
end
function window.getTitle()
return __params.title
end
---------------------------------------------------------------------------------------------------------------
function window.visible( state )
if state then C.glfwShowWindow(W)
else C.glfwHideWindow(W) end
end
function window.isVisible()
return C.glfwGetWindowAttrib(W, C.GLFW_VISIBLE) == 1
end
---------------------------------------------------------------------------------------------------------------
function window.setFullscreen( state, fstype, index )
index = index or 1
index = check_monitor(index) and index-1 or 0
local screenmode = C.glfwGetVideoMode( __monitors[index] )
if state then
assert(fstype == 'desktop' or fstype == 'exclusive', 'Invalid fullscreen type \''..tostring(fstype)..'\', expected one of : \'exclusive\' or \'desktop\'')
if fstype == 'desktop' then
C.glfwSetWindowAttrib(W, C.GLFW_DECORATED, 0)
local mx, my = ffi.new('int[1]'), ffi.new('int[1]')
C.glfwGetMonitorPos(__monitors[index], mx, my)
C.glfwSetWindowMonitor(W, nil, mx[0],my[0], screenmode.width, screenmode.height, 0)
elseif fstype == 'exclusive' then
C.glfwSetWindowMonitor(W, __monitors[index], 0,0, screenmode.width, screenmode.height, 0)
end
__params.fullscreentype = fstype
__params.fullscreen = true
else
__params.fullscreen = false
__params.fullscreentype = nil
if __params.x == nil or __params.y == nil then
__params.x = math.random(0, screenmode.width*0.3)
__params.y = math.random(0, screenmode.height*0.3)
centered = false
end
C.glfwSetWindowAttrib(W, C.GLFW_DECORATED, __params.borderless and 0 or 1)
C.glfwSetWindowMonitor(W, nil, __params.x, __params.y, __params.width, __params.height, 0)
end
end
function window.getFullscreen()
__params.fullscreen = C.glfwGetWindowMonitor(W) ~= nil
return __params.fullscreen, __params.fullscreentype
end
---------------------------------------------------------------------------------------------------------------
function window.getMode()
local flags = {}
flags.fullscreen = C.glfwGetWindowMonitor(W) ~= nil
flags.fullscreentype = __params.fullscreentype
flags.x, flags.y = ffi.new('int[1]'), ffi.new('int[1]')
C.glfwGetWindowPos(W, flags.x, flags.y)
flags.x, flags.y = flags.x[0], flags.y[0]
local width, height = ffi.new('int[1]'), ffi.new('int[1]')
C.glfwGetWindowSize(W, width, height)
width, height = width[0], height[0]
flags.msaa = __params.msaa
flags.vsync = __params.vsync
flags.topmost = C.glfwGetWindowAttrib(W, C.GLFW_FLOATING) == 1
flags.opacity = C.glfwGetWindowOpacity(W)
flags.borderless = C.glfwGetWindowAttrib(W, C.GLFW_DECORATED) == 0
flags.resizable = C.glfwGetWindowAttrib(W, C.GLFW_RESIZABLE) == 1
flags.centered = __params.centered
flags.display = __params.display
flags.minwidth = __params.minwidth
flags.minheight = __params.minheight
return width, height, flags
end
function window.setMode( width, height, flags )
if flags then
local _, _, mode = window.getMode()
for k,v in pairs(mode) do
if not flags[k] or flags[k] == nil then
flags[k] = v
end
end
flags.display = check_monitor(flags.display) and flags.display or 1
if flags.centered then
local screenmode = C.glfwGetVideoMode( __monitors[flags.display-1] )
local mx, my = ffi.new('int[1]'), ffi.new('int[1]')
C.glfwGetMonitorPos(__monitors[flags.display-1], mx, my)
flags.x = mx[0] + screenmode.width*0.5 - width*0.5
flags.y = my[0] + screenmode.height*0.5 - height*0.5
end
C.glfwSetWindowPos(W, flags.x, flags.y)
C.glfwSetWindowSizeLimits(W, flags.minwidth, flags.minheight, -1, -1)
C.glfwSetWindowAttrib(W, C.GLFW_DECORATED, flags.borderless and 0 or 1)
C.glfwSetWindowAttrib(W, C.GLFW_FLOATING, flags.topmost and 1 or 0)
C.glfwSetWindowAttrib(W, C.GLFW_RESIZABLE, flags.resizable and 1 or 0)
flags.opacity = math.max(0, math.min(flags.opacity, 1))
C.glfwSetWindowOpacity(W, flags.opacity)
if flags.fullscreen then
window.setFullscreen(flags.fullscreen, flags.fullscreentype, flags.display)
else
C.glfwSetWindowSize(W, width, height)
end
__params.width = width
__params.height = height
for k,v in pairs(flags) do
__params[k] = v
end
else
C.glfwSetWindowSize(W, width, height)
end
end
---------------------------------------------------------------------------------------------------------------
C.glfwSetWindowMaximizeCallback(W, function( target, maximized )
local width, height = ffi.new('int[1]'), ffi.new('int[1]')
C.glfwGetWindowSize(W, width, height)
lovr.event.push('maximized', maximized == 1, width[0], height[0])
end)
C.glfwSetWindowPosCallback(W, function( target, x,y )
if lovr.windowmoved then
lovr.windowmoved(x, y)
end
end)
C.glfwSetDropCallback(W, function( target, count, c_paths )
if lovr.dragdrop then
local paths = {}
for i=0, count-1 do
table.insert(paths, C_str(c_paths[i]))
end
lovr.dragdrop(paths)
end
end)
return window