976181935a786d63162baf5263e027cc236a859d
[dcpomatic.git] / src / wx / cucumber_bridge.cc
1 /*
2     Copyright (C) 2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic 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.
10
11     DCP-o-matic 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.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "cucumber_bridge.h"
23 #include "lib/nanomsg.h"
24 #include "lib/dcpomatic_assert.h"
25 #include <boost/ref.hpp>
26 #include <iostream>
27
28
29 using std::string;
30 using boost::optional;
31 using boost::bind;
32
33
34 CucumberBridge::CucumberBridge ()
35 {
36
37 }
38
39
40 void
41 CucumberBridge::listener ()
42 {
43         Nanomsg nanomsg (false);
44         nanomsg.send (CUCUMBER_BRIDGE_READY "\n", -1);
45
46         while (true) {
47                 optional<string> message = nanomsg.receive (-1);
48                 if (!message) {
49                         continue;
50                 }
51
52                 if (*message == CUCUMBER_BRIDGE_CLICK_BUTTON) {
53                         emit (bind(boost::ref(ClickButton), get_parameter(nanomsg)));
54                 } else if (*message == CUCUMBER_BRIDGE_SELECT_MENU) {
55                         emit (bind(boost::ref(SelectMenu), get_parameter(nanomsg)));
56                 } else if (*message == CUCUMBER_BRIDGE_TYPE) {
57                         emit (bind(boost::ref(Type), get_parameter(nanomsg)));
58                 } else if (*message == CUCUMBER_BRIDGE_ADD_CONTENT_FILE) {
59                         emit (bind(boost::ref(AddContentFile), get_parameter(nanomsg)));
60                 } else if (*message == CUCUMBER_BRIDGE_GET_CONTENT_LIST) {
61                         nanomsg.send (GetContentList()->c_str(), -1);
62                 }
63         }
64 }
65
66
67 string
68 CucumberBridge::get_parameter (Nanomsg& nanomsg)
69 {
70         optional<string> p = nanomsg.receive (100);
71         DCPOMATIC_ASSERT (p);
72         return *p;
73 }
74
75
76 void
77 CucumberBridge::start ()
78 {
79         _thread = boost::thread(&CucumberBridge::listener, this);
80 }
81