2 Copyright 2006-2020 Chris Tallon
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP. If not, see <https://www.gnu.org/licenses/>.
29 const char* InputUDP::myModName = "InputUDP";
33 if (initted) return false;
35 log = Log::getInstance();
36 log->log("InputUDP", Log::DEBUG, "Starting InputUDP command server");
40 log->log("InputUDP", Log::DEBUG, "UDP4 init error");
46 quitPipe = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
47 if (quitPipe == INVALID_SOCKET)
49 Log::getInstance()->log("InputUDP", Log::ERR, "Win32 socket fail");
51 if (pipe2(pfds, O_NONBLOCK) == -1)
53 Log::getInstance()->log("InputUDP", Log::ERR, "pipe2() fail");
63 void InputUDP::shutdown()
66 CLOSESOCKET(quitPipe);
79 bool InputUDP::start()
81 threadStartProtect.lock(); // Make sure listenThread is fully initted before start returns
82 listenThread = std::thread( [this]
84 threadStartProtect.lock();
85 threadStartProtect.unlock();
88 threadStartProtect.unlock();
90 log->log("InputUDP", Log::DEBUG, "InputUDP command server started");
96 std::lock_guard<std::mutex> lg(threadStartProtect); // Also use it to protect against starting while stopping
100 if (listenThread.joinable())
103 Log::getInstance()->log("InputUDP", Log::DEBUG, "Calling CLOSESOCKET on WIN32 quitPipe");
105 CLOSESOCKET(quitPipe);
107 write(pfds[1], "1", 1); // break the select in listenLoop
113 void InputUDP::listenLoop()
119 retval = udp4.waitforMessage(3, quitPipe);
121 retval = udp4.waitforMessage(3, pfds[0]);
123 Log::getInstance()->log("InputUDP", Log::DEBUG, "Back from waitForMessage");
127 processRequest(udp4.getData(), udp4.getDataLength());
129 else if (retval == 3) // quit
131 Log::getInstance()->log("InputUDP", Log::DEBUG, "quit");
136 log->log("InputUDP", Log::CRIT, "Wait for packet error");
142 void InputUDP::processRequest(const void* data, UINT length)
144 log->log("InputUDP", Log::DEBUG, "Got request");
146 char* temp = new char[length + 1];
147 memcpy(temp, data, length);
149 UINT command = static_cast<UINT>(atoi(temp));
152 log->log("InputUDP", Log::DEBUG, "Command %i recieved", command);
153 sendInputKey(command);
156 const char* InputUDP::getHardCodedHardwareKeyNamesForVompKey(UCHAR /* vompKey */)
161 std::string InputUDP::getHardwareKeyName(HWC_TYPE /* hardwareKey */)