essential-actions

Clone or download

Use ListValue for media key states

Modified Files

# Media Key Action
# Media Key Action
Documentation is WIP
Control playback with the following media keys:
- Play / Pause toggle
- Stop
- Mute
- Play Previous Track
- Play Next Track
- Volume Up
- Volume Down
## Dependencies
### on Linux
This action needs [xdotool](https://github.com/jordansissel/xdotool) to be installed.
package com.stream_pi.mediakeyaction;
package com.stream_pi.mediakeyaction;
import com.stream_pi.action_api.actionproperty.property.ListValue;
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.externalplugin.NormalAction;
import com.stream_pi.action_api.externalplugin.NormalAction;
import com.stream_pi.util.exception.MinorException;
import com.stream_pi.util.exception.MinorException;
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;
import java.util.Objects;
import java.util.Objects;
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/tree/master/mediakeyaction");
setHelpLink("https://github.com/stream-pi/essentialactions/tree/master/mediakeyaction");
setVersion(new Version(1,1,1));
setVersion(new Version(1, 1, 1));
}
}
@Override
@Override
public void initProperties() throws MinorException
public void initProperties() throws MinorException
{
{
List<String> states = List.of("Play/Pause", "Previous", "Next", "Vol Up", "Vol Down", "Mute", "Stop");
final List<ListValue> states = List.of(
new ListValue("Play/Pause"),
new ListValue("Previous"),
new ListValue("Next"),
new ListValue("Vol Up"),
new ListValue("Vol Down"),
new ListValue("Mute"),
new ListValue("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 MinorException
public void initAction() throws MinorException
{
{
try
try
{
{
if(getServerConnection().getPlatform() == Platform.WINDOWS)
if (getServerConnection().getPlatform() == Platform.WINDOWS)
{
extractKeyEXEToTmpDirectory();
extractKeyEXEToTmpDirectory();
}
}
catch (IOException e)
} catch (IOException e)
{
{
e.printStackTrace();
e.printStackTrace();
throw new MinorException("Error","IOException occurred. \n\n"+e.getMessage());
throw new MinorException("Error", "IOException occurred. \n\n" + e.getMessage());
}
}
}
}
private static String absolutePathToExe;
private static String absolutePathToExe;
private void extractKeyEXEToTmpDirectory() throws IOException
private void extractKeyEXEToTmpDirectory() throws IOException
{
{
String tmpDirectory = System.getProperty("java.io.tmpdir");
String tmpDirectory = System.getProperty("java.io.tmpdir");
InputStream is = Objects.requireNonNull(getClass().getResource("key.exe")).openStream();
InputStream is = Objects.requireNonNull(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 MinorException
public void onActionClicked() throws MinorException
{
{
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();
}
}
}
}
private void 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);
pr.waitFor();
pr.waitFor();
}
}
private void linuxHandler(int state)
private void linuxHandler(int state)
{
{
try
try
{
{
switch(state)
switch (state)
{
{
case 0: runCommand("xdotool key XF86AudioPlay"); break;
case 0:
case 1: runCommand("xdotool key XF86AudioPrev"); break;
runCommand("xdotool key XF86AudioPlay");
case 2: runCommand("xdotool key XF86AudioNext"); break;
break;
case 3: runCommand("xdotool key XF86AudioRaiseVolume"); break;
case 1:
case 4: runCommand("xdotool key XF86AudioLowerVolume"); break;
runCommand("xdotool key XF86AudioPrev");
case 5: runCommand("xdotool key XF86AudioMute"); break;
break;
case 6: runCommand("xdotool key XF86AudioStop"); break;
case 2:
runCommand("xdotool key XF86AudioNext");
break;
case 3:
runCommand("xdotool key XF86AudioRaiseVolume");
break;
case 4:
runCommand("xdotool key XF86AudioLowerVolume");
break;
case 5:
runCommand("xdotool key XF86AudioMute");
break;
case 6:
runCommand("xdotool key XF86AudioStop");
break;
}
}
}
} catch (Exception e)
catch (Exception e)
{
{
e.printStackTrace();
e.printStackTrace();
throw new UnsupportedOperationException(
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."
);
);
}
}
}
}
private void windowsHandler(int state)
private void windowsHandler(int state)
{
{
try
try
{
{
String exe = "\""+absolutePathToExe+"\"";
String exe = "\"" + absolutePathToExe + "\"";
switch(state)
switch (state)
{
{
case 0: runCommand(exe+" 0xB3"); break;
case 0:
case 1: runCommand(exe+" 0xB1"); break;
runCommand(exe + " 0xB3");
case 2: runCommand(exe+" 0xB0"); break;
break;
case 3: runCommand(exe+" 0xAF"); break;
case 1:
case 4: runCommand(exe+" 0xAE"); break;
runCommand(exe + " 0xB1");
case 5: runCommand(exe+" 0xAD"); break;
break;
case 6: runCommand(exe+" 0xB2"); break;
case 2:
runCommand(exe + " 0xB0");
break;
case 3:
runCommand(exe + " 0xAF");
break;
case 4:
runCommand(exe + " 0xAE");
break;
case 5:
runCommand(exe + " 0xAD");
break;
case 6:
runCommand(exe + " 0xB2");
break;
}
}
}
} catch (Exception e)
catch (Exception e)
{
{
e.printStackTrace();
e.printStackTrace();
throw new UnsupportedOperationException(
throw new UnsupportedOperationException(
"Internal Error occurred. Check logs, stacktrace. Report to us."
"Internal Error occurred. Check logs, stacktrace. Report to us."
);
);
}
}
}
}
private void othersHandler()
private void othersHandler()
{
{
throw new UnsupportedOperationException(
throw new UnsupportedOperationException(
"Media Keys arent supported on this platform yet. Check out the supported platforms by clicking the '?' button on Plugins Pane"
"Media Keys arent supported on this platform yet. Check out the supported platforms by clicking the '?' button on Plugins Pane"
);
);
}
}
}
}