GoogleGit

blob: bfe5ea058240dffec29826994a7a74e33a291783 [file] [log] [blame]
  1. /*
  2. * Copyright © 2008 Kristian Høgsberg
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and its
  5. * documentation for any purpose is hereby granted without fee, provided that
  6. * the above copyright notice appear in all copies and that both that copyright
  7. * notice and this permission notice appear in supporting documentation, and
  8. * that the name of the copyright holders not be used in advertising or
  9. * publicity pertaining to distribution of the software without specific,
  10. * written prior permission. The copyright holders make no representations
  11. * about the suitability of this software for any purpose. It is provided "as
  12. * is" without express or implied warranty.
  13. *
  14. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  19. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  20. * OF THIS SOFTWARE.
  21. */
  22. #include <stdint.h>
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <fcntl.h>
  27. #include <unistd.h>
  28. #include <sys/mman.h>
  29. #include <glib.h>
  30. #include <gdk-pixbuf/gdk-pixbuf.h>
  31. #include <wayland-client.h>
  32. #include "screenshooter-client-protocol.h"
  33. /* The screenshooter is a good example of a custom object exposed by
  34. * the compositor and serves as a test bed for implementing client
  35. * side marshalling outside libwayland.so */
  36. static struct wl_output *output;
  37. static struct wl_shm *shm;
  38. static struct screenshooter *screenshooter;
  39. static int output_width, output_height;
  40. static void
  41. display_handle_geometry(void *data,
  42. struct wl_output *wl_output,
  43. int x,
  44. int y,
  45. int physical_width,
  46. int physical_height,
  47. int subpixel,
  48. const char *make,
  49. const char *model)
  50. {
  51. }
  52. static void
  53. display_handle_mode(void *data,
  54. struct wl_output *wl_output,
  55. uint32_t flags,
  56. int width,
  57. int height,
  58. int refresh)
  59. {
  60. output_width = width;
  61. output_height = height;
  62. }
  63. static const struct wl_output_listener output_listener = {
  64. display_handle_geometry,
  65. display_handle_mode
  66. };
  67. static void
  68. handle_global(struct wl_display *display, uint32_t id,
  69. const char *interface, uint32_t version, void *data)
  70. {
  71. if (strcmp(interface, "wl_output") == 0) {
  72. output = wl_display_bind(display, id, &wl_output_interface);
  73. wl_output_add_listener(output, &output_listener, NULL);
  74. } else if (strcmp(interface, "wl_shm") == 0) {
  75. shm = wl_display_bind(display, id, &wl_shm_interface);
  76. } else if (strcmp(interface, "screenshooter") == 0) {
  77. screenshooter = wl_display_bind(display, id, &screenshooter_interface);
  78. }
  79. }
  80. static struct wl_buffer *
  81. create_shm_buffer(int width, int height, void **data_out)
  82. {
  83. char filename[] = "/tmp/wayland-shm-XXXXXX";
  84. struct wl_buffer *buffer;
  85. int fd, size, stride;
  86. void *data;
  87. fd = mkstemp(filename);
  88. if (fd < 0) {
  89. fprintf(stderr, "open %s failed: %m\n", filename);
  90. return NULL;
  91. }
  92. stride = width * 4;
  93. size = stride * height;
  94. if (ftruncate(fd, size) < 0) {
  95. fprintf(stderr, "ftruncate failed: %m\n");
  96. close(fd);
  97. return NULL;
  98. }
  99. data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  100. unlink(filename);
  101. if (data == MAP_FAILED) {
  102. fprintf(stderr, "mmap failed: %m\n");
  103. close(fd);
  104. return NULL;
  105. }
  106. buffer = wl_shm_create_buffer(shm, fd, width, height, stride,
  107. WL_SHM_FORMAT_XRGB32);
  108. close(fd);
  109. *data_out = data;
  110. return buffer;
  111. }
  112. static void
  113. write_png(int width, int height, void *data)
  114. {
  115. GdkPixbuf *pixbuf, *normal;
  116. GError *error = NULL;
  117. g_type_init();
  118. pixbuf = gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
  119. 8, width, height, width * 4, NULL,
  120. NULL);
  121. normal = gdk_pixbuf_flip(pixbuf, FALSE);
  122. gdk_pixbuf_save(normal, "wayland-screenshot.png", "png", &error, NULL);
  123. g_object_unref(normal);
  124. g_object_unref(pixbuf);
  125. }
  126. int main(int argc, char *argv[])
  127. {
  128. struct wl_display *display;
  129. struct wl_buffer *buffer;
  130. void *data = NULL;
  131. display = wl_display_connect(NULL);
  132. if (display == NULL) {
  133. fprintf(stderr, "failed to create display: %m\n");
  134. return -1;
  135. }
  136. wl_display_add_global_listener(display, handle_global, &screenshooter);
  137. wl_display_iterate(display, WL_DISPLAY_READABLE);
  138. wl_display_roundtrip(display);
  139. if (screenshooter == NULL) {
  140. fprintf(stderr, "display doesn't support screenshooter\n");
  141. return -1;
  142. }
  143. buffer = create_shm_buffer(output_width, output_height, &data);
  144. screenshooter_shoot(screenshooter, output, buffer);
  145. wl_display_roundtrip(display);
  146. write_png(output_width, output_height, data);
  147. return 0;
  148. }