blob: 2f66fd55dab683e0c28a49210dbd5768fbf73d80 [file] [log] [blame]
Eddie James18e1e462018-12-11 13:24:36 -06001#include "ikvm_manager.hpp"
2
3#include <thread>
4
5namespace ikvm
6{
Eddie James18e1e462018-12-11 13:24:36 -06007Manager::Manager(const Args& args) :
8 continueExecuting(true), serverDone(false), videoDone(true),
Marvin Linfe685fb2022-10-25 16:20:08 +08009 input(args.getKeyboardPath(), args.getPointerPath(), args.getUdcName()),
George Liuf79f6f52022-07-06 09:32:35 +080010 video(args.getVideoPath(), input, args.getFrameRate(),
11 args.getSubsampling()),
Eddie James18e1e462018-12-11 13:24:36 -060012 server(args, input, video)
George Liuf79f6f52022-07-06 09:32:35 +080013{}
Eddie James18e1e462018-12-11 13:24:36 -060014
15void Manager::run()
16{
17 std::thread run(serverThread, this);
18
19 while (continueExecuting)
20 {
21 if (server.wantsFrame())
22 {
Jae Hyun Yoo0049bfa2019-03-06 15:39:58 -080023 video.start();
Eddie James18e1e462018-12-11 13:24:36 -060024 video.getFrame();
25 server.sendFrame();
26 }
27 else
28 {
29 video.stop();
30 }
31
32 if (video.needsResize())
33 {
34 videoDone = false;
35 waitServer();
36 video.resize();
37 server.resize();
38 setVideoDone();
39 }
40 else
41 {
42 setVideoDone();
43 waitServer();
44 }
45 }
46
47 run.join();
48}
49
50void Manager::serverThread(Manager* manager)
51{
52 while (manager->continueExecuting)
53 {
54 manager->server.run();
55 manager->setServerDone();
56 manager->waitVideo();
57 }
58}
59
60void Manager::setServerDone()
61{
62 std::unique_lock<std::mutex> ulock(lock);
63
64 serverDone = true;
65 sync.notify_all();
66}
67
68void Manager::setVideoDone()
69{
70 std::unique_lock<std::mutex> ulock(lock);
71
72 videoDone = true;
73 sync.notify_all();
74}
75
76void 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
88void 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