| 1 |
/** |
| 2 |
* libcaca Java bindings for libcaca |
| 3 |
* Copyright (c) 2009 Adrien Grand <[email protected]> |
| 4 |
* |
| 5 |
* This library is free software. It comes without any warranty, to |
| 6 |
* the extent permitted by applicable law. You can redistribute it |
| 7 |
* and/or modify it under the terms of the Do What the Fuck You Want |
| 8 |
* to Public License, Version 2, as published by Sam Hocevar. See |
| 9 |
* http://www.wtfpl.net/ for more details. |
| 10 |
*/ |
| 11 |
|
| 12 |
package org.zoy.caca; |
| 13 |
|
| 14 |
public class Event extends NativeObject { |
| 15 |
|
| 16 |
static { |
| 17 |
Caca.load(); |
| 18 |
} |
| 19 |
|
| 20 |
public static enum Type { |
| 21 |
NONE(0x0000), |
| 22 |
KEY_PRESS(0x0001), |
| 23 |
KEY_RELEASE(0x0002), |
| 24 |
MOUSE_PRESS(0x0004), |
| 25 |
MOUSE_RELEASE(0x0008), |
| 26 |
MOUSE_MOTION(0x0010), |
| 27 |
RESIZE(0x0020), |
| 28 |
QUIT(0x0040), |
| 29 |
ANY(0xffff); |
| 30 |
|
| 31 |
protected final int code; |
| 32 |
private Type(int code) { |
| 33 |
this.code = code; |
| 34 |
} |
| 35 |
public static Type forCode(int code) { |
| 36 |
Type[] types = Type.values(); |
| 37 |
for (Type type : types) { |
| 38 |
if (type.code == code) { |
| 39 |
return type; |
| 40 |
} |
| 41 |
} |
| 42 |
return null; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
public static enum Key { |
| 47 |
CTRL_A(0x01), |
| 48 |
CTRL_B(0x02), |
| 49 |
CTRL_C(0x03), |
| 50 |
CTRL_D(0x04), |
| 51 |
CTRL_E(0x05), |
| 52 |
CTRL_F(0x06), |
| 53 |
CTRL_G(0x07), |
| 54 |
BACKSPACE(0x08), |
| 55 |
TAB(0x09), |
| 56 |
CTRL_J(0x0a), |
| 57 |
CTRL_K(0x0b), |
| 58 |
CTRL_L(0x0c), |
| 59 |
RETURN(0x0d), |
| 60 |
CTRL_N(0x0e), |
| 61 |
CTRL_O(0x0f), |
| 62 |
CTRL_P(0x10), |
| 63 |
CTRL_Q(0x11), |
| 64 |
CTRL_R(0x12), |
| 65 |
PAUSE(0x13), |
| 66 |
CTRL_T(0x14), |
| 67 |
CTRL_U(0x15), |
| 68 |
CTRL_V(0x16), |
| 69 |
CTRL_W(0x17), |
| 70 |
CTRL_X(0x18), |
| 71 |
CTRL_Y(0x19), |
| 72 |
CTRL_Z(0x20), |
| 73 |
|
| 74 |
UP(0x111), |
| 75 |
DOWN(0x112), |
| 76 |
LEFT(0x113), |
| 77 |
RIGHT(0x114), |
| 78 |
|
| 79 |
INSERT(0x115), |
| 80 |
HOME(0x116), |
| 81 |
END(0x117), |
| 82 |
PAGE_HOME(0x118), |
| 83 |
PAGE_DOWN(0x119), |
| 84 |
|
| 85 |
F1(0x11a), |
| 86 |
F2(0x11b), |
| 87 |
F3(0x11c), |
| 88 |
F4(0x11d), |
| 89 |
F5(0x11e), |
| 90 |
F6(0x11f), |
| 91 |
F7(0x120), |
| 92 |
F8(0x121), |
| 93 |
F9(0x122), |
| 94 |
F10(0x123), |
| 95 |
F11(0x124), |
| 96 |
F12(0x125), |
| 97 |
F13(0x126), |
| 98 |
F14(0x127), |
| 99 |
F15(0x128); |
| 100 |
|
| 101 |
protected final int code; |
| 102 |
private Key(int code) { |
| 103 |
this.code = code; |
| 104 |
} |
| 105 |
public static Key forCode(int code) { |
| 106 |
Key[] keys = Key.values(); |
| 107 |
for (Key key : keys) { |
| 108 |
if (key.code == code) { |
| 109 |
return key; |
| 110 |
} |
| 111 |
} |
| 112 |
return null; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
protected Event(long ptr) { |
| 117 |
this.ptr = ptr; |
| 118 |
} |
| 119 |
|
| 120 |
private static native int getEventType(long eventPtr); |
| 121 |
|
| 122 |
public Type getType() { |
| 123 |
return Type.forCode(getEventType(ptr)); |
| 124 |
} |
| 125 |
|
| 126 |
private static native int getEventKeyCh(long eventPtr); |
| 127 |
|
| 128 |
public int getKeyCh() { |
| 129 |
return getEventKeyCh(ptr); |
| 130 |
} |
| 131 |
|
| 132 |
private static native int getEventKeyUtf32(long eventPtr); |
| 133 |
|
| 134 |
public int getKeyUtf32() { |
| 135 |
return getEventKeyUtf32(ptr); |
| 136 |
} |
| 137 |
|
| 138 |
private static native String getEventKeyUtf8(long eventPtr); |
| 139 |
|
| 140 |
public String getKeyUtf8() { |
| 141 |
return getEventKeyUtf8(ptr); |
| 142 |
} |
| 143 |
|
| 144 |
private static native int getEventMouseButton(long eventPtr); |
| 145 |
|
| 146 |
public int getMouseButton() { |
| 147 |
return getEventMouseButton(ptr); |
| 148 |
} |
| 149 |
|
| 150 |
private static native int getEventMouseX(long eventPtr); |
| 151 |
|
| 152 |
public int getMouseX() { |
| 153 |
return getEventMouseX(ptr); |
| 154 |
} |
| 155 |
|
| 156 |
private static native int getEventMouseY(long eventPtr); |
| 157 |
|
| 158 |
public int getMouseY() { |
| 159 |
return getEventMouseY(ptr); |
| 160 |
} |
| 161 |
|
| 162 |
private static native int getEventResizeWidth(long eventPtr); |
| 163 |
|
| 164 |
public int getResizeWidth() { |
| 165 |
return getEventResizeWidth(ptr); |
| 166 |
} |
| 167 |
|
| 168 |
private static native int getEventResizeHeight(long eventPtr); |
| 169 |
|
| 170 |
public int getResizeHeight() { |
| 171 |
return getEventResizeHeight(ptr); |
| 172 |
} |
| 173 |
|
| 174 |
private static native void freeEvent(long eventPtr); |
| 175 |
|
| 176 |
@Override |
| 177 |
public void finalize() throws Throwable { |
| 178 |
freeEvent(ptr); |
| 179 |
super.finalize(); |
| 180 |
} |
| 181 |
|
| 182 |
} |