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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
/*
* Copyright © 2008 Kristian Høgsberg
* Copyright © 2012 Intel Corporation
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting documentation, and
* that the name of the copyright holders not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. The copyright holders make no representations
* about the suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, 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 "config.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <cairo.h>
#include <linux/input.h>
#include <wayland-client.h>
#include "window.h"
struct transformed {
struct display *display;
struct window *window;
struct widget *widget;
int width, height;
int fullscreen;
};
static void
draw_stuff(cairo_t *cr, int width, int height)
{
cairo_matrix_t m;
cairo_get_matrix (cr, &m);
cairo_translate(cr, width / 2, height / 2);
cairo_scale(cr, width / 2, height / 2);
cairo_set_source_rgba(cr, 0, 0, 0.3, 1.0);
cairo_set_source_rgba(cr, 0, 0, 0, 1.0);
cairo_rectangle(cr, -1, -1, 2, 2);
cairo_fill(cr);
cairo_set_source_rgb(cr, 1, 0, 0);
cairo_move_to(cr, 0, 0);
cairo_line_to(cr, 0, -1);
cairo_save(cr);
cairo_set_matrix(cr, &m);
cairo_set_line_width(cr, 2.0);
cairo_stroke(cr);
cairo_restore(cr);
cairo_set_source_rgb(cr, 0, 1, 0);
cairo_move_to(cr, 0, 0);
cairo_line_to(cr, 1, 0);
cairo_save(cr);
cairo_set_matrix(cr, &m);
cairo_set_line_width(cr, 2.0);
cairo_stroke(cr);
cairo_restore(cr);
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_move_to(cr, 0, 0);
cairo_line_to(cr, 0, 1);
cairo_move_to(cr, 0, 0);
cairo_line_to(cr, -1, 0);
cairo_save(cr);
cairo_set_matrix(cr, &m);
cairo_set_line_width(cr, 2.0);
cairo_stroke(cr);
cairo_restore(cr);
cairo_destroy(cr);
}
static void
fullscreen_handler(struct window *window, void *data)
{
struct transformed *transformed = data;
transformed->fullscreen ^= 1;
window_set_fullscreen(window, transformed->fullscreen);
}
static void
redraw_handler(struct widget *widget, void *data)
{
struct transformed *transformed = data;
struct rectangle allocation;
cairo_surface_t *surface;
cairo_t *cr;
surface = window_get_surface(transformed->window);
if (surface == NULL ||
cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
fprintf(stderr, "failed to create cairo egl surface\n");
return;
}
widget_get_allocation(transformed->widget, &allocation);
cr = widget_cairo_create(widget);
draw_stuff(cr, allocation.width, allocation.height);
cairo_surface_destroy(surface);
}
static void
output_handler(struct window *window, struct output *output, int enter,
void *data)
{
if (!enter)
return;
window_set_buffer_transform(window, output_get_transform(output));
window_set_buffer_scale(window, output_get_scale(output));
window_schedule_redraw(window);
}
static void
key_handler(struct window *window, struct input *input, uint32_t time,
uint32_t key, uint32_t sym, enum wl_keyboard_key_state state,
void *data)
{
int transform, scale;
if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
return;
transform = window_get_buffer_transform (window);
scale = window_get_buffer_scale (window);
switch (sym) {
case XKB_KEY_Left:
if (transform == 0)
transform = 3;
else if (transform == 4)
transform = 7;
else
transform--;
break;
case XKB_KEY_Right:
if (transform == 3)
transform = 0;
else if (transform == 7)
transform = 4;
else
transform++;
break;
case XKB_KEY_space:
if (transform >= 4)
transform -= 4;
else
transform += 4;
break;
case XKB_KEY_z:
if (scale == 1)
scale = 2;
else
scale = 1;
break;
}
printf ("setting buffer transform to %d\n", transform);
printf ("setting buffer scale to %d\n", scale);
window_set_buffer_transform(window, transform);
window_set_buffer_scale(window, scale);
window_schedule_redraw(window);
}
static void
button_handler(struct widget *widget,
struct input *input, uint32_t time,
uint32_t button, enum wl_pointer_button_state state, void *data)
{
struct transformed *transformed = data;
switch (button) {
case BTN_LEFT:
if (state == WL_POINTER_BUTTON_STATE_PRESSED)
window_move(transformed->window, input,
display_get_serial(transformed->display));
break;
case BTN_MIDDLE:
if (state == WL_POINTER_BUTTON_STATE_PRESSED)
widget_schedule_redraw(widget);
break;
case BTN_RIGHT:
if (state == WL_POINTER_BUTTON_STATE_PRESSED)
window_show_frame_menu(transformed->window, input, time);
break;
}
}
static void
touch_handler(struct widget *widget, struct input *input,
uint32_t serial, uint32_t time, int32_t id,
float x, float y, void *data)
{
struct transformed *transformed = data;
window_move(transformed->window, input, display_get_serial(transformed->display));
}
static void
usage(int error_code)
{
fprintf(stderr, "Usage: transformed [OPTIONS]\n\n"
" -d\t\tUse \"driver\" fullscreen method\n"
" -w <width>\tSet window width to <width>\n"
" -h <height>\tSet window height to <height>\n"
" --help\tShow this help text\n\n");
exit(error_code);
}
int main(int argc, char *argv[])
{
struct transformed transformed;
struct display *d;
int i;
transformed.width = 500;
transformed.height = 250;
transformed.fullscreen = 0;
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-w") == 0) {
if (++i >= argc)
usage(EXIT_FAILURE);
transformed.width = atol(argv[i]);
} else if (strcmp(argv[i], "-h") == 0) {
if (++i >= argc)
usage(EXIT_FAILURE);
transformed.height = atol(argv[i]);
} else if (strcmp(argv[i], "--help") == 0)
usage(EXIT_SUCCESS);
else
usage(EXIT_FAILURE);
}
d = display_create(&argc, argv);
if (d == NULL) {
fprintf(stderr, "failed to create display: %m\n");
return -1;
}
transformed.display = d;
transformed.window = window_create(d);
transformed.widget =
window_add_widget(transformed.window, &transformed);
window_set_title(transformed.window, "Transformed");
widget_set_transparent(transformed.widget, 0);
widget_set_default_cursor(transformed.widget, CURSOR_BLANK);
widget_set_redraw_handler(transformed.widget, redraw_handler);
widget_set_button_handler(transformed.widget, button_handler);
widget_set_touch_down_handler(transformed.widget, touch_handler);
window_set_key_handler(transformed.window, key_handler);
window_set_fullscreen_handler(transformed.window, fullscreen_handler);
window_set_output_handler(transformed.window, output_handler);
window_set_user_data(transformed.window, &transformed);
window_schedule_resize(transformed.window,
transformed.width, transformed.height);
display_run(d);
return 0;
}
|