Merge commands with other control events

Several commands were grouped under the same event type "command", with
a separate field to indicate the actual command.

Move these commands at the same level as other control events. It will
allow to implement commands with arguments.
This commit is contained in:
Romain Vimont 2019-05-30 16:07:24 +02:00
parent 3b4366e5bf
commit 63909fd10d
6 changed files with 29 additions and 60 deletions

View file

@ -43,9 +43,11 @@ control_event_serialize(const struct control_event *event, unsigned char *buf) {
buffer_write32be(&buf[13], (uint32_t) event->scroll_event.hscroll);
buffer_write32be(&buf[17], (uint32_t) event->scroll_event.vscroll);
return 21;
case CONTROL_EVENT_TYPE_COMMAND:
buf[1] = event->command_event.action;
return 2;
case CONTROL_EVENT_TYPE_BACK_OR_SCREEN_ON:
case CONTROL_EVENT_TYPE_EXPAND_NOTIFICATION_PANEL:
case CONTROL_EVENT_TYPE_COLLAPSE_NOTIFICATION_PANEL:
// no additional data
return 1;
default:
LOGW("Unknown event type: %u", (unsigned) event->type);
return 0;

View file

@ -17,13 +17,9 @@ enum control_event_type {
CONTROL_EVENT_TYPE_TEXT,
CONTROL_EVENT_TYPE_MOUSE,
CONTROL_EVENT_TYPE_SCROLL,
CONTROL_EVENT_TYPE_COMMAND,
};
enum control_event_command {
CONTROL_EVENT_COMMAND_BACK_OR_SCREEN_ON,
CONTROL_EVENT_COMMAND_EXPAND_NOTIFICATION_PANEL,
CONTROL_EVENT_COMMAND_COLLAPSE_NOTIFICATION_PANEL,
CONTROL_EVENT_TYPE_BACK_OR_SCREEN_ON,
CONTROL_EVENT_TYPE_EXPAND_NOTIFICATION_PANEL,
CONTROL_EVENT_TYPE_COLLAPSE_NOTIFICATION_PANEL,
};
struct control_event {
@ -47,9 +43,6 @@ struct control_event {
int32_t hscroll;
int32_t vscroll;
} scroll_event;
struct {
enum control_event_command action;
} command_event;
};
};

View file

@ -99,9 +99,7 @@ action_menu(struct controller *controller, int actions) {
static void
press_back_or_turn_screen_on(struct controller *controller) {
struct control_event control_event;
control_event.type = CONTROL_EVENT_TYPE_COMMAND;
control_event.command_event.action =
CONTROL_EVENT_COMMAND_BACK_OR_SCREEN_ON;
control_event.type = CONTROL_EVENT_TYPE_BACK_OR_SCREEN_ON;
if (!controller_push_event(controller, &control_event)) {
LOGW("Cannot turn screen on");
@ -111,9 +109,7 @@ press_back_or_turn_screen_on(struct controller *controller) {
static void
expand_notification_panel(struct controller *controller) {
struct control_event control_event;
control_event.type = CONTROL_EVENT_TYPE_COMMAND;
control_event.command_event.action =
CONTROL_EVENT_COMMAND_EXPAND_NOTIFICATION_PANEL;
control_event.type = CONTROL_EVENT_TYPE_EXPAND_NOTIFICATION_PANEL;
if (!controller_push_event(controller, &control_event)) {
LOGW("Cannot expand notification panel");
@ -123,9 +119,7 @@ expand_notification_panel(struct controller *controller) {
static void
collapse_notification_panel(struct controller *controller) {
struct control_event control_event;
control_event.type = CONTROL_EVENT_TYPE_COMMAND;
control_event.command_event.action =
CONTROL_EVENT_COMMAND_COLLAPSE_NOTIFICATION_PANEL;
control_event.type = CONTROL_EVENT_TYPE_COLLAPSE_NOTIFICATION_PANEL;
if (!controller_push_event(controller, &control_event)) {
LOGW("Cannot collapse notification panel");

View file

@ -9,16 +9,14 @@ public final class ControlEvent {
public static final int TYPE_TEXT = 1;
public static final int TYPE_MOUSE = 2;
public static final int TYPE_SCROLL = 3;
public static final int TYPE_COMMAND = 4;
public static final int COMMAND_BACK_OR_SCREEN_ON = 0;
public static final int COMMAND_EXPAND_NOTIFICATION_PANEL = 1;
public static final int COMMAND_COLLAPSE_NOTIFICATION_PANEL = 2;
public static final int TYPE_BACK_OR_SCREEN_ON = 4;
public static final int TYPE_EXPAND_NOTIFICATION_PANEL = 5;
public static final int TYPE_COLLAPSE_NOTIFICATION_PANEL = 6;
private int type;
private String text;
private int metaState; // KeyEvent.META_*
private int action; // KeyEvent.ACTION_* or MotionEvent.ACTION_* or COMMAND_*
private int action; // KeyEvent.ACTION_* or MotionEvent.ACTION_*
private int keycode; // KeyEvent.KEYCODE_*
private int buttons; // MotionEvent.BUTTON_*
private Position position;
@ -62,10 +60,9 @@ public final class ControlEvent {
return event;
}
public static ControlEvent createCommandControlEvent(int action) {
public static ControlEvent createSimpleControlEvent(int type) {
ControlEvent event = new ControlEvent();
event.type = TYPE_COMMAND;
event.action = action;
event.type = type;
return event;
}

View file

@ -11,7 +11,6 @@ public class ControlEventReader {
private static final int KEYCODE_PAYLOAD_LENGTH = 9;
private static final int MOUSE_PAYLOAD_LENGTH = 17;
private static final int SCROLL_PAYLOAD_LENGTH = 20;
private static final int COMMAND_PAYLOAD_LENGTH = 1;
public static final int TEXT_MAX_LENGTH = 300;
private static final int RAW_BUFFER_SIZE = 1024;
@ -64,8 +63,10 @@ public class ControlEventReader {
case ControlEvent.TYPE_SCROLL:
controlEvent = parseScrollControlEvent();
break;
case ControlEvent.TYPE_COMMAND:
controlEvent = parseCommandControlEvent();
case ControlEvent.TYPE_BACK_OR_SCREEN_ON:
case ControlEvent.TYPE_EXPAND_NOTIFICATION_PANEL:
case ControlEvent.TYPE_COLLAPSE_NOTIFICATION_PANEL:
controlEvent = ControlEvent.createSimpleControlEvent(type);
break;
default:
Ln.w("Unknown event type: " + type);
@ -123,14 +124,6 @@ public class ControlEventReader {
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) {
int x = buffer.getInt();
int y = buffer.getInt();

View file

@ -77,8 +77,14 @@ public class EventController {
case ControlEvent.TYPE_SCROLL:
injectScroll(controlEvent.getPosition(), controlEvent.getHScroll(), controlEvent.getVScroll());
break;
case ControlEvent.TYPE_COMMAND:
executeCommand(controlEvent.getAction());
case ControlEvent.TYPE_BACK_OR_SCREEN_ON:
pressBackOrTurnScreenOn();
break;
case ControlEvent.TYPE_EXPAND_NOTIFICATION_PANEL:
device.expandNotificationPanel();
break;
case ControlEvent.TYPE_COLLAPSE_NOTIFICATION_PANEL:
device.collapsePanels();
break;
default:
// do nothing
@ -170,20 +176,4 @@ public class EventController {
int keycode = device.isScreenOn() ? KeyEvent.KEYCODE_BACK : KeyEvent.KEYCODE_POWER;
return injectKeycode(keycode);
}
private boolean executeCommand(int action) {
switch (action) {
case ControlEvent.COMMAND_BACK_OR_SCREEN_ON:
return pressBackOrTurnScreenOn();
case ControlEvent.COMMAND_EXPAND_NOTIFICATION_PANEL:
device.expandNotificationPanel();
return true;
case ControlEvent.COMMAND_COLLAPSE_NOTIFICATION_PANEL:
device.collapsePanels();
return true;
default:
Ln.w("Unsupported command: " + action);
}
return false;
}
}