Repository navigation
Expand file tree
/
Copy pathGamepadIntegration.lua
More file actions
1310 lines (1303 loc) · 38.3 KB
/
Copy pathGamepadIntegration.lua
File metadata and controls
1310 lines (1303 loc) · 38.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
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 Forever navigation, using capability-checked wrappers. QT owns every
-- configured frame; native focus/binding tables and cursor anchors stay native.
local QT = _G.QuestTogether
local records, observers, owner = {}, {}, {}
local state = { generation = 0, order = {}, events = {} }
local Reconcile, Schedule, Notify, Retire
local function Accessible(value)
return not (issecretvalue and issecretvalue(value)) and (not canaccessvalue or canaccessvalue(value))
end
local function Methods(object, names)
if not object or not Accessible(object) then
return false
end
for _, name in ipairs(names) do
if type(object[name]) ~= "function" then
return false
end
end
return true
end
local function Manager()
return GamepadMode and GamepadMode.FrameControlsManager
end
local function BuildDiagnostic(value)
if not Accessible(value) then return "unknown" end
if type(value) == "number" then
if value ~= value or value < 0 or value > 999999999999 then return "unknown" end
value = tostring(value)
end
if type(value) ~= "string" or #value > 32 or not value:match("^%d[%d%.]*$") then return "unknown" end
return value
end
local function SupportedClient()
if state.supportedClient ~= nil then
return state.supportedClient
end
if type(GetBuildInfo) ~= "function" then
return false
end
local ok, version, build = pcall(GetBuildInfo)
if not ok then return false end
state.clientVersion, state.clientBuild = BuildDiagnostic(version), BuildDiagnostic(build)
-- Build numbers are diagnostic only; native capabilities govern compatibility.
state.supportedClient = Accessible(version) and version == "1.60.1"
return state.supportedClient
end
local function AuthoredGeneration()
if not SupportedClient() then
return false, "unsupported_client"
end
if
not Methods(
SmartNavigationSecureHelper,
{ "WrapFunction", "SetSecureValue", "SecureCallOptionalFunction", "IsSecureOrOutOfCombatRestriction" }
)
then
return false, "native_missing_functions"
end
-- The client family, helper feature set and secure entrypoints bound
-- support without requiring a patch for each client build. Never
-- call WrapFunction/SetSecureValue or write the helper's attributes ourselves.
if type(issecurevariable) ~= "function" then
return false, "native_security_wrapper_unavailable"
end
for _, spec in ipairs({
{ Manager(), "FrameShown", "FrameHidden", "HandlePopupShown" },
{ SmartNavigation, "SetTargetButtonForFrame", "SetScrollFrameForFrame", "RefreshButtonGroups" },
}) do
if not spec[1] or not Accessible(spec[1]) then
return false, "native_missing_functions"
end
for i = 2, #spec do
local ok, secure = pcall(issecurevariable, spec[1], spec[i])
if not ok or not Accessible(secure) or secure ~= true then
return false, "native_security_wrapper_unavailable"
end
end
end
return true
end
local function Capable()
if state.capable then
return true
end
local authored, reason = AuthoredGeneration()
state.capable = authored
and Methods(
Manager(),
{ "FrameShown", "FrameHidden", "HandlePopupShown", "GetActiveFrame", "GetShownFrameCount" }
)
and Methods(SmartNavigation, {
"SetTargetButtonForFrame",
"SetScrollFrameForFrame",
"RefreshButtonGroups",
"GetActiveFrame",
"GetCurrentButton",
"IsShown",
"IsVisible",
"RegisterCallback",
})
and Methods(UIParent, { "IsShown", "IsVisible" })
and Methods(
InputUtil,
{ "IsGamepadUIEnabled", "RegisterForInterfaceTransitions", "RegisterGamepadUninit", "RegisterGamepadInit" }
)
and Methods(C_Timer, { "After" })
and Methods(EventRegistry, { "RegisterCallback" })
and Methods(C_RestrictedActions, { "IsAddOnRestrictionActive" })
and Enum
and Enum.AddOnRestrictionType
and Enum.AddOnRestrictionType.Combat
and Methods(GamepadSharedUtility, { "CreatePromptedBindingFooter" })
and Methods(PromptedBindingFooterMixin, {
"AddStandardSelectPrompt",
"AddStandardBackPrompt",
"Finalize",
"IsBindingGroupRequired",
"ShowAndActivateBindings",
"HideAndDeactivateBindings",
})
and type(SmartNavigation_MarkFrameIgnoredContainerFrame) == "function"
and type(SmartNavigation_MarkFrameIgnored) == "function"
and type(SmartNavigation_ClearIgnoreStatus) == "function"
and type(SmartNavigation_AddJumpNavigationOverride) == "function"
and SMART_NAV_INPUT_DIRECTION
and Accessible(SMART_NAV_INPUT_DIRECTION)
if state.capable then
for _, name in ipairs({ "UP", "DOWN", "LEFT", "RIGHT" }) do
local direction = SMART_NAV_INPUT_DIRECTION[name]
if not Accessible(direction) or type(direction) ~= "table" or direction.dirKey ~= name:lower() then
state.capable = false
break
end
end
end
if state.capable then state.unavailableReason = nil
else state.unavailableReason = reason or "native_missing_functions" end
return state.capable
end
local function Restricted()
return QT:IsRuntimeRestricted()
or (InCombatLockdown and InCombatLockdown())
or (
C_RestrictedActions
and Enum
and Enum.AddOnRestrictionType
and C_RestrictedActions.IsAddOnRestrictionActive(Enum.AddOnRestrictionType.Combat)
)
end
-- Dismissal only removes existing owned presentation. Ordinary combat lockdown
-- does not itself forbid the native wrapper, but an active addon restriction
-- does. Never use this narrower policy for registration or quest actions.
local function DismissRestricted()
if not Methods(C_RestrictedActions, { "IsAddOnRestrictionActive" })
or not Enum or not Enum.AddOnRestrictionType then return true end
local ok, active = pcall(C_RestrictedActions.IsAddOnRestrictionActive, Enum.AddOnRestrictionType.Combat)
if not ok or not Accessible(active) or active ~= false then return true end
if type(QT.IsRuntimeRestrictionTypeActive) ~= "function" then return true end
for kind in pairs(QT.runtimeRestrictionTypes or { combat = true, encounter = true, challenge = true, pvp = true, map = true }) do
if QT:IsRuntimeRestrictionTypeActive(kind) then
-- Activating is the documented pre-enforcement event window. Only
-- teardown may use it; ordinary actions still treat it as blocked.
local states = Enum.AddOnRestrictionState
if kind ~= "combat" or not states or states.Activating == nil
or not Methods(C_RestrictedActions, { "GetAddOnRestrictionState" }) then return true end
local read, phase = pcall(C_RestrictedActions.GetAddOnRestrictionState, Enum.AddOnRestrictionType.Combat)
if not read or not Accessible(phase) or phase ~= states.Activating then return true end
end
end
return false
end
-- Keep only the latest combat close attempt, including after combat recovery.
-- This is local scalar state: no player identities, quest data or frame refs.
local function DiagnosticBool(fn, ...)
if type(fn) ~= "function" then return "unknown" end
local ok, value = pcall(fn, ...)
if ok and Accessible(value) and type(value) == "boolean" then return value end
return "unknown"
end
local function RememberCombatClose(record, source)
if DiagnosticBool(Restricted) == false then return end
local snapshot = {
role = record.role, source = source, outcome = "requested",
lockdown = DiagnosticBool(InCombatLockdown),
runtimeRestricted = DiagnosticBool(QT.IsRuntimeRestricted, QT),
dismissalRestricted = DiagnosticBool(DismissRestricted),
registered = record.registered == true, retiring = record.retiring == true,
failed = state.failed == true,
}
local api, kinds = C_RestrictedActions, Enum and Enum.AddOnRestrictionType
snapshot.nativeCombat = DiagnosticBool(api and api.IsAddOnRestrictionActive, kinds and kinds.Combat)
for _, kind in ipairs({ "combat", "encounter", "challenge", "pvp", "map" }) do
snapshot[kind] = DiagnosticBool(QT.IsRuntimeRestrictionTypeActive, QT, kind)
end
state.lastCombatClose = snapshot
return snapshot
end
local function Owned(frame)
return frame and QT.LibChev.CanMutateOwnedRegion(frame)
end
local function Visible()
return UIParent:IsShown() and UIParent:IsVisible()
end
local function AncestorsVisible(record)
if not Visible() then
return false
end
local parent = record.frame:GetParent()
if not parent then
return true
end
return Accessible(parent)
and (not QT.CanAccessForeignFrame or QT:CanAccessForeignFrame(parent))
and parent:IsShown()
and parent:IsVisible()
end
local function Log(event, role)
state.events[#state.events + 1] = { event = event, role = role or "", generation = state.generation }
if #state.events > 64 then
table.remove(state.events, 1)
end
end
local function Fail(reason)
if state.failed then
return
end
state.failed, state.failure, state.reloadRequired = true, reason, true
state.generation, state.scheduled = state.generation + 1, nil
Log("stopped", reason)
if QT.RecordDiagnosticError then
QT:RecordDiagnosticError("native_gamepad", reason .. "; reload required")
end
if Notify then
Notify()
end
end
local function Native(method, target, ...)
local blocked = method == "FrameHidden" and DismissRestricted() or method ~= "FrameHidden" and Restricted()
if state.failed or blocked then
return false
end
if not AuthoredGeneration() then
Fail("native_entrypoint_unavailable")
return false
end
state.monitoring = true
local ok, result = pcall(target[method], target, ...)
if not ok then
Fail(method)
return false
end
if state.failed then
return false
end
return true, result
end
local function Call(callback, ...)
if type(callback) ~= "function" then
return true
end
local ok = pcall(callback, ...)
if not ok then
Fail("owned_callback")
end
return ok and not state.failed
end
-- This remains independent of the QT setting/capability latch so controller
-- callers never fall back to a native context menu after integration stops.
function QT:IsNativeGamepadMode()
if self ~= QT or rawget(QT, "isRunningTests") == true then
return false
end
if not Methods(InputUtil, { "IsGamepadUIEnabled" }) then
return false
end
local ok, enabled = pcall(InputUtil.IsGamepadUIEnabled)
return ok and Accessible(enabled) and enabled == true
end
QT.GetNativeGamepadMode = QT.IsNativeGamepadMode
local function ReadFrame(frame)
if not Accessible(frame) or not frame then
return nil
end
if QT.CanAccessForeignFrame and not QT:CanAccessForeignFrame(frame) then
return nil
end
return frame
end
function QT:GetNativeGamepadFocus()
if self ~= QT or rawget(QT, "isRunningTests") == true then
return nil, nil, nil
end
if not Capable() or Restricted() or not self:IsNativeGamepadMode() then
return nil, nil, nil
end
local focused = Manager().isUIFocused
if not Accessible(focused) or focused ~= true then
return nil, nil, nil
end
local manager = ReadFrame(Manager():GetActiveFrame())
if not SmartNavigation:IsShown() or not SmartNavigation:IsVisible() then
return manager, nil, nil
end
return manager, ReadFrame(SmartNavigation:GetActiveFrame()), ReadFrame(SmartNavigation:GetCurrentButton())
end
local function Snapshot()
local available = not state.failed
and state.enabled
and Capable()
and not Restricted()
and QT:IsNativeGamepadMode()
and Visible()
if not available then
return { available = false, uiFocused = false }
end
local manager, frame, button = QT:GetNativeGamepadFocus()
local focused = Manager().isUIFocused
local uiFocused = Accessible(focused) and focused == true
return {
available = true,
uiFocused = uiFocused,
manager = uiFocused and manager or nil,
frame = uiFocused and frame or nil,
button = uiFocused and button or nil,
}
end
Notify = function()
local snapshot = Snapshot()
local failedBefore = state.failed
for subscriber, callback in pairs(observers) do
-- Consumers receive a fresh QT table, never the native registry/payload.
local ok = pcall(callback, {
available = snapshot.available,
uiFocused = snapshot.uiFocused,
manager = snapshot.manager,
frame = snapshot.frame,
button = snapshot.button,
})
if not ok then
observers[subscriber] = nil
-- Remove the failing observer before publishing the stopped snapshot.
-- Fail can notify the remaining observers; never continue this stale
-- snapshot or recursively latch another failure during that delivery.
if not state.failed then
Fail("observer_callback")
end
return
end
if not failedBefore and state.failed then
return
end
end
local record = snapshot.frame and records[snapshot.frame]
if record and record.registered and not record.retiring then
Call(record.policy.onSelection, record.frame, snapshot.button)
end
end
function QT:RegisterGamepadObserver(subscriber, callback)
if self ~= QT or rawget(QT, "isRunningTests") == true then
return false
end
if subscriber == nil or type(callback) ~= "function" then
return false
end
observers[subscriber] = callback
self:InitializeGamepadIntegration()
Notify()
return not state.failed
end
function QT:UnregisterGamepadObserver(subscriber)
if self == QT and rawget(QT, "isRunningTests") ~= true then
observers[subscriber] = nil
end
end
function QT:IsGamepadWindowFocused(frame)
if self ~= QT or rawget(QT, "isRunningTests") == true then
return false
end
local snapshot = Snapshot()
return snapshot.available and snapshot.uiFocused and snapshot.manager == frame and snapshot.frame == frame
end
function QT:IsGamepadWindowRegistered(frame)
if self ~= QT or rawget(QT, "isRunningTests") == true then
return false
end
local record = records[frame]
return record ~= nil and (record.attempted == true or record.registered == true or record.retiring == true)
end
function QT:IsGamepadActionCurrent(frame)
if self ~= QT or rawget(QT, "isRunningTests") == true then
return false
end
local record = records[frame]
return record ~= nil
and record.registered == true
and not record.retiring
and not state.failed
and state.enabled
and Owned(frame)
and frame:IsVisible()
and self:IsGamepadWindowFocused(frame)
end
local function Verify(frame, target, reason)
local manager, active, selected = QT:GetNativeGamepadFocus()
if manager ~= frame or active ~= frame or (target and selected ~= target) or not SmartNavigation:IsShown() then
Fail("focus_mismatch_" .. reason)
return false
end
return not state.failed
end
local function FooterHide(record)
local footer = record.footer
if state.failed then
return false
end
if not footer then
return true
end
if footer:IsBindingGroupRequired() or footer.bindings then
Fail("footer_bindings")
return false
end
return Call(footer.HideAndDeactivateBindings, footer)
end
local function Setup(record)
if record.prepared then
return true
end
state.monitoring = true
local ok = pcall(SmartNavigation_MarkFrameIgnoredContainerFrame, record.frame)
if not ok then
Fail("container_setup")
end
if state.failed then
return false
end
if record.policy.footer ~= false then
local made, footer =
pcall(GamepadSharedUtility.CreatePromptedBindingFooter, record.frame, "QuestTogether" .. record.role)
if not made or state.failed then
if not state.failed then
Fail("footer_setup")
end
return false
end
if
not Methods(footer, {
"AddStandardSelectPrompt",
"AddStandardBackPrompt",
"Finalize",
"IsBindingGroupRequired",
"ShowAndActivateBindings",
"HideAndDeactivateBindings",
})
then
Fail("footer_contract")
return false
end
if
not Call(footer.AddStandardSelectPrompt, footer)
or not Call(footer.AddStandardBackPrompt, footer)
or not Call(footer.Finalize, footer)
then
return false
end
if footer:IsBindingGroupRequired() or footer.bindings then
Fail("footer_bindings")
return false
end
record.footer = footer
end
record.prepared = true
return true
end
local function ResetComplete()
local active, selected, focused =
SmartNavigation:GetActiveFrame(), SmartNavigation:GetCurrentButton(), Manager():GetActiveFrame()
local bindings, scrolling = SmartNavigation.bindingActive, SmartNavigation.rightStickScrolling
local count = Manager():GetShownFrameCount()
return Accessible(active)
and Accessible(selected)
and Accessible(focused)
and Accessible(bindings)
and Accessible(scrolling)
and Accessible(count)
and active == nil
and selected == nil
and focused == nil
and bindings == false
and scrolling == false
and count == 0
end
local function RemoveOrder(record)
for i = #state.order, 1, -1 do
if state.order[i] == record then
table.remove(state.order, i)
return
end
end
end
local function Retained(record)
local focused, active = Manager():GetActiveFrame(), SmartNavigation:GetActiveFrame()
if not Accessible(focused) or not Accessible(active) then
Fail("detach_focus_unreadable")
return true
end
if focused == record.frame or active == record.frame then
Log("retained_focus", record.role)
Fail("detach_retained_focus_" .. record.role)
return true
end
return false
end
local function Detach(record)
if state.failed or DismissRestricted() or not Owned(record.frame) or not AncestorsVisible(record) then
return false
end
if record.modeReset and not ResetComplete() then
Fail("mode_reset_changed_before_hide")
return false
end
if record.attempted then
if record.detachTried then
Fail("repeated_detach")
return false
end
record.detachTried = true
local ok, removed = Native("FrameHidden", Manager(), record.frame)
if not ok then
return false
end
if removed ~= true then
Fail("detach_unverified")
return false
end
if Retained(record) then
return false
end
record.attempted, record.registered = false, false
end
-- Recheck after every native callback: it can hide UIParent synchronously.
if state.failed or DismissRestricted() or not AncestorsVisible(record) then
return false
end
if not FooterHide(record) or not AncestorsVisible(record) or Retained(record) then
return false
end
if record.hideRequested then
record.hiding = true
local ok = pcall(record.hide, record.frame)
record.hiding = false
if not ok then
Fail("owned_hide")
return false
end
end
if state.failed then
return false
end
RemoveOrder(record)
local complete = record.complete
record.complete, record.retiring, record.hideRequested, record.modeReset = nil, nil, nil, nil
record.parent, record.generation = nil, record.generation + 1
Log("detached", record.role)
if complete then
Call(complete, record.frame)
end
Notify()
return not state.failed
end
Retire = function(record, hide, complete)
if state.failed then
return false
end
if complete then
record.complete = complete
end
record.retiring, record.hideRequested = true, record.hideRequested or hide
record.generation = record.generation + 1
-- Retire descendants first; independent windows retain native focus ordering.
for _, child in ipairs(state.order) do
local parent = child.parent
while parent do
if parent == record.frame then
child.retiring, child.hideRequested = true, true
child.generation = child.generation + 1
break
end
parent = records[parent] and records[parent].parent
end
end
Schedule()
return true
end
Reconcile = function()
if state.reconciling or state.failed or DismissRestricted() or not Capable() or not Visible() then
return
end
state.reconciling = true
local ok = pcall(function()
while true do
local nextRecord
for i = #state.order, 1, -1 do
if state.order[i].retiring then
nextRecord = state.order[i]
break
end
end
if not nextRecord or not Detach(nextRecord) then
break
end
end
if not Restricted() and state.worldReady and not state.failed and QT.TryInstallDefaultGamepadLauncher
and QT:IsDefaultGamepadLauncherPending() then
QT:TryInstallDefaultGamepadLauncher()
end
end)
state.reconciling = nil
if not ok then
Fail("reconcile")
end
end
Schedule = function()
if state.failed or not Methods(C_Timer, { "After" }) then
return
end
if state.scheduled then
return
end
local token = { generation = state.generation }
state.scheduled = token
C_Timer.After(0, function()
if state.scheduled ~= token then
return
end
state.scheduled = nil
if state.failed or token.generation ~= state.generation then
return
end
local ok = pcall(Reconcile)
if not ok then
Fail("reconcile")
end
end)
end
local function InstallOwnedHooks(record)
if record.hooked then
return
end
record.hooked = true
local frame = record.frame
-- Only explicitly configured QT frames get this interception. Library-owned
-- QT close buttons and Escape paths also must unregister before C Hide.
frame.Hide = function(region)
if rawget(QT, "isRunningTests") == true then
return false
end
if record.hiding then
return record.hide(region)
end
local handled, done = QT:HideGamepadWindow(region)
if handled then
return done
end
return record.hide(region)
end
frame.SmartNavigationCloseHandler = function()
local closeSnapshot = RememberCombatClose(record, "back")
if closeSnapshot then closeSnapshot.outcome = "not_current" end
if state.failed or record.retiring then
if closeSnapshot then closeSnapshot.outcome = state.failed and "failed" or "already_pending" end
return true
end
if Restricted() then
-- Back remains dismissal-only in combat. Read native focus directly;
-- the normal action predicate deliberately rejects combat. The hide
-- path queues cleanup if the client currently forbids detachment.
if Capable() and record.registered and Owned(frame) and frame:IsVisible() then
local focused, managed, active = Manager().isUIFocused, Manager():GetActiveFrame(), SmartNavigation:GetActiveFrame()
if Accessible(focused) and focused == true and Accessible(managed) and managed == frame
and Accessible(active) and active == frame then
QT:DismissManagedWindow(frame, "gamepad_back")
end
end
return true
end
if not QT:IsGamepadActionCurrent(frame) then
return true
end
if record.policy.close then
Call(record.policy.close, frame, "gamepad_back")
else
QT:DismissManagedWindow(frame, "gamepad_back")
end
return true
end
frame.FocusGamepad = function()
if state.failed then
return
end
if record.footer then
if record.footer:IsBindingGroupRequired() or record.footer.bindings then
Fail("footer_bindings")
return
end
if not Call(record.footer.ShowAndActivateBindings, record.footer) then
return
end
end
Call(record.policy.onFocus, frame, true)
Notify()
end
frame.UnfocusGamepad = function()
if state.failed then
return
end
if not FooterHide(record) then
return
end
Call(record.policy.onFocus, frame, false)
Notify()
end
frame:HookScript("OnHide", function()
if state.failed or record.hiding or not record.attempted then
return
end
if not AncestorsVisible(record) then
Retire(record, true)
return
end
-- Direct hiding bypasses authored unregister. Do not compound it by retry.
Fail("unexpected_hide_" .. record.role)
end)
frame:HookScript("OnShow", function()
if record.retiring then
Schedule()
end -- Never register on inherited OnShow.
end)
end
function QT:ConfigureGamepadWindow(frame, policy)
if self ~= QT or rawget(QT, "isRunningTests") == true then
return false
end
if not Owned(frame) or type(policy) ~= "table" then
return false
end
local record = records[frame]
if record then
record.policy = policy
else
record = {
frame = frame,
policy = policy,
generation = 0,
hide = frame.Hide,
role = tostring(policy.role or "window"):gsub("[^%w_]", ""):sub(1, 48),
}
records[frame] = record
end
-- Unsupported clients keep only QT policy data: no frame hooks or method
-- interception, native callbacks, footers or navigation registration.
if state.enabled and Capable() then
InstallOwnedHooks(record)
end
return true
end
function QT:PresentGamepadWindow(frame, intent)
if self ~= QT or rawget(QT, "isRunningTests") == true then
return false
end
local record = records[frame]
local explicit = intent == "user" or (type(intent) == "table" and intent.userInitiated == true)
local automatic = intent == "automatic" and record and record.policy.allowAutomaticFocus == true
if
(not explicit and not automatic)
or not record
or state.failed
or not state.enabled
or Restricted()
or not self:IsNativeGamepadMode()
or not Capable()
or not Visible()
or not Owned(frame)
or not frame:IsVisible()
or record.retiring
then
return false
end
self:InitializeGamepadIntegration()
if not state.attached then
return false
end
InstallOwnedHooks(record)
if not Setup(record) then
return false
end
if record.registered then
return self:RefreshGamepadNavigation(frame)
end
local parent = type(intent) == "table" and intent.parent or nil
if parent and not Verify(parent, nil, "popup_parent") then
return false
end
if not parent and record.policy.kind == "popup" then
parent = ReadFrame(Manager():GetActiveFrame())
end
record.parent, record.detachTried, record.modeReset = parent, nil, nil
record.generation = record.generation + 1
record.attempted, record.hadNative = true, true
state.order[#state.order + 1] = record
local ok, added
if record.policy.kind == "popup" or parent then
ok, added = Native("HandlePopupShown", Manager(), frame)
else
ok, added = Native("FrameShown", Manager(), frame, false, true)
end
if not ok then
return false
end
if record.policy.kind ~= "popup" and not parent and added ~= true then
Fail("registration_rejected")
return false
end
if not Verify(frame, nil, record.role) then
return false
end
record.registered = true
local target = record.policy.defaultFocus
if type(target) == "function" then
local got, value = pcall(target, frame)
if not got then
Fail("default_focus")
return false
end
target = value
end
if target then
if not Owned(target) then
Fail("default_focus_unavailable")
return false
end
if
not Native("SetTargetButtonForFrame", SmartNavigation, frame, target)
or not Verify(frame, target, record.role .. "_target")
then
return false
end
end
local scrollFrame = record.policy.scrollFrame
if type(scrollFrame) == "function" then
local got, value = pcall(scrollFrame, frame)
if not got then
Fail("scroll_target")
return false
end
scrollFrame = value
end
if scrollFrame then
if not Owned(scrollFrame) then
Fail("scroll_target_unavailable")
return false
end
if not Native("SetScrollFrameForFrame", SmartNavigation, frame, scrollFrame) then
return false
end
end
Log("registered", record.role)
Notify()
return not state.failed
end
function QT:RefreshGamepadNavigation(frame, target)
if self ~= QT or rawget(QT, "isRunningTests") == true then
return false
end
local record = records[frame]
if
not record
or not record.registered
or record.retiring
or state.failed
or not state.enabled
or Restricted()
or not self:IsNativeGamepadMode()
or not Visible()
or not Owned(frame)
or not frame:IsVisible()
then
return false
end
if not Native("RefreshButtonGroups", SmartNavigation, frame) then
return false
end
if target then
if not Owned(target) or not self:IsGamepadWindowFocused(frame) then
return false
end
if
not Native("SetTargetButtonForFrame", SmartNavigation, frame, target)
or not Verify(frame, target, "refresh")
then
return false
end
end
return true
end
-- A hidden selected child can hide the cursor through its anchor and thereby
-- execute native OnHide outside the authored unregister wrapper. Park selection
-- before callers change the child's visibility. SetTargetButtonForFrame calls
-- SelectButton -> UpdateCursorPosition synchronously in the audited source.
local function Within(frame, ancestor)
for _ = 1, 64 do
if not frame then
return false
end
if not Accessible(frame) or not ReadFrame(frame) then
return nil
end
if frame == ancestor then
return true
end
if not Methods(frame, { "GetParent" }) then
return nil
end
local ok, parent = pcall(frame.GetParent, frame)
if not ok or not Accessible(parent) then
return nil
end
frame = parent
end
return nil
end
local function NeverRegisteredWindow(control)
local frame = control
for _ = 1, 64 do
if not frame or frame == UIParent then
return false
end
if not ReadFrame(frame) then
return false
end
local record = records[frame]
if record then
return not record.hadNative and not record.attempted and not record.retiring
end
if not Methods(frame, { "GetParent" }) then
return false
end
local ok, parent = pcall(frame.GetParent, frame)
if not ok or not Accessible(parent) then
return false
end
frame = parent
end
return false
end
function QT:PrepareGamepadControlHide(control, fallback)
-- This is a pure permission result for isolated UI fixtures, not a native
-- entrypoint for their mock regions or the synchronous /qt test singleton.
if self ~= QT or rawget(QT, "isRunningTests") == true then
return true
end
if not Owned(control) or state.failed then
return false
end
if not self:IsNativeGamepadMode() then
return true
end
if not Capable() then
return NeverRegisteredWindow(control)
end
if Restricted() or not Visible() then
return false
end
local readVisibility, controlVisible = pcall(function() return control:IsVisible() end)
if not readVisibility or not Accessible(controlVisible) or type(controlVisible) ~= "boolean" then
return false
end
-- Fresh and closed menu trees are already invisible. Hiding their children
-- cannot trigger another visibility change beneath the native cursor. Map
-- canvas focus legitimately leaves navigation shown with no selected button.
if controlVisible == false then
return true
end
if not SmartNavigation:IsShown() or not SmartNavigation:IsVisible() then
return true
end
local selected = SmartNavigation:GetCurrentButton()
local active, manager = SmartNavigation:GetActiveFrame(), Manager():GetActiveFrame()
if not Accessible(selected) or not Accessible(active) or not Accessible(manager) then
return false
end
if not selected then
return false
end -- A nil selection does not clear the old native anchor.
local affected = Within(selected, control)