Author: ks156
Date: 2009-01-14 16:43:48 +0100 (Wed, 14 Jan 2009)
New Revision: 3449
Modified:
software_suite_v2/software/gadgets/tuxdroid-gadget-xmms/trunk/tuxdroid-gadget-xmms/sources/xmms.java
Log:
* Added a PID checker to be able to stop the gadget when XMMS is closed by the
user.
Modified:
software_suite_v2/software/gadgets/tuxdroid-gadget-xmms/trunk/tuxdroid-gadget-xmms/sources/xmms.java
===================================================================
---
software_suite_v2/software/gadgets/tuxdroid-gadget-xmms/trunk/tuxdroid-gadget-xmms/sources/xmms.java
2009-01-14 15:32:34 UTC (rev 3448)
+++
software_suite_v2/software/gadgets/tuxdroid-gadget-xmms/trunk/tuxdroid-gadget-xmms/sources/xmms.java
2009-01-14 15:43:48 UTC (rev 3449)
@@ -21,9 +21,12 @@
*/
import java.io.IOException;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
import com.kysoh.tuxdroid.gadget.framework.gadget.SimpleGadget;
import com.kysoh.tuxdroid.gadget.framework.gadget.SimpleGadgetConfiguration;
+
import com.tuxisalive.api.TuxAPI;
import com.tuxisalive.api.TuxAPIConst;
@@ -50,25 +53,47 @@
*/
public int startXMMS(String path)
{
- try
+ /* Check if the process is already active */
+ if (! isProcess("xmms"))
{
- if(path == null)
+ /* If not, start if */
+ try
{
- Runtime.getRuntime().exec("xmms");
+ if(path == null)
+ Runtime.getRuntime().exec("xmms");
+ else
+ Runtime.getRuntime().exec(new String[] { "xmms",
path});
+
+ return 0;
}
- else
- {
- Runtime.getRuntime().exec(new String[] { "xmms", path});
- }
- return 0;
+ catch (java.io.IOException e)
+ {
+ throwMessage("The gadget could not find XMMS. For using
this gadget, please make sure you have XMMS installed.");
+ return -1;
+ }
}
- catch (java.io.IOException e)
+ /* Otherwise, return 0 : all is OK */
+ else
{
- throwMessage("The gadget could not find XMMS. For using this
gadget, please make sure you have XMMS installed.");
- return -1;
+ return 0;
}
}
+ /**
+ * Remote control 'standby' button pushed callback.
+ * @param value
+ * @param delay
+ */
+ public void onStandbyPushed(String value, Double delay)
+ {
+
+ if(this.configuration().quit)
+ {
+ new CommandThread("xmms --quit").start();
+ }
+ this.tux.destroy();
+ System.exit(0);
+ }
/**
* Remote control 'next' button callback.
@@ -76,17 +101,16 @@
* @param delay
*/
public void onNextPushed(String value, Double delay){
- new CommandThread("xmms -f").start();
+ this.evalCommand("xmms -f");
}
-
/**
* Remote control 'previous' button pushed callback.
* @param value
* @param delay
*/
public void onPreviousPushed(String value, Double delay){
- new CommandThread("xmms -r").start();
+ this.evalCommand("xmms -r");
}
@@ -96,7 +120,7 @@
* @param delay
*/
public void onOKPushed(String value, Double delay){
- new CommandThread("xmms --play-pause").start();
+ this.evalCommand("xmms --play-pause");
}
@@ -106,7 +130,7 @@
* @param delay
*/
public void onPlayPausePushed(String value, Double delay){
- new CommandThread("xmms --play-pause").start();
+ this.evalCommand("xmms --play-pause");
}
@@ -120,20 +144,6 @@
}
- /**
- * Remote control 'standby' button pushed callback.
- * @param value
- * @param delay
- */
- public void onStandbyPushed(String value, Double delay)
- {
- if(this.configuration().quit)
- {
- new CommandThread("xmms --quit").start();
- }
- this.tux.destroy();
- System.exit(0);
- }
/**
@@ -142,7 +152,7 @@
* @param delay
*/
public void onPlayerNextPushed(String value, Double delay){
- new CommandThread("xmms --fwd").start();
+ this.evalCommand("xmms --fwd");
}
@@ -152,7 +162,7 @@
* @param delay
*/
public void onPlayerPreviousPushed(String value, Double delay){
- new CommandThread("xmms --rew").start();
+ this.evalCommand("xmms --rew");
}
@@ -177,6 +187,7 @@
this.tux.button.left.registerEventOnPressed(this,
"onNextPushed");
this.tux.server.autoConnect(TuxAPIConst.CLIENT_LEVEL_FREE,
"tuxdroid-gadget-xmms", "tuxdroid-gadget-xmms-2120");
+ /* Start XMMS */
if(this.configuration().userPlaylist)
{
ret = this.startXMMS(this.configuration().path);
@@ -187,9 +198,11 @@
}
if (ret < 0)
{
- this.tux.destroy();
System.exit(1);
}
+
+ /* Start a thread to monitor the instance of XMMS */
+ new InstanceChecker("xmms").start();
}
@@ -197,6 +210,56 @@
new xmms().boot(args, new XMMSConfiguration());
}
+ /**
+ * Evaluate if the command can be executed.
+ * If the process isn't running, the command shouldn't be sent, and the
+ * gadgt must be destroyed.
+ * @param command The command to send
+ */
+ private void evalCommand(String command)
+ {
+ if (isProcess("xmms"))
+ new CommandThread(command).start();
+ else
+ System.exit(0);
+ }
+
+ /**
+ * Check for a process presence
+ * @param process The process to find
+ * @return true if the process exists
+ */
+ private static boolean isProcess(String process)
+ {
+ try {
+ // Retrieve the unix process
+ Process p = Runtime.getRuntime().exec("ps -e");
+
+ //Create a buffer to read the command output
+ BufferedReader stdInput = new BufferedReader(new
+ InputStreamReader(p.getInputStream()));
+
+ String s;
+ int count = 0;
+ while ((s = stdInput.readLine()) != null)
+ {
+ count++;
+ // Search for the process
+ if (s.contains(process))
+ {
+ stdInput.close();
+ return true;
+ }
+ }
+ stdInput.close();
+ return false;
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ return false;
+ }
+ }
/**
* Class that create a thread object for starting a system command.
@@ -249,4 +312,45 @@
}
}
+ /**
+ * Spy an instance
+ * This class check every second if the process to watch is alive.
+ * When the process die, the gadget stops its execution.
+ */
+ private class InstanceChecker extends Thread
+ {
+ String process = null;
+
+ /**
+ * Register a process to spy
+ * @param process
+ */
+ public InstanceChecker(String process)
+ {
+ this.process = process;
+ }
+
+ /**
+ * Mainloop :
+ * Each second, the process is searched. If it's not running,
+ * the gadget is killed.
+ */
+ public void run()
+ {
+ while (true)
+ {
+ if (!xmms.isProcess(process))
+ {
+ System.exit(0);
+ break;
+ }
+ try
+ {
+ Thread.sleep(1000);
+ }
+ catch (java.lang.InterruptedException e)
+ { }
+ }
+ }
+ }
}
------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn