aboutsummaryrefslogtreecommitdiffstats
path: root/src/jake2/sys/KBD.java
blob: 3272090ad90570884910d7d90cffe597afd3356f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * KBD.java
 * Copyright (C) 2004
 * 
 * $Id: KBD.java,v 1.5 2004-11-03 10:16:42 hzi Exp $
 */
/*
Copyright (C) 1997-2001 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
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 General Public License for more details.

You should have received a copy of the GNU 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 jake2.sys;

import jake2.client.Key;

import java.awt.*;
import java.awt.event.*;

/**
 * KBD
 */
public final class KBD {
	
	static int win_x = 0;
	static int win_y = 0;
	static int win_w2 = 0;
	static int win_h2 = 0;
		
	// motion values
	public static int mx = 0;
	public static int my = 0;
	
	static Robot robot;
	public static InputListener listener = new InputListener();
		
	static {
		try {
			robot = new Robot();
		} catch (AWTException e) {
			System.exit(1);
		}
	}
		
	public static void Init() {
	}

	public static void Update() {
		// get events
		HandleEvents();
	}

	public static void Close() {
	}
	
	static void HandleEvents() {
		int b;

		Jake2InputEvent event;
		while ( (event=InputListener.nextEvent()) != null ) {
			switch(event.type) {
				case Jake2InputEvent.KeyPress:
				case Jake2InputEvent.KeyRelease:
					Do_Key_Event(XLateKey((KeyEvent)event.ev), event.type == Jake2InputEvent.KeyPress);
					break;

				case Jake2InputEvent.MotionNotify:
//					if (IN.ignorefirst) {
//						IN.ignorefirst = false;
//						break;
//					}
					if (IN.mouse_active) {
						mx = (((MouseEvent)event.ev).getX() - win_w2) * 2;
						my = (((MouseEvent)event.ev).getY() - win_h2) * 2;
					} else {
						mx = 0;
						my = 0;
					}
					break;

				case Jake2InputEvent.ButtonPress:
					b=((MouseEvent)event.ev).getButton()-1;
					Do_Key_Event(Key.K_MOUSE1 + b, true);
					break;
 
				case Jake2InputEvent.ButtonRelease:
					b=((MouseEvent)event.ev).getButton()-1;
					Do_Key_Event(Key.K_MOUSE1 + b, false);
					break;
 
				case Jake2InputEvent.WheelMoved:
				    int dir = ((MouseWheelEvent)event.ev).getWheelRotation();
					if (dir > 0) {
					    Do_Key_Event(Key.K_MWHEELDOWN, true);
						Do_Key_Event(Key.K_MWHEELDOWN, false);
					} else {
					    Do_Key_Event(Key.K_MWHEELUP, true);
						Do_Key_Event(Key.K_MWHEELUP, false);					    
					}
				    break;
				
				case Jake2InputEvent.CreateNotify :
				case Jake2InputEvent.ConfigureNotify :
					Component c = ((ComponentEvent)event.ev).getComponent();
					win_x = 0;
					win_y = 0;
					win_w2 = c.getWidth() / 2;
					win_h2 = c.getHeight() / 2;
					while (c != null) {
						if (c instanceof Container) {
							Insets insets = ((Container)c).getInsets();
							win_x += insets.left;
							win_y += insets.top;
						}
						win_x += c.getX();
						win_y += c.getY();
						c = c.getParent();
					}
					break;
			}
		}
            
		if (mx != 0 || my != 0) {
			// move the mouse to the window center again
			robot.mouseMove(win_x + win_w2, win_y + win_h2);
		}		
	}

	private static int XLateKey(KeyEvent ev) {
 
		int key = 0;
		int code = ev.getKeyCode();

		switch(code) {
//	00626                 case XK_KP_Page_Up:      key = K_KP_PGUP; break;
			case KeyEvent.VK_PAGE_UP: key = Key.K_PGUP; break;
 
//	00629                 case XK_KP_Page_Down: key = K_KP_PGDN; break;
			case KeyEvent.VK_PAGE_DOWN: key = Key.K_PGDN; break;

//	00632                 case XK_KP_Home: key = K_KP_HOME; break;
			case KeyEvent.VK_HOME: key = Key.K_HOME; break;

//	00635                 case XK_KP_End:  key = K_KP_END; break;
			case KeyEvent.VK_END: key = Key.K_END; break;
 
			case KeyEvent.VK_KP_LEFT: key = Key.K_KP_LEFTARROW; break;
			case KeyEvent.VK_LEFT: key = Key.K_LEFTARROW; break;
 
			case KeyEvent.VK_KP_RIGHT: key = Key.K_KP_RIGHTARROW; break;
			case KeyEvent.VK_RIGHT: key = Key.K_RIGHTARROW; break;

			case KeyEvent.VK_KP_DOWN: key = Key.K_KP_DOWNARROW; break;
			case KeyEvent.VK_DOWN: key = Key.K_DOWNARROW; break;

			case KeyEvent.VK_KP_UP: key = Key.K_KP_UPARROW; break;
			case KeyEvent.VK_UP: key = Key.K_UPARROW; break; 

			case KeyEvent.VK_ESCAPE: key = Key.K_ESCAPE; break; 

			
			case KeyEvent.VK_ENTER: key = Key.K_ENTER; break; 
//	00652                 case XK_KP_Enter: key = K_KP_ENTER;     break;

			case KeyEvent.VK_TAB: key = Key.K_TAB; break; 

			case KeyEvent.VK_F1: key = Key.K_F1; break;
			case KeyEvent.VK_F2: key = Key.K_F2; break;
			case KeyEvent.VK_F3: key = Key.K_F3; break;
			case KeyEvent.VK_F4: key = Key.K_F4; break;
			case KeyEvent.VK_F5: key = Key.K_F5; break;
			case KeyEvent.VK_F6: key = Key.K_F6; break;
			case KeyEvent.VK_F7: key = Key.K_F7; break;
			case KeyEvent.VK_F8: key = Key.K_F8; break;
			case KeyEvent.VK_F9: key = Key.K_F9; break;
			case KeyEvent.VK_F10: key = Key.K_F10; break;
			case KeyEvent.VK_F11: key = Key.K_F11; break;
			case KeyEvent.VK_F12: key = Key.K_F12; break; 

			case KeyEvent.VK_BACK_SPACE: key = Key.K_BACKSPACE; break; 

			case KeyEvent.VK_DELETE: key = Key.K_DEL; break; 
//	00683                 case XK_KP_Delete: key = K_KP_DEL; break;

			case KeyEvent.VK_PAUSE: key = Key.K_PAUSE; break; 
	
			case KeyEvent.VK_SHIFT: key = Key.K_SHIFT; break; 
			case KeyEvent.VK_CONTROL: key = Key.K_CTRL; break; 
			
			case KeyEvent.VK_ALT:
			case KeyEvent.VK_ALT_GRAPH: key = Key.K_ALT; break;
 
//	00700                 case XK_KP_Begin: key = K_KP_5; break;
//	00701
			case KeyEvent.VK_INSERT: key = Key.K_INS; break;
			// toggle console for DE and US keyboards
			case KeyEvent.VK_DEAD_ACUTE:
			case KeyEvent.VK_CIRCUMFLEX:
			case KeyEvent.VK_DEAD_CIRCUMFLEX: key = '`'; break;
 
			default:
				if ((ev.getModifiers() & (InputEvent.ALT_MASK | InputEvent.CTRL_MASK)) != 0) {
				    key = ev.getKeyCode();
				} else {
				    key = ev.getKeyChar();
				}
				if (key >= 'A' && key <= 'Z')
					key = key - 'A' + 'a';
			break;
		}
		if (key > 255) key = 0;

		return key;
	}	
		
	static void Do_Key_Event(int key, boolean down) {
		Key.Event(key, down, Sys.Milliseconds());
	}
	
	static void centerMouse() {
		robot.mouseMove(win_x + win_w2, win_y + win_h2);
	}
	
}