yolobs-studio/plugins/linux-capture/xshm-input.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

577 lines
14 KiB
C
Raw Permalink Normal View History

2016-02-23 23:16:51 +00:00
/*
Copyright (C) 2014 by Leonhard Oelke <leonhard@in-verted.de>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
2019-07-27 12:47:10 +00:00
#include <xcb/randr.h>
2016-02-23 23:16:51 +00:00
#include <xcb/shm.h>
#include <xcb/xfixes.h>
#include <xcb/xinerama.h>
#include <obs-module.h>
#include <util/dstr.h>
#include "xcursor-xcb.h"
#include "xhelpers.h"
#define XSHM_DATA(voidptr) struct xshm_data *data = voidptr;
#define blog(level, msg, ...) blog(level, "xshm-input: " msg, ##__VA_ARGS__)
struct xshm_data {
2019-09-22 21:19:10 +00:00
obs_source_t *source;
2016-02-23 23:16:51 +00:00
xcb_connection_t *xcb;
2019-09-22 21:19:10 +00:00
xcb_screen_t *xcb_screen;
xcb_shm_t *xshm;
xcb_xcursor_t *cursor;
char *server;
uint_fast32_t screen_id;
int_fast32_t x_org;
int_fast32_t y_org;
int_fast32_t width;
int_fast32_t height;
gs_texture_t *texture;
2020-10-01 20:15:25 +00:00
int_fast32_t cut_top;
int_fast32_t cut_left;
int_fast32_t cut_right;
int_fast32_t cut_bot;
int_fast32_t adj_x_org;
int_fast32_t adj_y_org;
int_fast32_t adj_width;
int_fast32_t adj_height;
2019-09-22 21:19:10 +00:00
bool show_cursor;
bool use_xinerama;
bool use_randr;
bool advanced;
2016-02-23 23:16:51 +00:00
};
/**
* Resize the texture
*
* This will automatically create the texture if it does not exist
*
* @note requires to be called within the obs graphics context
*/
static inline void xshm_resize_texture(struct xshm_data *data)
{
if (data->texture)
gs_texture_destroy(data->texture);
2020-10-01 20:15:25 +00:00
data->texture = gs_texture_create(data->adj_width, data->adj_height,
GS_BGRA, 1, NULL, GS_DYNAMIC);
2016-02-23 23:16:51 +00:00
}
/**
* Check if the xserver supports all the extensions we need
*/
static bool xshm_check_extensions(xcb_connection_t *xcb)
{
bool ok = true;
if (!xcb_get_extension_data(xcb, &xcb_shm_id)->present) {
blog(LOG_ERROR, "Missing SHM extension !");
ok = false;
}
if (!xcb_get_extension_data(xcb, &xcb_xinerama_id)->present)
blog(LOG_INFO, "Missing Xinerama extension !");
2019-07-27 12:47:10 +00:00
if (!xcb_get_extension_data(xcb, &xcb_randr_id)->present)
blog(LOG_INFO, "Missing Randr extension !");
2016-02-23 23:16:51 +00:00
return ok;
}
/**
* Update the capture
*
* @return < 0 on error, 0 when size is unchanged, > 1 on size change
*/
static int_fast32_t xshm_update_geometry(struct xshm_data *data)
{
2020-10-01 20:15:25 +00:00
int_fast32_t prev_width = data->adj_width;
int_fast32_t prev_height = data->adj_height;
2016-02-23 23:16:51 +00:00
2019-07-27 12:47:10 +00:00
if (data->use_randr) {
2019-09-22 21:19:10 +00:00
if (randr_screen_geo(data->xcb, data->screen_id, &data->x_org,
&data->y_org, &data->width, &data->height,
2020-03-25 08:07:22 +00:00
&data->xcb_screen, NULL) < 0) {
2019-07-27 12:47:10 +00:00
return -1;
}
2019-09-22 21:19:10 +00:00
} else if (data->use_xinerama) {
2016-02-23 23:16:51 +00:00
if (xinerama_screen_geo(data->xcb, data->screen_id,
2019-09-22 21:19:10 +00:00
&data->x_org, &data->y_org,
&data->width, &data->height) < 0) {
2016-02-23 23:16:51 +00:00
return -1;
}
data->xcb_screen = xcb_get_screen(data->xcb, 0);
2019-09-22 21:19:10 +00:00
} else {
2016-02-23 23:16:51 +00:00
data->x_org = 0;
data->y_org = 0;
2019-09-22 21:19:10 +00:00
if (x11_screen_geo(data->xcb, data->screen_id, &data->width,
&data->height) < 0) {
2016-02-23 23:16:51 +00:00
return -1;
}
data->xcb_screen = xcb_get_screen(data->xcb, data->screen_id);
}
if (!data->width || !data->height) {
blog(LOG_ERROR, "Failed to get geometry");
return -1;
}
2020-10-01 20:15:25 +00:00
data->adj_y_org = data->y_org;
data->adj_x_org = data->x_org;
data->adj_height = data->height;
data->adj_width = data->width;
if (data->cut_top != 0) {
if (data->y_org > 0)
data->adj_y_org = data->y_org + data->cut_top;
else
data->adj_y_org = data->cut_top;
data->adj_height = data->adj_height - data->cut_top;
}
if (data->cut_left != 0) {
if (data->x_org > 0)
data->adj_x_org = data->x_org + data->cut_left;
else
data->adj_x_org = data->cut_left;
data->adj_width = data->adj_width - data->cut_left;
}
if (data->cut_right != 0)
data->adj_width = data->adj_width - data->cut_right;
if (data->cut_bot != 0)
data->adj_height = data->adj_height - data->cut_bot;
2019-09-22 21:19:10 +00:00
blog(LOG_INFO,
"Geometry %" PRIdFAST32 "x%" PRIdFAST32 " @ %" PRIdFAST32
",%" PRIdFAST32,
data->width, data->height, data->x_org, data->y_org);
2016-02-23 23:16:51 +00:00
2020-10-01 20:15:25 +00:00
if (prev_width == data->adj_width && prev_height == data->adj_height)
2016-02-23 23:16:51 +00:00
return 0;
return 1;
}
/**
* Returns the name of the plugin
*/
2019-09-22 21:19:10 +00:00
static const char *xshm_getname(void *unused)
2016-02-23 23:16:51 +00:00
{
UNUSED_PARAMETER(unused);
return obs_module_text("X11SharedMemoryScreenInput");
}
/**
* Stop the capture
*/
static void xshm_capture_stop(struct xshm_data *data)
{
obs_enter_graphics();
if (data->texture) {
gs_texture_destroy(data->texture);
data->texture = NULL;
}
if (data->cursor) {
xcb_xcursor_destroy(data->cursor);
data->cursor = NULL;
}
obs_leave_graphics();
if (data->xshm) {
xshm_xcb_detach(data->xshm);
data->xshm = NULL;
}
if (data->xcb) {
xcb_disconnect(data->xcb);
data->xcb = NULL;
}
if (data->server) {
bfree(data->server);
data->server = NULL;
}
}
/**
* Start the capture
*/
static void xshm_capture_start(struct xshm_data *data)
{
2019-09-22 21:19:10 +00:00
const char *server = (data->advanced && *data->server) ? data->server
: NULL;
2016-02-23 23:16:51 +00:00
data->xcb = xcb_connect(server, NULL);
if (!data->xcb || xcb_connection_has_error(data->xcb)) {
blog(LOG_ERROR, "Unable to open X display !");
goto fail;
}
if (!xshm_check_extensions(data->xcb))
goto fail;
2019-07-27 12:47:10 +00:00
data->use_randr = randr_is_active(data->xcb) ? true : false;
2016-02-23 23:16:51 +00:00
data->use_xinerama = xinerama_is_active(data->xcb) ? true : false;
if (xshm_update_geometry(data) < 0) {
blog(LOG_ERROR, "failed to update geometry !");
goto fail;
}
2020-10-01 20:15:25 +00:00
data->xshm =
xshm_xcb_attach(data->xcb, data->adj_width, data->adj_height);
2016-02-23 23:16:51 +00:00
if (!data->xshm) {
blog(LOG_ERROR, "failed to attach shm !");
goto fail;
}
data->cursor = xcb_xcursor_init(data->xcb);
2020-10-01 20:15:25 +00:00
xcb_xcursor_offset(data->cursor, data->adj_x_org, data->adj_y_org);
2016-02-23 23:16:51 +00:00
obs_enter_graphics();
xshm_resize_texture(data);
obs_leave_graphics();
return;
fail:
xshm_capture_stop(data);
}
/**
* Update the capture with changed settings
*/
static void xshm_update(void *vptr, obs_data_t *settings)
{
XSHM_DATA(vptr);
xshm_capture_stop(data);
2019-09-22 21:19:10 +00:00
data->screen_id = obs_data_get_int(settings, "screen");
2016-02-23 23:16:51 +00:00
data->show_cursor = obs_data_get_bool(settings, "show_cursor");
2019-09-22 21:19:10 +00:00
data->advanced = obs_data_get_bool(settings, "advanced");
data->server = bstrdup(obs_data_get_string(settings, "server"));
2016-02-23 23:16:51 +00:00
2020-10-01 20:15:25 +00:00
data->cut_top = obs_data_get_int(settings, "cut_top");
data->cut_left = obs_data_get_int(settings, "cut_left");
data->cut_right = obs_data_get_int(settings, "cut_right");
data->cut_bot = obs_data_get_int(settings, "cut_bot");
2016-02-23 23:16:51 +00:00
xshm_capture_start(data);
}
/**
* Get the default settings for the capture
*/
static void xshm_defaults(obs_data_t *defaults)
{
obs_data_set_default_int(defaults, "screen", 0);
obs_data_set_default_bool(defaults, "show_cursor", true);
obs_data_set_default_bool(defaults, "advanced", false);
2020-10-01 20:15:25 +00:00
obs_data_set_default_int(defaults, "cut_top", 0);
obs_data_set_default_int(defaults, "cut_left", 0);
obs_data_set_default_int(defaults, "cut_right", 0);
obs_data_set_default_int(defaults, "cut_bot", 0);
2016-02-23 23:16:51 +00:00
}
/**
* Toggle visibility of advanced settings
*/
2019-09-22 21:19:10 +00:00
static bool xshm_toggle_advanced(obs_properties_t *props, obs_property_t *p,
obs_data_t *settings)
2016-02-23 23:16:51 +00:00
{
UNUSED_PARAMETER(p);
2019-09-22 21:19:10 +00:00
const bool visible = obs_data_get_bool(settings, "advanced");
2016-02-23 23:16:51 +00:00
obs_property_t *server = obs_properties_get(props, "server");
obs_property_set_visible(server, visible);
/* trigger server changed callback so the screen list is refreshed */
obs_property_modified(server, settings);
return true;
}
/**
* The x server was changed
*/
2019-09-22 21:19:10 +00:00
static bool xshm_server_changed(obs_properties_t *props, obs_property_t *p,
obs_data_t *settings)
2016-02-23 23:16:51 +00:00
{
UNUSED_PARAMETER(p);
2019-09-22 21:19:10 +00:00
bool advanced = obs_data_get_bool(settings, "advanced");
2016-02-23 23:16:51 +00:00
int_fast32_t old_screen = obs_data_get_int(settings, "screen");
2019-09-22 21:19:10 +00:00
const char *server = obs_data_get_string(settings, "server");
2016-02-23 23:16:51 +00:00
obs_property_t *screens = obs_properties_get(props, "screen");
/* we want a real NULL here in case there is no string here */
server = (advanced && *server) ? server : NULL;
obs_property_list_clear(screens);
xcb_connection_t *xcb = xcb_connect(server, NULL);
if (!xcb || xcb_connection_has_error(xcb)) {
obs_property_set_enabled(screens, false);
return true;
}
struct dstr screen_info;
dstr_init(&screen_info);
2019-07-27 12:47:10 +00:00
bool randr = randr_is_active(xcb);
2016-02-23 23:16:51 +00:00
bool xinerama = xinerama_is_active(xcb);
2019-09-22 21:19:10 +00:00
int_fast32_t count =
(randr) ? randr_screen_count(xcb)
: (xinerama)
? xinerama_screen_count(xcb)
: xcb_setup_roots_length(xcb_get_setup(xcb));
2016-02-23 23:16:51 +00:00
for (int_fast32_t i = 0; i < count; ++i) {
2020-03-25 08:07:22 +00:00
char *name;
char name_tmp[12];
2016-02-23 23:16:51 +00:00
int_fast32_t x, y, w, h;
x = y = w = h = 0;
2020-03-25 08:07:22 +00:00
name = NULL;
2019-07-27 12:47:10 +00:00
if (randr)
2020-03-25 08:07:22 +00:00
randr_screen_geo(xcb, i, &x, &y, &w, &h, NULL, &name);
2019-07-27 12:47:10 +00:00
else if (xinerama)
2016-02-23 23:16:51 +00:00
xinerama_screen_geo(xcb, i, &x, &y, &w, &h);
else
x11_screen_geo(xcb, i, &w, &h);
2020-03-25 08:07:22 +00:00
if (name == NULL) {
sprintf(name_tmp, "%" PRIuFAST32, i);
name = name_tmp;
}
2019-09-22 21:19:10 +00:00
dstr_printf(&screen_info,
2020-03-25 08:07:22 +00:00
"Screen %s (%" PRIuFAST32 "x%" PRIuFAST32
" @ %" PRIuFAST32 ",%" PRIuFAST32 ")",
name, w, h, x, y);
if (name != name_tmp)
free(name);
2016-02-23 23:16:51 +00:00
2019-12-10 19:31:54 +00:00
if (h > 0 && w > 0)
obs_property_list_add_int(screens, screen_info.array,
i);
2016-02-23 23:16:51 +00:00
}
/* handle missing screen */
if (old_screen + 1 > count) {
2019-09-22 21:19:10 +00:00
dstr_printf(&screen_info, "Screen %" PRIuFAST32 " (not found)",
old_screen);
size_t index = obs_property_list_add_int(
screens, screen_info.array, old_screen);
2016-02-23 23:16:51 +00:00
obs_property_list_item_disable(screens, index, true);
}
dstr_free(&screen_info);
xcb_disconnect(xcb);
obs_property_set_enabled(screens, true);
return true;
}
/**
* Get the properties for the capture
*/
static obs_properties_t *xshm_properties(void *vptr)
{
XSHM_DATA(vptr);
obs_properties_t *props = obs_properties_create();
obs_properties_add_list(props, "screen", obs_module_text("Screen"),
2019-09-22 21:19:10 +00:00
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
2016-02-23 23:16:51 +00:00
obs_properties_add_bool(props, "show_cursor",
2019-09-22 21:19:10 +00:00
obs_module_text("CaptureCursor"));
obs_property_t *advanced = obs_properties_add_bool(
props, "advanced", obs_module_text("AdvancedSettings"));
2020-10-01 20:15:25 +00:00
obs_properties_add_int(props, "cut_top", obs_module_text("CropTop"),
-4096, 4096, 1);
obs_properties_add_int(props, "cut_left", obs_module_text("CropLeft"),
-4096, 4096, 1);
obs_properties_add_int(props, "cut_right", obs_module_text("CropRight"),
0, 4096, 1);
obs_properties_add_int(props, "cut_bot", obs_module_text("CropBottom"),
0, 4096, 1);
2019-09-22 21:19:10 +00:00
obs_property_t *server = obs_properties_add_text(
props, "server", obs_module_text("XServer"), OBS_TEXT_DEFAULT);
2016-02-23 23:16:51 +00:00
obs_property_set_modified_callback(advanced, xshm_toggle_advanced);
obs_property_set_modified_callback(server, xshm_server_changed);
/* trigger server callback to get screen count ... */
obs_data_t *settings = obs_source_get_settings(data->source);
obs_property_modified(server, settings);
obs_data_release(settings);
return props;
}
/**
* Destroy the capture
*/
static void xshm_destroy(void *vptr)
{
XSHM_DATA(vptr);
if (!data)
return;
xshm_capture_stop(data);
bfree(data);
}
/**
* Create the capture
*/
static void *xshm_create(obs_data_t *settings, obs_source_t *source)
{
struct xshm_data *data = bzalloc(sizeof(struct xshm_data));
data->source = source;
xshm_update(data, settings);
return data;
}
/**
* Prepare the capture data
*/
static void xshm_video_tick(void *vptr, float seconds)
{
UNUSED_PARAMETER(seconds);
XSHM_DATA(vptr);
if (!data->texture)
return;
if (!obs_source_showing(data->source))
return;
2019-09-22 21:19:10 +00:00
xcb_shm_get_image_cookie_t img_c;
xcb_shm_get_image_reply_t *img_r;
2016-02-23 23:16:51 +00:00
xcb_xfixes_get_cursor_image_cookie_t cur_c;
2019-09-22 21:19:10 +00:00
xcb_xfixes_get_cursor_image_reply_t *cur_r;
2016-02-23 23:16:51 +00:00
img_c = xcb_shm_get_image_unchecked(data->xcb, data->xcb_screen->root,
2020-10-01 20:15:25 +00:00
data->adj_x_org, data->adj_y_org,
data->adj_width, data->adj_height,
~0, XCB_IMAGE_FORMAT_Z_PIXMAP,
2019-09-22 21:19:10 +00:00
data->xshm->seg, 0);
2016-02-23 23:16:51 +00:00
cur_c = xcb_xfixes_get_cursor_image_unchecked(data->xcb);
img_r = xcb_shm_get_image_reply(data->xcb, img_c, NULL);
cur_r = xcb_xfixes_get_cursor_image_reply(data->xcb, cur_c, NULL);
if (!img_r)
goto exit;
obs_enter_graphics();
2019-09-22 21:19:10 +00:00
gs_texture_set_image(data->texture, (void *)data->xshm->data,
2020-10-01 20:15:25 +00:00
data->adj_width * 4, false);
2016-02-23 23:16:51 +00:00
xcb_xcursor_update(data->cursor, cur_r);
obs_leave_graphics();
exit:
free(img_r);
free(cur_r);
}
/**
* Render the capture data
*/
static void xshm_video_render(void *vptr, gs_effect_t *effect)
{
XSHM_DATA(vptr);
effect = obs_get_base_effect(OBS_EFFECT_OPAQUE);
if (!data->texture)
return;
gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
gs_effect_set_texture(image, data->texture);
while (gs_effect_loop(effect, "Draw")) {
gs_draw_sprite(data->texture, 0, 0, 0);
}
if (data->show_cursor) {
effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
while (gs_effect_loop(effect, "Draw")) {
xcb_xcursor_render(data->cursor);
}
}
}
/**
* Width of the captured data
*/
static uint32_t xshm_getwidth(void *vptr)
{
XSHM_DATA(vptr);
2020-10-01 20:15:25 +00:00
return data->adj_width;
2016-02-23 23:16:51 +00:00
}
/**
* Height of the captured data
*/
static uint32_t xshm_getheight(void *vptr)
{
XSHM_DATA(vptr);
2020-10-01 20:15:25 +00:00
return data->adj_height;
2016-02-23 23:16:51 +00:00
}
struct obs_source_info xshm_input = {
2019-09-22 21:19:10 +00:00
.id = "xshm_input",
.type = OBS_SOURCE_TYPE_INPUT,
.output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
OBS_SOURCE_DO_NOT_DUPLICATE,
.get_name = xshm_getname,
.create = xshm_create,
.destroy = xshm_destroy,
.update = xshm_update,
.get_defaults = xshm_defaults,
2016-02-23 23:16:51 +00:00
.get_properties = xshm_properties,
2019-09-22 21:19:10 +00:00
.video_tick = xshm_video_tick,
.video_render = xshm_video_render,
.get_width = xshm_getwidth,
.get_height = xshm_getheight,
2020-03-25 08:07:22 +00:00
.icon_type = OBS_ICON_TYPE_DESKTOP_CAPTURE,
2016-02-23 23:16:51 +00:00
};