001: ///////////////////////////////////////////////////////////////////////////////
002: //
003: // This program is free software; you can redistribute it and/or modify
004: // it under the terms of the GNU General Public License and GNU Library
005: // General Public License as published by the Free Software Foundation;
006: // either version 2, or (at your option) any later version.
007: //
008: // This program is distributed in the hope that it will be useful,
009: // but WITHOUT ANY WARRANTY; without even the implied warranty of
010: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: // GNU General Public License and GNU Library General Public License
012: // for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // and GNU Library General Public License along with this program; if
016: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
017: // MA 02139, USA.
018: //
019: ///////////////////////////////////////////////////////////////////////////////
020:
021: package org.rdesktop.server.rdp.keymapping;
022:
023: import java.io.*;
024: import java.util.*;
025: import java.awt.event.KeyEvent;
026:
027: import org.rdesktop.server.rdp.RdpOptions;
028:
029: public class MapDef {
030: private final int FLAG_SHIFT = 0x01;
031: private final int FLAG_CTRL = 0x02;
032: private final int FLAG_ALT = 0x04;
033: private final int FLAG_CAPSLOCK = 0x08;
034:
035: private int m_scancode;
036: private boolean m_altDown;
037: private boolean m_ctrlDown;
038: private boolean m_shiftDown;
039: private boolean m_capslockDown;
040:
041: private char m_keyChar;
042: private int m_keyCode;
043: private int m_keyLocation;
044: private boolean m_characterDef;
045:
046: public MapDef(char keyChar, int keyLocation, int scancode,
047: boolean ctrlDown, boolean shiftDown, boolean altDown,
048: boolean capslockDown) {
049: m_keyChar = keyChar;
050: m_characterDef = true;
051:
052: m_keyLocation = keyLocation;
053: m_scancode = scancode;
054: m_ctrlDown = ctrlDown;
055: m_altDown = altDown;
056: m_shiftDown = shiftDown;
057: m_capslockDown = capslockDown;
058: }
059:
060: public MapDef(int keyCode, int keyLocation, int scancode,
061: boolean ctrlDown, boolean shiftDown, boolean altDown,
062: boolean capslockDown) {
063: m_keyCode = keyCode;
064: m_characterDef = false;
065:
066: m_keyLocation = keyLocation;
067: m_scancode = scancode;
068: m_ctrlDown = ctrlDown;
069: m_altDown = altDown;
070: m_shiftDown = shiftDown;
071: m_capslockDown = capslockDown;
072: }
073:
074: public MapDef(String definition) throws KeyMapException {
075: try {
076: StringTokenizer st = new StringTokenizer(definition);
077: m_characterDef = ((Integer.parseInt(st.nextToken()) == 1) ? true
078: : false);
079:
080: if (m_characterDef == true) {
081: m_keyChar = (char) Integer.parseInt(st.nextToken());
082: } else {
083: m_keyCode = Integer.parseInt(st.nextToken());
084: }
085:
086: m_keyLocation = Integer.parseInt(st.nextToken());
087: m_scancode = Integer.decode(st.nextToken()).intValue();
088:
089: int modifiers = Integer.parseInt(st.nextToken());
090: m_shiftDown = ((modifiers & FLAG_SHIFT) != 0);
091: m_ctrlDown = ((modifiers & FLAG_CTRL) != 0);
092: m_altDown = ((modifiers & FLAG_ALT) != 0);
093: m_capslockDown = ((modifiers & FLAG_CAPSLOCK) != 0);
094: } catch (NumberFormatException nfEx) {
095: throw new KeyMapException("" + nfEx.getMessage()
096: + " is not numeric");
097: } catch (NoSuchElementException nseEx) {
098: throw new KeyMapException(
099: "Not enough parameters in definition");
100: }
101: }
102:
103: protected boolean appliesToTyped(KeyEvent e) {
104: return ((m_characterDef == true) && (m_keyChar == e
105: .getKeyChar()));
106: }
107:
108: protected boolean appliesToTyped(KeyEvent e, boolean capslock) {
109: return ((m_characterDef) && (m_keyChar == e.getKeyChar()));
110: }
111:
112: protected boolean appliesToPressed(KeyEvent e) {
113: if (m_characterDef == false) {
114: if ((((m_ctrlDown == true) && (e.isControlDown() == true)) || (m_ctrlDown == false)) == false) {
115: return false;
116: }
117:
118: if ((((m_altDown == true) && (e.isAltDown() == true)) || (m_altDown == false)) == false) {
119: return false;
120: }
121: }
122:
123: return ((m_characterDef == false) && (m_keyCode == e
124: .getKeyCode()));
125: }
126:
127: public int getKeyCode() {
128: return m_keyCode;
129: }
130:
131: public char getKeyChar() {
132: return m_keyChar;
133: }
134:
135: public int getScancode() {
136: return m_scancode;
137: }
138:
139: public boolean isCharacterDef() {
140: return m_characterDef;
141: }
142:
143: public boolean isCtrlDown() {
144: return m_ctrlDown;
145: }
146:
147: public boolean isAltDown() {
148: return m_altDown;
149: }
150:
151: public boolean isShiftDown() {
152: return m_shiftDown;
153: }
154:
155: public boolean isCapslockOn() {
156: return m_capslockDown;
157: }
158:
159: public int modifierDistance(KeyEvent e, boolean capslock) {
160: if (m_characterDef == false) {
161: return 0;
162: }
163:
164: int dist = 0;
165: if (m_ctrlDown != e.isControlDown()) {
166: dist += 1;
167: }
168:
169: if (m_altDown != e.isAltDown()) {
170: dist += 1;
171: }
172:
173: if (m_shiftDown != e.isShiftDown()) {
174: dist += 1;
175: }
176:
177: if (m_capslockDown != capslock) {
178: dist += 1;
179: }
180:
181: return dist;
182: }
183:
184: public boolean appliesTo(char c) {
185: return ((m_characterDef == true) && (m_keyChar == c) && (m_capslockDown == false));
186: }
187:
188: public void writeToStream(PrintStream printStream) {
189: String definition = "" + (m_characterDef ? 1 : 0);
190:
191: definition += "\t";
192: if (m_characterDef == true) {
193: definition += (int) m_keyChar;
194: } else {
195: definition += m_keyCode;
196: }
197:
198: definition += "\t" + m_keyLocation;
199: definition += "\t0x" + Integer.toHexString(m_scancode);
200:
201: int modifiers = 0;
202: modifiers |= (m_shiftDown ? FLAG_SHIFT : 0);
203: modifiers |= (m_ctrlDown ? FLAG_CTRL : 0);
204: modifiers |= (m_altDown ? FLAG_ALT : 0);
205: modifiers |= (m_capslockDown ? FLAG_CAPSLOCK : 0);
206: definition += "\t" + modifiers;
207:
208: if (m_characterDef == false) {
209: definition += "\t" + KeyEvent.getKeyText(m_keyCode);
210: }
211:
212: printStream.println(definition);
213: }
214: }
|