main_unix.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright 2016 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/core/SkTypes.h"
  8. #include "include/private/SkTHash.h"
  9. #include "tools/sk_app/Application.h"
  10. #include "tools/sk_app/unix/Window_unix.h"
  11. #include "tools/timer/Timer.h"
  12. using sk_app::Application;
  13. void finishWindow(sk_app::Window_unix* win) {
  14. win->finishResize();
  15. win->finishPaint();
  16. }
  17. int main(int argc, char**argv) {
  18. XInitThreads();
  19. Display* display = XOpenDisplay(nullptr);
  20. Application* app = Application::Create(argc, argv, (void*)display);
  21. // Get the file descriptor for the X display
  22. int x11_fd = ConnectionNumber(display);
  23. int count = x11_fd + 1;
  24. SkTHashSet<sk_app::Window_unix*> pendingWindows;
  25. bool done = false;
  26. while (!done) {
  27. // Create a file description set containing x11_fd
  28. fd_set in_fds;
  29. FD_ZERO(&in_fds);
  30. FD_SET(x11_fd, &in_fds);
  31. // Set a sleep timer
  32. struct timeval tv;
  33. tv.tv_usec = 100;
  34. tv.tv_sec = 0;
  35. while (!XPending(display)) {
  36. // Wait for an event on the file descriptor or for timer expiration
  37. (void) select(count, &in_fds, nullptr, nullptr, &tv);
  38. }
  39. // Handle XEvents (if any) and flush the input
  40. int count = XPending(display);
  41. while (count-- && !done) {
  42. XEvent event;
  43. XNextEvent(display, &event);
  44. sk_app::Window_unix* win = sk_app::Window_unix::gWindowMap.find(event.xany.window);
  45. if (!win) {
  46. continue;
  47. }
  48. // paint and resize events get collapsed
  49. switch (event.type) {
  50. case Expose:
  51. win->markPendingPaint();
  52. pendingWindows.add(win);
  53. break;
  54. case ConfigureNotify:
  55. win->markPendingResize(event.xconfigurerequest.width,
  56. event.xconfigurerequest.height);
  57. pendingWindows.add(win);
  58. break;
  59. default:
  60. if (win->handleEvent(event)) {
  61. done = true;
  62. }
  63. break;
  64. }
  65. }
  66. pendingWindows.foreach(finishWindow);
  67. if (pendingWindows.count() > 0) {
  68. app->onIdle();
  69. }
  70. pendingWindows.reset();
  71. XFlush(display);
  72. }
  73. delete app;
  74. XCloseDisplay(display);
  75. return 0;
  76. }