Repository navigation
Expand file tree
/
Copy pathslurpy.c
More file actions
156 lines (140 loc) · 4.79 KB
/
Copy pathslurpy.c
File metadata and controls
156 lines (140 loc) · 4.79 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/***********************************************************************************
* 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 <linux/input-event-codes.h>
#include <stdio.h>
#include <samure/backends/cairo.h>
#include <samure/context.h>
enum slurpy_state {
STATE_NONE,
STATE_CHANGE,
};
struct slurpy_point {
double x;
double y;
};
struct slurpy_data {
struct slurpy_point start;
struct slurpy_point end;
enum slurpy_state state;
};
static void on_event(struct samure_context *ctx, struct samure_event *e,
void *user_data) {
struct slurpy_data *d = (struct slurpy_data *)user_data;
switch (e->type) {
case SAMURE_EVENT_POINTER_BUTTON:
if (e->button == BTN_LEFT) {
if (e->state == WL_POINTER_BUTTON_STATE_PRESSED) {
d->end.x = d->start.x;
d->end.y = d->start.y;
d->state = STATE_CHANGE;
} else {
ctx->running = 0;
}
}
break;
case SAMURE_EVENT_POINTER_MOTION:
switch (d->state) {
case STATE_NONE:
d->start.x = e->x + e->seat->pointer_focus.output->geo.x;
d->start.y = e->y + e->seat->pointer_focus.output->geo.y;
break;
case STATE_CHANGE:
d->end.x = e->x + e->seat->pointer_focus.output->geo.x;
d->end.y = e->y + e->seat->pointer_focus.output->geo.y;
samure_context_set_render_state(ctx, SAMURE_RENDER_STATE_ONCE);
break;
}
break;
case SAMURE_EVENT_KEYBOARD_KEY:
if ((e->button == KEY_ESC || e->button == KEY_ENTER) &&
e->state == WL_KEYBOARD_KEY_STATE_RELEASED) {
ctx->running = 0;
}
break;
}
}
static void on_render(struct samure_context *ctx,
struct samure_layer_surface *sfc,
struct samure_rect output_geo, void *user_data) {
struct slurpy_data *d = (struct slurpy_data *)user_data;
struct samure_cairo_surface *c =
(struct samure_cairo_surface *)sfc->backend_data;
cairo_t *cr = c->cairo;
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, (double)(0x40) / 255.0);
cairo_paint(cr);
if (d->state == STATE_CHANGE) {
const double width = RENDER_SCALE(d->end.x - d->start.x);
const double height = RENDER_SCALE(d->end.y - d->start.y);
cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
cairo_rectangle(cr, RENDER_X(d->start.x), RENDER_Y(d->start.y), width,
height);
cairo_fill(cr);
cairo_rectangle(cr, RENDER_X(d->start.x), RENDER_Y(d->start.y), width,
height);
cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
cairo_stroke(cr);
}
}
int main(void) {
struct slurpy_data d = {0};
struct samure_context_config cfg =
samure_create_context_config(on_event, on_render, NULL, &d);
cfg.backend = SAMURE_BACKEND_CAIRO;
cfg.pointer_interaction = 1;
cfg.keyboard_interaction = 1;
struct samure_context *ctx =
SAMURE_UNWRAP(context, samure_create_context(&cfg));
samure_context_set_pointer_shape(ctx,
WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_CROSSHAIR);
samure_context_set_render_state(ctx, SAMURE_RENDER_STATE_ONCE);
samure_context_run(ctx);
struct slurpy_point start;
struct slurpy_point end;
if (d.start.x > d.end.x) {
start.x = d.end.x;
end.x = d.start.x;
} else {
start.x = d.start.x;
end.x = d.end.x;
}
if (d.start.y > d.end.y) {
start.y = d.end.y;
end.y = d.start.y;
} else {
start.y = d.start.y;
end.y = d.end.y;
}
const struct slurpy_point dims = {
.x = end.x - start.x,
.y = end.y - start.y,
};
if (dims.x == 0.0 && dims.y == 0.0) {
return 1;
}
printf("%.0f,%.0f %.0fx%.0f\n", start.x, start.y, dims.x, dims.y);
return 0;
}