The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

t/ctl: Refactor connection opening

authored by

Jakob Bornecrantz and committed by
Jakob Bornecrantz
5c08dc16 691881f5

+79 -37
+79 -37
src/xrt/targets/ctl/main.c
··· 14 14 #include <sys/un.h> 15 15 #include <ctype.h> 16 16 #include <unistd.h> 17 + #include <sys/mman.h> 18 + 17 19 18 20 typedef enum op_mode 19 21 { ··· 22 24 MODE_SET_FOCUSED, 23 25 MODE_TOGGLE_IO, 24 26 } op_mode_t; 27 + 28 + static int 29 + do_connect(struct ipc_connection *ipc_c); 30 + 25 31 26 32 int 27 33 get_mode(struct ipc_connection *ipc_c) ··· 120 126 int 121 127 main(int argc, char *argv[]) 122 128 { 123 - struct ipc_connection ipc_c; 124 - os_mutex_init(&ipc_c.mutex); 125 - 126 129 op_mode_t op_mode = MODE_GET; 127 130 128 131 // parse arguments ··· 166 169 } 167 170 } 168 171 169 - bool socket_created = true; 170 - bool socket_connected = true; 172 + struct ipc_connection ipc_c; 173 + os_mutex_init(&ipc_c.mutex); 174 + int ret = do_connect(&ipc_c); 175 + if (ret != 0) { 176 + return ret; 177 + } 178 + 179 + switch (op_mode) { 180 + case MODE_GET: exit(get_mode(&ipc_c)); break; 181 + case MODE_SET_PRIMARY: exit(set_primary(&ipc_c, s_val)); break; 182 + case MODE_SET_FOCUSED: exit(set_focused(&ipc_c, s_val)); break; 183 + case MODE_TOGGLE_IO: exit(toggle_io(&ipc_c, s_val)); break; 184 + default: printf("Unrecognised operation mode.\n"); exit(1); 185 + } 186 + 187 + return 0; 188 + } 189 + 190 + static int 191 + do_connect(struct ipc_connection *ipc_c) 192 + { 193 + int ret; 194 + 171 195 172 - int fd; 173 - struct sockaddr_un addr; 196 + /* 197 + * Conenct. 198 + */ 174 199 175 - if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) { 176 - printf("Socket Create Error!\n"); 177 - socket_created = false; 200 + ipc_c->imc.socket_fd = socket(PF_UNIX, SOCK_STREAM, 0); 201 + if (ipc_c->imc.socket_fd < 0) { 202 + IPC_ERROR(ipc_c, "Socket Create Error!\n"); 203 + return -1; 178 204 } 179 205 180 - if (socket_created) { 181 - memset(&addr, 0, sizeof(addr)); 182 - addr.sun_family = AF_UNIX; 183 - strcpy(addr.sun_path, IPC_MSG_SOCK_FILE); 184 - if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) { 185 - printf("Socket Connect error!\n"); 186 - socket_connected = false; 187 - } 206 + struct sockaddr_un addr = {0}; 207 + addr.sun_family = AF_UNIX; 208 + strcpy(addr.sun_path, IPC_MSG_SOCK_FILE); 209 + 210 + ret = connect(ipc_c->imc.socket_fd, // socket 211 + (struct sockaddr *)&addr, // address 212 + sizeof(addr)); // size 213 + if (ret < 0) { 214 + IPC_ERROR(ipc_c, "Socket Connect error!\n"); 215 + return -1; 188 216 } 189 217 190 - if (socket_connected) { 191 - ipc_c.imc.socket_fd = fd; 192 218 193 - struct ipc_app_state cs; 194 - cs.pid = getpid(); 219 + /* 220 + * Client info. 221 + */ 195 222 196 - snprintf(cs.info.application_name, 197 - sizeof(cs.info.application_name), "%s", "monado-ctl"); 223 + struct ipc_app_state cs; 224 + cs.pid = getpid(); 225 + snprintf(cs.info.application_name, sizeof(cs.info.application_name), 226 + "%s", "monado-ctl"); 198 227 199 - xrt_result_t r = ipc_call_system_set_client_info(&ipc_c, &cs); 200 - if (r != XRT_SUCCESS) { 201 - printf("failed to set client info.\n"); 202 - exit(1); 203 - } 228 + xrt_result_t r = ipc_call_system_set_client_info(ipc_c, &cs); 229 + if (r != XRT_SUCCESS) { 230 + IPC_ERROR(ipc_c, "failed to set client info.\n"); 231 + return -1; 232 + } 204 233 205 - switch (op_mode) { 206 - case MODE_GET: exit(get_mode(&ipc_c)); break; 207 - case MODE_SET_PRIMARY: exit(set_primary(&ipc_c, s_val)); break; 208 - case MODE_SET_FOCUSED: exit(set_focused(&ipc_c, s_val)); break; 209 - case MODE_TOGGLE_IO: exit(toggle_io(&ipc_c, s_val)); break; 210 - default: printf("Unrecognised operation mode.\n"); exit(1); 211 - } 234 + 235 + /* 236 + * Shared memory. 237 + */ 238 + 239 + // get our xdev shm from the server and mmap it 240 + r = ipc_call_instance_get_shm_fd(ipc_c, &ipc_c->ism_handle, 1); 241 + if (r != XRT_SUCCESS) { 242 + IPC_ERROR(ipc_c, "Failed to retrieve shm fd"); 243 + return -1; 244 + } 245 + 246 + const int flags = MAP_SHARED; 247 + const int access = PROT_READ | PROT_WRITE; 248 + const size_t size = sizeof(struct ipc_shared_memory); 249 + 250 + ipc_c->ism = mmap(NULL, size, access, flags, ipc_c->ism_handle, 0); 251 + if (ipc_c->ism == NULL) { 252 + IPC_ERROR(ipc_c, "Failed to mmap shm "); 253 + return -1; 212 254 } 213 255 214 - close(fd); 256 + return 0; 215 257 }