Skip to content

Commit

Permalink
Add debug button to copy Linux command output
Browse files Browse the repository at this point in the history
  • Loading branch information
nvdweem committed Jun 14, 2023
1 parent bdac8fd commit 2fb47c4
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 3 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>

<dependency>
<groupId>net.java.dev.jna</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.collections4.queue.CircularFifoQueue;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
Expand All @@ -23,6 +26,8 @@
public class PulseAudioEventListener extends Thread {
private final ApplicationEventPublisher eventPublisher;
private final ProcessHelper processHelper;
private final CircularFifoQueue<String> latestEvents = new CircularFifoQueue<>(50);

private boolean running = true;

@PostConstruct
Expand All @@ -44,9 +49,11 @@ public void run() {
var process = processHelper.builder("pactl", "subscribe").start();
var reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

var dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String line;
//noinspection NestedAssignment
while ((line = reader.readLine()) != null) {
latestEvents.add(dateFormat.format(new Date()) + " - " + line);
checkTrigger(line);
}
} catch (IOException e) {
Expand All @@ -55,6 +62,10 @@ public void run() {
}
}

String getDebugOutput() {
return "pactl subscribe:\n" + String.join("\n", latestEvents);
}

private void checkTrigger(String line) {
if (StringUtils.containsIgnoreCase(line, "Event 'new' on sink-input") || StringUtils.containsIgnoreCase(line, "Event 'remove' on sink-input")) {
eventPublisher.publishEvent(new LinuxSessionChangedEvent());
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/getpcpanel/cpp/linux/PulseAudioWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.Map;
import java.util.regex.Pattern;

import javax.annotation.Nonnull;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
Expand Down Expand Up @@ -143,6 +145,17 @@ private String idxOrDefaultDevice(int idx) {
return idx == DEFAULT_DEVICE ? "@DEFAULT_SINK@" : String.valueOf(idx);
}

@Nonnull
List<String> getDebugOutput() {
return StreamEx.of(InOutput.values())
.map(t -> new String[] { "pactl", "list", t.pulseType })
.mapToEntry(cmd -> runAndRead(processHelper.builder(cmd)))
.mapKeys(cmd -> String.join(" ", cmd))
.mapValues(lines -> String.join("\n", lines))
.mapKeyValue((cmd, lns) -> cmd + ":\n" + lns)
.toList();
}

@Builder
record PulseAudioTarget(int index, boolean isDefault, Map<String, String> metas, Map<String, String> properties, InOutput type) {
}
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/getpcpanel/cpp/linux/SndCtrlLinuxDebug.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.getpcpanel.cpp.linux;

import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;

import org.springframework.stereotype.Service;

import com.getpcpanel.spring.ConditionalOnLinux;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import one.util.streamex.StreamEx;

@Log4j2
@Service
@ConditionalOnLinux
@RequiredArgsConstructor
public class SndCtrlLinuxDebug {
private final PulseAudioWrapper paWrapper;
private final PulseAudioEventListener paEventListener;

public void copyDebugOutput() {
var output = StreamEx.of(paWrapper.getDebugOutput())
.append(paEventListener.getDebugOutput())
.joining("\n".repeat(5));
var content = new StringSelection(output);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(content, null);
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/getpcpanel/spring/OsHelper.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.getpcpanel.spring;

import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.springframework.stereotype.Service;

import javafx.css.Styleable;
import javafx.scene.Node;
import one.util.streamex.StreamEx;

@Service
Expand Down Expand Up @@ -38,6 +40,13 @@ public boolean isSupported(Styleable elem) {
return toHideClasses.stream().noneMatch(toHideClass -> elem.getStyleClass().contains(toHideClass));
}

public void hideUnsupportedChildren(List<Node> children) {
StreamEx.of(children).remove(this::isSupported).forEach(c -> {
c.setVisible(false);
c.setManaged(false);
});
}

public boolean isOs(String os) {
return StringUtils.equalsAny(os, "*", osString());
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/getpcpanel/ui/SettingsDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.stereotype.Component;

import com.getpcpanel.MainFX;
import com.getpcpanel.cpp.linux.SndCtrlLinuxDebug;
import com.getpcpanel.cpp.windows.SndCtrlWindows;
import com.getpcpanel.obs.OBS;
import com.getpcpanel.profile.Save;
Expand All @@ -36,6 +37,7 @@
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;
Expand Down Expand Up @@ -82,6 +84,8 @@ public class SettingsDialog extends Application implements UIInitializer<SingleP
@FXML private TextField txtOnlyIfDelta;
@FXML private CheckBox cbFixOnlySliders;
@FXML private OSCSettingsDialog oscSettingsController;
@FXML private VBox debug;
@FXML private Label copied;

@Override
public void initUI(@Nonnull SingleParamInitializer<Stage> args) {
Expand Down Expand Up @@ -217,6 +221,7 @@ private void initOverlayColors(Save save) {

private void postInit() {
initFields();
osHelper.hideUnsupportedChildren(debug.getChildrenUnmodifiable());
}

public void doTest(ActionEvent ignored) {
Expand Down Expand Up @@ -246,4 +251,10 @@ public void doTest(ActionEvent ignored) {
public void triggerAv(ActionEvent ignored) {
MainFX.getBean(SndCtrlWindows.class).triggerAv();
}

public void copyAudioOutput(ActionEvent ignored) {
copied.setText("Preparing output");
MainFX.getBean(SndCtrlLinuxDebug.class).copyDebugOutput();
copied.setText("Output was copied to your clipboard");
}
}
14 changes: 11 additions & 3 deletions src/main/resources/assets/SettingsDialog.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,21 @@
</VBox>
</VBox>
</Tab>
<Tab text="Logs">
<VBox layoutX="30.0" layoutY="28.0">
<Tab text="Debug">
<VBox fx:id="debug" layoutX="30.0" layoutY="28.0">
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
</padding>
<Button layoutX="30.0" layoutY="28.0" mnemonicParsing="false" onAction="#openLogsFolder" text="Open Logs Folder"/>
<Button layoutX="30.0" layoutY="28.0" mnemonicParsing="false" onAction="#triggerAv" text="Trigger AV (for debugging)"/>
<Button layoutX="30.0" layoutY="28.0" mnemonicParsing="false" onAction="#triggerAv" styleClass="only-windows" text="Trigger AV (for debugging)"/>
<HBox alignment="CENTER_LEFT" styleClass="only-linux">
<Button layoutX="30.0" layoutY="28.0" mnemonicParsing="false" onAction="#copyAudioOutput" text="Copy (Linux) audio output"/>
<Label fx:id="copied" text="Useful when submitting an issue, check the output for personal data">
<padding>
<Insets left="10.0"/>
</padding>
</Label>
</HBox>
</VBox>
</Tab>
<Tab text="Faulty hardware workarounds">
Expand Down

0 comments on commit 2fb47c4

Please sign in to comment.