openmsx-control-stdio.cc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /**
  2. * Example implementation for bidirectional communication with openMSX.
  3. *
  4. * requires: libxml2
  5. * compile: g++ `xml2-config --cflags` `xml2-config --libs` openmsx-control.cc
  6. */
  7. #include <string>
  8. #include <list>
  9. #include <iostream>
  10. #include <unistd.h>
  11. #include <sys/types.h>
  12. #include <sys/select.h>
  13. #include <libxml/parser.h>
  14. using std::cout;
  15. using std::endl;
  16. using std::list;
  17. using std::string;
  18. class OpenMSXComm
  19. {
  20. public:
  21. // main loop
  22. void start();
  23. // send a command to openmsx
  24. void sendCommand(const string& command);
  25. private:
  26. // therse methods get called when openMSX has send the corresponding tag
  27. void openmsx_cmd_ok(const string& msg);
  28. void openmsx_cmd_nok(const string& msg);
  29. void openmsx_cmd_info(const string& msg);
  30. void openmsx_cmd_warning(const string& msg);
  31. void openmsx_cmd_update(const string& name, const string& value);
  32. // XML parsing call-back functions
  33. static void cb_start_element(OpenMSXComm* comm, const xmlChar* name,
  34. const xmlChar** attrs);
  35. static void cb_end_element(OpenMSXComm* comm, const xmlChar* name);
  36. static void cb_text(OpenMSXComm* comm, const xmlChar* chars, int len);
  37. void parseReply(const char** attrs);
  38. void parseLog(const char** attrs);
  39. void parseUpdate(const char** attrs);
  40. void doReply();
  41. void doLog();
  42. void doUpdate();
  43. void deprecated();
  44. // commands being executed
  45. list<string> commandStack;
  46. // XML parsing
  47. enum State {
  48. START,
  49. TAG_OPENMSX,
  50. TAG_REPLY,
  51. TAG_LOG,
  52. TAG_UPDATE,
  53. TAG_OK,
  54. TAG_NOK,
  55. TAG_INFO,
  56. TAG_WARNING,
  57. } state;
  58. unsigned unknownLevel;
  59. string content;
  60. xmlSAXHandler sax_handler;
  61. xmlParserCtxt* parser_context;
  62. enum ReplyStatus {
  63. REPLY_UNKNOWN,
  64. REPLY_OK,
  65. REPLY_NOK
  66. } replyStatus;
  67. enum LogLevel {
  68. LOG_UNKNOWN,
  69. LOG_INFO,
  70. LOG_WARNING
  71. } logLevel;
  72. enum UpdateType {
  73. UPDATE_UNKNOWN,
  74. UPDATE_LED
  75. } updateType;
  76. string updateName;
  77. // communication with openmsx process
  78. int fd_out;
  79. };
  80. void OpenMSXComm::openmsx_cmd_ok(const string& msg)
  81. {
  82. const string& command = commandStack.front();
  83. cout << "CMD: '" << command << "' executed" << endl;
  84. if (!msg.empty()) {
  85. cout << msg << endl;
  86. }
  87. commandStack.pop_front();
  88. }
  89. void OpenMSXComm::openmsx_cmd_nok(const string& msg)
  90. {
  91. const string& command = commandStack.front();
  92. cout << "CMD: '" << command << "' failed" << endl;
  93. if (!msg.empty()) {
  94. cout << msg << endl;
  95. }
  96. commandStack.pop_front();
  97. }
  98. void OpenMSXComm::openmsx_cmd_info(const string& msg)
  99. {
  100. cout << "INFO: " << msg << endl;
  101. }
  102. void OpenMSXComm::openmsx_cmd_warning(const string& msg)
  103. {
  104. cout << "WARNING: " << msg << endl;
  105. }
  106. void OpenMSXComm::openmsx_cmd_update(const string& name, const string& value)
  107. {
  108. cout << "UPDATE: " << name << " " << value << endl;
  109. }
  110. void OpenMSXComm::deprecated()
  111. {
  112. static bool alreadyPrinted = false;
  113. if (!alreadyPrinted) {
  114. alreadyPrinted = true;
  115. cout << "The openMSX you're running still uses the old communication protocol." << endl
  116. << "Because of this some stuff will not work (LED status for example)." << endl
  117. << "Please upgrade to openMSX 0.4.0 or higher." << endl;
  118. }
  119. }
  120. void OpenMSXComm::cb_start_element(OpenMSXComm* comm, const xmlChar* name,
  121. const xmlChar** attrs)
  122. {
  123. if (comm->unknownLevel) {
  124. ++(comm->unknownLevel);
  125. return;
  126. }
  127. switch (comm->state) {
  128. case START:
  129. if (strcmp((const char*)name, "openmsx-output") == 0) {
  130. comm->state = TAG_OPENMSX;
  131. } else {
  132. ++(comm->unknownLevel);
  133. }
  134. break;
  135. case TAG_OPENMSX:
  136. if (strcmp((const char*)name, "reply") == 0) {
  137. comm->state = TAG_REPLY;
  138. comm->parseReply((const char**)attrs);
  139. } else if (strcmp((const char*)name, "log") == 0) {
  140. comm->state = TAG_LOG;
  141. comm->parseLog((const char**)attrs);
  142. } else if (strcmp((const char*)name, "update") == 0) {
  143. comm->state = TAG_UPDATE;
  144. comm->parseUpdate((const char**)attrs);
  145. } // backwards compatibilty stuff
  146. else if (strcmp((const char*)name, "ok") == 0) {
  147. comm->state = TAG_OK;
  148. comm->replyStatus = REPLY_OK;
  149. comm->deprecated();
  150. } else if (strcmp((const char*)name, "nok") == 0) {
  151. comm->state = TAG_NOK;
  152. comm->replyStatus = REPLY_NOK;
  153. comm->deprecated();
  154. } else if (strcmp((const char*)name, "info") == 0) {
  155. comm->state = TAG_INFO;
  156. comm->logLevel = LOG_INFO;
  157. comm->deprecated();
  158. } else if (strcmp((const char*)name, "warning") == 0) {
  159. comm->state = TAG_WARNING;
  160. comm->logLevel = LOG_WARNING;
  161. comm->deprecated();
  162. } else {
  163. ++(comm->unknownLevel);
  164. }
  165. break;
  166. default:
  167. ++(comm->unknownLevel);
  168. break;
  169. }
  170. comm->content.clear();
  171. }
  172. void OpenMSXComm::parseReply(const char** attrs)
  173. {
  174. replyStatus = REPLY_UNKNOWN;
  175. if (attrs) {
  176. for ( ; *attrs; attrs += 2) {
  177. if (strcmp(attrs[0], "result") == 0) {
  178. if (strcmp(attrs[1], "ok") == 0) {
  179. replyStatus = REPLY_OK;
  180. } else if (strcmp(attrs[1], "nok") == 0) {
  181. replyStatus = REPLY_NOK;
  182. }
  183. }
  184. }
  185. }
  186. }
  187. void OpenMSXComm::parseLog(const char** attrs)
  188. {
  189. logLevel = LOG_UNKNOWN;
  190. if (attrs) {
  191. for ( ; *attrs; attrs += 2) {
  192. if (strcmp(attrs[0], "level") == 0) {
  193. if (strcmp(attrs[1], "info") == 0) {
  194. logLevel = LOG_INFO;
  195. } else if (strcmp(attrs[1], "warning") == 0) {
  196. logLevel = LOG_WARNING;
  197. }
  198. }
  199. }
  200. }
  201. }
  202. void OpenMSXComm::parseUpdate(const char** attrs)
  203. {
  204. updateType = UPDATE_UNKNOWN;
  205. if (attrs) {
  206. for ( ; *attrs; attrs += 2) {
  207. if (strcmp(attrs[0], "type") == 0) {
  208. if (strcmp(attrs[1], "led") == 0) {
  209. updateType = UPDATE_LED;
  210. }
  211. } else if (strcmp(attrs[0], "name") == 0) {
  212. updateName = attrs[1];
  213. }
  214. }
  215. }
  216. }
  217. void OpenMSXComm::cb_end_element(OpenMSXComm* comm, const xmlChar* name)
  218. {
  219. if (comm->unknownLevel) {
  220. --(comm->unknownLevel);
  221. return;
  222. }
  223. switch (comm->state) {
  224. case TAG_OPENMSX:
  225. comm->state = START;
  226. break;
  227. case TAG_REPLY:
  228. comm->doReply();
  229. comm->state = TAG_OPENMSX;
  230. break;
  231. case TAG_LOG:
  232. comm->doLog();
  233. comm->state = TAG_OPENMSX;
  234. break;
  235. case TAG_UPDATE:
  236. comm->doUpdate();
  237. comm->state = TAG_OPENMSX;
  238. break;
  239. // backwards compatibilty stuff
  240. case TAG_OK:
  241. comm->openmsx_cmd_ok(comm->content);
  242. comm->state = TAG_OPENMSX;
  243. break;
  244. case TAG_NOK:
  245. comm->openmsx_cmd_nok(comm->content);
  246. comm->state = TAG_OPENMSX;
  247. break;
  248. case TAG_INFO:
  249. comm->openmsx_cmd_info(comm->content);
  250. comm->state = TAG_OPENMSX;
  251. break;
  252. case TAG_WARNING:
  253. comm->openmsx_cmd_warning(comm->content);
  254. comm->state = TAG_OPENMSX;
  255. break;
  256. default:
  257. break;
  258. }
  259. }
  260. void OpenMSXComm::doReply()
  261. {
  262. switch (replyStatus) {
  263. case REPLY_OK:
  264. openmsx_cmd_ok(content);
  265. break;
  266. case REPLY_NOK:
  267. openmsx_cmd_nok(content);
  268. break;
  269. }
  270. }
  271. void OpenMSXComm::doLog()
  272. {
  273. switch (logLevel) {
  274. case LOG_INFO:
  275. openmsx_cmd_info(content);
  276. break;
  277. case LOG_WARNING:
  278. openmsx_cmd_warning(content);
  279. break;
  280. }
  281. }
  282. void OpenMSXComm::doUpdate()
  283. {
  284. switch (updateType) {
  285. case UPDATE_LED:
  286. openmsx_cmd_update(updateName, content);
  287. break;
  288. }
  289. }
  290. void OpenMSXComm::cb_text(OpenMSXComm* comm, const xmlChar* chars, int len)
  291. {
  292. switch (comm->state) {
  293. case TAG_REPLY:
  294. case TAG_LOG:
  295. case TAG_UPDATE:
  296. case TAG_OK:
  297. case TAG_NOK:
  298. case TAG_INFO:
  299. case TAG_WARNING:
  300. comm->content.append((const char*)chars, len);
  301. break;
  302. default:
  303. break;
  304. }
  305. }
  306. void OpenMSXComm::sendCommand(const string& command)
  307. {
  308. write(fd_out, "<command>", 9);
  309. write(fd_out, command.c_str(), command.length());
  310. write(fd_out, "</command>", 10);
  311. commandStack.push_back(command);
  312. }
  313. void OpenMSXComm::start()
  314. {
  315. // init XML parser
  316. state = START;
  317. unknownLevel = 0;
  318. memset(&sax_handler, 0, sizeof(sax_handler));
  319. sax_handler.startElement = (startElementSAXFunc)cb_start_element;
  320. sax_handler.endElement = (endElementSAXFunc) cb_end_element;
  321. sax_handler.characters = (charactersSAXFunc) cb_text;
  322. parser_context = xmlCreatePushParserCtxt(&sax_handler, this, 0, 0, 0);
  323. // create pipes
  324. int pipe_to_child[2];
  325. int pipe_from_child[2];
  326. pipe(pipe_to_child);
  327. pipe(pipe_from_child);
  328. fd_out = pipe_to_child[1];
  329. // start openmsx sub-process
  330. pid_t pid = fork();
  331. if (pid == 0) {
  332. dup2(pipe_to_child[0], STDIN_FILENO);
  333. dup2(pipe_from_child[1], STDOUT_FILENO);
  334. close(pipe_to_child[0]);
  335. close(pipe_to_child[1]);
  336. close(pipe_from_child[0]);
  337. close(pipe_from_child[1]);
  338. execlp("openmsx", "openmsx", "-control", "stdio:", 0);
  339. exit(0);
  340. }
  341. close(pipe_to_child[0]);
  342. close(pipe_from_child[1]);
  343. // send initial commands
  344. write(pipe_to_child[1], "<openmsx-control>", 17);
  345. sendCommand("set power on");
  346. sendCommand("restoredefault renderer");
  347. // event loop
  348. string command; // (partial) input from STDIN
  349. while (true) {
  350. char buf[4096];
  351. fd_set rdfs;
  352. FD_ZERO(&rdfs);
  353. FD_SET(pipe_from_child[0], &rdfs);
  354. FD_SET(STDIN_FILENO, &rdfs);
  355. select(pipe_from_child[0] + 1, &rdfs, NULL, NULL, NULL);
  356. if (FD_ISSET(pipe_from_child[0], &rdfs)) {
  357. // data available from openMSX
  358. ssize_t size = read(pipe_from_child[0], buf, 4096);
  359. if (size == 0) {
  360. // openmsx process died
  361. break;
  362. }
  363. xmlParseChunk(parser_context, buf, size, 0);
  364. }
  365. if (FD_ISSET(STDIN_FILENO, &rdfs)) {
  366. // data available from STDIN
  367. ssize_t size = read(STDIN_FILENO, buf, 4096);
  368. char* oldpos = buf;
  369. while (true) {
  370. char* pos = (char*)memchr(oldpos, '\n', size);
  371. if (pos) {
  372. unsigned num = pos - oldpos;
  373. command.append(oldpos, num);
  374. sendCommand(command);
  375. command.clear();
  376. oldpos = pos + 1;
  377. size -= num + 1;
  378. } else {
  379. command.append(pos, size);
  380. break;
  381. }
  382. }
  383. }
  384. }
  385. // cleanup
  386. xmlFreeParserCtxt(parser_context);
  387. }
  388. int main()
  389. {
  390. OpenMSXComm comm;
  391. comm.start();
  392. return 0;
  393. }