WindowsApplication systeem gedeeltelijk verhuisd naar Windows class in het utils pakket.

This commit is contained in:
2011-02-22 21:03:57 +00:00
parent 88c849ff89
commit 3c1f96db46
5 changed files with 58 additions and 46 deletions

View File

@@ -2,25 +2,28 @@ package pm.application.windows;
import java.io.IOException;
import com.eaio.nativecall.IntCall;
import com.eaio.nativecall.NativeCall;
import pm.Application;
import pm.exception.application.ApplicationExitException;
import pm.exception.application.ApplicationInitialiseException;
import pm.exception.application.windows.SendCommandException;
import pm.exception.application.windows.SendKeyException;
import pm.util.Native;
import pm.util.VBScript;
import pm.util.Windows;
import com.eaio.nativecall.IntCall;
import com.eaio.nativecall.NativeCall;
abstract public class WindowsApplication extends Application {
protected final static int TERMINATE_SLEEP = 500;
protected final static int START_SLEEP = 500;
protected final static String REGISTRY = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths";
protected final static int WM_COMMAND = 0x0111;
protected final static int WM_APPCOMMAND = 0x0319;
protected String program;
protected String title;
protected String name;
protected String target;
@@ -39,39 +42,32 @@ abstract public class WindowsApplication extends Application {
}
}
public WindowsApplication(String program, String name, String target) {
public WindowsApplication(String program, String title, String name) {
this.program = program;
this.title = title;
this.name = name;
this.target = target;
handle = -1;
}
public void initialise() throws ApplicationInitialiseException {
try {
if (VBScript.isRunning(program)) {
handle = Native.getHandle(name);
if (handle < 0) {
while (VBScript.isRunning(program)) {
VBScript.terminate(program);
sleep(TERMINATE_SLEEP);
}
}
}
if (handle < 0) {
process = Runtime.getRuntime().exec(target);
while (!VBScript.isRunning(program)) {
sleep(START_SLEEP);
}
IntCall findWindow = new IntCall("user32", "FindWindowA");
handle = findWindow.executeCall(new Object[] {null, name});
}
} catch (IOException e) {}
if (handle < 1) {
throw new ApplicationInitialiseException();
}
sendMessage = new IntCall("user32", "SendMessageA");
postMessage = new IntCall("user32", "PostMessageA");
mapVirtualKey = new IntCall("user32", "MapVirtualKeyA");
handle = Windows.findWindow(name, null);
if (handle < 1) {
String key = String.format("%s\\%s", REGISTRY, program);
String path = Native.getValue(key);
try {
String command = String.format("\"%s\"", path);
process = Runtime.getRuntime().exec(command);
sleep(START_SLEEP);
handle = Windows.findWindow(name, null);
} catch (IOException e) {}
}
if (handle < 1) {
throw new ApplicationInitialiseException();
}
}
public void exit() throws ApplicationExitException {

View File

@@ -9,21 +9,20 @@ import pm.exception.application.windows.SendCommandException;
import pm.exception.application.windows.SendKeyException;
public class GomPlayerApplication extends WindowsApplication {
protected final static String REGISTRY = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Clients\\Media\\GomPlayer\\shell\\open\\command";
protected final static String PROGRAM = "GOM.exe";
protected final static String NAME = "GOM Player";
protected final static String TITLE = "GOM Player";
protected final static String NAME = "GomPlayer1.x";
public GomPlayerApplication() {
super(PROGRAM, NAME, "C:\\Program Files (x86)\\GRETECH\\GomPlayer\\GOM.exe");
super(PROGRAM, TITLE, NAME);
}
public void action(Action action) {
System.out.println(handle);
System.out.println("GomPlayerApplication: " + action);
//http://www.keyxl.com/aaa0602/267/GOM-Player-keyboard-shortcuts.htm
try {
switch (action) {
case PLAY:
System.out.println("spacie " + handle);
command(0x800C);
break;
case NEXT:

View File

@@ -1,9 +1,5 @@
package pm.application.windows.wmp;
import java.util.prefs.Preferences;
import com.eaio.nativecall.IntCall;
import pm.Action;
import pm.application.windows.Command;
import pm.application.windows.WindowsApplication;
@@ -11,17 +7,12 @@ import pm.exception.application.ApplicationInitialiseException;
import pm.exception.application.windows.SendCommandException;
public class WMPApplication extends WindowsApplication {
protected final static String PATH = "C:\\Program Files (x86)\\Windows Media Player\\";
protected final static String PROGRAM = "wmplayer.exe";
protected final static String NAME = "Windows Media Player";
protected final static String TITLE = "Windows Media Player";
protected final static String NAME = "WMPlayerApp";
public WMPApplication() {
super(PATH, PROGRAM, NAME);
}
public void initialise() throws ApplicationInitialiseException {
super.initialise();
//RegistryKey;
super(PROGRAM, TITLE, NAME);
}
public void action(Action action) {

View File

@@ -37,7 +37,8 @@ public class Native {
}
public static String getValue(String key, String name) throws IOException {
Process process = Runtime.getRuntime().exec("reg query " + key);
String command = String.format("reg query \"%s\"", key);
Process process = Runtime.getRuntime().exec(command);
Scanner processScanner = new Scanner(process.getInputStream());
try {
processScanner.nextLine();

View File

@@ -0,0 +1,25 @@
package pm.util;
import com.eaio.nativecall.IntCall;
public class Windows {
protected static IntCall findWindow;
protected static IntCall sendMessage;
protected static IntCall postMessage;
protected static IntCall mapVirtualKey;
static {
findWindow = new IntCall("user32", "FindWindowA");
sendMessage = new IntCall("user32", "SendMessageA");
postMessage = new IntCall("user32", "PostMessageA");
mapVirtualKey = new IntCall("user32", "MapVirtualKeyA");
}
public static int findWindow(String className, String windowName) {
return findWindow.executeCall(new Object[] {className, windowName});
}
public static boolean postMessage(int handle, int message, int wParam, int lParam) {
return postMessage.executeBooleanCall(new Object[] {handle, message, wParam, lParam});
}
}