command.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #ifndef COMMAND_H
  2. #define COMMAND_H
  3. #include <stdbool.h>
  4. #include <inttypes.h>
  5. #ifdef _WIN32
  6. // not needed here, but winsock2.h must never be included AFTER windows.h
  7. # include <winsock2.h>
  8. # include <windows.h>
  9. # define PATH_SEPARATOR '\\'
  10. # define PRIexitcode "lu"
  11. // <https://stackoverflow.com/a/44383330/1987178>
  12. # ifdef _WIN64
  13. # define PRIsizet PRIu64
  14. # else
  15. # define PRIsizet PRIu32
  16. # endif
  17. # define PROCESS_NONE NULL
  18. # define NO_EXIT_CODE -1u // max value as unsigned
  19. typedef HANDLE process_t;
  20. typedef DWORD exit_code_t;
  21. #else
  22. # include <sys/types.h>
  23. # define PATH_SEPARATOR '/'
  24. # define PRIsizet "zu"
  25. # define PRIexitcode "d"
  26. # define PROCESS_NONE -1
  27. # define NO_EXIT_CODE -1
  28. typedef pid_t process_t;
  29. typedef int exit_code_t;
  30. #endif
  31. enum process_result {
  32. PROCESS_SUCCESS,
  33. PROCESS_ERROR_GENERIC,
  34. PROCESS_ERROR_MISSING_BINARY,
  35. };
  36. enum process_result
  37. cmd_execute(const char *const argv[], process_t *process);
  38. bool
  39. cmd_terminate(process_t pid);
  40. bool
  41. cmd_simple_wait(process_t pid, exit_code_t *exit_code);
  42. process_t
  43. adb_execute(const char *serial, const char *const adb_cmd[], size_t len);
  44. process_t
  45. adb_forward(const char *serial, uint16_t local_port,
  46. const char *device_socket_name);
  47. process_t
  48. adb_forward_remove(const char *serial, uint16_t local_port);
  49. process_t
  50. adb_reverse(const char *serial, const char *device_socket_name,
  51. uint16_t local_port);
  52. process_t
  53. adb_reverse_remove(const char *serial, const char *device_socket_name);
  54. process_t
  55. adb_push(const char *serial, const char *local, const char *remote);
  56. process_t
  57. adb_install(const char *serial, const char *local);
  58. // convenience function to wait for a successful process execution
  59. // automatically log process errors with the provided process name
  60. bool
  61. process_check_success(process_t proc, const char *name);
  62. // return the absolute path of the executable (the scrcpy binary)
  63. // may be NULL on error; to be freed by SDL_free
  64. char *
  65. get_executable_path(void);
  66. // returns true if the file exists and is not a directory
  67. bool
  68. is_regular_file(const char *path);
  69. #endif