blob: f22781c2e33d01411bd71fbaaebef230f51cfd74 [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 Paalanen93a6afd2014-09-23 22:08:44 -040037#include "presentation_timing-client-protocol.h"
38
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
66 struct {
67 int32_t x, y;
68 int32_t physical_width, physical_height;
69 enum wl_output_subpixel subpixel;
70 enum wl_output_transform output_transform;
71 char *make;
72 char *model;
73 } geometry;
74
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +020075 struct wl_list modes;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020076};
77
78struct shm_format {
79 struct wl_list link;
80
81 uint32_t format;
82};
83
84struct shm_info {
85 struct global_info global;
86 struct wl_shm *shm;
87
88 struct wl_list formats;
89};
90
91struct seat_info {
92 struct global_info global;
93 struct wl_seat *seat;
Jonny Lamb06959082014-08-12 14:58:27 +020094 struct weston_info *info;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020095
96 uint32_t capabilities;
Rob Bradford14a76012013-05-31 18:09:52 +010097 char *name;
Jonny Lamb06959082014-08-12 14:58:27 +020098
99 int32_t repeat_rate;
100 int32_t repeat_delay;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200101};
102
Pekka Paalanen93a6afd2014-09-23 22:08:44 -0400103struct presentation_info {
104 struct global_info global;
105 struct presentation *presentation;
106
107 clockid_t clk_id;
108};
109
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200110struct weston_info {
111 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400112 struct wl_registry *registry;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200113
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200114 struct wl_list infos;
Philipp Brüschweilerbb0d4b92012-08-15 21:57:23 +0200115 bool roundtrip_needed;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200116};
117
Kristian Høgsberg06b16c22013-08-15 14:20:53 -0700118static void *
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800119fail_on_null(void *p)
Kristian Høgsberg06b16c22013-08-15 14:20:53 -0700120{
Kristian Høgsberg06b16c22013-08-15 14:20:53 -0700121 if (p == NULL) {
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800122 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
123 exit(EXIT_FAILURE);
Kristian Høgsberg06b16c22013-08-15 14:20:53 -0700124 }
125
126 return p;
127}
128
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800129static void *
130xmalloc(size_t s)
131{
132 return fail_on_null(malloc(s));
133}
134
135static void *
136xzalloc(size_t s)
137{
138 return fail_on_null(calloc(1, s));
139}
140
141static char *
142xstrdup(const char *s)
143{
144 return fail_on_null(strdup(s));
145}
146
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200147static void
148print_global_info(void *data)
149{
150 struct global_info *global = data;
151
152 printf("interface: '%s', version: %u, name: %u\n",
153 global->interface, global->version, global->id);
154}
155
156static void
157init_global_info(struct weston_info *info,
158 struct global_info *global, uint32_t id,
159 const char *interface, uint32_t version)
160{
161 global->id = id;
162 global->version = version;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800163 global->interface = xstrdup(interface);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200164
165 wl_list_insert(info->infos.prev, &global->link);
166}
167
168static void
169print_output_info(void *data)
170{
171 struct output_info *output = data;
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +0200172 struct output_mode *mode;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200173 const char *subpixel_orientation;
174 const char *transform;
175
176 print_global_info(data);
177
178 switch (output->geometry.subpixel) {
179 case WL_OUTPUT_SUBPIXEL_UNKNOWN:
180 subpixel_orientation = "unknown";
181 break;
182 case WL_OUTPUT_SUBPIXEL_NONE:
183 subpixel_orientation = "none";
184 break;
185 case WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB:
186 subpixel_orientation = "horizontal rgb";
187 break;
188 case WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR:
189 subpixel_orientation = "horizontal bgr";
190 break;
191 case WL_OUTPUT_SUBPIXEL_VERTICAL_RGB:
192 subpixel_orientation = "vertical rgb";
193 break;
194 case WL_OUTPUT_SUBPIXEL_VERTICAL_BGR:
195 subpixel_orientation = "vertical bgr";
196 break;
197 default:
198 fprintf(stderr, "unknown subpixel orientation %u\n",
199 output->geometry.subpixel);
200 subpixel_orientation = "unexpected value";
201 break;
202 }
203
204 switch (output->geometry.output_transform) {
205 case WL_OUTPUT_TRANSFORM_NORMAL:
206 transform = "normal";
207 break;
208 case WL_OUTPUT_TRANSFORM_90:
209 transform = "90°";
210 break;
211 case WL_OUTPUT_TRANSFORM_180:
212 transform = "180°";
213 break;
214 case WL_OUTPUT_TRANSFORM_270:
215 transform = "270°";
216 break;
217 case WL_OUTPUT_TRANSFORM_FLIPPED:
218 transform = "flipped";
219 break;
220 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
221 transform = "flipped 90°";
222 break;
223 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
224 transform = "flipped 180°";
225 break;
226 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
227 transform = "flipped 270°";
228 break;
229 default:
230 fprintf(stderr, "unknown output transform %u\n",
231 output->geometry.output_transform);
232 transform = "unexpected value";
233 break;
234 }
235
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +0200236 printf("\tx: %d, y: %d,\n",
237 output->geometry.x, output->geometry.y);
238 printf("\tphysical_width: %d mm, physical_height: %d mm,\n",
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200239 output->geometry.physical_width,
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +0200240 output->geometry.physical_height);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200241 printf("\tmake: '%s', model: '%s',\n",
242 output->geometry.make, output->geometry.model);
U. Artie Eoffcb0e3572014-04-18 09:30:07 -0700243 printf("\tsubpixel_orientation: %s, output_transform: %s,\n",
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200244 subpixel_orientation, transform);
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +0200245
246 wl_list_for_each(mode, &output->modes, link) {
247 printf("\tmode:\n");
248
249 printf("\t\twidth: %d px, height: %d px, refresh: %.f Hz,\n",
250 mode->width, mode->height,
251 (float) mode->refresh / 1000);
252
253 printf("\t\tflags:");
254 if (mode->flags & WL_OUTPUT_MODE_CURRENT)
255 printf(" current");
256 if (mode->flags & WL_OUTPUT_MODE_PREFERRED)
257 printf(" preferred");
258 printf("\n");
259 }
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200260}
261
262static void
263print_shm_info(void *data)
264{
265 struct shm_info *shm = data;
266 struct shm_format *format;
267
268 print_global_info(data);
269
270 printf("\tformats:");
271
272 wl_list_for_each(format, &shm->formats, link)
Kristian Høgsberg8b66ebd2013-11-20 13:54:00 -0800273 switch (format->format) {
274 case WL_SHM_FORMAT_ARGB8888:
275 printf(" ARGB8888");
276 break;
277 case WL_SHM_FORMAT_XRGB8888:
278 printf(" XRGB8888");
279 break;
280 case WL_SHM_FORMAT_RGB565:
281 printf(" RGB565");
282 break;
283 default:
284 printf(" unknown(%08x)", format->format);
285 break;
286 }
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200287
288 printf("\n");
289}
290
291static void
292print_seat_info(void *data)
293{
294 struct seat_info *seat = data;
295
296 print_global_info(data);
297
Rob Bradford14a76012013-05-31 18:09:52 +0100298 printf("\tname: %s\n", seat->name);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200299 printf("\tcapabilities:");
300
301 if (seat->capabilities & WL_SEAT_CAPABILITY_POINTER)
302 printf(" pointer");
303 if (seat->capabilities & WL_SEAT_CAPABILITY_KEYBOARD)
304 printf(" keyboard");
305 if (seat->capabilities & WL_SEAT_CAPABILITY_TOUCH)
306 printf(" touch");
307
308 printf("\n");
Jonny Lamb06959082014-08-12 14:58:27 +0200309
310 if (seat->repeat_rate > 0)
311 printf("\tkeyboard repeat rate: %d\n", seat->repeat_rate);
312 if (seat->repeat_delay > 0)
313 printf("\tkeyboard repeat delay: %d\n", seat->repeat_delay);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200314}
315
316static void
Jonny Lamb06959082014-08-12 14:58:27 +0200317keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
318 uint32_t format, int fd, uint32_t size)
319{
320}
321
322static void
323keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
324 uint32_t serial, struct wl_surface *surface,
325 struct wl_array *keys)
326{
327}
328
329static void
330keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
331 uint32_t serial, struct wl_surface *surface)
332{
333}
334
335static void
336keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
337 uint32_t serial, uint32_t time, uint32_t key,
338 uint32_t state)
339{
340}
341
342static void
343keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
344 uint32_t serial, uint32_t mods_depressed,
345 uint32_t mods_latched, uint32_t mods_locked,
346 uint32_t group)
347{
348}
349
350static void
351keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
352 int32_t rate, int32_t delay)
353{
354 struct seat_info *seat = data;
355
356 seat->repeat_rate = rate;
357 seat->repeat_delay = delay;
358}
359
360static const struct wl_keyboard_listener keyboard_listener = {
361 keyboard_handle_keymap,
362 keyboard_handle_enter,
363 keyboard_handle_leave,
364 keyboard_handle_key,
365 keyboard_handle_modifiers,
366 keyboard_handle_repeat_info,
367};
368
369static void
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200370seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
371 enum wl_seat_capability caps)
372{
373 struct seat_info *seat = data;
Jonny Lamb06959082014-08-12 14:58:27 +0200374
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200375 seat->capabilities = caps;
Jonny Lamb06959082014-08-12 14:58:27 +0200376
377 /* we want listen for repeat_info from wl_keyboard, but only
378 * do so if the seat info is >= 4 and if we actually have a
379 * keyboard */
380 if (seat->global.version < 4)
381 return;
382
383 if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
384 struct wl_keyboard *keyboard;
385
386 keyboard = wl_seat_get_keyboard(seat->seat);
387 wl_keyboard_add_listener(keyboard, &keyboard_listener,
388 seat);
389
390 seat->info->roundtrip_needed = true;
391 }
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200392}
393
Rob Bradford14a76012013-05-31 18:09:52 +0100394static void
395seat_handle_name(void *data, struct wl_seat *wl_seat,
396 const char *name)
397{
398 struct seat_info *seat = data;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800399 seat->name = xstrdup(name);
Rob Bradford14a76012013-05-31 18:09:52 +0100400}
401
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200402static const struct wl_seat_listener seat_listener = {
403 seat_handle_capabilities,
Rob Bradford14a76012013-05-31 18:09:52 +0100404 seat_handle_name,
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200405};
406
407static void
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800408destroy_seat_info(void *data)
409{
410 struct seat_info *seat = data;
411
412 wl_seat_destroy(seat->seat);
413
414 if (seat->name != NULL)
415 free(seat->name);
416}
417
418static void
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200419add_seat_info(struct weston_info *info, uint32_t id, uint32_t version)
420{
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800421 struct seat_info *seat = xzalloc(sizeof *seat);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200422
Jonny Lamb06959082014-08-12 14:58:27 +0200423 /* required to set roundtrip_needed to true in capabilities
424 * handler */
425 seat->info = info;
426
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200427 init_global_info(info, &seat->global, id, "wl_seat", version);
428 seat->global.print = print_seat_info;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800429 seat->global.destroy = destroy_seat_info;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200430
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400431 seat->seat = wl_registry_bind(info->registry,
Jonny Lamb06959082014-08-12 14:58:27 +0200432 id, &wl_seat_interface, MIN(version, 4));
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200433 wl_seat_add_listener(seat->seat, &seat_listener, seat);
Philipp Brüschweilerbb0d4b92012-08-15 21:57:23 +0200434
Jonny Lamb06959082014-08-12 14:58:27 +0200435 seat->repeat_rate = seat->repeat_delay = -1;
436
Philipp Brüschweilerbb0d4b92012-08-15 21:57:23 +0200437 info->roundtrip_needed = true;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200438}
439
440static void
441shm_handle_format(void *data, struct wl_shm *wl_shm, uint32_t format)
442{
443 struct shm_info *shm = data;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800444 struct shm_format *shm_format = xzalloc(sizeof *shm_format);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200445
446 wl_list_insert(&shm->formats, &shm_format->link);
447 shm_format->format = format;
448}
449
450static const struct wl_shm_listener shm_listener = {
451 shm_handle_format,
452};
453
454static void
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800455destroy_shm_info(void *data)
456{
457 struct shm_info *shm = data;
458 struct shm_format *format, *tmp;
459
460 wl_list_for_each_safe(format, tmp, &shm->formats, link) {
461 wl_list_remove(&format->link);
462 free(format);
463 }
464
465 wl_shm_destroy(shm->shm);
466}
467
468static void
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200469add_shm_info(struct weston_info *info, uint32_t id, uint32_t version)
470{
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800471 struct shm_info *shm = xzalloc(sizeof *shm);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200472
473 init_global_info(info, &shm->global, id, "wl_shm", version);
474 shm->global.print = print_shm_info;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800475 shm->global.destroy = destroy_shm_info;
476
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200477 wl_list_init(&shm->formats);
478
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400479 shm->shm = wl_registry_bind(info->registry,
480 id, &wl_shm_interface, 1);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200481 wl_shm_add_listener(shm->shm, &shm_listener, shm);
Philipp Brüschweilerbb0d4b92012-08-15 21:57:23 +0200482
483 info->roundtrip_needed = true;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200484}
485
486static void
487output_handle_geometry(void *data, struct wl_output *wl_output,
488 int32_t x, int32_t y,
489 int32_t physical_width, int32_t physical_height,
490 int32_t subpixel,
491 const char *make, const char *model,
492 int32_t output_transform)
493{
494 struct output_info *output = data;
495
496 output->geometry.x = x;
497 output->geometry.y = y;
498 output->geometry.physical_width = physical_width;
499 output->geometry.physical_height = physical_height;
500 output->geometry.subpixel = subpixel;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800501 output->geometry.make = xstrdup(make);
502 output->geometry.model = xstrdup(model);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200503 output->geometry.output_transform = output_transform;
504}
505
506static void
507output_handle_mode(void *data, struct wl_output *wl_output,
508 uint32_t flags, int32_t width, int32_t height,
509 int32_t refresh)
510{
511 struct output_info *output = data;
Kristian Høgsberg06b16c22013-08-15 14:20:53 -0700512 struct output_mode *mode = xmalloc(sizeof *mode);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200513
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +0200514 mode->flags = flags;
515 mode->width = width;
516 mode->height = height;
517 mode->refresh = refresh;
518
519 wl_list_insert(output->modes.prev, &mode->link);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200520}
521
522static const struct wl_output_listener output_listener = {
523 output_handle_geometry,
524 output_handle_mode,
525};
526
527static void
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800528destroy_output_info(void *data)
529{
530 struct output_info *output = data;
531 struct output_mode *mode, *tmp;
532
533 wl_output_destroy(output->output);
534
535 if (output->geometry.make != NULL)
536 free(output->geometry.make);
537 if (output->geometry.model != NULL)
538 free(output->geometry.model);
539
540 wl_list_for_each_safe(mode, tmp, &output->modes, link) {
541 wl_list_remove(&mode->link);
542 free(mode);
543 }
544}
545
546static void
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200547add_output_info(struct weston_info *info, uint32_t id, uint32_t version)
548{
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800549 struct output_info *output = xzalloc(sizeof *output);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200550
551 init_global_info(info, &output->global, id, "wl_output", version);
552 output->global.print = print_output_info;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800553 output->global.destroy = destroy_output_info;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200554
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +0200555 wl_list_init(&output->modes);
556
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400557 output->output = wl_registry_bind(info->registry, id,
558 &wl_output_interface, 1);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200559 wl_output_add_listener(output->output, &output_listener,
560 output);
Philipp Brüschweilerbb0d4b92012-08-15 21:57:23 +0200561
562 info->roundtrip_needed = true;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200563}
564
565static void
Pekka Paalanen93a6afd2014-09-23 22:08:44 -0400566destroy_presentation_info(void *info)
567{
568 struct presentation_info *prinfo = info;
569
570 presentation_destroy(prinfo->presentation);
571}
572
573static const char *
574clock_name(clockid_t clk_id)
575{
576 static const char *names[] = {
577 [CLOCK_REALTIME] = "CLOCK_REALTIME",
578 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
579 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
580 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
581 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
Derek Foreman32838c92015-06-29 13:20:34 -0500582#ifdef CLOCK_BOOTTIME
Pekka Paalanen93a6afd2014-09-23 22:08:44 -0400583 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
Derek Foreman32838c92015-06-29 13:20:34 -0500584#endif
Pekka Paalanen93a6afd2014-09-23 22:08:44 -0400585 };
586
587 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
588 return "unknown";
589
590 return names[clk_id];
591}
592
593static void
594print_presentation_info(void *info)
595{
596 struct presentation_info *prinfo = info;
597
598 print_global_info(info);
599
600 printf("\tpresentation clock id: %d (%s)\n",
601 prinfo->clk_id, clock_name(prinfo->clk_id));
602}
603
604static void
605presentation_handle_clock_id(void *data, struct presentation *presentation,
606 uint32_t clk_id)
607{
608 struct presentation_info *prinfo = data;
609
610 prinfo->clk_id = clk_id;
611}
612
613static const struct presentation_listener presentation_listener = {
614 presentation_handle_clock_id
615};
616
617static void
618add_presentation_info(struct weston_info *info, uint32_t id, uint32_t version)
619{
620 struct presentation_info *prinfo = xzalloc(sizeof *prinfo);
621
622 init_global_info(info, &prinfo->global, id, "presentation", version);
623 prinfo->global.print = print_presentation_info;
624 prinfo->global.destroy = destroy_presentation_info;
625
626 prinfo->clk_id = -1;
627 prinfo->presentation = wl_registry_bind(info->registry, id,
628 &presentation_interface, 1);
629 presentation_add_listener(prinfo->presentation, &presentation_listener,
630 prinfo);
631
632 info->roundtrip_needed = true;
633}
634
635static void
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800636destroy_global_info(void *data)
637{
638}
639
640static void
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200641add_global_info(struct weston_info *info, uint32_t id,
642 const char *interface, uint32_t version)
643{
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800644 struct global_info *global = xzalloc(sizeof *global);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200645
646 init_global_info(info, global, id, interface, version);
647 global->print = print_global_info;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800648 global->destroy = destroy_global_info;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200649}
650
651static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400652global_handler(void *data, struct wl_registry *registry, uint32_t id,
653 const char *interface, uint32_t version)
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200654{
655 struct weston_info *info = data;
656
657 if (!strcmp(interface, "wl_seat"))
658 add_seat_info(info, id, version);
659 else if (!strcmp(interface, "wl_shm"))
660 add_shm_info(info, id, version);
661 else if (!strcmp(interface, "wl_output"))
662 add_output_info(info, id, version);
Pekka Paalanen93a6afd2014-09-23 22:08:44 -0400663 else if (!strcmp(interface, "presentation"))
664 add_presentation_info(info, id, version);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200665 else
666 add_global_info(info, id, interface, version);
667}
668
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200669static void
670global_remove_handler(void *data, struct wl_registry *registry, uint32_t name)
671{
672}
673
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400674static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200675 global_handler,
676 global_remove_handler
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400677};
678
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200679static void
680print_infos(struct wl_list *infos)
681{
682 struct global_info *info;
683
684 wl_list_for_each(info, infos, link)
685 info->print(info);
686}
687
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800688static void
689destroy_info(void *data)
690{
691 struct global_info *global = data;
692
693 global->destroy(data);
694 wl_list_remove(&global->link);
695 free(global->interface);
696 free(data);
697}
698
699static void
700destroy_infos(struct wl_list *infos)
701{
702 struct global_info *info, *tmp;
703 wl_list_for_each_safe(info, tmp, infos, link)
704 destroy_info(info);
705}
706
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200707int
708main(int argc, char **argv)
709{
710 struct weston_info info;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200711
712 info.display = wl_display_connect(NULL);
713 if (!info.display) {
714 fprintf(stderr, "failed to create display: %m\n");
715 return -1;
716 }
717
718 wl_list_init(&info.infos);
719
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400720 info.registry = wl_display_get_registry(info.display);
721 wl_registry_add_listener(info.registry, &registry_listener, &info);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200722
Philipp Brüschweilerbb0d4b92012-08-15 21:57:23 +0200723 do {
724 info.roundtrip_needed = false;
725 wl_display_roundtrip(info.display);
726 } while (info.roundtrip_needed);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200727
728 print_infos(&info.infos);
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800729 destroy_infos(&info.infos);
730
731 wl_registry_destroy(info.registry);
732 wl_display_disconnect(info.display);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200733
734 return 0;
735}