Skip to content

Commit

Permalink
Add delay for register listeners to prevent deadlock on Linux.
Browse files Browse the repository at this point in the history
  • Loading branch information
breiler committed Dec 2, 2024
1 parent 640761e commit 10d694f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This file is part of Universal Gcode Sender (UGS).
import com.willwinder.ugs.nbm.visualizer.jogl.VisualizationPanel;
import com.willwinder.ugs.nbm.visualizer.options.VisualizerOptions;
import com.willwinder.ugs.nbp.lib.services.LocalizingService;
import static com.willwinder.ugs.nbp.lib.services.LocalizingService.lang;
import com.willwinder.ugs.nbp.lib.services.TopComponentLocalizer;
import com.willwinder.universalgcodesender.i18n.Localization;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -41,8 +42,6 @@ This file is part of Universal Gcode Sender (UGS).
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.willwinder.ugs.nbp.lib.services.LocalizingService.lang;

/**
* Setup JOGL canvas, GcodeRenderer and RendererInputHandler.
*/
Expand Down Expand Up @@ -98,22 +97,26 @@ protected void componentOpened() {
if (VisualizerOptions.getBooleanOption(VisualizerOptions.VISUALIZER_OPTION_LEGACY, false)) {
borderedPanel.add(new VisualizationPanel(), BorderLayout.CENTER);
} else {
WindowManager.getDefault().invokeWhenUIReady(() -> {
executor.execute(() -> {
try {
SwingUtilities.invokeAndWait(() -> {
borderedPanel.add(new NewtVisualizationPanel(), BorderLayout.CENTER);
borderedPanel.revalidate();
});
} catch (InterruptedException | InvocationTargetException e) {
throw new RuntimeException(e);
}
});
});
createAndAddNewtPanel(borderedPanel);
}
add(borderedPanel, BorderLayout.CENTER);
}

private void createAndAddNewtPanel(JPanel borderedPanel) {
WindowManager.getDefault().invokeWhenUIReady(() -> {
executor.execute(() -> {
try {
SwingUtilities.invokeAndWait(() -> {
borderedPanel.add(new NewtVisualizationPanel(), BorderLayout.CENTER);
borderedPanel.revalidate();
});
} catch (InterruptedException | InvocationTargetException e) {
throw new RuntimeException(e);
}
});
});
}

@OnStart
public static class Localizer extends TopComponentLocalizer {
public Localizer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ This file is part of Universal Gcode Sender (UGS).
import com.willwinder.ugs.nbp.core.actions.OpenLogDirectoryAction;
import com.willwinder.ugs.nbp.lib.lookup.CentralLookup;
import com.willwinder.universalgcodesender.model.BackendAPI;
import com.willwinder.universalgcodesender.utils.ThreadHelper;
import org.openide.util.Lookup;
import org.openide.util.NbPreferences;

Expand Down Expand Up @@ -63,7 +64,6 @@ public class NewtVisualizationPanel extends JPanel {
private transient RendererInputHandler rih;
private transient GLWindow glWindow;


public NewtVisualizationPanel() {
backend = CentralLookup.getDefault().lookup(BackendAPI.class);
setLayout(new BorderLayout());
Expand Down Expand Up @@ -121,8 +121,6 @@ private NewtCanvasAWT makeWindow() throws GLException {
glWindow.unlockSurface();
}

NewtCanvasAWT p = new NewtCanvasAWT(glWindow);

GcodeRenderer renderer = Lookup.getDefault().lookup(GcodeRenderer.class);
if (renderer == null) {
throw new IllegalArgumentException("Failed to access GcodeRenderer.");
Expand All @@ -134,24 +132,48 @@ private NewtCanvasAWT makeWindow() throws GLException {
Preferences pref = NbPreferences.forModule(VisualizerOptionsPanel.class);
pref.addPreferenceChangeListener(this.rih);

File f = (backend.getProcessedGcodeFile() != null) ?
backend.getProcessedGcodeFile() : backend.getGcodeFile();
File f = (backend.getProcessedGcodeFile() != null) ? backend.getProcessedGcodeFile() : backend.getGcodeFile();
if (f != null) {
this.rih.setGcodeFile(f.getAbsolutePath());
}

// Install listeners...
backend.addUGSEventListener(this.rih);
glWindow.addMouseListener(new NewtMouseListenerAdapter(p, this.rih, this.rih, this.rih));
glWindow.addKeyListener(new NewtKeyboardListenerAdapter(p, this.rih));

glWindow.addGLEventListener(renderer);

NewtCanvasAWT p = new NewtCanvasAWT(glWindow);
p.setShallUseOffscreenLayer(true);
p.setBackground(Color.BLACK);

p.setIgnoreRepaint(true);
glWindow.setSurfaceScale(new float[]{ScalableSurface.IDENTITY_PIXELSCALE,
ScalableSurface.IDENTITY_PIXELSCALE});
glWindow.setSurfaceScale(new float[]{ScalableSurface.IDENTITY_PIXELSCALE, ScalableSurface.IDENTITY_PIXELSCALE});

// Workaround for linux, register listeners after the window has been created
ThreadHelper.invokeLater(() -> {
glWindow.addMouseListener(new NewtMouseListenerAdapter(p, this.rih, this.rih, this.rih));
glWindow.addKeyListener(new NewtKeyboardListenerAdapter(p, this.rih));
glWindow.addWindowListener(new com.jogamp.newt.event.WindowAdapter() {
@Override
public void windowResized(final com.jogamp.newt.event.WindowEvent e) {
resize();
}
});
}, 500);
return p;
}

@Override
public void setBounds(int x, int y, int width, int height) {
super.setBounds(x, y, width, height);
resize();
}

private void resize() {
UPDATE_SIZE_SCHEDULER.execute(() -> {
if (glWindow.isVisible()) {
glWindow.setPosition(0, 0);
glWindow.setSize(getWidth(), getHeight());
}
});
}
}

0 comments on commit 10d694f

Please sign in to comment.