gdk-screenshot.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2009, The Mozilla Foundation
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the Mozilla Foundation nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY The Mozilla Foundation ''AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL The Mozilla Foundation BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. * Contributors:
  28. * Ted Mielczarek <ted.mielczarek@gmail.com>
  29. * Karl Tomlinson <karlt+@karlt.net>
  30. */
  31. /*
  32. * gdk-screenshot.cpp: Save a screenshot of the root window in .png format.
  33. * If a filename is specified as the first argument on the commandline,
  34. * then the image will be saved to that filename. Otherwise, the image will
  35. * be written to stdout.
  36. */
  37. #include <gdk/gdk.h>
  38. #include <gdk/gdkx.h>
  39. #ifdef HAVE_LIBXSS
  40. #include <X11/extensions/scrnsaver.h>
  41. #endif
  42. #include <errno.h>
  43. #include <stdio.h>
  44. gboolean save_to_stdout(const gchar *buf, gsize count,
  45. GError **error, gpointer data)
  46. {
  47. size_t written = fwrite(buf, 1, count, stdout);
  48. if (written != count) {
  49. g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errno),
  50. "Write to stdout failed: %s", g_strerror(errno));
  51. return FALSE;
  52. }
  53. return TRUE;
  54. }
  55. int main(int argc, char** argv)
  56. {
  57. gdk_init(&argc, &argv);
  58. #if defined(HAVE_LIBXSS) && defined(MOZ_WIDGET_GTK)
  59. int event_base, error_base;
  60. Bool have_xscreensaver =
  61. XScreenSaverQueryExtension(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()),
  62. &event_base, &error_base);
  63. if (!have_xscreensaver) {
  64. fprintf(stderr, "No XScreenSaver extension on display\n");
  65. } else {
  66. XScreenSaverInfo* info = XScreenSaverAllocInfo();
  67. if (!info) {
  68. fprintf(stderr, "%s: Out of memory\n", argv[0]);
  69. return 1;
  70. }
  71. XScreenSaverQueryInfo(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()),
  72. GDK_ROOT_WINDOW(), info);
  73. const char* state;
  74. const char* til_or_since = nullptr;
  75. switch (info->state) {
  76. case ScreenSaverOff:
  77. state = "Off";
  78. til_or_since = "XScreenSaver will activate after another %lu seconds idle time\n";
  79. break;
  80. case ScreenSaverOn:
  81. state = "On";
  82. if (info->til_or_since) {
  83. til_or_since = "XScreenSaver idle timer activated %lu seconds ago\n";
  84. } else {
  85. til_or_since = "XScreenSaver idle activation is disabled\n";
  86. }
  87. break;
  88. case ScreenSaverDisabled:
  89. state = "Disabled";
  90. break;
  91. default:
  92. state = "unknown";
  93. }
  94. const char* kind;
  95. switch (info->kind) {
  96. case ScreenSaverBlanked:
  97. kind = "Blanked";
  98. break;
  99. case ScreenSaverInternal:
  100. state = "Internal";
  101. break;
  102. case ScreenSaverExternal:
  103. state = "External";
  104. break;
  105. default:
  106. state = "unknown";
  107. }
  108. fprintf(stderr, "XScreenSaver state: %s\n", state);
  109. if (til_or_since) {
  110. fprintf(stderr, "XScreenSaver kind: %s\n", kind);
  111. fprintf(stderr, til_or_since, info->til_or_since / 1000);
  112. }
  113. fprintf(stderr, "User input has been idle for %lu seconds\n", info->idle / 1000);
  114. XFree(info);
  115. }
  116. #endif
  117. GdkPixbuf* screenshot = nullptr;
  118. GdkWindow* window = gdk_get_default_root_window();
  119. #if (MOZ_WIDGET_GTK == 2)
  120. screenshot = gdk_pixbuf_get_from_drawable(nullptr, window, nullptr,
  121. 0, 0, 0, 0,
  122. gdk_screen_width(),
  123. gdk_screen_height());
  124. #else
  125. screenshot = gdk_pixbuf_get_from_window(window, 0, 0,
  126. gdk_window_get_width(window),
  127. gdk_window_get_height(window));
  128. #endif
  129. if (!screenshot) {
  130. fprintf(stderr, "%s: failed to create screenshot GdkPixbuf\n", argv[0]);
  131. return 1;
  132. }
  133. GError* error = nullptr;
  134. if (argc > 1) {
  135. gdk_pixbuf_save(screenshot, argv[1], "png", &error, nullptr);
  136. } else {
  137. gdk_pixbuf_save_to_callback(screenshot, save_to_stdout, nullptr,
  138. "png", &error, nullptr);
  139. }
  140. if (error) {
  141. fprintf(stderr, "%s: failed to write screenshot as png: %s\n",
  142. argv[0], error->message);
  143. return error->code;
  144. }
  145. return 0;
  146. }
  147. // These options are copied from mozglue/build/AsanOptions.cpp
  148. #ifdef MOZ_ASAN
  149. extern "C"
  150. const char* __asan_default_options() {
  151. return "allow_user_segv_handler=1:alloc_dealloc_mismatch=0:detect_leaks=0";
  152. }
  153. #endif