Mimis Native functionaliteit overgebracht op JNI.

This commit is contained in:
2011-07-17 20:36:51 +00:00
parent 2733693273
commit 395a22bc5a
20 changed files with 94 additions and 220 deletions

View File

@@ -16,7 +16,7 @@ import org.apache.commons.logging.LogFactory;
public class Manager<T extends Worker & Titled & Exitable> extends Worker {
protected Log log = LogFactory.getLog(getClass());
protected static final long serialVersionUID = 1L;
protected static final int INTERVAL = 500;
protected static final int INTERVAL = 1000;
protected T[] manageableArray;
protected Map<T, ManageButton<T>> buttonMap;

View File

@@ -14,6 +14,7 @@ public abstract class Worker implements Runnable {
protected boolean run = false;
protected boolean active = false;
protected int work = 0;
public void start(boolean thread) {
run = true;
@@ -42,6 +43,7 @@ public abstract class Worker implements Runnable {
synchronized (this) {
notifyAll();
}
log.debug(String.format("%s: %d", getClass(), work));
}
protected void sleep(int time) {
@@ -86,6 +88,7 @@ public abstract class Worker implements Runnable {
public final void run() {
while (run) {
if (active()) {
++work;
work();
} else {
try {

View File

@@ -6,8 +6,6 @@ import mimis.Application;
import mimis.exception.worker.ActivateException;
import mimis.exception.worker.DeactivateException;
import mimis.util.Native;
import mimis.util.VBScript;
public abstract class CMDApplication extends Application {
protected final static String REGISTRY = "HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths";
@@ -43,11 +41,7 @@ public abstract class CMDApplication extends Application {
}
public boolean active() {
try {
return active = VBScript.isRunning(program);
} catch (IOException e) {
return active = false;
}
return active = Native.isRunning(program);
}
public void deactivate() throws DeactivateException {

View File

@@ -1,12 +1,9 @@
package mimis.application.cmd.windows;
import java.io.IOException;
import mimis.application.cmd.CMDApplication;
import mimis.exception.worker.ActivateException;
import mimis.exception.worker.DeactivateException;
import mimis.util.VBScript;
import mimis.util.Windows;
import mimis.util.Native;
import mimis.value.Command;
import mimis.value.Key;
import mimis.value.Type;
@@ -15,61 +12,64 @@ public abstract class WindowsApplication extends CMDApplication {
protected final static int TERMINATE_SLEEP = 500;
protected final static int START_SLEEP = 500;
protected String name;
public static final int WM_CLOSE = 0x0010;
public static final int WM_COMMAND = 0x0111;
public static final int WM_APPCOMMAND = 0x0319;
public static final int WM_USER = 0x0400;
public static final int MAPVK_VK_TO_VSC = 0;
protected String window;
protected Process process;
protected int handle;
public WindowsApplication(String program, String title, String name) {
public WindowsApplication(String program, String title, String window) {
super(program, title);
this.name = name;
handle = -1;
this.window = window;
handle = 0;
}
public void activate() throws ActivateException {
super.activate();
handle = Windows.findWindow(name, null);
handle = Native.getHandle(window);
if (handle < 1) {
sleep(START_SLEEP);
handle = Windows.findWindow(name, null);
handle = Native.getHandle(window);
}
active = handle > 0;
if (handle < 1) {
if (!active) {
throw new ActivateException();
}
}
public boolean active() {
if (!active) {
handle = Windows.findWindow(name, null);
handle = Native.getHandle(window);
}
return super.active();
}
public void deactivate() throws DeactivateException {
try {
VBScript.terminate(program);
} catch (IOException e) {
log.error(e);
if (!Native.terminate(program)) {
throw new DeactivateException();
}
}
protected void command(Command command) {
Windows.sendMessage(handle, Windows.WM_APPCOMMAND, handle, command.getCode() << 16);
Native.sendMessage(handle, WM_APPCOMMAND, handle, command.getCode() << 16);
}
protected void command(int command) {
Windows.sendMessage(handle, Windows.WM_COMMAND, command, 0);
Native.sendMessage(handle, WM_COMMAND, command, 0);
}
protected int user(int wParam, int lParam) {
return Windows.sendMessage(handle, Windows.WM_USER, wParam, lParam);
return Native.sendMessage(handle, WM_USER, wParam, lParam);
//return Windows.sendMessage(handle, Windows.WM_USER + wParam, 0, 0);
}
protected void key(Type type, int code) {
int scanCode = Windows.mapVirtualKey(code, Windows.MAPVK_VK_TO_VSC);
Windows.postMessage(handle, type.getCode(), code, 1 | (scanCode << 16));
int scanCode = Native.mapVirtualKey(code, MAPVK_VK_TO_VSC);
Native.postMessage(handle, type.getCode(), code, 1 | (scanCode << 16));
}
protected void key(Type type, char character) {

View File

@@ -10,7 +10,7 @@ import mimis.value.Amount;
public class GomPlayerApplication extends WindowsApplication {
protected final static String PROGRAM = "GOM.exe";
protected final static String TITLE = "GOM Player";
protected final static String NAME = "GomPlayer1.x";
protected final static String WINDOW = "GomPlayer1.x";
protected static final int VOLUME_SLEEP = 100;
protected static final int SEEK_SLEEP = 100;
@@ -19,7 +19,7 @@ public class GomPlayerApplication extends WindowsApplication {
protected SeekWorker seekWorker;
public GomPlayerApplication() {
super(PROGRAM, TITLE, NAME);
super(PROGRAM, TITLE, WINDOW);
volumeWorker = new VolumeWorker();
seekWorker = new SeekWorker();
}

View File

@@ -9,12 +9,12 @@ import mimis.value.Action;
public class WinampApplication extends WindowsApplication {
protected final static String PROGRAM = "winamp.exe";
protected final static String TITLE = "Winamp";
protected final static String NAME = "Winamp v1.x";
protected final static String WINDOW = "Winamp v1.x";
protected final static int STATUS_PLAYING = 1;
protected final static int STATUS_PAUSED = 3;
protected final static int STATUS_STOPPED = 0;
protected final static int IPC_ISPLAYING = 104;
protected final static int IPC_GETOUTPUTTIME = 105;
protected final static int IPC_SETVOLUME = 122;
@@ -40,9 +40,9 @@ public class WinampApplication extends WindowsApplication {
protected SeekWorker seekWorker;
protected double volume;
protected boolean muted;
public WinampApplication() {
super(PROGRAM, TITLE, NAME);
super(PROGRAM, TITLE, WINDOW);
volume = getVolume();
muted = volume == 0;
volumeWorker = new VolumeWorker();

View File

@@ -9,14 +9,14 @@ import mimis.value.Action;
public class WMPApplication extends WindowsApplication {
protected final static String PROGRAM = "wmplayer.exe";
protected final static String TITLE = "Windows Media Player";
protected final static String NAME = "WMPlayerApp";
protected final static String WINDOW = "WMPlayerApp";
protected static final int VOLUME_SLEEP = 120;
protected VolumeWorker volumeWorker;
public WMPApplication() {
super(PROGRAM, TITLE, NAME);
super(PROGRAM, TITLE, WINDOW);
volumeWorker = new VolumeWorker();
}

View File

@@ -1,12 +1,10 @@
package mimis.application.itunes;
import java.io.IOException;
import mimis.Application;
import mimis.Worker;
import mimis.exception.worker.ActivateException;
import mimis.exception.worker.DeactivateException;
import mimis.util.VBScript;
import mimis.util.Native;
import mimis.value.Action;
import com.dt.iTunesController.ITCOMDisabledReason;
@@ -48,16 +46,12 @@ public class iTunesApplication extends Application implements iTunesEventsInterf
}
public boolean active() {
try {
if (!active && !quiting && VBScript.isRunning(PROGRAM)) {
try {
activate();
} catch (ActivateException e) {
log.error(e);
}
if (!active && !quiting && Native.isRunning(PROGRAM)) {
try {
activate();
} catch (ActivateException e) {
log.error(e);
}
} catch (IOException e) {
log.error(e);
}
try {
iTunes.getMute();

View File

@@ -9,7 +9,7 @@ import mimis.value.Action;
public class MPCApplication extends WindowsApplication {
protected final static String PROGRAM = "mpc-hc.exe";
protected final static String TITLE = "Media Player Classic";
protected final static String NAME = "MediaPlayerClassicW";
protected final static String WINDOW = "MediaPlayerClassicW";
protected static final int VOLUME_SLEEP = 50;
protected static final int SEEK_SLEEP = 50;
@@ -18,7 +18,7 @@ public class MPCApplication extends WindowsApplication {
protected SeekWorker seekWorker;
public MPCApplication() {
super(PROGRAM, TITLE, NAME);
super(PROGRAM, TITLE, WINDOW);
volumeWorker = new VolumeWorker();
seekWorker = new SeekWorker();
}

View File

@@ -12,7 +12,6 @@ import mimis.application.cmd.CMDApplication;
import mimis.exception.worker.ActivateException;
import mimis.exception.worker.DeactivateException;
import mimis.util.Native;
import mimis.util.VBScript;
import mimis.value.Action;
import mimis.value.Amount;
@@ -41,7 +40,7 @@ public class VLCApplication extends CMDApplication {
volumeWorker = new VolumeWorker();
seekWorker = new SeekWorker();
}
public String getPath() {
Pattern pattern = Pattern.compile("\"([^\"]+)\"");
Matcher matcher = pattern.matcher(Native.getValue(REGISTRY));
@@ -66,11 +65,7 @@ public class VLCApplication extends CMDApplication {
super.stop();
volumeWorker.stop();
seekWorker.stop();
try {
VBScript.terminate(program);
} catch (IOException e) {
log.error(e);
}
Native.terminate(program);
}
public void begin(Action action) {

View File

@@ -1,6 +1,5 @@
package mimis.device.lirc;
import java.io.IOException;
import java.util.HashMap;
import mimis.Button;
@@ -12,7 +11,7 @@ import mimis.exception.worker.DeactivateException;
import mimis.sequence.state.Press;
import mimis.sequence.state.Release;
import mimis.util.Multiplexer;
import mimis.util.VBScript;
import mimis.util.Native;
import mimis.util.multiplexer.SignalListener;
import mimis.value.Signal;
@@ -56,16 +55,12 @@ public class LircDevice extends Device implements LircButtonListener, SignalList
log.error(e);
}
} else if (!active) {
try {
if (VBScript.isRunning(PROGRAM)) {
try {
activate();
} catch (ActivateException e) {
log.error(e);
}
if (Native.isRunning(PROGRAM)) {
try {
activate();
} catch (ActivateException e) {
log.error(e);
}
} catch (IOException e) {
log.error(e);
}
}
return active;

View File

@@ -44,7 +44,7 @@ public class WiimoteDevice extends Device implements GestureListener {
public WiimoteDevice() {
super(TITLE);
eventMapCycle = new WiimoteEventMapCycle();
//wiimoteDiscovery = new WiimoteDiscovery(this);
wiimoteDiscovery = new WiimoteDiscovery(this);
gestureDevice = new GestureDevice();
gestureDevice.add(this);
gestureId = 0;
@@ -53,7 +53,7 @@ public class WiimoteDevice extends Device implements GestureListener {
/* Worker */
public void activate() throws ActivateException {
//connect();
connect();
try {
wiimote = wiimoteService.getDevice(this);
ledWorker.activate();
@@ -65,15 +65,15 @@ public class WiimoteDevice extends Device implements GestureListener {
super.activate();
}
/*public boolean active() {
public boolean active() {
if (wiimote != null) {
/*if (!ledWorker.active()) {
if (!ledWorker.active()) {
try {
ledWorker.activate();
} catch (ActivateException e) {
log.error(e);
}
}*
}
connected = false;
wiimote.getStatus();
synchronized (this) {
@@ -93,16 +93,16 @@ public class WiimoteDevice extends Device implements GestureListener {
}
}
return active;
}*/
}
public void stop() {
super.stop();
ledWorker.stop();
/*if (wiimote != null) {
disconnect();
}
wiimoteService.exit();*/
//wiimoteDiscovery.stop();
}*/
wiimoteService.exit();
wiimoteDiscovery.stop();
}
/* Events */
@@ -170,7 +170,7 @@ public class WiimoteDevice extends Device implements GestureListener {
}
}
/*public void connected() {
public void connected() {
try {
wiimote = wiimoteService.getDevice(this);
try {
@@ -181,20 +181,20 @@ public class WiimoteDevice extends Device implements GestureListener {
} catch (DeviceNotFoundException e) {
log.error(e);
}
}*/
}
/*public void disconnect() {
public void disconnect() {
wiimote.disconnect();
wiimote = null;
}*/
}
/*public void disconnected() {
public void disconnected() {
try {
wiimoteDiscovery.activate();
} catch (ActivateException e) {
log.error(e);
}
}*/
}
/* Listeners */
public void onButtonsEvent(WiimoteButtonsEvent event) {
@@ -222,7 +222,7 @@ public class WiimoteDevice extends Device implements GestureListener {
System.out.printf("id #%d, prob %.0f%%, valid %b\n", event.getId(), 100 * event.getProbability(), event.isValid());
}
}
class LedWorker extends Worker {
protected ArrayCycle<Integer> ledCycle;

View File

@@ -14,6 +14,7 @@ public class WiimoteDiscovery extends Worker {
public WiimoteDiscovery(WiimoteDevice wiimoteDevice) {
this.wiimoteDevice = wiimoteDevice;
disconnect = true;
}
protected boolean connect() {
@@ -53,7 +54,6 @@ public class WiimoteDiscovery extends Worker {
public void activate() throws ActivateException {
super.activate();
disconnect = true;
}
public void deactivate() throws DeactivateException {

View File

@@ -1,48 +1,44 @@
package mimis.util;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class Native {
public static int getHandle(String title) throws IOException {
String command = String.format("list.exe w");
Process process = Runtime.getRuntime().exec(command);
Scanner scanner = new Scanner(process.getInputStream());
scanner.nextLine();
while (scanner.hasNextLine()) {
Scanner line = new Scanner(scanner.nextLine());
line.useDelimiter("\t");
try {
int handle = line.nextInt();
line.nextInt();
if (line.hasNext() && line.next().equals(title)) {
return handle;
}
} catch (InputMismatchException e) {}
}
return -1;
static {
System.loadLibrary("mimis");
}
public static String getProgram(int processId) throws IOException {
String command = String.format("list.exe p");
Process process = Runtime.getRuntime().exec(command);
Scanner scanner = new Scanner(process.getInputStream());
scanner.nextLine();
while (scanner.hasNextLine()) {
Scanner line = new Scanner(scanner.nextLine());
line.useDelimiter("\t");
public void start() {
/*int handle = getHandle("Winamp v1.x");
System.out.println(handle);
sendMessage(handle, WindowsApplication.WM_CLOSE, 0, 0);
/*/
while (true) {//Winamp v1.x
System.out.println(isRunning("winamp.exe"));
//System.out.println(new Native().terminate("winamp.exe"));
//System.out.println(new Native().running("wmplayer.exe"));
try {
if (line.nextInt() == processId) {
return line.next();
}
} catch (InputMismatchException e) {}
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
public static void main(String[] args) {
new Native().start();
}
public native static int getHandle(String window);
public native static int sendMessage(int handle, int message, int wParam, int lParam);
public native static int postMessage(int handle, int message, int wParam, int lParam);
public static native int mapVirtualKey(int code, int type);
public native static boolean isRunning(String program);
public native static boolean terminate(String program);
public static String getValue(String key, String name) {
String command = String.format("reg query \"%s\"", key);
try {

View File

@@ -1,59 +0,0 @@
package mimis.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class VBScript {
public static boolean isRunning(String program) throws IOException {
boolean found = false;
File file = File.createTempFile("vbsutils", ".vbs");
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(String.format(
"Set WshShell = WScript.CreateObject(\"WScript.Shell\")\n"
+ "Set locator = CreateObject(\"WbemScripting.SWbemLocator\")\n"
+ "Set service = locator.ConnectServer()\n"
+ "Set processes = service.ExecQuery _\n"
+ " (\"select * from Win32_Process where name='%s'\")\n"
+ "For Each process in processes\n"
+ "wscript.echo process.Name\n"
+ "Next\n"
+ "Set WSHShell = Nothing\n", program));
fileWriter.close();
Process process = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream());
BufferedReader input = new BufferedReader(inputStreamReader);
String line = input.readLine();
found = line != null && line.equals(program);
input.close();
try {
process.waitFor();
} catch (InterruptedException e) {}
file.delete();
return found;
}
public static void terminate(String program) throws IOException {
File file = File.createTempFile("vbsutils", ".vbs");
file.deleteOnExit();
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(String.format(
"Set WshShell = WScript.CreateObject(\"WScript.Shell\")\n"
+ "Set locator = CreateObject(\"WbemScripting.SWbemLocator\")\n"
+ "Set service = locator.ConnectServer()\n"
+ "Set processes = service.ExecQuery _\n"
+ " (\"select * from Win32_Process where name='%s'\")\n"
+ "For Each process in processes\n"
+ "process.Terminate()\n"
+ "Next\n"
+ "Set WSHShell = Nothing\n", program));
fileWriter.close();
Process process = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
try {
process.waitFor();
} catch (InterruptedException e) {}
file.delete();
}
}

View File

@@ -1,44 +0,0 @@
package mimis.util;
import com.eaio.nativecall.IntCall;
import com.eaio.nativecall.NativeCall;
public class Windows {
public static final int WM_COMMAND = 0x0111;
public static final int WM_APPCOMMAND = 0x0319;
public static final int MAPVK_VK_TO_VSC = 0;
public static final int WM_USER = 0x0400;
protected static IntCall findWindow;
protected static IntCall sendMessage;
protected static IntCall postMessage;
protected static IntCall mapVirtualKey;
static {
try {
NativeCall.init();
findWindow = new IntCall("user32", "FindWindowA");
sendMessage = new IntCall("user32", "SendMessageA");
postMessage = new IntCall("user32", "PostMessageA");
mapVirtualKey = new IntCall("user32", "MapVirtualKeyA");
} catch (Exception e) {
e.printStackTrace();
}
}
public static int findWindow(String className, String windowName) {
return findWindow.executeCall(new Object[] {className, windowName});
}
public static int postMessage(int handle, int message, int wParam, int lParam) {
return postMessage.executeCall(new Object[] {handle, message, wParam, lParam});
}
public static int sendMessage(int handle, int message, int wParam, int lParam) {
return sendMessage.executeCall(new Object[] {handle, message, wParam, lParam});
}
public static int mapVirtualKey(int code, int mapType) {
return mapVirtualKey.executeCall(new Object[] {code, 0});
}
}