?? windows.java
字號:
/* * Java USB Library * Copyright (C) 2000 by David Brownell * Copyright (C) 2002 by Wayne Westerman * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * */package usb.windows;import java.io.IOException;import java.util.Enumeration;import java.util.Vector;import java.util.Hashtable;import usb.core.*;/** * Provides access to native USB host objects * * @author Mike Stahl * @version $id: Windows.java, v1.0 Created on 11. Juli 2003, 21:23 */public class Windows extends usb.core.HostFactory { public static final boolean debug = false; /** If true the debugs the enumeration process */ public static final boolean debugEnum = false; // To look what happens in the Watcher class /** if true then debugs the Watcher issues */ public static final boolean debugWatcher = false; /** if true then debugs the USB issues */ public static final boolean debugUSB = false; /** if true then debugs the DeviceImpl issues */ public static final boolean debugDeviceImpl = false; /** if true then debugs the JUSB issues */ public static final boolean debugJUSB = false; /** if true then debugs the USB issues */ public static boolean createUSB; private static Windows.HostImpl self; static private Watcher watcher; static private Thread daemon; /** The polling period for scanning the bus in seconds */ static final int POLL_PERIOD = 4; // seconds /** The string to regocnise a jUSB driver. If the "DeviceDesc" value in the Windows * registry for an usb device starts with this name then the device is supposed to * be a jUSB device. */ public static final String A_JUSB_DRIVER = "JUSB Driver --:"; /** * Not part of the API; implements reference implementation SPI. */ public Windows() { } /** * Not part of the API; implements reference implementation SPI. */ public Host createHost() throws java.io.IOException { return Windows.getHost(); } /** * Provides access to a singelton USB Host */ public static Host getHost() throws IOException{ synchronized (Host.class){ // check whether there exists already a HostImpl or not if (self == null) { // there exists no Host; create our own Host. // load the jusb.dll which must be put into the // Windows system32 folder System.loadLibrary("jusb"); self = new Windows.HostImpl(); } } // end of synchronized return self; } /** Return the host controller name of the Windows OS * @param hcdDevicePath The host controller device path from the Windows OS * @return The name of the host controller or null. */ static public native String getHostControllerName(String hcdDevicePath); /** Return the ith host controller device path from the Windows OS * @param number The number indicates the ith host controller on the system. Start with 0 and * increment until null is returned * @return The host controller device path or null. */ static public native String getHostControllerDevicePath(int number); /******************************************************************** * Represents a Windows host associated with one or more * Universal Serial Busses (USBs) * * To provide missunderstanding in the USB topology of Windows * operating systems, one Universal Serial Bus is managed by one * Host Controller. So the host we create through the jusb API has * nothing to do with the Host Controller from the Windows os. * In fact a host controller in the Windows os correspond to a * Bus according to this jusb API. This means if we have more * than one host controller on our Windows PC, we will have also * more USB Busses. The amount of busses is equal to the amount of * host controllers. */ private static final class HostImpl implements Host{ private final transient Hashtable busses = new Hashtable(3); private final transient Vector listeners = new Vector(3); HostImpl() throws IOException{ super(); watcher = new Watcher(busses,listeners); // create a new Thread with the target watcher, which implements the run() method // and name the thread "USB_watcher" daemon = new Thread(watcher,"USB-Watcher"); // we set this thread as a daemon. This make sure that our watcher terminate as // soon our main application stops. daemon.setDaemon(true); daemon.start(); } // actually not necessary because our thread is a daemon thread // and is terminated when our main application is terminated // protected void finalize(){ daemon.interrupt(); } public void addUSBListener(USBListener l) throws IOException { if (l == null) throw new IllegalArgumentException(); listeners.add(l); } public Bus[] getBusses() throws IOException { synchronized(busses){ Bus retval [] = new Bus [busses.size()]; int i=0; for(Enumeration e = busses.keys(); e.hasMoreElements(); ) retval[i++] = (Bus) busses.get(e.nextElement()); return retval; } } public Device getDevice(String portId) throws IOException { return new PortIdentifier(portId).getDevice(this); } public void removeUSBListener(USBListener l) throws IOException { listeners.remove(l); } } // end of class HostImpl /****************************************************************** * Monitors the USB bus structure about removal and attachment of * devices. */ private static final class Watcher implements Runnable{ private final Hashtable busses; private final Vector listeners; boolean finishedScan; Watcher(Hashtable b, Vector l) throws IOException{ busses = b; listeners = l; //initial population of all the busses if(Windows.debugWatcher) System.out.println(Windows.createUSB + " Watcher.Constructor() >>> before scan! \n");
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -