summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormanzerbredes <loic.guegan_secondary@yahoo.fr>2016-03-29 08:28:01 +0200
committermanzerbredes <loic.guegan_secondary@yahoo.fr>2016-03-29 08:28:01 +0200
commitfee23aed22ebdc81eb4d0d807f8d17712a71bb43 (patch)
tree29a413a567e3469d09795772c81754552bc7fca5
parentd2115332a74eeac45c6324f4b41338b5a655de04 (diff)
Change project structure and driver modularity
-rw-r--r--src/app/App.java7
-rw-r--r--src/client/MainWindow.java22
-rw-r--r--src/drivers/Driver.java25
-rw-r--r--src/drivers/DriverManager.java48
-rw-r--r--src/drivers/DriverTypeA.java2
-rw-r--r--src/drivers/Driver_1770_ff00.java (renamed from src/drivers/Device_1770_ff00.java)41
6 files changed, 117 insertions, 28 deletions
diff --git a/src/app/App.java b/src/app/App.java
index c509962..8de6d69 100644
--- a/src/app/App.java
+++ b/src/app/App.java
@@ -24,7 +24,8 @@ public class App
public void parseArguments(){
if(this.leftColor!=null || this.rightColor!=null || this.middleColor!=null){
try {
- DriverTypeA device=new Device_1770_ff00();
+ DriverTypeA device=new Driver_1770_ff00();
+
if(this.leftColor==null)
this.leftColor="OFF";
if(this.middleColor==null)
@@ -35,7 +36,7 @@ public class App
device.setRegionColor(Region.MIDDLE, Color.valueOf(middleColor), Intensity.HIGH);
device.setRegionColor(Region.RIGHT, Color.valueOf(rightColor), Intensity.HIGH);
- } catch (InstantiationException e) {
+ } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
@@ -51,6 +52,6 @@ public class App
parser.parseArgument(args);
app.parseArguments();
new MainWindow();
-
+
}
}
diff --git a/src/client/MainWindow.java b/src/client/MainWindow.java
index b60b9ec..1f24f64 100644
--- a/src/client/MainWindow.java
+++ b/src/client/MainWindow.java
@@ -23,16 +23,26 @@ public class MainWindow extends JFrame {
private JComboBox<Color> right;
private JButton apply=new JButton("Apply");
- private DriverTypeA keyboard;
+ private DriverTypeA keyboardTypeA;
+ private Class<?> driverType;
+ private DriverManager drvMan=new DriverManager();
public MainWindow() throws InstantiationException{
this.initUI();
- this.keyboard=new Device_1770_ff00();
+ Driver drv=drvMan.getDevice();
+ if(drv==null){
+ System.err.println("No driver avalaible (try as root)");
+ System.exit(1);
+ }
+ else{
+ this.driverType=drv.getType();
+ this.keyboardTypeA=(DriverTypeA) drv;
+ }
+
this.left=new JComboBox<>(Color.values());
this.middle=new JComboBox<>(Color.values());
this.right=new JComboBox<>(Color.values());
-
this.apply.addActionListener(new ActionListener() {
@Override
@@ -41,9 +51,9 @@ public class MainWindow extends JFrame {
Color middleRegion=(Color) middle.getSelectedItem();
Color rightRegion=(Color) right.getSelectedItem();
- keyboard.setRegionColor(Region.LEFT, leftRegion, Intensity.HIGH);
- keyboard.setRegionColor(Region.MIDDLE, middleRegion, Intensity.HIGH);
- keyboard.setRegionColor(Region.RIGHT, rightRegion, Intensity.HIGH);
+ keyboardTypeA.setRegionColor(Region.LEFT, leftRegion, Intensity.HIGH);
+ keyboardTypeA.setRegionColor(Region.MIDDLE, middleRegion, Intensity.HIGH);
+ keyboardTypeA.setRegionColor(Region.RIGHT, rightRegion, Intensity.HIGH);
}
diff --git a/src/drivers/Driver.java b/src/drivers/Driver.java
new file mode 100644
index 0000000..d362d19
--- /dev/null
+++ b/src/drivers/Driver.java
@@ -0,0 +1,25 @@
+package org.manzerbredes.open_klm.drivers;
+
+
+/**
+ * Driver must implement this interface
+ *
+ * @author Manzerbredes
+ *
+ */
+public interface Driver{
+
+ /**
+ * Get the type of the driver
+ *
+ * @return class that represent the type of the driver (DriverTypeA.class for example)
+ */
+ public Class<?> getType();
+
+ /**
+ * Initialise the driver (do not initialise anything in the constructor).
+ *
+ * @return true if success (device is present and accessible) false else.
+ */
+ public boolean initDriver();
+} \ No newline at end of file
diff --git a/src/drivers/DriverManager.java b/src/drivers/DriverManager.java
new file mode 100644
index 0000000..9a3c37c
--- /dev/null
+++ b/src/drivers/DriverManager.java
@@ -0,0 +1,48 @@
+package org.manzerbredes.open_klm.drivers;
+
+
+/**
+ * Driver Manager
+ *
+ * @author Manzerbredes
+ *
+ */
+public class DriverManager{
+
+ /**
+ * List of avalaible drivers
+ */
+ private Class<?>[] drivers={
+ Driver_1770_ff00.class
+ };
+
+
+
+ /**
+ * Get a successfully loaded driver
+ *
+ * @return Driver the loaded driver.
+ */
+ public Driver getDevice(){
+ // Walk on driver list
+ for(int i=0;i<this.drivers.length;i++){
+ // Try to load each drivers
+ try {
+ Driver drv=(Driver) this.drivers[i].newInstance();
+ // If success return it
+ if(drv.initDriver())
+ return drv;
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ // If no driver avalaible
+ return null;
+ }
+
+
+
+
+
+
+} \ No newline at end of file
diff --git a/src/drivers/DriverTypeA.java b/src/drivers/DriverTypeA.java
index 37e8d4c..e7048df 100644
--- a/src/drivers/DriverTypeA.java
+++ b/src/drivers/DriverTypeA.java
@@ -1,7 +1,7 @@
package org.manzerbredes.open_klm.drivers;
/**
- * Basic driver with basic functionalities.
+ * Driver of type A (driver with is own functionalities).
*
* @author Manzerbredes
*
diff --git a/src/drivers/Device_1770_ff00.java b/src/drivers/Driver_1770_ff00.java
index 77ee461..9f849f9 100644
--- a/src/drivers/Device_1770_ff00.java
+++ b/src/drivers/Driver_1770_ff00.java
@@ -14,7 +14,7 @@ import com.codeminders.hidapi.*;
* @author Manzerbredes
*
*/
-public class Device_1770_ff00 implements DriverTypeA{
+public class Driver_1770_ff00 implements Driver, DriverTypeA{
@@ -32,17 +32,10 @@ public class Device_1770_ff00 implements DriverTypeA{
* Define Keyboard mode state
*/
private Mode mode=Mode.NORMAL;
-
-
-
-
-
- /**
- * Init driver and HIDAPI library
- *
- * @throws InstantiationException
- */
- public Device_1770_ff00() throws InstantiationException{
+
+
+ @Override
+ public boolean initDriver(){
// Init HIDAPI Library
com.codeminders.hidapi.ClassPathLibraryLoader.loadNativeHIDLibrary();
@@ -50,13 +43,13 @@ public class Device_1770_ff00 implements DriverTypeA{
try {
HIDManager man=HIDManager.getInstance();
this.device=man.openById(0x1770, 0xff00, null);
- if(this.device==null)
- throw new Exception();
+ if(this.device!=null)
+ return true;
}
catch(Exception e){
- throw new InstantiationException("Failed to instanciate driver. Device not found or permission denied (try as root)");
+ System.err.println(e.getMessage());
}
-
+ return false;
}
@@ -90,7 +83,9 @@ public class Device_1770_ff00 implements DriverTypeA{
-
+ /**
+ * Commit (apply current mode to update the color)
+ */
private void commit(){
try {
this.device.sendFeatureReport(this.getReport(1,2,65,this.mode.intValue(),0,0,0,236));
@@ -135,5 +130,15 @@ public class Device_1770_ff00 implements DriverTypeA{
this.mode=mode;
this.commit();
}
-
+
+
+ @Override
+ public Class<?> getType() {
+ return DriverTypeA.class;
+ }
+
+
+
+
+
} \ No newline at end of file