compositor-x11: Rename the output make to "weston-X11"
[platform/upstream/weston.git] / clients / weston-info.c
1 /*
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
23 #include "config.h"
24
25 #include <errno.h>
26 #include <stdbool.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <wayland-client.h>
32
33 #include "../shared/os-compatibility.h"
34
35 #define MIN(x,y) (((x) < (y)) ? (x) : (y))
36
37 typedef void (*print_info_t)(void *info);
38 typedef void (*destroy_info_t)(void *info);
39
40 struct global_info {
41         struct wl_list link;
42
43         uint32_t id;
44         uint32_t version;
45         char *interface;
46
47         print_info_t print;
48         destroy_info_t destroy;
49 };
50
51 struct output_mode {
52         struct wl_list link;
53
54         uint32_t flags;
55         int32_t width, height;
56         int32_t refresh;
57 };
58
59 struct output_info {
60         struct global_info global;
61
62         struct wl_output *output;
63
64         struct {
65                 int32_t x, y;
66                 int32_t physical_width, physical_height;
67                 enum wl_output_subpixel subpixel;
68                 enum wl_output_transform output_transform;
69                 char *make;
70                 char *model;
71         } geometry;
72
73         struct wl_list modes;
74 };
75
76 struct shm_format {
77         struct wl_list link;
78
79         uint32_t format;
80 };
81
82 struct shm_info {
83         struct global_info global;
84         struct wl_shm *shm;
85
86         struct wl_list formats;
87 };
88
89 struct seat_info {
90         struct global_info global;
91         struct wl_seat *seat;
92         struct weston_info *info;
93
94         uint32_t capabilities;
95         char *name;
96
97         int32_t repeat_rate;
98         int32_t repeat_delay;
99 };
100
101 struct weston_info {
102         struct wl_display *display;
103         struct wl_registry *registry;
104
105         struct wl_list infos;
106         bool roundtrip_needed;
107 };
108
109 static void *
110 fail_on_null(void *p)
111 {
112         if (p == NULL) {
113                 fprintf(stderr, "%s: out of memory\n", program_invocation_short_name);
114                 exit(EXIT_FAILURE);
115         }
116
117         return p;
118 }
119
120 static void *
121 xmalloc(size_t s)
122 {
123         return fail_on_null(malloc(s));
124 }
125
126 static void *
127 xzalloc(size_t s)
128 {
129         return fail_on_null(calloc(1, s));
130 }
131
132 static char *
133 xstrdup(const char *s)
134 {
135         return fail_on_null(strdup(s));
136 }
137
138 static void
139 print_global_info(void *data)
140 {
141         struct global_info *global = data;
142
143         printf("interface: '%s', version: %u, name: %u\n",
144                global->interface, global->version, global->id);
145 }
146
147 static void
148 init_global_info(struct weston_info *info,
149                  struct global_info *global, uint32_t id,
150                  const char *interface, uint32_t version)
151 {
152         global->id = id;
153         global->version = version;
154         global->interface = xstrdup(interface);
155
156         wl_list_insert(info->infos.prev, &global->link);
157 }
158
159 static void
160 print_output_info(void *data)
161 {
162         struct output_info *output = data;
163         struct output_mode *mode;
164         const char *subpixel_orientation;
165         const char *transform;
166
167         print_global_info(data);
168
169         switch (output->geometry.subpixel) {
170         case WL_OUTPUT_SUBPIXEL_UNKNOWN:
171                 subpixel_orientation = "unknown";
172                 break;
173         case WL_OUTPUT_SUBPIXEL_NONE:
174                 subpixel_orientation = "none";
175                 break;
176         case WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB:
177                 subpixel_orientation = "horizontal rgb";
178                 break;
179         case WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR:
180                 subpixel_orientation = "horizontal bgr";
181                 break;
182         case WL_OUTPUT_SUBPIXEL_VERTICAL_RGB:
183                 subpixel_orientation = "vertical rgb";
184                 break;
185         case WL_OUTPUT_SUBPIXEL_VERTICAL_BGR:
186                 subpixel_orientation = "vertical bgr";
187                 break;
188         default:
189                 fprintf(stderr, "unknown subpixel orientation %u\n",
190                         output->geometry.subpixel);
191                 subpixel_orientation = "unexpected value";
192                 break;
193         }
194
195         switch (output->geometry.output_transform) {
196         case WL_OUTPUT_TRANSFORM_NORMAL:
197                 transform = "normal";
198                 break;
199         case WL_OUTPUT_TRANSFORM_90:
200                 transform = "90°";
201                 break;
202         case WL_OUTPUT_TRANSFORM_180:
203                 transform = "180°";
204                 break;
205         case WL_OUTPUT_TRANSFORM_270:
206                 transform = "270°";
207                 break;
208         case WL_OUTPUT_TRANSFORM_FLIPPED:
209                 transform = "flipped";
210                 break;
211         case WL_OUTPUT_TRANSFORM_FLIPPED_90:
212                 transform = "flipped 90°";
213                 break;
214         case WL_OUTPUT_TRANSFORM_FLIPPED_180:
215                 transform = "flipped 180°";
216                 break;
217         case WL_OUTPUT_TRANSFORM_FLIPPED_270:
218                 transform = "flipped 270°";
219                 break;
220         default:
221                 fprintf(stderr, "unknown output transform %u\n",
222                         output->geometry.output_transform);
223                 transform = "unexpected value";
224                 break;
225         }
226
227         printf("\tx: %d, y: %d,\n",
228                output->geometry.x, output->geometry.y);
229         printf("\tphysical_width: %d mm, physical_height: %d mm,\n",
230                output->geometry.physical_width,
231                output->geometry.physical_height);
232         printf("\tmake: '%s', model: '%s',\n",
233                output->geometry.make, output->geometry.model);
234         printf("\tsubpixel_orientation: %s, output_transform: %s,\n",
235                subpixel_orientation, transform);
236
237         wl_list_for_each(mode, &output->modes, link) {
238                 printf("\tmode:\n");
239
240                 printf("\t\twidth: %d px, height: %d px, refresh: %.f Hz,\n",
241                        mode->width, mode->height,
242                        (float) mode->refresh / 1000);
243
244                 printf("\t\tflags:");
245                 if (mode->flags & WL_OUTPUT_MODE_CURRENT)
246                         printf(" current");
247                 if (mode->flags & WL_OUTPUT_MODE_PREFERRED)
248                         printf(" preferred");
249                 printf("\n");
250         }
251 }
252
253 static void
254 print_shm_info(void *data)
255 {
256         struct shm_info *shm = data;
257         struct shm_format *format;
258
259         print_global_info(data);
260
261         printf("\tformats:");
262
263         wl_list_for_each(format, &shm->formats, link)
264                 switch (format->format) {
265                 case WL_SHM_FORMAT_ARGB8888:
266                         printf(" ARGB8888");
267                         break;
268                 case WL_SHM_FORMAT_XRGB8888:
269                         printf(" XRGB8888");
270                         break;
271                 case WL_SHM_FORMAT_RGB565:
272                         printf(" RGB565");
273                         break;
274                 default:
275                         printf(" unknown(%08x)", format->format);
276                         break;
277                 }
278
279         printf("\n");
280 }
281
282 static void
283 print_seat_info(void *data)
284 {
285         struct seat_info *seat = data;
286
287         print_global_info(data);
288
289         printf("\tname: %s\n", seat->name);
290         printf("\tcapabilities:");
291
292         if (seat->capabilities & WL_SEAT_CAPABILITY_POINTER)
293                 printf(" pointer");
294         if (seat->capabilities & WL_SEAT_CAPABILITY_KEYBOARD)
295                 printf(" keyboard");
296         if (seat->capabilities & WL_SEAT_CAPABILITY_TOUCH)
297                 printf(" touch");
298
299         printf("\n");
300
301         if (seat->repeat_rate > 0)
302                 printf("\tkeyboard repeat rate: %d\n", seat->repeat_rate);
303         if (seat->repeat_delay > 0)
304                 printf("\tkeyboard repeat delay: %d\n", seat->repeat_delay);
305 }
306
307 static void
308 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
309                        uint32_t format, int fd, uint32_t size)
310 {
311 }
312
313 static void
314 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
315                       uint32_t serial, struct wl_surface *surface,
316                       struct wl_array *keys)
317 {
318 }
319
320 static void
321 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
322                       uint32_t serial, struct wl_surface *surface)
323 {
324 }
325
326 static void
327 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
328                     uint32_t serial, uint32_t time, uint32_t key,
329                     uint32_t state)
330 {
331 }
332
333 static void
334 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
335                           uint32_t serial, uint32_t mods_depressed,
336                           uint32_t mods_latched, uint32_t mods_locked,
337                           uint32_t group)
338 {
339 }
340
341 static void
342 keyboard_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
343                             int32_t rate, int32_t delay)
344 {
345         struct seat_info *seat = data;
346
347         seat->repeat_rate = rate;
348         seat->repeat_delay = delay;
349 }
350
351 static const struct wl_keyboard_listener keyboard_listener = {
352         keyboard_handle_keymap,
353         keyboard_handle_enter,
354         keyboard_handle_leave,
355         keyboard_handle_key,
356         keyboard_handle_modifiers,
357         keyboard_handle_repeat_info,
358 };
359
360 static void
361 seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
362                          enum wl_seat_capability caps)
363 {
364         struct seat_info *seat = data;
365
366         seat->capabilities = caps;
367
368         /* we want listen for repeat_info from wl_keyboard, but only
369          * do so if the seat info is >= 4 and if we actually have a
370          * keyboard */
371         if (seat->global.version < 4)
372                 return;
373
374         if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
375                 struct wl_keyboard *keyboard;
376
377                 keyboard = wl_seat_get_keyboard(seat->seat);
378                 wl_keyboard_add_listener(keyboard, &keyboard_listener,
379                                          seat);
380
381                 seat->info->roundtrip_needed = true;
382         }
383 }
384
385 static void
386 seat_handle_name(void *data, struct wl_seat *wl_seat,
387                  const char *name)
388 {
389         struct seat_info *seat = data;
390         seat->name = xstrdup(name);
391 }
392
393 static const struct wl_seat_listener seat_listener = {
394         seat_handle_capabilities,
395         seat_handle_name,
396 };
397
398 static void
399 destroy_seat_info(void *data)
400 {
401         struct seat_info *seat = data;
402
403         wl_seat_destroy(seat->seat);
404
405         if (seat->name != NULL)
406                 free(seat->name);
407 }
408
409 static void
410 add_seat_info(struct weston_info *info, uint32_t id, uint32_t version)
411 {
412         struct seat_info *seat = xzalloc(sizeof *seat);
413
414         /* required to set roundtrip_needed to true in capabilities
415          * handler */
416         seat->info = info;
417
418         init_global_info(info, &seat->global, id, "wl_seat", version);
419         seat->global.print = print_seat_info;
420         seat->global.destroy = destroy_seat_info;
421
422         seat->seat = wl_registry_bind(info->registry,
423                                       id, &wl_seat_interface, MIN(version, 4));
424         wl_seat_add_listener(seat->seat, &seat_listener, seat);
425
426         seat->repeat_rate = seat->repeat_delay = -1;
427
428         info->roundtrip_needed = true;
429 }
430
431 static void
432 shm_handle_format(void *data, struct wl_shm *wl_shm, uint32_t format)
433 {
434         struct shm_info *shm = data;
435         struct shm_format *shm_format = xzalloc(sizeof *shm_format);
436
437         wl_list_insert(&shm->formats, &shm_format->link);
438         shm_format->format = format;
439 }
440
441 static const struct wl_shm_listener shm_listener = {
442         shm_handle_format,
443 };
444
445 static void
446 destroy_shm_info(void *data)
447 {
448         struct shm_info *shm = data;
449         struct shm_format *format, *tmp;
450
451         wl_list_for_each_safe(format, tmp, &shm->formats, link) {
452                 wl_list_remove(&format->link);
453                 free(format);
454         }
455
456         wl_shm_destroy(shm->shm);
457 }
458
459 static void
460 add_shm_info(struct weston_info *info, uint32_t id, uint32_t version)
461 {
462         struct shm_info *shm = xzalloc(sizeof *shm);
463
464         init_global_info(info, &shm->global, id, "wl_shm", version);
465         shm->global.print = print_shm_info;
466         shm->global.destroy = destroy_shm_info;
467
468         wl_list_init(&shm->formats);
469
470         shm->shm = wl_registry_bind(info->registry,
471                                     id, &wl_shm_interface, 1);
472         wl_shm_add_listener(shm->shm, &shm_listener, shm);
473
474         info->roundtrip_needed = true;
475 }
476
477 static void
478 output_handle_geometry(void *data, struct wl_output *wl_output,
479                        int32_t x, int32_t y,
480                        int32_t physical_width, int32_t physical_height,
481                        int32_t subpixel,
482                        const char *make, const char *model,
483                        int32_t output_transform)
484 {
485         struct output_info *output = data;
486
487         output->geometry.x = x;
488         output->geometry.y = y;
489         output->geometry.physical_width = physical_width;
490         output->geometry.physical_height = physical_height;
491         output->geometry.subpixel = subpixel;
492         output->geometry.make = xstrdup(make);
493         output->geometry.model = xstrdup(model);
494         output->geometry.output_transform = output_transform;
495 }
496
497 static void
498 output_handle_mode(void *data, struct wl_output *wl_output,
499                    uint32_t flags, int32_t width, int32_t height,
500                    int32_t refresh)
501 {
502         struct output_info *output = data;
503         struct output_mode *mode = xmalloc(sizeof *mode);
504
505         mode->flags = flags;
506         mode->width = width;
507         mode->height = height;
508         mode->refresh = refresh;
509
510         wl_list_insert(output->modes.prev, &mode->link);
511 }
512
513 static const struct wl_output_listener output_listener = {
514         output_handle_geometry,
515         output_handle_mode,
516 };
517
518 static void
519 destroy_output_info(void *data)
520 {
521         struct output_info *output = data;
522         struct output_mode *mode, *tmp;
523
524         wl_output_destroy(output->output);
525
526         if (output->geometry.make != NULL)
527                 free(output->geometry.make);
528         if (output->geometry.model != NULL)
529                 free(output->geometry.model);
530
531         wl_list_for_each_safe(mode, tmp, &output->modes, link) {
532                 wl_list_remove(&mode->link);
533                 free(mode);
534         }
535 }
536
537 static void
538 add_output_info(struct weston_info *info, uint32_t id, uint32_t version)
539 {
540         struct output_info *output = xzalloc(sizeof *output);
541
542         init_global_info(info, &output->global, id, "wl_output", version);
543         output->global.print = print_output_info;
544         output->global.destroy = destroy_output_info;
545
546         wl_list_init(&output->modes);
547
548         output->output = wl_registry_bind(info->registry, id,
549                                           &wl_output_interface, 1);
550         wl_output_add_listener(output->output, &output_listener,
551                                output);
552
553         info->roundtrip_needed = true;
554 }
555
556 static void
557 destroy_global_info(void *data)
558 {
559 }
560
561 static void
562 add_global_info(struct weston_info *info, uint32_t id,
563                 const char *interface, uint32_t version)
564 {
565         struct global_info *global = xzalloc(sizeof *global);
566
567         init_global_info(info, global, id, interface, version);
568         global->print = print_global_info;
569         global->destroy = destroy_global_info;
570 }
571
572 static void
573 global_handler(void *data, struct wl_registry *registry, uint32_t id,
574                const char *interface, uint32_t version)
575 {
576         struct weston_info *info = data;
577
578         if (!strcmp(interface, "wl_seat"))
579                 add_seat_info(info, id, version);
580         else if (!strcmp(interface, "wl_shm"))
581                 add_shm_info(info, id, version);
582         else if (!strcmp(interface, "wl_output"))
583                 add_output_info(info, id, version);
584         else
585                 add_global_info(info, id, interface, version);
586 }
587
588 static void
589 global_remove_handler(void *data, struct wl_registry *registry, uint32_t name)
590 {
591 }
592
593 static const struct wl_registry_listener registry_listener = {
594         global_handler,
595         global_remove_handler
596 };
597
598 static void
599 print_infos(struct wl_list *infos)
600 {
601         struct global_info *info;
602
603         wl_list_for_each(info, infos, link)
604                 info->print(info);
605 }
606
607 static void
608 destroy_info(void *data)
609 {
610         struct global_info *global = data;
611
612         global->destroy(data);
613         wl_list_remove(&global->link);
614         free(global->interface);
615         free(data);
616 }
617
618 static void
619 destroy_infos(struct wl_list *infos)
620 {
621         struct global_info *info, *tmp;
622         wl_list_for_each_safe(info, tmp, infos, link)
623                 destroy_info(info);
624 }
625
626 int
627 main(int argc, char **argv)
628 {
629         struct weston_info info;
630
631         info.display = wl_display_connect(NULL);
632         if (!info.display) {
633                 fprintf(stderr, "failed to create display: %m\n");
634                 return -1;
635         }
636
637         wl_list_init(&info.infos);
638
639         info.registry = wl_display_get_registry(info.display);
640         wl_registry_add_listener(info.registry, &registry_listener, &info);
641
642         do {
643                 info.roundtrip_needed = false;
644                 wl_display_roundtrip(info.display);
645         } while (info.roundtrip_needed);
646
647         print_infos(&info.infos);
648         destroy_infos(&info.infos);
649
650         wl_registry_destroy(info.registry);
651         wl_display_disconnect(info.display);
652
653         return 0;
654 }