blob: f77798255909cbdaec3fae53f9eee1cec228f5cb [file] [log] [blame]
Philipp Brüschweiler585acb02012-08-15 17:12:00 +02001/*
2 * Copyright © 2012 Philipp Brüschweiler
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
U. Artie Eoff86c68b32014-01-15 13:02:28 -080023#include "config.h"
24
25#include <errno.h>
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020026#include <stdbool.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
Pekka Paalanen93a6afd2014-09-23 22:08:44 -040030#include <time.h>
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020031
32#include <wayland-client.h>
33
34#include "../shared/os-compatibility.h"
Pekka Paalanen93a6afd2014-09-23 22:08:44 -040035#include "presentation_timing-client-protocol.h"
36
37#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020038
Jonny Lamb06959082014-08-12 14:58:27 +020039#define MIN(x,y) (((x) < (y)) ? (x) : (y))
40
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020041typedef void (*print_info_t)(void *info);
U. Artie Eoff86c68b32014-01-15 13:02:28 -080042typedef void (*destroy_info_t)(void *info);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020043
44struct global_info {
45 struct wl_list link;
46
47 uint32_t id;
48 uint32_t version;
49 char *interface;
50
51 print_info_t print;
U. Artie Eoff86c68b32014-01-15 13:02:28 -080052 destroy_info_t destroy;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020053};
54
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +020055struct output_mode {
56 struct wl_list link;
57
58 uint32_t flags;
59 int32_t width, height;
60 int32_t refresh;
61};
62
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020063struct output_info {
64 struct global_info global;
65
66 struct wl_output *output;
67
68 struct {
69 int32_t x, y;
70 int32_t physical_width, physical_height;
71 enum wl_output_subpixel subpixel;
72 enum wl_output_transform output_transform;
73 char *make;
74 char *model;
75 } geometry;
76
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +020077 struct wl_list modes;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020078};
79
80struct shm_format {
81 struct wl_list link;
82
83 uint32_t format;
84};
85
86struct shm_info {
87 struct global_info global;
88 struct wl_shm *shm;
89
90 struct wl_list formats;
91};
92
93struct seat_info {
94 struct global_info global;
95 struct wl_seat *seat;
Jonny Lamb06959082014-08-12 14:58:27 +020096 struct weston_info *info;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020097
98 uint32_t capabilities;
Rob Bradford14a76012013-05-31 18:09:52 +010099 char *name;
Jonny Lamb06959082014-08-12 14:58:27 +0200100
101 int32_t repeat_rate;
102 int32_t repeat_delay;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200103};
104
Pekka Paalanen93a6afd2014-09-23 22:08:44 -0400105struct presentation_info {
106 struct global_info global;
107 struct presentation *presentation;
108
109 clockid_t clk_id;
110};
111
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200112struct weston_info {
113 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400114 struct wl_registry *registry;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200115
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200116 struct wl_list infos;
Philipp Brüschweilerbb0d4b92012-08-15 21:57:23 +0200117 bool roundtrip_needed;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200118};
119
Kristian Høgsberg06b16c22013-08-15 14:20:53 -0700120static void *
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800121fail_on_null(void *p)
Kristian Høgsberg06b16c22013-08-15 14:20:53 -0700122{
Kristian Høgsberg06b16c22013-08-15 14:20:53 -0700123 if (p == NULL) {
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800124 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
125 exit(EXIT_FAILURE);
Kristian Høgsberg06b16c22013-08-15 14:20:53 -0700126 }
127
128 return p;
129}
130
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800131static void *
132xmalloc(size_t s)
133{
134 return fail_on_null(malloc(s));
135}
136
137static void *
138xzalloc(size_t s)
139{
140 return fail_on_null(calloc(1, s));
141}
142
143static char *
144xstrdup(const char *s)
145{
146 return fail_on_null(strdup(s));
147}
148
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200149static void
150print_global_info(void *data)
151{
152 struct global_info *global = data;
153
154 printf("interface: '%s', version: %u, name: %u\n",
155 global->interface, global->version, global->id);
156}
157
158static void
159init_global_info(struct weston_info *info,
160 struct global_info *global, uint32_t id,
161 const char *interface, uint32_t version)
162{
163 global->id = id;
164 global->version = version;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800165 global->interface = xstrdup(interface);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200166
167 wl_list_insert(info->infos.prev, &global->link);
168}
169
170static void
171print_output_info(void *data)
172{
173 struct output_info *output = data;
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +0200174 struct output_mode *mode;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200175 const char *subpixel_orientation;
176 const char *transform;
177
178 print_global_info(data);
179
180 switch (output->geometry.subpixel) {
181 case WL_OUTPUT_SUBPIXEL_UNKNOWN:
182 subpixel_orientation = "unknown";
183 break;
184 case WL_OUTPUT_SUBPIXEL_NONE:
185 subpixel_orientation = "none";
186 break;
187 case WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB:
188 subpixel_orientation = "horizontal rgb";
189 break;
190 case WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR:
191 subpixel_orientation = "horizontal bgr";
192 break;
193 case WL_OUTPUT_SUBPIXEL_VERTICAL_RGB:
194 subpixel_orientation = "vertical rgb";
195 break;
196 case WL_OUTPUT_SUBPIXEL_VERTICAL_BGR:
197 subpixel_orientation = "vertical bgr";
198 break;
199 default:
200 fprintf(stderr, "unknown subpixel orientation %u\n",
201 output->geometry.subpixel);
202 subpixel_orientation = "unexpected value";
203 break;
204 }
205
206 switch (output->geometry.output_transform) {
207 case WL_OUTPUT_TRANSFORM_NORMAL:
208 transform = "normal";
209 break;
210 case WL_OUTPUT_TRANSFORM_90:
211 transform = "90°";
212 break;
213 case WL_OUTPUT_TRANSFORM_180:
214 transform = "180°";
215 break;
216 case WL_OUTPUT_TRANSFORM_270:
217 transform = "270°";
218 break;
219 case WL_OUTPUT_TRANSFORM_FLIPPED:
220 transform = "flipped";
221 break;
222 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
223 transform = "flipped 90°";
224 break;
225 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
226 transform = "flipped 180°";
227 break;
228 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
229 transform = "flipped 270°";
230 break;
231 default:
232 fprintf(stderr, "unknown output transform %u\n",
233 output->geometry.output_transform);
234 transform = "unexpected value";
235 break;
236 }
237
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +0200238 printf("\tx: %d, y: %d,\n",
239 output->geometry.x, output->geometry.y);
240 printf("\tphysical_width: %d mm, physical_height: %d mm,\n",
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200241 output->geometry.physical_width,
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +0200242 output->geometry.physical_height);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200243 printf("\tmake: '%s', model: '%s',\n",
244 output->geometry.make, output->geometry.model);
U. Artie Eoffcb0e3572014-04-18 09:30:07 -0700245 printf("\tsubpixel_orientation: %s, output_transform: %s,\n",
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200246 subpixel_orientation, transform);
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +0200247
248 wl_list_for_each(mode, &output->modes, link) {
249 printf("\tmode:\n");
250
251 printf("\t\twidth: %d px, height: %d px, refresh: %.f Hz,\n",
252 mode->width, mode->height,
253 (float) mode->refresh / 1000);
254
255 printf("\t\tflags:");
256 if (mode->flags & WL_OUTPUT_MODE_CURRENT)
257 printf(" current");
258 if (mode->flags & WL_OUTPUT_MODE_PREFERRED)
259 printf(" preferred");
260 printf("\n");
261 }
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200262}
263
264static void
265print_shm_info(void *data)
266{
267 struct shm_info *shm = data;
268 struct shm_format *format;
269
270 print_global_info(data);
271
272 printf("\tformats:");
273
274 wl_list_for_each(format, &shm->formats, link)
Kristian Høgsberg8b66ebd2013-11-20 13:54:00 -0800275 switch (format->format) {
276 case WL_SHM_FORMAT_ARGB8888:
277 printf(" ARGB8888");
278 break;
279 case WL_SHM_FORMAT_XRGB8888:
280 printf(" XRGB8888");
281 break;
282 case WL_SHM_FORMAT_RGB565:
283 printf(" RGB565");
284 break;
285 default:
286 printf(" unknown(%08x)", format->format);
287 break;
288 }
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200289
290 printf("\n");
291}
292
293static void
294print_seat_info(void *data)
295{
296 struct seat_info *seat = data;
297
298 print_global_info(data);
299
Rob Bradford14a76012013-05-31 18:09:52 +0100300 printf("\tname: %s\n", seat->name);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200301 printf("\tcapabilities:");
302
303 if (seat->capabilities & WL_SEAT_CAPABILITY_POINTER)
304 printf(" pointer");
305 if (seat->capabilities & WL_SEAT_CAPABILITY_KEYBOARD)
306 printf(" keyboard");
307 if (seat->capabilities & WL_SEAT_CAPABILITY_TOUCH)
308 printf(" touch");
309
310 printf("\n");
Jonny Lamb06959082014-08-12 14:58:27 +0200311
312 if (seat->repeat_rate > 0)
313 printf("\tkeyboard repeat rate: %d\n", seat->repeat_rate);
314 if (seat->repeat_delay > 0)
315 printf("\tkeyboard repeat delay: %d\n", seat->repeat_delay);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200316}
317
318static void
Jonny Lamb06959082014-08-12 14:58:27 +0200319keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
320 uint32_t format, int fd, uint32_t size)
321{
322}
323
324static void
325keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
326 uint32_t serial, struct wl_surface *surface,
327 struct wl_array *keys)
328{
329}
330
331static void
332keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
333 uint32_t serial, struct wl_surface *surface)
334{
335}
336
337static void
338keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
339 uint32_t serial, uint32_t time, uint32_t key,
340 uint32_t state)
341{
342}
343
344static void
345keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
346 uint32_t serial, uint32_t mods_depressed,
347 uint32_t mods_latched, uint32_t mods_locked,
348 uint32_t group)
349{
350}
351
352static void
353keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
354 int32_t rate, int32_t delay)
355{
356 struct seat_info *seat = data;
357
358 seat->repeat_rate = rate;
359 seat->repeat_delay = delay;
360}
361
362static const struct wl_keyboard_listener keyboard_listener = {
363 keyboard_handle_keymap,
364 keyboard_handle_enter,
365 keyboard_handle_leave,
366 keyboard_handle_key,
367 keyboard_handle_modifiers,
368 keyboard_handle_repeat_info,
369};
370
371static void
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200372seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
373 enum wl_seat_capability caps)
374{
375 struct seat_info *seat = data;
Jonny Lamb06959082014-08-12 14:58:27 +0200376
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200377 seat->capabilities = caps;
Jonny Lamb06959082014-08-12 14:58:27 +0200378
379 /* we want listen for repeat_info from wl_keyboard, but only
380 * do so if the seat info is >= 4 and if we actually have a
381 * keyboard */
382 if (seat->global.version < 4)
383 return;
384
385 if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
386 struct wl_keyboard *keyboard;
387
388 keyboard = wl_seat_get_keyboard(seat->seat);
389 wl_keyboard_add_listener(keyboard, &keyboard_listener,
390 seat);
391
392 seat->info->roundtrip_needed = true;
393 }
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200394}
395
Rob Bradford14a76012013-05-31 18:09:52 +0100396static void
397seat_handle_name(void *data, struct wl_seat *wl_seat,
398 const char *name)
399{
400 struct seat_info *seat = data;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800401 seat->name = xstrdup(name);
Rob Bradford14a76012013-05-31 18:09:52 +0100402}
403
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200404static const struct wl_seat_listener seat_listener = {
405 seat_handle_capabilities,
Rob Bradford14a76012013-05-31 18:09:52 +0100406 seat_handle_name,
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200407};
408
409static void
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800410destroy_seat_info(void *data)
411{
412 struct seat_info *seat = data;
413
414 wl_seat_destroy(seat->seat);
415
416 if (seat->name != NULL)
417 free(seat->name);
418}
419
420static void
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200421add_seat_info(struct weston_info *info, uint32_t id, uint32_t version)
422{
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800423 struct seat_info *seat = xzalloc(sizeof *seat);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200424
Jonny Lamb06959082014-08-12 14:58:27 +0200425 /* required to set roundtrip_needed to true in capabilities
426 * handler */
427 seat->info = info;
428
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200429 init_global_info(info, &seat->global, id, "wl_seat", version);
430 seat->global.print = print_seat_info;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800431 seat->global.destroy = destroy_seat_info;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200432
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400433 seat->seat = wl_registry_bind(info->registry,
Jonny Lamb06959082014-08-12 14:58:27 +0200434 id, &wl_seat_interface, MIN(version, 4));
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200435 wl_seat_add_listener(seat->seat, &seat_listener, seat);
Philipp Brüschweilerbb0d4b92012-08-15 21:57:23 +0200436
Jonny Lamb06959082014-08-12 14:58:27 +0200437 seat->repeat_rate = seat->repeat_delay = -1;
438
Philipp Brüschweilerbb0d4b92012-08-15 21:57:23 +0200439 info->roundtrip_needed = true;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200440}
441
442static void
443shm_handle_format(void *data, struct wl_shm *wl_shm, uint32_t format)
444{
445 struct shm_info *shm = data;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800446 struct shm_format *shm_format = xzalloc(sizeof *shm_format);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200447
448 wl_list_insert(&shm->formats, &shm_format->link);
449 shm_format->format = format;
450}
451
452static const struct wl_shm_listener shm_listener = {
453 shm_handle_format,
454};
455
456static void
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800457destroy_shm_info(void *data)
458{
459 struct shm_info *shm = data;
460 struct shm_format *format, *tmp;
461
462 wl_list_for_each_safe(format, tmp, &shm->formats, link) {
463 wl_list_remove(&format->link);
464 free(format);
465 }
466
467 wl_shm_destroy(shm->shm);
468}
469
470static void
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200471add_shm_info(struct weston_info *info, uint32_t id, uint32_t version)
472{
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800473 struct shm_info *shm = xzalloc(sizeof *shm);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200474
475 init_global_info(info, &shm->global, id, "wl_shm", version);
476 shm->global.print = print_shm_info;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800477 shm->global.destroy = destroy_shm_info;
478
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200479 wl_list_init(&shm->formats);
480
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400481 shm->shm = wl_registry_bind(info->registry,
482 id, &wl_shm_interface, 1);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200483 wl_shm_add_listener(shm->shm, &shm_listener, shm);
Philipp Brüschweilerbb0d4b92012-08-15 21:57:23 +0200484
485 info->roundtrip_needed = true;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200486}
487
488static void
489output_handle_geometry(void *data, struct wl_output *wl_output,
490 int32_t x, int32_t y,
491 int32_t physical_width, int32_t physical_height,
492 int32_t subpixel,
493 const char *make, const char *model,
494 int32_t output_transform)
495{
496 struct output_info *output = data;
497
498 output->geometry.x = x;
499 output->geometry.y = y;
500 output->geometry.physical_width = physical_width;
501 output->geometry.physical_height = physical_height;
502 output->geometry.subpixel = subpixel;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800503 output->geometry.make = xstrdup(make);
504 output->geometry.model = xstrdup(model);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200505 output->geometry.output_transform = output_transform;
506}
507
508static void
509output_handle_mode(void *data, struct wl_output *wl_output,
510 uint32_t flags, int32_t width, int32_t height,
511 int32_t refresh)
512{
513 struct output_info *output = data;
Kristian Høgsberg06b16c22013-08-15 14:20:53 -0700514 struct output_mode *mode = xmalloc(sizeof *mode);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200515
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +0200516 mode->flags = flags;
517 mode->width = width;
518 mode->height = height;
519 mode->refresh = refresh;
520
521 wl_list_insert(output->modes.prev, &mode->link);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200522}
523
524static const struct wl_output_listener output_listener = {
525 output_handle_geometry,
526 output_handle_mode,
527};
528
529static void
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800530destroy_output_info(void *data)
531{
532 struct output_info *output = data;
533 struct output_mode *mode, *tmp;
534
535 wl_output_destroy(output->output);
536
537 if (output->geometry.make != NULL)
538 free(output->geometry.make);
539 if (output->geometry.model != NULL)
540 free(output->geometry.model);
541
542 wl_list_for_each_safe(mode, tmp, &output->modes, link) {
543 wl_list_remove(&mode->link);
544 free(mode);
545 }
546}
547
548static void
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200549add_output_info(struct weston_info *info, uint32_t id, uint32_t version)
550{
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800551 struct output_info *output = xzalloc(sizeof *output);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200552
553 init_global_info(info, &output->global, id, "wl_output", version);
554 output->global.print = print_output_info;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800555 output->global.destroy = destroy_output_info;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200556
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +0200557 wl_list_init(&output->modes);
558
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400559 output->output = wl_registry_bind(info->registry, id,
560 &wl_output_interface, 1);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200561 wl_output_add_listener(output->output, &output_listener,
562 output);
Philipp Brüschweilerbb0d4b92012-08-15 21:57:23 +0200563
564 info->roundtrip_needed = true;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200565}
566
567static void
Pekka Paalanen93a6afd2014-09-23 22:08:44 -0400568destroy_presentation_info(void *info)
569{
570 struct presentation_info *prinfo = info;
571
572 presentation_destroy(prinfo->presentation);
573}
574
575static const char *
576clock_name(clockid_t clk_id)
577{
578 static const char *names[] = {
579 [CLOCK_REALTIME] = "CLOCK_REALTIME",
580 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
581 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
582 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
583 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
584 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
585 };
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}