diff --git a/resources/release/open-klm.jar b/resources/release/open-klm.jar index fe1be9f..4948a28 100644 Binary files a/resources/release/open-klm.jar and b/resources/release/open-klm.jar differ diff --git a/src/app/App.java b/src/app/App.java index b71c5bc..186202c 100644 --- a/src/app/App.java +++ b/src/app/App.java @@ -1,74 +1,44 @@ package org.manzerbredes.open_klm.app; -import java.io.ByteArrayOutputStream; -import java.io.IOException; - -import javax.sound.sampled.AudioFormat; -import javax.sound.sampled.AudioSystem; -import javax.sound.sampled.DataLine; -import javax.sound.sampled.LineUnavailableException; -import javax.sound.sampled.SourceDataLine; -import javax.sound.sampled.TargetDataLine; - -import org.manzerbredes.open_klm.args.ArgsTypeA; +import org.manzerbredes.open_klm.args.ArgsManager; import org.manzerbredes.open_klm.client.MainWindow; import org.manzerbredes.open_klm.drivers.*; -import org.manzerbredes.open_klm.drivers.DriverTypeA.*; /** * Hello world! * */ public class App -{/* - @Option(name="-leftColor",usage="Set the left color") - public String leftColor=null; - @Option(name="-middleColor",usage="Set the middle color") - public String middleColor=null; - @Option(name="-rightColor",usage="Set the right color") - public String rightColor=null; - - public void parseArguments(){ - if(this.leftColor!=null || this.rightColor!=null || this.middleColor!=null){ - try { - DriverTypeA device=new Driver_1770_ff00(); - - if(this.leftColor==null) - this.leftColor="OFF"; - if(this.middleColor==null) - this.middleColor="OFF"; - if(this.rightColor==null) - this.rightColor="OFF"; - device.setRegionColor(Region.LEFT, Color.valueOf(leftColor), Intensity.HIGH); - device.setRegionColor(Region.MIDDLE, Color.valueOf(middleColor), Intensity.HIGH); - device.setRegionColor(Region.RIGHT, Color.valueOf(rightColor), Intensity.HIGH); - - } catch (Exception e) { +{ + public static void main( String[] args ) + { + // Get driver + DriverManager driverManager=new DriverManager(); + Driver aDriver=driverManager.getDevice(); + + // If a driver is found run the program + if(aDriver!=null){ + + // Parse argument + ArgsManager argsManager=new ArgsManager(); + argsManager.parse(aDriver, args); + + // Else run GUI + try { + MainWindow mw = new MainWindow(aDriver); + } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } - System.exit(0); - } - - }*/ - - public static void main( String[] args ) - { + } + else{ + // Exit with error + System.err.println("No driver avalaible for your system. Try as root !"); + System.exit(1); + } - /*app.parseArguments(); - new MainWindow();*/ - /*DriverTypeA device=new Driver_1770_ff00(); - Driver a=(Driver) device; - a.initDriver(); - device.setRegionColor(Region.LEFT, Color.RED, Intensity.HIGH); - device.setRegionColor(Region.MIDDLE, Color.ORANGE, Intensity.HIGH); - device.setRegionColor(Region.RIGHT, Color.PURPLE, Intensity.HIGH); - device.setMode(Mode.WAVE);*/ - System.out.println("Parsing... "+ args.length); - ArgsTypeA a=new ArgsTypeA(); - Driver aa=new Driver_1770_ff00(); - aa.initDriver(); - a.applyAndExit(aa, args); + + } } diff --git a/src/args/ArgsManager.java b/src/args/ArgsManager.java index 20eb39b..332553c 100644 --- a/src/args/ArgsManager.java +++ b/src/args/ArgsManager.java @@ -1,5 +1,48 @@ package org.manzerbredes.open_klm.args; -public interface ArgsManager{ +import org.manzerbredes.open_klm.drivers.Driver; +import org.manzerbredes.open_klm.drivers.DriverTypeA; +import org.manzerbredes.open_klm.drivers.Driver_1770_ff00; + +/** + * + * Arguments manager + * + * @author Manzerbredes + * + */ +public class ArgsManager{ + + /** + * List of Arguments Parser available + */ + private Class[] parsers={ + ArgsTypeA.class + }; + + + /** + * + * Parse the arguments and exit + * + * @param aDriver driver used to parse + * @param args arguments to parse + */ + public void parse(Driver aDriver, String[] args){ + if(args.length>0){ + for(Class argsParser : this.parsers){ + ArgsParser parser; + try { + parser = (ArgsParser) argsParser.newInstance(); + if(parser.getType().equals(aDriver.getType())){ + parser.applyAndExit(aDriver, args); + } + } catch (Exception e){ + System.err.println(e.getMessage()); + System.exit(1); + } + } + } + } } \ No newline at end of file diff --git a/src/args/Args.java b/src/args/ArgsParser.java similarity index 57% rename from src/args/Args.java rename to src/args/ArgsParser.java index b614189..cf77fa4 100644 --- a/src/args/Args.java +++ b/src/args/ArgsParser.java @@ -8,9 +8,16 @@ import org.manzerbredes.open_klm.drivers.Driver; * @author Manzerbredes * */ -public interface Args{ +public interface ArgsParser{ /** * Apply parsed argument and exit the program */ public void applyAndExit(Driver aDriver, String[] args); + + /** + * Get the type of the driver the parser should parse + * + * @return class that represent the type of the driver (DriverTypeA.class for example) + */ + public Class getType(); } \ No newline at end of file diff --git a/src/args/ArgsTypeA.java b/src/args/ArgsTypeA.java index 1eabf46..3a95109 100644 --- a/src/args/ArgsTypeA.java +++ b/src/args/ArgsTypeA.java @@ -16,7 +16,7 @@ import joptsimple.OptionSet; * @author Manzerbredes * */ -public class ArgsTypeA implements Args{ +public class ArgsTypeA implements ArgsParser{ /** * Define Keyboard primary color state */ @@ -129,5 +129,11 @@ public class ArgsTypeA implements Args{ } driver.setMode(this.mode); } + + + @Override + public Class getType() { + return DriverTypeA.class; + } } \ No newline at end of file diff --git a/src/client/MainWindow.java b/src/client/MainWindow.java index 15baf4c..1147592 100644 --- a/src/client/MainWindow.java +++ b/src/client/MainWindow.java @@ -28,16 +28,15 @@ public class MainWindow extends JFrame { private Class driverType; private DriverManager drvMan=new DriverManager(); - public MainWindow() throws InstantiationException{ + public MainWindow(Driver aDriver) throws InstantiationException{ this.initUI(); - Driver drv=drvMan.getDevice(); - if(drv==null){ + if(aDriver==null){ System.err.println("No driver avalaible (try as root)"); System.exit(1); } else{ - this.driverType=drv.getType(); - this.keyboardTypeA=(DriverTypeA) drv; + this.driverType=aDriver.getType(); + this.keyboardTypeA=(DriverTypeA) aDriver; } this.left=new JComboBox<>(Color.values());