Skip to content

Commit

Permalink
Make sure commands are performed synchronously
Browse files Browse the repository at this point in the history
When scripting commands, request messages would pile up and overflow the
message queue resulting in crash or errors. To avoid this we now wait
for each command to finish (wait for ok response) before firing the next
command.
  • Loading branch information
Martin Lund committed Jan 21, 2023
1 parent 74c0625 commit b383289
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 12 deletions.
16 changes: 16 additions & 0 deletions src/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,8 @@ void do_keyboard_start(void *message)
{
UNUSED(message);
keyboard_create();

msg_send_rsp_ok();
}

void do_keyboard_start_request(void)
Expand All @@ -491,6 +493,8 @@ void do_keyboard_start_request(void)
msg_create(&message, REQ_KBD_START, NULL, 0);
msg_send(message);
msg_destroy(message);

msg_receive_rsp_ok();
}

void do_keyboard_keydown(void *message)
Expand All @@ -505,6 +509,8 @@ void do_keyboard_keydown(void *message)
}

keyboard_press(*key);

msg_send_rsp_ok();
}

void do_keyboard_keydown_request(uint32_t key)
Expand All @@ -514,6 +520,8 @@ void do_keyboard_keydown_request(uint32_t key)
msg_create(&message, REQ_KBD_KEYDOWN, &key, sizeof(key));
msg_send(message);
msg_destroy(message);

msg_receive_rsp_ok();
}

void do_keyboard_keyup(void *message)
Expand All @@ -528,6 +536,8 @@ void do_keyboard_keyup(void *message)
}

keyboard_release(*key);

msg_send_rsp_ok();
}

void do_keyboard_keyup_request(uint32_t key)
Expand All @@ -537,6 +547,8 @@ void do_keyboard_keyup_request(uint32_t key)
msg_create(&message, REQ_KBD_KEYUP, &key, sizeof(key));
msg_send(message);
msg_destroy(message);

msg_receive_rsp_ok();
}

void do_keyboard_key(void *message)
Expand All @@ -553,6 +565,8 @@ void do_keyboard_key(void *message)
keyboard_press(*key);
usleep(14*1000);
keyboard_release(*key);

msg_send_rsp_ok();
}

void do_keyboard_key_request(uint32_t key)
Expand All @@ -562,6 +576,8 @@ void do_keyboard_key_request(uint32_t key)
msg_create(&message, REQ_KBD_KEY, &key, sizeof(key));
msg_send(message);
msg_destroy(message);

msg_receive_rsp_ok();
}

void do_keyboard_key_requests(const wchar_t *wc_string)
Expand Down
31 changes: 31 additions & 0 deletions src/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,34 @@ int msg_receive(void **message)

return 0;
}

void msg_send_rsp_ok(void)
{
void *message = NULL;

// Send response
msg_create(&message, RSP_OK, NULL, 0);
msg_send(message);
msg_destroy(message);
}

void msg_receive_rsp_ok(void)
{
void *message = NULL;
message_header_t *header;

// Receive response
msg_receive(&message);
header = message;
if (header->type == RSP_ERROR)
{
// char *rsp_text = message + sizeof(message_header_t);
// error_printf("%s", rsp_text);
}
else if (header->type != RSP_OK)
{
warning_printf("Invalid message type received\n");
}

msg_destroy(message);
}
4 changes: 4 additions & 0 deletions src/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ typedef enum
RSP_STATUS,
REQ_STOP,
RSP_STOP,
RSP_OK,
RSP_ERROR,
} message_type_t;

void message_queue_server_open(void);
Expand All @@ -55,3 +57,5 @@ int msg_create(void **message, message_type_t type, void *payload, uint32_t payl
void msg_destroy(void *message);
int msg_send(void *message);
int msg_receive(void **message);
void msg_send_rsp_ok(void);
void msg_receive_rsp_ok(void);
24 changes: 24 additions & 0 deletions src/mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ void do_mouse_click(void *message)
}

mouse_click(*button);

msg_send_rsp_ok();
}

void do_mouse_click_request(int button)
Expand All @@ -251,6 +253,8 @@ void do_mouse_click_request(int button)
msg_create(&message, REQ_MOUSE_CLICK, &button, sizeof(button));
msg_send(message);
msg_destroy(message);

msg_receive_rsp_ok();
}

void do_mouse_scroll(void *message)
Expand All @@ -265,6 +269,8 @@ void do_mouse_scroll(void *message)
}

mouse_scroll(*ticks);

msg_send_rsp_ok();
}

void do_mouse_scroll_request(int32_t ticks)
Expand All @@ -274,6 +280,8 @@ void do_mouse_scroll_request(int32_t ticks)
msg_create(&message, REQ_MOUSE_SCROLL, &ticks, sizeof(ticks));
msg_send(message);
msg_destroy(message);

msg_receive_rsp_ok();
}

void do_mouse_down(void *message)
Expand All @@ -288,6 +296,8 @@ void do_mouse_down(void *message)
}

mouse_press(*button);

msg_send_rsp_ok();
}

void do_mouse_down_request(int button)
Expand All @@ -297,6 +307,8 @@ void do_mouse_down_request(int button)
msg_create(&message, REQ_MOUSE_DOWN, &button, sizeof(button));
msg_send(message);
msg_destroy(message);

msg_receive_rsp_ok();
}

void do_mouse_up(void *message)
Expand All @@ -311,6 +323,8 @@ void do_mouse_up(void *message)
}

mouse_release(*button);

msg_send_rsp_ok();
}

void do_mouse_up_request(int button)
Expand All @@ -320,6 +334,8 @@ void do_mouse_up_request(int button)
msg_create(&message, REQ_MOUSE_UP, &button, sizeof(button));
msg_send(message);
msg_destroy(message);

msg_receive_rsp_ok();
}

void do_mouse_move(void *message)
Expand All @@ -334,6 +350,8 @@ void do_mouse_move(void *message)
}

mouse_move(move->x, move->y);

msg_send_rsp_ok();
}

void do_mouse_move_request(int32_t x, int32_t y)
Expand All @@ -347,6 +365,8 @@ void do_mouse_move_request(int32_t x, int32_t y)
msg_create(&message, REQ_MOUSE_MOVE, &mouse_move_data, sizeof(mouse_move_data_t));
msg_send(message);
msg_destroy(message);

msg_receive_rsp_ok();
}

void do_mouse_start(void *message)
Expand All @@ -361,6 +381,8 @@ void do_mouse_start(void *message)
}

mouse_create(data->x_max, data->y_max);

msg_send_rsp_ok();
}

void do_mouse_start_request(uint32_t x_max, uint32_t y_max)
Expand All @@ -376,5 +398,7 @@ void do_mouse_start_request(uint32_t x_max, uint32_t y_max)
msg_create(&message, REQ_MOUSE_START, &data, sizeof(data));
msg_send(message);
msg_destroy(message);

msg_receive_rsp_ok();
}

28 changes: 16 additions & 12 deletions src/service.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,6 @@ void daemonize(void)
freopen( "/dev/null", "w", stderr);
}

void do_service_stop_request(device_t device)
{
void *message = NULL;

debug_printf("Sending stop message!\n");

msg_create(&message, REQ_STOP, &device, sizeof(device));

msg_send(message);
msg_destroy(message);
}

void do_service_stop(void *message)
{
message_header_t *header = message;
Expand Down Expand Up @@ -153,6 +141,22 @@ void do_service_stop(void *message)
case DEV_NONE:
break;
}

msg_send_rsp_ok();
}

void do_service_stop_request(device_t device)
{
void *message = NULL;

debug_printf("Sending stop message!\n");

msg_create(&message, REQ_STOP, &device, sizeof(device));

msg_send(message);
msg_destroy(message);

msg_receive_rsp_ok();
}

void do_service_status(void *message)
Expand Down
8 changes: 8 additions & 0 deletions src/touch.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ void do_touch_tap(void *message)

debug_printf("Touch tap at %d,%d for %d ms\n", tap->x, tap->y, tap->duration);
touch_tap(tap->x, tap->y, tap->duration);

msg_send_rsp_ok();
}

void do_touch_tap_request(uint32_t x, uint32_t y, uint32_t duration)
Expand All @@ -217,6 +219,8 @@ void do_touch_tap_request(uint32_t x, uint32_t y, uint32_t duration)
msg_create(&message, REQ_TOUCH_TAP, &touch_tap_data, sizeof(touch_tap_data_t));
msg_send(message);
msg_destroy(message);

msg_receive_rsp_ok();
}

void do_touch_start(void *message)
Expand All @@ -231,6 +235,8 @@ void do_touch_start(void *message)
}

touch_create(data->x_max, data->y_max, data->slots);

msg_send_rsp_ok();
}

void do_touch_start_request(uint32_t x_max, uint32_t y_max, uint8_t slots)
Expand All @@ -245,5 +251,7 @@ void do_touch_start_request(uint32_t x_max, uint32_t y_max, uint8_t slots)
msg_create(&message, REQ_TOUCH_START, &data, sizeof(data));
msg_send(message);
msg_destroy(message);

msg_receive_rsp_ok();
}

0 comments on commit b383289

Please sign in to comment.