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