LMMS
Loading...
Searching...
No Matches
PipeClient.cpp
Go to the documentation of this file.
1/*
2 * Carla Plugin Host
3 * Copyright (C) 2011-2023 Filipe Coelho <falktx@falktx.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
16 */
17
18#include "CarlaUtils.h"
19
20#include "CarlaPipeUtils.hpp"
21
22namespace CB = CARLA_BACKEND_NAMESPACE;
23
24// --------------------------------------------------------------------------------------------------------------------
25
26class ExposedCarlaPipeClient : public CarlaPipeClient
27{
28public:
29 ExposedCarlaPipeClient(const CarlaPipeCallbackFunc callbackFunc, void* const callbackPtr) noexcept
30 : CarlaPipeClient(),
31 fCallbackFunc(callbackFunc),
32 fCallbackPtr(callbackPtr),
33 fLastReadLine(nullptr)
34 {
36 }
37
39 {
40 if (fLastReadLine != nullptr)
41 {
42 delete[] fLastReadLine;
43 fLastReadLine = nullptr;
44 }
45 }
46
47 const char* readlineblock(const uint timeout) noexcept
48 {
49 delete[] fLastReadLine;
50 fLastReadLine = CarlaPipeClient::_readlineblock(true, 0, timeout);
51 return fLastReadLine;
52 }
53
54 bool readlineblock_bool(const uint timeout) noexcept
55 {
56 if (const char* const line = CarlaPipeClient::_readlineblock(false, 0, timeout))
57 return std::strcmp(line, "true") == 0;
58
59 return false;
60 }
61
62 int readlineblock_int(const uint timeout) noexcept
63 {
64 if (const char* const line = CarlaPipeClient::_readlineblock(false, 0, timeout))
65 return std::atoi(line);
66
67 return 0;
68 }
69
70 double readlineblock_float(const uint timeout) noexcept
71 {
72 if (const char* const line = CarlaPipeClient::_readlineblock(false, 0, timeout))
73 return std::atof(line);
74
75 return 0.0;
76 }
77
78 bool msgReceived(const char* const msg) noexcept override
79 {
80 if (fCallbackFunc != nullptr)
81 {
82 try {
84 } CARLA_SAFE_EXCEPTION("msgReceived");
85 }
86
87 return true;
88 }
89
90private:
92 void* const fCallbackPtr;
93 const char* fLastReadLine;
94
95 CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ExposedCarlaPipeClient)
96};
97
98// --------------------------------------------------------------------------------------------------------------------
99
100CarlaPipeClientHandle carla_pipe_client_new(const char* argv[], CarlaPipeCallbackFunc callbackFunc, void* callbackPtr)
101{
102 carla_debug("carla_pipe_client_new(%p, %p, %p)", argv, callbackFunc, callbackPtr);
103
104 ExposedCarlaPipeClient* const pipe = new ExposedCarlaPipeClient(callbackFunc, callbackPtr);
105
106 if (! pipe->initPipeClient(argv))
107 {
108 delete pipe;
109 return nullptr;
110 }
111
112 return pipe;
113}
114
116{
117 CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
118
119 ((ExposedCarlaPipeClient*)handle)->idlePipe();
120}
121
123{
124 CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
125
126 return ((ExposedCarlaPipeClient*)handle)->isPipeRunning();
127}
128
130{
131 CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
132
133 return ((ExposedCarlaPipeClient*)handle)->lockPipe();
134}
135
137{
138 CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
139
140 return ((ExposedCarlaPipeClient*)handle)->unlockPipe();
141}
142
144{
145 CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
146
147 return ((ExposedCarlaPipeClient*)handle)->readlineblock(timeout);
148}
149
151{
152 CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
153
154 return ((ExposedCarlaPipeClient*)handle)->readlineblock_bool(timeout);
155}
156
158{
159 CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
160
161 return ((ExposedCarlaPipeClient*)handle)->readlineblock_int(timeout);
162}
163
165{
166 CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0.0);
167
168 return ((ExposedCarlaPipeClient*)handle)->readlineblock_float(timeout);
169}
170
172{
173 CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
174
175 return ((ExposedCarlaPipeClient*)handle)->writeMessage(msg);
176}
177
179{
180 CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
181
182 return ((ExposedCarlaPipeClient*)handle)->writeAndFixMessage(msg);
183}
184
186{
187 CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
188
189 return ((ExposedCarlaPipeClient*)handle)->syncMessages();
190}
191
193{
194 CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
195
196 ExposedCarlaPipeClient* const pipe = (ExposedCarlaPipeClient*)handle;
197 const bool ret = pipe->syncMessages();
198 pipe->unlockPipe();
199 return ret;
200}
201
203{
204 CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
205 carla_debug("carla_pipe_client_destroy(%p)", handle);
206
207 ExposedCarlaPipeClient* const pipe = (ExposedCarlaPipeClient*)handle;
208 pipe->closePipeClient();
209 delete pipe;
210}
211
212// --------------------------------------------------------------------------------------------------------------------
213
218
223
224// --------------------------------------------------------------------------------------------------------------------
225
226#ifndef CARLA_PLUGIN_BUILD
227# include "CarlaPipeUtils.cpp"
228#endif
229
230// --------------------------------------------------------------------------------------------------------------------
#define CARLA_BACKEND_NAMESPACE
Definition CarlaBackend.h:32
#define CARLA_SAFE_EXCEPTION(msg)
Definition CarlaDefines.h:228
#define CARLA_SAFE_ASSERT_RETURN(cond, ret)
Definition CarlaDefines.h:190
unsigned int uint
Definition CarlaDefines.h:327
#define CARLA_SAFE_ASSERT(cond)
Definition CarlaDefines.h:182
#define noexcept
Definition DistrhoDefines.h:72
Definition PipeClient.cpp:27
double readlineblock_float(const uint timeout) noexcept
Definition PipeClient.cpp:70
ExposedCarlaPipeClient(const CarlaPipeCallbackFunc callbackFunc, void *const callbackPtr) noexcept
Definition PipeClient.cpp:29
const char * fLastReadLine
Definition PipeClient.cpp:93
const char * readlineblock(const uint timeout) noexcept
Definition PipeClient.cpp:47
~ExposedCarlaPipeClient() override
Definition PipeClient.cpp:38
void *const fCallbackPtr
Definition PipeClient.cpp:92
bool readlineblock_bool(const uint timeout) noexcept
Definition PipeClient.cpp:54
int readlineblock_int(const uint timeout) noexcept
Definition PipeClient.cpp:62
bool msgReceived(const char *const msg) noexcept override
Definition PipeClient.cpp:78
const CarlaPipeCallbackFunc fCallbackFunc
Definition PipeClient.cpp:91
CarlaPipeClientHandle carla_pipe_client_new(const char *argv[], CarlaPipeCallbackFunc callbackFunc, void *callbackPtr)
Definition PipeClient.cpp:100
void carla_pipe_client_idle(CarlaPipeClientHandle handle)
Definition PipeClient.cpp:115
bool carla_pipe_client_flush_and_unlock(CarlaPipeClientHandle handle)
Definition PipeClient.cpp:219
bool carla_pipe_client_is_running(CarlaPipeClientHandle handle)
Definition PipeClient.cpp:122
void * CarlaPipeClientHandle
Definition CarlaUtils.h:397
bool carla_pipe_client_readlineblock_bool(CarlaPipeClientHandle handle, uint timeout)
Definition PipeClient.cpp:150
int carla_pipe_client_readlineblock_int(CarlaPipeClientHandle handle, uint timeout)
Definition PipeClient.cpp:157
void(* CarlaPipeCallbackFunc)(void *ptr, const char *msg)
Definition CarlaUtils.h:403
void carla_pipe_client_destroy(CarlaPipeClientHandle handle)
Definition PipeClient.cpp:202
void carla_pipe_client_unlock(CarlaPipeClientHandle handle)
Definition PipeClient.cpp:136
bool carla_pipe_client_sync(CarlaPipeClientHandle handle)
Definition PipeClient.cpp:185
bool carla_pipe_client_sync_and_unlock(CarlaPipeClientHandle handle)
Definition PipeClient.cpp:192
double carla_pipe_client_readlineblock_float(CarlaPipeClientHandle handle, uint timeout)
Definition PipeClient.cpp:164
bool carla_pipe_client_write_and_fix_msg(CarlaPipeClientHandle handle, const char *msg)
Definition PipeClient.cpp:178
void carla_pipe_client_lock(CarlaPipeClientHandle handle)
Definition PipeClient.cpp:129
bool carla_pipe_client_write_msg(CarlaPipeClientHandle handle, const char *msg)
Definition PipeClient.cpp:171
const char * carla_pipe_client_readlineblock(CarlaPipeClientHandle handle, uint timeout)
Definition PipeClient.cpp:143
bool carla_pipe_client_flush(CarlaPipeClientHandle handle)
Definition PipeClient.cpp:214
char * argv[]
Definition unzip.c:738
static double timeout
Definition pugl.h:1799
const char * msg
Definition missing_descriptor.c:20