Code style

This commit is contained in:
emersion 2017-10-07 19:22:55 +02:00
parent 63af97800f
commit c9909a45ab
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
1 changed files with 45 additions and 85 deletions

View File

@ -51,21 +51,10 @@ struct screenshooter_output {
struct wl_list link; struct wl_list link;
}; };
static void static void display_handle_geometry(void *data, struct wl_output *wl_output,
display_handle_geometry(void *data, int x, int y, int physical_width, int physical_height, int subpixel,
struct wl_output *wl_output, const char *make, const char *model, int transform) {
int x, struct screenshooter_output *output = wl_output_get_user_data(wl_output);
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) { if (wl_output == output->output) {
output->offset_x = x; output->offset_x = x;
@ -73,17 +62,9 @@ display_handle_geometry(void *data,
} }
} }
static void static void display_handle_mode(void *data, struct wl_output *wl_output,
display_handle_mode(void *data, uint32_t flags, int width, int height, int refresh) {
struct wl_output *wl_output, struct screenshooter_output *output = wl_output_get_user_data(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)) { if (wl_output == output->output && (flags & WL_OUTPUT_MODE_CURRENT)) {
output->width = width; output->width = width;
@ -96,9 +77,7 @@ static const struct wl_output_listener output_listener = {
display_handle_mode display_handle_mode
}; };
static void static void screenshot_done(void *data, struct orbital_screenshot *screenshot) {
screenshot_done(void *data, struct orbital_screenshot *screenshot)
{
buffer_copy_done = 1; buffer_copy_done = 1;
} }
@ -106,31 +85,27 @@ static const struct orbital_screenshot_listener screenshot_listener = {
screenshot_done screenshot_done
}; };
static void static void handle_global(void *data, struct wl_registry *registry,
handle_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) {
uint32_t name, const char *interface, uint32_t version)
{
static struct screenshooter_output *output; static struct screenshooter_output *output;
if (strcmp(interface, "wl_output") == 0) { if (strcmp(interface, "wl_output") == 0) {
output = calloc(1, sizeof *output); output = calloc(1, sizeof *output);
output->output = wl_registry_bind(registry, name, output->output = wl_registry_bind(registry, name, &wl_output_interface,
&wl_output_interface, 1); 1);
wl_list_insert(&output_list, &output->link); wl_list_insert(&output_list, &output->link);
wl_output_add_listener(output->output, &output_listener, output); wl_output_add_listener(output->output, &output_listener, output);
} else if (strcmp(interface, "wl_shm") == 0) { } else if (strcmp(interface, "wl_shm") == 0) {
shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
} else if (strcmp(interface, "orbital_screenshooter") == 0) { } else if (strcmp(interface, "orbital_screenshooter") == 0) {
screenshooter = wl_registry_bind(registry, name, screenshooter = wl_registry_bind(registry, name,
&orbital_screenshooter_interface, &orbital_screenshooter_interface, 1);
1);
} }
} }
static void static void handle_global_remove(void *data, struct wl_registry *registry,
handle_global_remove(void *data, struct wl_registry *registry, uint32_t name) uint32_t name) {
{ // Unimplemented
/* XXX: unimplemented */
} }
static const struct wl_registry_listener registry_listener = { static const struct wl_registry_listener registry_listener = {
@ -138,35 +113,28 @@ static const struct wl_registry_listener registry_listener = {
handle_global_remove handle_global_remove
}; };
static struct wl_buffer * static struct wl_buffer *create_shm_buffer(int width, int height,
create_shm_buffer(int width, int height, void **data_out) void **data_out) {
{ int stride = width * 4;
struct wl_shm_pool *pool; int size = stride * height;
struct wl_buffer *buffer;
int fd, size, stride;
void *data;
stride = width * 4; int fd = os_create_anonymous_file(size);
size = stride * height;
fd = os_create_anonymous_file(size);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "creating a buffer file for %d B failed: %m\n", fprintf(stderr, "creating a buffer file for %d B failed: %m\n", size);
size);
return NULL; return NULL;
} }
data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); void *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (data == MAP_FAILED) { if (data == MAP_FAILED) {
fprintf(stderr, "mmap failed: %m\n"); fprintf(stderr, "mmap failed: %m\n");
close(fd); close(fd);
return NULL; return NULL;
} }
pool = wl_shm_create_pool(shm, fd, size); struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, size);
close(fd); close(fd);
buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, struct wl_buffer *buffer = wl_shm_pool_create_buffer(pool, 0, width, height,
WL_SHM_FORMAT_XRGB8888); stride, WL_SHM_FORMAT_XRGB8888);
wl_shm_pool_destroy(pool); wl_shm_pool_destroy(pool);
*data_out = data; *data_out = data;
@ -184,26 +152,22 @@ static void argb_to_rgba(uint32_t *data, size_t height, size_t stride) {
} }
} }
static void static void write_image(const char *filename, int width, int height) {
write_image(const char *filename, int width, int height) int buffer_stride = width * 4;
{
int output_stride, buffer_stride, i;
void *data, *d, *s;
struct screenshooter_output *output, *next;
buffer_stride = width * 4; void *data = calloc(1, buffer_stride * height);
if (!data) {
data = calloc(1, buffer_stride * height);
if (!data)
return; return;
}
struct screenshooter_output *output, *next;
wl_list_for_each_safe(output, next, &output_list, link) { wl_list_for_each_safe(output, next, &output_list, link) {
output_stride = output->width * 4; int output_stride = output->width * 4;
s = output->data; void *s = output->data;
d = data + (output->offset_y - min_y) * buffer_stride + void *d = data + (output->offset_y - min_y) * buffer_stride +
(output->offset_x - min_x) * 4; (output->offset_x - min_x) * 4;
for (i = 0; i < output->height; i++) { for (int i = 0; i < output->height; i++) {
memcpy(d, s, output_stride); memcpy(d, s, output_stride);
d += buffer_stride; d += buffer_stride;
s += output_stride; s += output_stride;
@ -234,6 +198,7 @@ write_image(const char *filename, int width, int height)
close(fd[1]); close(fd[1]);
if (dup2(fd[0], 0) != 0) { if (dup2(fd[0], 0) != 0) {
fprintf(stderr, "cannot dup the pipe\n"); fprintf(stderr, "cannot dup the pipe\n");
exit(EXIT_FAILURE);
} }
close(fd[0]); close(fd[0]);
execlp("convert", "convert", "-depth", "8", "-size", size, "rgba:-", execlp("convert", "convert", "-depth", "8", "-size", size, "rgba:-",
@ -243,9 +208,7 @@ write_image(const char *filename, int width, int height)
} }
} }
static int static int set_buffer_size(int *width, int *height) {
set_buffer_size(int *width, int *height)
{
struct screenshooter_output *output; struct screenshooter_output *output;
min_x = min_y = INT_MAX; min_x = min_y = INT_MAX;
max_x = max_y = INT_MIN; max_x = max_y = INT_MIN;
@ -263,8 +226,9 @@ set_buffer_size(int *width, int *height)
max_y = MAX(max_y, output->offset_y + output->height); max_y = MAX(max_y, output->offset_y + output->height);
} }
if (max_x <= min_x || max_y <= min_y) if (max_x <= min_x || max_y <= min_y) {
return -1; return -1;
}
*width = max_x - min_x; *width = max_x - min_x;
*height = max_y - min_y; *height = max_y - min_y;
@ -272,21 +236,15 @@ set_buffer_size(int *width, int *height)
return 0; return 0;
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[]) {
{ struct wl_display * display = wl_display_connect(NULL);
struct wl_display *display;
struct wl_registry *registry;
struct screenshooter_output *output;
int width, height;
display = wl_display_connect(NULL);
if (display == NULL) { if (display == NULL) {
fprintf(stderr, "failed to create display: %m\n"); fprintf(stderr, "failed to create display: %m\n");
return -1; return -1;
} }
wl_list_init(&output_list); wl_list_init(&output_list);
registry = wl_display_get_registry(display); struct wl_registry *registry = wl_display_get_registry(display);
wl_registry_add_listener(registry, &registry_listener, NULL); wl_registry_add_listener(registry, &registry_listener, NULL);
wl_display_dispatch(display); wl_display_dispatch(display);
wl_display_roundtrip(display); wl_display_roundtrip(display);
@ -296,11 +254,13 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
int width, height;
if (set_buffer_size(&width, &height)) { if (set_buffer_size(&width, &height)) {
fprintf(stderr, "cannot set buffer size\n"); fprintf(stderr, "cannot set buffer size\n");
return -1; return -1;
} }
struct screenshooter_output *output;
wl_list_for_each(output, &output_list, link) { wl_list_for_each(output, &output_list, link) {
if (output->width == 0 || output->height == 0) { if (output->width == 0 || output->height == 0) {
continue; continue;