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,60 +47,85 @@ 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:
int action = toUnsigned(buffer.get()); controlEvent = parseTextControlEvent();
int keycode = buffer.getInt(); break;
int metaState = buffer.getInt(); case ControlEvent.TYPE_MOUSE:
return ControlEvent.createKeycodeControlEvent(action, keycode, metaState); controlEvent = parseMouseControlEvent();
} break;
case ControlEvent.TYPE_TEXT: { case ControlEvent.TYPE_SCROLL:
if (buffer.remaining() < 1) { controlEvent = parseScrollControlEvent();
break; break;
} case ControlEvent.TYPE_COMMAND:
int len = toUnsigned(buffer.get()); controlEvent = parseCommandControlEvent();
if (buffer.remaining() < len) { break;
break;
}
buffer.get(textBuffer, 0, len);
String text = new String(textBuffer, 0, len, StandardCharsets.UTF_8);
return ControlEvent.createTextControlEvent(text);
}
case ControlEvent.TYPE_MOUSE: {
if (buffer.remaining() < MOUSE_PAYLOAD_LENGTH) {
break;
}
int action = toUnsigned(buffer.get());
int buttons = buffer.getInt();
Position position = readPosition(buffer);
return ControlEvent.createMotionControlEvent(action, buttons, position);
}
case ControlEvent.TYPE_SCROLL: {
if (buffer.remaining() < SCROLL_PAYLOAD_LENGTH) {
break;
}
Position position = readPosition(buffer);
int hScroll = buffer.getInt();
int vScroll = buffer.getInt();
return ControlEvent.createScrollControlEvent(position, hScroll, vScroll);
}
case ControlEvent.TYPE_COMMAND: {
if (buffer.remaining() < COMMAND_PAYLOAD_LENGTH) {
break;
}
int action = toUnsigned(buffer.get());
return ControlEvent.createCommandControlEvent(action);
}
default: default:
Ln.w("Unknown event type: " + type); Ln.w("Unknown event type: " + type);
controlEvent = null;
break;
} }
// failure, reset savedPosition if (controlEvent == null) {
buffer.position(savedPosition); // failure, reset savedPosition
return null; 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);
}
private ControlEvent parseTextControlEvent() {
if (buffer.remaining() < 1) {
return null;
}
int len = toUnsigned(buffer.get());
if (buffer.remaining() < len) {
return null;
}
buffer.get(textBuffer, 0, len);
String text = new String(textBuffer, 0, len, StandardCharsets.UTF_8);
return ControlEvent.createTextControlEvent(text);
}
private ControlEvent parseMouseControlEvent() {
if (buffer.remaining() < MOUSE_PAYLOAD_LENGTH) {
return null;
}
int action = toUnsigned(buffer.get());
int buttons = buffer.getInt();
Position position = readPosition(buffer);
return ControlEvent.createMotionControlEvent(action, buttons, position);
}
private ControlEvent parseScrollControlEvent() {
if (buffer.remaining() < SCROLL_PAYLOAD_LENGTH) {
return null;
}
Position position = readPosition(buffer);
int hScroll = buffer.getInt();
int vScroll = buffer.getInt();
return ControlEvent.createScrollControlEvent(position, hScroll, vScroll);
}
private ControlEvent parseCommandControlEvent() {
if (buffer.remaining() < COMMAND_PAYLOAD_LENGTH) {
return null;
}
int action = toUnsigned(buffer.get());
return ControlEvent.createCommandControlEvent(action);
} }
private static Position readPosition(ByteBuffer buffer) { private static Position readPosition(ByteBuffer buffer) {