File: Pipes.cpp

package info (click to toggle)
dolphin-emu 5.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 29,052 kB
  • sloc: cpp: 213,146; java: 6,252; asm: 2,277; xml: 1,998; ansic: 1,514; python: 462; sh: 279; pascal: 247; makefile: 124; perl: 97
file content (190 lines) | stat: -rw-r--r-- 4,315 bytes parent folder | download | duplicates (2)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2015 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include <array>
#include <cstdlib>
#include <fcntl.h>
#include <iostream>
#include <locale>
#include <map>
#include <sstream>
#include <string>
#include <unistd.h>
#include <vector>
#include <sys/stat.h>

#include "Common/FileUtil.h"
#include "Common/MathUtil.h"
#include "Common/StringUtil.h"
#include "InputCommon/ControllerInterface/Pipes/Pipes.h"

namespace ciface
{
namespace Pipes
{

static const std::array<std::string, 12> s_button_tokens
{{
	"A",
	"B",
	"X",
	"Y",
	"Z",
	"START",
	"L",
	"R",
	"D_UP",
	"D_DOWN",
	"D_LEFT",
	"D_RIGHT"
}};

static const std::array<std::string, 2> s_shoulder_tokens
{{
	"L",
	"R"
}};

static const std::array<std::string, 2> s_axis_tokens
{{
	"MAIN",
	"C"
}};

static double StringToDouble(const std::string& text)
{
	std::istringstream is(text);
	// ignore current locale
	is.imbue(std::locale::classic());
	double result;
	is >> result;
	return result;
}

void Init(std::vector<Core::Device*>& devices)
{
	// Search the Pipes directory for files that we can open in read-only,
	// non-blocking mode. The device name is the virtual name of the file.
	File::FSTEntry fst;
	int found = 0;
	std::string dir_path = File::GetUserPath(D_PIPES_IDX);
	if (!File::Exists(dir_path))
		return;
	fst = File::ScanDirectoryTree(dir_path, false);
	if (!fst.isDirectory)
		return;
	for (unsigned int i = 0; i < fst.size; ++i)
	{
		const File::FSTEntry& child = fst.children[i];
		if (child.isDirectory)
			continue;
		int fd = open(child.physicalName.c_str(), O_RDONLY | O_NONBLOCK);
		if (fd < 0)
			continue;
		devices.push_back(new PipeDevice(fd, child.virtualName, found++));
	}
}

PipeDevice::PipeDevice(int fd, const std::string& name, int id)
	: m_fd(fd), m_name(name), m_id(id)
{
	for (const auto& tok : s_button_tokens)
	{
		PipeInput* btn = new PipeInput("Button " + tok);
		AddInput(btn);
		m_buttons[tok] = btn;
	}
	for (const auto& tok : s_shoulder_tokens)
	{
		AddAxis(tok, 0.0);
	}
	for (const auto& tok : s_axis_tokens)
	{
		AddAxis(tok + " X", 0.5);
		AddAxis(tok + " Y", 0.5);
	}
}

PipeDevice::~PipeDevice()
{
	close(m_fd);
}

void PipeDevice::UpdateInput()
{
	// Read any pending characters off the pipe. If we hit a newline,
	// then dequeue a command off the front of m_buf and parse it.
	char buf[32];
	ssize_t bytes_read = read(m_fd, buf, sizeof buf);
	while (bytes_read > 0)
	{
		m_buf.append(buf, bytes_read);
		bytes_read = read(m_fd, buf, sizeof buf);
	}
	std::size_t newline = m_buf.find("\n");
	while (newline != std::string::npos)
	{
		std::string command = m_buf.substr(0, newline);
		ParseCommand(command);
		m_buf.erase(0, newline + 1);
		newline = m_buf.find("\n");
	}
}

void PipeDevice::AddAxis(const std::string& name, double value)
{
	// Dolphin uses separate axes for left/right, which complicates things.
	PipeInput* ax_hi = new PipeInput("Axis " + name + " +");
	ax_hi->SetState(value);
	PipeInput* ax_lo = new PipeInput("Axis " + name + " -");
	ax_lo->SetState(value);
	m_axes[name + " +"] = ax_hi;
	m_axes[name + " -"] = ax_lo;
	AddAnalogInputs(ax_lo, ax_hi);
}

void PipeDevice::SetAxis(const std::string& entry, double value)
{
	value = MathUtil::Clamp(value, 0.0, 1.0);
	double hi = std::max(0.0, value - 0.5) * 2.0;
	double lo = (0.5 - std::min(0.5, value)) * 2.0;
	auto search_hi = m_axes.find(entry + " +");
	if (search_hi != m_axes.end())
		search_hi->second->SetState(hi);
	auto search_lo = m_axes.find(entry + " -");
	if (search_lo != m_axes.end())
		search_lo->second->SetState(lo);
}

void PipeDevice::ParseCommand(const std::string& command)
{
	std::vector<std::string> tokens;
	SplitString(command, ' ', tokens);
	if (tokens.size() < 2 || tokens.size() > 4)
		return;
	if (tokens[0] == "PRESS" || tokens[0] == "RELEASE")
	{
		auto search = m_buttons.find(tokens[1]);
		if (search != m_buttons.end())
			search->second->SetState(tokens[0] == "PRESS" ? 1.0 : 0.0);
	}
	else if (tokens[0] == "SET")
	{
		if (tokens.size() == 3)
		{
			double value = StringToDouble(tokens[2]);
			SetAxis(tokens[1], (value / 2.0) + 0.5);
		}
		else if (tokens.size() == 4)
		{
			double x = StringToDouble(tokens[2]);
			double y = StringToDouble(tokens[3]);
			SetAxis(tokens[1] + " X", x);
			SetAxis(tokens[1] + " Y", y);
		}
	}
}

}
}