Clean water File(const String&) usage
[carla.git] / source / bridges-plugin / CarlaBridgePlugin.cpp
blob028c15811d350264fc3e7c29caad431470ec2315
1 // SPDX-FileCopyrightText: 2011-2024 Filipe Coelho <falktx@falktx.com>
2 // SPDX-License-Identifier: GPL-2.0-or-later
4 #ifndef BUILD_BRIDGE
5 # error This file should not be compiled if not building bridge
6 #endif
8 #include "CarlaEngine.hpp"
9 #include "CarlaHost.h"
10 #include "CarlaUtils.h"
12 #include "CarlaBackendUtils.hpp"
13 #include "CarlaJuceUtils.hpp"
14 #include "CarlaMainLoop.hpp"
15 #include "CarlaTimeUtils.hpp"
17 #include "CarlaMIDI.h"
19 #ifdef CARLA_OS_MAC
20 # include "CarlaMacUtils.hpp"
21 #endif
23 #ifdef CARLA_OS_UNIX
24 # include <signal.h>
25 #endif
27 #ifdef CARLA_OS_LINUX
28 # include <sched.h>
29 # define SCHED_RESET_ON_FORK 0x40000000
30 #endif
32 #ifdef CARLA_OS_WIN
33 # include <pthread.h>
34 # include <objbase.h>
35 #endif
37 #ifdef HAVE_X11
38 # include <X11/Xlib.h>
39 #endif
41 #include "water/files/File.h"
42 #include "water/misc/Time.h"
44 // must be last
45 #include "jackbridge/JackBridge.hpp"
47 using CARLA_BACKEND_NAMESPACE::CarlaEngine;
48 using CARLA_BACKEND_NAMESPACE::EngineCallbackOpcode;
49 using CARLA_BACKEND_NAMESPACE::EngineCallbackOpcode2Str;
50 using CARLA_BACKEND_NAMESPACE::runMainLoopOnce;
52 using water::CharPointer_UTF8;
53 using water::File;
54 using water::String;
56 // -------------------------------------------------------------------------
58 static bool gIsInitiated = false;
59 static volatile bool gCloseBridge = false;
60 static volatile bool gCloseSignal = false;
61 static volatile bool gSaveNow = false;
63 #if defined(CARLA_OS_UNIX)
64 static void closeSignalHandler(int) noexcept
66 gCloseSignal = true;
68 static void saveSignalHandler(int) noexcept
70 gSaveNow = true;
72 #elif defined(CARLA_OS_WIN)
73 static LONG WINAPI winExceptionFilter(_EXCEPTION_POINTERS*)
75 return EXCEPTION_EXECUTE_HANDLER;
78 static BOOL WINAPI winSignalHandler(DWORD dwCtrlType) noexcept
80 if (dwCtrlType == CTRL_C_EVENT)
82 gCloseSignal = true;
83 return TRUE;
85 return FALSE;
87 #endif
89 static void initSignalHandler()
91 #if defined(CARLA_OS_UNIX)
92 struct sigaction sig;
93 carla_zeroStruct(sig);
95 sig.sa_handler = closeSignalHandler;
96 sig.sa_flags = SA_RESTART;
97 sigemptyset(&sig.sa_mask);
98 sigaction(SIGTERM, &sig, nullptr);
99 sigaction(SIGINT, &sig, nullptr);
101 sig.sa_handler = saveSignalHandler;
102 sig.sa_flags = SA_RESTART;
103 sigemptyset(&sig.sa_mask);
104 sigaction(SIGUSR1, &sig, nullptr);
105 #elif defined(CARLA_OS_WIN)
106 SetConsoleCtrlHandler(winSignalHandler, TRUE);
107 SetErrorMode(SEM_NOGPFAULTERRORBOX);
108 SetUnhandledExceptionFilter(winExceptionFilter);
109 #endif
112 // -------------------------------------------------------------------------
114 static String gProjectFilename;
115 static CarlaHostHandle gHostHandle;
117 static void gIdle()
119 carla_engine_idle(gHostHandle);
121 if (gSaveNow)
123 gSaveNow = false;
125 if (gProjectFilename.isNotEmpty())
127 if (! carla_save_plugin_state(gHostHandle, 0, gProjectFilename.toRawUTF8()))
128 carla_stderr("Plugin preset save failed, error was:\n%s", carla_get_last_error(gHostHandle));
133 // -------------------------------------------------------------------------
135 class CarlaBridgePlugin
137 public:
138 CarlaBridgePlugin(const bool useBridge, const char* const clientName, const char* const audioPoolBaseName,
139 const char* const rtClientBaseName, const char* const nonRtClientBaseName, const char* const nonRtServerBaseName)
140 : fEngine(nullptr),
141 fUsingBridge(false),
142 fUsingExec(false)
144 CARLA_ASSERT(clientName != nullptr && clientName[0] != '\0');
145 carla_debug("CarlaBridgePlugin::CarlaBridgePlugin(%s, \"%s\", %s, %s, %s, %s)",
146 bool2str(useBridge), clientName, audioPoolBaseName, rtClientBaseName, nonRtClientBaseName, nonRtServerBaseName);
148 carla_set_engine_callback(gHostHandle, callback, this);
150 if (useBridge)
152 carla_engine_init_bridge(gHostHandle,
153 audioPoolBaseName,
154 rtClientBaseName,
155 nonRtClientBaseName,
156 nonRtServerBaseName,
157 clientName);
159 else if (std::getenv("CARLA_BRIDGE_DUMMY") != nullptr)
161 carla_engine_init(gHostHandle, "Dummy", clientName);
163 else
165 carla_engine_init(gHostHandle, "JACK", clientName);
168 fEngine = carla_get_engine_from_handle(gHostHandle);
171 ~CarlaBridgePlugin()
173 carla_debug("CarlaBridgePlugin::~CarlaBridgePlugin()");
175 if (fEngine != nullptr && ! fUsingExec)
176 carla_engine_close(gHostHandle);
179 bool isOk() const noexcept
181 return (fEngine != nullptr);
184 // ---------------------------------------------------------------------
186 void exec(const bool useBridge)
188 fUsingBridge = useBridge;
189 fUsingExec = true;
191 const bool testing = std::getenv("CARLA_BRIDGE_TESTING") != nullptr;
193 if (! useBridge && ! testing)
195 const CarlaPluginInfo* const pInfo = carla_get_plugin_info(gHostHandle, 0);
196 CARLA_SAFE_ASSERT_RETURN(pInfo != nullptr,);
198 gProjectFilename = CharPointer_UTF8(pInfo->name);
199 gProjectFilename += ".carxs";
201 if (! File::isAbsolutePath(gProjectFilename))
202 gProjectFilename = File::getCurrentWorkingDirectory().getChildFile(gProjectFilename).getFullPathName();
204 if (File(gProjectFilename.toRawUTF8()).existsAsFile())
206 if (carla_load_plugin_state(gHostHandle, 0, gProjectFilename.toRawUTF8()))
207 carla_stdout("Plugin state loaded successfully");
208 else
209 carla_stderr("Plugin state load failed, error was:\n%s", carla_get_last_error(gHostHandle));
211 else
213 carla_stdout("Previous plugin state in '%s' is non-existent, will start from default state",
214 gProjectFilename.toRawUTF8());
218 gIsInitiated = true;
220 int64_t timeToEnd = 0;
222 if (testing)
224 timeToEnd = water::Time::currentTimeMillis() + 5 * 1000;
225 fEngine->transportPlay();
228 for (; runMainLoopOnce() && ! gCloseBridge;)
230 gIdle();
231 #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
232 // MacOS and Win32 have event-loops to run, so minimize sleep time
233 carla_msleep(1);
234 #else
235 carla_msleep(5);
236 #endif
237 if (testing && timeToEnd - water::Time::currentTimeMillis() < 0)
238 break;
239 if (gCloseSignal && ! fUsingBridge)
240 break;
243 carla_engine_close(gHostHandle);
246 // ---------------------------------------------------------------------
248 protected:
249 void handleCallback(const EngineCallbackOpcode action,
250 const int value1,
251 const int, const int, const float, const char* const)
253 CARLA_BACKEND_USE_NAMESPACE;
255 switch (action)
257 case ENGINE_CALLBACK_ENGINE_STOPPED:
258 case ENGINE_CALLBACK_PLUGIN_REMOVED:
259 case ENGINE_CALLBACK_QUIT:
260 gCloseBridge = gCloseSignal = true;
261 break;
263 case ENGINE_CALLBACK_UI_STATE_CHANGED:
264 if (gIsInitiated && value1 != 1 && ! fUsingBridge)
265 gCloseBridge = gCloseSignal = true;
266 break;
268 default:
269 break;
273 private:
274 CarlaEngine* fEngine;
276 bool fUsingBridge;
277 bool fUsingExec;
279 static void callback(void* ptr, EngineCallbackOpcode action, unsigned int pluginId,
280 int value1, int value2, int value3,
281 float valuef, const char* valueStr)
283 carla_debug("CarlaBridgePlugin::callback(%p, %i:%s, %i, %i, %i, %i, %f, \"%s\")",
284 ptr, action, EngineCallbackOpcode2Str(action),
285 pluginId, value1, value2, value3, static_cast<double>(valuef), valueStr);
287 // ptr must not be null
288 CARLA_SAFE_ASSERT_RETURN(ptr != nullptr,);
290 #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
291 // pluginId must be 0 (first), except for patchbay things
292 if (action < CARLA_BACKEND_NAMESPACE::ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED ||
293 action > CARLA_BACKEND_NAMESPACE::ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED)
294 #endif
296 CARLA_SAFE_ASSERT_UINT_RETURN(pluginId == 0, pluginId,);
299 return ((CarlaBridgePlugin*)ptr)->handleCallback(action, value1, value2, value3, valuef, valueStr);
302 CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaBridgePlugin)
305 // -------------------------------------------------------------------------
307 int main(int argc, char* argv[])
309 // ---------------------------------------------------------------------
310 // Check argument count
312 if (argc != 4 && argc != 5)
314 carla_stdout("usage: %s <type> <filename> <label> [uniqueId]", argv[0]);
315 return 1;
318 #if defined(CARLA_OS_WIN) && defined(BUILDING_CARLA_FOR_WINE)
319 // ---------------------------------------------------------------------
320 // Test if bridge is working
322 if (! jackbridge_is_ok())
324 carla_stderr("A JACK or Wine library is missing, cannot continue");
325 return 1;
327 #endif
329 // ---------------------------------------------------------------------
330 // Get args
332 const char* const stype = argv[1];
333 const char* filename = argv[2];
334 const char* label = argv[3];
335 const int64_t uniqueId = (argc == 5) ? static_cast<int64_t>(std::atoll(argv[4])) : 0;
337 if (filename[0] == '\0' || std::strcmp(filename, "(none)") == 0)
338 filename = nullptr;
340 if (label[0] == '\0' || std::strcmp(label, "(none)") == 0)
341 label = nullptr;
343 // ---------------------------------------------------------------------
344 // Check binary type
346 CARLA_BACKEND_NAMESPACE::BinaryType btype = CARLA_BACKEND_NAMESPACE::BINARY_NATIVE;
348 if (const char* const binaryTypeStr = std::getenv("CARLA_BRIDGE_PLUGIN_BINARY_TYPE"))
349 btype = CARLA_BACKEND_NAMESPACE::getBinaryTypeFromString(binaryTypeStr);
351 if (btype == CARLA_BACKEND_NAMESPACE::BINARY_NONE)
353 carla_stderr("Invalid binary type '%i'", btype);
354 return 1;
357 // ---------------------------------------------------------------------
358 // Check plugin type
360 CARLA_BACKEND_NAMESPACE::PluginType itype = CARLA_BACKEND_NAMESPACE::getPluginTypeFromString(stype);
362 if (itype == CARLA_BACKEND_NAMESPACE::PLUGIN_NONE)
364 carla_stderr("Invalid plugin type '%s'", stype);
365 return 1;
368 // ---------------------------------------------------------------------
369 // Set file
371 const File file(filename != nullptr ? filename : "");
373 // ---------------------------------------------------------------------
374 // Set name
376 const char* name(std::getenv("CARLA_CLIENT_NAME"));
378 if (name != nullptr && (name[0] == '\0' || std::strcmp(name, "(none)") == 0))
379 name = nullptr;
381 // ---------------------------------------------------------------------
382 // Setup options
384 const char* const shmIds(std::getenv("ENGINE_BRIDGE_SHM_IDS"));
386 const bool useBridge = (shmIds != nullptr);
388 // ---------------------------------------------------------------------
389 // Setup bridge ids
391 char audioPoolBaseName[6+1];
392 char rtClientBaseName[6+1];
393 char nonRtClientBaseName[6+1];
394 char nonRtServerBaseName[6+1];
396 if (useBridge)
398 CARLA_SAFE_ASSERT_RETURN(std::strlen(shmIds) == 6*4, 1);
399 std::strncpy(audioPoolBaseName, shmIds+6*0, 6);
400 std::strncpy(rtClientBaseName, shmIds+6*1, 6);
401 std::strncpy(nonRtClientBaseName, shmIds+6*2, 6);
402 std::strncpy(nonRtServerBaseName, shmIds+6*3, 6);
403 audioPoolBaseName[6] = '\0';
404 rtClientBaseName[6] = '\0';
405 nonRtClientBaseName[6] = '\0';
406 nonRtServerBaseName[6] = '\0';
407 jackbridge_parent_deathsig(false);
409 else
411 audioPoolBaseName[0] = '\0';
412 rtClientBaseName[0] = '\0';
413 nonRtClientBaseName[0] = '\0';
414 nonRtServerBaseName[0] = '\0';
415 jackbridge_init();
418 // ---------------------------------------------------------------------
419 // Set client name
421 CarlaString clientName;
423 if (name != nullptr)
425 clientName = name;
427 else if (itype == CARLA_BACKEND_NAMESPACE::PLUGIN_LV2)
429 // LV2 requires URI
430 CARLA_SAFE_ASSERT_RETURN(label != nullptr && label[0] != '\0', 1);
432 // LV2 URI is not usable as client name, create a usable name from URI
433 CarlaString label2(label);
435 // truncate until last valid char
436 for (std::size_t i=label2.length()-1; i != 0; --i)
438 if (! std::isalnum(label2[i]))
439 continue;
441 label2.truncate(i+1);
442 break;
445 // get last used separator
446 bool found;
447 std::size_t septmp, sep = 0;
449 septmp = label2.rfind('#', &found)+1;
450 if (found && septmp > sep)
451 sep = septmp;
453 septmp = label2.rfind('/', &found)+1;
454 if (found && septmp > sep)
455 sep = septmp;
457 septmp = label2.rfind('=', &found)+1;
458 if (found && septmp > sep)
459 sep = septmp;
461 septmp = label2.rfind(':', &found)+1;
462 if (found && septmp > sep)
463 sep = septmp;
465 // make name starting from the separator and first valid char
466 const char* name2 = label2.buffer() + sep;
467 for (; *name2 != '\0' && ! std::isalnum(*name2); ++name2) {}
469 if (*name2 != '\0')
470 clientName = name2;
472 else if (label != nullptr)
474 clientName = label;
476 else
478 clientName = file.getFileNameWithoutExtension().toRawUTF8();
481 // if we still have no client name by now, use a dummy one
482 if (clientName.isEmpty())
483 clientName = "carla-plugin";
485 // just to be safe
486 clientName.toBasic();
488 // ---------------------------------------------------------------------
489 // Set extraStuff
491 const void* extraStuff = nullptr;
493 if (itype == CARLA_BACKEND_NAMESPACE::PLUGIN_SF2)
495 if (label == nullptr)
496 label = clientName;
498 if (std::strstr(label, " (16 outs)") != nullptr)
499 extraStuff = "true";
502 // ---------------------------------------------------------------------
503 // Initialize OS features
505 const bool dummy = std::getenv("CARLA_BRIDGE_DUMMY") != nullptr;
506 const bool testing = std::getenv("CARLA_BRIDGE_TESTING") != nullptr;
508 #ifdef CARLA_OS_MAC
509 CARLA_BACKEND_NAMESPACE::initStandaloneApplication();
510 #endif
512 #ifdef CARLA_OS_WIN
513 OleInitialize(nullptr);
514 CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
515 # ifndef __WINPTHREADS_VERSION
516 // (non-portable) initialization of statically linked pthread library
517 pthread_win32_process_attach_np();
518 pthread_win32_thread_attach_np();
519 # endif
520 #endif
522 #ifdef HAVE_X11
523 if (std::getenv("DISPLAY") != nullptr)
524 XInitThreads();
525 #endif
527 // ---------------------------------------------------------------------
528 // Set ourselves with high priority
530 if (!dummy && !testing)
532 #ifdef CARLA_OS_LINUX
533 // reset scheduler to normal mode
534 struct sched_param sparam;
535 carla_zeroStruct(sparam);
536 sched_setscheduler(0, SCHED_OTHER|SCHED_RESET_ON_FORK, &sparam);
538 // try niceness first, if it fails, try SCHED_RR
539 if (nice(-5) < 0)
541 sparam.sched_priority = (sched_get_priority_max(SCHED_RR) + sched_get_priority_min(SCHED_RR*7)) / 8;
543 if (sparam.sched_priority > 0)
545 if (sched_setscheduler(0, SCHED_RR|SCHED_RESET_ON_FORK, &sparam) < 0)
547 CarlaString error(std::strerror(errno));
548 carla_stderr("Failed to set high priority, error %i: %s", errno, error.buffer());
552 #endif
554 #ifdef CARLA_OS_WIN
555 if (! SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS))
556 carla_stderr("Failed to set high priority.");
557 #endif
560 // ---------------------------------------------------------------------
561 // Listen for ctrl+c or sigint/sigterm events
563 initSignalHandler();
565 // ---------------------------------------------------------------------
566 // Init plugin bridge
568 int ret;
571 gHostHandle = carla_standalone_host_init();
573 CarlaBridgePlugin bridge(useBridge, clientName,
574 audioPoolBaseName, rtClientBaseName, nonRtClientBaseName, nonRtServerBaseName);
576 if (! bridge.isOk())
578 carla_stderr("Failed to init engine, error was:\n%s", carla_get_last_error(gHostHandle));
579 return 1;
582 if (! useBridge && ! testing)
584 #ifdef HAVE_X11
585 if (std::getenv("DISPLAY") != nullptr)
586 #endif
587 carla_set_engine_option(gHostHandle,
588 CARLA_BACKEND_NAMESPACE::ENGINE_OPTION_FRONTEND_UI_SCALE,
589 static_cast<int>(carla_get_desktop_scale_factor()*1000+0.5),
590 nullptr);
593 // -----------------------------------------------------------------
594 // Init plugin
596 if (carla_add_plugin(gHostHandle,
597 btype, itype,
598 file.getFullPathName().toRawUTF8(), name, label, uniqueId, extraStuff,
599 CARLA_BACKEND_NAMESPACE::PLUGIN_OPTIONS_NULL))
601 ret = 0;
603 if (! useBridge)
605 carla_set_active(gHostHandle, 0, true);
606 carla_set_engine_option(gHostHandle, CARLA_BACKEND_NAMESPACE::ENGINE_OPTION_PLUGINS_ARE_STANDALONE, 1, nullptr);
608 if (const CarlaPluginInfo* const pluginInfo = carla_get_plugin_info(gHostHandle, 0))
610 if (itype == CARLA_BACKEND_NAMESPACE::PLUGIN_INTERNAL && (std::strcmp(label, "audiofile") == 0 || std::strcmp(label, "midifile") == 0))
612 if (file.exists())
613 carla_set_custom_data(gHostHandle, 0,
614 CARLA_BACKEND_NAMESPACE::CUSTOM_DATA_TYPE_STRING,
615 "file", file.getFullPathName().toRawUTF8());
617 else if (pluginInfo->hints & CARLA_BACKEND_NAMESPACE::PLUGIN_HAS_CUSTOM_UI)
619 #ifdef HAVE_X11
620 if (std::getenv("DISPLAY") != nullptr)
621 #endif
622 if (! testing)
623 carla_show_custom_ui(gHostHandle, 0, true);
626 // on standalone usage, enable everything that makes sense
627 const uint optsAvailable = pluginInfo->optionsAvailable;
628 if (optsAvailable & CARLA_BACKEND_NAMESPACE::PLUGIN_OPTION_FIXED_BUFFERS)
629 carla_set_option(gHostHandle, 0, CARLA_BACKEND_NAMESPACE::PLUGIN_OPTION_FIXED_BUFFERS, true);
633 bridge.exec(useBridge);
635 else
637 ret = 1;
639 const char* const lastError(carla_get_last_error(gHostHandle));
640 carla_stderr("Plugin failed to load, error was:\n%s", lastError);
642 if (useBridge)
644 // do a single idle so that we can send error message to server
645 gIdle();
647 #ifdef CARLA_OS_UNIX
648 // kill ourselves now if we can't load plugin in bridge mode
649 ::kill(::getpid(), SIGKILL);
650 #endif
655 #ifdef CARLA_OS_WIN
656 #ifndef __WINPTHREADS_VERSION
657 pthread_win32_thread_detach_np();
658 pthread_win32_process_detach_np();
659 #endif
660 CoUninitialize();
661 OleUninitialize();
662 #endif
664 return ret;