1 #include "ikvm_manager.hpp" 2 3 #include <thread> 4 5 namespace ikvm 6 { 7 8 Manager::Manager(const Args& args) : 9 continueExecuting(true), serverDone(false), videoDone(true), 10 input(args.getKeyboardPath(), args.getPointerPath(), args.getUdcName()), 11 video(args.getVideoPath(), input, args.getFrameRate(), 12 args.getSubsampling()), 13 server(args, input, video) 14 {} 15 16 void Manager::run() 17 { 18 std::thread run(serverThread, this); 19 20 while (continueExecuting) 21 { 22 if (server.wantsFrame()) 23 { 24 video.start(); 25 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 51 void Manager::serverThread(Manager* manager) 52 { 53 while (manager->continueExecuting) 54 { 55 manager->server.run(); 56 manager->setServerDone(); 57 manager->waitVideo(); 58 } 59 } 60 61 void Manager::setServerDone() 62 { 63 std::unique_lock<std::mutex> ulock(lock); 64 65 serverDone = true; 66 sync.notify_all(); 67 } 68 69 void Manager::setVideoDone() 70 { 71 std::unique_lock<std::mutex> ulock(lock); 72 73 videoDone = true; 74 sync.notify_all(); 75 } 76 77 void 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 89 void 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 102