Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 435799 - [GTK3] Display#getActiveShell() is null while context menu is shown
Summary: [GTK3] Display#getActiveShell() is null while context menu is shown
Status: VERIFIED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 4.4   Edit
Hardware: PC Linux
: P3 critical (vote)
Target Milestone: 4.4 RC4   Edit
Assignee: Arun Thondapu CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 435635 (view as bug list)
Depends on:
Blocks: 436635
  Show dependency tree
 
Reported: 2014-05-26 11:02 EDT by Arthur Daussy CLA
Modified: 2014-06-06 09:29 EDT (History)
9 users (show)

See Also:
daniel_megert: pmc_approved+
markus.kell.r: review+
Silenio_Quarti: review+


Attachments
NPE stack trace (2.64 KB, text/plain)
2014-05-26 11:02 EDT, Arthur Daussy CLA
no flags Details
Project to reproduce (5.12 KB, application/zip)
2014-05-26 11:03 EDT, Arthur Daussy CLA
no flags Details
MenuActiveShell.java - SWT Snippet (2.35 KB, text/plain)
2014-06-05 08:13 EDT, Markus Keller CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Arthur Daussy CLA 2014-05-26 11:02:53 EDT
Created attachment 243494 [details]
NPE stack trace

My environment:
 * Luna M7
 ** EMF Compare nightly
 * Linux Mint 16 (with Mate Desktop)


I'm working on EMF Compare project and at some point I have to get the active shell. To do so, I receive a SWT event (from the workbench) and I use event.display.getActiveShell(). However the activeShell is set to null. I have also tried with Display.getDefault().getActiveShell() but it is null too.

If I force the use of GTK2 (export SWT_GTK3=0) this display has a correct activeShell (!=null).

To reproduce:
 * Download EMF Compare sources (http://git.eclipse.org/c/emfcompare/org.eclipse.emf.compare.git)
 * Set the Luna target platform (available in org.eclipse.emf.compare-parent/targetPlatforms/luna-sdk.target)
 * Run a runtime platform
 * Compare the 3 models provided (see attachment).
 ** Select the three files (extlibraryLeft.ecore,extlibraryOrigin.ecore and extlibraryRight.ecore)
 ** Open the contextual menu (Compare With > Each Other)
 * Change the active group to default (http://www.eclipse.org/emf/compare/documentation/latest/user/user-guide.html#Grouping_Differences)

You will get an NPE (see attachment). This NPE comes from a display with an activeShell set to null. This does not happen on neither on Mac or Windows neither with a GTK2 Linux.
Comment 1 Arthur Daussy CLA 2014-05-26 11:03:43 EDT
Created attachment 243495 [details]
Project to reproduce
Comment 2 Arthur Daussy CLA 2014-05-26 11:11:30 EDT
*** Bug 435635 has been marked as a duplicate of this bug. ***
Comment 3 Marc-André Laperle CLA 2014-05-28 13:53:42 EDT
Hello Arthur. I doesn't look like it's guaranteed that there will be an active Shell (in focus) when this code is called. In fact, I've looked around the platform code and they pass around the IWorkbenchWindow which can be used to retrieve the Shell so I think that's the way to go. For example, see org.eclipse.ui.actions.ExportResourcesAction.ExportResourcesAction

As for the difference between GTK2 and GTK3, I have noticed that in GTK3, there is a FOCUS_OUT_EVENT which calls gtk_focus_out_event and nulls out the activeShell. To me, it doesn't look like this is a bug, it means that when the context menu is brought up, it gets focus and the main Shell loses it. Perhaps some SWT expert can clarify.
Comment 4 Arthur Daussy CLA 2014-06-02 03:13:57 EDT
Hello Mark,
 I agree that nothing tells us that the active shell should not be null. However I think eclipse is multi platform so I would have expected that the same code does the same way in GTK3 and in the other platforms (GTK2, Mac and Windows which are all the platform we have tested for the moment). I'm afraid that if this behavior stays the same in GTK3 a lot a of bugs will be raised in other projects too.
 If you are sure that this the expected behavior I would change this in my code however I'm not sure this is the correct solution.

Thanks for your help,

Regards,


Arthur
Comment 5 Dani Megert CLA 2014-06-05 06:04:59 EDT
This bug also breaks the 'Show In' context menu, see bug 436635.
Comment 6 Markus Keller CLA 2014-06-05 08:13:27 EDT
Created attachment 243989 [details]
MenuActiveShell.java - SWT Snippet

Here's an SWT snippet that reproduces the problem on GTK3, where it prints "menuShown: activeShell == null".

On GTK2 (env var. SWT_GTK3 = 0), it prints
"menuShown: activeShell == Shell {snippet.MenuActiveShell}"

The problem seems to be in Control#gtk_event_after(int, int) line 3134, where

	int /*LONG*/ grabHandle = OS.gtk_grab_get_current ();
	if (grabHandle != 0) {
		if (OS.G_OBJECT_TYPE (grabHandle) == OS.GTK_TYPE_MENU ()) {
			display.ignoreFocus = true;
			break;
		}
	}

has "grabHandle == 0" on GTK3 and hence it misses to set display.ignoreFocus.
Comment 7 Arun Thondapu CLA 2014-06-05 13:39:27 EDT
I have pushed a patch to gerrit - https://git.eclipse.org/r/#/c/28064/

The change is to not set activeShell to null in the Shell's FocusOut event handler when there is a context menu showing. I've tested with the snippet from comment 6 and with the context menus in Eclipse as well.
Comment 8 Markus Keller CLA 2014-06-05 15:01:24 EDT
(In reply to Arun Thondapu from comment #7)
> I have pushed a patch to gerrit - https://git.eclipse.org/r/#/c/28064/

+1 for RC4.

The patch works fine for the scenarios without detached views, but we still get FocusOut/In and activation events on GTK3 when the context menu is shown. On GTK2 and Windows, no such events are sent. This needs to be investigated for 4.5.

There are still problems with detached views on GTK, but I'll open a separate bug for that.
Comment 9 Arun Thondapu CLA 2014-06-05 15:51:21 EDT
(In reply to Arun Thondapu from comment #7)
> I have pushed a patch to gerrit - https://git.eclipse.org/r/#/c/28064/

Merged to master - http://git.eclipse.org/c/platform/eclipse.platform.swt.git/commit/?id=988bede29f39493a457063957c6a9c6fd48a2ddf
Comment 10 Sravan Kumar Lakkimsetti CLA 2014-06-06 02:54:55 EDT
I verified this defect in I20140605-1635

We are still getting the FocusIn and FocusOut evets in GTK3. These are not coming in GTK2.
Comment 11 Dani Megert CLA 2014-06-06 03:18:37 EDT
(In reply to Sravan Kumar Lakkimsetti from comment #10)
> I verified this defect in I20140605-1635
> 
> We are still getting the FocusIn and FocusOut evets in GTK3. These are not
> coming in GTK2.

This probably causes bug 436749.
Comment 12 Markus Keller CLA 2014-06-06 09:00:20 EDT
(In reply to Markus Keller from comment #8)
> There are still problems with detached views on GTK, but I'll open a
> separate bug for that.

Bug 436749.

(In reply to Dani Megert from comment #11)
> This probably causes bug 436749.
That's probably a different issue, see bug 436749 comment 2.

Verified in I20140605-1635 and opened bug 436841 for comment 8.
Comment 13 Arthur Daussy CLA 2014-06-06 09:29:24 EDT
I have verified with our workflow and everything is working just fine now.

Thanks a lot.