summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--clients/meson.build40
-rw-r--r--clients/screenshooter.c657
-rw-r--r--meson.build4
-rw-r--r--shared/file-util.c126
-rw-r--r--shared/file-util.h43
-rw-r--r--shared/xalloc.c50
-rw-r--r--shared/xalloc.h51
7 files changed, 971 insertions, 0 deletions
diff --git a/clients/meson.build b/clients/meson.build
new file mode 100644
index 0000000..08b2c08
--- /dev/null
+++ b/clients/meson.build
@@ -0,0 +1,40 @@
+dep_wayland_client = dependency('wayland-client', version: '>= 1.17.0')
+
+clients = [
+{
+ 'basename': 'agl-screenshooter',
+ 'sources': [
+ 'screenshooter.c',
+ '../shared/file-util.c',
+ '../shared/os-compatibility.c',
+ '../shared/xalloc.c',
+ agl_screenshooter_client_protocol_h,
+ agl_screenshooter_protocol_c,
+ xdg_output_unstable_v1_client_protocol_h,
+ xdg_output_unstable_v1_protocol_c,
+ ],
+ 'deps_objs' : [ dep_wayland_client ],
+ 'deps': [ 'cairo' ],
+},
+]
+
+foreach t: clients
+ t_name = t.get('basename')
+ t_deps = t.get('deps_objs', [])
+
+ foreach xdep: t.get('deps', [])
+ dep = dependency(xdep, required: false)
+ if dep.found()
+ t_deps += dep
+ endif
+ endforeach
+
+ executable(
+ t_name, t.get('sources'),
+ include_directories: [ common_inc ],
+ dependencies: [ t_deps, libweston_dep ],
+ install: true,
+ )
+
+ message('Building client ' + t_name)
+endforeach
diff --git a/clients/screenshooter.c b/clients/screenshooter.c
new file mode 100644
index 0000000..faced55
--- /dev/null
+++ b/clients/screenshooter.c
@@ -0,0 +1,657 @@
+/*
+ * Copyright © 2020 Collabora, Ltd.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial
+ * portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdint.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <limits.h>
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <getopt.h>
+#include <cairo.h>
+
+#include <wayland-client.h>
+#include "shared/helpers.h"
+#include "shared/xalloc.h"
+#include "shared/file-util.h"
+#include "shared/os-compatibility.h"
+#include "agl-screenshooter-client-protocol.h"
+#include "xdg-output-unstable-v1-client-protocol.h"
+
+struct screenshooter_data;
+
+struct screenshooter_output {
+ struct wl_output *output;
+ struct wl_buffer *buffer;
+
+ int width, height, offset_x, offset_y, scale;
+ void *data;
+ struct screenshooter_data *sh_data;
+
+ struct wl_list link; /** screenshooter_data::output_list */
+};
+
+struct xdg_output_v1_info {
+ struct zxdg_output_v1 *xdg_output;
+ struct screenshooter_output *output;
+
+ char *name, *description;
+ struct wl_list link; /** screenshooter_data::xdg_output_list */
+};
+
+
+struct buffer_size {
+ int width, height;
+
+ int min_x, min_y;
+ int max_x, max_y;
+};
+
+struct screenshooter_data {
+ struct wl_display *display;
+ struct wl_shm *shm;
+ struct wl_list output_list; /** screenshooter_output::link */
+ struct wl_list xdg_output_list; /** xdg_output_v1_info::link */
+
+ struct zxdg_output_manager_v1 *xdg_output_manager;
+ struct agl_screenshooter *screenshooter;
+ int buffer_copy_done;
+};
+
+static int opts = 0x0;
+
+#define OPT_SCREENSHOT_OUTPUT 1
+#define OPT_SHOW_ALL_OUTPUTS 2
+#define OPT_SCREENSHOT_ALL_OUTPUTS 3
+
+static void
+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
+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
+display_handle_done(void *data, struct wl_output *wl_output)
+{
+
+}
+
+static void
+display_handle_scale(void *data, struct wl_output *wl_output,
+ int32_t scale)
+{
+ struct screenshooter_output *output = data;
+ output->scale = scale;
+}
+
+
+static const struct wl_output_listener output_listener = {
+ display_handle_geometry,
+ display_handle_mode,
+ display_handle_done,
+ display_handle_scale,
+};
+
+static void
+handle_xdg_output_v1_logical_position(void *data, struct zxdg_output_v1 *output,
+ int32_t x, int32_t y)
+{
+}
+
+static void
+handle_xdg_output_v1_logical_size(void *data, struct zxdg_output_v1 *output,
+ int32_t width, int32_t height)
+{
+}
+
+static void
+handle_xdg_output_v1_done(void *data, struct zxdg_output_v1 *output)
+{
+ /* Don't bother waiting for this; there's no good reason a
+ * compositor will wait more than one roundtrip before sending
+ * these initial events. */
+}
+
+static void
+handle_xdg_output_v1_name(void *data, struct zxdg_output_v1 *output,
+ const char *name)
+{
+ struct xdg_output_v1_info *xdg_output = data;
+ xdg_output->name = strdup(name);
+}
+
+static void
+handle_xdg_output_v1_description(void *data, struct zxdg_output_v1 *output,
+ const char *description)
+{
+ struct xdg_output_v1_info *xdg_output = data;
+ xdg_output->description = strdup(description);
+}
+
+static const struct zxdg_output_v1_listener xdg_output_v1_listener = {
+ .logical_position = handle_xdg_output_v1_logical_position,
+ .logical_size = handle_xdg_output_v1_logical_size,
+ .done = handle_xdg_output_v1_done,
+ .name = handle_xdg_output_v1_name,
+ .description = handle_xdg_output_v1_description,
+};
+
+static void
+add_xdg_output_v1_info(struct screenshooter_data *shooter_data,
+ struct screenshooter_output *output)
+{
+ struct xdg_output_v1_info *xdg_output = zalloc(sizeof(*xdg_output));
+
+ wl_list_insert(&shooter_data->xdg_output_list, &xdg_output->link);
+
+ xdg_output->xdg_output =
+ zxdg_output_manager_v1_get_xdg_output(shooter_data->xdg_output_manager,
+ output->output);
+
+ zxdg_output_v1_add_listener(xdg_output->xdg_output,
+ &xdg_output_v1_listener, xdg_output);
+ xdg_output->output = output;
+}
+
+static void
+screenshot_done(void *data, struct agl_screenshooter *screenshooter, uint32_t status)
+{
+ struct screenshooter_data *sh_data = data;
+ sh_data->buffer_copy_done = 1;
+}
+
+static const struct agl_screenshooter_listener screenshooter_listener = {
+ screenshot_done
+};
+
+static void
+handle_global(void *data, struct wl_registry *registry,
+ uint32_t name, const char *interface, uint32_t version)
+{
+ struct screenshooter_output *output;
+ struct screenshooter_data *sh_data = data;
+
+ if (strcmp(interface, "wl_output") == 0) {
+ output = zalloc(sizeof(*output));
+ if (!output)
+ return;
+
+ output->output = wl_registry_bind(registry, name,
+ &wl_output_interface, 1);
+ output->sh_data = sh_data;
+ 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, "agl_screenshooter") == 0) {
+ sh_data->screenshooter = wl_registry_bind(registry, name,
+ &agl_screenshooter_interface, 1);
+
+ agl_screenshooter_add_listener(sh_data->screenshooter,
+ &screenshooter_listener, sh_data);
+ } else if (strcmp(interface, "zxdg_output_manager_v1") == 0) {
+ sh_data->xdg_output_manager = wl_registry_bind(registry, name,
+ &zxdg_output_manager_v1_interface, version);
+ }
+}
+
+static void
+handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
+{
+ /* XXX: unimplemented */
+}
+
+static const struct wl_registry_listener registry_listener = {
+ handle_global,
+ handle_global_remove
+};
+
+static struct wl_buffer *
+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
+screenshot_write_png_per_output(const struct buffer_size *buff_size,
+ struct screenshooter_output *sh_output)
+{
+ int output_stride, buffer_stride, i;
+ cairo_surface_t *surface;
+ void *data, *d, *s;
+ FILE *fp;
+ char filepath[PATH_MAX];
+
+ buffer_stride = buff_size->width * 4;
+ data = xmalloc(buffer_stride * buff_size->height);
+ if (!data)
+ return;
+
+ output_stride = sh_output->width * 4;
+ s = sh_output->data;
+ d = data + (sh_output->offset_y - buff_size->min_y) * buffer_stride +
+ (sh_output->offset_x - buff_size->min_x) * 4;
+
+ for (i = 0; i < sh_output->height; i++) {
+ memcpy(d, s, output_stride);
+ d += buffer_stride;
+ s += output_stride;
+ }
+
+ surface = cairo_image_surface_create_for_data(data,
+ CAIRO_FORMAT_ARGB32,
+ buff_size->width,
+ buff_size->height,
+ buffer_stride);
+
+ fp = file_create_dated(getenv("XDG_PICTURES_DIR"), "agl-screenshot-",
+ ".png", filepath, sizeof(filepath));
+ if (fp) {
+ fclose(fp);
+ cairo_surface_write_to_png(surface, filepath);
+ }
+
+ cairo_surface_destroy(surface);
+ free(data);
+}
+
+static void
+screenshot_write_png(const struct buffer_size *buff_size,
+ struct wl_list *output_list)
+{
+ int output_stride, buffer_stride, i;
+ cairo_surface_t *surface;
+ void *data, *d, *s;
+ struct screenshooter_output *output, *next;
+ FILE *fp;
+ char filepath[PATH_MAX];
+
+ buffer_stride = buff_size->width * 4;
+
+ data = xmalloc(buffer_stride * buff_size->height);
+ if (!data)
+ return;
+
+ wl_list_for_each_safe(output, next, output_list, link) {
+ output_stride = output->width * 4;
+ s = output->data;
+ d = data + (output->offset_y - buff_size->min_y) * buffer_stride +
+ (output->offset_x - buff_size->min_x) * 4;
+
+ for (i = 0; i < output->height; i++) {
+ memcpy(d, s, output_stride);
+ d += buffer_stride;
+ s += output_stride;
+ }
+
+ free(output);
+ }
+
+ surface = cairo_image_surface_create_for_data(data,
+ CAIRO_FORMAT_ARGB32,
+ buff_size->width,
+ buff_size->height,
+ buffer_stride);
+
+ fp = file_create_dated(getenv("XDG_PICTURES_DIR"), "agl-screenshot-",
+ ".png", filepath, sizeof(filepath));
+ if (fp) {
+ fclose(fp);
+ cairo_surface_write_to_png(surface, filepath);
+ }
+
+ cairo_surface_destroy(surface);
+ free(data);
+}
+
+static int
+screenshot_set_buffer_size_per_output(struct buffer_size *buff_size,
+ struct screenshooter_output *output)
+{
+ buff_size->min_x = buff_size->min_y = INT_MAX;
+ buff_size->max_x = buff_size->max_y = INT_MIN;
+
+ 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 void
+screenshot_compute_output_offset(int *pos, struct screenshooter_output *sh_output)
+{
+ sh_output->offset_x = *pos;
+ *pos += sh_output->width;
+}
+
+static int
+screenshot_set_buffer_size(struct buffer_size *buff_size, struct wl_list *output_list)
+{
+ struct screenshooter_output *output;
+ int pos = 0;
+
+ wl_list_for_each_reverse(output, output_list, link)
+ screenshot_compute_output_offset(&pos, output);
+
+ wl_list_for_each(output, output_list, link)
+ if (screenshot_set_buffer_size_per_output(buff_size, output))
+ return -1;
+
+ return 0;
+}
+
+static struct screenshooter_output *
+agl_shooter_search_for_output(const char *output_name,
+ struct screenshooter_data *sh_data)
+{
+ struct screenshooter_output *found_output = NULL;
+ struct xdg_output_v1_info *output;
+
+ if (!output_name)
+ return found_output;
+
+ wl_list_for_each(output, &sh_data->xdg_output_list, link) {
+ if (output->name && strcmp(output->name, output_name) == 0) {
+ found_output = output->output;
+ break;
+ }
+ }
+
+ return found_output;
+}
+
+static void
+agl_shooter_display_all_outputs(struct screenshooter_data *sh_data)
+{
+ struct xdg_output_v1_info *xdg_output;
+ wl_list_for_each(xdg_output, &sh_data->xdg_output_list, link) {
+ fprintf(stdout, "Output '%s', desc: '%s'\n", xdg_output->name,
+ xdg_output->description);
+ }
+}
+
+
+static void
+agl_shooter_screenshot_all_outputs(struct screenshooter_data *sh_data)
+{
+ struct screenshooter_output *output;
+ struct buffer_size buff_size = {};
+
+ if (screenshot_set_buffer_size(&buff_size, &sh_data->output_list))
+ return;
+
+ 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);
+
+ agl_screenshooter_take_shot(sh_data->screenshooter,
+ output->output,
+ output->buffer);
+
+ sh_data->buffer_copy_done = 0;
+ while (!sh_data->buffer_copy_done)
+ wl_display_roundtrip(sh_data->display);
+ }
+
+ screenshot_write_png(&buff_size, &sh_data->output_list);
+}
+
+static void
+agl_shooter_screenshot_output(struct screenshooter_output *sh_output)
+{
+ int pos = 0;
+ struct buffer_size buff_size = {};
+ struct screenshooter_data *sh_data = sh_output->sh_data;
+
+ screenshot_compute_output_offset(&pos, sh_output);
+ screenshot_set_buffer_size_per_output(&buff_size, sh_output);
+
+ sh_output->buffer =
+ screenshot_create_shm_buffer(sh_output->width,
+ sh_output->height,
+ &sh_output->data, sh_data->shm);
+
+ agl_screenshooter_take_shot(sh_data->screenshooter,
+ sh_output->output,
+ sh_output->buffer);
+
+ sh_data->buffer_copy_done = 0;
+ while (!sh_data->buffer_copy_done)
+ wl_display_roundtrip(sh_data->display);
+
+ screenshot_write_png_per_output(&buff_size, sh_output);
+}
+
+static void
+agl_shooter_destroy_xdg_output_manager(struct screenshooter_data *sh_data)
+{
+ struct xdg_output_v1_info *xdg_output;
+
+ wl_list_for_each(xdg_output, &sh_data->xdg_output_list, link) {
+ free(xdg_output->name);
+ free(xdg_output->description);
+ zxdg_output_v1_destroy(xdg_output->xdg_output);
+ }
+
+ zxdg_output_manager_v1_destroy(sh_data->xdg_output_manager);
+}
+
+static void
+print_usage_and_exit(void)
+{
+ fprintf(stderr, "./agl-screenshooter [-o OUTPUT_NAME] [-l] [-a]\n");
+
+ fprintf(stderr, "\t-o OUTPUT_NAME -- take a screenshot of the output "
+ "specified by OUTPUT_NAME\n");
+ fprintf(stderr, "\t-a -- take a screenshot of all the outputs found\n");
+ fprintf(stderr, "\t-l -- list all the outputs found\n");
+ exit(EXIT_FAILURE);
+}
+
+int main(int argc, char *argv[])
+{
+ struct wl_display *display;
+ struct wl_registry *registry;
+
+ struct screenshooter_data sh_data = {};
+ struct screenshooter_output *sh_output = NULL;
+ int c, option_index;
+
+ char *output_name = NULL;
+
+ static struct option long_options[] = {
+ {"output", required_argument, 0, 'o' },
+ {"list", required_argument, 0, 'l' },
+ {"all", required_argument, 0, 'a' },
+ {"help", no_argument , 0, 'h' },
+ {0, 0, 0, 0}
+ };
+
+ while ((c = getopt_long(argc, argv, "o:lah",
+ long_options, &option_index)) != -1) {
+ switch (c) {
+ case 'o':
+ output_name = optarg;
+ opts |= (1 << OPT_SCREENSHOT_OUTPUT);
+ break;
+ case 'l':
+ opts |= (1 << OPT_SHOW_ALL_OUTPUTS);
+ break;
+ case 'a':
+ opts |= (1 << OPT_SCREENSHOT_ALL_OUTPUTS);
+ break;
+ default:
+ print_usage_and_exit();
+ }
+ }
+
+ display = wl_display_connect(NULL);
+ if (display == NULL) {
+ fprintf(stderr, "failed to create display: %s\n",
+ strerror(errno));
+ return EXIT_FAILURE;
+ }
+
+ wl_list_init(&sh_data.output_list);
+ wl_list_init(&sh_data.xdg_output_list);
+ sh_data.display = display;
+
+ registry = wl_display_get_registry(display);
+ wl_registry_add_listener(registry, &registry_listener, &sh_data);
+
+ wl_display_dispatch(display);
+ wl_display_roundtrip(display);
+
+
+ if (sh_data.screenshooter == NULL) {
+ fprintf(stderr, "Compositor doesn't support screenshooter\n");
+ return EXIT_FAILURE;
+ }
+
+ wl_list_for_each(sh_output, &sh_data.output_list, link)
+ add_xdg_output_v1_info(&sh_data, sh_output);
+
+ /* do another round-trip for xdg_output */
+ wl_display_roundtrip(sh_data.display);
+
+ if (opts & (1 << OPT_SHOW_ALL_OUTPUTS)) {
+ agl_shooter_display_all_outputs(&sh_data);
+ agl_shooter_destroy_xdg_output_manager(&sh_data);
+ return EXIT_SUCCESS;
+ }
+
+ if (opts & (1 << OPT_SCREENSHOT_ALL_OUTPUTS)) {
+ agl_shooter_screenshot_all_outputs(&sh_data);
+ agl_shooter_destroy_xdg_output_manager(&sh_data);
+ return EXIT_SUCCESS;
+ }
+
+ sh_output = NULL;
+ if (output_name)
+ sh_output = agl_shooter_search_for_output(output_name, &sh_data);
+
+ if (!sh_output && (opts & (1 << OPT_SCREENSHOT_OUTPUT))) {
+ fprintf(stderr, "Could not find an output matching '%s'\n",
+ output_name);
+ agl_shooter_destroy_xdg_output_manager(&sh_data);
+ return EXIT_FAILURE;
+ }
+
+ /* if we're still here just pick the first one available
+ * and use that. Still useful in case we are run without
+ * any args whatsoever */
+ if (!sh_output)
+ sh_output = container_of(sh_data.output_list.next,
+ struct screenshooter_output, link);
+
+ /* take a screenshot only of that specific output */
+ agl_shooter_screenshot_output(sh_output);
+ agl_shooter_destroy_xdg_output_manager(&sh_data);
+
+ return 0;
+}
diff --git a/meson.build b/meson.build
index 4299d58..6809579 100644
--- a/meson.build
+++ b/meson.build
@@ -73,6 +73,7 @@ protocols = [
{ 'name': 'agl-shell-desktop', 'source': 'internal' },
{ 'name': 'agl-screenshooter', 'source': 'internal' },
{ 'name': 'xdg-shell', 'source': 'wp-stable' },
+ { 'name': 'xdg-output', 'source': 'unstable', 'version': 'v1' },
]
foreach proto: protocols
@@ -225,3 +226,6 @@ install_data(
[ agl_shell_xml, agl_shell_desktop_xml ],
install_dir: join_paths(dir_data, dir_data_agl_compositor)
)
+
+common_inc = [ include_directories('src'), include_directories('.') ]
+subdir('clients')
diff --git a/shared/file-util.c b/shared/file-util.c
new file mode 100644
index 0000000..4170771
--- /dev/null
+++ b/shared/file-util.c
@@ -0,0 +1,126 @@
+/*
+ * Copyright © 2015 Collabora, Ltd.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial
+ * portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <errno.h>
+#include <time.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdlib.h>
+
+#include "file-util.h"
+
+static int
+current_time_str(char *str, size_t len, const char *fmt)
+{
+ time_t t;
+ struct tm *t_local;
+ int ret;
+
+ t = time(NULL);
+ t_local = localtime(&t);
+ if (!t_local) {
+ errno = ETIME;
+ return -1;
+ }
+
+ ret = strftime(str, len, fmt, t_local);
+ if (ret == 0) {
+ errno = ETIME;
+ return -1;
+ }
+
+ return ret;
+}
+
+static int
+create_file_excl(const char *fname)
+{
+ return open(fname, O_RDWR | O_CLOEXEC | O_CREAT | O_EXCL, 00666);
+}
+
+/** Create a unique file with date and time in the name
+ *
+ * \param path File path
+ * \param prefix File name prefix.
+ * \param suffix File name suffix.
+ * \param[out] name_out Buffer for the resulting file name.
+ * \param name_len Number of bytes usable in name_out.
+ * \return stdio FILE pointer, or NULL on failure.
+ *
+ * Create and open a new file with the name concatenated from
+ * path/prefix, date and time, and suffix. If a file with this name
+ * already exists, an counter number is added to the end of the
+ * date and time sub-string. The counter is increased until a free file
+ * name is found.
+ *
+ * Once creating the file succeeds, the name of the file is in name_out.
+ * On failure, the contents of name_out are undefined and errno is set.
+ */
+FILE *
+file_create_dated(const char *path, const char *prefix, const char *suffix,
+ char *name_out, size_t name_len)
+{
+ char timestr[128];
+ int ret;
+ int fd;
+ int cnt = 0;
+ int with_path;
+
+ with_path = path && path[0];
+
+ if (current_time_str(timestr, sizeof(timestr), "%F_%H-%M-%S") < 0)
+ return NULL;
+
+ ret = snprintf(name_out, name_len, "%s%s%s%s%s",
+ with_path ? path : "", with_path ? "/" : "",
+ prefix, timestr, suffix);
+ if (ret < 0 || (size_t)ret >= name_len) {
+ errno = ENOBUFS;
+ return NULL;
+ }
+
+ fd = create_file_excl(name_out);
+
+ while (fd == -1 && errno == EEXIST) {
+ cnt++;
+
+ ret = snprintf(name_out, name_len, "%s%s%s%s-%d%s",
+ with_path ? path : "", with_path ? "/" : "",
+ prefix, timestr, cnt, suffix);
+ if (ret < 0 || (size_t)ret >= name_len) {
+ errno = ENOBUFS;
+ return NULL;
+ }
+
+ fd = create_file_excl(name_out);
+ }
+
+ if (fd == -1)
+ return NULL;
+
+ return fdopen(fd, "w");
+}
diff --git a/shared/file-util.h b/shared/file-util.h
new file mode 100644
index 0000000..e272dde
--- /dev/null
+++ b/shared/file-util.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright © 2015 Collabora, Ltd.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial
+ * portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef WESTON_FILE_UTIL_H
+#define WESTON_FILE_UTIL_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdio.h>
+
+FILE *
+file_create_dated(const char *path, const char *prefix, const char *suffix,
+ char *name_out, size_t name_len);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* WESTON_FILE_UTIL_H */
diff --git a/shared/xalloc.c b/shared/xalloc.c
new file mode 100644
index 0000000..1cc5c12
--- /dev/null
+++ b/shared/xalloc.c
@@ -0,0 +1,50 @@
+/*
+ * Copyright © 2008 Kristian Høgsberg
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial
+ * portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "config.h"
+
+#include <errno.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "xalloc.h"
+
+void *
+fail_on_null(void *p, size_t size, char *file, int32_t line)
+{
+ if (p == NULL) {
+ fprintf(stderr, "[%s] ", program_invocation_short_name);
+ if (file)
+ fprintf(stderr, "%s:%d: ", file, line);
+ fprintf(stderr, "out of memory");
+ if (size)
+ fprintf(stderr, " (%zd)", size);
+ fprintf(stderr, "\n");
+ exit(EXIT_FAILURE);
+ }
+
+ return p;
+}
diff --git a/shared/xalloc.h b/shared/xalloc.h
new file mode 100644
index 0000000..cd39dd8
--- /dev/null
+++ b/shared/xalloc.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright © 2008 Kristian Høgsberg
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial
+ * portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef WESTON_XALLOC_H
+#define WESTON_XALLOC_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <libweston/zalloc.h>
+
+void *
+fail_on_null(void *p, size_t size, char *file, int32_t line);
+
+#define xmalloc(s) (fail_on_null(malloc(s), (s), __FILE__, __LINE__))
+#define xzalloc(s) (fail_on_null(zalloc(s), (s), __FILE__, __LINE__))
+#define xstrdup(s) (fail_on_null(strdup(s), 0, __FILE__, __LINE__))
+#define xrealloc(p, s) (fail_on_null(realloc(p, s), (s), __FILE__, __LINE__))
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* WESTON_XALLOC_H */