helge17 / tuxguitar

Improve TuxGuitar and provide builds
Other
414 stars 36 forks source link

MacOS version does not open files when double clicked #99

Closed rhys073 closed 5 months ago

rhys073 commented 1 year ago

All files opens untitled.tg in Tuxguitar

When tuxguitar.sh is called from the Terminal with a relevant argument, TuxGuitar will open the file, however double-clicking a file (calling the .app) presumably does not pass through the /path/filename arguement to java

guiv42 commented 1 year ago

I can reproduce something similar on Linux with 1.6.0beta1. bisect leads me to 2cfb9c919119eedece46eb52d656c88000b4af66, but to be honest I do not really understand. Investigation in progress...

guiv42 commented 1 year ago

Progressing, it looks like an attempt to draw a marker in a not fully initialized track table.

If someone has access to a MacOS environment, I would appreciate the following tests to confirm my analysis:

  1. With 1.6.0beta1

    • create 2 .tg files, one with a marker in it, and the other one without any
    • open TG, make sure track table is displayed (see menu "View/Show track table")
    • close TG
    • double-click on the .tg file with no marker in it => it should open correctly
    • mask the track table, then close TG
    • double-click on the .tg file with a marker => it should open correctly
    • display the track table again, then close TG
    • double-click on the .tg file with a marker => there it should fail
  2. with the version in this branch There I have completely removed the feature by reverting 2cfb9c919119eedece46eb52d656c88000b4af66, this should completely solve the issue. Note: this version is for test only, not ready for a merge

helge17 commented 1 year ago

The two issues are not related, imho.

TuxGuitar on MacOS not opening the file selected in Finder

As far as I understand, the file associations and the startup script are defined in the Contents/Info.plist file:

<key>CFBundleExecutable</key>
<string>tuxguitar.sh</string>

tuxguitar.sh reads the file to open from "$@", the command arguments. Now if you add something like

echo "# TEST"         > /tmp/test
echo "First arg: $1" >> /tmp/test
echo "Alls args: $@" >> /tmp/test

to tuxguitar.sh and click on a TuxGuitar file in Finder, you can see in /tmp/test, that there are no arguments: Finder just doesn't pass any arguments to tuxguitar.sh, so TuxGuitar has no way of knowing that it should open a file. At the moment I have no idea where TuxGuitar can get this information from or how to start TuxGuitar properly.

TuxGuitar on Linux/Windows/MacOS/BSD hanging when starting with a file

I have tested with two files containing only one measure; one without marker, the other with one single marker. The problem does not seem to occur without markers, but also occurs quite rarely (10% of the time?) with the file with only one marker.

I have also tested the behavior with other files containing a complete song, and indeed it seems to be related to the markers: the more markers there are in the file, the more often TuxGuitar hangs.

When opening files containing many markes with 1.6.0beta1 (SWT, deb package), TuxGuitar hangs nearly on every start and shows the following error:

java.lang.IllegalArgumentException: Argument not valid
        at org.eclipse.swt.SWT.error(SWT.java:4692)
        at org.eclipse.swt.SWT.error(SWT.java:4626)
        at org.eclipse.swt.SWT.error(SWT.java:4597)
        at org.eclipse.swt.graphics.GC.drawImage(GC.java:838)
        at org.herac.tuxguitar.ui.swt.resource.SWTPainter.drawImage(SWTPainter.java:90)
        at org.herac.tuxguitar.app.view.component.table.TGTableHeaderMeasures.paintControl(TGTableHeaderMeasures.java:110)
        at org.herac.tuxguitar.app.view.util.TGBufferedPainterLocked.fillPaintBuffer(TGBufferedPainterLocked.java:50)
        at org.herac.tuxguitar.app.view.util.TGBufferedPainterLocked.paintBufferLocked(TGBufferedPainterLocked.java:59)
        at org.herac.tuxguitar.app.view.util.TGBufferedPainterListenerLocked.onPaint(TGBufferedPainterListenerLocked.java:14)
        at org.herac.tuxguitar.ui.event.UIPaintListenerManager.onPaint(UIPaintListenerManager.java:33)
        at org.herac.tuxguitar.ui.swt.event.SWTPaintListenerManager.paintControl(SWTPaintListenerManager.java:20)
        at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:234)
        at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:89)
        at org.eclipse.swt.widgets.Display.sendEvent(Display.java:5618)
        at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1405)
        at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1431)
        at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1414)
        at org.eclipse.swt.widgets.Control.gtk_draw(Control.java:3940)
        at org.eclipse.swt.widgets.Scrollable.gtk_draw(Scrollable.java:342)
        at org.eclipse.swt.widgets.Composite.gtk_draw(Composite.java:455)
        at org.eclipse.swt.widgets.Widget.windowProc(Widget.java:2217)
        at org.eclipse.swt.widgets.Control.windowProc(Control.java:6846)
        at org.eclipse.swt.widgets.Display.windowProc(Display.java:5842)
        at org.eclipse.swt.internal.gtk.GTK._gtk_main_do_event(Native Method)
        at org.eclipse.swt.internal.gtk.GTK.gtk_main_do_event(GTK.java:4154)
        at org.eclipse.swt.widgets.Display.eventProc(Display.java:1474)
        at org.eclipse.swt.internal.gtk.OS._g_main_context_iteration(Native Method)
        at org.eclipse.swt.internal.gtk.OS.g_main_context_iteration(OS.java:1603)
        at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:4404)
        at org.herac.tuxguitar.ui.swt.SWTApplication.start(SWTApplication.java:64)
        at org.herac.tuxguitar.app.TuxGuitar.createUIContext(TuxGuitar.java:116)
        at org.herac.tuxguitar.app.TuxGuitar.createApplication(TuxGuitar.java:101)
        at org.herac.tuxguitar.app.TGMainSingleton.launchTuxGuitar(TGMainSingleton.java:57)
        at org.herac.tuxguitar.app.TGMainSingleton.launchSingleton(TGMainSingleton.java:45)
        at org.herac.tuxguitar.app.TGMainSingleton.main(TGMainSingleton.java:35)

The behavior is the same if you start TuxGuitar from the command line (tuxguitar song_with_markers.tg) or by clicking on song_with_markers.tg in the file manager (in my case KDE Dolphin).

The error does not occur if TuxGuitar is already running. Then, as expected, a new tab is opened containing the given file.

The error occurs on Linux, FreeBSD, MacOS and Windows.

The error occurs only with SWT, not with JFX.

@guiv42, I also tested your fix99 version: The "hang" error no longer occurs, the problem that TuxGuitar does not open the selected file on MacOS is still there.

guiv42 commented 1 year ago

thank you @helge17 for this detailed analysis, that's very clear. About Linux and markers, I will submit a new pull request to fix the issue, still keeping the markers displayed (it's currently under test)

Regarding MacOS issue: do you know if it was correctly working with 1.5.6? (I don't have in mind any bug report of this kind in SF)

helge17 commented 1 year ago

Same problem with 1.5.6: When clicking on a TG file in Finder, TuxGuitar starts with an empty file.

guiv42 commented 1 year ago

"TuxGuitar on Linux/Windows/MacOS/BSD hanging when starting with a file" issue should be solved by #100. Issue seems quite critical to me, so it could deserve a beta2 release. @helge17: it's up to you, I don't know how complex and time-consuming the full delivery process is

About the MacOS issue unfortunately I don't think I can help in any way. But at least it's not a bug we have introduced, and since it's been there for a while it may be treated with lower priority I think.

helge17 commented 1 year ago

Thanks for the patch; release 1.6.0beta2 is now online.

helge17 commented 1 year ago

I have tried two approaches to fix the "TuxGuitar on MacOS not opening the file selected in Finder" issue, but neither are ready for commit.

  1. Use an AppleScript wrapper to pass filenames to tuxguitar.sh

Basically this worked, you can double click on a TG/GP file in Finder (or drop it on the TuxGuitar app) and TuxGuitar will open the file. But two small problems remain:

You can reproduce the second problem by running tuxguitar.sh from the MacOS terminal app.

  1. Create a pkg package

This would be the modern way to distribute applications for MacOS anyway. I was able to create a simple pkg using jpackage. But that doesn't solve our initial problem; it seems that you also have to modify the Java code so that TuxGuitar can take over the filenames from Finder:

https://stackoverflow.com/questions/1575190/double-click-document-file-in-mac-os-x-to-open-java-application

So for version 1.6.x there probably won't be a fix, but for further versions there is hope ;-)

guiv42 commented 1 year ago

I also searched a bit the Internet on this topic. I have zero knowledge of Apple's ecosystem, but it seems the solution is not in a command line parameter but an Apple event. It seems current code defines dedicated classes to manage this, even an "opendoc" folder...

Could you please check what happens when double-clicking a file whilst TG is already opened? If this file opens correctly it may be a timing issue between the app initialization and when the event is sent by MacOS

helge17 commented 1 year ago

It makes no difference whether TuxGuitar is running or not, the file will not be opened by Finder (the MacOS file manager) in either case.

tuxguitar.sh expects the file to be opened as an argument and then passes the filename to Java. Finder does not pass the filenames via arguments, but by other means.

You can open a file from the command line with:

% open /path/to/tuxguitar-1.6.0beta3-macosx-swt-cocoa-x86_64.app --args /path/to/tab.tg

or

% /path/to/tuxguitar-1.6.0beta3-macosx-swt-cocoa-x86_64.app/Contents/MacOS/tuxguitar.sh /path/to/tab.tg

I think for a clean solution we need to get rid of the shell script and start Java directly "the Apple way". For example jpackage could do the job. But I don't know the Apple ecosystem, nor can I really code, so I could well be wrong ;-)

By the way, if you put more than one file on TuxGuitar, only the first file will be opened, not all of them, on all operating systems.

G-eos commented 6 months ago

I try do dig a little on this subject. I have no background with java application development, nor with Apple development not tux guitar. Thus this can be completely out of meaning. To go quick, I ask ChatGPT if he has some good hints. As expressed above, it seems that MacOS use a special event for this purpose as @guiv42 you highlight before.

Please find ChatGPT hints:

When an application is launched by double-clicking a file associated with it in Finder on macOS, the file's path is not directly provided as a command-line argument to the application. Instead, macOS launches the application and sends an Apple Event to it containing information about the file that was double-clicked.

To handle file opening events like this in a Java application on macOS, you typically need to use the Apple-specific Java Application Events API. This API allows your Java application to receive and handle Apple Events, including those related to file opening.

Here's a basic example of how you might use the Java Application Events API to handle file opening events in a Java application on macOS:

import com.apple.eawt.AppEvent;
import com.apple.eawt.AppEventListener;
import com.apple.eawt.Application;

public class MyApp {
    public static void main(String[] args) {
        // Register an application event listener
        Application.getApplication().setAppEventListener(new AppEventListener() {
            @Override
            public void handleOpenFile(AppEvent.OpenFilesEvent event) {
                // Handle file opening events
                for (java.io.File file : event.getFiles()) {
                    String filePath = file.getAbsolutePath();
                    System.out.println("File opened: " + filePath);
                    // Process the file as needed
                }
            }
        });
    }
}

In this example, we're using classes from the com.apple.eawt package, which is part of the Apple Java Extensions. These classes provide access to macOS-specific functionality, including handling file opening events.

When a file is opened by double-clicking on it in Finder, the handleOpenFile method in the AppEventListener is called, and you can access the file's path from the event.getFiles() method. You can then process the file as needed within your application.

It's worth noting that the com.apple.eawt package is specific to macOS and may not be available on other platforms. Therefore, you may need to use platform-specific code or libraries if you're targeting multiple operating systems.

G-eos commented 6 months ago

I also searched a bit the Internet on this topic. I have zero knowledge of Apple's ecosystem, but it seems the solution is not in a command line parameter but an Apple event. It seems current code defines dedicated classes to manage this, even an "opendoc" folder...

Could you please check what happens when double-clicking a file whilst TG is already opened? If this file opens correctly it may be a timing issue between the app initialization and when the event is sent by MacOS

With Tuxguitar 1.6.1:

G-eos commented 6 months ago

To handle file opening events like this in a Java application on macOS, you typically need to use the Apple-specific Java Application Events API. This API allows your Java application to receive and handle Apple Events, including those related to file opening.

east has been deprecated ... see there https://openjdk.org/jeps/272 need to to use awt ...

G-eos commented 5 months ago

It seems that https://github.com/AvaloniaUI/Avalonia/discussions/5504#discussioncomment-1552446 provides some interesting information (but googling an searching GitHub the UrlsOpened seems not something used ...). We need to register to Application.UrlsOpened event and this has to be done as soon as possible in the startup of the Application to not miss the event.

Some solution there with JFX https://openjfx-dev.openjdk.java.narkive.com/emgVmZMH/how-to-handle-file-open-requests-on-macos moreover EventHandler keyword is everywhere in JFX and Qt part of tuxguitar and not another place ...

Need to dig more :)

guiv42 commented 5 months ago

this has to be done as soon as possible in the startup of the Application to not miss the event

App's entry point is there, however I have no idea where the first call to OS-specific code can be found...

G-eos commented 5 months ago

After some digging, and following obsolete API. The good one since Java 9 since ok in Java 20 seem to be: java.awt.desktop.OpenFilesHandler

Usage seems to be done here or There

I try to add into desktop/TuxGuitar-cocoa-integration-swt/src/org/herac/tuxguitar/cocoa/opendoc/OpenDocListener.java the following code without great success. It compiles, run, I see the Test INIT on console but never see something when try to drag tuxguitar files on the opened app. When double-click it launch another TuxGuitar which stop immediately :-(

import java.awt.Desktop;
import java.awt.desktop.OpenFilesEvent;
import java.awt.desktop.OpenFilesHandler;
...
public void OpenMyAppFilesHandler(OpenFilesEvent e) {
        System.out.println("OpenFileHandler()");
     }
...
    public void init() throws Throwable{
        Desktop.getDesktop().setOpenFileHandler(this::OpenMyAppFilesHandler);
        System.out.println("Test INIT OK");
....

Does someone can bring some help :)

guiv42 commented 5 months ago

Could you please push your modifications in a dedicated (experimental) branch, so I can have a look? (knowing I won't be able to build nor to test...)

G-eos commented 5 months ago

I will, but this is absolutely ugly :)

guiv42 commented 5 months ago

Just reading the java doc you have mentioned: public void setOpenFileHandler(OpenFilesHandler openFileHandler)

I understand the argument you need to pass is an instance of a class which implements OpenFilesHandler interface.

This could look like this warning: NOT built, tested or whatever, not even written in an IDE checking syntax, I can also do ugly things ;)

private class MyOpenFilesHandler implements OpenFilesHandler {   // should require one more "import" statement?
    void openFiles(OpenFilesEvent e) {
        // here, try to print some attribute of e, to check you get the expected info
        // see https://docs.oracle.com/en/java/javase/20/docs/api/java.desktop/java/awt/desktop/OpenFilesEvent.html
    }
}

(...)
    public void init() throws Throwable{
        Desktop.getDesktop().setOpenFileHandler(new MyOpenFilesHandler());
        System.out.println("Test INIT OK");

note: a short test like getDesktop().isDesktopSupported() could prove useful (?)

G-eos commented 5 months ago

It's there https://github.com/G-eos/tuxguitar/tree/wip/mac_open_files

G-eos commented 5 months ago

I try your code, does not compile. But I am not a java coder, probably trivial ... the code is pushed on the branch

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project tuxguitar-cocoa-integration-swt: Compilation failure: Compilation failure: [ERROR] /Users/xav/git/tuxguitar/desktop/TuxGuitar-cocoa-integration-swt/src/org/herac/tuxguitar/cocoa/opendoc/OpenDocListener.java:[9,9] modifier private not allowed here [ERROR] /Users/xav/git/tuxguitar/desktop/TuxGuitar-cocoa-integration-swt/src/org/herac/tuxguitar/cocoa/opendoc/OpenDocListener.java:[10,10] openFiles(java.awt.desktop.OpenFilesEvent) in org.herac.tuxguitar.cocoa.opendoc.MyOpenFilesHandler cannot implement openFiles(java.awt.desktop.OpenFilesEvent) in java.awt.desktop.OpenFilesHandler [ERROR] attempting to assign weaker access privileges; was public

guiv42 commented 5 months ago

1. move MyOpenFilesHandler class declaration inside OpenDocListener

2. replace void openFiles(OpenFilesEvent e) { by public void openFiles(OpenFilesEvent e) { ?

G-eos commented 5 months ago

Thanks ! I will have a look tomorrow

guiv42 commented 5 months ago

https://github.com/G-eos/tuxguitar/pull/1

G-eos commented 5 months ago

Some progress here. Look at the branch mac_open_files. Latest commit. Double click on a .tg file launch TuxGuitar without catching the event. But all later double click lead to an event triggered :-) The log files is

addListener OpenDocument
OMG Event !!!!!!
Event {type=46 null time=428746473 data=null x=0 y=0 width=0 height=0 detail=0}

Note: anoure files -> only the time change.

Good doc for this event is HERE.

This sounds promising

Note: No way to catch event with AWT, SWT take the lead. We shall stay with it.

guiv42 commented 5 months ago

all later double click lead to an event triggered :-)

OK, that's very good news!

Double click on a .tg file launch TuxGuitar without catching the event.

Possibly a timing issue. I'll have a look at your branch, and see if we can trigger the registration of a listener earlier in the init process. And debug log might be a contributor to an init delay... I'll try to propose an alternative implementation based on your work. Just one question: why did you register the "openDocument" listener in MacMenu? Got to check when it is initialized...

G-eos commented 5 months ago

Double click on a .tg file launch TuxGuitar without catching the event.

Possibly a timing issue. I'll have a look at your branch, and see if we can trigger the registration of a listener earlier in the init process. And debug log might be a contributor to an init delay...

In the SWT doc, there explain that the register must be done before "It should register this listener before calling PlatformUI.createAndRunWorkbench ... before ... starts running the event loop."

I'll try to propose an alternative implementation based on your work. Just one question: why did you register the "openDocument" listener in MacMenu? Got to check when it is initialized...

This was just a test, and reuse the available variables of Menu functions. And as it works, I was very happy to start to catch event and so share ;-)

There is more work needed, first to receive valuable information like the file path. Todo I see so far:

guiv42 commented 5 months ago

@G-eos : could you please have a try with my experimental branch? https://github.com/guiv42/tuxguitar/tree/experimental_mac

Just like before: I still cannot build for mac, so some modifications should be expected

It's inspired from your last experiment. In your todo list, this should check all actions but the last one. This last action is more tricky, as I don't know exactly when the plugin manager initializes the different plugins during app start (and it may be complex to modify...)

G-eos commented 5 months ago

It works well :-) including multi files open at the same time :).

As planned, at first open when tuxguitar is not started, it only start tuxguitar but not open the file.

Great thanks.

guiv42 commented 5 months ago

at first open when tuxguitar is not started

not understood. Shall I understand that:

?

G-eos commented 5 months ago

Yes you understand well.

guiv42 commented 5 months ago

OK, so the most complex use case is still not solved. I've got to look at the initialization of plugins, but I'm not so confident I can change that in a safe manner...

G-eos commented 5 months ago

The ability to open a file when TuxGuitar is started is already a good step. Even if it lacks the important part, I agree.

Does this code is specific to Mac ? Does swt does not abstract it, the method are not available if not on Mac or are they empty ? If yes, it could be done without a plugin ?

No other platforms available to check the idea.

guiv42 commented 5 months ago

On other platforms, file to open is passed to app as an argument of the launch script. This way of working with events is specific to mac

G-eos commented 5 months ago

This is clear. But what happens if this event is registered on the other platform ? Does it crash ? Does not compile ?

guiv42 commented 5 months ago

what happens if this event is registered on the other platform ?

I don't know, but just by precaution I would not try to modify something which has been working correctly for years! It seems OK for me to keep this plugin specific to mac.

I'm currently trying to understand the init sequence. I don't know exactly when the "event loop" is started, but my understanding is that 1st action calling user interface (after splash screen is launched in dedicated thread) is there. Plugins are initialized a few lines below.

One possibility could be to create a category of "early initialization" plugins. In this early init of open document plugin, just catch the event and store it, don't try to trigger any other action. Then, when plugin is "normally" initialized (i.e. as of today), read the stored event and trigger the open action. It seems to me quite close to the example from swt doc you have mentioned above. Go to have a look at this option (not trivial)

@G-eos could you please share your last version (whatever its quality, it's just for prototyping). Just to use it as a starting point

G-eos commented 5 months ago

Probably more complex, but clearly safer :-). Please note that the splash screen is not always displayed. In particular when TuxGuitar is launched with a double click on a .tg file on the finder.

G-eos commented 5 months ago

@guiv42 my last version is your branch, no need to modify. Just compile and run.

https://github.com/guiv42/tuxguitar/tree/experimental_mac

guiv42 commented 5 months ago

Early init of some specific plugins seems feasible, without breaking compatibility with other plugins. Since implementation would not be trivial, I would like first to make sure the diagnosis is correct. So I implemented a mock-up, where I manually initialize the specific macOS openDoc plugin before calling createWindow().

@G-eos : could you have a try with the latest version of this experimental branch? (as usual, not built not tested at all) https://github.com/guiv42/tuxguitar/commit/4c3676f8481d93728c91d122ccff054050eaad07

If it still does not work, one possibility is to edit TuxGuitar.java, and move the "dirty hack" even earlier in the init process, for example at the end of createMainContext()

G-eos commented 5 months ago

Hello, not better.

I try to move it and end of createMainContext(), it does not work anymore. Even if tuxguitar is already started. It is too early ...

guiv42 commented 5 months ago

OK thanks for the test. I tried to set up a test config in Linux, since implementation of openDoc plugin is no more dependent from macOS-specific lib. Unfortunately this does not work: I don't even catch any event when I double-click on a file with TG already open (apparently this works on macOS). Trying an alternative implementation: @G-eos , could you please have a try with https://github.com/guiv42/tuxguitar/commit/aa47b28b254f4e88b94a91553f031c6bd2169d7f?

suggested test procedure:

  1. start tuxguitar.sh, check no exception is raised and default template is opened. If KO, stop here
  2. double-click on a file -> is file opened?
  3. close TG, wait a few seconds
  4. double-click on a file, TG should start -> is file opened?

If it fails at some point, you can try to move the plugin early init statement at different places in the init sequence in TuxGuitar.java...

aol-nnov commented 5 months ago

Guys, you could peek the solution from some other java app for mac. For example there is mind mapping app called freemind that opens its files from finder.

It looks somewhat abandoned, but I have v 1.0.1 installed on Sonoma (14.3.1) and it still works like a charm.

G-eos commented 5 months ago

I use your version aa47b28b254f4e88b94a91553f031c6bd2169d7f latest commit complains about a context missing in OpenDocPlugin.java in the line this.openDocListener = new OpenDocListener(); but, no way for me to find which variable provides the context. Neverthe less the test on the following procedure is

  • start tuxguitar.sh, check no exception is raised and default template is opened. If KO, stop here No exception, it starts well, no file opened. (with splash screen)

  • double-click on a file -> is file opened? A new instance of TuxGuitar seems starting, and stop (just icon jumping in the bar. No file opened.

  • close TG, wait a few seconds

  • double-click on a file, TG should start -> is file opened?

  • tg files -> launch TuxGuitar, no splash screen and 2 new blank file opened.

  • gp files -> launch TuxGuitar, no splash screen and 2 new blank file opened.

  • gtp files -> launch TuxGuitar, no splash screen, popup with "Error: format de fichier non supporté"

  • image

Then, whatever the file opened without success. All next double click on a file, open correctly the file :) including gtp files.

I will add debug trace

G-eos commented 5 months ago

Guys, you could peek the solution from some other java app for mac. For example there is mind mapping app called freemind that opens its files from finder.

It looks somewhat abandoned, but I have v 1.0.1 installed on Sonoma (14.3.1) and it still works like a charm.

Thanks for the hint, the difficulty here is that SWT which provides GFX toolkit seems to have registered the OS event, we must use its own way of doing. We can have a look at https://github.com/google/gapid/blob/15b44454df3a2469ee9ba42cb116d9b50786c7a4/gapic/src/main/com/google/gapid/util/MacApplication.java#L41 which seems to do the same as what we are trying to do ;-)

guiv42 commented 5 months ago

@G-eos : I don't understand what configuration your tests refer to, since it seems my last commit does not build. But nevermind.

Without a context variable, it cannot work. You can try this: this.openDocListener = new OpenDocListener(TuxGuitar.getInstance().getContext());

this requires and additional import: import org.herac.tuxguitar.app.TuxGuitar;

About the other application you mention: interesting to see that it retrieves Display object with Display.getDefault() instead of Display.getCurrent()...

G-eos commented 5 months ago

I tested on the previous commit on your branch.

Thanks a lot for the modification you propose for context. It compiles well. And, this is a very good news:

guiv42 commented 5 months ago

OK, this is good news indeed!

So, there's still some synchronization issue when opening TG by double-clicking a file: both actions "create new default" and "open double-clicked file" are competing. It means the opendoc plugin must wait a little more before launching action to open file. Got to find the correct criterion to stop the waiting loop.

guiv42 commented 5 months ago

@G-eos : could you please try to update the waiting loop, in OpenDocListener.java:

  1. change start criterion Replace if (!pluginManager.allEnabledPluginsInitialized()) { by if (!TuxGuitar.getInstance().isInitialized()) {

  2. change stop criterion Replace while (!pluginManager.allEnabledPluginsInitialized()) { by while (!TuxGuitar.getInstance().isInitialized()) {

edit: just refer to the last version of my prototyping branch https://github.com/guiv42/tuxguitar/commit/e896ccce13a508e4cc5588db2e55c7b2a7a61c94

edit (again...): might not be a good idea. isInitialized can be set just after launching a new thread to create default song. So this gives no information about the status of this tread: competition between the 2 actions should persist. A possible alternative solution could be to set up a TGActionInterceptor when an event is received in opendoc plugin, to intercept the creation of default song.

Got to spend more time on this. But at this point, there's no reason why we could fail I think :)

guiv42 commented 5 months ago

@G-eos : when you have some time, could you please test https://github.com/guiv42/tuxguitar/commit/8da71191a52ca4c4fbac0badd357cc899347ba21?

I tried to cleanup the condition to enable processing of swt events (no more waiting loop). The logic is the following:

If some test scenarios fail, we'll need to add some logging (yet there is none).

G-eos commented 5 months ago

At startup from the CLI calling directly tuxguitar.sh plugin seems to crash with this information. I try to debug a bit without great success. Still no Java experience :)

java.lang.InstantiationException: org.herac.tuxguitar.cocoa.opendoc.OpenDocPlugin at java.base/java.lang.Class.newInstance(Class.java:719) at org.herac.tuxguitar.util.TGServiceReader$IteratorImpl.next(TGServiceReader.java:91) at org.herac.tuxguitar.util.plugin.TGPluginManager.lookupPlugins(TGPluginManager.java:46) at org.herac.tuxguitar.util.plugin.TGPluginManager.<init>(TGPluginManager.java:30) at org.herac.tuxguitar.util.plugin.TGPluginManager.<init>(TGPluginManager.java:14) at org.herac.tuxguitar.util.plugin.TGPluginManager$1.createInstance(TGPluginManager.java:171) at org.herac.tuxguitar.util.plugin.TGPluginManager$1.createInstance(TGPluginManager.java:169) at org.herac.tuxguitar.util.singleton.TGSingletonUtil.getInstance(TGSingletonUtil.java:12) at org.herac.tuxguitar.util.plugin.TGPluginManager.getInstance(TGPluginManager.java:169) at org.herac.tuxguitar.app.TuxGuitar.getPluginManager(TuxGuitar.java:263) at org.herac.tuxguitar.app.TuxGuitar.createUIContext(TuxGuitar.java:119) at org.herac.tuxguitar.app.TuxGuitar.createApplication(TuxGuitar.java:102) at org.herac.tuxguitar.app.TGMainSingleton.launchTuxGuitar(TGMainSingleton.java:57) at org.herac.tuxguitar.app.TGMainSingleton.launchSingleton(TGMainSingleton.java:45) at org.herac.tuxguitar.app.TGMainSingleton.main(TGMainSingleton.java:35) Caused by: java.lang.NoSuchMethodException: org.herac.tuxguitar.cocoa.opendoc.OpenDocPlugin.<init>() at java.base/java.lang.Class.getConstructor0(Class.java:3761) at java.base/java.lang.Class.newInstance(Class.java:706) ... 14 more org.herac.tuxguitar.util.plugin.TGPluginException: An error ocurred when trying to lookup plugin at org.herac.tuxguitar.util.plugin.TGPluginManager.lookupPlugins(TGPluginManager.java:51) at org.herac.tuxguitar.util.plugin.TGPluginManager.<init>(TGPluginManager.java:30) at org.herac.tuxguitar.util.plugin.TGPluginManager.<init>(TGPluginManager.java:14) at org.herac.tuxguitar.util.plugin.TGPluginManager$1.createInstance(TGPluginManager.java:171) at org.herac.tuxguitar.util.plugin.TGPluginManager$1.createInstance(TGPluginManager.java:169) at org.herac.tuxguitar.util.singleton.TGSingletonUtil.getInstance(TGSingletonUtil.java:12) at org.herac.tuxguitar.util.plugin.TGPluginManager.getInstance(TGPluginManager.java:169) at org.herac.tuxguitar.app.TuxGuitar.getPluginManager(TuxGuitar.java:263) at org.herac.tuxguitar.app.TuxGuitar.createUIContext(TuxGuitar.java:119) at org.herac.tuxguitar.app.TuxGuitar.createApplication(TuxGuitar.java:102) at org.herac.tuxguitar.app.TGMainSingleton.launchTuxGuitar(TGMainSingleton.java:57) at org.herac.tuxguitar.app.TGMainSingleton.launchSingleton(TGMainSingleton.java:45) at org.herac.tuxguitar.app.TGMainSingleton.main(TGMainSingleton.java:35) Caused by: java.util.NoSuchElementException at org.herac.tuxguitar.util.TGServiceReader$IteratorImpl.next(TGServiceReader.java:98) at org.herac.tuxguitar.util.plugin.TGPluginManager.lookupPlugins(TGPluginManager.java:46) ... 12 more Exception in thread "Thread-1" java.lang.NullPointerException: Cannot invoke "org.herac.tuxguitar.action.TGActionContext.setAttribute(String, Object)" because "actionContext" is null at org.herac.tuxguitar.editor.action.TGActionProcessor.appendAttributes(TGActionProcessor.java:67) at org.herac.tuxguitar.editor.action.TGActionProcessor.fillActionContext(TGActionProcessor.java:57) at org.herac.tuxguitar.editor.action.TGActionProcessor.processOnCurrentThread(TGActionProcessor.java:75) at org.herac.tuxguitar.editor.action.TGActionProcessor.processOnCurrentThread(TGActionProcessor.java:88) at org.herac.tuxguitar.editor.action.TGActionProcessor$1.run(TGActionProcessor.java:98) at org.herac.tuxguitar.thread.TGPooledThreadHandler.processNext(TGPooledThreadHandler.java:66) at org.herac.tuxguitar.thread.TGPooledThreadHandler.run(TGPooledThreadHandler.java:86) at java.base/java.lang.Thread.run(Thread.java:1583)