t/ctl: Refactor code to make more tidy

This commit is contained in:
Jakob Bornecrantz 2020-08-18 12:50:57 +01:00 committed by Jakob Bornecrantz
parent 4e6a137c62
commit 7b9d7091bd

View file

@ -20,9 +20,76 @@ typedef enum op_mode
MODE_GET,
MODE_SET_PRIMARY,
MODE_SET_FOCUSED,
} op_mode_t;
int
get_mode(struct ipc_connection *ipc_c)
{
struct ipc_client_list clients;
xrt_result_t r;
r = ipc_call_system_get_clients(ipc_c, &clients);
if (r != XRT_SUCCESS) {
printf("failed to get client list.\n");
exit(1);
}
for (uint32_t i = 0; i < IPC_MAX_CLIENTS; i++) {
if (clients.ids[i] < 0) {
continue;
}
struct ipc_app_state cs;
r = ipc_call_system_get_client_info(ipc_c, i, &cs);
if (r != XRT_SUCCESS) {
printf(
"failed to get client info "
"for client %d.\n",
i);
return 1;
}
printf(
"id: %d\tact: %d\tdisp: "
"%d\tfoc: %d\tovly: %d\tz: "
"%d\tpid: "
"%d\t %s\t\n",
clients.ids[i], cs.session_active, cs.session_visible,
cs.session_focused, cs.session_overlay, cs.z_order, cs.pid,
cs.info.application_name);
}
return 0;
}
int
set_primary(struct ipc_connection *ipc_c, int client_id)
{
xrt_result_t r;
r = ipc_call_system_set_primary_client(ipc_c, client_id);
if (r != XRT_SUCCESS) {
printf("failed to set active client to %d.\n", client_id);
return 1;
}
return 0;
}
int
set_focused(struct ipc_connection *ipc_c, int client_id)
{
xrt_result_t r;
r = ipc_call_system_set_focused_client(ipc_c, client_id);
if (r != XRT_SUCCESS) {
printf("failed to set focused client to %d.\n", client_id);
return 1;
}
return 0;
}
int
main(int argc, char *argv[])
@ -99,69 +166,24 @@ main(int argc, char *argv[])
sizeof(cs.info.application_name), "%s", "monado-ctl");
xrt_result_t r = ipc_call_system_set_client_info(&ipc_c, &cs);
if (r != XRT_SUCCESS) {
printf("failed to set client info.\n");
exit(1);
}
switch (op_mode) {
case MODE_GET: {
struct ipc_client_list clients;
xrt_result_t r =
ipc_call_system_get_clients(&ipc_c, &clients);
if (r != XRT_SUCCESS) {
printf("failed to get client list.\n");
exit(1);
}
for (uint32_t i = 0; i < IPC_MAX_CLIENTS; i++) {
if (clients.ids[i] >= 0) {
struct ipc_app_state cs;
xrt_result_t r =
ipc_call_system_get_client_info(
&ipc_c, i, &cs);
if (r != XRT_SUCCESS) {
printf(
"failed to get client info "
"for client %d.\n",
i);
exit(1);
}
printf(
"id: %d\tact: %d\tdisp: "
"%d\tfoc: %d\tovly: %d\tz: "
"%d\tpid: "
"%d\t %s\t\n",
clients.ids[i], cs.session_active,
cs.session_visible,
cs.session_focused,
cs.session_overlay, cs.z_order,
cs.pid, cs.info.application_name);
}
}
} break;
case MODE_SET_PRIMARY: {
xrt_result_t r =
ipc_call_system_set_primary_client(&ipc_c, s_val);
if (r != XRT_SUCCESS) {
printf("failed to set active client to %d.\n",
s_val);
exit(1);
}
} break;
case MODE_SET_FOCUSED: {
xrt_result_t r =
ipc_call_system_set_focused_client(&ipc_c, s_val);
if (r != XRT_SUCCESS) {
printf("failed to set focused client to %d.\n",
s_val);
exit(1);
}
} break;
case MODE_GET:
exit(get_mode(&ipc_c));
break;
case MODE_SET_PRIMARY:
exit(set_primary(&ipc_c, s_val));
break;
case MODE_SET_FOCUSED:
exit(set_focused(&ipc_c, s_val));
break;
default: printf("Unrecognised operation mode.\n"); exit(1);
}
}
close(fd);
}