essential-actions
Clone or download
Modified Files
# Media Key Action
Documentation is WIP
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<modelVersion>4.0.0</modelVersion>
<groupId>com.stream-pi</groupId>
<groupId>com.stream-pi</groupId>
<artifactId>mediakeyaction</artifactId>
<artifactId>mediakeyaction</artifactId>
<version>1.0.1</version>
<version>1.1.0</version>
<build>
<build>
<plugins>
<plugins>
<plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<version>3.1.0</version>
<executions>
<executions>
<execution>
<execution>
<id>test-jar</id>
<id>test-jar</id>
<phase>package</phase>
<phase>package</phase>
<goals>
<goals>
<goal>test-jar</goal>
<goal>test-jar</goal>
</goals>
</goals>
</execution>
</execution>
</executions>
</executions>
</plugin>
</plugin>
<plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<version>3.8.1</version>
<configuration>
<configuration>
<release>11</release>
<release>11</release>
</configuration>
</configuration>
</plugin>
</plugin>
</plugins>
</plugins>
</build>
</build>
<properties>
<properties>
<ActionAPIVersion>1.0.0-SNAPSHOT</ActionAPIVersion>
<ActionAPIVersion>1.0.0-SNAPSHOT</ActionAPIVersion>
<UtilVersion>1.0.0-SNAPSHOT</UtilVersion>
<UtilVersion>1.0.0-SNAPSHOT</UtilVersion>
<JavaFXVersion>16-ea+5</JavaFXVersion>
<JavaFXVersion>16-ea+5</JavaFXVersion>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.target>11</maven.compiler.target>
<IkonliVersion>11.5.0</IkonliVersion>
<IkonliVersion>11.5.0</IkonliVersion>
<IkonliFA5PackVersion>11.5.0</IkonliFA5PackVersion>
<IkonliFA5PackVersion>11.5.0</IkonliFA5PackVersion>
</properties>
</properties>
<dependencies>
<dependencies>
<dependency>
<dependency>
<groupId>com.stream-pi</groupId>
<groupId>com.stream-pi</groupId>
<artifactId>util</artifactId>
<artifactId>util</artifactId>
<version>${UtilVersion}</version>
<version>${UtilVersion}</version>
</dependency>
</dependency>
<dependency>
<dependency>
<groupId>com.stream-pi</groupId>
<groupId>com.stream-pi</groupId>
<artifactId>action-api</artifactId>
<artifactId>action-api</artifactId>
<version>${ActionAPIVersion}</version>
<version>${ActionAPIVersion}</version>
</dependency>
</dependency>
<dependency>
<dependency>
<groupId>org.openjfx</groupId>
<groupId>org.openjfx</groupId>
<artifactId>javafx-media</artifactId>
<artifactId>javafx-media</artifactId>
<version>${JavaFXVersion}</version>
<version>${JavaFXVersion}</version>
</dependency>
</dependency>
<dependency>
<dependency>
<groupId>org.kordamp.ikonli</groupId>
<groupId>org.kordamp.ikonli</groupId>
<artifactId>ikonli-fontawesome5-pack</artifactId>
<artifactId>ikonli-fontawesome5-pack</artifactId>
<version>${IkonliFA5PackVersion}</version>
<version>${IkonliFA5PackVersion}</version>
</dependency>
</dependency>
<dependency>
<dependency>
<groupId>org.kordamp.ikonli</groupId>
<groupId>org.kordamp.ikonli</groupId>
<artifactId>ikonli-javafx</artifactId>
<artifactId>ikonli-javafx</artifactId>
<version>${IkonliVersion}</version>
<version>${IkonliVersion}</version>
</dependency>
</dependency>
</dependencies>
</dependencies>
</project>
</project>
package com.stream_pi.mediakeyaction;
package com.stream_pi.mediakeyaction;
import com.stream_pi.action_api.actionproperty.property.Property;
import com.stream_pi.action_api.actionproperty.property.Property;
import com.stream_pi.action_api.actionproperty.property.Type;
import com.stream_pi.action_api.actionproperty.property.Type;
import com.stream_pi.action_api.normalaction.NormalAction;
import com.stream_pi.action_api.normalaction.NormalAction;
import com.stream_pi.util.alert.StreamPiAlert;
import com.stream_pi.util.alert.StreamPiAlert;
import com.stream_pi.util.alert.StreamPiAlertType;
import com.stream_pi.util.alert.StreamPiAlertType;
import com.stream_pi.util.platform.Platform;
import com.stream_pi.util.platform.Platform;
import com.stream_pi.util.version.Version;
import com.stream_pi.util.version.Version;
import java.io.*;
import java.io.*;
import java.util.List;
import java.util.List;
public class MediaKeyAction extends NormalAction
public class MediaKeyAction extends NormalAction
{
{
public MediaKeyAction()
public MediaKeyAction()
{
{
setName("Media Key Action");
setName("Media Key Action");
setCategory("Essentials");
setCategory("Essentials");
setAuthor("rnayabed");
setAuthor("rnayabed");
setServerButtonGraphic("fas-volume-up");
setServerButtonGraphic("fas-volume-up");
setHelpLink("https://github.com/Stream-Pi/EssentialActions");
setHelpLink("https://github.com/stream-pi/essentialactions/tree/master/mediakeyaction");
setVersion(new Version(1,0,0));
setVersion(new Version(1,1,0));
}
}
private List<String> states;
@Override
@Override
public void initProperties() throws Exception {
public void initProperties() throws Exception {
states = List.of("Play/Pause", "Previous", "Next", "Vol Up", "Vol Down", "Mute", "Stop");
List<String> states = List.of("Play/Pause", "Previous", "Next", "Vol Up", "Vol Down", "Mute", "Stop");
Property mediaKeyTypeProperty = new Property("media_key_type", Type.LIST);
Property mediaKeyTypeProperty = new Property("media_key_type", Type.LIST);
mediaKeyTypeProperty.setListValue(states);
mediaKeyTypeProperty.setListValue(states);
mediaKeyTypeProperty.setDisplayName("Media Key");
mediaKeyTypeProperty.setDisplayName("Media Key");
addClientProperties(mediaKeyTypeProperty);
addClientProperties(mediaKeyTypeProperty);
}
}
@Override
@Override
public void initAction() throws Exception {
public void initAction() throws Exception {
if(getServerConnection().getPlatform() == Platform.WINDOWS)
if(getServerConnection().getPlatform() == Platform.WINDOWS)
extractKeyEXEToTmpDirectory();
extractKeyEXEToTmpDirectory();
}
}
private static String absolutePathToExe;
private static String absolutePathToExe;
private void extractKeyEXEToTmpDirectory() throws Exception
private void extractKeyEXEToTmpDirectory() throws Exception
{
{
String tmpDirectory = System.getProperty("java.io.tmpdir");
String tmpDirectory = System.getProperty("java.io.tmpdir");
InputStream is = getClass().getResource("key.exe").openStream();
InputStream is = getClass().getResource("key.exe").openStream();
absolutePathToExe = tmpDirectory+"/key.exe";
absolutePathToExe = tmpDirectory+"/key.exe";
File file = new File(absolutePathToExe);
File file = new File(absolutePathToExe);
file.deleteOnExit();
file.deleteOnExit();
OutputStream os = new FileOutputStream(file);
OutputStream os = new FileOutputStream(file);
byte[] b = new byte[2048];
byte[] b = new byte[2048];
int length;
int length;
while ((length = is.read(b)) != -1) {
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
os.write(b, 0, length);
}
}
is.close();
is.close();
os.close();
os.close();
}
}
@Override
@Override
public void onActionClicked() throws Exception
public void onActionClicked() throws Exception
{
{
int state = getClientProperties().getSingleProperty("media_key_type").getSelectedIndex();
int state = getClientProperties().getSingleProperty("media_key_type").getSelectedIndex();
switch(getServerConnection().getPlatform())
switch(getServerConnection().getPlatform())
{
{
case LINUX :
case LINUX :
linuxHandler(state);
linuxHandler(state);
break;
break;
case WINDOWS:
case WINDOWS:
windowsHandler(state);
windowsHandler(state);
break;
break;
default:
default:
othersHandler();
othersHandler();
}
}
}
}
@Override
@Override
public void onShutDown() throws Exception
public void onShutDown()
{
{
}
}
private int runCommand(String command) throws Exception
private void runCommand(String command) throws Exception
{
{
Runtime rt = Runtime.getRuntime();
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);
Process pr = rt.exec(command);
return pr.waitFor();
pr.waitFor();
}
}
private void linuxHandler(int state) throws Exception
private void linuxHandler(int state) throws Exception
{
{
try
try
{
{
switch(state)
switch(state)
{
{
case 0: runCommand("xdotool key XF86AudioPlay"); break;
case 0: runCommand("xdotool key XF86AudioPlay"); break;
case 1: runCommand("xdotool key XF86AudioPrev"); break;
case 1: runCommand("xdotool key XF86AudioPrev"); break;
case 2: runCommand("xdotool key XF86AudioNext"); break;
case 2: runCommand("xdotool key XF86AudioNext"); break;
case 3: runCommand("xdotool key XF86AudioRaiseVolume"); break;
case 3: runCommand("xdotool key XF86AudioRaiseVolume"); break;
case 4: runCommand("xdotool key XF86AudioLowerVolume"); break;
case 4: runCommand("xdotool key XF86AudioLowerVolume"); break;
case 5: runCommand("xdotool key XF86AudioMute"); break;
case 5: runCommand("xdotool key XF86AudioMute"); break;
case 6: runCommand("xdotool key XF86AudioStop"); break;
case 6: runCommand("xdotool key XF86AudioStop"); break;
}
}
}
}
catch (Exception e)
catch (Exception e)
{
{
e.printStackTrace();
e.printStackTrace();
new StreamPiAlert("Uh Oh!",
throw new UnsupportedOperationException(
"It looks like 'xdotool' is not installed in this computer. Please install it and try again.",
"It looks like 'xdotool' is not installed in this computer. Please install it and try again."
StreamPiAlertType.ERROR
);
).show();
}
}
}
}
private void windowsHandler(int state) throws Exception
private void windowsHandler(int state) throws Exception {
{
try
try
{
{
String exe = "\""+absolutePathToExe+"\"";
switch(state)
switch(state)
{
{
case 0: runCommand(absolutePathToExe+" 0xB3"); break;
case 0: runCommand(exe+" 0xB3"); break;
case 1: runCommand(absolutePathToExe+" 0xB1"); break;
case 1: runCommand(exe+" 0xB1"); break;
case 2: runCommand(absolutePathToExe+" 0xB0"); break;
case 2: runCommand(exe+" 0xB0"); break;
case 3: runCommand(absolutePathToExe+" 0xAF"); break;
case 3: runCommand(exe+" 0xAF"); break;
case 4: runCommand(absolutePathToExe+" 0xAE"); break;
case 4: runCommand(exe+" 0xAE"); break;
case 5: runCommand(absolutePathToExe+" 0xAD"); break;
case 5: runCommand(exe+" 0xAD"); break;
case 6: runCommand(absolutePathToExe+" 0xB2"); break;
case 6: runCommand(exe+" 0xB2"); break;
}
}
}
}
catch (Exception e)
catch (Exception e)
{
{
e.printStackTrace();
e.printStackTrace();
new StreamPiAlert("Uh Oh!",
throw new UnsupportedOperationException(
"Internal Error occured. Check logs, stacktrace. Report to us.",
"Internal Error occurred. Check logs, stacktrace. Report to us."
StreamPiAlertType.ERROR
);
).show();
}
}
}
}
private void othersHandler()
private void othersHandler()
{
{
new StreamPiAlert("Uh Oh!",
throw new UnsupportedOperationException(
"Media Keys arent supported on this platform yet. Check out the supported platforms by clicking the '?' button on plugins pane/Plugins Settings.",
"Media Keys arent supported on this platform yet. Check out the supported platforms by clicking the '?' button on Plugins Pane"
StreamPiAlertType.ERROR
);
).show();
}
}
}
}