Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
emersion committed May 11, 2018
1 parent 8999b00 commit d4648e8
Show file tree
Hide file tree
Showing 8 changed files with 554 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -50,3 +50,5 @@ modules.order
Module.symvers
Mkfile.old
dkms.conf

/build
83 changes: 83 additions & 0 deletions buffer.c
@@ -0,0 +1,83 @@
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>

#include "buffer.h"

static int create_pool_file(size_t size, char **name) {
static const char template[] = "grim-XXXXXX";
const char *path = getenv("XDG_RUNTIME_DIR");
if (path == NULL) {
return -1;
}

int ts = (path[strlen(path) - 1] == '/');

*name = malloc(
strlen(template) +
strlen(path) +
(ts ? 0 : 1) + 1);
sprintf(*name, "%s%s%s", path, ts ? "" : "/", template);

int fd = mkstemp(*name);
if (fd < 0) {
return -1;
}

if (ftruncate(fd, size) < 0) {
close(fd);
return -1;
}

return fd;
}

struct grim_buffer *create_buffer(struct wl_shm *shm, int32_t width,
int32_t height) {
const enum wl_shm_format wl_fmt = WL_SHM_FORMAT_ARGB8888;

uint32_t stride = 4 * width;
size_t size = stride * height;

char *name;
int fd = create_pool_file(size, &name);
if (fd == -1) {
return NULL;
}

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

struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, size);
struct wl_buffer *wl_buffer =
wl_shm_pool_create_buffer(pool, 0, width, height, stride, wl_fmt);
wl_shm_pool_destroy(pool);

close(fd);
fd = -1;
unlink(name);
free(name);

struct grim_buffer *buffer = calloc(1, sizeof(struct grim_buffer));
buffer->wl_buffer = wl_buffer;
buffer->data = data;
buffer->width = width;
buffer->height = height;
buffer->stride = stride;
buffer->size = size;
return buffer;
}

void destroy_buffer(struct grim_buffer *buffer) {
if (buffer == NULL) {
return;
}
munmap(buffer->data, buffer->size);
wl_buffer_destroy(buffer->wl_buffer);
free(buffer);
}
18 changes: 18 additions & 0 deletions include/buffer.h
@@ -0,0 +1,18 @@
#ifndef _BUFFER_H
#define _BUFFER_H

#include <wayland-client.h>

struct grim_buffer {
struct wl_buffer *wl_buffer;
void *data;
int32_t width, height, stride;
size_t size;
enum wl_shm_format format;
};

struct grim_buffer *create_buffer(struct wl_shm *shm, int32_t width,
int32_t height);
void destroy_buffer(struct grim_buffer *buffer);

#endif
42 changes: 42 additions & 0 deletions include/grim.h
@@ -0,0 +1,42 @@
#ifndef _GRIM_H
#define _GRIM_H

#include <wayland-client.h>

#include "orbital_screenshooter-client-protocol.h"
#include "xdg-output-unstable-v1-client-protocol.h"

struct grim_state {
struct wl_display *display;
struct wl_registry *registry;
struct wl_shm *shm;
struct zxdg_output_manager_v1 *xdg_output_manager;
struct orbital_screenshooter *orbital_screenshooter;
struct wl_list outputs;

size_t n_done;
};

struct grim_buffer;

struct grim_output {
struct grim_state *state;
struct wl_output *wl_output;
struct zxdg_output_v1 *xdg_output;
struct wl_list link;

int32_t x, y;
int32_t width, height;
enum wl_output_transform transform;
int32_t scale;

struct {
int32_t x, y;
int32_t width, height;
} logical;

struct grim_buffer *buffer;
struct orbital_screenshot *orbital_screenshot;
};

#endif

0 comments on commit d4648e8

Please sign in to comment.