1 #include "ikvm_manager.hpp"
2
3 #include <thread>
4
5 namespace ikvm
6 {
Manager(const Args & args)7 Manager::Manager(const Args& args) :
8 continueExecuting(true), serverDone(false), videoDone(true),
9 input(args.getKeyboardPath(), args.getPointerPath(), args.getUdcName()),
10 video(args.getVideoPath(), input, args.getFrameRate(),
11 args.getSubsampling()),
12 server(args, input, video)
13 {}
14
run()15 void Manager::run()
16 {
17 std::thread run(serverThread, this);
18
19 while (continueExecuting)
20 {
21 if (server.wantsFrame())
22 {
23 video.start();
24 video.getFrame();
25 server.sendFrame();
26 }
27 else
28 {
29 video.stop();
30 }
31
32 if (video.needsResize())
33 {
34 waitServer();
35 videoDone = false;
36 video.resize();
37 server.resize();
38 setVideoDone();
39 }
40 else
41 {
42 setVideoDone();
43 waitServer();
44 }
45 }
46
47 run.join();
48 }
49
serverThread(Manager * manager)50 void Manager::serverThread(Manager* manager)
51 {
52 while (manager->continueExecuting)
53 {
54 manager->server.run();
55 manager->setServerDone();
56 manager->waitVideo();
57 }
58 }
59
setServerDone()60 void Manager::setServerDone()
61 {
62 std::unique_lock<std::mutex> ulock(lock);
63
64 serverDone = true;
65 sync.notify_all();
66 }
67
setVideoDone()68 void Manager::setVideoDone()
69 {
70 std::unique_lock<std::mutex> ulock(lock);
71
72 videoDone = true;
73 sync.notify_all();
74 }
75
waitServer()76 void Manager::waitServer()
77 {
78 std::unique_lock<std::mutex> ulock(lock);
79
80 while (!serverDone)
81 {
82 sync.wait(ulock);
83 }
84
85 serverDone = false;
86 }
87
waitVideo()88 void Manager::waitVideo()
89 {
90 std::unique_lock<std::mutex> ulock(lock);
91
92 while (!videoDone)
93 {
94 sync.wait(ulock);
95 }
96
97 // don't reset videoDone
98 }
99
100 } // namespace ikvm
101