| Ticket UUID: | 85c8397412fbb9ffea96838c1df6739d367969cb | |||
| Title: | Wrapper proc for exerting timing control over pointer warping | |||
| Type: | Patch | Version: | 8.6.11 | |
| Submitter: | erikleunissen | Created on: | 2021-01-09 23:39:01 | |
| Subsystem: | 69. Events | Assigned To: | fvogel | |
| Priority: | 5 Medium | Severity: | Minor | |
| Status: | Closed | Last Modified: | 2021-04-25 17:04:17 | |
| Resolution: | Accepted | Closed By: | fvogel | |
| Closed on: | 2021-04-25 17:04:17 | |||
| Description: |
This patch proposal is targeted at those tests in the Tk test suite that warp the mouse pointer. Quite some Tk test scripts warp the mouse pointer, even if the target of the test is not related to pointer warping per se. Since a few years (lets say roughly after release of Tk 8.6.9) pointer warping in test scripts has become subject to increasingly specific control of timing aspects[*]. The reasons for the choice of the methods[**] for exerting this timing control are rather intricate. They concern aspects of (regular) scheduling and the handling of race conditions. Moreover, the choice of methods varies with operating system and Tk version, and therefore they are different for the development branches 8.6 vs trunk/8.7 in Fossil. [*] for now: the SendInput() issue on MS Windows and the synchronous or asynchronous warping strategy. [**] using calls to [update] or [update idletasks], and [after 50]. Furthermore, it is awkward to add effective explanation to the timimng control statements inside the individual tests. Elaborate explanations distract from the main logic of the test, whereas single line comments cannot be more than hints. This combination of intricacy, code variability, and little opportunity for thorough explanation makes that the control statements inside individual tests are hard to comprehend for those unfamiliar with the specific aspects of timing. And even if you are familiar, there is the risk of making mistakes caused by oversight of an essential aspect in this fragile area. In short: it doesn't make for easy maintainance and overview. Therefore, this patch proposal proposes to introduce a wrapper proc to collect and unify the details of the timing control in one central place, instead of letting them recur in their varying forms in individual tests. Advantages: - avoiding the burden of repetition; any future changes to the method of timimng control can be done in one place instead of inside each individual test (e.g. making waiting time adjustable). - similarly, it provides a single place to add explanation about the reasons / conditions that underly the intricacies in more detail. - handles conditions that underly code variability explicitly, and also in one place (conditions w.r.t. OS and Tk release). Summarized: the main reason is code maintainability in several forms. Implementation aspects ====================== a. If this is deemed a good idea, then there still remains the question exactly where to define this wrapper proc: - in a single file that is common to some or all test scripts (if such a thing exists, I didn't see it). If this doesn't exist: - in each test script where where the issue is relevant (for now: bind.test and textTag.test). I do not have a good overview here. This is the main reason that this is not yet a ready-to-apply patch, but a patch proposal. b. W.r.t specific code explanation regarding the SendInput() issue: I took the liberty to write a new text explaining the matter. This text is based on: - the explanation from ticket [69b48f427e] (specifically in the comment by Francois Vogel dated 2019-10-27 14:24:26) - an explicit inspection of the program flow at those places in the Tk code where the calls to SendInput() are made, and - the documentation at: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendinput https://docs.microsoft.com/en-us/windows/win32/inputdev/wm-mousemove , all this in order to really understand the callback mechanism from which the unexpected delays originate. Regardless, an explicit review of this explanatory text is advised (indicated by the marker *** PLEASE REVIEW *** in the proc comment below). Sample implementation ===================== # Record whether we need to account for pointer warping at idle time: # # - In Tk releases 8.6 and older, pointer warping is scheduled for execution at idle time. # - In release 8.7 and newer this happens synchronously. # set ::idle_pointer_warping [expr {[package vcompare [package present Tk] 8.7] >= 0?0:1}] # controlPointerWarpTiming -- # # Ensures proper timing of the following details of pointer warping: # # a. Allow pointer warping to happen if it was scheduled for execution at idle time. # # b. Work around an awkwardness of the SendInput() function of the Win32 API. # Please refer to Tk ticket [69b48f427e] for the history, specifically the comment on 2019-10-27 14:24:26. An outline follows: # # When calling [event generate $w $event -warp 1 ...], the following sequence occurs: ( *** PLEASE REVIEW *** ) # - At some point in the processing of this command, either via a synchronous execution path, # or asynchronously at idle time, Tk calls the OS function SendInput() to carry out the mouse cursor motion. # - Tk has registered a callback function[*] with the OS, for the OS to call in order to notify Tk when the mouse move is completed. # - The notification from the OS usually arrives very fast and Tk doesn't wait for it (assuming arrival is fast enough # to suit most use cases) but continues processing. # - A problem arises if Tk performs some processing, immediately following up on [event generate $w $event -warp 1 ...], # that relies on the mouse pointer having actually moved. If that happens just before the notification from the OS has been received, # Tk will still be using the old info (e.g. mouse coordinates not yet updated). Hickup, choke etc ... ! # # [*] the callback function is TkWinChildProc() # # This timing issue can be addressed by putting the Tk process on hold (do nothing at all) for a somewhat extended amount of time, # while letting the OS complete its job in the meantime. This is exactly what is accomplished by calling [after ms]. # proc controlPointerWarpTiming {script {duration 50}} { uplevel 1 $script if {$::idle_pointer_warping} { update idletasks ;# see a. above } if {($::tcl_platform(os) eq "Windows NT") && ($::tcl_platform(osVersion) eq "10.0")} { after $duration ;# see b. above } } Example usage in individual tests ================================= controlPointerWarpTiming { event generate .top <Motion> -x 20 -y 20 -warp 1 } instead of: event generate .top <Motion> -x 20 -y 20 -warp 1 update idletasks ; # DoWarp is an idle callback after 50 ; # Win specific - wait for SendInput to be executed and the variants thereof. Notes aside and indirectly related issues that may affect the proposal ====================================================================== a. As I understand it, the TkSetCursorPos() -> SendInput() route is a workaround for a bug that is specific for Win10. Is that correct? If so, why is it that this workaround has been implemented for all Windows OS versions, instead of conditionally for Win10 (e.g. using an #ifdef preprocessor directive that is specific for Win 10) ? b. Why does TkSetCursorPos() not check the return value of SendInput() ? -- end -- | |||
| User Comments: |
fvogel added on 2021-04-25 17:04:17:
Merged branch patch-85c8397412-86 into core-8-6-branch, and branch patch-85c8397412 into trunk. Closing now, thanks! jan.nijtmans added on 2021-03-13 19:44:26: LGTM fvogel added on 2021-03-13 18:45:16:
> Next steps for me: > > 1. Fix c. and d. > 2. Merge the fixes into patch-85c8397412-86 and patch-85c8397412, > confirm no new failures with all three platforms. > 3. Merge patch-85c8397412-86 into core-8-6-branch and patch-85c8397412 > into trunk, and close the present ticket. 1. is now done. About 2.: On macOS I see no failure with either bugfix branch, for On Linux Debian 10, running the same four test files I see a few failures of event-9 in 100 runs, with both bugfix branches. This is also happening in core-8-6-branch and trunk, and is analyzed in [9e1312f32c]. On Windows 10, running the same four test files with branch patch-85c8397412-86, I see 3 failures of textTag-16.2 in 100 runs. With branch patch-85c8397412 I couldn't see a failure. I'm also dealing with this in [9e1312f32c] (even if this ticket is not a place dedicated to this failure). Conclusion: I think we're ready for item 3. above, i.e. merge the bugfix branches and close this ticket. Should anyone have a different opinion, please raise your hand with a few days. fvogel added on 2021-03-09 21:44:23: > c. On Linux Debian 10, bind-34.1 fails sometimes, indicating a race > condition. It turns out that this also happens in core-8-6-branch [...] > (not due to the present patch). This is now dealt with in ticket [2374c602bf]. fvogel added on 2021-02-28 09:33:35:
> d. On macOS, textTag-18.1 always fails. > This also happens in trunk and in core-8-6-branch I have found the cause for this issue. Test event-9 as it was written in the frame of [9e1312f32c] introduced a coupling with other tests, here with textTag-18.1. I have fixed this, see [3d9e6f5e], and have merged the fix into core-8-6-branch and trunk, and then in our bugfix branches patch-85c8397412-86 and with patch-85c8397412. Final note on this: I thought the problem with textTag-18.1 was on macOS only but that was due to my testing scheme. On Windows I'm running the full test suite, which must clear the coupling event-9/textTag-18.1 in some test file run between the two, on Linux I was testing each event.test and textTag.test file separately (therefore no coupling), and on the mac I was running fvogel added on 2021-02-27 11:09:40: Thanks to both for your comments. > 1) I think that "after $duration" is necessary on all Win32 platforms, > not only Windows 10. Tcl doesn't have a check for Windows 10 in its > warp implementation either. It's true that the warp implementation on Windows does not have a check on the OS version. I have changed the warp implementation from SetCursorPos() to SendInput() due to a specific breakage of SendInput starting at a specific version of Windows 10, see the comment here in the code. I didn't observe the need for an "after $duration" for all Windows versions, but it does not harm, so fine with me. > Decoupling of [controlPointerWarpTiming] and [event generate ... -warp 1] I also agree. I have implemented both changes above in [ee33dc3968] (in patch-85c8397412-86) and [33dc4adb] (in patch-85c8397412). > Will retest on all platforms and with both patch branches. Here are my test results, obtained just before the two above commits, i.e. at [382d6ba0] (in patch-85c8397412-86) and [24fd2f0b] (in patch-85c8397412). All tests are done with the test files changed by the patch. a. Results are the same with patch-85c8397412-86 and with patch-85c8397412, for all the three platforms (macOS, Linux Debian 10 with KDE/Plasma, Windows10). b. There is no failure at all on Windows. c. On Linux Debian 10, bind-34.1 fails sometimes, indicating a race condition. It turns out that this also happens in core-8-6-branch (a few failures in 50 runs) but perhaps not in trunk (it didn't happen once in 50 runs). This is something that needs to be fixed but since it happens outside of our bugfix branches it does not seem to be due to the present patch, at least for patch-85c8397412-86. Perhaps the patch in patch-85c8397412 has an impact but I think it changes the timings, simply making the failure to happen more often than once in 50 runs (the issue is certainly also in trunk, I just didn't manage to trigger it in trunk but I could inpatch-85c8397412 because of different timings). So basically: sporadic failure of bind-34.1 is not due to the present patch. d. On macOS, textTag-18.1 always fails. This also happens in trunk and in core-8-6-branch and is therefore not due to the present patch. Bisection (in trunk for example) shows that the culprit is check-in [94e45daa], i.e. my fix for [9e1312f32c] (Delivery of <Enter> event to master after destruction of slave widget). I will investigate that (I'm wondering how I didn't see this before merging the fix). In any case, again not something due to the present patch. General conclusion: I think the patch is fine! Next steps for me:
anonymous added on 2021-02-26 22:37:18: Commenting on remark 2) by Jan: Decoupling of [controlPointerWarpTiming] and [event generate ... -warp 1] ========================================================================= I agree that Jan's proposition improves readability when applying the proc. Using the script as an argument was based on the idea (wrong logic though) that this kind of tight coupling would prevent pointer warp timing control from being forgotten. It doesn't. Also, in the case that pointer warp timing control is being forgotten it doesn't matter whether [controlPointerWarpTiming] is missing as a wrapper that includes [event generate -warp 1] or as a follow-up command to [event generate]. So I'm in favor of removing the script argument and use [controlPointerWarpTiming] as a follow-up command as suggested. (Erik logged in as anonymous) jan.nijtmans added on 2021-02-26 07:50:47: Looking at all this, I have two remarks. 1) I think that "after $duration" is necessary on all Win32 platforms, not only Windows 10. Tcl doesn't have a check for Windows 10 in its warp implementation either. 2) In stead of
controlPointerWarpTiming {event generate $w <Button-1> -warp 1} 50
It would be more readable to be able to do:
event generate .top.mb <Button-1> -warp 1
controlPointerWarpTiming 50
Just remove the first line "uplevel 1 $script" and the "script" parameter from the function to get to that.
fvogel added on 2021-02-14 16:13:19: Thanks. I have implemented the checks differently (taking inspiration at how it's done elsewhere in Tk). See [a87809c0]. Will retest on all platforms and with both patch branches. erikleunissen added on 2021-02-14 07:59:00: With of course:
($::tcl_platform(osVersion) >= 10.0)
erikleunissen added on 2021-02-14 07:47:16: In fact the latter proposed simple variant obviates the need for a separate namespace variable alltogether:
proc controlPointerWarpTiming {script {duration 50}} {
uplevel 1 $script
if {$::tk_version < 8.7} {
update idletasks ;# see a. above
}
if {($::tcl_platform(os) eq "Windows NT") && ($::tcl_platform(osVersion) eq "10.0")} {
after $duration ;# see b. above
}
}
erikleunissen added on 2021-02-13 21:41:15: I meant: Maybe that also explains the errors that you observed *in trunk*? erikleunissen added on 2021-02-13 21:20:10: Bummer!
There is a problem with alpha (and beta) version nrs in:
variable idle_pointer_warping [expr {[package vcompare [package present Tk] 8.7] >= 0?0:1}]
% package vcompare 8.7a4 8.7
-1
% package vcompare 8.7b4 8.7
-1
So in the context of this patch [package vcompare] treats 8.7a4 like it does 8.6.11. Therefore, in trunk [controlPointerWarpTiming] does not skip [update idletasks] where it should.
Maybe that also explains the errors that you observed?
The problem can be remedied by using instead:
variable idle_pointer_warping [expr {[package vcompare [package present Tk] 8.7a0] >= 0?0:1}]
or, simpler:
variable idle_pointer_warping [expr {$::tk_version < 8.7}]
erikleunissen added on 2021-02-13 14:13:06: Regarding > So it seems we're seeing different results on similar OSes! How strange! Yes weird. And now I run the test again, and they fail here too (on Linux, both patch-branches. I didn't care about Windows anymore). Please disregard the matter. Sorry for the noise. fvogel added on 2021-02-13 13:19:50: More test results: - I have discovered a problem in one of my commits (this affected all platforms) and have fixed it, see [be2b4b85] - With patch-85c8397412-86 (8.6-like) and also with patch-85c8397412 (8.7-like), bind-34.1 may fail sometimes (once in 5 to 10 runs) on Linux Debian 10 (but apparently not on Windows since it didn't fail event once in 20 runs). I'll need to investigate this race condition on Linux. It may or may not be linked to the present ticket, I don't know yet. - With patch-85c8397412-86 (8.6-like), textTag-18.1 fails on the Mac (10.15.7 - Catalina). All other affected tests pass (menu-38.1 is still skipped, this is dealt with in [d50f63a0e]). I didn't check yet whether this test passes in core-8-6-branch or not. - With patch-85c8397412 (8.7-like), both bind-36.1 and textTag-18.1 fail on the Mac (10.15.7 - Catalina). All other affected tests pass (menu-38.1 is still skipped, this is dealt with in [d50f63a0e]). I didn't check yet whether these tests pass in trunk or not. fvogel added on 2021-02-13 12:51:16:
> A. I missed one outright [update] that maybe should also be replaced/removed after all. > In event-9 [...] > if I remove this [update] and let [controlPointerWarpTiming] do just its [update idletasks] > with core-8.6 (idle pointer warping), the test PASSES on Linux and Windows 7 If I do this, event-9 fails (everytime): - on both Win10 and Linux Debian 10 - with either patch-85c8397412 (8.7-like) or patch-85c8397412-86 (8.6-like). So it seems we're seeing different results on similar OSes! How strange! > B. Regarding forward merging Opinions differ here. People are not all doing the same way. I tend to work in a single branch, off core-8-6-branch. Then when the feature is ready, merge into core-8-6-branch, and then merge into trunk. Note there is no branch off trunk in this process at any time. DOing it the other way is not merging but cherry-picking followed by merge marks. I tend to find this less natural. Just my opinion, I don't think it's worth to start a debate on this in the present ticket. > C. Regarding tcltest::configure -loadfile common.tcl with common.tcl sourcing other files Why not. I won't oppose, and might even give this a try. Not right now though. erikleunissen added on 2021-02-12 20:02:30: Thanks for handling all of these integration steps and checks with two patch branches. It's getting shape nicely, and promising given your checks.
There are a few loose ends that I'd like to tie up:
A. I missed one outright [update] that maybe should also be replaced/removed after all.
In event-9 we currently have:
controlPointerWarpTiming {event generate .top.f <Motion> -warp 1 -x 25 -y 25}
update ; # idletasks not enough
My patch didn't touch this [update] statement because of what the comment says.
However, if I remove this [update] and let [controlPointerWarpTiming] do just its [update idletasks] with core-8.6 (idle pointer warping), the test PASSES on Linux and Windows 7.
Therefore, I can't reproduce what the comment says. Could you please double-check to determine whether this [update] really cannot be removed (from both patch branches)?
B. Regarding:
> Your patch deals with both 8.6 and 8.7, therefore I think it should be
> applied first to core-8-6-branch and then merged forward.
The issue did cross my mind when I ceated the patch. This is just my inexperience with multi-branch development. Apparently I didn't make the most appropriate choice. I want to learn. My logic was:
- from the point of view of merging conflicts it doesn't matter which direction you take (8.6 => trunk) or (trunk => 8.6). Merging conflicts will occur in both directions.
- trunk is targeted at a further future and therefore more generically appropriate for a patch that is also meant to be generic.
Apparently my logic isn't right or there are other considerations. I'd be very interested to learn why the forward direction (8.6 => trunk) is better, so I'll know next time.
C. Regarding:
> I would expect it would be possible to add a new file specifically
> dedicated to helper procs, say helpers.tcl, and let it read by the test
> suite, in the same way constraints.tcl is read. However, from a quick
> glance at the tcltest man page there does not seem to be a way to specify
> more than one file in the -loadfile argument of tcltest::configure.
OK. Is it best to leave this as it is? Or is it possible to do the split one step later like:
a. tcltest::configure -loadfile common.tcl
b. common.tcl in turn sources any amount of subordinate files:
- source constraints.tcl
- source helpers.tcl
- source whatever the future brings ...
fvogel added on 2021-02-11 21:41:11:
>> O2. [after 50] >> [controlPointerWarpTiming] performs [after ms] only if the OS is (exactly) Windows 10 > I think we should extend the condition. Now done (in both patch branches). fvogel added on 2021-02-11 21:31:24: I have derived the corresponding patch for 8.6, see branch patch-85c8397412-86. Test results: - Win10: all affected tests PASSED, menu-38.1 SKIPPED. -Linux Debian 10: all affected tests PASSED, menu-38.1 SKIPPED. fvogel added on 2021-02-11 21:10:38: > B. menu-38.1 > ============ > This test has a constraint "userInteraction". On this specific point, please follow-up to [d50f63a0e1]. fvogel added on 2021-02-11 21:04:51: I have applied your patch to a test branch patch-85c8397412. Since you wrote this patch against trunk I created the patch branch off trunk. however I has reservations about this. Your patch deals with both 8.6 and 8.7, therefore I think it should be applied first to core-8-6-branch and then merged forward. Another possibility would have been to write two different patches, each of them targeted to either 8.6 or 8.7, and forget about the switch on $idle_pointer_warping. But that's not the logic of your patch. Therefore I should really apply it first to core-8-6-branch (well, to a patch branch off core-8-6-branch). I'll take care of this, and maintain two patch branches, which will also allow easy testing in the (8.6) idle warping environment or in the (8.7) synchronous warping environment. A patch for 8.6 is not just a cherrypick of your patch against trunk however (e.g. in addition 'update idletasks' should be removed from some tests since they should no longer be needed). I'll have to look at this in depth. > If the file "constraints.tcl" currently is the only file that all test files have in common [...] I would expect it would be possible to add a new file specifically dedicated to helper procs, say helpers.tcl, and let it read by the test suite, in the same way constraints.tcl is read. However, from a quick glance at the tcltest man page there does not seem to be a way to specify more than one file in the -loadfile argument of tcltest::configure. > O2. [after 50] > [controlPointerWarpTiming] performs [after ms] only if the OS is (exactly) Windows 10 I think we should extend the condition. There is no sign Microsoft thinks the Win10 behaviour is a bug at all, and if we limit to exactly Win 10 then these tests will suddenly start failing with Win 11. > (===> check ***). François, could you please review? > A. bind.test, initial code section This one is OK, this update is an overkill (the intent is to move the pointer away). > B. menu-38.1 > ============ This test has a constraint "userInteractive". You mean "userInteraction". See the tcltest man page for details. I have never understood why dgp constrained this test. The commit log says "Constrain test that demands user interaction", but I don't see this test needing user interaction. I was quite tempted to remove this constraint altogether, so I tried. And it hangs for me. Quick analysis: I think there is an interaction between two fixes (from tickets [fa32290898] and [59fccbfa27]). I'm convinced that menu-38.1 does not fail because it requires user interaction, but rather because of wrong fixes (that I made). I'll need to research this in another ticket. > C. textTag-18.1 > =============== We don't need controlPointerWarpTiming in these six calls here because we're not checking that the mouse pointer has moved but rather that the tag binding mechanism of the text widget correctly triggers. Test results on the systems I have access to, for the patch against trunk: - Win10: all affected tests PASSED, menu-38.1 SKIPPED. -Linux Debian 10: all affected tests PASSED, menu-38.1 SKIPPED. erikleunissen added on 2021-02-09 20:44:22: I developed a ready-to-apply patch against trunk (checkin tk-94e45daa) that:
- defines [controlPointerWarpTiming] in the file constraints.tcl. If the file "constraints.tcl" currently
is the only file that all test files have in common, than it seems to me that for now that is the most
suitable place for adding the definition of the wrapper proc to (see F. below for reservations).
- replaces various pointer warping commands + associated timing control statements in the files bind.test, event.test, menu.test, and textTag.test.
I tested the patch by running the relevant tests in the 4 mentioned test files:
- for both the patched and unpatched code. This is done to detect whether the patch makes a difference for the test result, not for correct PASSing of the tests per se.
- for Linux KDE/Plasma and Windows 7
Result: all affected tests PASSED, one SKIPPED (menu-38.1).
The patch replaces the various statements in the files bind.test, event.test, menu.test, and textTag.test that aim to control timing of pointer warping.
The patch ensures that method of timing control depends deterministically on OS and Tk version, and is executed in the same manner under the same circumstances.
It removes any unnecessary variability and any unnecessary invariability. This is described in O1, O2 or O3 below for each control statement.
O1. [update idletasks] (or possibly [update])
=============================================
[controlPointerWarpTiming] performs calls to [update idletasks] only for Tk8.6- where pointer warping is scheduled for execution at idle time.
Some tests in trunk currently use [update idletasks] (or [update]? See O3 below). These should be unnecessary since trunk corresponds to Tk version 8.7+ where pointer warping happens synchronously.
O2. [after 50]
==============
[controlPointerWarpTiming] performs [after ms] only if the OS is Windows 10. Maybe the relevant bug that [after 50] works around, will be fixed in a next Windows release. If so, then the patch is OK as it is now. However, if the bug persists, then the OS condition needs to be extended. Alternatively, we could make the condition more generic to affect all windows platforms (though useless [after 50] doesn't harm), but that is contrary to the specific nature/intention of [controlPointerWarpTiming].
Currently, control statements in trunk use [after 50] regardless OS.
O3. [update] vs. [update idletasks]
===================================
Some tests use [update] after a call to [event generate -warp 1 ...]. If this was intended to let idle pointer warping happen, it is overkill: [update idletasks] should suffice.
[controlPointerWarpTiming] uses [update idletasks], not [update] for that purpose. This is a moot point for trunk, however, because trunk deals with Tk8.7+ and under that circumstance, [controlPointerWarpTiming] skips these statements.
I am unsure about these calls to [update]. There may have been reasons for using [update] instead of [update idletasks] which I don't know.
In these cases, the replacement by [controlPointerWarpTiming] may be wrong. This needs to be checked explicitly, even if the tests PASS for trunk.
Below, I indicate the tests where an explicit check is needed with (===> check ***). François, could you please review?
A. bind.test, initial code section
==================================
--- tk-trunk.old/tests/bind.test 2021-02-07 15:18:33.000000000 +0100
+++ tk-trunk.new/tests/bind.test 2021-02-07 21:21:28.000000000 +0100
@@ -44,8 +44,7 @@
toplevel .top
wm geometry .top 50x50-50-50
update
-event generate .top <Button-1> -warp 1
-update
+controlPointerWarpTiming {event generate .top <Button-1> -warp 1}
destroy .top
test bind-1.1 {bind command} -body {
destroy .top
===> check *** O3.
B. menu-38.1
============
This test has a constraint "userInteractive". I didn't figure out how to comply with that constraint; even when sourcing the test file from an interactive tclsh this test is skipped.
The replacement with [controlPointerWarpTiming] has not been tested.
diff -ur tk-trunk.old/tests/menu.test tk-trunk.new/tests/menu.test
--- tk-trunk.old/tests/menu.test 2021-02-07 15:18:33.000000000 +0100
+++ tk-trunk.new/tests/menu.test 2021-02-07 21:26:12.000000000 +0100
@@ -3957,9 +3957,7 @@
pack .top.mb
update
# simulate mouse click on the menubutton, which posts its menu
- event generate .top.mb <Button-1> -warp 1
- update
- after 50
+ controlPointerWarpTiming {event generate .top.mb <Button-1> -warp 1}
event generate .top.mb <ButtonRelease-1>
update
# simulate mouse click on the menu again, i.e. without
===> check *** O3 and find a way to execute this test.
C. textTag-18.1
===============
There were no MSWindows specific [after] statements in the series of six successive cases of [event generate -warp 1].
I assumed that that is for a good reason, and the patch does not affect the body of this test. I am not entirely sure though.
*** ==> Please check the correctness of my assumption.
D. Testing on Win10
===================
I cannot do that, I don't have Win10 installed.
E. core-8.6 branch
==================
Can this be done by applying the currently proposed patch for trunk in a separate feature branch (for trunk), and subsequently open another feature branch for core-8.6, and merge/cherry pick from the branch for trunk? If all is proven well later, we could merge each feature branch into its corresponding main Tk development branch.
Or do I need to provide a separate patch against branch core-8.6?
F. The file "constraints.tcl"
=============================
1. This file holds test constraints as well as other utilities used by test files. It gives me concerns w.r.t structure and maintainability.
I believe it would be more logical to do one of the following:
- rename the file "constraints.tcl" to "common.tcl", and indicate in its contents separate sections for constraints and other utilities.
- separate out the non-constraint parts into a file "utilities.tcl"
2. Furthermore, I believe that the code could do with improvements w.r.t. structure / document lay out and explanatory comments in several places.
In its current state, it's not much more than a concatenation of various snippets of code in immediate succession (e.g. not even an extra empty line in between procs in the interest of document readability). This comes across as surficial hacks (I know they aren't) in a formal document.
Well that's separate from the issue of this ticket. But if you agree and it is feasible, I could look into this (another time as another ticket).
--
erikleunissen added on 2021-02-09 20:41:03: > Because the SendInput() thing also works for Windows OSes older than 10. > Ifdeff-ing would just clutter the code for no advantage. I understand / agree. >> Why does TkSetCursorPos() not check the return value of SendInput() ? > Because I didn't see what to do with this return value > ... > How would you use this return value? I didn't realize that a return value can be useless (under circumstances). I understand the choice. fvogel added on 2021-02-07 14:05:18: Thank you for this proposal. I concur regarding the good reasons you exhibit about why we should do this. Indeed, code maintenance would be easier. Let me try to answer your questions: > where to define this wrapper proc? I suggest tests/constraints.tcl . There are already a few procs defined there that are common to all test files. Have a look at how it is done, it is quite straightforward to add a new proc I think. > As I understand it, the TkSetCursorPos() -> SendInput() route is a workaround for a bug that is specific for Win10. Is that correct? Yes. Actually the change is from SendCursorPos() to SendInput(). See this comment in the code here. > If so, why is it that this workaround has been implemented for all Windows OS versions, instead of conditionally for Win10 > (e.g. using an #ifdef preprocessor directive that is specific for Win 10) ? Because the SendInput() thing also works for Windows OSes older than 10. Ifdeff-ing would just clutter the code for no advantage. > Why does TkSetCursorPos() not check the return value of SendInput() ? Because I didn't see what to do with this return value. The documentation says: The function returns the number of events that it successfully inserted into the keyboard or mouse input stream. If the function returns zero, the input was already blocked by another thread. To get extended error information, call GetLastError. How would you use this return value? erikleunissen added on 2021-01-11 18:05:56: Corrected explanation of the race condition once more. The corrected sample implementation has now been attached as a Tcl script sample_implementation.tcl (now version 1.1), to not overload this ticket with the details. erikleunissen added on 2021-01-11 12:35:22: Even better explanation to case b.
==================================
# controlPointerWarpTiming --
#
# Ensures proper timing of the following details of pointer warping:
#
# a. Allow pointer warping to happen if it was scheduled for execution at idle time.
#
# b. Prevent a race condition associated with OS notification of mouse motion.
#
# When calling [event generate $w $event -warp 1 ...], the following sequence occurs: ( *** PLEASE REVIEW *** )
# - At some point in the processing of this command, either via a synchronous execution path,
# or asynchronously at idle time, Tk calls an OS function[*] to carry out the mouse cursor motion.
# - Tk has previously registered a callback function[**] with the OS, for the OS to call in order to notify Tk when a mouse moves is completed.
# - Tk doesn't wait for the callback function to receive the notification from the OS, but continues processing.
# This suits most use cases because (usually) the notification comes quickly enough (range: a few ms?). However ...
# - A problem arises if Tk performs some processing, immediately following up on [event generate $w $event -warp 1 ...], and that
# processing relies on the mouse pointer having actually moved. If such processing happens just before the notification from the OS
# has been received, Tk will be using not yet updated info (e.g. mouse coordinates). Hickup, choke etc ... !
#
# [*] the function SendInput() of the Win32 API
# [**] the callback function is TkWinChildProc()
#
# This timing issue can be addressed by putting the Tk process on hold (do nothing at all) for a somewhat extended amount of time,
# while letting the OS complete its job in the meantime. This is exactly what is accomplished by calling [after ms].
#
# ----
# For the history of this issue pleasse refer to Tk ticket [69b48f427e], specifically the comment on 2019-10-27 14:24:26.
#
proc controlPointerWarpTiming {script {duration 50}} {
uplevel 1 $script
if {$::idle_pointer_warping} {
update idletasks ;# see a. above
}
if {($::tcl_platform(os) eq "Windows NT") && ($::tcl_platform(osVersion) eq "10.0")} {
after $duration ;# see b. above
}
}
| |||
Attachments:
- tk-94e45daa.patch [download] added by erikleunissen on 2021-02-09 20:45:17. [details]
- sample_implementation-1.1.tcl [download] added by erikleunissen on 2021-01-11 18:07:06. [details]
