Change project structure and driver modularity
This commit is contained in:
parent
d2115332a7
commit
fee23aed22
6 changed files with 117 additions and 28 deletions
|
@ -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();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
||||
}
|
||||
|
|
25
src/drivers/Driver.java
Normal file
25
src/drivers/Driver.java
Normal file
|
@ -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();
|
||||
}
|
48
src/drivers/DriverManager.java
Normal file
48
src/drivers/DriverManager.java
Normal file
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -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
|
||||
*
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue