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