Skip to content

Commit

Permalink
Extract make_shm_pool
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanGriffiths committed Oct 5, 2023
1 parent 4922742 commit 893152d
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 95 deletions.
6 changes: 5 additions & 1 deletion examples/client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ add_custom_command(
COMMAND "sh" "-c" "wayland-scanner private-code ${MIR_SHELL_X} ${MIR_SHELL_C}"
)

add_library (mir_demo_wayland_extensions STATIC ${XDG_SHELL_C} ${XDG_SHELL_H} ${MIR_SHELL_C} ${MIR_SHELL_H})
add_library (mir_demo_wayland_extensions STATIC
${XDG_SHELL_C} ${XDG_SHELL_H}
${MIR_SHELL_C} ${MIR_SHELL_H}
make_shm_pool.c make_shm_pool.h
)
set_target_properties (mir_demo_wayland_extensions PROPERTIES COMPILE_FLAGS "${CMAKE_CFLAGS} -fvisibility=hidden")
target_include_directories(mir_demo_wayland_extensions PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries (mir_demo_wayland_extensions PUBLIC PkgConfig::WAYLAND_CLIENT)
Expand Down
45 changes: 45 additions & 0 deletions examples/client/make_shm_pool.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright © Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 or 3 as
* published by the Free Software Foundation.
*
* 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 "make_shm_pool.h"

#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>

struct wl_shm_pool*
make_shm_pool(struct wl_shm* shm, int size, void **data)
{
int fd = memfd_create("make_shm_pool", MFD_CLOEXEC);

if (fd < 0) {
return NULL;
}

posix_fallocate(fd, 0, size);

*data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (*data == MAP_FAILED) {
close(fd);
return NULL;
}

struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, size);

close(fd);

return pool;
}
33 changes: 33 additions & 0 deletions examples/client/make_shm_pool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright © Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 or 3 as
* published by the Free Software Foundation.
*
* 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/>.
*/

#ifndef MIR_MAKE_SHM_POOL_H
#define MIR_MAKE_SHM_POOL_H

#include <wayland-client.h>

#ifdef __cplusplus
extern "C" {
#endif

struct wl_shm_pool*
make_shm_pool(struct wl_shm* shm, int size, void** data);

#ifdef __cplusplus
}
#endif

#endif //MIR_MAKE_SHM_POOL_H
60 changes: 7 additions & 53 deletions examples/client/mir_shell_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <fcntl.h>
#include <memory>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#include "make_shm_pool.h"
#include "xdg-shell.h"
#include "mir-shell.h"

#include <wayland-client.h>
#include <wayland-client-core.h>
#include "xdg-shell.h"
#include "mir-shell.h"

#include <memory>
#include <signal.h>
#include <string.h>

namespace
{
Expand Down Expand Up @@ -140,49 +137,6 @@ void globals::init()
xdg_wm_base_add_listener(globals::wm_base, &shell_listener, NULL);
}

wl_shm_pool* make_shm_pool(wl_shm* shm, int size, void** data)
{
static char* shm_dir = NULL;
if (!shm_dir) shm_dir = getenv("XDG_RUNTIME_DIR");

int fd = open(shm_dir, O_TMPFILE | O_RDWR | O_EXCL, S_IRWXU);

// Workaround for filesystems that don't support O_TMPFILE
if (fd < 0)
{
char template_filename[] = "/dev/shm/mir-buffer-XXXXXX";
fd = mkostemp(template_filename, O_CLOEXEC);
if (fd != -1)
{
if (unlink(template_filename) < 0)
{
close(fd);
fd = -1;
}
}
}

if (fd < 0)
{
return NULL;
}

posix_fallocate(fd, 0, size);

*data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (*data == MAP_FAILED)
{
close(fd);
return NULL;
}

auto const pool = wl_shm_create_pool(shm, fd, size);

close(fd);

return pool;
}

class pulsing_window
{
public:
Expand Down
42 changes: 1 addition & 41 deletions examples/client/wayland_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <wayland-client.h>
#include <wayland-client-core.h>
#include "xdg-shell.h"
#include "make_shm_pool.h"

static int const pixel_size = 4;
#define NO_OF_BUFFERS 4
Expand Down Expand Up @@ -121,47 +122,6 @@ static struct wl_registry_listener const registry_listener = {
.global_remove = global_remove
};

static struct wl_shm_pool*
make_shm_pool(struct wl_shm* shm, int size, void **data)
{
static char* shm_dir = NULL;
if (!shm_dir) shm_dir = getenv("XDG_RUNTIME_DIR");

int fd = open(shm_dir, O_TMPFILE | O_RDWR | O_EXCL, S_IRWXU);

// Workaround for filesystems that don't support O_TMPFILE
if (fd < 0) {
char template_filename[] = "/dev/shm/mir-buffer-XXXXXX";
fd = mkostemp(template_filename, O_CLOEXEC);
if (fd != -1)
{
if (unlink(template_filename) < 0)
{
close(fd);
fd = -1;
}
}
}

if (fd < 0) {
return NULL;
}

posix_fallocate(fd, 0, size);

*data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (*data == MAP_FAILED) {
close(fd);
return NULL;
}

struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, size);

close(fd);

return pool;
}

static struct wl_buffer_listener const buffer_listener;

typedef struct buffer
Expand Down

0 comments on commit 893152d

Please sign in to comment.