diff --git a/WiiUseJ/src/wiiusej/WiiUseApi.java b/WiiUseJ/src/wiiusej/WiiUseApi.java index 3f169e6..f03eedc 100644 --- a/WiiUseJ/src/wiiusej/WiiUseApi.java +++ b/WiiUseJ/src/wiiusej/WiiUseApi.java @@ -51,18 +51,18 @@ public class WiiUseApi { * @return 0 if there is an error otherwise it returns the number of * wiimotes connected. */ - native int doConnections(int nb, boolean rumble); + synchronized native int doConnections(int nb, boolean rumble); /** * Close connection to the wiimote with the given id. * */ - native void closeConnection(int id); + synchronized native void closeConnection(int id); /** * Shutdown Wiiuse API. */ - native void shutdownApi(); + synchronized native void shutdownApi(); /** * Activate rumble on the wiimote with the given id. @@ -70,7 +70,7 @@ public class WiiUseApi { * @param id * the id of the wiimote. */ - native void activateRumble(int id); + synchronized native void activateRumble(int id); /** * Deactivate rumble on the wiimote with the given id. @@ -78,7 +78,7 @@ public class WiiUseApi { * @param id * the id of the wiimote. */ - native void deactivateRumble(int id); + synchronized native void deactivateRumble(int id); /** * Activate IR Tracking on the wiimote with the given id. @@ -86,7 +86,7 @@ public class WiiUseApi { * @param id * the id of the wiimote. */ - native void activateIRTracking(int id); + synchronized native void activateIRTracking(int id); /** * Deactivate IR Tracking on the wiimote with the given id. @@ -94,7 +94,7 @@ public class WiiUseApi { * @param id * the id of the wiimote. */ - native void deactivateIRTracking(int id); + synchronized native void deactivateIRTracking(int id); /** * Activate motion sensing on the wiimote with the given id. @@ -102,7 +102,7 @@ public class WiiUseApi { * @param id * the id of the wiimote. */ - native void activateMotionSensing(int id); + synchronized native void activateMotionSensing(int id); /** * Deactivate motion sensing on the wiimote with the given id. @@ -110,7 +110,7 @@ public class WiiUseApi { * @param id * the id of the wiimote. */ - native void deactivateMotionSensing(int id); + synchronized native void deactivateMotionSensing(int id); /** * Set wiimote leds status. @@ -126,7 +126,7 @@ public class WiiUseApi { * @param led4 * status of led4: True=ON, False=OFF */ - native void setLeds(int id, boolean led1, boolean led2, boolean led3, + synchronized native void setLeds(int id, boolean led1, boolean led2, boolean led3, boolean led4); /** @@ -137,7 +137,7 @@ public class WiiUseApi { * @param angle * minimum angle detected by an event */ - native void setOrientThreshold(int id, float angle); + synchronized native void setOrientThreshold(int id, float angle); /** * Set how much acceleration must change to generate an event. @@ -147,7 +147,7 @@ public class WiiUseApi { * @param value * minimum value detected by an event */ - native void setAccelThreshold(int id, int value); + synchronized native void setAccelThreshold(int id, int value); /** * Set alpha smoothing parameter for the given id. @@ -157,7 +157,7 @@ public class WiiUseApi { * @param value * alpha smoothing value */ - native void setAlphaSmoothing(int id, float value); + synchronized native void setAlphaSmoothing(int id, float value); /** * Try to resync with the wiimote by starting a new handshake. @@ -165,7 +165,7 @@ public class WiiUseApi { * @param id * id of the wiimote concerned */ - native void reSync(int id); + synchronized native void reSync(int id); /** * Make the the accelerometers give smoother results. This is set by @@ -174,7 +174,7 @@ public class WiiUseApi { * @param id * the id of the wiimote concerned */ - native void activateSmoothing(int id); + synchronized native void activateSmoothing(int id); /** * Make the the accelerometers give raw results. @@ -182,7 +182,7 @@ public class WiiUseApi { * @param id * the id of the wiimote concerned */ - native void deactivateSmoothing(int id); + synchronized native void deactivateSmoothing(int id); /** * Make the wiimote generate an event each time we poll. Not set by default. @@ -190,7 +190,7 @@ public class WiiUseApi { * @param id * the id of the wiimote concerned */ - native void activateContinuous(int id); + synchronized native void activateContinuous(int id); /** * Make the wiimote generate an event only when there is one. @@ -198,7 +198,7 @@ public class WiiUseApi { * @param id * the id of the wiimote concerned */ - native void deactivateContinuous(int id); + synchronized native void deactivateContinuous(int id); /** * Notify wiiuse that your screen has an aspect ratio of 4/3. @@ -206,7 +206,7 @@ public class WiiUseApi { * @param id * the id of the wiimote of which we want the status. */ - native void setScreenRatio43(int id); + synchronized native void setScreenRatio43(int id); /** * Notify wiiuse that your screen has an aspect ratio of 16/9. @@ -214,7 +214,7 @@ public class WiiUseApi { * @param id * the id of the wiimote of which we want the status. */ - native void setScreenRatio169(int id); + synchronized native void setScreenRatio169(int id); /** * Notify wiiuse that the sensor bar is above your screen. @@ -222,7 +222,7 @@ public class WiiUseApi { * @param id * the id of the wiimote of which we want the status. */ - native void setSensorBarAboveScreen(int id); + synchronized native void setSensorBarAboveScreen(int id); /** * Notify wiiuse that the sensor bar is below your screen. @@ -230,7 +230,7 @@ public class WiiUseApi { * @param id * the id of the wiimote of which we want the status. */ - native void setSensorBarBelowScreen(int id); + synchronized native void setSensorBarBelowScreen(int id); /** * Set virtual screen resolution. It is used to automatically compute the @@ -244,7 +244,7 @@ public class WiiUseApi { * @param y * y resolution. */ - native void setVirtualScreenResolution(int id, int x, int y); + synchronized native void setVirtualScreenResolution(int id, int x, int y); /** * Get status and values from the wiimotes and send it through callbacks. @@ -252,7 +252,7 @@ public class WiiUseApi { * @param id * the id of the wiimote of which we want the status. */ - native void getStatus(int id); + synchronized native void getStatus(int id); /** * Check for new Events and Get it. @@ -260,6 +260,6 @@ public class WiiUseApi { * @param gath * the object where we store all the new events. */ - native void specialPoll(EventsGatherer gath); + native void specialPoll(EventsGatherer gath); } diff --git a/WiiUseJ/src/wiiusej/WiiUseApiManager.java b/WiiUseJ/src/wiiusej/WiiUseApiManager.java index 4f273b4..5ca67d6 100644 --- a/WiiUseJ/src/wiiusej/WiiUseApiManager.java +++ b/WiiUseJ/src/wiiusej/WiiUseApiManager.java @@ -16,7 +16,6 @@ */ package wiiusej; -import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.atomic.AtomicBoolean; import javax.swing.event.EventListenerList; @@ -24,11 +23,6 @@ import javax.swing.event.EventListenerList; import wiiusej.wiiuseapievents.EventsGatherer; import wiiusej.wiiuseapievents.WiiUseApiEvent; import wiiusej.wiiuseapievents.WiiUseApiListener; -import wiiusej.wiiuseapirequest.FloatValueRequest; -import wiiusej.wiiuseapirequest.IntValueRequest; -import wiiusej.wiiuseapirequest.LedsRequest; -import wiiusej.wiiuseapirequest.TwoIntValueRequest; -import wiiusej.wiiuseapirequest.WiiUseApiRequest; /** * Class that manages the use of Wiiuse API. @@ -49,9 +43,6 @@ public class WiiUseApiManager extends Thread { private AtomicBoolean running = new AtomicBoolean(false); - private WiiUseApiRequestsManager requestManager = WiiUseApiRequestsManager - .getInstance(); - public static WiiUseApiManager getInstance() { return instance; } @@ -83,10 +74,8 @@ public class WiiUseApiManager extends Thread { return new Wiimote[0]; } - if (!manager.isAlive()){ + if (!manager.isAlive()) manager.start(); - manager.requestManager.start(); - } return manager.wiimotes; } @@ -122,12 +111,12 @@ public class WiiUseApiManager extends Thread { connected--; if (connected == 0) {// stop this thread if there is // no more wiimotes connected. - //stop thread + // stop thread shutdown(); } - requestManager.addRequests(new WiiUseApiRequest(id, - WiiUseApiRequest.WIIUSE_CLOSE_CONNECTION_REQUEST)); - // System.out.println("Wiimote " + id + " disconnected !"); + + /* Close connections requests */ + wiiuse.closeConnection(id); } /** @@ -160,8 +149,7 @@ public class WiiUseApiManager extends Thread { * id of the wiimote. */ public void activateRumble(int id) { - requestManager.addRequests(new WiiUseApiRequest(id, - WiiUseApiRequest.WIIUSE_ACTIVATE_RUMBLE_REQUEST)); + wiiuse.activateRumble(id); } /** @@ -171,8 +159,7 @@ public class WiiUseApiManager extends Thread { * id of the wiimote. */ public void deactivateRumble(int id) { - requestManager.addRequests(new WiiUseApiRequest(id, - WiiUseApiRequest.WIIUSE_DEACTIVATE_RUMBLE_REQUEST)); + wiiuse.deactivateRumble(id); } /** @@ -182,8 +169,7 @@ public class WiiUseApiManager extends Thread { * id of the wiimote. */ public void activateIRTRacking(int id) { - requestManager.addRequests(new WiiUseApiRequest(id, - WiiUseApiRequest.WIIUSE_ACTIVATE_IR_TRACKING_REQUEST)); + wiiuse.activateIRTracking(id); } /** @@ -193,8 +179,7 @@ public class WiiUseApiManager extends Thread { * id of the wiimote. */ public void deactivateIRTRacking(int id) { - requestManager.addRequests(new WiiUseApiRequest(id, - WiiUseApiRequest.WIIUSE_DEACTIVATE_IR_TRACKING_REQUEST)); + wiiuse.deactivateIRTracking(id); } /** @@ -204,8 +189,7 @@ public class WiiUseApiManager extends Thread { * id of the wiimote. */ public void activateMotionSensing(int id) { - requestManager.addRequests(new WiiUseApiRequest(id, - WiiUseApiRequest.WIIUSE_ACTIVATE_MOTION_SENSING_REQUEST)); + wiiuse.activateMotionSensing(id); } /** @@ -215,8 +199,7 @@ public class WiiUseApiManager extends Thread { * id of the wiimote. */ public void deactivateMotionSensing(int id) { - requestManager.addRequests(new WiiUseApiRequest(id, - WiiUseApiRequest.WIIUSE_DEACTIVATE_MOTION_SENSING_REQUEST)); + wiiuse.deactivateMotionSensing(id); } /** @@ -226,8 +209,7 @@ public class WiiUseApiManager extends Thread { * id of the wiimote. */ public void activateSmoothing(int id) { - requestManager.addRequests(new WiiUseApiRequest(id, - WiiUseApiRequest.WIIUSE_ACTIVATE_SMOOTHING_REQUEST)); + wiiuse.activateSmoothing(id); } /** @@ -237,8 +219,7 @@ public class WiiUseApiManager extends Thread { * id of the wiimote. */ public void deactivateSmoothing(int id) { - requestManager.addRequests(new WiiUseApiRequest(id, - WiiUseApiRequest.WIIUSE_DEACTIVATE_SMOOTHING_REQUEST)); + wiiuse.deactivateSmoothing(id); } /** @@ -248,8 +229,7 @@ public class WiiUseApiManager extends Thread { * id of the wiimote. */ public void activateContinuous(int id) { - requestManager.addRequests(new WiiUseApiRequest(id, - WiiUseApiRequest.WIIUSE_ACTIVATE_CONTINUOUS_REQUEST)); + wiiuse.activateContinuous(id); } /** @@ -259,8 +239,7 @@ public class WiiUseApiManager extends Thread { * id of the wiimote */ public void deactivateContinuous(int id) { - requestManager.addRequests(new WiiUseApiRequest(id, - WiiUseApiRequest.WIIUSE_DEACTIVATE_CONTINUOUS_REQUEST)); + wiiuse.deactivateContinuous(id); } /** @@ -278,8 +257,7 @@ public class WiiUseApiManager extends Thread { * status of led4. True : ON, False : OFF */ public void setLeds(int id, boolean l1, boolean l2, boolean l3, boolean l4) { - requestManager.addRequests(new LedsRequest(id, - WiiUseApiRequest.WIIUSE_LEDS_REQUEST, l1, l2, l3, l4)); + wiiuse.setLeds(id, l1, l2, l3, l4); } /** @@ -291,8 +269,7 @@ public class WiiUseApiManager extends Thread { * threshold in degrees */ public void setOrientationThreshold(int id, float th) { - requestManager.addRequests(new FloatValueRequest(id, - WiiUseApiRequest.WIIUSE_ORIENT_THRESHOLHD_REQUEST, th)); + wiiuse.setOrientThreshold(id, th); } /** @@ -304,8 +281,7 @@ public class WiiUseApiManager extends Thread { * threshold */ public void setAccelerationThreshold(int id, int th) { - requestManager.addRequests(new IntValueRequest(id, - WiiUseApiRequest.WIIUSE_ACCEL_THRESHOLHD_REQUEST, th)); + wiiuse.setAccelThreshold(id, th); } /** @@ -317,8 +293,7 @@ public class WiiUseApiManager extends Thread { * threshold */ public void setAlphaSmoothing(int id, float th) { - requestManager.addRequests(new FloatValueRequest(id, - WiiUseApiRequest.WIIUSE_ALPHA_SMOOTHING_REQUEST, th)); + wiiuse.setAlphaSmoothing(id, th); } /** @@ -328,8 +303,7 @@ public class WiiUseApiManager extends Thread { * id of the wiimote */ public void reSync(int id) { - requestManager.addRequests(new WiiUseApiRequest(id, - WiiUseApiRequest.WIIUSE_RESYNC)); + wiiuse.reSync(id); } /** @@ -339,8 +313,7 @@ public class WiiUseApiManager extends Thread { * id of the wiimote */ public void setScreenAspectRatio43(int id) { - requestManager.addRequests(new WiiUseApiRequest(id, - WiiUseApiRequest.WIIUSE_ASPECT_RATIO_4_3)); + wiiuse.setScreenRatio43(id); } /** @@ -350,8 +323,7 @@ public class WiiUseApiManager extends Thread { * id of the wiimote */ public void setScreenAspectRatio169(int id) { - requestManager.addRequests(new WiiUseApiRequest(id, - WiiUseApiRequest.WIIUSE_ASPECT_RATIO_16_9)); + wiiuse.setScreenRatio169(id); } /** @@ -361,8 +333,7 @@ public class WiiUseApiManager extends Thread { * id of the wiimote */ public void setSensorBarAboveScreen(int id) { - requestManager.addRequests(new WiiUseApiRequest(id, - WiiUseApiRequest.WIIUSE_SENSOR_BAR_ABOVE)); + wiiuse.setSensorBarAboveScreen(id); } /** @@ -372,8 +343,7 @@ public class WiiUseApiManager extends Thread { * id of the wiimote */ public void setSensorBarBelowScreen(int id) { - requestManager.addRequests(new WiiUseApiRequest(id, - WiiUseApiRequest.WIIUSE_SENSOR_BAR_BELOW)); + wiiuse.setSensorBarBelowScreen(id); } /** @@ -389,8 +359,7 @@ public class WiiUseApiManager extends Thread { * y resolution */ public void setVirtualResolution(int id, int x, int y) { - requestManager.addRequests(new TwoIntValueRequest(id, - WiiUseApiRequest.WIIUSE_SET_VIRTUAL_RESOLUTION, x, y)); + wiiuse.setVirtualScreenResolution(id, x, y); } /** @@ -400,8 +369,7 @@ public class WiiUseApiManager extends Thread { * id of the wiimote */ public void getStatus(int id) { - requestManager.addRequests(new WiiUseApiRequest(id, - WiiUseApiRequest.WIIUSE_STATUS_REQUEST)); + wiiuse.getStatus(id); } @Override @@ -417,9 +385,7 @@ public class WiiUseApiManager extends Thread { while (running.get() && connected > 0) { /* Polling */ - synchronized (wiiuse) { - wiiuse.specialPoll(gather); - } + wiiuse.specialPoll(gather); /* deal with events gathered in Wiiuse API */ for (WiiUseApiEvent evt : gather.getEvents()) { diff --git a/WiiUseJ/src/wiiusej/Wiimote.java b/WiiUseJ/src/wiiusej/Wiimote.java index f04e66c..db48be6 100644 --- a/WiiUseJ/src/wiiusej/Wiimote.java +++ b/WiiUseJ/src/wiiusej/Wiimote.java @@ -19,12 +19,11 @@ package wiiusej; import javax.swing.event.EventListenerList; import wiiusej.wiiuseapievents.DisconnectionEvent; -import wiiusej.wiiuseapievents.StatusEvent; import wiiusej.wiiuseapievents.GenericEvent; +import wiiusej.wiiuseapievents.StatusEvent; import wiiusej.wiiuseapievents.WiiUseApiEvent; import wiiusej.wiiuseapievents.WiiUseApiListener; import wiiusej.wiiuseapievents.WiimoteListener; -import wiiusej.wiiuseapirequest.WiiUseApiRequest; /** * Class that represents a wiimote.