-
Notifications
You must be signed in to change notification settings - Fork 5
/
nw_xlib.lua
1713 lines (1410 loc) · 42.2 KB
/
nw_xlib.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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--native windows - Xlib backend.
--Written by Cosmin Apreutesei. Public domain.
--A desktop made by Unix people is Satan's gift to us.
local ffi = require'ffi'
local bit = require'bit'
local glue = require'glue'
local box2d = require'box2d'
local xlib = require'xlib'
require'xlib_keysym_h'
local time = require'time' --for timers
local heap = require'heap' --for timers
local cast = ffi.cast
local xid = xlib.xid
local C = xlib.C
local glx --runtime dependency
local nw = {name = 'xlib'}
--xlib debugging -------------------------------------------------------------
local dbg = false --turn on to debug on xlib
if dbg then
dbg = require'xlib_debug'
function dbg_init()
dbg = dbg.connect(xlib)
--dbg.trace() --trace Xlib calls
end
local t0 = time.clock()
function dbg_event(e)
local dt = time.clock() - t0; t0 = t0 + dt
if dt > 0.5 then
print(('-'):rep(100))
end
print('EVENT', dbg.event_tostring(e))
end
end
--app object -----------------------------------------------------------------
local app = {}
nw.app = app
function app:init(frontend)
self.frontend = frontend
xlib = xlib.connect()
if dbg then dbg_init() end
xlib.synchronize(true) --shave off one source of unpredictability
xlib.set_xsettings_change_notify() --setup to receive XSETTINGS changes
return self
end
--version checks -------------------------------------------------------------
function app:ver(what)
if what == 'x' then
local maj, min = xlib.version()
return maj..'.'..min
end
end
--message loop ---------------------------------------------------------------
local winmap = {} --{XWindow (always a number) -> window_backend}
local function win(id) --window_id -> window_backend
return winmap[xid(id)]
end
local evname = { --{event_code -> event_name}
[C.PropertyNotify] = 'PropertyNotify',
[C.ClientMessage] = 'ClientMessage',
[C.FocusIn] = 'FocusIn',
[C.FocusOut] = 'FocusOut',
[C.ConfigureNotify] = 'ConfigureNotify',
[C.KeyPress] = 'KeyPress',
[C.KeyRelease] = 'KeyRelease',
[C.ButtonPress] = 'ButtonPress',
[C.ButtonRelease] = 'ButtonRelease',
[C.MotionNotify] = 'MotionNotify',
[C.EnterNotify] = 'EnterNotify',
[C.LeaveNotify] = 'LeaveNotify',
[C.Expose] = 'Expose',
}
local evfield = { --{event_code -> XEvent_field}
[C.PropertyNotify] = 'xproperty',
[C.ClientMessage] = 'xclient',
[C.FocusIn] = 'xfocus',
[C.FocusOut] = 'xfocus',
[C.ConfigureNotify] = 'xconfigure',
[C.KeyPress] = 'xkey',
[C.KeyRelease] = 'xkey',
[C.ButtonPress] = 'xbutton',
[C.ButtonRelease] = 'xbutton',
[C.MotionNotify] = 'xmotion',
[C.EnterNotify] = 'xcrossing',
[C.LeaveNotify] = 'xcrossing',
[C.Expose] = 'xexpose',
}
--event types for which we only keep the last one from any series
--of consecutive events of the same type (this is called event compression
--and it's important for preventing lag when resizing frameless windows).
local evrepeat = {
[C.Expose] = true,
[C.ConfigureNotify] = true,
}
local function dispatch(e)
if not e then return end
if dbg then dbg_event(e) end
local etype = tonumber(e.type)
local field = evfield[etype]
if not field then return end
local e = e[field]
local win = win(e.window)
return e, etype, win
end
local function poll(timeout)
local e, etype, win = dispatch(xlib.poll(timeout))
if win then
if evrepeat[etype] then
while true do --compress events
local e1, etype1, win1 = dispatch(xlib.peek())
if etype1 == etype and win1 == win then
e, etype, win = e1, etype1, win1
xlib.poll(true) --remove it
else
break
end
end
end
local f = win[evname[etype]]
if f then f(win, e) end
else
local f = app[evname[etype]]
if f then f(app, e) end
end
return true
end
function app:run()
while not self._stop do
local timeout = self:_pull_timers()
if self._stop then --stop() called from timer
while poll() do end --empty the queue
break
end
poll(timeout)
end
self._stop = false
end
function app:stop()
self._stop = true
end
--timers ---------------------------------------------------------------------
local function cmp(t1, t2)
return t1.time < t2.time
end
local timers = heap.valueheap{cmp = cmp}
local select_accuracy = 0.01 --assume 10ms accuracy of select()
--pull and execute all expired timers and return the timeout until the next one.
function app:_pull_timers()
while timers:length() > 0 do
local t = timers:peek()
local now = time.clock()
if now + select_accuracy / 2 > t.time then
if t.func() == false then --func wants timer to stop
timers:pop()
else --func wants timer to keep going
t.time = now + t.interval
timers:replace(1, t)
--break to avoid recurrent timers to starve the event loop.
return 0
end
else
return t.time - now --wait till next scheduled timer
end
end
return true --block indefinitely
end
function app:runevery(seconds, func)
timers:push({time = time.clock() + seconds, interval = seconds, func = func})
end
--xsettings ------------------------------------------------------------------
local xsettings
function app:PropertyNotify(e)
local prop = tonumber(e.atom)
if prop == xlib.atom'_XSETTINGS_SETTINGS' then
xsettings = nil
end
end
function app:_xsettings(key)
if xsettings == nil then
xsettings = xlib.get_xsettings() or false
end
if not xsettings then return end
return xsettings[key] and xsettings[key].value
end
--windows --------------------------------------------------------------------
local window = {}
app.window = window
--constrain the client size (cw, ch) based on current size constraints.
local function clamp_opt(x, min, max)
if min then x = math.max(x, min) end
if max then x = math.min(x, max) end
return x
end
function window:__constrain(cw, ch)
cw = clamp_opt(cw, self._min_cw, self._max_cw)
ch = clamp_opt(ch, self._min_ch, self._max_ch)
return cw, ch
end
--check if a given screen has a bgra8 visual (for creating windows with alpha).
local function find_bgra8_visual(screen)
for i=0,screen.ndepths-1 do
local d = screen.depths[i]
if d.depth == 32 then
for i=0,d.nvisuals-1 do
local v = d.visuals[i]
if v.bits_per_rgb == 8 and v.blue_mask == 0xff then --BGRA8
return v
end
end
end
end
end
local last_active_window
function window:new(app, frontend, t)
self = setmetatable({app = app, frontend = frontend}, {__index = self})
local attrs = {}
--say that we don't want the server to keep a pixmap for the window.
attrs.background_pixmap = 0
--declare what events we want to receive.
attrs.event_mask = bit.bor(
C.KeyPressMask,
C.KeyReleaseMask,
C.ButtonPressMask,
C.ButtonReleaseMask,
C.EnterWindowMask,
C.LeaveWindowMask,
C.PointerMotionMask,
--C.KeymapStateMask, --not used
C.ExposureMask,
--C.VisibilityChangeMask, --not used
C.StructureNotifyMask,
--C.ResizeRedirectMask, --not working
C.SubstructureNotifyMask,
--C.SubstructureRedirectMask, --not used
C.FocusChangeMask,
C.PropertyChangeMask,
--C.ColormapChangeMask, --not used
C.OwnerGrabButtonMask,
0)
if t.transparent then
--find a 32bit BGRA8 visual so we can create a window with alpha.
attrs.visual = find_bgra8_visual(xlib.screen)
if attrs.visual then
attrs.depth = 32
--creating a 32bit-depth window requires creating a colormap!
attrs.colormap = xlib.create_colormap(xlib.screen.root, attrs.visual)
--setting a colormap requires setting border_pixel!
attrs.border_pixel = 0
end
end
--store window's depth for put_image()
self._depth = attrs.depth or xlib.screen.root_depth
self._visual = attrs.visual or xlib.screen.root_visual
--store and apply constraints to client size
self._min_cw = t.min_cw
self._min_ch = t.min_ch
self._max_cw = t.max_cw
self._max_ch = t.max_ch
local cx, cy, cw, ch = app.frontend:frame_to_client(
t.frame, t.menu, t.x or 0, t.y or 0, t.w, t.h)
cx = t.x and cx
cy = t.y and cy
cw, ch = self:__constrain(cw, ch)
attrs.x = cx
attrs.y = cy
attrs.width = cw
attrs.height = ch
self.win = xlib.create_window(attrs)
xlib.flush() --lame: XSynchronize() didn't do it's job here
--NOTE: WMs ignore the initial position unless we set WM_NORMAL_HINTS too
--(values don't matter, but we're using the same coordinates just in case).
local hints = {x = cx, y = cy}
if not t.resizeable then
--this is how X knows that a window is non-resizeable (there's no flag).
hints.min_width = cw
hints.min_height = ch
hints.max_width = cw
hints.max_height = ch
else
--tell X about any (already-applied) constraints.
hints.min_width = t.min_cw
hints.min_height = t.min_ch
--NOTE: we can't set a constraint on one axis alone, hence the 2^24.
if t.max_cw or t.max_ch then
hints.max_width = t.max_cw or 2^24
hints.max_height = t.max_ch or 2^24
end
end
xlib.set_wm_size_hints(self.win, hints)
--declare the X protocols that the window supports.
xlib.set_atom_map_prop(self.win, 'WM_PROTOCOLS', {
WM_DELETE_WINDOW = true, --don't close the connection when a window is closed
_NET_WM_PING = true, --respond to ping events
})
--set info for _NET_WM_PING to allow the user to kill a non-responsive process.
xlib.set_net_wm_ping_info(self.win)
if t.title then
self:set_title(t.title)
end
if t.parent then
xlib.set_transient_for(self.win, t.parent.backend.win)
end
--set motif hints before mapping the window.
--NOTE: MWM_DECOR_RESIZEH alone brings all frame elements with it,
--so we can't have frameless windows that are also resizeable.
local hints = ffi.new'PropMotifWmHints'
hints.flags = bit.bor(
C.MWM_HINTS_FUNCTIONS,
C.MWM_HINTS_DECORATIONS)
hints.functions = bit.bor(
t.resizeable and C.MWM_FUNC_RESIZE or 0,
C.MWM_FUNC_MOVE,
t.minimizable and C.MWM_FUNC_MINIMIZE or 0,
t.maximizable and C.MWM_FUNC_MAXIMIZE or 0,
t.closeable and C.MWM_FUNC_CLOSE or 0)
hints.decorations = t.frame == 'none' and 0 or bit.bor(
C.MWM_DECOR_BORDER,
C.MWM_DECOR_TITLE,
C.MWM_DECOR_MENU,
t.resizeable and C.MWM_DECOR_RESIZEH or 0,
t.minimizable and C.MWM_DECOR_MINIMIZE or 0,
t.maximizable and C.MWM_DECOR_MAXIMIZE or 0)
xlib.set_motif_wm_hints(self.win, hints)
--flag to mask off window's reported state while the window is unmapped.
self._hidden = true
--if given, the initial _outer_ position that must be set again on show!
self._init_pos = (t.x or t.y) and {x = t.x, y = t.y}
--state flags to be reported while the window is hidden.
self._minimized = t.minimized or false
self._maximized = t.maximized or false
self._fullscreen = t.fullscreen or false
self._topmost = t.topmost or false
self:_init_opengl(t.opengl)
winmap[self.win] = self
--if this is the first window, register it as the last active window
--just in case the user calls app:activate() before this window activates.
if not last_active_window then
last_active_window = self
end
return self
end
function window:PropertyNotify(e)
local prop = tonumber(e.atom)
if prop == xlib.atom'WM_STATE' then
self:PropertyNotify_WM_STATE()
elseif prop == xlib.atom'_NET_WM_STATE' then
self:PropertyNotify__NET_WM_STATE()
end
end
--closing --------------------------------------------------------------------
function window:ClientMessage(e)
local v = e.data.l[0]
if e.message_type == xlib.atom'WM_PROTOCOLS' then
if v == xlib.atom'WM_DELETE_WINDOW' then
self.frontend:close()
elseif v == xlib.atom'_NET_WM_PING' then
xlib.pong(e)
end
end
end
function window:forceclose()
--force-close child windows first, consistent with Windows.
for i,win in ipairs(self.frontend:children()) do
win:close(true)
end
--trigger closed event after children are closed but before destroying the window.
self.frontend:_backend_closed()
xlib.destroy_window(self.win)
winmap[self.win] = nil --discard further messages
self.win = nil --prevent usage
--register another random window as the last active window so that
--app:activate() works even before the next window gets activated.
--in any case we want to release the reference to self.
if last_active_window == self then
local _, backend = next(winmap)
last_active_window = backend
end
end
--activation -----------------------------------------------------------------
--how much to wait for another window to become active after a window
--is deactivated, before triggering an 'app deactivated' event.
local focus_out_timeout = 0.1
local last_focus_out
local focus_timer_started
local app_active = false
--NOTE: FocusIn is also sent after ending a resizing operation.
function window:FocusIn(e)
if self._hiding or self._hidden then return end --ignore while hiding
last_active_window = self
last_focus_out = nil --disable the app deactivate timer
app_active = true --window activation implies app activation.
self.app.frontend:_backend_changed()
self.frontend:_backend_changed()
end
--NOTE: FocusOut is also sent before starting a resizing operation.
--NOTE: when hiding, FocusOut is sent before PropertyNotify/WM_STATE.
--NOTE: when minimizing, by the time FocusOut is sent, _NET_WM_STATE_HIDDEN
--is also set, thus was_minimized event will happen here.
function window:FocusOut(e)
if self._hiding or self._hidden then return end --ignore while hiding
--start a delayed check for when the app is deactivated.
--if a timer is already started, just advance the delay.
last_focus_out = time.clock()
if not focus_timer_started then
self.app.frontend:runafter(focus_out_timeout, function()
if last_focus_out and time.clock() - last_focus_out > focus_out_timeout then
last_focus_out = nil
app_active = false
self.app.frontend:_backend_changed()
end
focus_timer_started = false
end)
focus_timer_started = true
end
self.frontend:_backend_changed()
end
function app:activate(mode)
if mode ~= 'force' then return end
--unlike OSX, in X you don't activate an app, you can only activate a window.
--activating this app means activating the last window that was active.
local win = last_active_window
if win and not win.frontend:dead() then
xlib.change_net_active_window(last_active_window.win)
end
end
function app:active_window()
--return the active window only if the app is active, consistent with OSX.
local win = app_active and win(xlib.get_input_focus())
return win and win.frontend or nil
end
function app:active()
return app_active
end
function window:activate()
--for consistency with OSX, if the app is inactive, this function
--doesn't activate the window, instead it marks the window that must
--be activated on the next call to app:activate().
if not app_active then
last_active_window = self
else
xlib.change_net_active_window(self.win)
end
end
function window:active()
return self.app:active_window() == self.frontend
end
--state/app visibility -------------------------------------------------------
function app:visible() return true end
function app:hide() end
function app:unhide() end
--state/visibility -----------------------------------------------------------
function window:visible()
return not self._hidden
end
function window:show()
if not self._hidden then return end --hiding or visible: ignore
if self._normal_rect then
--set the saved normal rect before mapping the window.
--this is because the normal rect is lost when hiding a maximized window.
self:set_frame_rect(unpack(self._normal_rect))
else
--store _normal_rect
self._normal_rect = {self:get_frame_rect()}
end
--the initial _frame_ position (if any) must be set again now.
if self._init_pos then
xlib.config(self.win, self._init_pos)
self._init_pos = nil
end
--set the _NET_WM_STATE property before mapping the window.
--later on we have to use change_net_wm_state() to change these values.
xlib.set_net_wm_state(self.win, {
_NET_WM_STATE_MAXIMIZED_HORZ = self._maximized or nil,
_NET_WM_STATE_MAXIMIZED_VERT = self._maximized or nil,
_NET_WM_STATE_HIDDEN = self._minimized or nil,
_NET_WM_STATE_ABOVE = self._topmost or nil,
_NET_WM_STATE_FULLSCREEN = self._fullscreen or nil,
})
xlib.map(self.win) --async operation
end
function window:hide()
if self._hidden then return end
--window state to report while hidden.
self._minimized = self:minimized()
self._maximized = self:maximized()
self._fullscreen = self:fullscreen()
self._hiding = true --signal intent to hide in subsequent events
local x, y = self:get_client_pos()
xlib.withdraw(self.win) --async operation
--move it back to position because Unity moves it at the frame coordinates!
xlib.config(self.win, {x = x, y = y})
end
function window:PropertyNotify_WM_STATE()
if self._hiding and not xlib.get_wm_state_visible(self.win) then
self._hiding = false
self._hidden = true --switch to reporting stored state
elseif self._hidden and xlib.get_wm_state_visible(self.win) then
self._hidden = false --switch to reporting real state
end
self.frontend:_backend_changed() --visibility and minimization changes
end
--state/minimizing -----------------------------------------------------------
function window:minimized()
if self._hidden then
return self._minimized
end
return xlib.get_net_wm_state_hidden(self.win)
end
function window:minimize()
if self:fullscreen() then return end --TODO: remove this after fixing OSX
if self._hidden then
self._minimized = true
self:show()
else
xlib.iconify(self.win)
end
end
function window:_unminimize()
xlib.map(self.win)
self:activate() --because map alone doesn't activate a minimized window
end
--state/maximizing -----------------------------------------------------------
function window:maximized()
if self._hidden then
return self._maximized
end
return xlib.get_net_wm_state_maximized(self.win)
end
function window:maximize()
if self:fullscreen() then return end --TODO: remove this after fixing OSX
if self._hidden then
self._maximized = true
self._minimized = false
self:show()
else
xlib.change_net_wm_state_maximized(self.win, true)
self:_unminimize()
end
end
function window:_unmaximize()
if self._hidden then
self._minimized = false
self._maximized = false
self:show()
else
xlib.change_net_wm_state_maximized(self.win, false)
end
end
function window:PropertyNotify__NET_WM_STATE()
if self._hiding or self._hidden then return end --ignore events while hidden
self.frontend:_backend_changed() --maximization and fullscreen changes
end
--state/restoring ------------------------------------------------------------
function window:restore()
if self._hidden then
if self._minimized then
self._minimized = false
elseif self._fullscreen then
self._fullscreen = false
elseif self._maximized then
self._maximized = false
end
self:show()
elseif self:minimized() then
self:_unminimize()
elseif self:fullscreen() then
self:fullscreen(false)
else
self:_unmaximize()
end
end
function window:shownormal()
if self:fullscreen() then return end --TODO: remove this after fixing OSX
if self._hidden then
self._minimized = false
self._maximized = false
self._fullscreen = false
self:show()
else
self:fullscreen(false)
self:_unmaximize()
self:_unminimize()
end
end
--state/fullscreen -----------------------------------------------------------
function window:fullscreen()
if self._hidden then
return self._fullscreen
end
return xlib.get_net_wm_state_fullscreen(self.win)
end
function window:enter_fullscreen()
if self._hidden then
self._fullscreen = true
self._minimized = false
self:show()
else
self:_unminimize()
xlib.change_net_wm_state_fullscreen(self.win, true)
end
end
function window:exit_fullscreen()
xlib.change_net_wm_state_fullscreen(self.win, false)
end
--state/enabled --------------------------------------------------------------
function window:get_enabled()
return not self._disabled
end
function window:set_enabled(enabled)
self._disabled = not enabled
end
--positioning/frame extents --------------------------------------------------
local function unmapped_frame_extents(win)
local w1, h1, w2, h2
if xlib.frame_extents_supported() then
xlib.request_frame_extents(win)
w1, h1, w2, h2 = xlib.get_frame_extents(win)
--some WMs set the extents later or not at all so we have to poll.
if not w1 then
local timeout = time.clock() + 0.2
local period = 0.01
while true do
w1, h1, w2, h2 = xlib.get_frame_extents(win)
if w1 or time.clock() > timeout then break end
time.sleep(period)
period = period * 2
end
end
end
if not w1 then --bail out with wrong values.
w1, h1, w2, h2 = 0, 0, 0, 0
end
return w1, h1, w2, h2
end
local frame_extents = glue.memoize(function(frame, has_menu, resizeable)
--create a dummy window
local win = xlib.create_window{width = 200, height = 200}
--get its frame extents
local w1, h1, w2, h2 = unmapped_frame_extents(win)
--destroy the window
xlib.destroy_window(win)
--compute/return the frame rectangle
return {w1, h1, w2, h2}
end)
function app:frame_extents(frame, has_menu, resizeable)
return unpack(frame_extents(frame, has_menu, resizeable))
end
--positioning/rectangles -----------------------------------------------------
function window:get_client_size()
local _, _, w, h = xlib.get_geometry(self.win)
return w, h
end
function window:get_client_pos()
local x, y = xlib.translate_coords(self.win, xlib.screen.root, 0, 0)
return x, y
end
local function frame_rect(x, y, w, h, w1, h1, w2, h2)
return x - w1, y - h1, w + w1 + w2, h + h1 + h2
end
local function unframe_rect(x, y, w, h, w1, h1, w2, h2)
return frame_rect(x, y, w, h, -w1, -h1, -w2, -h2)
end
function window:_frame_extents()
if self.frontend:frame() == 'none' then
return 0, 0, 0, 0
end
return unmapped_frame_extents(self.win)
end
function window:get_normal_frame_rect()
if self._normal_rect then
return unpack(self._normal_rect)
else
return self:get_frame_rect()
end
end
function window:get_frame_rect()
local x, y = self:get_client_pos()
local w, h = self:get_client_size()
return frame_rect(x, y, w, h, self:_frame_extents())
end
function window:set_frame_rect(x, y, w, h)
local _, _, cw, ch = unframe_rect(x, y, w, h, self:_frame_extents())
xlib.config(self.win, {x = x, y = y, width = cw, height = ch, border_width = 0})
end
--positioning/constraints ----------------------------------------------------
function window:_apply_constraints()
--get the current client size and the new (constrained) client size
local cw0, ch0 = self:get_client_size()
local cw, ch = self:__constrain(cw0, ch0)
--update constraints
local hints = {}
if not self.frontend:resizeable() then
if cw ~= cw0 or ch ~= ch0 then
hints.min_width = cw
hints.min_height = ch
hints.max_width = cw
hints.max_height = ch
end
else
hints.min_width = self._min_cw or 0
hints.min_height = self._min_ch or 0
hints.max_width = self._max_cw or 2^24
hints.max_height = self._max_ch or 2^24
end
xlib.set_wm_size_hints(self.win, hints)
--resize the window if dimensions changed
if cw ~= cw0 or ch ~= ch0 then
xlib.config(self.win, {width = cw, height = ch, border_width = 0})
end
end
function window:get_minsize()
return self._min_cw, self._min_ch
end
function window:get_maxsize()
return self._max_cw, self._max_ch
end
function window:set_minsize(min_cw, min_ch)
self._min_cw, self._min_ch = min_cw, min_ch
self:_apply_constraints()
end
function window:set_maxsize(max_cw, max_ch)
self._max_cw, self._max_ch = max_cw, max_ch
self:_apply_constraints()
end
--positioning/resizing -------------------------------------------------------
function window:ConfigureNotify(e)
if self._hiding or self._hidden then return end --reparenting, ignore
--track normal rect because there's no way to get it from X.
--NOTE: we have to track this through resizes instead of just saving it
--before maximizing because by the time PropertyNotify for _NET_WM_STATE
--is triggered the frame is already maximized.
if not (self:maximized() or self:fullscreen()) then
self._normal_rect = {self:get_frame_rect()}
end
--NOTE: don't bother trying to implement sticky children.
--It's impossible to do that right with the async model (think about it).
self.frontend:_backend_changed()
end
--positioning/magnets --------------------------------------------------------
--not useful since we can't intervene in window resizing.
function window:magnets() end
--titlebar -------------------------------------------------------------------
function window:get_title()
return xlib.get_wm_name(self.win)
end
function window:set_title(title)
xlib.set_wm_name(self.win, title)
xlib.set_wm_icon_name(self.win, title)
end
--z-order --------------------------------------------------------------------
function window:get_topmost()
return self._topmost
end
function window:set_topmost(topmost)
xlib.change_net_wm_state(self.win, topmost, '_NET_WM_STATE_ABOVE')
end
function window:raise(relto)
--TODO: implement relto
xlib.raise(self.win)
end
function window:lower(relto)
--TODO: implement relto
xlib.lower(self.win)
end
--displays -------------------------------------------------------------------
function app:displays()
local t = {}
local screens, n = xlib.xinerama_screens() --the order is undefined
if screens and n > 1 then --multi-monitor setup
for i = 0, n-1 do
local scr = screens[i]
local x = scr.x_org
local y = scr.y_org
local w = scr.width
local h = scr.height
--TODO: find a way to get the workarea of each monitor in multi-monitor setups.
--NOTE: _NET_WORKAREA spans all monitors so it's no good here!
local cx, cy, cw, ch = x, y, w, h
t[#t+1] = self.frontend:_display{
x = x, y = y, w = w, h = h,
cx = cx, cy = cy, cw = cw, ch = ch,
scalingfactor = 1}
end
else
--Xinerama not present or single monitor setup
local x = 0
local y = 0
local w = xlib.screen.width
local h = xlib.screen.height
local cx, cy, cw, ch
--get the workarea of the first virtual desktop
local wa = xlib.get_net_workarea(nil, 1)
if wa then
cx, cy, cw, ch = unpack(wa)
else
--_NET_WORKAREA not set, fallback to using screen area
cx, cy, cw, ch = x, y, w, h
end
t[1] = self.frontend:_display{
x = x, y = y, w = w, h = h,
cx = cx, cy = cy, cw = cw, ch = ch,
scalingfactor = 1}
end
return t
end
function app:display_count()
local _, n = xlib.xinerama_screens()
return n or 1
end
function app:_display_overlapping(x, y, w, h)
--get all displays and the overlapping area between them and the window
local t = {}
for i,d in ipairs(self:displays()) do
local _, _, w1, h1 = box2d.clip(d.x, d.y, d.w, d.h, x, y, w, h)
t[#t+1] = {area = w1 * h1, display = d}
end
--sort displays by overlapping area in descending order.
table.sort(t, function(t1, t2) return t1.area > t2.area end)
--return nil if there's no overlapping area i.e. the window is off-screen
if t[1].area == 0 then return end
--return the display with the most overlapping
return t[1].display
end
function app:main_display()
for i,d in ipairs(self:displays()) do
if d.x == 0 and d.y == 0 then --main display is at (0, 0) by definition
return d
end
end
end
function app:active_display()
local display
local win = xlib.get_input_focus()
if win then
--get the client rect of the active window in screen space.