Extract control event parsing to separate methods

Use one parse method per control event type.
This commit is contained in:
Romain Vimont 2018-02-07 17:41:14 +01:00
parent 1eaa27ed9e
commit 03c5f97e3f

View file

@ -47,61 +47,86 @@ public class ControlEventReader {
int savedPosition = buffer.position(); int savedPosition = buffer.position();
int type = buffer.get(); int type = buffer.get();
ControlEvent controlEvent;
switch (type) { switch (type) {
case ControlEvent.TYPE_KEYCODE: { case ControlEvent.TYPE_KEYCODE:
if (buffer.remaining() < KEYCODE_PAYLOAD_LENGTH) { controlEvent = parseKeycodeControlEvent();
break; 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 action = toUnsigned(buffer.get());
int keycode = buffer.getInt(); int keycode = buffer.getInt();
int metaState = buffer.getInt(); int metaState = buffer.getInt();
return ControlEvent.createKeycodeControlEvent(action, keycode, metaState); return ControlEvent.createKeycodeControlEvent(action, keycode, metaState);
} }
case ControlEvent.TYPE_TEXT: {
private ControlEvent parseTextControlEvent() {
if (buffer.remaining() < 1) { if (buffer.remaining() < 1) {
break; return null;
} }
int len = toUnsigned(buffer.get()); int len = toUnsigned(buffer.get());
if (buffer.remaining() < len) { if (buffer.remaining() < len) {
break; return null;
} }
buffer.get(textBuffer, 0, len); buffer.get(textBuffer, 0, len);
String text = new String(textBuffer, 0, len, StandardCharsets.UTF_8); String text = new String(textBuffer, 0, len, StandardCharsets.UTF_8);
return ControlEvent.createTextControlEvent(text); return ControlEvent.createTextControlEvent(text);
} }
case ControlEvent.TYPE_MOUSE: {
private ControlEvent parseMouseControlEvent() {
if (buffer.remaining() < MOUSE_PAYLOAD_LENGTH) { if (buffer.remaining() < MOUSE_PAYLOAD_LENGTH) {
break; return null;
} }
int action = toUnsigned(buffer.get()); int action = toUnsigned(buffer.get());
int buttons = buffer.getInt(); int buttons = buffer.getInt();
Position position = readPosition(buffer); Position position = readPosition(buffer);
return ControlEvent.createMotionControlEvent(action, buttons, position); return ControlEvent.createMotionControlEvent(action, buttons, position);
} }
case ControlEvent.TYPE_SCROLL: {
private ControlEvent parseScrollControlEvent() {
if (buffer.remaining() < SCROLL_PAYLOAD_LENGTH) { if (buffer.remaining() < SCROLL_PAYLOAD_LENGTH) {
break; return null;
} }
Position position = readPosition(buffer); Position position = readPosition(buffer);
int hScroll = buffer.getInt(); int hScroll = buffer.getInt();
int vScroll = buffer.getInt(); int vScroll = buffer.getInt();
return ControlEvent.createScrollControlEvent(position, hScroll, vScroll); return ControlEvent.createScrollControlEvent(position, hScroll, vScroll);
} }
case ControlEvent.TYPE_COMMAND: {
private ControlEvent parseCommandControlEvent() {
if (buffer.remaining() < COMMAND_PAYLOAD_LENGTH) { if (buffer.remaining() < COMMAND_PAYLOAD_LENGTH) {
break; return null;
} }
int action = toUnsigned(buffer.get()); int action = toUnsigned(buffer.get());
return ControlEvent.createCommandControlEvent(action); 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) { private static Position readPosition(ByteBuffer buffer) {
int x = toUnsigned(buffer.getShort()); int x = toUnsigned(buffer.getShort());