summaryrefslogtreecommitdiff
path: root/src/wx/cucumber_bridge.cc
blob: d392b5554c76f3d358e551fdf2743df26f1b6f03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
    Copyright (C) 2020 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DCP-o-matic is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/


#include "cucumber_bridge.h"
#include "lib/nanomsg.h"
#include "lib/dcpomatic_assert.h"
#include <dcp/raw_convert.h>
#include <boost/ref.hpp>
#include <iostream>


using std::string;
using boost::optional;
using boost::bind;


CucumberBridge::CucumberBridge()
	: _nanomsg(true, "cucumber")
{

}


void
CucumberBridge::listener()
{
	if (!_nanomsg.send(CUCUMBER_BRIDGE_READY "\n", 200)) {
		std::cerr << "Could not say hello to cucumber server.\n";
	}

	while (true) {
		auto id = _nanomsg.receive(-1);
		if (!id) {
			continue;
		}

		Message message;
		message.id = *id;

		if (
			*id == CUCUMBER_BRIDGE_CLICK_REGISTERED_BUTTON ||
			*id == CUCUMBER_BRIDGE_CLICK_BUTTON_ID ||
			*id == CUCUMBER_BRIDGE_SELECT_MENU ||
			*id == CUCUMBER_BRIDGE_TYPE ||
			*id == CUCUMBER_BRIDGE_ADD_CONTENT_FILE
		   ) {
			auto p = _nanomsg.receive(100);
			DCPOMATIC_ASSERT(p);
			message.parameter = *p;
		}

		boost::mutex::scoped_lock lm(_mutex);
		_queue.push_back(message);
	}
}


void
CucumberBridge::start()
{
	wxTheApp->Bind(wxEVT_IDLE, boost::bind(&CucumberBridge::idle, this));
	_thread = boost::thread(&CucumberBridge::listener, this);
}


void
CucumberBridge::idle()
{
	boost::mutex::scoped_lock lm(_mutex);
	if (_queue.empty()) {
		return;
	}

	auto message = _queue.front();
	_queue.pop_front();
	lm.unlock();

	if (message.id == CUCUMBER_BRIDGE_CLICK_REGISTERED_BUTTON) {
		ClickRegisteredButton(*message.parameter);
	} else if (message.id == CUCUMBER_BRIDGE_CLICK_BUTTON_ID) {
		ClickButtonId(static_cast<wxStandardID>(dcp::raw_convert<int>(*message.parameter)));
	} else if (message.id == CUCUMBER_BRIDGE_SELECT_MENU) {
		SelectMenu(*message.parameter);
	} else if (message.id == CUCUMBER_BRIDGE_TYPE) {
		Type(*message.parameter);
	} else if (message.id == CUCUMBER_BRIDGE_ADD_CONTENT_FILE) {
		AddContentFile(*message.parameter);
	} else if (message.id == CUCUMBER_BRIDGE_GET_CONTENT_LIST) {
		/* I hope it's OK to send and receive from nanomsg sockets from different threads */
		if (!_nanomsg.send(GetContentList()->c_str(), 200)) {
			std::cerr << "Failed to send content list reply.\n";
		}
	}
}