2014-02-13 13:05:23 -08: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/>.
|
|
|
|
*/
|
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <inttypes.h>
|
2014-02-09 04:50:15 -08:00
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#include <X11/Xutil.h>
|
2014-02-13 13:05:23 -08:00
|
|
|
|
2014-07-09 22:12:57 -07:00
|
|
|
#include <obs-module.h>
|
2014-02-09 11:36:52 -08:00
|
|
|
#include "xcursor.h"
|
2014-04-22 10:53:06 -07:00
|
|
|
#include "xhelpers.h"
|
2014-02-10 10:09:44 -08:00
|
|
|
|
|
|
|
#define XSHM_DATA(voidptr) struct xshm_data *data = voidptr;
|
2014-02-09 04:50:15 -08:00
|
|
|
|
|
|
|
struct xshm_data {
|
2014-02-20 10:41:04 -08:00
|
|
|
Display *dpy;
|
2014-04-22 10:53:06 -07:00
|
|
|
Screen *screen;
|
|
|
|
|
|
|
|
int_fast32_t x_org, y_org;
|
|
|
|
int_fast32_t width, height;
|
|
|
|
|
|
|
|
xshm_t *xshm;
|
2014-02-20 10:41:04 -08:00
|
|
|
texture_t texture;
|
2014-04-22 10:53:06 -07:00
|
|
|
|
|
|
|
bool show_cursor;
|
2014-02-20 10:41:04 -08:00
|
|
|
xcursor_t *cursor;
|
2014-04-22 10:53:06 -07:00
|
|
|
|
|
|
|
bool use_xinerama;
|
2014-02-09 04:50:15 -08:00
|
|
|
};
|
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
/**
|
|
|
|
* Resize the texture
|
|
|
|
*
|
|
|
|
* This will automatically create the texture if it does not exist
|
|
|
|
*/
|
|
|
|
static void xshm_resize_texture(struct xshm_data *data)
|
|
|
|
{
|
|
|
|
gs_entercontext(obs_graphics());
|
|
|
|
|
|
|
|
if (data->texture)
|
|
|
|
texture_destroy(data->texture);
|
|
|
|
data->texture = gs_create_texture(data->width, data->height,
|
|
|
|
GS_BGRA, 1, NULL, GS_DYNAMIC);
|
|
|
|
|
|
|
|
gs_leavecontext();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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,
|
|
|
|
obs_data_t settings)
|
|
|
|
{
|
|
|
|
int_fast32_t old_width = data->width;
|
|
|
|
int_fast32_t old_height = data->height;
|
|
|
|
int_fast32_t screen = obs_data_getint(settings, "screen");
|
|
|
|
|
|
|
|
if (data->use_xinerama) {
|
|
|
|
if (xinerama_screen_geo(data->dpy, screen,
|
|
|
|
&data->x_org, &data->y_org,
|
|
|
|
&data->width, &data->height) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
data->screen = XDefaultScreenOfDisplay(data->dpy);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
data->x_org = 0;
|
|
|
|
data->y_org = 0;
|
|
|
|
if (x11_screen_geo(data->dpy, screen,
|
|
|
|
&data->width, &data->height) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
data->screen = XScreenOfDisplay(data->dpy, screen);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!data->width || !data->height) {
|
|
|
|
blog(LOG_ERROR, "xshm-input: Failed to get geometry");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
blog(LOG_INFO, "xshm-input: Geometry %"PRIdFAST32"x%"PRIdFAST32
|
|
|
|
" @ %"PRIdFAST32",%"PRIdFAST32,
|
|
|
|
data->width, data->height, data->x_org, data->y_org);
|
|
|
|
|
|
|
|
if (old_width == data->width && old_height == data->height)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the name of the plugin
|
|
|
|
*/
|
2014-06-25 00:13:00 -07:00
|
|
|
static const char* xshm_getname(void)
|
2014-02-09 04:50:15 -08:00
|
|
|
{
|
2014-07-09 22:12:57 -07:00
|
|
|
return obs_module_text("X11SharedMemoryScreenInput");
|
2014-02-09 04:50:15 -08:00
|
|
|
}
|
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
/**
|
|
|
|
* Update the capture with changed settings
|
|
|
|
*/
|
|
|
|
static void xshm_update(void *vptr, obs_data_t settings)
|
2014-02-10 10:09:44 -08:00
|
|
|
{
|
2014-02-20 10:41:04 -08:00
|
|
|
XSHM_DATA(vptr);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
data->show_cursor = obs_data_getbool(settings, "show_cursor");
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
if (data->xshm)
|
|
|
|
xshm_detach(data->xshm);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
if (xshm_update_geometry(data, settings) < 0) {
|
|
|
|
blog(LOG_ERROR, "xshm-input: failed to update geometry !");
|
|
|
|
return;
|
|
|
|
}
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
xshm_resize_texture(data);
|
|
|
|
xcursor_offset(data->cursor, data->x_org, data->y_org);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
data->xshm = xshm_attach(data->dpy, data->screen,
|
|
|
|
data->width, data->height);
|
|
|
|
if (!data->xshm) {
|
|
|
|
blog(LOG_ERROR, "xshm-input: failed to attach shm !");
|
|
|
|
return;
|
2014-02-20 10:41:04 -08:00
|
|
|
}
|
2014-04-22 10:53:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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_setbool(defaults, "show_cursor", true);
|
|
|
|
}
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
/**
|
|
|
|
* Get the properties for the capture
|
|
|
|
*/
|
2014-06-25 00:13:00 -07:00
|
|
|
static obs_properties_t xshm_properties(void)
|
2014-04-22 10:53:06 -07:00
|
|
|
{
|
2014-06-24 23:54:10 -07:00
|
|
|
obs_properties_t props = obs_properties_create();
|
2014-04-22 10:53:06 -07:00
|
|
|
int_fast32_t screen_max;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
Display *dpy = XOpenDisplay(NULL);
|
|
|
|
screen_max = xinerama_is_active(dpy)
|
|
|
|
? xinerama_screen_count(dpy)
|
|
|
|
: XScreenCount(dpy);
|
|
|
|
screen_max = (screen_max) ? screen_max - 1 : 0;
|
|
|
|
XCloseDisplay(dpy);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-07-09 22:12:57 -07:00
|
|
|
obs_properties_add_int(props, "screen",
|
|
|
|
obs_module_text("Screen"), 0, screen_max, 1);
|
|
|
|
obs_properties_add_bool(props, "show_cursor",
|
|
|
|
obs_module_text("CaptureCursor"));
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
return props;
|
2014-02-10 10:09:44 -08:00
|
|
|
}
|
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
/**
|
|
|
|
* Destroy the capture
|
|
|
|
*/
|
|
|
|
static void xshm_destroy(void *vptr)
|
2014-02-09 04:50:15 -08:00
|
|
|
{
|
2014-04-22 10:53:06 -07:00
|
|
|
XSHM_DATA(vptr);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
if (!data)
|
|
|
|
return;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
gs_entercontext(obs_graphics());
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
if (data->texture)
|
|
|
|
texture_destroy(data->texture);
|
|
|
|
if (data->cursor)
|
|
|
|
xcursor_destroy(data->cursor);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
gs_leavecontext();
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
if (data->xshm)
|
|
|
|
xshm_detach(data->xshm);
|
|
|
|
if (data->dpy)
|
|
|
|
XCloseDisplay(data->dpy);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
bfree(data);
|
|
|
|
}
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
/**
|
|
|
|
* Create the capture
|
|
|
|
*/
|
|
|
|
static void *xshm_create(obs_data_t settings, obs_source_t source)
|
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(source);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
struct xshm_data *data = bzalloc(sizeof(struct xshm_data));
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
data->dpy = XOpenDisplay(NULL);
|
|
|
|
if (!data->dpy) {
|
|
|
|
blog(LOG_ERROR, "xshm-input: Unable to open X display !");
|
2014-02-20 10:41:04 -08:00
|
|
|
goto fail;
|
2014-04-22 10:53:06 -07:00
|
|
|
}
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
if (!XShmQueryExtension(data->dpy)) {
|
|
|
|
blog(LOG_ERROR, "xshm-input: XShm extension not found !");
|
2014-02-20 10:41:04 -08:00
|
|
|
goto fail;
|
|
|
|
}
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
data->use_xinerama = xinerama_is_active(data->dpy) ? true : false;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-20 10:41:04 -08:00
|
|
|
gs_entercontext(obs_graphics());
|
|
|
|
data->cursor = xcursor_init(data->dpy);
|
|
|
|
gs_leavecontext();
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
xshm_update(data, settings);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-20 10:41:04 -08:00
|
|
|
return data;
|
2014-02-09 04:50:15 -08:00
|
|
|
fail:
|
2014-02-20 10:41:04 -08:00
|
|
|
xshm_destroy(data);
|
|
|
|
return NULL;
|
2014-02-09 04:50:15 -08:00
|
|
|
}
|
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
/**
|
|
|
|
* Prepare the capture data
|
|
|
|
*/
|
2014-02-20 10:41:04 -08:00
|
|
|
static void xshm_video_tick(void *vptr, float seconds)
|
2014-02-09 04:50:15 -08:00
|
|
|
{
|
2014-02-20 10:41:04 -08:00
|
|
|
UNUSED_PARAMETER(seconds);
|
|
|
|
XSHM_DATA(vptr);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
if (!data->xshm)
|
|
|
|
return;
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
gs_entercontext(obs_graphics());
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
XShmGetImage(data->dpy, XRootWindowOfScreen(data->screen),
|
|
|
|
data->xshm->image, data->x_org, data->y_org, AllPlanes);
|
|
|
|
texture_setimage(data->texture, (void *) data->xshm->image->data,
|
|
|
|
data->width * 4, false);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-20 10:41:04 -08:00
|
|
|
xcursor_tick(data->cursor);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-02-20 10:41:04 -08:00
|
|
|
gs_leavecontext();
|
2014-02-10 10:09:44 -08:00
|
|
|
}
|
2014-02-09 04:50:15 -08:00
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
/**
|
|
|
|
* Render the capture data
|
|
|
|
*/
|
2014-02-20 10:41:04 -08:00
|
|
|
static void xshm_video_render(void *vptr, effect_t effect)
|
2014-02-10 10:09:44 -08:00
|
|
|
{
|
2014-02-20 10:41:04 -08:00
|
|
|
XSHM_DATA(vptr);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
if (!data->xshm)
|
|
|
|
return;
|
|
|
|
|
2014-02-20 10:41:04 -08:00
|
|
|
eparam_t image = effect_getparambyname(effect, "image");
|
2014-06-25 20:29:46 -07:00
|
|
|
effect_settexture(image, data->texture);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
gs_enable_blending(false);
|
2014-02-20 10:41:04 -08:00
|
|
|
gs_draw_sprite(data->texture, 0, 0, 0);
|
2014-03-11 10:12:54 -07:00
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
if (data->show_cursor)
|
|
|
|
xcursor_render(data->cursor);
|
2014-07-03 14:12:48 -07:00
|
|
|
|
|
|
|
gs_reset_blend_state();
|
2014-02-09 04:50:15 -08:00
|
|
|
}
|
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
/**
|
|
|
|
* Width of the captured data
|
|
|
|
*/
|
2014-02-20 10:41:04 -08:00
|
|
|
static uint32_t xshm_getwidth(void *vptr)
|
2014-02-09 04:50:15 -08:00
|
|
|
{
|
2014-02-20 10:41:04 -08:00
|
|
|
XSHM_DATA(vptr);
|
2014-04-22 10:53:06 -07:00
|
|
|
return data->width;
|
2014-02-09 04:50:15 -08:00
|
|
|
}
|
|
|
|
|
2014-04-22 10:53:06 -07:00
|
|
|
/**
|
|
|
|
* Height of the captured data
|
|
|
|
*/
|
2014-02-20 10:41:04 -08:00
|
|
|
static uint32_t xshm_getheight(void *vptr)
|
2014-02-09 04:50:15 -08:00
|
|
|
{
|
2014-02-20 10:41:04 -08:00
|
|
|
XSHM_DATA(vptr);
|
2014-04-22 10:53:06 -07:00
|
|
|
return data->height;
|
2014-02-09 04:50:15 -08:00
|
|
|
}
|
2014-02-10 10:09:44 -08:00
|
|
|
|
|
|
|
struct obs_source_info xshm_input = {
|
2014-04-22 10:53:06 -07:00
|
|
|
.id = "xshm_input",
|
|
|
|
.type = OBS_SOURCE_TYPE_INPUT,
|
|
|
|
.output_flags = OBS_SOURCE_VIDEO,
|
|
|
|
.getname = xshm_getname,
|
|
|
|
.create = xshm_create,
|
|
|
|
.destroy = xshm_destroy,
|
|
|
|
.update = xshm_update,
|
|
|
|
.defaults = xshm_defaults,
|
|
|
|
.properties = xshm_properties,
|
|
|
|
.video_tick = xshm_video_tick,
|
|
|
|
.video_render = xshm_video_render,
|
|
|
|
.getwidth = xshm_getwidth,
|
|
|
|
.getheight = xshm_getheight
|
2014-02-10 10:09:44 -08:00
|
|
|
};
|