2017-12-12 22:12:07 +08:00
|
|
|
#ifndef COMMAND_H
|
|
|
|
#define COMMAND_H
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
2017-12-16 00:34:16 +08:00
|
|
|
#include <SDL2/SDL_stdinc.h>
|
2017-12-12 22:12:07 +08:00
|
|
|
#include <SDL2/SDL_platform.h>
|
|
|
|
|
|
|
|
// <https://stackoverflow.com/a/44383330/1987178>
|
|
|
|
#ifdef _WIN32
|
2018-03-16 15:51:46 +08:00
|
|
|
# define PRIexitcode "lu"
|
2017-12-12 22:12:07 +08:00
|
|
|
# ifdef _WIN64
|
|
|
|
# define PRIsizet PRIu64
|
|
|
|
# else
|
|
|
|
# define PRIsizet PRIu32
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# define PRIsizet "zu"
|
|
|
|
# define PRIexitcode "d"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __WINDOWS__
|
Replace SDL_net by custom implementation
SDL_net is not very suitable for scrcpy.
For example, SDLNet_TCP_Accept() is non-blocking, so we have to wrap it
by calling many SDL_Net-specific functions to make it blocking.
But above all, SDLNet_TCP_Open() is a server socket only when no IP is
provided; otherwise, it's a client socket. Therefore, it is not possible
to create a server socket bound to localhost, so it accepts connections
from anywhere.
This is a problem for scrcpy, because on start, the application listens
for nearly 1 second until it accepts the first connection, supposedly
from the device. If someone on the local network manages to connect to
the server socket first, then they can stream arbitrary H.264 video.
This may be troublesome, for example during a public presentation ;-)
Provide our own simplified API (net.h) instead, implemented for the
different platforms.
2018-02-16 05:59:21 +08:00
|
|
|
# include <winsock2.h> // not needed here, but must never be included AFTER windows.h
|
2017-12-12 22:12:07 +08:00
|
|
|
# include <windows.h>
|
|
|
|
# define PROCESS_NONE NULL
|
|
|
|
typedef HANDLE process_t;
|
|
|
|
typedef DWORD exit_code_t;
|
|
|
|
#else
|
|
|
|
# include <sys/types.h>
|
|
|
|
# define PROCESS_NONE -1
|
|
|
|
typedef pid_t process_t;
|
|
|
|
typedef int exit_code_t;
|
|
|
|
#endif
|
|
|
|
# define NO_EXIT_CODE -1
|
|
|
|
|
2018-09-04 14:42:25 +08:00
|
|
|
enum process_result {
|
|
|
|
PROCESS_SUCCESS,
|
|
|
|
PROCESS_ERROR_GENERIC,
|
|
|
|
PROCESS_ERROR_MISSING_BINARY,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum process_result cmd_execute(const char *path, const char *const argv[], process_t *process);
|
2017-12-12 22:12:07 +08:00
|
|
|
SDL_bool cmd_terminate(process_t pid);
|
|
|
|
SDL_bool cmd_simple_wait(process_t pid, exit_code_t *exit_code);
|
|
|
|
|
|
|
|
process_t adb_execute(const char *serial, const char *const adb_cmd[], int len);
|
|
|
|
process_t adb_forward(const char *serial, uint16_t local_port, const char *device_socket_name);
|
2018-03-12 15:35:51 +08:00
|
|
|
process_t adb_forward_remove(const char *serial, uint16_t local_port);
|
2017-12-12 22:12:07 +08:00
|
|
|
process_t adb_reverse(const char *serial, const char *device_socket_name, uint16_t local_port);
|
|
|
|
process_t adb_reverse_remove(const char *serial, const char *device_socket_name);
|
|
|
|
process_t adb_push(const char *serial, const char *local, const char *remote);
|
2018-04-29 06:17:34 +08:00
|
|
|
process_t adb_install(const char *serial, const char *local);
|
2017-12-12 22:12:07 +08:00
|
|
|
|
2018-02-08 22:16:27 +08:00
|
|
|
// convenience function to wait for a successful process execution
|
|
|
|
// automatically log process errors with the provided process name
|
|
|
|
SDL_bool process_check_success(process_t process, const char *name);
|
|
|
|
|
2017-12-12 22:12:07 +08:00
|
|
|
#endif
|