View Javadoc

1   /*
2    * @(#)$Id: WacomReader.java 689 2009-07-22 00:10:27Z bsigner $
3    *
4    * Author       :   Michele Croci, mcroci@gmail.com
5    *
6    * Purpose      :   A wacom tablet PC reader.
7    *                  
8    * -----------------------------------------------------------------------
9    *
10   * Revision Information:
11   *
12   * Date             Who         Reason
13   *
14   * Nov 15, 2007		crocimi    Initial release    
15   * 
16   *
17   * -----------------------------------------------------------------------
18   *
19   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
20   *
21   * This software is the proprietary information of ETH Zurich.
22   * Use is subject to license terms.
23   * 
24   */
25  
26  
27  package org.ximtec.igesture.io.wacomclient;
28  
29  import java.awt.Point;
30  import java.util.ArrayList;
31  import java.util.HashSet;
32  import java.util.List;
33  import java.util.logging.Logger;
34  
35  import org.sigtec.graphix.Orientation;
36  import org.sigtec.ink.input.CompleteLocation;
37  import org.sigtec.ink.input.Location;
38  import org.sigtec.input.AbstractInputDevice;
39  import org.sigtec.input.InputDeviceEvent;
40  import org.sigtec.util.Constant;
41  import org.ximtec.igesture.io.button.ButtonDeviceEventListener;
42  import org.ximtec.igesture.io.button.ButtonDeviceHandler;
43  import org.ximtec.igesture.io.wacom.WacomCallback;
44  import org.ximtec.igesture.io.wacom.WacomUtils;
45  import org.ximtec.igesture.io.wacom.Wintab32.ORIENTATION;
46  import org.ximtec.igesture.io.wacom.Wintab32.ROTATION;
47  
48  
49  /**
50   * A wacom tablet PC reader.
51   * 
52   * @version 1.0, Nov 2007
53   * @author Michele Croci, mcroci@gmail.com
54   */
55  
56  public class WacomReader extends AbstractInputDevice implements ButtonDeviceEventListener,
57        WacomCallback {
58  
59     private static final Logger LOGGER = Logger.getLogger(WacomReader.class.getName());
60  
61     private boolean lastKeyState = false;
62     private WacomUtils wacomUtils;
63  
64     private int BUTTON_0 = 0;
65     private int BUTTON_5 = 5;
66  
67     private HashSet<ButtonDeviceHandler> buttonUpEvents;
68  
69  
70     public WacomReader() {
71        init();
72        buttonUpEvents = new HashSet<ButtonDeviceHandler>();
73     }
74  
75  
76     /**
77      * Initialises the tablet reader and starts a new thread polling the tablet.
78      * 
79      */
80     public void init() {
81  
82        init_callback();
83     } // init
84  
85  
86     void init_callback() {
87        // FIXME uk: refactoring
88        wacomUtils = new WacomUtils(this);
89        wacomUtils.start();
90     }
91  
92  
93     public void callbackFunction(int x, int y, int z, int pkstatus, int npress,
94           int tpress, long timeStamp, ORIENTATION orientation, ROTATION rotation,
95           int button) {
96  
97        if (button == BUTTON_5) {
98           Location location = new Location("screen", 1, new Point(x, y));
99           CompleteLocation tbl = null;
100          tbl = new CompleteLocation(location, timeStamp, npress,
101                Constant.NOT_AVAILABLE, new Orientation(rotation.roYaw,
102                      rotation.roPitch, rotation.roRoll));
103          WacomReader.this.fireInputDeviceEvent(new WacomReaderEvent(tbl));
104          lastKeyState = true;
105 
106       }
107       else {
108 
109          if (lastKeyState) {
110             WacomReader.this.fireTabletButtonEvent(new InputDeviceEvent() {
111 
112                public long getTimestamp() {
113                   return System.currentTimeMillis();
114                }
115             });
116 
117             lastKeyState = false;
118          }
119       }
120 
121    }
122 
123 
124    /**
125     * Creates a list of tablet readers. Only one tablet reader is created.
126     * 
127     * @return a list of tablet readers.
128     */
129    public static List<WacomReader> createTablet() {
130       final List<WacomReader> list = new ArrayList<WacomReader>();
131       list.add(new WacomReader());
132       return list;
133    } // createtablet
134 
135 
136    public void addButtonDeviceEventListener(ButtonDeviceHandler listener) {
137       buttonUpEvents.add(listener);
138    } // addButtonDeviceEventListener
139 
140 
141    public void removeButtonDeviceEventListener(ButtonDeviceHandler listener) {
142       buttonUpEvents.remove(listener);
143    } // removeButtonDeviceEventListener
144 
145 
146    private void fireTabletButtonEvent(InputDeviceEvent event) {
147       for (final ButtonDeviceHandler listener : buttonUpEvents) {
148          listener.handleButtonPressedEvent(event);
149       }
150 
151    } // fireTabletButtonEvent
152 
153 }