test-compositor.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2014 Red Hat, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the
  13. * next paragraph) shall be included in all copies or substantial
  14. * portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  20. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  21. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. */
  25. #include <stdint.h>
  26. #include <unistd.h>
  27. #include <stdatomic.h>
  28. #include "wayland-server.h"
  29. #include "wayland-client.h"
  30. /* info about a client on server side */
  31. struct client_info {
  32. struct display *display;
  33. struct wl_client *wl_client;
  34. struct wl_listener destroy_listener;
  35. const char *name; /* for debugging */
  36. int pipe;
  37. pid_t pid;
  38. int exit_code;
  39. int kill_code;
  40. struct wl_list link;
  41. void *data; /* for arbitrary use */
  42. int log_fd;
  43. };
  44. struct display {
  45. struct wl_display *wl_display;
  46. struct wl_global *test_global;
  47. struct wl_list clients;
  48. uint32_t clients_no;
  49. uint32_t clients_terminated_no;
  50. /* list of clients waiting for display_resumed event */
  51. struct wl_list waiting_for_resume;
  52. uint32_t wfr_num;
  53. };
  54. /* This is a helper structure for clients.
  55. * Instead of calling wl_display_connect() and all the other stuff,
  56. * client can use client_connect and it will return this structure
  57. * filled. */
  58. struct client {
  59. struct wl_display *wl_display;
  60. struct wl_fixes *wl_fixes;
  61. struct test_compositor *tc;
  62. atomic_bool display_stopped;
  63. };
  64. struct client *client_connect(void);
  65. void client_disconnect(struct client *);
  66. int stop_display(struct client *, int);
  67. void noop_request(struct client *);
  68. /**
  69. * Usual workflow:
  70. *
  71. * d = display_create();
  72. *
  73. * wl_global_create(d->wl_display, ...);
  74. * ... other setups ...
  75. *
  76. * client_create(d, client_main, data);
  77. * client_create(d, client_main2, data);
  78. *
  79. * display_run(d);
  80. * display_destroy(d);
  81. */
  82. struct display *display_create(void);
  83. void display_destroy(struct display *d);
  84. void display_destroy_expect_signal(struct display *d, int signum);
  85. void display_run(struct display *d);
  86. /* This function posts the display_resumed event to all waiting clients,
  87. * so that after flushing events the clients will stop waiting and continue.
  88. *
  89. * (Calling `display_run` after this function will resume the display loop.)
  90. */
  91. void display_post_resume_events(struct display *d);
  92. /* After n clients called stop_display(..., n), the display
  93. * is stopped and can process the code after display_run().
  94. *
  95. * This function posts the display_resumed event to the waiting
  96. * clients, so that the clients will stop waiting and continue;
  97. * it then reruns the display. */
  98. void display_resume(struct display *d);
  99. /* The file descriptor containing the client log. This is only valid in the
  100. * test client processes. */
  101. extern int client_log_fd;
  102. struct client_info *client_create_with_name(struct display *d,
  103. void (*client_main)(void *data),
  104. void *data,
  105. const char *name);
  106. #define client_create(d, c, data) client_create_with_name((d), (c), data, (#c))
  107. static inline void noarg_cb(void *data)
  108. {
  109. void (*cb)(void) = data;
  110. cb();
  111. }
  112. static inline struct client_info *client_create_with_name_noarg(struct display *d,
  113. void (*client_main)(void),
  114. const char *name)
  115. {
  116. return client_create_with_name(d, noarg_cb, client_main, name);
  117. }
  118. #define client_create_noarg(d, c) \
  119. client_create_with_name_noarg((d), (c), (#c))