Skip to content

Commit

Permalink
Add functions to create and destroy a gbm_device to wlr_screencast_co…
Browse files Browse the repository at this point in the history
…mmon.c
  • Loading branch information
columbarius committed Sep 13, 2020
1 parent dda4da1 commit 7723fa1
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 0 deletions.
5 changes: 5 additions & 0 deletions include/wlr_screencast_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@

#include <screencast_common.h>

#include <gbm.h>

struct wl_buffer *wlr_create_shm_buffer(struct xdpw_screencast_instance *cast,
enum wl_shm_format fmt, int width, int height, int stride,
void **data_out);

struct gbm_device *create_gbm_device();
void destroy_gbm_device(struct gbm_device *gbm);

#endif /* !WLR_SCREENCAST_COMMON_H */
5 changes: 5 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ rt = cc.find_library('rt')
pipewire = dependency('libpipewire-0.3', version: '>= 0.2.9')
wayland_client = dependency('wayland-client')
wayland_protos = dependency('wayland-protocols', version: '>=1.14')
gbm = dependency('gbm')
drm = dependency('libdrm')

logind = dependency('libsystemd', required: false)
if logind.found()
Expand All @@ -51,13 +53,16 @@ executable(
'src/screencast/wlr_screencast_common.c',
'src/screencast/wlr_screencopy.c',
'src/screencast/pipewire_screencast.c',
'src/tmp/strlcpy.c',
]),
dependencies: [
wayland_client,
wlr_protos,
logind,
pipewire,
rt,
gbm,
drm,
],
include_directories: [inc],
install: true,
Expand Down
10 changes: 10 additions & 0 deletions src/screencast/wlr_screencast.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "wlr_screencast.h"
#include "wlr_screencast_common.h"
#include "wlr_screencopy.h"

#include "wlr-screencopy-unstable-v1-client-protocol.h"
Expand Down Expand Up @@ -213,6 +214,12 @@ int xdpw_wlr_screencast_init(struct xdpw_state *state) {
return -1;
}

// create gbm_device
ctx->gbm = create_gbm_device();
if (!ctx->gbm) {
logprint(ERROR, "System doesn't support %s!", "gbm");
}

if (xdpw_wlr_screencopy_init(state) != 0) {
return -1;
}
Expand All @@ -238,6 +245,9 @@ void xdpw_wlr_screencast_finish(struct xdpw_screencast_context *ctx) {
if (ctx->shm) {
wl_shm_destroy(ctx->shm);
}
if (ctx->gbm) {
destroy_gbm_device(ctx->gbm);
}
if (ctx->xdg_output_manager) {
zxdg_output_manager_v1_destroy(ctx->xdg_output_manager);
}
Expand Down
48 changes: 48 additions & 0 deletions src/screencast/wlr_screencast_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#include "xdpw.h"
#include "logger.h"

#include<gdbm.h>
#include<xf86drm.h>
#include <bsd/string.h>

static int anonymous_shm_open(void) {
char name[] = "/xdpw-shm-XXXXXX";
int retries = 100;
Expand Down Expand Up @@ -75,3 +79,47 @@ struct wl_buffer *wlr_create_shm_buffer(struct xdpw_screencast_instance *cast,
*data_out = data;
return buffer;
}

static int gbm_find_render_node(char *node, size_t maxlen) {
bool r = -1;
drmDevice *devices[64];

int n = drmGetDevices2(0, devices, sizeof(devices) / sizeof(devices[0]));
for (int i = 0; i < n; ++i) {
drmDevice *dev = devices[i];
if (!(dev->available_nodes & (1 << DRM_NODE_RENDER)))
continue;

strlcpy(node, dev->nodes[DRM_NODE_RENDER], maxlen);
r = 0;
break;
}

drmFreeDevices(devices, n);
return r;
}

struct gbm_device *create_gbm_device(){
struct gbm_device *gbm;
char render_node[256];

if (gbm_find_render_node(render_node, sizeof(render_node)) < 0) {
logprint(ERROR, "xdpw: Could not find render node");
return NULL;
}

int fd = open(render_node, O_RDWR);
if (fd < 0) {
logprint(ERROR, "xdpw: Could not open render node %s",render_node);
close(fd);
return NULL;
}

gbm = gbm_create_device(fd);
close(fd);
return gbm;
}

void destroy_gbm_device(struct gbm_device *gbm) {
gbm_device_destroy(gbm);
}
50 changes: 50 additions & 0 deletions src/tmp/strlcpy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* $OpenBSD: strlcpy.c,v 1.13 2015/08/31 02:53:57 guenther Exp $ */

/*
* Copyright (c) 1998, 2015 Todd C. Miller <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include <sys/types.h>
#include <string.h>

/*
* Copy string src to buffer dst of size dsize. At most dsize-1
* chars will be copied. Always NUL terminates (unless dsize == 0).
* Returns strlen(src); if retval >= dsize, truncation occurred.
*/
size_t
strlcpy(char *dst, const char *src, size_t dsize)
{
const char *osrc = src;
size_t nleft = dsize;

/* Copy as many bytes as will fit. */
if (nleft != 0) {
while (--nleft != 0) {
if ((*dst++ = *src++) == '\0')
break;
}
}

/* Not enough room in dst, add NUL and traverse rest of src. */
if (nleft == 0) {
if (dsize != 0)
*dst = '\0'; /* NUL-terminate dst */
while (*src++)
;
}

return(src - osrc - 1); /* count does not include NUL */
}

0 comments on commit 7723fa1

Please sign in to comment.