blob: 9d42599a8a7a691f3c8a5225eb7646cfb9c0fb2f [file] [log] [blame]
Philipp Brüschweiler585acb02012-08-15 17:12:00 +02001/*
2 * Copyright © 2012 Philipp Brüschweiler
3 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -07004 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020010 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -070011 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020022 */
23
U. Artie Eoff86c68b32014-01-15 13:02:28 -080024#include "config.h"
25
26#include <errno.h>
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020027#include <stdbool.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Pekka Paalanen93a6afd2014-09-23 22:08:44 -040031#include <time.h>
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020032
33#include <wayland-client.h>
34
Jon Cruz35b2eaa2015-06-15 15:37:08 -070035#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070036#include "shared/os-compatibility.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020037#include "presentation-time-client-protocol.h"
Pekka Paalanen93a6afd2014-09-23 22:08:44 -040038
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020039typedef void (*print_info_t)(void *info);
U. Artie Eoff86c68b32014-01-15 13:02:28 -080040typedef void (*destroy_info_t)(void *info);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020041
42struct global_info {
43 struct wl_list link;
44
45 uint32_t id;
46 uint32_t version;
47 char *interface;
48
49 print_info_t print;
U. Artie Eoff86c68b32014-01-15 13:02:28 -080050 destroy_info_t destroy;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020051};
52
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +020053struct output_mode {
54 struct wl_list link;
55
56 uint32_t flags;
57 int32_t width, height;
58 int32_t refresh;
59};
60
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020061struct output_info {
62 struct global_info global;
63
64 struct wl_output *output;
65
Jonny Lambba9dca02015-12-17 12:31:56 +000066 int32_t version;
67
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020068 struct {
69 int32_t x, y;
Jonny Lambba9dca02015-12-17 12:31:56 +000070 int32_t scale;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020071 int32_t physical_width, physical_height;
72 enum wl_output_subpixel subpixel;
73 enum wl_output_transform output_transform;
74 char *make;
75 char *model;
76 } geometry;
77
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +020078 struct wl_list modes;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020079};
80
81struct shm_format {
82 struct wl_list link;
83
84 uint32_t format;
85};
86
87struct shm_info {
88 struct global_info global;
89 struct wl_shm *shm;
90
91 struct wl_list formats;
92};
93
94struct seat_info {
95 struct global_info global;
96 struct wl_seat *seat;
Jonny Lamb06959082014-08-12 14:58:27 +020097 struct weston_info *info;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020098
99 uint32_t capabilities;
Rob Bradford14a76012013-05-31 18:09:52 +0100100 char *name;
Jonny Lamb06959082014-08-12 14:58:27 +0200101
102 int32_t repeat_rate;
103 int32_t repeat_delay;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200104};
105
Pekka Paalanen93a6afd2014-09-23 22:08:44 -0400106struct presentation_info {
107 struct global_info global;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200108 struct wp_presentation *presentation;
Pekka Paalanen93a6afd2014-09-23 22:08:44 -0400109
110 clockid_t clk_id;
111};
112
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200113struct weston_info {
114 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400115 struct wl_registry *registry;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200116
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200117 struct wl_list infos;
Philipp Brüschweilerbb0d4b92012-08-15 21:57:23 +0200118 bool roundtrip_needed;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200119};
120
Kristian Høgsberg06b16c22013-08-15 14:20:53 -0700121static void *
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800122fail_on_null(void *p)
Kristian Høgsberg06b16c22013-08-15 14:20:53 -0700123{
Kristian Høgsberg06b16c22013-08-15 14:20:53 -0700124 if (p == NULL) {
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800125 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
126 exit(EXIT_FAILURE);
Kristian Høgsberg06b16c22013-08-15 14:20:53 -0700127 }
128
129 return p;
130}
131
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800132static void *
133xmalloc(size_t s)
134{
135 return fail_on_null(malloc(s));
136}
137
138static void *
139xzalloc(size_t s)
140{
141 return fail_on_null(calloc(1, s));
142}
143
144static char *
145xstrdup(const char *s)
146{
147 return fail_on_null(strdup(s));
148}
149
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200150static void
151print_global_info(void *data)
152{
153 struct global_info *global = data;
154
155 printf("interface: '%s', version: %u, name: %u\n",
156 global->interface, global->version, global->id);
157}
158
159static void
160init_global_info(struct weston_info *info,
161 struct global_info *global, uint32_t id,
162 const char *interface, uint32_t version)
163{
164 global->id = id;
165 global->version = version;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800166 global->interface = xstrdup(interface);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200167
168 wl_list_insert(info->infos.prev, &global->link);
169}
170
171static void
172print_output_info(void *data)
173{
174 struct output_info *output = data;
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +0200175 struct output_mode *mode;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200176 const char *subpixel_orientation;
177 const char *transform;
178
179 print_global_info(data);
180
181 switch (output->geometry.subpixel) {
182 case WL_OUTPUT_SUBPIXEL_UNKNOWN:
183 subpixel_orientation = "unknown";
184 break;
185 case WL_OUTPUT_SUBPIXEL_NONE:
186 subpixel_orientation = "none";
187 break;
188 case WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB:
189 subpixel_orientation = "horizontal rgb";
190 break;
191 case WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR:
192 subpixel_orientation = "horizontal bgr";
193 break;
194 case WL_OUTPUT_SUBPIXEL_VERTICAL_RGB:
195 subpixel_orientation = "vertical rgb";
196 break;
197 case WL_OUTPUT_SUBPIXEL_VERTICAL_BGR:
198 subpixel_orientation = "vertical bgr";
199 break;
200 default:
201 fprintf(stderr, "unknown subpixel orientation %u\n",
202 output->geometry.subpixel);
203 subpixel_orientation = "unexpected value";
204 break;
205 }
206
207 switch (output->geometry.output_transform) {
208 case WL_OUTPUT_TRANSFORM_NORMAL:
209 transform = "normal";
210 break;
211 case WL_OUTPUT_TRANSFORM_90:
212 transform = "90°";
213 break;
214 case WL_OUTPUT_TRANSFORM_180:
215 transform = "180°";
216 break;
217 case WL_OUTPUT_TRANSFORM_270:
218 transform = "270°";
219 break;
220 case WL_OUTPUT_TRANSFORM_FLIPPED:
221 transform = "flipped";
222 break;
223 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
224 transform = "flipped 90°";
225 break;
226 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
227 transform = "flipped 180°";
228 break;
229 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
230 transform = "flipped 270°";
231 break;
232 default:
233 fprintf(stderr, "unknown output transform %u\n",
234 output->geometry.output_transform);
235 transform = "unexpected value";
236 break;
237 }
238
Jonny Lambba9dca02015-12-17 12:31:56 +0000239 printf("\tx: %d, y: %d,",
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +0200240 output->geometry.x, output->geometry.y);
Jonny Lambba9dca02015-12-17 12:31:56 +0000241 if (output->version >= 2)
242 printf(" scale: %d,", output->geometry.scale);
243 printf("\n");
244
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +0200245 printf("\tphysical_width: %d mm, physical_height: %d mm,\n",
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200246 output->geometry.physical_width,
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +0200247 output->geometry.physical_height);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200248 printf("\tmake: '%s', model: '%s',\n",
249 output->geometry.make, output->geometry.model);
U. Artie Eoffcb0e3572014-04-18 09:30:07 -0700250 printf("\tsubpixel_orientation: %s, output_transform: %s,\n",
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200251 subpixel_orientation, transform);
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +0200252
253 wl_list_for_each(mode, &output->modes, link) {
254 printf("\tmode:\n");
255
256 printf("\t\twidth: %d px, height: %d px, refresh: %.f Hz,\n",
257 mode->width, mode->height,
258 (float) mode->refresh / 1000);
259
260 printf("\t\tflags:");
261 if (mode->flags & WL_OUTPUT_MODE_CURRENT)
262 printf(" current");
263 if (mode->flags & WL_OUTPUT_MODE_PREFERRED)
264 printf(" preferred");
265 printf("\n");
266 }
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200267}
268
269static void
270print_shm_info(void *data)
271{
272 struct shm_info *shm = data;
273 struct shm_format *format;
274
275 print_global_info(data);
276
277 printf("\tformats:");
278
279 wl_list_for_each(format, &shm->formats, link)
Kristian Høgsberg8b66ebd2013-11-20 13:54:00 -0800280 switch (format->format) {
281 case WL_SHM_FORMAT_ARGB8888:
282 printf(" ARGB8888");
283 break;
284 case WL_SHM_FORMAT_XRGB8888:
285 printf(" XRGB8888");
286 break;
287 case WL_SHM_FORMAT_RGB565:
288 printf(" RGB565");
289 break;
290 default:
291 printf(" unknown(%08x)", format->format);
292 break;
293 }
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200294
295 printf("\n");
296}
297
298static void
299print_seat_info(void *data)
300{
301 struct seat_info *seat = data;
302
303 print_global_info(data);
304
Rob Bradford14a76012013-05-31 18:09:52 +0100305 printf("\tname: %s\n", seat->name);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200306 printf("\tcapabilities:");
307
308 if (seat->capabilities & WL_SEAT_CAPABILITY_POINTER)
309 printf(" pointer");
310 if (seat->capabilities & WL_SEAT_CAPABILITY_KEYBOARD)
311 printf(" keyboard");
312 if (seat->capabilities & WL_SEAT_CAPABILITY_TOUCH)
313 printf(" touch");
314
315 printf("\n");
Jonny Lamb06959082014-08-12 14:58:27 +0200316
317 if (seat->repeat_rate > 0)
318 printf("\tkeyboard repeat rate: %d\n", seat->repeat_rate);
319 if (seat->repeat_delay > 0)
320 printf("\tkeyboard repeat delay: %d\n", seat->repeat_delay);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200321}
322
323static void
Jonny Lamb06959082014-08-12 14:58:27 +0200324keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
325 uint32_t format, int fd, uint32_t size)
326{
327}
328
329static void
330keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
331 uint32_t serial, struct wl_surface *surface,
332 struct wl_array *keys)
333{
334}
335
336static void
337keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
338 uint32_t serial, struct wl_surface *surface)
339{
340}
341
342static void
343keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
344 uint32_t serial, uint32_t time, uint32_t key,
345 uint32_t state)
346{
347}
348
349static void
350keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
351 uint32_t serial, uint32_t mods_depressed,
352 uint32_t mods_latched, uint32_t mods_locked,
353 uint32_t group)
354{
355}
356
357static void
358keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
359 int32_t rate, int32_t delay)
360{
361 struct seat_info *seat = data;
362
363 seat->repeat_rate = rate;
364 seat->repeat_delay = delay;
365}
366
367static const struct wl_keyboard_listener keyboard_listener = {
368 keyboard_handle_keymap,
369 keyboard_handle_enter,
370 keyboard_handle_leave,
371 keyboard_handle_key,
372 keyboard_handle_modifiers,
373 keyboard_handle_repeat_info,
374};
375
376static void
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200377seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
378 enum wl_seat_capability caps)
379{
380 struct seat_info *seat = data;
Jonny Lamb06959082014-08-12 14:58:27 +0200381
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200382 seat->capabilities = caps;
Jonny Lamb06959082014-08-12 14:58:27 +0200383
384 /* we want listen for repeat_info from wl_keyboard, but only
385 * do so if the seat info is >= 4 and if we actually have a
386 * keyboard */
387 if (seat->global.version < 4)
388 return;
389
390 if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
391 struct wl_keyboard *keyboard;
392
393 keyboard = wl_seat_get_keyboard(seat->seat);
394 wl_keyboard_add_listener(keyboard, &keyboard_listener,
395 seat);
396
397 seat->info->roundtrip_needed = true;
398 }
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200399}
400
Rob Bradford14a76012013-05-31 18:09:52 +0100401static void
402seat_handle_name(void *data, struct wl_seat *wl_seat,
403 const char *name)
404{
405 struct seat_info *seat = data;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800406 seat->name = xstrdup(name);
Rob Bradford14a76012013-05-31 18:09:52 +0100407}
408
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200409static const struct wl_seat_listener seat_listener = {
410 seat_handle_capabilities,
Rob Bradford14a76012013-05-31 18:09:52 +0100411 seat_handle_name,
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200412};
413
414static void
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800415destroy_seat_info(void *data)
416{
417 struct seat_info *seat = data;
418
419 wl_seat_destroy(seat->seat);
420
421 if (seat->name != NULL)
422 free(seat->name);
423}
424
425static void
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200426add_seat_info(struct weston_info *info, uint32_t id, uint32_t version)
427{
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800428 struct seat_info *seat = xzalloc(sizeof *seat);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200429
Jonny Lamb06959082014-08-12 14:58:27 +0200430 /* required to set roundtrip_needed to true in capabilities
431 * handler */
432 seat->info = info;
433
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200434 init_global_info(info, &seat->global, id, "wl_seat", version);
435 seat->global.print = print_seat_info;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800436 seat->global.destroy = destroy_seat_info;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200437
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400438 seat->seat = wl_registry_bind(info->registry,
Jonny Lamb06959082014-08-12 14:58:27 +0200439 id, &wl_seat_interface, MIN(version, 4));
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200440 wl_seat_add_listener(seat->seat, &seat_listener, seat);
Philipp Brüschweilerbb0d4b92012-08-15 21:57:23 +0200441
Jonny Lamb06959082014-08-12 14:58:27 +0200442 seat->repeat_rate = seat->repeat_delay = -1;
443
Philipp Brüschweilerbb0d4b92012-08-15 21:57:23 +0200444 info->roundtrip_needed = true;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200445}
446
447static void
448shm_handle_format(void *data, struct wl_shm *wl_shm, uint32_t format)
449{
450 struct shm_info *shm = data;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800451 struct shm_format *shm_format = xzalloc(sizeof *shm_format);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200452
453 wl_list_insert(&shm->formats, &shm_format->link);
454 shm_format->format = format;
455}
456
457static const struct wl_shm_listener shm_listener = {
458 shm_handle_format,
459};
460
461static void
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800462destroy_shm_info(void *data)
463{
464 struct shm_info *shm = data;
465 struct shm_format *format, *tmp;
466
467 wl_list_for_each_safe(format, tmp, &shm->formats, link) {
468 wl_list_remove(&format->link);
469 free(format);
470 }
471
472 wl_shm_destroy(shm->shm);
473}
474
475static void
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200476add_shm_info(struct weston_info *info, uint32_t id, uint32_t version)
477{
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800478 struct shm_info *shm = xzalloc(sizeof *shm);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200479
480 init_global_info(info, &shm->global, id, "wl_shm", version);
481 shm->global.print = print_shm_info;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800482 shm->global.destroy = destroy_shm_info;
483
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200484 wl_list_init(&shm->formats);
485
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400486 shm->shm = wl_registry_bind(info->registry,
487 id, &wl_shm_interface, 1);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200488 wl_shm_add_listener(shm->shm, &shm_listener, shm);
Philipp Brüschweilerbb0d4b92012-08-15 21:57:23 +0200489
490 info->roundtrip_needed = true;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200491}
492
493static void
494output_handle_geometry(void *data, struct wl_output *wl_output,
495 int32_t x, int32_t y,
496 int32_t physical_width, int32_t physical_height,
497 int32_t subpixel,
498 const char *make, const char *model,
499 int32_t output_transform)
500{
501 struct output_info *output = data;
502
503 output->geometry.x = x;
504 output->geometry.y = y;
505 output->geometry.physical_width = physical_width;
506 output->geometry.physical_height = physical_height;
507 output->geometry.subpixel = subpixel;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800508 output->geometry.make = xstrdup(make);
509 output->geometry.model = xstrdup(model);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200510 output->geometry.output_transform = output_transform;
511}
512
513static void
514output_handle_mode(void *data, struct wl_output *wl_output,
515 uint32_t flags, int32_t width, int32_t height,
516 int32_t refresh)
517{
518 struct output_info *output = data;
Kristian Høgsberg06b16c22013-08-15 14:20:53 -0700519 struct output_mode *mode = xmalloc(sizeof *mode);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200520
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +0200521 mode->flags = flags;
522 mode->width = width;
523 mode->height = height;
524 mode->refresh = refresh;
525
526 wl_list_insert(output->modes.prev, &mode->link);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200527}
528
Jonny Lambba9dca02015-12-17 12:31:56 +0000529static void
530output_handle_done(void *data, struct wl_output *wl_output)
531{
532 /* don't bother waiting for this; there's no good reason a
533 * compositor will wait more than one roundtrip before sending
534 * these initial events. */
535}
536
537static void
538output_handle_scale(void *data, struct wl_output *wl_output,
539 int32_t scale)
540{
541 struct output_info *output = data;
542
543 output->geometry.scale = scale;
544}
545
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200546static const struct wl_output_listener output_listener = {
547 output_handle_geometry,
548 output_handle_mode,
Jonny Lambba9dca02015-12-17 12:31:56 +0000549 output_handle_done,
550 output_handle_scale,
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200551};
552
553static void
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800554destroy_output_info(void *data)
555{
556 struct output_info *output = data;
557 struct output_mode *mode, *tmp;
558
559 wl_output_destroy(output->output);
560
561 if (output->geometry.make != NULL)
562 free(output->geometry.make);
563 if (output->geometry.model != NULL)
564 free(output->geometry.model);
565
566 wl_list_for_each_safe(mode, tmp, &output->modes, link) {
567 wl_list_remove(&mode->link);
568 free(mode);
569 }
570}
571
572static void
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200573add_output_info(struct weston_info *info, uint32_t id, uint32_t version)
574{
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800575 struct output_info *output = xzalloc(sizeof *output);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200576
577 init_global_info(info, &output->global, id, "wl_output", version);
578 output->global.print = print_output_info;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800579 output->global.destroy = destroy_output_info;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200580
Jonny Lambba9dca02015-12-17 12:31:56 +0000581 output->version = MIN(version, 2);
582 output->geometry.scale = 1;
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +0200583 wl_list_init(&output->modes);
584
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400585 output->output = wl_registry_bind(info->registry, id,
Jonny Lambba9dca02015-12-17 12:31:56 +0000586 &wl_output_interface, output->version);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200587 wl_output_add_listener(output->output, &output_listener,
588 output);
Philipp Brüschweilerbb0d4b92012-08-15 21:57:23 +0200589
590 info->roundtrip_needed = true;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200591}
592
593static void
Pekka Paalanen93a6afd2014-09-23 22:08:44 -0400594destroy_presentation_info(void *info)
595{
596 struct presentation_info *prinfo = info;
597
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200598 wp_presentation_destroy(prinfo->presentation);
Pekka Paalanen93a6afd2014-09-23 22:08:44 -0400599}
600
601static const char *
602clock_name(clockid_t clk_id)
603{
604 static const char *names[] = {
605 [CLOCK_REALTIME] = "CLOCK_REALTIME",
606 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
607 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
608 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
609 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
Derek Foreman32838c92015-06-29 13:20:34 -0500610#ifdef CLOCK_BOOTTIME
Pekka Paalanen93a6afd2014-09-23 22:08:44 -0400611 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
Derek Foreman32838c92015-06-29 13:20:34 -0500612#endif
Pekka Paalanen93a6afd2014-09-23 22:08:44 -0400613 };
614
615 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
616 return "unknown";
617
618 return names[clk_id];
619}
620
621static void
622print_presentation_info(void *info)
623{
624 struct presentation_info *prinfo = info;
625
626 print_global_info(info);
627
628 printf("\tpresentation clock id: %d (%s)\n",
629 prinfo->clk_id, clock_name(prinfo->clk_id));
630}
631
632static void
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200633presentation_handle_clock_id(void *data, struct wp_presentation *presentation,
Pekka Paalanen93a6afd2014-09-23 22:08:44 -0400634 uint32_t clk_id)
635{
636 struct presentation_info *prinfo = data;
637
638 prinfo->clk_id = clk_id;
639}
640
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200641static const struct wp_presentation_listener presentation_listener = {
Pekka Paalanen93a6afd2014-09-23 22:08:44 -0400642 presentation_handle_clock_id
643};
644
645static void
646add_presentation_info(struct weston_info *info, uint32_t id, uint32_t version)
647{
648 struct presentation_info *prinfo = xzalloc(sizeof *prinfo);
649
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200650 init_global_info(info, &prinfo->global, id,
651 wp_presentation_interface.name, version);
Pekka Paalanen93a6afd2014-09-23 22:08:44 -0400652 prinfo->global.print = print_presentation_info;
653 prinfo->global.destroy = destroy_presentation_info;
654
655 prinfo->clk_id = -1;
656 prinfo->presentation = wl_registry_bind(info->registry, id,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200657 &wp_presentation_interface, 1);
658 wp_presentation_add_listener(prinfo->presentation,
659 &presentation_listener, prinfo);
Pekka Paalanen93a6afd2014-09-23 22:08:44 -0400660
661 info->roundtrip_needed = true;
662}
663
664static void
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800665destroy_global_info(void *data)
666{
667}
668
669static void
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200670add_global_info(struct weston_info *info, uint32_t id,
671 const char *interface, uint32_t version)
672{
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800673 struct global_info *global = xzalloc(sizeof *global);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200674
675 init_global_info(info, global, id, interface, version);
676 global->print = print_global_info;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800677 global->destroy = destroy_global_info;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200678}
679
680static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400681global_handler(void *data, struct wl_registry *registry, uint32_t id,
682 const char *interface, uint32_t version)
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200683{
684 struct weston_info *info = data;
685
686 if (!strcmp(interface, "wl_seat"))
687 add_seat_info(info, id, version);
688 else if (!strcmp(interface, "wl_shm"))
689 add_shm_info(info, id, version);
690 else if (!strcmp(interface, "wl_output"))
691 add_output_info(info, id, version);
Pekka Paalanen93a6afd2014-09-23 22:08:44 -0400692 else if (!strcmp(interface, "presentation"))
693 add_presentation_info(info, id, version);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200694 else
695 add_global_info(info, id, interface, version);
696}
697
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200698static void
699global_remove_handler(void *data, struct wl_registry *registry, uint32_t name)
700{
701}
702
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400703static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200704 global_handler,
705 global_remove_handler
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400706};
707
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200708static void
709print_infos(struct wl_list *infos)
710{
711 struct global_info *info;
712
713 wl_list_for_each(info, infos, link)
714 info->print(info);
715}
716
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800717static void
718destroy_info(void *data)
719{
720 struct global_info *global = data;
721
722 global->destroy(data);
723 wl_list_remove(&global->link);
724 free(global->interface);
725 free(data);
726}
727
728static void
729destroy_infos(struct wl_list *infos)
730{
731 struct global_info *info, *tmp;
732 wl_list_for_each_safe(info, tmp, infos, link)
733 destroy_info(info);
734}
735
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200736int
737main(int argc, char **argv)
738{
739 struct weston_info info;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200740
741 info.display = wl_display_connect(NULL);
742 if (!info.display) {
743 fprintf(stderr, "failed to create display: %m\n");
744 return -1;
745 }
746
747 wl_list_init(&info.infos);
748
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400749 info.registry = wl_display_get_registry(info.display);
750 wl_registry_add_listener(info.registry, &registry_listener, &info);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200751
Philipp Brüschweilerbb0d4b92012-08-15 21:57:23 +0200752 do {
753 info.roundtrip_needed = false;
754 wl_display_roundtrip(info.display);
755 } while (info.roundtrip_needed);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200756
757 print_infos(&info.infos);
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800758 destroy_infos(&info.infos);
759
760 wl_registry_destroy(info.registry);
761 wl_display_disconnect(info.display);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200762
763 return 0;
764}