Extract control event parsing to separate methods
Use one parse method per control event type.
This commit is contained in:
parent
1eaa27ed9e
commit
03c5f97e3f
1 changed files with 74 additions and 49 deletions
|
@ -47,61 +47,86 @@ public class ControlEventReader {
|
|||
int savedPosition = buffer.position();
|
||||
|
||||
int type = buffer.get();
|
||||
ControlEvent controlEvent;
|
||||
switch (type) {
|
||||
case ControlEvent.TYPE_KEYCODE: {
|
||||
if (buffer.remaining() < KEYCODE_PAYLOAD_LENGTH) {
|
||||
case ControlEvent.TYPE_KEYCODE:
|
||||
controlEvent = parseKeycodeControlEvent();
|
||||
break;
|
||||
case ControlEvent.TYPE_TEXT:
|
||||
controlEvent = parseTextControlEvent();
|
||||
break;
|
||||
case ControlEvent.TYPE_MOUSE:
|
||||
controlEvent = parseMouseControlEvent();
|
||||
break;
|
||||
case ControlEvent.TYPE_SCROLL:
|
||||
controlEvent = parseScrollControlEvent();
|
||||
break;
|
||||
case ControlEvent.TYPE_COMMAND:
|
||||
controlEvent = parseCommandControlEvent();
|
||||
break;
|
||||
default:
|
||||
Ln.w("Unknown event type: " + type);
|
||||
controlEvent = null;
|
||||
break;
|
||||
}
|
||||
|
||||
if (controlEvent == null) {
|
||||
// failure, reset savedPosition
|
||||
buffer.position(savedPosition);
|
||||
}
|
||||
return controlEvent;
|
||||
}
|
||||
|
||||
private ControlEvent parseKeycodeControlEvent() {
|
||||
if (buffer.remaining() < KEYCODE_PAYLOAD_LENGTH) {
|
||||
return null;
|
||||
}
|
||||
int action = toUnsigned(buffer.get());
|
||||
int keycode = buffer.getInt();
|
||||
int metaState = buffer.getInt();
|
||||
return ControlEvent.createKeycodeControlEvent(action, keycode, metaState);
|
||||
}
|
||||
case ControlEvent.TYPE_TEXT: {
|
||||
|
||||
private ControlEvent parseTextControlEvent() {
|
||||
if (buffer.remaining() < 1) {
|
||||
break;
|
||||
return null;
|
||||
}
|
||||
int len = toUnsigned(buffer.get());
|
||||
if (buffer.remaining() < len) {
|
||||
break;
|
||||
return null;
|
||||
}
|
||||
buffer.get(textBuffer, 0, len);
|
||||
String text = new String(textBuffer, 0, len, StandardCharsets.UTF_8);
|
||||
return ControlEvent.createTextControlEvent(text);
|
||||
}
|
||||
case ControlEvent.TYPE_MOUSE: {
|
||||
|
||||
private ControlEvent parseMouseControlEvent() {
|
||||
if (buffer.remaining() < MOUSE_PAYLOAD_LENGTH) {
|
||||
break;
|
||||
return null;
|
||||
}
|
||||
int action = toUnsigned(buffer.get());
|
||||
int buttons = buffer.getInt();
|
||||
Position position = readPosition(buffer);
|
||||
return ControlEvent.createMotionControlEvent(action, buttons, position);
|
||||
}
|
||||
case ControlEvent.TYPE_SCROLL: {
|
||||
|
||||
private ControlEvent parseScrollControlEvent() {
|
||||
if (buffer.remaining() < SCROLL_PAYLOAD_LENGTH) {
|
||||
break;
|
||||
return null;
|
||||
}
|
||||
Position position = readPosition(buffer);
|
||||
int hScroll = buffer.getInt();
|
||||
int vScroll = buffer.getInt();
|
||||
return ControlEvent.createScrollControlEvent(position, hScroll, vScroll);
|
||||
}
|
||||
case ControlEvent.TYPE_COMMAND: {
|
||||
|
||||
private ControlEvent parseCommandControlEvent() {
|
||||
if (buffer.remaining() < COMMAND_PAYLOAD_LENGTH) {
|
||||
break;
|
||||
return null;
|
||||
}
|
||||
int action = toUnsigned(buffer.get());
|
||||
return ControlEvent.createCommandControlEvent(action);
|
||||
}
|
||||
default:
|
||||
Ln.w("Unknown event type: " + type);
|
||||
}
|
||||
|
||||
// failure, reset savedPosition
|
||||
buffer.position(savedPosition);
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Position readPosition(ByteBuffer buffer) {
|
||||
int x = toUnsigned(buffer.getShort());
|
||||
|
|
Loading…
Reference in a new issue