diff --git a/.clangd b/.clangd index 32bb9c5..c71d16c 100644 --- a/.clangd +++ b/.clangd @@ -1,2 +1,2 @@ CompileFlags: - Add: ['-I/usr/include/gstreamer-1.0', '-I/usr/lib/glib-2.0/include', '-I/usr/include/glib-2.0'] + Add: ['-I/usr/include/gstreamer-1.0', '-I/usr/lib/glib-2.0/include', '-I/usr/include/glib-2.0', '-I/media/user/luks/Work/Piklema/imx-weston'] diff --git a/clients/gstwestonimagesrc.c b/clients/gstwestonimagesrc.c index ff44992..df50645 100644 --- a/clients/gstwestonimagesrc.c +++ b/clients/gstwestonimagesrc.c @@ -1,7 +1,26 @@ -#include "gstwestonimagesrc.h" -#include "gst/gstinfo.h" +#include +#include +#include +#include +#include +#include #include +#include +#include +#include +#include +#include "config.h" + +#include "shared/file-util.h" +#include "shared/os-compatibility.h" +#include "shared/xalloc.h" +#include "weston-screenshooter-client-protocol.h" + +#include "gst/gstinfo.h" +#include "gstwestonimagesrc.h" + +// Gtreamer plugin related defines and declarations GST_DEBUG_CATEGORY_STATIC(gst_westonimagesrc_debug); #define GST_CAT_DEFAULT gst_westonimagesrc_debug @@ -22,6 +41,147 @@ static void gst_westonimagesrc_get_property(GObject *object, guint prop_id, GVal G_DEFINE_TYPE(GstWestonImageSrc, gst_westonimagesrc, GST_TYPE_BASE_SRC); +// Screenshoter logic related declarations and defines +struct screenshooter_output { + struct wl_output *output; + struct wl_buffer *buffer; + int width, height, offset_x, offset_y; + void *data; + struct wl_list link; +}; + +struct buffer_size { + int width, height; + int min_x, min_y; + int max_x, max_y; +}; + +struct screenshooter_data { + struct wl_shm *shm; + struct wl_list output_list; + struct weston_screenshooter *screenshooter; + int buffer_copy_done; +}; + +static void weston_display_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags, int width, int height, int refresh); +static void weston_display_handle_geometry(void *data, struct wl_output *wl_output, int x, int y, int physical_width, int physical_height, int subpixel, const char *make, + const char *model, int transform); +static void weston_screenshot_done(void *data, struct weston_screenshooter *screenshooter); +static void weston_handle_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version); +static void weston_handle_global_remove(void *data, struct wl_registry *registry, uint32_t name); + +static void weston_handle_global_remove(void *data, struct wl_registry *registry, uint32_t name) { /* XXX: unimplemented */ } +static struct wl_buffer *weston_screenshot_create_shm_buffer(int width, int height, void **data_out, struct wl_shm *shm); +static int weston_screenshot_set_buffer_size(struct buffer_size *buff_size, struct wl_list *output_list); + +static const struct wl_output_listener output_listener = {weston_display_handle_geometry, weston_display_handle_mode}; +static const struct weston_screenshooter_listener screenshooter_listener = {weston_screenshot_done}; +static const struct wl_registry_listener registry_listener = {weston_handle_global, weston_handle_global_remove}; + +static int weston_screenshot_set_buffer_size(struct buffer_size *buff_size, struct wl_list *output_list) { + struct screenshooter_output *output; + buff_size->min_x = buff_size->min_y = INT_MAX; + buff_size->max_x = buff_size->max_y = INT_MIN; + int position = 0; + + wl_list_for_each_reverse(output, output_list, link) { + output->offset_x = position; + position += output->width; + } + + wl_list_for_each(output, output_list, link) { + buff_size->min_x = MIN(buff_size->min_x, output->offset_x); + buff_size->min_y = MIN(buff_size->min_y, output->offset_y); + buff_size->max_x = MAX(buff_size->max_x, output->offset_x + output->width); + buff_size->max_y = MAX(buff_size->max_y, output->offset_y + output->height); + } + + if (buff_size->max_x <= buff_size->min_x || buff_size->max_y <= buff_size->min_y) + return -1; + + buff_size->width = buff_size->max_x - buff_size->min_x; + buff_size->height = buff_size->max_y - buff_size->min_y; + + return 0; +} + +static struct wl_buffer *weston_screenshot_create_shm_buffer(int width, int height, void **data_out, struct wl_shm *shm) { + struct wl_shm_pool *pool; + struct wl_buffer *buffer; + int fd, size, stride; + void *data; + + stride = width * 4; + size = stride * height; + + fd = os_create_anonymous_file(size); + if (fd < 0) { + fprintf(stderr, "creating a buffer file for %d B failed: %s\n", size, strerror(errno)); + return NULL; + } + + data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (data == MAP_FAILED) { + fprintf(stderr, "mmap failed: %s\n", strerror(errno)); + close(fd); + return NULL; + } + + pool = wl_shm_create_pool(shm, fd, size); + close(fd); + buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, WL_SHM_FORMAT_XRGB8888); + wl_shm_pool_destroy(pool); + + *data_out = data; + + return buffer; +} + +static void weston_handle_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) { + static struct screenshooter_output *output; + struct screenshooter_data *sh_data = data; + + if (strcmp(interface, "wl_output") == 0) { + + output = xmalloc(sizeof *output); + output->output = wl_registry_bind(registry, name, &wl_output_interface, 1); + wl_list_insert(&sh_data->output_list, &output->link); + wl_output_add_listener(output->output, &output_listener, output); + } else if (strcmp(interface, "wl_shm") == 0) { + + sh_data->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); + } else if (strcmp(interface, "weston_screenshooter") == 0) { + + sh_data->screenshooter = wl_registry_bind(registry, name, &weston_screenshooter_interface, 1); + } +} + +static void screenshot_done(void *data, struct weston_screenshooter *screenshooter) { + struct screenshooter_data *sh_data = data; + sh_data->buffer_copy_done = 1; +} + +static void weston_display_handle_geometry(void *data, struct wl_output *wl_output, int x, int y, int physical_width, int physical_height, int subpixel, const char *make, + const char *model, int transform) { + struct screenshooter_output *output; + output = wl_output_get_user_data(wl_output); + + if (wl_output == output->output) { + output->offset_x = x; + output->offset_y = y; + } +} + +static void weston_display_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags, int width, int height, int refresh) { + struct screenshooter_output *output; + output = wl_output_get_user_data(wl_output); + + if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT)) { + output->width = width; + output->height = height; + } +} + static void gst_westonimagesrc_class_init(GstWestonImageSrcClass *klass) { GObjectClass *gobject_class = G_OBJECT_CLASS(klass); GstElementClass *gstelement_class = GST_ELEMENT_CLASS(klass); @@ -58,6 +218,25 @@ static void gst_westonimagesrc_init(GstWestonImageSrc *westonimagesrc) { g_mutex_init(&westonimagesrc->lock); g_cond_init(&westonimagesrc->cond); + + // Get current display + display = wl_display_connect(NULL); + if (display == NULL) { + fprintf(stderr, "failed to create display: %s\n", strerror(errno)); + } + + wl_list_init(&sh_data.output_list); + registry = wl_display_get_registry(display); + wl_registry_add_listener(registry, ®istry_listener, &sh_data); + wl_display_dispatch(display); + wl_display_roundtrip(display); + + if (sh_data.screenshooter == NULL) { + fprintf(stderr, "display doesn't support screenshooter\n"); + } + + weston_screenshooter_add_listener(sh_data.screenshooter, &screenshooter_listener, &sh_data); + screenshot_set_buffer_size(&buff_size, &sh_data.output_list); } static void gst_westonimagesrc_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) { @@ -125,6 +304,7 @@ static gboolean gst_westonimagesrc_stop(GstBaseSrc *basesrc) { } static GstFlowReturn gst_westonimagesrc_create(GstBaseSrc *basesrc, guint64 offset, guint size, GstBuffer **buf) { + // Gstreamer related GstWestonImageSrc *westonimagesrc = GST_WESTONIMAGESRC(basesrc); GstMapInfo map; gchar *message; @@ -149,6 +329,14 @@ static GstFlowReturn gst_westonimagesrc_create(GstBaseSrc *basesrc, guint64 offs return GST_FLOW_FLUSHING; } + wl_list_for_each(output, &sh_data.output_list, link) { + output->buffer = screenshot_create_shm_buffer(output->width, output->height, &output->data, sh_data.shm); + weston_screenshooter_shoot(sh_data.screenshooter, output->output, output->buffer); + sh_data.buffer_copy_done = 0; + while (!sh_data.buffer_copy_done) + wl_display_roundtrip(display); + } + message = g_strdup_printf("westonimagesrc world #%lu\n", (gulong)westonimagesrc->offset); message_len = strlen(message); diff --git a/clients/gstwestonimagesrc.h b/clients/gstwestonimagesrc.h index e1c7bd7..a3ee0e8 100644 --- a/clients/gstwestonimagesrc.h +++ b/clients/gstwestonimagesrc.h @@ -33,4 +33,11 @@ GType gst_westonimagesrc_get_type(void); G_END_DECLS +// Weston related structures +struct wl_display *display; +struct wl_registry *registry; +struct screenshooter_output *output; +struct buffer_size buff_size = {}; +struct screenshooter_data sh_data = {}; + #endif /* __GST_WESTONIMAGESRC_H__ */ diff --git a/clients/meson.build b/clients/meson.build index 2c016b8..c3371ed 100644 --- a/clients/meson.build +++ b/clients/meson.build @@ -343,16 +343,48 @@ if get_option('shell-desktop') env_modmap += 'weston-screenshooter=@0@;'.format(exe_shooter.full_path()) exe_shell_desktop = executable( - 'weston-desktop-shell', - 'desktop-shell.c', - weston_desktop_shell_client_protocol_h, - weston_desktop_shell_protocol_c, - include_directories: common_inc, - dependencies: dep_toytoolkit, - install_dir: get_option('libexecdir'), - install: true + 'weston-desktop-shell', + 'desktop-shell.c', + weston_desktop_shell_client_protocol_h, + weston_desktop_shell_protocol_c, + include_directories: common_inc, + dependencies: dep_toytoolkit, + install_dir: get_option('libexecdir'), + install: true ) env_modmap += 'weston-desktop-shell=@0@;'.format(exe_shell_desktop.full_path()) + + plugins_install_dir = join_paths(get_option('libdir'), 'gstreamer-1.0') + cc = meson.get_compiler('c') + gst_version = meson.project_version() + api_version = '0.1' + + gst_dep = dependency('gstreamer-1.0', + fallback : ['gstreamer', 'gst_dep']) + + gst_westonimagesrc_plugin_c_args = ['-DHAVE_CONFIG_H'] + + gst_westonimagesrc_cdata = configuration_data() + gst_westonimagesrc_cdata.set_quoted('PACKAGE_VERSION', gst_version) + gst_westonimagesrc_cdata.set_quoted('PACKAGE', 'gst-template-plugin') + gst_westonimagesrc_cdata.set_quoted('GST_LICENSE', 'LGPL') + gst_westonimagesrc_cdata.set_quoted('GST_API_VERSION', api_version) + gst_westonimagesrc_cdata.set_quoted('GST_PACKAGE_NAME', 'GStreamer template Plug-ins') + gst_westonimagesrc_cdata.set_quoted('GST_PACKAGE_ORIGIN', 'https://gstreamer.freedesktop.org') + configure_file(output : 'config.h', configuration : gst_westonimagesrc_cdata) + + # The westonimagesrc Plugin + gstwestonimagesrc_sources = [ + 'src/gstwestonimagesrc.c', + ] + + gstwestonimagesrcexample = library('gstwestonimagesrc', + gstwestonimagesrc_sources, + c_args: gst_westonimagesrc_plugin_c_args, + dependencies : [gst_dep, gstbase_dep], + install : true, + install_dir : plugins_install_dir, + ) endif diff --git a/clients/screenshot.c b/clients/screenshot.c index bbf2e6b..46ecc5a 100644 --- a/clients/screenshot.c +++ b/clients/screenshot.c @@ -36,6 +36,7 @@ #include #include +#include #include "weston-screenshooter-client-protocol.h" #include "shared/os-compatibility.h" #include "shared/xalloc.h"