blob: 712346a01287399af98b56b8e1cd7b2589a84d06 [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>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030028#include <stdint.h>
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020029#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
Pekka Paalanen93a6afd2014-09-23 22:08:44 -040032#include <time.h>
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020033
34#include <wayland-client.h>
35
Jon Cruz35b2eaa2015-06-15 15:37:08 -070036#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070037#include "shared/os-compatibility.h"
Bryce Harringtone99e4bf2016-03-16 14:15:18 -070038#include "shared/xalloc.h"
Bryce Harrington0d1a6222016-02-11 16:42:49 -080039#include "shared/zalloc.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020040#include "presentation-time-client-protocol.h"
Pekka Paalanen93a6afd2014-09-23 22:08:44 -040041
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020042typedef void (*print_info_t)(void *info);
U. Artie Eoff86c68b32014-01-15 13:02:28 -080043typedef void (*destroy_info_t)(void *info);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020044
45struct global_info {
46 struct wl_list link;
47
48 uint32_t id;
49 uint32_t version;
50 char *interface;
51
52 print_info_t print;
U. Artie Eoff86c68b32014-01-15 13:02:28 -080053 destroy_info_t destroy;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020054};
55
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +020056struct output_mode {
57 struct wl_list link;
58
59 uint32_t flags;
60 int32_t width, height;
61 int32_t refresh;
62};
63
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020064struct output_info {
65 struct global_info global;
66
67 struct wl_output *output;
68
Jonny Lambba9dca02015-12-17 12:31:56 +000069 int32_t version;
70
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020071 struct {
72 int32_t x, y;
Jonny Lambba9dca02015-12-17 12:31:56 +000073 int32_t scale;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020074 int32_t physical_width, physical_height;
75 enum wl_output_subpixel subpixel;
76 enum wl_output_transform output_transform;
77 char *make;
78 char *model;
79 } geometry;
80
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +020081 struct wl_list modes;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +020082};
83
84struct shm_format {
85 struct wl_list link;
86
87 uint32_t format;
88};
89
90struct shm_info {
91 struct global_info global;
92 struct wl_shm *shm;
93
94 struct wl_list formats;
95};
96
97struct seat_info {
98 struct global_info global;
99 struct wl_seat *seat;
Jonny Lamb06959082014-08-12 14:58:27 +0200100 struct weston_info *info;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200101
102 uint32_t capabilities;
Rob Bradford14a76012013-05-31 18:09:52 +0100103 char *name;
Jonny Lamb06959082014-08-12 14:58:27 +0200104
105 int32_t repeat_rate;
106 int32_t repeat_delay;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200107};
108
Pekka Paalanen93a6afd2014-09-23 22:08:44 -0400109struct presentation_info {
110 struct global_info global;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200111 struct wp_presentation *presentation;
Pekka Paalanen93a6afd2014-09-23 22:08:44 -0400112
113 clockid_t clk_id;
114};
115
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200116struct weston_info {
117 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400118 struct wl_registry *registry;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200119
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200120 struct wl_list infos;
Philipp Brüschweilerbb0d4b92012-08-15 21:57:23 +0200121 bool roundtrip_needed;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200122};
123
124static void
125print_global_info(void *data)
126{
127 struct global_info *global = data;
128
129 printf("interface: '%s', version: %u, name: %u\n",
130 global->interface, global->version, global->id);
131}
132
133static void
134init_global_info(struct weston_info *info,
135 struct global_info *global, uint32_t id,
136 const char *interface, uint32_t version)
137{
138 global->id = id;
139 global->version = version;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800140 global->interface = xstrdup(interface);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200141
142 wl_list_insert(info->infos.prev, &global->link);
143}
144
145static void
146print_output_info(void *data)
147{
148 struct output_info *output = data;
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +0200149 struct output_mode *mode;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200150 const char *subpixel_orientation;
151 const char *transform;
152
153 print_global_info(data);
154
155 switch (output->geometry.subpixel) {
156 case WL_OUTPUT_SUBPIXEL_UNKNOWN:
157 subpixel_orientation = "unknown";
158 break;
159 case WL_OUTPUT_SUBPIXEL_NONE:
160 subpixel_orientation = "none";
161 break;
162 case WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB:
163 subpixel_orientation = "horizontal rgb";
164 break;
165 case WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR:
166 subpixel_orientation = "horizontal bgr";
167 break;
168 case WL_OUTPUT_SUBPIXEL_VERTICAL_RGB:
169 subpixel_orientation = "vertical rgb";
170 break;
171 case WL_OUTPUT_SUBPIXEL_VERTICAL_BGR:
172 subpixel_orientation = "vertical bgr";
173 break;
174 default:
175 fprintf(stderr, "unknown subpixel orientation %u\n",
176 output->geometry.subpixel);
177 subpixel_orientation = "unexpected value";
178 break;
179 }
180
181 switch (output->geometry.output_transform) {
182 case WL_OUTPUT_TRANSFORM_NORMAL:
183 transform = "normal";
184 break;
185 case WL_OUTPUT_TRANSFORM_90:
186 transform = "90°";
187 break;
188 case WL_OUTPUT_TRANSFORM_180:
189 transform = "180°";
190 break;
191 case WL_OUTPUT_TRANSFORM_270:
192 transform = "270°";
193 break;
194 case WL_OUTPUT_TRANSFORM_FLIPPED:
195 transform = "flipped";
196 break;
197 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
198 transform = "flipped 90°";
199 break;
200 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
201 transform = "flipped 180°";
202 break;
203 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
204 transform = "flipped 270°";
205 break;
206 default:
207 fprintf(stderr, "unknown output transform %u\n",
208 output->geometry.output_transform);
209 transform = "unexpected value";
210 break;
211 }
212
Jonny Lambba9dca02015-12-17 12:31:56 +0000213 printf("\tx: %d, y: %d,",
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +0200214 output->geometry.x, output->geometry.y);
Jonny Lambba9dca02015-12-17 12:31:56 +0000215 if (output->version >= 2)
216 printf(" scale: %d,", output->geometry.scale);
217 printf("\n");
218
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +0200219 printf("\tphysical_width: %d mm, physical_height: %d mm,\n",
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200220 output->geometry.physical_width,
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +0200221 output->geometry.physical_height);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200222 printf("\tmake: '%s', model: '%s',\n",
223 output->geometry.make, output->geometry.model);
U. Artie Eoffcb0e3572014-04-18 09:30:07 -0700224 printf("\tsubpixel_orientation: %s, output_transform: %s,\n",
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200225 subpixel_orientation, transform);
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +0200226
227 wl_list_for_each(mode, &output->modes, link) {
228 printf("\tmode:\n");
229
Pekka Paalanen48fbb542016-04-12 11:05:23 +0300230 printf("\t\twidth: %d px, height: %d px, refresh: %.3f Hz,\n",
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +0200231 mode->width, mode->height,
232 (float) mode->refresh / 1000);
233
234 printf("\t\tflags:");
235 if (mode->flags & WL_OUTPUT_MODE_CURRENT)
236 printf(" current");
237 if (mode->flags & WL_OUTPUT_MODE_PREFERRED)
238 printf(" preferred");
239 printf("\n");
240 }
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200241}
242
243static void
244print_shm_info(void *data)
245{
246 struct shm_info *shm = data;
247 struct shm_format *format;
248
249 print_global_info(data);
250
251 printf("\tformats:");
252
253 wl_list_for_each(format, &shm->formats, link)
Kristian Høgsberg8b66ebd2013-11-20 13:54:00 -0800254 switch (format->format) {
255 case WL_SHM_FORMAT_ARGB8888:
256 printf(" ARGB8888");
257 break;
258 case WL_SHM_FORMAT_XRGB8888:
259 printf(" XRGB8888");
260 break;
261 case WL_SHM_FORMAT_RGB565:
262 printf(" RGB565");
263 break;
264 default:
265 printf(" unknown(%08x)", format->format);
266 break;
267 }
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200268
269 printf("\n");
270}
271
272static void
273print_seat_info(void *data)
274{
275 struct seat_info *seat = data;
276
277 print_global_info(data);
278
Rob Bradford14a76012013-05-31 18:09:52 +0100279 printf("\tname: %s\n", seat->name);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200280 printf("\tcapabilities:");
281
282 if (seat->capabilities & WL_SEAT_CAPABILITY_POINTER)
283 printf(" pointer");
284 if (seat->capabilities & WL_SEAT_CAPABILITY_KEYBOARD)
285 printf(" keyboard");
286 if (seat->capabilities & WL_SEAT_CAPABILITY_TOUCH)
287 printf(" touch");
288
289 printf("\n");
Jonny Lamb06959082014-08-12 14:58:27 +0200290
291 if (seat->repeat_rate > 0)
292 printf("\tkeyboard repeat rate: %d\n", seat->repeat_rate);
293 if (seat->repeat_delay > 0)
294 printf("\tkeyboard repeat delay: %d\n", seat->repeat_delay);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200295}
296
297static void
Jonny Lamb06959082014-08-12 14:58:27 +0200298keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
299 uint32_t format, int fd, uint32_t size)
300{
301}
302
303static void
304keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
305 uint32_t serial, struct wl_surface *surface,
306 struct wl_array *keys)
307{
308}
309
310static void
311keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
312 uint32_t serial, struct wl_surface *surface)
313{
314}
315
316static void
317keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
318 uint32_t serial, uint32_t time, uint32_t key,
319 uint32_t state)
320{
321}
322
323static void
324keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
325 uint32_t serial, uint32_t mods_depressed,
326 uint32_t mods_latched, uint32_t mods_locked,
327 uint32_t group)
328{
329}
330
331static void
332keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
333 int32_t rate, int32_t delay)
334{
335 struct seat_info *seat = data;
336
337 seat->repeat_rate = rate;
338 seat->repeat_delay = delay;
339}
340
341static const struct wl_keyboard_listener keyboard_listener = {
342 keyboard_handle_keymap,
343 keyboard_handle_enter,
344 keyboard_handle_leave,
345 keyboard_handle_key,
346 keyboard_handle_modifiers,
347 keyboard_handle_repeat_info,
348};
349
350static void
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200351seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
352 enum wl_seat_capability caps)
353{
354 struct seat_info *seat = data;
Jonny Lamb06959082014-08-12 14:58:27 +0200355
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200356 seat->capabilities = caps;
Jonny Lamb06959082014-08-12 14:58:27 +0200357
358 /* we want listen for repeat_info from wl_keyboard, but only
359 * do so if the seat info is >= 4 and if we actually have a
360 * keyboard */
361 if (seat->global.version < 4)
362 return;
363
364 if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
365 struct wl_keyboard *keyboard;
366
367 keyboard = wl_seat_get_keyboard(seat->seat);
368 wl_keyboard_add_listener(keyboard, &keyboard_listener,
369 seat);
370
371 seat->info->roundtrip_needed = true;
372 }
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200373}
374
Rob Bradford14a76012013-05-31 18:09:52 +0100375static void
376seat_handle_name(void *data, struct wl_seat *wl_seat,
377 const char *name)
378{
379 struct seat_info *seat = data;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800380 seat->name = xstrdup(name);
Rob Bradford14a76012013-05-31 18:09:52 +0100381}
382
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200383static const struct wl_seat_listener seat_listener = {
384 seat_handle_capabilities,
Rob Bradford14a76012013-05-31 18:09:52 +0100385 seat_handle_name,
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200386};
387
388static void
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800389destroy_seat_info(void *data)
390{
391 struct seat_info *seat = data;
392
393 wl_seat_destroy(seat->seat);
394
395 if (seat->name != NULL)
396 free(seat->name);
397}
398
399static void
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200400add_seat_info(struct weston_info *info, uint32_t id, uint32_t version)
401{
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800402 struct seat_info *seat = xzalloc(sizeof *seat);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200403
Jonny Lamb06959082014-08-12 14:58:27 +0200404 /* required to set roundtrip_needed to true in capabilities
405 * handler */
406 seat->info = info;
407
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200408 init_global_info(info, &seat->global, id, "wl_seat", version);
409 seat->global.print = print_seat_info;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800410 seat->global.destroy = destroy_seat_info;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200411
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400412 seat->seat = wl_registry_bind(info->registry,
Jonny Lamb06959082014-08-12 14:58:27 +0200413 id, &wl_seat_interface, MIN(version, 4));
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200414 wl_seat_add_listener(seat->seat, &seat_listener, seat);
Philipp Brüschweilerbb0d4b92012-08-15 21:57:23 +0200415
Jonny Lamb06959082014-08-12 14:58:27 +0200416 seat->repeat_rate = seat->repeat_delay = -1;
417
Philipp Brüschweilerbb0d4b92012-08-15 21:57:23 +0200418 info->roundtrip_needed = true;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200419}
420
421static void
422shm_handle_format(void *data, struct wl_shm *wl_shm, uint32_t format)
423{
424 struct shm_info *shm = data;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800425 struct shm_format *shm_format = xzalloc(sizeof *shm_format);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200426
427 wl_list_insert(&shm->formats, &shm_format->link);
428 shm_format->format = format;
429}
430
431static const struct wl_shm_listener shm_listener = {
432 shm_handle_format,
433};
434
435static void
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800436destroy_shm_info(void *data)
437{
438 struct shm_info *shm = data;
439 struct shm_format *format, *tmp;
440
441 wl_list_for_each_safe(format, tmp, &shm->formats, link) {
442 wl_list_remove(&format->link);
443 free(format);
444 }
445
446 wl_shm_destroy(shm->shm);
447}
448
449static void
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200450add_shm_info(struct weston_info *info, uint32_t id, uint32_t version)
451{
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800452 struct shm_info *shm = xzalloc(sizeof *shm);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200453
454 init_global_info(info, &shm->global, id, "wl_shm", version);
455 shm->global.print = print_shm_info;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800456 shm->global.destroy = destroy_shm_info;
457
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200458 wl_list_init(&shm->formats);
459
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400460 shm->shm = wl_registry_bind(info->registry,
461 id, &wl_shm_interface, 1);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200462 wl_shm_add_listener(shm->shm, &shm_listener, shm);
Philipp Brüschweilerbb0d4b92012-08-15 21:57:23 +0200463
464 info->roundtrip_needed = true;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200465}
466
467static void
468output_handle_geometry(void *data, struct wl_output *wl_output,
469 int32_t x, int32_t y,
470 int32_t physical_width, int32_t physical_height,
471 int32_t subpixel,
472 const char *make, const char *model,
473 int32_t output_transform)
474{
475 struct output_info *output = data;
476
477 output->geometry.x = x;
478 output->geometry.y = y;
479 output->geometry.physical_width = physical_width;
480 output->geometry.physical_height = physical_height;
481 output->geometry.subpixel = subpixel;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800482 output->geometry.make = xstrdup(make);
483 output->geometry.model = xstrdup(model);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200484 output->geometry.output_transform = output_transform;
485}
486
487static void
488output_handle_mode(void *data, struct wl_output *wl_output,
489 uint32_t flags, int32_t width, int32_t height,
490 int32_t refresh)
491{
492 struct output_info *output = data;
Kristian Høgsberg06b16c22013-08-15 14:20:53 -0700493 struct output_mode *mode = xmalloc(sizeof *mode);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200494
Philipp Brüschweiler97cb62a2012-08-15 21:57:24 +0200495 mode->flags = flags;
496 mode->width = width;
497 mode->height = height;
498 mode->refresh = refresh;
499
500 wl_list_insert(output->modes.prev, &mode->link);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200501}
502
Jonny Lambba9dca02015-12-17 12:31:56 +0000503static void
504output_handle_done(void *data, struct wl_output *wl_output)
505{
506 /* don't bother waiting for this; there's no good reason a
507 * compositor will wait more than one roundtrip before sending
508 * these initial events. */
509}
510
511static void
512output_handle_scale(void *data, struct wl_output *wl_output,
513 int32_t scale)
514{
515 struct output_info *output = data;
516
517 output->geometry.scale = scale;
518}
519
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200520static const struct wl_output_listener output_listener = {
521 output_handle_geometry,
522 output_handle_mode,
Jonny Lambba9dca02015-12-17 12:31:56 +0000523 output_handle_done,
524 output_handle_scale,
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200525};
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
Jonny Lambba9dca02015-12-17 12:31:56 +0000555 output->version = MIN(version, 2);
556 output->geometry.scale = 1;
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,
Jonny Lambba9dca02015-12-17 12:31:56 +0000560 &wl_output_interface, output->version);
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
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200572 wp_presentation_destroy(prinfo->presentation);
Pekka Paalanen93a6afd2014-09-23 22:08:44 -0400573}
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",
Derek Foreman32838c92015-06-29 13:20:34 -0500584#ifdef CLOCK_BOOTTIME
Pekka Paalanen93a6afd2014-09-23 22:08:44 -0400585 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
Derek Foreman32838c92015-06-29 13:20:34 -0500586#endif
Pekka Paalanen93a6afd2014-09-23 22:08:44 -0400587 };
588
589 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
590 return "unknown";
591
592 return names[clk_id];
593}
594
595static void
596print_presentation_info(void *info)
597{
598 struct presentation_info *prinfo = info;
599
600 print_global_info(info);
601
602 printf("\tpresentation clock id: %d (%s)\n",
603 prinfo->clk_id, clock_name(prinfo->clk_id));
604}
605
606static void
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200607presentation_handle_clock_id(void *data, struct wp_presentation *presentation,
Pekka Paalanen93a6afd2014-09-23 22:08:44 -0400608 uint32_t clk_id)
609{
610 struct presentation_info *prinfo = data;
611
612 prinfo->clk_id = clk_id;
613}
614
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200615static const struct wp_presentation_listener presentation_listener = {
Pekka Paalanen93a6afd2014-09-23 22:08:44 -0400616 presentation_handle_clock_id
617};
618
619static void
620add_presentation_info(struct weston_info *info, uint32_t id, uint32_t version)
621{
622 struct presentation_info *prinfo = xzalloc(sizeof *prinfo);
623
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200624 init_global_info(info, &prinfo->global, id,
625 wp_presentation_interface.name, version);
Pekka Paalanen93a6afd2014-09-23 22:08:44 -0400626 prinfo->global.print = print_presentation_info;
627 prinfo->global.destroy = destroy_presentation_info;
628
629 prinfo->clk_id = -1;
630 prinfo->presentation = wl_registry_bind(info->registry, id,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200631 &wp_presentation_interface, 1);
632 wp_presentation_add_listener(prinfo->presentation,
633 &presentation_listener, prinfo);
Pekka Paalanen93a6afd2014-09-23 22:08:44 -0400634
635 info->roundtrip_needed = true;
636}
637
638static void
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800639destroy_global_info(void *data)
640{
641}
642
643static void
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200644add_global_info(struct weston_info *info, uint32_t id,
645 const char *interface, uint32_t version)
646{
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800647 struct global_info *global = xzalloc(sizeof *global);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200648
649 init_global_info(info, global, id, interface, version);
650 global->print = print_global_info;
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800651 global->destroy = destroy_global_info;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200652}
653
654static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400655global_handler(void *data, struct wl_registry *registry, uint32_t id,
656 const char *interface, uint32_t version)
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200657{
658 struct weston_info *info = data;
659
660 if (!strcmp(interface, "wl_seat"))
661 add_seat_info(info, id, version);
662 else if (!strcmp(interface, "wl_shm"))
663 add_shm_info(info, id, version);
664 else if (!strcmp(interface, "wl_output"))
665 add_output_info(info, id, version);
Pekka Paalanen13a26e02016-04-19 16:16:29 +0300666 else if (!strcmp(interface, wp_presentation_interface.name))
Pekka Paalanen93a6afd2014-09-23 22:08:44 -0400667 add_presentation_info(info, id, version);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200668 else
669 add_global_info(info, id, interface, version);
670}
671
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200672static void
673global_remove_handler(void *data, struct wl_registry *registry, uint32_t name)
674{
675}
676
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400677static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200678 global_handler,
679 global_remove_handler
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400680};
681
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200682static void
683print_infos(struct wl_list *infos)
684{
685 struct global_info *info;
686
687 wl_list_for_each(info, infos, link)
688 info->print(info);
689}
690
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800691static void
692destroy_info(void *data)
693{
694 struct global_info *global = data;
695
696 global->destroy(data);
697 wl_list_remove(&global->link);
698 free(global->interface);
699 free(data);
700}
701
702static void
703destroy_infos(struct wl_list *infos)
704{
705 struct global_info *info, *tmp;
706 wl_list_for_each_safe(info, tmp, infos, link)
707 destroy_info(info);
708}
709
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200710int
711main(int argc, char **argv)
712{
713 struct weston_info info;
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200714
715 info.display = wl_display_connect(NULL);
716 if (!info.display) {
717 fprintf(stderr, "failed to create display: %m\n");
718 return -1;
719 }
720
721 wl_list_init(&info.infos);
722
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400723 info.registry = wl_display_get_registry(info.display);
724 wl_registry_add_listener(info.registry, &registry_listener, &info);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200725
Philipp Brüschweilerbb0d4b92012-08-15 21:57:23 +0200726 do {
727 info.roundtrip_needed = false;
728 wl_display_roundtrip(info.display);
729 } while (info.roundtrip_needed);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200730
731 print_infos(&info.infos);
U. Artie Eoff86c68b32014-01-15 13:02:28 -0800732 destroy_infos(&info.infos);
733
734 wl_registry_destroy(info.registry);
735 wl_display_disconnect(info.display);
Philipp Brüschweiler585acb02012-08-15 17:12:00 +0200736
737 return 0;
738}