-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreenshot_draw.c
More file actions
137 lines (117 loc) · 4.65 KB
/
screenshot_draw.c
File metadata and controls
137 lines (117 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/***********************************************************************************
* This file is part of samurai-render
* https://github.com/Samudevv/samurai-render
***********************************************************************************
* Copyright (c) 2026 Kassandra Pucher
*
* This software is provided ‘as-is’, without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
************************************************************************************/
#include <cairo/cairo.h>
#include <linux/input-event-codes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <samure/backends/cairo.h>
#include <samure/context.h>
struct screenshot_draw_data {
double x;
double y;
int pressed;
};
static void event_callback(struct samure_context *ctx, struct samure_event *e,
void *data) {
struct screenshot_draw_data *d = (struct screenshot_draw_data *)data;
switch (e->type) {
case SAMURE_EVENT_POINTER_BUTTON:
if (e->button == BTN_LEFT) {
d->pressed = e->state == WL_POINTER_BUTTON_STATE_PRESSED;
}
break;
case SAMURE_EVENT_POINTER_MOTION:
d->x = e->x + e->seat->pointer_focus.output->geo.x;
d->y = e->y + e->seat->pointer_focus.output->geo.y;
if (d->pressed) {
samure_context_set_render_state(ctx, SAMURE_RENDER_STATE_ONCE);
}
break;
case SAMURE_EVENT_KEYBOARD_KEY:
if (e->button == KEY_ESC && e->state == WL_KEYBOARD_KEY_STATE_RELEASED) {
ctx->running = 0;
}
break;
}
}
static void render_callback(struct samure_context *ctx,
struct samure_layer_surface *sfc,
struct samure_rect output_geo, void *data) {
struct samure_cairo_surface *c =
(struct samure_cairo_surface *)sfc->backend_data;
struct screenshot_draw_data *d = (struct screenshot_draw_data *)data;
cairo_t *cairo = c->cairo;
cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
cairo_set_source_rgba(cairo, 1.0, 0.0, 0.0, 1.0);
if (d->pressed) {
cairo_arc(cairo, RENDER_X(d->x), RENDER_Y(d->y), RENDER_SCALE(10.0), 0.0,
M_PI * 2.0);
cairo_fill(cairo);
}
}
int main(int args, char *argv[]) {
struct screenshot_draw_data d = {0};
struct samure_context_config context_config =
samure_create_context_config(event_callback, render_callback, NULL, &d);
context_config.backend = SAMURE_BACKEND_CAIRO;
context_config.pointer_interaction = 1;
context_config.keyboard_interaction = 1;
SAMURE_RESULT(context) ctx_rs = samure_create_context(&context_config);
if (ctx_rs.error != SAMURE_ERROR_NONE) {
samure_perror("failed to create context", ctx_rs.error);
return 1;
}
struct samure_context *ctx = ctx_rs.result;
puts("Successfully initialized samurai-render context");
struct samure_layer_surface **bgs = NULL;
bgs = malloc(ctx->num_outputs * sizeof(struct samure_layer_surface *));
for (size_t i = 0; i < ctx->num_outputs; i++) {
bgs[i] = SAMURE_UNWRAP(
layer_surface,
samure_create_layer_surface(ctx, ctx->outputs[i], SAMURE_LAYER_TOP,
SAMURE_LAYER_SURFACE_ANCHOR_FILL, 0, 0, 0));
SAMURE_RESULT(shared_buffer) screenshot_rs = samure_output_screenshot(ctx, ctx->outputs[i], 0);
if (screenshot_rs.error != SAMURE_ERROR_NONE) {
samure_perror("failed to take screenshot", screenshot_rs.error);
} else {
struct samure_shared_buffer *screenshot = screenshot_rs.result;
samure_layer_surface_draw_buffer(bgs[i], screenshot);
samure_destroy_shared_buffer(screenshot);
}
}
samure_context_set_render_state(ctx, SAMURE_RENDER_STATE_ONCE);
samure_context_run(ctx);
if (bgs) {
for (size_t i = 0; i < ctx->num_outputs; i++) {
samure_destroy_layer_surface(ctx, bgs[i]);
}
free(bgs);
}
samure_destroy_context(ctx);
puts("Successfully destroyed samurai-render context");
return EXIT_SUCCESS;
}