2018-02-08 11:14:13 +01:00
|
|
|
#include "screen.h"
|
|
|
|
|
|
2019-11-27 21:11:40 +01:00
|
|
|
#include <assert.h>
|
2018-02-08 11:14:13 +01:00
|
|
|
#include <string.h>
|
2025-07-11 09:42:03 +02:00
|
|
|
#include <SDL3/SDL.h>
|
2018-02-08 11:14:13 +01:00
|
|
|
|
2021-02-15 18:44:53 +01:00
|
|
|
#include "events.h"
|
2021-10-22 18:51:20 +02:00
|
|
|
#include "icon.h"
|
2021-10-27 18:43:47 +02:00
|
|
|
#include "options.h"
|
2019-11-24 11:53:00 +01:00
|
|
|
#include "util/log.h"
|
2025-07-11 09:42:03 +02:00
|
|
|
#include "util/sdl.h"
|
2018-02-08 11:14:13 +01:00
|
|
|
|
|
|
|
|
#define DISPLAY_MARGINS 96
|
|
|
|
|
|
2022-01-14 22:17:30 +01:00
|
|
|
#define DOWNCAST(SINK) container_of(SINK, struct sc_screen, frame_sink)
|
2021-04-11 15:01:05 +02:00
|
|
|
|
2021-10-30 15:20:39 +02:00
|
|
|
static inline struct sc_size
|
2023-11-19 01:06:59 +01:00
|
|
|
get_oriented_size(struct sc_size size, enum sc_orientation orientation) {
|
|
|
|
|
struct sc_size oriented_size;
|
|
|
|
|
if (sc_orientation_is_swap(orientation)) {
|
|
|
|
|
oriented_size.width = size.height;
|
|
|
|
|
oriented_size.height = size.width;
|
2020-04-07 23:03:23 +02:00
|
|
|
} else {
|
2023-11-19 01:06:59 +01:00
|
|
|
oriented_size.width = size.width;
|
|
|
|
|
oriented_size.height = size.height;
|
2020-04-07 23:03:23 +02:00
|
|
|
}
|
2023-11-19 01:06:59 +01:00
|
|
|
return oriented_size;
|
2020-04-07 23:03:23 +02:00
|
|
|
}
|
|
|
|
|
|
2025-12-30 22:14:11 +01:00
|
|
|
static inline bool
|
|
|
|
|
is_windowed(struct sc_screen *screen) {
|
|
|
|
|
return !(SDL_GetWindowFlags(screen->window) & (SDL_WINDOW_FULLSCREEN
|
|
|
|
|
| SDL_WINDOW_MINIMIZED
|
|
|
|
|
| SDL_WINDOW_MAXIMIZED));
|
2018-02-08 11:14:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get the preferred display bounds (i.e. the screen bounds with some margins)
|
2019-03-02 23:52:22 +01:00
|
|
|
static bool
|
2021-10-30 15:20:39 +02:00
|
|
|
get_preferred_display_bounds(struct sc_size *bounds) {
|
2018-02-08 11:14:13 +01:00
|
|
|
SDL_Rect rect;
|
2025-07-11 09:42:03 +02:00
|
|
|
SDL_DisplayID display = SDL_GetPrimaryDisplay();
|
|
|
|
|
if (!display) {
|
|
|
|
|
LOGW("Could not get primary display: %s", SDL_GetError());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ok = SDL_GetDisplayUsableBounds(display, &rect);
|
|
|
|
|
if (!ok) {
|
2018-02-13 10:10:18 +01:00
|
|
|
LOGW("Could not get display usable bounds: %s", SDL_GetError());
|
2019-03-02 23:52:22 +01:00
|
|
|
return false;
|
2018-02-08 11:14:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bounds->width = MAX(0, rect.w - DISPLAY_MARGINS);
|
|
|
|
|
bounds->height = MAX(0, rect.h - DISPLAY_MARGINS);
|
2019-03-02 23:52:22 +01:00
|
|
|
return true;
|
2018-02-08 11:14:13 +01:00
|
|
|
}
|
|
|
|
|
|
2020-04-25 00:19:44 +02:00
|
|
|
static bool
|
2021-10-30 15:20:39 +02:00
|
|
|
is_optimal_size(struct sc_size current_size, struct sc_size content_size) {
|
2020-04-25 00:19:44 +02:00
|
|
|
// The size is optimal if we can recompute one dimension of the current
|
|
|
|
|
// size from the other
|
|
|
|
|
return current_size.height == current_size.width * content_size.height
|
|
|
|
|
/ content_size.width
|
|
|
|
|
|| current_size.width == current_size.height * content_size.width
|
|
|
|
|
/ content_size.height;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-08 11:14:13 +01:00
|
|
|
// return the optimal size of the window, with the following constraints:
|
2019-03-02 20:09:56 +01:00
|
|
|
// - it attempts to keep at least one dimension of the current_size (i.e. it
|
|
|
|
|
// crops the black borders)
|
2018-02-08 11:14:13 +01:00
|
|
|
// - it keeps the aspect ratio
|
|
|
|
|
// - it scales down to make it fit in the display_size
|
2021-10-30 15:20:39 +02:00
|
|
|
static struct sc_size
|
2021-12-20 19:17:15 +01:00
|
|
|
get_optimal_size(struct sc_size current_size, struct sc_size content_size,
|
|
|
|
|
bool within_display_bounds) {
|
2020-04-07 23:03:23 +02:00
|
|
|
if (content_size.width == 0 || content_size.height == 0) {
|
2018-02-15 23:50:38 +01:00
|
|
|
// avoid division by 0
|
|
|
|
|
return current_size;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-30 15:20:39 +02:00
|
|
|
struct sc_size window_size;
|
2018-02-08 11:14:13 +01:00
|
|
|
|
2021-10-30 15:20:39 +02:00
|
|
|
struct sc_size display_size;
|
2021-12-20 19:17:15 +01:00
|
|
|
if (!within_display_bounds ||
|
|
|
|
|
!get_preferred_display_bounds(&display_size)) {
|
|
|
|
|
// do not constraint the size
|
2021-12-20 19:21:19 +01:00
|
|
|
window_size = current_size;
|
2018-02-08 11:14:13 +01:00
|
|
|
} else {
|
2020-04-25 00:19:44 +02:00
|
|
|
window_size.width = MIN(current_size.width, display_size.width);
|
|
|
|
|
window_size.height = MIN(current_size.height, display_size.height);
|
2018-02-08 11:14:13 +01:00
|
|
|
}
|
|
|
|
|
|
2020-04-25 00:19:44 +02:00
|
|
|
if (is_optimal_size(window_size, content_size)) {
|
|
|
|
|
return window_size;
|
2020-04-24 22:52:02 +02:00
|
|
|
}
|
|
|
|
|
|
2020-04-25 00:19:44 +02:00
|
|
|
bool keep_width = content_size.width * window_size.height
|
|
|
|
|
> content_size.height * window_size.width;
|
2018-02-08 11:14:13 +01:00
|
|
|
if (keep_width) {
|
|
|
|
|
// remove black borders on top and bottom
|
2020-04-25 00:19:44 +02:00
|
|
|
window_size.height = content_size.height * window_size.width
|
|
|
|
|
/ content_size.width;
|
2018-02-08 11:14:13 +01:00
|
|
|
} else {
|
2019-03-02 20:09:56 +01:00
|
|
|
// remove black borders on left and right (or none at all if it already
|
|
|
|
|
// fits)
|
2020-04-25 00:19:44 +02:00
|
|
|
window_size.width = content_size.width * window_size.height
|
|
|
|
|
/ content_size.height;
|
2018-02-08 11:14:13 +01:00
|
|
|
}
|
|
|
|
|
|
2020-04-25 00:19:44 +02:00
|
|
|
return window_size;
|
2018-02-08 11:14:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// initially, there is no current size, so use the frame size as current size
|
2019-11-03 18:00:11 +01:00
|
|
|
// req_width and req_height, if not 0, are the sizes requested by the user
|
2021-10-30 15:20:39 +02:00
|
|
|
static inline struct sc_size
|
|
|
|
|
get_initial_optimal_size(struct sc_size content_size, uint16_t req_width,
|
2019-11-03 18:00:11 +01:00
|
|
|
uint16_t req_height) {
|
2021-10-30 15:20:39 +02:00
|
|
|
struct sc_size window_size;
|
2019-11-03 18:00:11 +01:00
|
|
|
if (!req_width && !req_height) {
|
2021-12-20 19:17:15 +01:00
|
|
|
window_size = get_optimal_size(content_size, content_size, true);
|
2019-11-03 18:00:11 +01:00
|
|
|
} else {
|
|
|
|
|
if (req_width) {
|
|
|
|
|
window_size.width = req_width;
|
|
|
|
|
} else {
|
|
|
|
|
// compute from the requested height
|
2020-04-07 23:03:23 +02:00
|
|
|
window_size.width = (uint32_t) req_height * content_size.width
|
|
|
|
|
/ content_size.height;
|
2019-11-03 18:00:11 +01:00
|
|
|
}
|
|
|
|
|
if (req_height) {
|
|
|
|
|
window_size.height = req_height;
|
|
|
|
|
} else {
|
|
|
|
|
// compute from the requested width
|
2020-04-07 23:03:23 +02:00
|
|
|
window_size.height = (uint32_t) req_width * content_size.height
|
|
|
|
|
/ content_size.width;
|
2019-11-03 18:00:11 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return window_size;
|
2018-02-08 11:14:13 +01:00
|
|
|
}
|
|
|
|
|
|
2022-01-23 21:23:31 +01:00
|
|
|
static inline bool
|
|
|
|
|
sc_screen_is_relative_mode(struct sc_screen *screen) {
|
|
|
|
|
// screen->im.mp may be NULL if --no-control
|
|
|
|
|
return screen->im.mp && screen->im.mp->relative_mode;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-17 18:44:24 +02:00
|
|
|
static void
|
2022-01-14 22:17:30 +01:00
|
|
|
sc_screen_update_content_rect(struct sc_screen *screen) {
|
2024-04-07 16:01:26 +02:00
|
|
|
assert(screen->video);
|
|
|
|
|
|
2021-10-30 15:20:39 +02:00
|
|
|
struct sc_size content_size = screen->content_size;
|
2020-04-17 18:44:24 +02:00
|
|
|
// The drawable size is the window size * the HiDPI scale
|
2025-07-11 09:42:03 +02:00
|
|
|
struct sc_size drawable_size =
|
|
|
|
|
sc_sdl_get_window_size_in_pixels(screen->window);
|
2020-04-17 18:44:24 +02:00
|
|
|
|
|
|
|
|
SDL_Rect *rect = &screen->rect;
|
|
|
|
|
|
|
|
|
|
if (is_optimal_size(drawable_size, content_size)) {
|
|
|
|
|
rect->x = 0;
|
|
|
|
|
rect->y = 0;
|
|
|
|
|
rect->w = drawable_size.width;
|
|
|
|
|
rect->h = drawable_size.height;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool keep_width = content_size.width * drawable_size.height
|
|
|
|
|
> content_size.height * drawable_size.width;
|
|
|
|
|
if (keep_width) {
|
|
|
|
|
rect->x = 0;
|
|
|
|
|
rect->w = drawable_size.width;
|
|
|
|
|
rect->h = drawable_size.width * content_size.height
|
|
|
|
|
/ content_size.width;
|
|
|
|
|
rect->y = (drawable_size.height - rect->h) / 2;
|
|
|
|
|
} else {
|
|
|
|
|
rect->y = 0;
|
|
|
|
|
rect->h = drawable_size.height;
|
|
|
|
|
rect->w = drawable_size.height * content_size.width
|
|
|
|
|
/ content_size.height;
|
|
|
|
|
rect->x = (drawable_size.width - rect->w) / 2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-12 22:39:36 +01:00
|
|
|
// render the texture to the renderer
|
|
|
|
|
//
|
|
|
|
|
// Set the update_content_rect flag if the window or content size may have
|
|
|
|
|
// changed, so that the content rectangle is recomputed
|
|
|
|
|
static void
|
2022-01-14 22:17:30 +01:00
|
|
|
sc_screen_render(struct sc_screen *screen, bool update_content_rect) {
|
2024-04-07 16:01:26 +02:00
|
|
|
assert(screen->video);
|
2025-07-03 18:49:35 +02:00
|
|
|
assert(screen->has_video_window);
|
2024-04-07 16:01:26 +02:00
|
|
|
|
2021-11-12 22:39:36 +01:00
|
|
|
if (update_content_rect) {
|
2022-01-14 22:17:30 +01:00
|
|
|
sc_screen_update_content_rect(screen);
|
2021-11-12 22:39:36 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-23 21:51:13 +01:00
|
|
|
bool ok =
|
2023-11-19 01:06:59 +01:00
|
|
|
sc_display_render(&screen->display, &screen->rect, screen->orientation);
|
2026-01-23 21:51:13 +01:00
|
|
|
(void) ok; // any error already logged
|
2021-11-12 22:39:36 +01:00
|
|
|
}
|
|
|
|
|
|
2024-04-07 16:01:26 +02:00
|
|
|
static void
|
|
|
|
|
sc_screen_render_novideo(struct sc_screen *screen) {
|
2026-01-23 21:51:13 +01:00
|
|
|
bool ok =
|
2024-04-07 16:01:26 +02:00
|
|
|
sc_display_render(&screen->display, NULL, SC_ORIENTATION_0);
|
2026-01-23 21:51:13 +01:00
|
|
|
(void) ok; // any error already logged
|
2024-04-07 16:01:26 +02:00
|
|
|
}
|
|
|
|
|
|
2025-07-10 23:01:05 +02:00
|
|
|
#if defined(__APPLE__) || defined(_WIN32)
|
2021-04-13 22:14:09 +02:00
|
|
|
# define CONTINUOUS_RESIZING_WORKAROUND
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef CONTINUOUS_RESIZING_WORKAROUND
|
|
|
|
|
// On Windows and MacOS, resizing blocks the event loop, so resizing events are
|
2022-12-26 12:42:59 +01:00
|
|
|
// not triggered. As a workaround, handle them in an event handler.
|
2021-04-13 22:14:09 +02:00
|
|
|
//
|
|
|
|
|
// <https://bugzilla.libsdl.org/show_bug.cgi?id=2077>
|
|
|
|
|
// <https://stackoverflow.com/a/40693139/1987178>
|
2025-07-11 09:42:03 +02:00
|
|
|
static bool
|
2021-04-13 22:14:09 +02:00
|
|
|
event_watcher(void *data, SDL_Event *event) {
|
2022-01-14 22:17:30 +01:00
|
|
|
struct sc_screen *screen = data;
|
2024-04-07 16:01:26 +02:00
|
|
|
assert(screen->video);
|
|
|
|
|
|
2025-07-11 09:42:03 +02:00
|
|
|
if (event->type == SDL_EVENT_WINDOW_RESIZED) {
|
2021-04-13 22:14:09 +02:00
|
|
|
// In practice, it seems to always be called from the same thread in
|
|
|
|
|
// that specific case. Anyway, it's just a workaround.
|
2022-01-14 22:17:30 +01:00
|
|
|
sc_screen_render(screen, true);
|
2021-04-13 22:14:09 +02:00
|
|
|
}
|
2025-07-11 09:42:03 +02:00
|
|
|
|
|
|
|
|
return true;
|
2021-04-13 22:14:09 +02:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-04-11 15:01:05 +02:00
|
|
|
static bool
|
2023-02-25 16:19:58 +01:00
|
|
|
sc_screen_frame_sink_open(struct sc_frame_sink *sink,
|
2025-05-10 10:23:02 +02:00
|
|
|
const AVCodecContext *ctx,
|
|
|
|
|
const struct sc_stream_session *session) {
|
2023-02-25 16:19:58 +01:00
|
|
|
assert(ctx->pix_fmt == AV_PIX_FMT_YUV420P);
|
|
|
|
|
(void) ctx;
|
2025-05-10 10:23:02 +02:00
|
|
|
(void) session;
|
2023-02-25 16:19:58 +01:00
|
|
|
|
2022-01-14 22:17:30 +01:00
|
|
|
struct sc_screen *screen = DOWNCAST(sink);
|
2025-07-11 09:42:03 +02:00
|
|
|
(void) screen;
|
2023-03-11 09:21:49 +01:00
|
|
|
|
2024-09-13 19:48:44 +02:00
|
|
|
if (ctx->width <= 0 || ctx->width > 0xFFFF
|
|
|
|
|
|| ctx->height <= 0 || ctx->height > 0xFFFF) {
|
|
|
|
|
LOGE("Invalid video size: %dx%d", ctx->width, ctx->height);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 15:01:05 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
|
screen->open = true;
|
|
|
|
|
#endif
|
2021-04-11 15:01:05 +02:00
|
|
|
|
|
|
|
|
// nothing to do, the screen is already open on the main thread
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2022-01-14 22:17:30 +01:00
|
|
|
sc_screen_frame_sink_close(struct sc_frame_sink *sink) {
|
|
|
|
|
struct sc_screen *screen = DOWNCAST(sink);
|
2021-04-11 15:01:05 +02:00
|
|
|
(void) screen;
|
2021-04-11 15:01:05 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
|
screen->open = false;
|
|
|
|
|
#endif
|
2021-04-11 15:01:05 +02:00
|
|
|
|
|
|
|
|
// nothing to do, the screen lifecycle is not managed by the frame producer
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
2022-01-14 22:17:30 +01:00
|
|
|
sc_screen_frame_sink_push(struct sc_frame_sink *sink, const AVFrame *frame) {
|
|
|
|
|
struct sc_screen *screen = DOWNCAST(sink);
|
2024-04-07 16:01:26 +02:00
|
|
|
assert(screen->video);
|
2021-04-11 15:01:05 +02:00
|
|
|
|
2023-03-02 21:30:24 +01:00
|
|
|
bool previous_skipped;
|
|
|
|
|
bool ok = sc_frame_buffer_push(&screen->fb, frame, &previous_skipped);
|
|
|
|
|
if (!ok) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-04-11 15:01:05 +02:00
|
|
|
|
2021-07-04 12:42:22 +02:00
|
|
|
if (previous_skipped) {
|
2022-02-17 19:55:14 +01:00
|
|
|
sc_fps_counter_add_skipped_frame(&screen->fps_counter);
|
2023-02-10 18:46:12 +01:00
|
|
|
// The SC_EVENT_NEW_FRAME triggered for the previous frame will consume
|
2023-03-01 18:45:22 +01:00
|
|
|
// this new frame instead
|
2021-04-11 15:01:05 +02:00
|
|
|
} else {
|
|
|
|
|
// Post the event on the UI thread
|
2024-09-06 23:08:08 +02:00
|
|
|
bool ok = sc_push_event(SC_EVENT_NEW_FRAME);
|
|
|
|
|
if (!ok) {
|
2023-03-01 18:45:22 +01:00
|
|
|
return false;
|
2021-10-30 20:21:00 +02:00
|
|
|
}
|
2021-04-11 15:01:05 +02:00
|
|
|
}
|
2023-03-01 18:45:22 +01:00
|
|
|
|
|
|
|
|
return true;
|
2021-04-11 15:01:05 +02:00
|
|
|
}
|
|
|
|
|
|
2019-03-02 23:52:22 +01:00
|
|
|
bool
|
2022-01-14 22:17:30 +01:00
|
|
|
sc_screen_init(struct sc_screen *screen,
|
|
|
|
|
const struct sc_screen_params *params) {
|
2021-02-25 22:06:33 +01:00
|
|
|
screen->resize_pending = false;
|
|
|
|
|
screen->has_frame = false;
|
2025-07-03 18:49:35 +02:00
|
|
|
screen->has_video_window = false;
|
2024-03-10 22:04:17 +01:00
|
|
|
screen->paused = false;
|
|
|
|
|
screen->resume_frame = NULL;
|
2024-04-07 16:01:26 +02:00
|
|
|
screen->orientation = SC_ORIENTATION_0;
|
|
|
|
|
|
|
|
|
|
screen->video = params->video;
|
2025-11-02 14:47:40 +01:00
|
|
|
screen->camera = params->camera;
|
2021-02-25 22:06:33 +01:00
|
|
|
|
2022-01-16 15:46:37 +01:00
|
|
|
screen->req.x = params->window_x;
|
|
|
|
|
screen->req.y = params->window_y;
|
|
|
|
|
screen->req.width = params->window_width;
|
|
|
|
|
screen->req.height = params->window_height;
|
|
|
|
|
screen->req.fullscreen = params->fullscreen;
|
2022-02-17 20:08:41 +01:00
|
|
|
screen->req.start_fps_counter = params->start_fps_counter;
|
2022-01-16 15:46:37 +01:00
|
|
|
|
2023-03-02 21:30:24 +01:00
|
|
|
bool ok = sc_frame_buffer_init(&screen->fb);
|
2021-04-11 15:01:05 +02:00
|
|
|
if (!ok) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-17 19:55:14 +01:00
|
|
|
if (!sc_fps_counter_init(&screen->fps_counter)) {
|
2023-03-02 21:30:24 +01:00
|
|
|
goto error_destroy_frame_buffer;
|
2021-05-16 18:26:20 +02:00
|
|
|
}
|
|
|
|
|
|
2024-04-07 16:01:26 +02:00
|
|
|
if (screen->video) {
|
|
|
|
|
screen->orientation = params->orientation;
|
|
|
|
|
if (screen->orientation != SC_ORIENTATION_0) {
|
|
|
|
|
LOGI("Initial display orientation set to %s",
|
|
|
|
|
sc_orientation_get_name(screen->orientation));
|
|
|
|
|
}
|
2020-04-08 10:17:12 +02:00
|
|
|
}
|
2018-02-08 11:14:13 +01:00
|
|
|
|
2026-01-09 19:08:04 +01:00
|
|
|
// Always create the window hidden to prevent blinking during initialization
|
|
|
|
|
uint32_t window_flags = SDL_WINDOW_HIGH_PIXEL_DENSITY | SDL_WINDOW_HIDDEN;
|
2021-02-25 22:00:34 +01:00
|
|
|
if (params->always_on_top) {
|
2019-01-27 19:04:22 +08:00
|
|
|
window_flags |= SDL_WINDOW_ALWAYS_ON_TOP;
|
|
|
|
|
}
|
2021-02-25 22:00:34 +01:00
|
|
|
if (params->window_borderless) {
|
2019-08-29 00:25:17 -05:00
|
|
|
window_flags |= SDL_WINDOW_BORDERLESS;
|
|
|
|
|
}
|
2024-04-07 16:01:26 +02:00
|
|
|
if (params->video) {
|
|
|
|
|
// The window will be shown on first frame
|
2026-01-09 19:08:04 +01:00
|
|
|
window_flags |= SDL_WINDOW_RESIZABLE;
|
2024-04-07 16:01:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *title = params->window_title;
|
|
|
|
|
assert(title);
|
|
|
|
|
|
|
|
|
|
int x = SDL_WINDOWPOS_UNDEFINED;
|
|
|
|
|
int y = SDL_WINDOWPOS_UNDEFINED;
|
|
|
|
|
int width = 256;
|
|
|
|
|
int height = 256;
|
|
|
|
|
if (params->window_x != SC_WINDOW_POSITION_UNDEFINED) {
|
|
|
|
|
x = params->window_x;
|
|
|
|
|
}
|
|
|
|
|
if (params->window_y != SC_WINDOW_POSITION_UNDEFINED) {
|
|
|
|
|
y = params->window_y;
|
|
|
|
|
}
|
|
|
|
|
if (params->window_width) {
|
|
|
|
|
width = params->window_width;
|
|
|
|
|
}
|
|
|
|
|
if (params->window_height) {
|
|
|
|
|
height = params->window_height;
|
|
|
|
|
}
|
2019-01-27 19:04:22 +08:00
|
|
|
|
2022-01-16 15:46:37 +01:00
|
|
|
// The window will be positioned and sized on first video frame
|
2025-07-11 09:42:03 +02:00
|
|
|
screen->window =
|
|
|
|
|
sc_sdl_create_window(title, x, y, width, height, window_flags);
|
2018-02-08 11:14:13 +01:00
|
|
|
if (!screen->window) {
|
2022-02-05 14:06:03 +01:00
|
|
|
LOGE("Could not create window: %s", SDL_GetError());
|
2021-05-16 18:36:07 +02:00
|
|
|
goto error_destroy_fps_counter;
|
2018-02-08 11:14:13 +01:00
|
|
|
}
|
|
|
|
|
|
2025-07-11 09:42:03 +02:00
|
|
|
ok = SDL_StartTextInput(screen->window);
|
|
|
|
|
if (!ok) {
|
|
|
|
|
LOGE("Could not enable text input: %s", SDL_GetError());
|
|
|
|
|
goto error_destroy_window;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-22 18:51:20 +02:00
|
|
|
SDL_Surface *icon = scrcpy_icon_load();
|
2019-05-23 20:58:08 +02:00
|
|
|
if (icon) {
|
2025-07-11 09:42:03 +02:00
|
|
|
if (!SDL_SetWindowIcon(screen->window, icon)) {
|
|
|
|
|
LOGW("Could not set window icon: %s", SDL_GetError());
|
|
|
|
|
}
|
2024-04-07 16:01:26 +02:00
|
|
|
} else if (params->video) {
|
|
|
|
|
// just a warning
|
2019-05-23 20:58:08 +02:00
|
|
|
LOGW("Could not load icon");
|
2024-04-07 16:01:26 +02:00
|
|
|
} else {
|
|
|
|
|
// without video, the icon is used as window content, it must be present
|
|
|
|
|
LOGE("Could not load icon");
|
2025-07-17 13:19:15 +02:00
|
|
|
goto error_destroy_window;
|
2024-04-07 16:01:26 +02:00
|
|
|
}
|
|
|
|
|
|
2026-01-24 22:40:54 +01:00
|
|
|
screen->renderer = SDL_CreateRenderer(screen->window, NULL);
|
|
|
|
|
if (!screen->renderer) {
|
|
|
|
|
LOGE("Could not create renderer: %s", SDL_GetError());
|
|
|
|
|
goto error_destroy_window;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef SC_DISPLAY_FORCE_OPENGL_CORE_PROFILE
|
|
|
|
|
screen->gl_context = NULL;
|
|
|
|
|
|
|
|
|
|
// starts with "opengl"
|
|
|
|
|
const char *renderer_name = SDL_GetRendererName(screen->renderer);
|
|
|
|
|
bool use_opengl = renderer_name && !strncmp(renderer_name, "opengl", 6);
|
|
|
|
|
if (use_opengl) {
|
|
|
|
|
// Persuade macOS to give us something better than OpenGL 2.1.
|
|
|
|
|
// If we create a Core Profile context, we get the best OpenGL version.
|
|
|
|
|
bool ok = SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
|
|
|
|
|
SDL_GL_CONTEXT_PROFILE_CORE);
|
|
|
|
|
if (!ok) {
|
|
|
|
|
LOGW("Could not set a GL Core Profile Context");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOGD("Creating OpenGL Core Profile context");
|
|
|
|
|
screen->gl_context = SDL_GL_CreateContext(screen->window);
|
|
|
|
|
if (!screen->gl_context) {
|
|
|
|
|
LOGE("Could not create OpenGL context: %s", SDL_GetError());
|
|
|
|
|
goto error_destroy_renderer;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-04-07 16:01:26 +02:00
|
|
|
SDL_Surface *icon_novideo = params->video ? NULL : icon;
|
|
|
|
|
bool mipmaps = params->video && params->mipmaps;
|
2026-01-24 22:40:54 +01:00
|
|
|
ok = sc_display_init(&screen->display, screen->renderer, icon_novideo,
|
2024-04-07 16:01:26 +02:00
|
|
|
mipmaps);
|
|
|
|
|
if (icon) {
|
|
|
|
|
scrcpy_icon_destroy(icon);
|
|
|
|
|
}
|
|
|
|
|
if (!ok) {
|
2026-01-24 22:40:54 +01:00
|
|
|
goto error_destroy_renderer;
|
2018-02-08 11:14:13 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-11 15:01:05 +02:00
|
|
|
screen->frame = av_frame_alloc();
|
|
|
|
|
if (!screen->frame) {
|
2021-11-24 22:06:11 +01:00
|
|
|
LOG_OOM();
|
2023-03-31 20:20:27 +02:00
|
|
|
goto error_destroy_display;
|
2021-04-11 15:01:05 +02:00
|
|
|
}
|
|
|
|
|
|
2022-01-14 22:17:30 +01:00
|
|
|
struct sc_input_manager_params im_params = {
|
2021-12-31 16:32:07 +01:00
|
|
|
.controller = params->controller,
|
2022-01-21 19:26:36 +01:00
|
|
|
.fp = params->fp,
|
2021-12-31 16:32:07 +01:00
|
|
|
.screen = screen,
|
|
|
|
|
.kp = params->kp,
|
|
|
|
|
.mp = params->mp,
|
2024-09-06 23:08:08 +02:00
|
|
|
.gp = params->gp,
|
2025-11-02 14:47:40 +01:00
|
|
|
.camera = params->camera,
|
2024-06-24 23:07:08 +02:00
|
|
|
.mouse_bindings = params->mouse_bindings,
|
2021-12-31 16:32:07 +01:00
|
|
|
.legacy_paste = params->legacy_paste,
|
|
|
|
|
.clipboard_autosync = params->clipboard_autosync,
|
|
|
|
|
.shortcut_mods = params->shortcut_mods,
|
|
|
|
|
};
|
|
|
|
|
|
2022-01-14 22:17:30 +01:00
|
|
|
sc_input_manager_init(&screen->im, &im_params);
|
2021-12-31 16:32:07 +01:00
|
|
|
|
2024-09-25 22:50:44 +02:00
|
|
|
// Initialize even if not used for simplicity
|
2024-09-27 18:32:39 +02:00
|
|
|
sc_mouse_capture_init(&screen->mc, screen->window, params->shortcut_mods);
|
2024-09-25 22:50:44 +02:00
|
|
|
|
2021-04-13 22:14:09 +02:00
|
|
|
#ifdef CONTINUOUS_RESIZING_WORKAROUND
|
2024-04-07 16:01:26 +02:00
|
|
|
if (screen->video) {
|
2025-07-11 09:42:03 +02:00
|
|
|
ok = SDL_AddEventWatch(event_watcher, screen);
|
|
|
|
|
if (!ok) {
|
|
|
|
|
LOGW("Could not add event watcher for continuous resizing: %s",
|
|
|
|
|
SDL_GetError());
|
|
|
|
|
}
|
2024-04-07 16:01:26 +02:00
|
|
|
}
|
2021-04-13 22:14:09 +02:00
|
|
|
#endif
|
|
|
|
|
|
2021-04-11 15:01:05 +02:00
|
|
|
static const struct sc_frame_sink_ops ops = {
|
2022-01-14 22:17:30 +01:00
|
|
|
.open = sc_screen_frame_sink_open,
|
|
|
|
|
.close = sc_screen_frame_sink_close,
|
|
|
|
|
.push = sc_screen_frame_sink_push,
|
2021-04-11 15:01:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
screen->frame_sink.ops = &ops;
|
|
|
|
|
|
2021-04-11 15:01:05 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
|
screen->open = false;
|
|
|
|
|
#endif
|
|
|
|
|
|
2026-01-09 19:08:04 +01:00
|
|
|
if (!screen->video) {
|
|
|
|
|
// Show the window immediately
|
|
|
|
|
sc_sdl_show_window(screen->window);
|
|
|
|
|
|
|
|
|
|
if (sc_screen_is_relative_mode(screen)) {
|
|
|
|
|
// Capture mouse immediately if video mirroring is disabled
|
|
|
|
|
sc_mouse_capture_set_active(&screen->mc, true);
|
|
|
|
|
}
|
2024-04-07 16:01:26 +02:00
|
|
|
}
|
|
|
|
|
|
2019-03-02 23:52:22 +01:00
|
|
|
return true;
|
2021-05-16 18:36:07 +02:00
|
|
|
|
2023-03-31 20:20:27 +02:00
|
|
|
error_destroy_display:
|
|
|
|
|
sc_display_destroy(&screen->display);
|
2026-01-24 22:40:54 +01:00
|
|
|
error_destroy_renderer:
|
|
|
|
|
#ifdef SC_DISPLAY_FORCE_OPENGL_CORE_PROFILE
|
|
|
|
|
if (screen->gl_context) {
|
|
|
|
|
SDL_GL_DestroyContext(screen->gl_context);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
SDL_DestroyRenderer(screen->renderer);
|
2021-05-16 18:36:07 +02:00
|
|
|
error_destroy_window:
|
|
|
|
|
SDL_DestroyWindow(screen->window);
|
|
|
|
|
error_destroy_fps_counter:
|
2022-02-17 19:55:14 +01:00
|
|
|
sc_fps_counter_destroy(&screen->fps_counter);
|
2023-03-02 21:30:24 +01:00
|
|
|
error_destroy_frame_buffer:
|
|
|
|
|
sc_frame_buffer_destroy(&screen->fb);
|
2021-05-16 18:36:07 +02:00
|
|
|
|
|
|
|
|
return false;
|
2018-02-08 11:14:13 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-13 22:04:38 +02:00
|
|
|
static void
|
2022-01-16 15:46:37 +01:00
|
|
|
sc_screen_show_initial_window(struct sc_screen *screen) {
|
|
|
|
|
int x = screen->req.x != SC_WINDOW_POSITION_UNDEFINED
|
|
|
|
|
? screen->req.x : (int) SDL_WINDOWPOS_CENTERED;
|
|
|
|
|
int y = screen->req.y != SC_WINDOW_POSITION_UNDEFINED
|
|
|
|
|
? screen->req.y : (int) SDL_WINDOWPOS_CENTERED;
|
2025-07-11 09:42:03 +02:00
|
|
|
struct sc_point position = {
|
|
|
|
|
.x = x,
|
|
|
|
|
.y = y,
|
|
|
|
|
};
|
2022-01-16 15:46:37 +01:00
|
|
|
|
|
|
|
|
struct sc_size window_size =
|
|
|
|
|
get_initial_optimal_size(screen->content_size, screen->req.width,
|
|
|
|
|
screen->req.height);
|
|
|
|
|
|
2025-12-30 22:14:11 +01:00
|
|
|
assert(is_windowed(screen));
|
2025-07-11 09:42:03 +02:00
|
|
|
sc_sdl_set_window_size(screen->window, window_size);
|
|
|
|
|
sc_sdl_set_window_position(screen->window, position);
|
2022-01-16 15:46:37 +01:00
|
|
|
|
|
|
|
|
if (screen->req.fullscreen) {
|
2024-09-25 19:59:49 +02:00
|
|
|
sc_screen_toggle_fullscreen(screen);
|
2022-01-16 15:46:37 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-17 20:08:41 +01:00
|
|
|
if (screen->req.start_fps_counter) {
|
|
|
|
|
sc_fps_counter_start(&screen->fps_counter);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-11 09:42:03 +02:00
|
|
|
sc_sdl_show_window(screen->window);
|
2023-06-27 18:38:10 +02:00
|
|
|
sc_screen_update_content_rect(screen);
|
Improve startup time
On startup, the client has to:
1. listen on a port
2. push and start the server to the device
3. wait for the server to connect (accept)
4. read device name and size
5. initialize SDL
6. initialize the window and renderer
7. show the window
From the execution of the app_process command to start the server on the
device, to the execution of the java main method, it takes ~800ms. As a
consequence, step 3 also takes ~800ms on the client.
Once complete, the client initializes SDL, which takes ~500ms.
These two expensive actions are executed sequentially:
HOST DEVICE
listen on port | |
push/start the server |----------------->|| app_process loads the jar
accept the connection . ^ ||
. | ||
. | WASTE ||
. | OF ||
. | TIME ||
. | ||
. | ||
. v X execution of our java main
connection accepted |<-----------------| connect to the host
init SDL || |
|| ,----------------| send frames
|| |,---------------|
|| ||,--------------|
|| |||,-------------|
|| ||||,------------|
init window/renderer | |||||,-----------|
display frames |<++++++-----------|
(many frames skipped)
The rationale for step 3 occuring before step 5 is that initializing
SDL replaces the SIGTERM handler to receive the event in the event loop,
so pressing Ctrl+C during step 5 would not work (since it blocks the
event loop).
But this is not so important; let's parallelize the SDL initialization
with the app_process execution (we'll just add a timeout to the
connection):
HOST DEVICE
listen on port | |
push/start the server |----------------->||app_process loads the jar
init SDL || ||
|| ||
|| ||
|| ||
|| ||
|| ||
accept the connection . ||
. X execution of our java main
connection accepted |<-----------------| connect to the host
init window/renderer | |
display frames |<-----------------| send frames
|<-----------------|
In addition, show the window only once the first frame is available to
avoid flickering (opening a black window for 100~200ms).
Note: the window and renderer are initialized after the connection is
accepted because they use the device information received from the
device.
2018-02-09 13:50:54 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-13 22:22:54 +02:00
|
|
|
void
|
2022-01-14 22:17:30 +01:00
|
|
|
sc_screen_hide_window(struct sc_screen *screen) {
|
2025-07-11 09:42:03 +02:00
|
|
|
sc_sdl_hide_window(screen->window);
|
2021-04-13 22:22:54 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-16 18:26:20 +02:00
|
|
|
void
|
2022-01-14 22:17:30 +01:00
|
|
|
sc_screen_interrupt(struct sc_screen *screen) {
|
2022-02-17 19:55:14 +01:00
|
|
|
sc_fps_counter_interrupt(&screen->fps_counter);
|
2021-05-16 18:26:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2022-01-14 22:17:30 +01:00
|
|
|
sc_screen_join(struct sc_screen *screen) {
|
2022-02-17 19:55:14 +01:00
|
|
|
sc_fps_counter_join(&screen->fps_counter);
|
2021-05-16 18:26:20 +02:00
|
|
|
}
|
|
|
|
|
|
2019-03-02 20:09:56 +01:00
|
|
|
void
|
2022-01-14 22:17:30 +01:00
|
|
|
sc_screen_destroy(struct sc_screen *screen) {
|
2021-04-11 15:01:05 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
|
assert(!screen->open);
|
|
|
|
|
#endif
|
2023-03-31 20:20:27 +02:00
|
|
|
sc_display_destroy(&screen->display);
|
2021-04-11 15:01:05 +02:00
|
|
|
av_frame_free(&screen->frame);
|
2026-01-24 22:40:54 +01:00
|
|
|
#ifdef SC_DISPLAY_FORCE_OPENGL_CORE_PROFILE
|
|
|
|
|
SDL_GL_DestroyContext(screen->gl_context);
|
|
|
|
|
#endif
|
|
|
|
|
SDL_DestroyRenderer(screen->renderer);
|
2021-02-13 14:40:00 +01:00
|
|
|
SDL_DestroyWindow(screen->window);
|
2022-02-17 19:55:14 +01:00
|
|
|
sc_fps_counter_destroy(&screen->fps_counter);
|
2023-03-02 21:30:24 +01:00
|
|
|
sc_frame_buffer_destroy(&screen->fb);
|
2018-02-08 11:14:13 +01:00
|
|
|
}
|
|
|
|
|
|
2020-05-11 02:00:46 +02:00
|
|
|
static void
|
2022-01-14 22:17:30 +01:00
|
|
|
resize_for_content(struct sc_screen *screen, struct sc_size old_content_size,
|
2021-10-30 15:20:39 +02:00
|
|
|
struct sc_size new_content_size) {
|
2024-04-07 16:01:26 +02:00
|
|
|
assert(screen->video);
|
|
|
|
|
|
2025-07-11 09:42:03 +02:00
|
|
|
struct sc_size window_size = sc_sdl_get_window_size(screen->window);
|
2021-10-30 15:20:39 +02:00
|
|
|
struct sc_size target_size = {
|
2020-05-11 03:11:13 +02:00
|
|
|
.width = (uint32_t) window_size.width * new_content_size.width
|
2020-05-11 02:00:46 +02:00
|
|
|
/ old_content_size.width,
|
2020-05-11 03:11:13 +02:00
|
|
|
.height = (uint32_t) window_size.height * new_content_size.height
|
2020-05-11 02:00:46 +02:00
|
|
|
/ old_content_size.height,
|
|
|
|
|
};
|
2021-12-20 19:17:15 +01:00
|
|
|
target_size = get_optimal_size(target_size, new_content_size, true);
|
2025-12-30 22:14:11 +01:00
|
|
|
assert(is_windowed(screen));
|
2025-07-11 09:42:03 +02:00
|
|
|
sc_sdl_set_window_size(screen->window, target_size);
|
2020-05-11 03:11:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2022-01-14 22:17:30 +01:00
|
|
|
set_content_size(struct sc_screen *screen, struct sc_size new_content_size) {
|
2024-04-07 16:01:26 +02:00
|
|
|
assert(screen->video);
|
|
|
|
|
|
2025-12-30 22:14:11 +01:00
|
|
|
if (is_windowed(screen)) {
|
2020-05-11 03:11:13 +02:00
|
|
|
resize_for_content(screen, screen->content_size, new_content_size);
|
|
|
|
|
} else if (!screen->resize_pending) {
|
|
|
|
|
// Store the windowed size to be able to compute the optimal size once
|
2023-05-08 21:02:01 +02:00
|
|
|
// fullscreen/maximized/minimized are disabled
|
2020-05-11 03:11:13 +02:00
|
|
|
screen->windowed_content_size = screen->content_size;
|
|
|
|
|
screen->resize_pending = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-11 02:00:46 +02:00
|
|
|
screen->content_size = new_content_size;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-11 03:11:13 +02:00
|
|
|
static void
|
2022-01-14 22:17:30 +01:00
|
|
|
apply_pending_resize(struct sc_screen *screen) {
|
2024-04-07 16:01:26 +02:00
|
|
|
assert(screen->video);
|
|
|
|
|
|
2025-12-30 22:14:11 +01:00
|
|
|
assert(is_windowed(screen));
|
2020-05-11 03:11:13 +02:00
|
|
|
if (screen->resize_pending) {
|
|
|
|
|
resize_for_content(screen, screen->windowed_content_size,
|
|
|
|
|
screen->content_size);
|
|
|
|
|
screen->resize_pending = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-07 23:03:23 +02:00
|
|
|
void
|
2023-11-19 01:06:59 +01:00
|
|
|
sc_screen_set_orientation(struct sc_screen *screen,
|
|
|
|
|
enum sc_orientation orientation) {
|
2024-04-07 16:01:26 +02:00
|
|
|
assert(screen->video);
|
|
|
|
|
|
2023-11-19 01:06:59 +01:00
|
|
|
if (orientation == screen->orientation) {
|
2020-04-07 23:03:23 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-30 15:20:39 +02:00
|
|
|
struct sc_size new_content_size =
|
2023-11-19 01:06:59 +01:00
|
|
|
get_oriented_size(screen->frame_size, orientation);
|
2020-04-07 23:03:23 +02:00
|
|
|
|
2020-05-11 02:00:46 +02:00
|
|
|
set_content_size(screen, new_content_size);
|
2020-04-17 18:44:24 +02:00
|
|
|
|
2023-11-19 01:06:59 +01:00
|
|
|
screen->orientation = orientation;
|
|
|
|
|
LOGI("Display orientation set to %s", sc_orientation_get_name(orientation));
|
2020-04-07 23:03:23 +02:00
|
|
|
|
2022-01-14 22:17:30 +01:00
|
|
|
sc_screen_render(screen, true);
|
2020-04-07 23:03:23 +02:00
|
|
|
}
|
|
|
|
|
|
2021-02-15 18:44:53 +01:00
|
|
|
static bool
|
2024-03-10 22:04:17 +01:00
|
|
|
sc_screen_apply_frame(struct sc_screen *screen) {
|
2024-04-07 16:01:26 +02:00
|
|
|
assert(screen->video);
|
|
|
|
|
|
2022-02-17 19:55:14 +01:00
|
|
|
sc_fps_counter_add_rendered_frame(&screen->fps_counter);
|
2021-02-19 20:56:09 +01:00
|
|
|
|
2024-03-10 22:04:17 +01:00
|
|
|
AVFrame *frame = screen->frame;
|
2021-10-30 15:20:39 +02:00
|
|
|
struct sc_size new_frame_size = {frame->width, frame->height};
|
2025-07-03 18:36:17 +02:00
|
|
|
|
2025-07-03 18:49:35 +02:00
|
|
|
if (!screen->has_frame
|
|
|
|
|
|| screen->frame_size.width != new_frame_size.width
|
2025-07-03 18:36:17 +02:00
|
|
|
|| screen->frame_size.height != new_frame_size.height) {
|
2026-01-23 21:51:13 +01:00
|
|
|
|
|
|
|
|
bool ok =
|
|
|
|
|
sc_display_prepare_texture(&screen->display, new_frame_size,
|
|
|
|
|
frame->colorspace, frame->color_range);
|
|
|
|
|
if (!ok) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-03 18:36:17 +02:00
|
|
|
// frame dimension changed
|
|
|
|
|
screen->frame_size = new_frame_size;
|
|
|
|
|
|
|
|
|
|
struct sc_size new_content_size =
|
|
|
|
|
get_oriented_size(new_frame_size, screen->orientation);
|
2025-07-03 18:49:35 +02:00
|
|
|
if (screen->has_frame) {
|
|
|
|
|
set_content_size(screen, new_content_size);
|
|
|
|
|
sc_screen_update_content_rect(screen);
|
|
|
|
|
} else {
|
|
|
|
|
// This is the first frame
|
|
|
|
|
screen->has_frame = true;
|
|
|
|
|
screen->content_size = new_content_size;
|
|
|
|
|
}
|
2023-05-08 18:16:38 +02:00
|
|
|
}
|
2023-03-31 20:20:27 +02:00
|
|
|
|
2026-01-23 21:51:13 +01:00
|
|
|
bool ok = sc_display_update_texture(&screen->display, frame);
|
|
|
|
|
if (!ok) {
|
2023-03-31 20:20:27 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
2018-02-09 11:14:47 +01:00
|
|
|
|
2025-07-03 18:49:35 +02:00
|
|
|
assert(screen->has_frame);
|
|
|
|
|
if (!screen->has_video_window) {
|
|
|
|
|
screen->has_video_window = true;
|
2022-01-16 15:47:11 +01:00
|
|
|
// this is the very first frame, show the window
|
2022-01-16 15:46:37 +01:00
|
|
|
sc_screen_show_initial_window(screen);
|
2022-01-23 21:30:30 +01:00
|
|
|
|
|
|
|
|
if (sc_screen_is_relative_mode(screen)) {
|
|
|
|
|
// Capture mouse on start
|
2024-09-25 22:50:44 +02:00
|
|
|
sc_mouse_capture_set_active(&screen->mc, true);
|
2022-01-23 21:30:30 +01:00
|
|
|
}
|
2022-01-16 15:47:11 +01:00
|
|
|
}
|
|
|
|
|
|
2022-01-14 22:17:30 +01:00
|
|
|
sc_screen_render(screen, false);
|
2019-03-02 23:52:22 +01:00
|
|
|
return true;
|
2018-02-08 11:14:13 +01:00
|
|
|
}
|
|
|
|
|
|
2024-03-10 22:04:17 +01:00
|
|
|
static bool
|
|
|
|
|
sc_screen_update_frame(struct sc_screen *screen) {
|
2024-04-07 16:01:26 +02:00
|
|
|
assert(screen->video);
|
|
|
|
|
|
2024-03-10 22:04:17 +01:00
|
|
|
if (screen->paused) {
|
|
|
|
|
if (!screen->resume_frame) {
|
|
|
|
|
screen->resume_frame = av_frame_alloc();
|
|
|
|
|
if (!screen->resume_frame) {
|
|
|
|
|
LOG_OOM();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
av_frame_unref(screen->resume_frame);
|
|
|
|
|
}
|
|
|
|
|
sc_frame_buffer_consume(&screen->fb, screen->resume_frame);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
av_frame_unref(screen->frame);
|
|
|
|
|
sc_frame_buffer_consume(&screen->fb, screen->frame);
|
|
|
|
|
return sc_screen_apply_frame(screen);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
sc_screen_set_paused(struct sc_screen *screen, bool paused) {
|
2024-04-07 16:01:26 +02:00
|
|
|
assert(screen->video);
|
|
|
|
|
|
2024-03-10 22:04:17 +01:00
|
|
|
if (!paused && !screen->paused) {
|
|
|
|
|
// nothing to do
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (screen->paused && screen->resume_frame) {
|
|
|
|
|
// If display screen was paused, refresh the frame immediately, even if
|
|
|
|
|
// the new state is also paused.
|
|
|
|
|
av_frame_free(&screen->frame);
|
|
|
|
|
screen->frame = screen->resume_frame;
|
|
|
|
|
screen->resume_frame = NULL;
|
2026-01-23 21:51:13 +01:00
|
|
|
bool ok = sc_screen_apply_frame(screen);
|
|
|
|
|
if (!ok) {
|
|
|
|
|
LOGE("Resume frame update failed");
|
|
|
|
|
}
|
2024-03-10 22:04:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!paused) {
|
|
|
|
|
LOGI("Display screen unpaused");
|
|
|
|
|
} else if (!screen->paused) {
|
|
|
|
|
LOGI("Display screen paused");
|
|
|
|
|
} else {
|
|
|
|
|
LOGI("Display screen re-paused");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
screen->paused = paused;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-02 20:09:56 +01:00
|
|
|
void
|
2024-09-25 19:59:49 +02:00
|
|
|
sc_screen_toggle_fullscreen(struct sc_screen *screen) {
|
2024-04-07 16:01:26 +02:00
|
|
|
assert(screen->video);
|
|
|
|
|
|
2025-12-30 22:14:11 +01:00
|
|
|
bool req_fullscreen =
|
|
|
|
|
!(SDL_GetWindowFlags(screen->window) & SDL_WINDOW_FULLSCREEN);
|
|
|
|
|
|
|
|
|
|
bool ok = SDL_SetWindowFullscreen(screen->window, req_fullscreen);
|
2025-07-11 09:42:03 +02:00
|
|
|
if (!ok) {
|
2018-02-13 10:10:18 +01:00
|
|
|
LOGW("Could not switch fullscreen mode: %s", SDL_GetError());
|
2018-02-08 11:14:13 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-30 22:14:11 +01:00
|
|
|
LOGD("Requested %s mode", req_fullscreen ? "fullscreen" : "windowed");
|
2018-02-08 11:14:13 +01:00
|
|
|
}
|
|
|
|
|
|
2019-03-02 20:09:56 +01:00
|
|
|
void
|
2022-01-14 22:17:30 +01:00
|
|
|
sc_screen_resize_to_fit(struct sc_screen *screen) {
|
2024-04-07 16:01:26 +02:00
|
|
|
assert(screen->video);
|
|
|
|
|
|
2025-12-30 22:14:11 +01:00
|
|
|
if (!is_windowed(screen)) {
|
2019-11-11 21:44:27 +01:00
|
|
|
return;
|
2018-02-08 11:14:13 +01:00
|
|
|
}
|
2019-11-11 21:44:27 +01:00
|
|
|
|
2025-07-11 09:42:03 +02:00
|
|
|
struct sc_point point = sc_sdl_get_window_position(screen->window);
|
|
|
|
|
struct sc_size window_size = sc_sdl_get_window_size(screen->window);
|
2021-06-14 21:24:51 +02:00
|
|
|
|
2021-10-30 15:20:39 +02:00
|
|
|
struct sc_size optimal_size =
|
2021-12-20 19:17:15 +01:00
|
|
|
get_optimal_size(window_size, screen->content_size, false);
|
2021-06-14 21:24:51 +02:00
|
|
|
|
|
|
|
|
// Center the window related to the device screen
|
|
|
|
|
assert(optimal_size.width <= window_size.width);
|
|
|
|
|
assert(optimal_size.height <= window_size.height);
|
|
|
|
|
|
2025-07-11 09:42:03 +02:00
|
|
|
struct sc_point new_position = {
|
|
|
|
|
.x = point.x + (window_size.width - optimal_size.width) / 2,
|
|
|
|
|
.y = point.y + (window_size.height - optimal_size.height) / 2,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
sc_sdl_set_window_size(screen->window, optimal_size);
|
|
|
|
|
sc_sdl_set_window_position(screen->window, new_position);
|
2020-04-18 00:31:31 +02:00
|
|
|
LOGD("Resized to optimal size: %ux%u", optimal_size.width,
|
|
|
|
|
optimal_size.height);
|
2018-02-08 11:14:13 +01:00
|
|
|
}
|
|
|
|
|
|
2019-03-02 20:09:56 +01:00
|
|
|
void
|
2022-01-14 22:17:30 +01:00
|
|
|
sc_screen_resize_to_pixel_perfect(struct sc_screen *screen) {
|
2024-04-07 16:01:26 +02:00
|
|
|
assert(screen->video);
|
|
|
|
|
|
2025-12-30 22:14:11 +01:00
|
|
|
if (!is_windowed(screen)) {
|
2019-11-11 21:44:27 +01:00
|
|
|
return;
|
2018-02-08 11:14:13 +01:00
|
|
|
}
|
2019-11-11 21:44:27 +01:00
|
|
|
|
2021-10-30 15:20:39 +02:00
|
|
|
struct sc_size content_size = screen->content_size;
|
2025-07-11 09:42:03 +02:00
|
|
|
sc_sdl_set_window_size(screen->window, content_size);
|
2020-04-18 00:31:31 +02:00
|
|
|
LOGD("Resized to pixel-perfect: %ux%u", content_size.width,
|
|
|
|
|
content_size.height);
|
2018-02-08 11:14:13 +01:00
|
|
|
}
|
2019-10-20 15:32:33 +02:00
|
|
|
|
2023-03-10 22:49:40 +01:00
|
|
|
bool
|
2023-04-03 21:41:54 +02:00
|
|
|
sc_screen_handle_event(struct sc_screen *screen, const SDL_Event *event) {
|
2025-07-11 09:42:03 +02:00
|
|
|
// !video implies !has_video_window
|
|
|
|
|
assert(screen->video || !screen->has_video_window);
|
2021-02-15 18:44:53 +01:00
|
|
|
switch (event->type) {
|
2023-02-10 18:46:12 +01:00
|
|
|
case SC_EVENT_NEW_FRAME: {
|
2022-01-14 22:17:30 +01:00
|
|
|
bool ok = sc_screen_update_frame(screen);
|
2021-02-15 18:44:53 +01:00
|
|
|
if (!ok) {
|
2023-03-10 22:49:40 +01:00
|
|
|
LOGE("Frame update failed\n");
|
2021-02-15 18:44:53 +01:00
|
|
|
}
|
2023-03-10 22:49:40 +01:00
|
|
|
return true;
|
2022-01-16 15:47:11 +01:00
|
|
|
}
|
2025-07-11 09:42:03 +02:00
|
|
|
case SDL_EVENT_WINDOW_EXPOSED:
|
|
|
|
|
if (!screen->video) {
|
2024-04-07 16:01:26 +02:00
|
|
|
sc_screen_render_novideo(screen);
|
2025-07-11 09:42:03 +02:00
|
|
|
} else if (screen->has_video_window) {
|
|
|
|
|
sc_screen_render(screen, true);
|
2024-04-07 16:01:26 +02:00
|
|
|
}
|
2025-07-11 09:42:03 +02:00
|
|
|
return true;
|
|
|
|
|
case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED:
|
|
|
|
|
if (screen->has_video_window) {
|
|
|
|
|
sc_screen_render(screen, true);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
case SDL_EVENT_WINDOW_RESTORED:
|
2025-12-30 22:14:11 +01:00
|
|
|
if (screen->has_video_window && is_windowed(screen)) {
|
|
|
|
|
apply_pending_resize(screen);
|
|
|
|
|
sc_screen_render(screen, true);
|
2021-02-15 18:44:53 +01:00
|
|
|
}
|
2025-12-30 22:14:11 +01:00
|
|
|
return true;
|
|
|
|
|
case SDL_EVENT_WINDOW_ENTER_FULLSCREEN:
|
|
|
|
|
LOGD("Switched to fullscreen mode");
|
|
|
|
|
assert(screen->has_video_window);
|
|
|
|
|
return true;
|
|
|
|
|
case SDL_EVENT_WINDOW_LEAVE_FULLSCREEN:
|
|
|
|
|
LOGD("Switched to windowed mode");
|
|
|
|
|
assert(screen->has_video_window);
|
|
|
|
|
if (is_windowed(screen)) {
|
2025-07-11 09:42:03 +02:00
|
|
|
apply_pending_resize(screen);
|
|
|
|
|
sc_screen_render(screen, true);
|
2021-02-15 18:47:17 +01:00
|
|
|
}
|
2023-03-10 22:49:40 +01:00
|
|
|
return true;
|
2024-09-25 22:50:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sc_screen_is_relative_mode(screen)
|
|
|
|
|
&& sc_mouse_capture_handle_event(&screen->mc, event)) {
|
|
|
|
|
// The mouse capture handler consumed the event
|
|
|
|
|
return true;
|
2021-02-15 18:44:53 +01:00
|
|
|
}
|
|
|
|
|
|
2022-01-21 19:16:32 +01:00
|
|
|
sc_input_manager_handle_event(&screen->im, event);
|
2023-03-10 22:49:40 +01:00
|
|
|
return true;
|
2021-02-15 18:44:53 +01:00
|
|
|
}
|
|
|
|
|
|
2021-10-30 15:20:39 +02:00
|
|
|
struct sc_point
|
2022-01-14 22:17:30 +01:00
|
|
|
sc_screen_convert_drawable_to_frame_coords(struct sc_screen *screen,
|
|
|
|
|
int32_t x, int32_t y) {
|
2024-04-07 16:01:26 +02:00
|
|
|
assert(screen->video);
|
|
|
|
|
|
2023-11-19 01:06:59 +01:00
|
|
|
enum sc_orientation orientation = screen->orientation;
|
2020-04-17 18:43:29 +02:00
|
|
|
|
|
|
|
|
int32_t w = screen->content_size.width;
|
|
|
|
|
int32_t h = screen->content_size.height;
|
2020-04-17 18:44:24 +02:00
|
|
|
|
2023-06-27 18:38:10 +02:00
|
|
|
// screen->rect must be initialized to avoid a division by zero
|
|
|
|
|
assert(screen->rect.w && screen->rect.h);
|
2020-04-17 18:44:24 +02:00
|
|
|
|
|
|
|
|
x = (int64_t) (x - screen->rect.x) * w / screen->rect.w;
|
|
|
|
|
y = (int64_t) (y - screen->rect.y) * h / screen->rect.h;
|
|
|
|
|
|
2021-10-30 15:20:39 +02:00
|
|
|
struct sc_point result;
|
2023-11-19 01:06:59 +01:00
|
|
|
switch (orientation) {
|
|
|
|
|
case SC_ORIENTATION_0:
|
2020-04-17 18:43:29 +02:00
|
|
|
result.x = x;
|
|
|
|
|
result.y = y;
|
|
|
|
|
break;
|
2023-11-19 01:06:59 +01:00
|
|
|
case SC_ORIENTATION_90:
|
|
|
|
|
result.x = y;
|
|
|
|
|
result.y = w - x;
|
|
|
|
|
break;
|
|
|
|
|
case SC_ORIENTATION_180:
|
|
|
|
|
result.x = w - x;
|
|
|
|
|
result.y = h - y;
|
|
|
|
|
break;
|
|
|
|
|
case SC_ORIENTATION_270:
|
2020-04-17 18:43:29 +02:00
|
|
|
result.x = h - y;
|
|
|
|
|
result.y = x;
|
|
|
|
|
break;
|
2023-11-19 01:06:59 +01:00
|
|
|
case SC_ORIENTATION_FLIP_0:
|
2020-04-17 18:43:29 +02:00
|
|
|
result.x = w - x;
|
2023-11-19 01:06:59 +01:00
|
|
|
result.y = y;
|
|
|
|
|
break;
|
|
|
|
|
case SC_ORIENTATION_FLIP_90:
|
|
|
|
|
result.x = h - y;
|
|
|
|
|
result.y = w - x;
|
|
|
|
|
break;
|
|
|
|
|
case SC_ORIENTATION_FLIP_180:
|
|
|
|
|
result.x = x;
|
2020-04-17 18:43:29 +02:00
|
|
|
result.y = h - y;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2023-11-19 01:06:59 +01:00
|
|
|
assert(orientation == SC_ORIENTATION_FLIP_270);
|
2020-04-17 18:43:29 +02:00
|
|
|
result.x = y;
|
2023-11-19 01:06:59 +01:00
|
|
|
result.y = x;
|
2020-04-17 18:43:29 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2023-11-19 01:06:59 +01:00
|
|
|
|
2020-04-17 18:43:29 +02:00
|
|
|
return result;
|
|
|
|
|
}
|
2020-04-17 18:44:24 +02:00
|
|
|
|
2021-10-30 15:20:39 +02:00
|
|
|
struct sc_point
|
2022-01-14 22:17:30 +01:00
|
|
|
sc_screen_convert_window_to_frame_coords(struct sc_screen *screen,
|
|
|
|
|
int32_t x, int32_t y) {
|
|
|
|
|
sc_screen_hidpi_scale_coords(screen, &x, &y);
|
|
|
|
|
return sc_screen_convert_drawable_to_frame_coords(screen, x, y);
|
2020-06-25 08:54:40 +02:00
|
|
|
}
|
|
|
|
|
|
2020-04-17 18:44:24 +02:00
|
|
|
void
|
2022-01-14 22:17:30 +01:00
|
|
|
sc_screen_hidpi_scale_coords(struct sc_screen *screen, int32_t *x, int32_t *y) {
|
2020-04-17 18:44:24 +02:00
|
|
|
// take the HiDPI scaling (dw/ww and dh/wh) into account
|
2025-07-11 09:42:03 +02:00
|
|
|
|
|
|
|
|
struct sc_size window_size = sc_sdl_get_window_size(screen->window);
|
|
|
|
|
int64_t ww = window_size.width;
|
|
|
|
|
int64_t wh = window_size.height;
|
|
|
|
|
|
|
|
|
|
struct sc_size drawable_size =
|
|
|
|
|
sc_sdl_get_window_size_in_pixels(screen->window);
|
|
|
|
|
int64_t dw = drawable_size.width;
|
|
|
|
|
int64_t dh = drawable_size.height;
|
2020-04-17 18:44:24 +02:00
|
|
|
|
|
|
|
|
// scale for HiDPI (64 bits for intermediate multiplications)
|
|
|
|
|
*x = (int64_t) *x * dw / ww;
|
|
|
|
|
*y = (int64_t) *y * dh / wh;
|
|
|
|
|
}
|