diff --git a/app/src/main.c b/app/src/main.c index 12c65ed4..348c3df0 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -110,6 +110,14 @@ static void usage(const char *arg0) { " --window-title text\n" " Set a custom window title.\n" "\n" + " --window-x value\n" + " Set the initial window horizontal position.\n" + " Default is -1 (automatic).\n" + "\n" + " --window-y value\n" + " Set the initial window vertical position.\n" + " Default is -1 (automatic).\n" + "\n" "Shortcuts:\n" "\n" " " CTRL_OR_CMD "+f\n" @@ -250,6 +258,27 @@ parse_max_size(char *optarg, uint16_t *max_size) { return true; } +static bool +parse_window_position(char *optarg, int16_t *position) { + char *endptr; + if (*optarg == '\0') { + LOGE("Window position parameter is empty"); + return false; + } + long value = strtol(optarg, &endptr, 0); + if (*endptr != '\0') { + LOGE("Invalid window position: %s", optarg); + return false; + } + if (value < -1 || value > 0x7fff) { + LOGE("Window position must be between -1 and 32767: %ld", value); + return false; + } + + *position = (int16_t) value; + return true; +} + static bool parse_port(char *optarg, uint16_t *port) { char *endptr; @@ -308,6 +337,8 @@ guess_record_format(const char *filename) { #define OPT_CROP 1004 #define OPT_RECORD_FORMAT 1005 #define OPT_PREFER_TEXT 1006 +#define OPT_WINDOW_X 1007 +#define OPT_WINDOW_Y 1008 static bool parse_args(struct args *args, int argc, char *argv[]) { @@ -331,8 +362,9 @@ parse_args(struct args *args, int argc, char *argv[]) { {"turn-screen-off", no_argument, NULL, 'S'}, {"prefer-text", no_argument, NULL, OPT_PREFER_TEXT}, {"version", no_argument, NULL, 'v'}, - {"window-title", required_argument, NULL, - OPT_WINDOW_TITLE}, + {"window-title", required_argument, NULL, OPT_WINDOW_TITLE}, + {"window-x", required_argument, NULL, OPT_WINDOW_X}, + {"window-y", required_argument, NULL, OPT_WINDOW_Y}, {NULL, 0, NULL, 0 }, }; @@ -410,6 +442,16 @@ parse_args(struct args *args, int argc, char *argv[]) { case OPT_WINDOW_TITLE: opts->window_title = optarg; break; + case OPT_WINDOW_X: + if (!parse_window_position(optarg, &opts->window_x)) { + return false; + } + break; + case OPT_WINDOW_Y: + if (!parse_window_position(optarg, &opts->window_y)) { + return false; + } + break; case OPT_PUSH_TARGET: opts->push_target = optarg; break; diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index 0e56696a..d9f0e308 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -387,7 +387,8 @@ scrcpy(const struct scrcpy_options *options) { options->window_title ? options->window_title : device_name; if (!screen_init_rendering(&screen, window_title, frame_size, - options->always_on_top)) { + options->always_on_top, options->window_x, + options->window_y)) { goto end; } diff --git a/app/src/scrcpy.h b/app/src/scrcpy.h index 70a41ec1..d0ef2392 100644 --- a/app/src/scrcpy.h +++ b/app/src/scrcpy.h @@ -18,6 +18,8 @@ struct scrcpy_options { uint16_t port; uint16_t max_size; uint32_t bit_rate; + int16_t window_x; + int16_t window_y; bool show_touches; bool fullscreen; bool always_on_top; @@ -38,6 +40,8 @@ struct scrcpy_options { .port = DEFAULT_LOCAL_PORT, \ .max_size = DEFAULT_LOCAL_PORT, \ .bit_rate = DEFAULT_BIT_RATE, \ + .window_x = -1, \ + .window_y = -1, \ .show_touches = false, \ .fullscreen = false, \ .always_on_top = false, \ diff --git a/app/src/screen.c b/app/src/screen.c index 7de57031..4543fab3 100644 --- a/app/src/screen.c +++ b/app/src/screen.c @@ -141,7 +141,8 @@ create_texture(SDL_Renderer *renderer, struct size frame_size) { bool screen_init_rendering(struct screen *screen, const char *window_title, - struct size frame_size, bool always_on_top) { + struct size frame_size, bool always_on_top, + int16_t window_x, int16_t window_y) { screen->frame_size = frame_size; struct size window_size = get_initial_optimal_size(frame_size); @@ -158,8 +159,9 @@ screen_init_rendering(struct screen *screen, const char *window_title, #endif } - screen->window = SDL_CreateWindow(window_title, SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, + int x = window_x != -1 ? window_x : SDL_WINDOWPOS_UNDEFINED; + int y = window_y != -1 ? window_y : SDL_WINDOWPOS_UNDEFINED; + screen->window = SDL_CreateWindow(window_title, x, y, window_size.width, window_size.height, window_flags); if (!screen->window) { diff --git a/app/src/screen.h b/app/src/screen.h index 275609ba..204e3226 100644 --- a/app/src/screen.h +++ b/app/src/screen.h @@ -55,7 +55,8 @@ screen_init(struct screen *screen); // initialize screen, create window, renderer and texture (window is hidden) bool screen_init_rendering(struct screen *screen, const char *window_title, - struct size frame_size, bool always_on_top); + struct size frame_size, bool always_on_top, + int16_t window_x, int16_t window_y); // show the window void