Tk Source Code

View Ticket
Login
Ticket UUID: c1c842ef779288b7f5185143a51cc0d861069206
Title: TIP #532 (new tkBind.c): wrong assumption about ButtonRelease to match Button presses?
Type: Bug Version: bug6e8afe516d, bug6e8afe516d-87
Submitter: fvogel Created on: 2019-06-16 20:38:46
Subsystem: 01. Bindings Assigned To: gcramer
Priority: 8 Severity: Critical
Status: Closed Last Modified: 2019-08-28 19:38:24
Resolution: Fixed Closed By: fvogel
    Closed on: 2019-08-28 19:38:24
Description:

The following code should trigger the binding and set variable x to ">>>>OK" but it doesn't:

    package require Tk

    set x >>>>BROKEN!

    toplevel .top
    update

    event generate .top <1>
     #  event generate .top <ButtonRelease-1>

    update
    destroy .top

    pack [canvas .c -height 200 -width 200] -fill both -expand yes
    update idletasks
    .c create rectangle 80 80 120 120 -fill blue -tags blue
    .c bind blue <1> {set x >>>>OK}

    event generate .c <1> -x 100 -y 100
    event generate .c <ButtonRelease-1> -x 100 -y 100
    set ::x

If the commented line (event generate .top <ButtonRelease-1>) is uncommented, then the script succeeds as expected (the binding is triggered and x is set to ">>>>OK").

It looks like the new implementation in tkBind.c, described in TIP #532, includes some code incorrectly assuming that button presses are matched with button releases.

There must be something wrong here, generation of the first events pair is for a toplevel (that in addition gets destroyed immediately after) and the second pair of events generation happens in a canvas item. There should be no interaction between these two.

User Comments: fvogel added on 2019-08-28 19:38:24:
TIP #532 implementation is now merged in core-8-6-branch and trunk.

As described in the TIP:

  *  core-8-6-branch has PREFER_MOST_SPECIALIZED_EVENT=0 and SUPPORT_ADDITIONAL_MOTION_SYNTAX=0

  *  trunk has PREFER_MOST_SPECIALIZED_EVENT=1 and SUPPORT_ADDITIONAL_MOTION_SYNTAX=1

The better documentation didn't came in, let's hope we can have it later.

fvogel added on 2019-08-13 18:52:57:

This issue was resolved and fixed by Marc Culler. The fix is [3b3c3b02] in the bug6e8afe516d branch, and [e4c41107] in the bug6e8afe516d-87 branch.

Now if we could get a bit of documentation regarding the algorithm used, that would be great!


marc_culler (claiming to be Marc Culler) added on 2019-08-02 19:42:56:

Here is a detailed step by step explanation of why the script in this ticket fails when the function UpdateButtonEventState is in use.

* When the event generated by event generate .top <1> is passed to UpdateButtonEventState the Button1 bit is set in the mouseButtonState field of the TkDisplay for the main window. Note that this bit would be cleared if the next line were uncommented.

* When the event generated by event generate .c <1> -x 100 -y 100 is passed to UpdateButtonEventState the Button1 bit in its xmouse.state field gets set. This event will be processed by CanvasBindProc since the event is sent to the canvas .c. First the xmouse.state of the event is copied to canvasPtr->state.

* At this point the currentItem for the canvas is set to NULL. CanvasBindProc calls PickCurrentItem to set the current item to the item containing the point (100, 100), namely the blue square. PickCurrentItem uses the following code to set a flag buttonDown which indicates that the event was generated while a button was down.

   4852     /*
   4853      * Check whether or not a button is down. If so, we'll log entry and exit
   4854      * into and out of the current item, but not entry into any other item.
   4855      * This implements a form of grabbing equivalent to what the X server does
   4856      * for windows.
   4857      */
   4858
   4859     buttonDown = canvasPtr->state & ALL_BUTTONS;

* Further along in PickCurrentItem, it is decided that since buttonDown is true, the current (NULL) item should be "grabbed" rather than setting the current item to be the blue square. Here is the code:

   4991     if ((canvasPtr->newCurrentPtr!=canvasPtr->currentItemPtr) && buttonDown) {
   4992         canvasPtr->flags |= LEFT_GRABBED_ITEM;
   4993         return;
   4994     }

* When control returns to CanvasBindProc, the currentItem is still NULL. That is interpreted to mean that the pointer location (100, 100) is not in in any item of the Canvas, so the event should just be ignored.


jan.nijtmans added on 2019-08-01 20:20:50:

See also the 8.3 release-notes: https://www.tcl.tk/software/tcltk/8.3.html: "warping via the event -warp option", that's what it was used for.

B.T.W.: I agree with removing it now, as it apparently creates more problems than what it solves. But - anyway - the "misterious reason" is clear now ;-)


jan.nijtmans added on 2019-08-01 20:14:18:
Yes, I remember. This can be traced back to commit [363f59223dec9d67]: The integration of the 'dash-patch' (as it was known at that time) in the Tk core. The function was needed to make XWarpPointer work, without the need to provide exact state information. So, the mouse pointer could be controlled by Tk for playing back a previously recorded track.

I don't think it's used that much ... It is considered bad habit to do that.   But - at least - this is the "mysterious reason" :-)

marc_culler (claiming to be Marc Culler) added on 2019-08-01 19:16:04:

I pushed a change which fixes this issue and makes all of the tests pass.

The problem was not in tkBind.c nor in tkCanvas.c.

The problem was, as suggested by Donal, that there was some code which was making the invalid assumption that between every two button presses there must be a button release. Worse, the code was assuming that a meaningful persistent button state could be maintained on a per display basis, even though press and release events can be generated completely arbitrarily.

The culprit was the function UpdateButtonEventState in tkEvent.c which not only attempted to maintain a button state for each display, but would also rewrite the state of a ButtonPress or ButtonRelease event with whatever garbage it had collected by inspecting all previous events. Since the CanvasBindProc uses the button state to decide how to handle press or release events, it is not surprising that it was behaving strangely.

The only call to UpdateButtonEventState was in Tk_HandleEvent, which was passing every event to UpdateButtonEventState so that the eventPtr->xbutton.state field of every ButtonPress or ButtonRelease event could get mangled. The fix is to just get rid of UpdateButtonEventState.

By the way, that function as well as the field mouseButtonState in TkDisplay were introduced, for some mysterious reason, in Tk 8.3.


marc_culler (claiming to be Marc Culler) added on 2019-08-01 12:19:24:

I characterized the issue incorrectly below. The NULL pointer which causes CanvasDoEvent to ignore the Button events is CanvPtr->currentItem which, according to tkCanvas.h, is supposed to point to the item which currently contains the mouse.

Somehow, when the bind-34.1 test generates a Button event located at the center of the blue square, the canvas is not noticing that its blue square item now contains the mouse. It is true that event generation is handled in tkBind.c but HandleGenerateEvent in the new tkBind.c appears to have been copied from the old one. And it does generate the correct Button events. So I am guessing that this is an old problem, having to do with how a Canvas widget handles Button events, that has somehow been unmasked by other changes in tkBind.c.


marc_culler (claiming to be Marc Culler) added on 2019-07-31 21:53:14:
The reason that bind-34.1 fails is that CanvasDoEvent ignores the mouse events
due to the fact that it thinks the canvas has no items.  You can demonstrate
this by running bind.test after applying the following patch:

Index: generic/tkCanvas.c
==================================================================
--- generic/tkCanvas.c
+++ generic/tkCanvas.c
@@ -5145,10 +5145,11 @@
     itemPtr = canvasPtr->currentItemPtr;
     if ((eventPtr->type == KeyPress) || (eventPtr->type == KeyRelease)) {
 	itemPtr = canvasPtr->textInfo.focusItemPtr;
     }
     if (itemPtr == NULL) {
+	fprintf(stderr, "CanvasDoEvent: NULL itemPtr.  Returning.\n");
 	return;
     }
 
 #ifdef USE_OLD_TAG_SEARCH
     /*

fvogel added on 2019-07-29 10:38:04:

One more datapoint:

With -singleproc 1 (as enforced by Tk when running the test suite), the canary test (bind-34.1) and canvas-13.1 both fail.

However, with -singleproc 0, only the canary test fails.

Doesn't this indicate that the canary test does not deal with the same issue as canvas-13.1? IOW, the canary test wouldn't be exactly equivalent to canvas-13.1. What do you think?


fvogel added on 2019-07-21 16:05:54:

The [69735dbff952956c|canary test] fails in branch bug6e8afe516d implementing TIP #532, but it passes in core-8-6-branch (after having added this canary to that branch).

We should see the same between trunk (pass) and branch bug6e8afe516d-87 (fail), although I didn't actually check.

So the canary test is correct in revealing the problem introduced by TIP #532.

What must be incorrect is my derivation (from the canary script) of the simpler test script that I'm providing in the present ticket. This simpler script is supposed to trigger the same issue as the canary, but it doesn't since it produces a ">>>>BROKEN!" result in both core-8-6-branch (old tkBind.c) and bug6e8afe516d (new tkBind.c from TIP #532). In any case this test script seems to unveil a so far unseen issue, which is apparently distinct from what the canary triggers.


fvogel added on 2019-07-15 19:41:43:
Apparently we have the same issue in core-8-6-branch, and in very old (say 8.5.16) Tk versions, that is with the old implementation of tkBind.c. What do you think of this?

dkf added on 2019-06-18 12:54:37:

This issue (events getting lost due to things happening on previously destroyed windows! We cannot prevent that from happening in user scripts) is blocking us from committing the #532 implementation to mainline branches.