Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CarlaEngineJack.cpp 161KB

11 years ago
9 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
6 years ago
10 years ago
10 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
10 years ago
5 years ago
10 years ago
10 years ago
10 years ago
10 years ago
6 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
6 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547
  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2020 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. #include "CarlaEngineClient.hpp"
  18. #include "CarlaEngineInit.hpp"
  19. #include "CarlaEngineInternal.hpp"
  20. #include "CarlaPlugin.hpp"
  21. #include "CarlaBackendUtils.hpp"
  22. #include "CarlaEngineUtils.hpp"
  23. #include "CarlaMathUtils.hpp"
  24. #include "CarlaMIDI.h"
  25. #include "CarlaPatchbayUtils.hpp"
  26. #include "CarlaStringList.hpp"
  27. #include "jackey.h"
  28. #ifdef USING_JUCE
  29. # if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  30. # pragma GCC diagnostic push
  31. # pragma GCC diagnostic ignored "-Wconversion"
  32. # pragma GCC diagnostic ignored "-Weffc++"
  33. # pragma GCC diagnostic ignored "-Wsign-conversion"
  34. # pragma GCC diagnostic ignored "-Wundef"
  35. # endif
  36. # include "AppConfig.h"
  37. # include "juce_events/juce_events.h"
  38. # if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  39. # pragma GCC diagnostic pop
  40. # endif
  41. #endif
  42. #ifdef __SSE2_MATH__
  43. # include <xmmintrin.h>
  44. #endif
  45. // must be last
  46. #include "jackbridge/JackBridge.hpp"
  47. #define URI_CANVAS_ICON "http://kxstudio.sf.net/ns/canvas/icon"
  48. #define URI_MAIN_CLIENT_NAME "https://kx.studio/ns/carla/main-client-name"
  49. #define URI_POSITION "https://kx.studio/ns/carla/position"
  50. #define URI_PLUGIN_ICON "https://kx.studio/ns/carla/plugin-icon"
  51. #define URI_PLUGIN_ID "https://kx.studio/ns/carla/plugin-id"
  52. #define URI_TYPE_INTEGER "http://www.w3.org/2001/XMLSchema#integer"
  53. #define URI_TYPE_STRING "text/plain"
  54. CARLA_BACKEND_START_NAMESPACE
  55. class CarlaEngineJack;
  56. class CarlaEngineJackClient;
  57. #ifndef BUILD_BRIDGE
  58. struct CarlaJackPortHints {
  59. bool isHardware : 1;
  60. bool isInput : 1;
  61. bool isAudio : 1;
  62. bool isMIDI : 1;
  63. bool isCV : 1;
  64. bool isOSC : 1;
  65. // NOTE: assumes fThreadSafeMetadataMutex lock done from caller
  66. static CarlaJackPortHints fromPort(const jack_port_t* const jackPort)
  67. {
  68. CarlaJackPortHints ph = { false, false, false, false, false, false };
  69. const int portFlags = jackbridge_port_flags(jackPort);
  70. const char* const portType = jackbridge_port_type(jackPort);
  71. ph.isHardware = portFlags & JackPortIsPhysical;
  72. ph.isInput = portFlags & JackPortIsInput;
  73. ph.isAudio = portType != nullptr && std::strcmp(portType, JACK_DEFAULT_AUDIO_TYPE) == 0;
  74. ph.isMIDI = portType != nullptr && std::strcmp(portType, JACK_DEFAULT_MIDI_TYPE) == 0;
  75. ph.isCV = false;
  76. ph.isOSC = false;
  77. if (const jack_uuid_t uuid = jackbridge_port_uuid(jackPort))
  78. {
  79. char* value = nullptr;
  80. char* type = nullptr;
  81. if (jackbridge_get_property(uuid, JACKEY_SIGNAL_TYPE, &value, &type)
  82. && value != nullptr
  83. && type != nullptr
  84. && std::strcmp(type, URI_TYPE_STRING) == 0)
  85. {
  86. ph.isCV = (std::strcmp(value, "CV") == 0);
  87. ph.isOSC = (std::strcmp(value, "OSC") == 0);
  88. jackbridge_free(value);
  89. jackbridge_free(type);
  90. }
  91. }
  92. return ph;
  93. }
  94. };
  95. #endif
  96. // -----------------------------------------------------------------------
  97. // Fallback data
  98. #ifndef BUILD_BRIDGE
  99. static const GroupNameToId kGroupNameToIdFallback = { 0, { '\0' } };
  100. static /* */ PortNameToId kPortNameToIdFallbackNC = { 0, 0, { '\0' }, { '\0' } };
  101. static const PortNameToId kPortNameToIdFallback = { 0, 0, { '\0' }, { '\0' } };
  102. static const ConnectionToId kConnectionToIdFallback = { 0, 0, 0, 0, 0 };
  103. #endif
  104. static const EngineEvent kFallbackJackEngineEvent = { kEngineEventTypeNull, 0, 0, {{ kEngineControlEventTypeNull, 0, 0.0f }} };
  105. // -----------------------------------------------------------------------
  106. // Carla Engine Port removal helper
  107. class CarlaEngineJackAudioPort;
  108. class CarlaEngineJackCVPort;
  109. class CarlaEngineJackEventPort;
  110. struct JackPortDeletionCallback {
  111. virtual ~JackPortDeletionCallback() noexcept {}
  112. virtual void jackAudioPortDeleted(CarlaEngineJackAudioPort* const) noexcept = 0;
  113. virtual void jackCVPortDeleted(CarlaEngineJackCVPort* const) noexcept = 0;
  114. virtual void jackEventPortDeleted(CarlaEngineJackEventPort* const) noexcept = 0;
  115. };
  116. // -----------------------------------------------------------------------
  117. // Carla Engine JACK-Audio port
  118. class CarlaEngineJackAudioPort : public CarlaEngineAudioPort
  119. {
  120. public:
  121. CarlaEngineJackAudioPort(const CarlaEngineClient& client,
  122. const bool isInputPort,
  123. const uint32_t indexOffset,
  124. jack_client_t* const jackClient,
  125. jack_port_t* const jackPort,
  126. CarlaRecursiveMutex& rmutex,
  127. JackPortDeletionCallback* const delCallback) noexcept
  128. : CarlaEngineAudioPort(client, isInputPort, indexOffset),
  129. fJackClient(jackClient),
  130. fJackPort(jackPort),
  131. fThreadSafeMetadataMutex(rmutex),
  132. kDeletionCallback(delCallback)
  133. {
  134. carla_debug("CarlaEngineJackAudioPort::CarlaEngineJackAudioPort(%s, %p, %p)", bool2str(isInputPort), jackClient, jackPort);
  135. switch (kClient.getEngine().getProccessMode())
  136. {
  137. case ENGINE_PROCESS_MODE_SINGLE_CLIENT:
  138. case ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS:
  139. CARLA_SAFE_ASSERT_RETURN(jackClient != nullptr && jackPort != nullptr,);
  140. {
  141. const CarlaRecursiveMutexLocker crml(fThreadSafeMetadataMutex);
  142. if (const jack_uuid_t uuid = jackbridge_port_uuid(jackPort))
  143. jackbridge_set_property(jackClient, uuid, JACKEY_SIGNAL_TYPE, "AUDIO", URI_TYPE_STRING);
  144. }
  145. break;
  146. default:
  147. CARLA_SAFE_ASSERT(jackClient == nullptr && jackPort == nullptr);
  148. break;
  149. }
  150. }
  151. ~CarlaEngineJackAudioPort() noexcept override
  152. {
  153. carla_debug("CarlaEngineJackAudioPort::~CarlaEngineJackAudioPort()");
  154. if (fJackClient != nullptr && fJackPort != nullptr)
  155. {
  156. try {
  157. jackbridge_port_unregister(fJackClient, fJackPort);
  158. } CARLA_SAFE_EXCEPTION("Audio port unregister");
  159. fJackClient = nullptr;
  160. fJackPort = nullptr;
  161. }
  162. if (kDeletionCallback != nullptr)
  163. kDeletionCallback->jackAudioPortDeleted(this);
  164. }
  165. void initBuffer() noexcept override
  166. {
  167. if (fJackPort == nullptr)
  168. return CarlaEngineAudioPort::initBuffer();
  169. const uint32_t bufferSize(kClient.getEngine().getBufferSize());
  170. try {
  171. fBuffer = (float*)jackbridge_port_get_buffer(fJackPort, bufferSize);
  172. }
  173. catch(...) {
  174. fBuffer = nullptr;
  175. return;
  176. }
  177. if (! kIsInput)
  178. carla_zeroFloats(fBuffer, bufferSize);
  179. }
  180. void invalidate() noexcept
  181. {
  182. fJackClient = nullptr;
  183. fJackPort = nullptr;
  184. }
  185. void setMetaData(const char* const key, const char* const value, const char* const type) override
  186. {
  187. if (fJackPort == nullptr)
  188. return CarlaEngineJackAudioPort::setMetaData(key, value, type);
  189. const CarlaRecursiveMutexLocker crml(fThreadSafeMetadataMutex);
  190. try {
  191. if (const jack_uuid_t uuid = jackbridge_port_uuid(fJackPort))
  192. jackbridge_set_property(fJackClient, uuid, key, value, type);
  193. } CARLA_SAFE_EXCEPTION("Port setMetaData");
  194. }
  195. private:
  196. jack_client_t* fJackClient;
  197. jack_port_t* fJackPort;
  198. CarlaRecursiveMutex& fThreadSafeMetadataMutex;
  199. JackPortDeletionCallback* const kDeletionCallback;
  200. friend class CarlaEngineJackClient;
  201. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJackAudioPort)
  202. };
  203. // -----------------------------------------------------------------------
  204. // Carla Engine JACK-CV port
  205. class CarlaEngineJackCVPort : public CarlaEngineCVPort
  206. {
  207. public:
  208. CarlaEngineJackCVPort(const CarlaEngineClient& client,
  209. const bool isInputPort,
  210. const uint32_t indexOffset,
  211. jack_client_t* const jackClient,
  212. jack_port_t* const jackPort,
  213. CarlaRecursiveMutex& rmutex,
  214. JackPortDeletionCallback* const delCallback) noexcept
  215. : CarlaEngineCVPort(client, isInputPort, indexOffset),
  216. fJackClient(jackClient),
  217. fJackPort(jackPort),
  218. fThreadSafeMetadataMutex(rmutex),
  219. kDeletionCallback(delCallback)
  220. {
  221. carla_debug("CarlaEngineJackCVPort::CarlaEngineJackCVPort(%s, %p, %p)", bool2str(isInputPort), jackClient, jackPort);
  222. switch (kClient.getEngine().getProccessMode())
  223. {
  224. case ENGINE_PROCESS_MODE_SINGLE_CLIENT:
  225. case ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS:
  226. CARLA_SAFE_ASSERT_RETURN(jackClient != nullptr && jackPort != nullptr,);
  227. {
  228. const CarlaRecursiveMutexLocker crml(fThreadSafeMetadataMutex);
  229. if (const jack_uuid_t uuid = jackbridge_port_uuid(jackPort))
  230. jackbridge_set_property(jackClient, uuid, JACKEY_SIGNAL_TYPE, "CV", URI_TYPE_STRING);
  231. }
  232. break;
  233. default:
  234. CARLA_SAFE_ASSERT(jackClient == nullptr && jackPort == nullptr);
  235. break;
  236. }
  237. }
  238. ~CarlaEngineJackCVPort() noexcept override
  239. {
  240. carla_debug("CarlaEngineJackCVPort::~CarlaEngineJackCVPort()");
  241. if (fJackClient != nullptr && fJackPort != nullptr)
  242. {
  243. try {
  244. jackbridge_port_unregister(fJackClient, fJackPort);
  245. } CARLA_SAFE_EXCEPTION("CV port unregister");
  246. fJackClient = nullptr;
  247. fJackPort = nullptr;
  248. }
  249. if (kDeletionCallback != nullptr)
  250. kDeletionCallback->jackCVPortDeleted(this);
  251. }
  252. void initBuffer() noexcept override
  253. {
  254. if (fJackPort == nullptr)
  255. return CarlaEngineCVPort::initBuffer();
  256. const uint32_t bufferSize(kClient.getEngine().getBufferSize());
  257. try {
  258. fBuffer = (float*)jackbridge_port_get_buffer(fJackPort, bufferSize);
  259. }
  260. catch(...) {
  261. fBuffer = nullptr;
  262. return;
  263. }
  264. if (! kIsInput)
  265. carla_zeroFloats(fBuffer, bufferSize);
  266. }
  267. void invalidate() noexcept
  268. {
  269. fJackClient = nullptr;
  270. fJackPort = nullptr;
  271. }
  272. void setMetaData(const char* const key, const char* const value, const char* const type) override
  273. {
  274. if (fJackPort == nullptr)
  275. return CarlaEngineCVPort::setMetaData(key, value, type);
  276. const CarlaRecursiveMutexLocker crml(fThreadSafeMetadataMutex);
  277. try {
  278. if (const jack_uuid_t uuid = jackbridge_port_uuid(fJackPort))
  279. jackbridge_set_property(fJackClient, uuid, key, value, type);
  280. } CARLA_SAFE_EXCEPTION("Port setMetaData");
  281. }
  282. private:
  283. jack_client_t* fJackClient;
  284. jack_port_t* fJackPort;
  285. CarlaRecursiveMutex& fThreadSafeMetadataMutex;
  286. JackPortDeletionCallback* const kDeletionCallback;
  287. friend class CarlaEngineJackClient;
  288. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJackCVPort)
  289. };
  290. // -----------------------------------------------------------------------
  291. // Carla Engine JACK-Event port
  292. class CarlaEngineJackEventPort : public CarlaEngineEventPort
  293. {
  294. public:
  295. CarlaEngineJackEventPort(const CarlaEngineClient& client,
  296. const bool isInputPort,
  297. const uint32_t indexOffset,
  298. jack_client_t* const jackClient,
  299. jack_port_t* const jackPort,
  300. CarlaRecursiveMutex& rmutex,
  301. JackPortDeletionCallback* const delCallback) noexcept
  302. : CarlaEngineEventPort(client, isInputPort, indexOffset),
  303. fJackClient(jackClient),
  304. fJackPort(jackPort),
  305. fJackBuffer(nullptr),
  306. fRetEvent(kFallbackJackEngineEvent),
  307. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  308. fCvSourceEvents(nullptr),
  309. fCvSourceEventCount(0),
  310. #endif
  311. fThreadSafeMetadataMutex(rmutex),
  312. kDeletionCallback(delCallback)
  313. {
  314. carla_debug("CarlaEngineJackEventPort::CarlaEngineJackEventPort(%s, %p, %p)", bool2str(isInputPort), jackClient, jackPort);
  315. switch (kClient.getEngine().getProccessMode())
  316. {
  317. case ENGINE_PROCESS_MODE_SINGLE_CLIENT:
  318. case ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS:
  319. CARLA_SAFE_ASSERT_RETURN(jackClient != nullptr && jackPort != nullptr,);
  320. break;
  321. default:
  322. CARLA_SAFE_ASSERT(jackClient == nullptr && jackPort == nullptr);
  323. break;
  324. }
  325. }
  326. ~CarlaEngineJackEventPort() noexcept override
  327. {
  328. carla_debug("CarlaEngineJackEventPort::~CarlaEngineJackEventPort()");
  329. if (fJackClient != nullptr && fJackPort != nullptr)
  330. {
  331. try {
  332. jackbridge_port_unregister(fJackClient, fJackPort);
  333. } CARLA_SAFE_EXCEPTION("Event port unregister");
  334. fJackClient = nullptr;
  335. fJackPort = nullptr;
  336. }
  337. if (kDeletionCallback != nullptr)
  338. kDeletionCallback->jackEventPortDeleted(this);
  339. }
  340. void initBuffer() noexcept override
  341. {
  342. if (fJackPort == nullptr)
  343. return CarlaEngineEventPort::initBuffer();
  344. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  345. fCvSourceEvents = nullptr;
  346. fCvSourceEventCount = 0;
  347. #endif
  348. try {
  349. fJackBuffer = jackbridge_port_get_buffer(fJackPort, kClient.getEngine().getBufferSize());
  350. }
  351. catch(...) {
  352. fJackBuffer = nullptr;
  353. return;
  354. }
  355. if (! kIsInput)
  356. jackbridge_midi_clear_buffer(fJackBuffer);
  357. }
  358. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  359. void setCvSourceEvents(EngineEvent* const events, const uint32_t eventCount) noexcept
  360. {
  361. fCvSourceEvents = events;
  362. fCvSourceEventCount = eventCount;
  363. }
  364. #endif
  365. uint32_t getEventCount() const noexcept override
  366. {
  367. if (fJackPort == nullptr)
  368. return CarlaEngineEventPort::getEventCount();
  369. CARLA_SAFE_ASSERT_RETURN(kIsInput, 0);
  370. CARLA_SAFE_ASSERT_RETURN(fJackBuffer != nullptr, 0);
  371. try {
  372. return jackbridge_midi_get_event_count(fJackBuffer)
  373. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  374. + fCvSourceEventCount
  375. #endif
  376. ;
  377. } CARLA_SAFE_EXCEPTION_RETURN("jack_midi_get_event_count", 0);
  378. }
  379. const EngineEvent& getEvent(const uint32_t index) const noexcept override
  380. {
  381. if (fJackPort == nullptr)
  382. return CarlaEngineEventPort::getEvent(index);
  383. CARLA_SAFE_ASSERT_RETURN(kIsInput, kFallbackJackEngineEvent);
  384. CARLA_SAFE_ASSERT_RETURN(fJackBuffer != nullptr, kFallbackJackEngineEvent);
  385. return getEventUnchecked(index);
  386. }
  387. const EngineEvent& getEventUnchecked(uint32_t index) const noexcept override
  388. {
  389. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  390. if (index < fCvSourceEventCount)
  391. return fCvSourceEvents[index];
  392. index -= fCvSourceEventCount;
  393. #endif
  394. jack_midi_event_t jackEvent;
  395. bool test = false;
  396. try {
  397. test = jackbridge_midi_event_get(&jackEvent, fJackBuffer, index);
  398. } CARLA_SAFE_EXCEPTION_RETURN("jack_midi_event_get", kFallbackJackEngineEvent);
  399. if (! test)
  400. return kFallbackJackEngineEvent;
  401. CARLA_SAFE_ASSERT_RETURN(jackEvent.size < 0xFF /* uint8_t max */, kFallbackJackEngineEvent);
  402. uint8_t port;
  403. if (kIndexOffset < 0xFF /* uint8_t max */)
  404. {
  405. port = static_cast<uint8_t>(kIndexOffset);
  406. }
  407. else
  408. {
  409. port = 0;
  410. carla_safe_assert_uint("kIndexOffset < 0xFF", __FILE__, __LINE__, kIndexOffset);
  411. }
  412. fRetEvent.time = jackEvent.time;
  413. fRetEvent.fillFromMidiData(static_cast<uint8_t>(jackEvent.size), jackEvent.buffer, port);
  414. return fRetEvent;
  415. }
  416. bool writeControlEvent(const uint32_t time, const uint8_t channel, const EngineControlEventType type, const uint16_t param, const float value) noexcept override
  417. {
  418. if (fJackPort == nullptr)
  419. return CarlaEngineEventPort::writeControlEvent(time, channel, type, param, value);
  420. CARLA_SAFE_ASSERT_RETURN(! kIsInput, false);
  421. CARLA_SAFE_ASSERT_RETURN(fJackBuffer != nullptr, false);
  422. CARLA_SAFE_ASSERT_RETURN(type != kEngineControlEventTypeNull, false);
  423. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS, false);
  424. CARLA_SAFE_ASSERT_RETURN(param < MAX_MIDI_VALUE, false);
  425. CARLA_SAFE_ASSERT(value >= 0.0f && value <= 1.0f);
  426. if (type == kEngineControlEventTypeParameter) {
  427. CARLA_SAFE_ASSERT(! MIDI_IS_CONTROL_BANK_SELECT(param));
  428. }
  429. uint8_t data[3] = { 0, 0, 0 };
  430. EngineControlEvent ctrlEvent = { type, param, value };
  431. const uint8_t size = ctrlEvent.convertToMidiData(channel, data);
  432. if (size == 0)
  433. return false;
  434. try {
  435. return jackbridge_midi_event_write(fJackBuffer, time, data, size);
  436. } CARLA_SAFE_EXCEPTION_RETURN("jack_midi_event_write", false);
  437. }
  438. bool writeMidiEvent(const uint32_t time, const uint8_t channel, const uint8_t size, const uint8_t* const data) noexcept override
  439. {
  440. if (fJackPort == nullptr)
  441. return CarlaEngineEventPort::writeMidiEvent(time, channel, size, data);
  442. CARLA_SAFE_ASSERT_RETURN(! kIsInput, false);
  443. CARLA_SAFE_ASSERT_RETURN(fJackBuffer != nullptr, false);
  444. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS, false);
  445. CARLA_SAFE_ASSERT_RETURN(size > 0, false);
  446. CARLA_SAFE_ASSERT_RETURN(data != nullptr, false);
  447. jack_midi_data_t jdata[size];
  448. jdata[0] = static_cast<jack_midi_data_t>(MIDI_GET_STATUS_FROM_DATA(data) + channel);
  449. for (uint8_t i=1; i < size; ++i)
  450. jdata[i] = data[i];
  451. try {
  452. return jackbridge_midi_event_write(fJackBuffer, time, jdata, size);
  453. } CARLA_SAFE_EXCEPTION_RETURN("jack_midi_event_write", false);
  454. }
  455. void invalidate() noexcept
  456. {
  457. fJackClient = nullptr;
  458. fJackPort = nullptr;
  459. }
  460. void setMetaData(const char* const key, const char* const value, const char* const type) override
  461. {
  462. if (fJackPort == nullptr)
  463. return CarlaEngineJackEventPort::setMetaData(key, value, type);
  464. const CarlaRecursiveMutexLocker crml(fThreadSafeMetadataMutex);
  465. try {
  466. if (const jack_uuid_t uuid = jackbridge_port_uuid(fJackPort))
  467. jackbridge_set_property(fJackClient, uuid, key, value, type);
  468. } CARLA_SAFE_EXCEPTION("Port setMetaData");
  469. }
  470. private:
  471. jack_client_t* fJackClient;
  472. jack_port_t* fJackPort;
  473. void* fJackBuffer;
  474. mutable EngineEvent fRetEvent;
  475. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  476. EngineEvent* fCvSourceEvents;
  477. uint32_t fCvSourceEventCount;
  478. #endif
  479. CarlaRecursiveMutex& fThreadSafeMetadataMutex;
  480. JackPortDeletionCallback* const kDeletionCallback;
  481. friend class CarlaEngineJackClient;
  482. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJackEventPort)
  483. };
  484. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  485. // -----------------------------------------------------------------------
  486. // Jack Engine CV source ports
  487. class CarlaEngineJackCVSourcePorts : public CarlaEngineCVSourcePorts
  488. {
  489. public:
  490. CarlaEngineJackCVSourcePorts(const bool useClient)
  491. : CarlaEngineCVSourcePorts(),
  492. fUseClient(useClient),
  493. fBuffer(nullptr),
  494. fBufferToDeleteLater(nullptr)
  495. {}
  496. ~CarlaEngineJackCVSourcePorts() override
  497. {
  498. if (fBufferToDeleteLater != nullptr)
  499. {
  500. delete[] fBufferToDeleteLater;
  501. fBufferToDeleteLater = nullptr;
  502. }
  503. }
  504. bool addCVSource(CarlaEngineCVPort* const port, const uint32_t portIndexOffset) override
  505. {
  506. if (! fUseClient)
  507. return CarlaEngineCVSourcePorts::addCVSource(port, portIndexOffset);
  508. const CarlaRecursiveMutexLocker crml(pData->rmutex);
  509. if (! CarlaEngineCVSourcePorts::addCVSource(port, portIndexOffset))
  510. return false;
  511. if (pData->cvs.size() == 1 && fBuffer == nullptr)
  512. {
  513. EngineEvent* const buffer = new EngineEvent[kMaxEngineEventInternalCount];
  514. carla_zeroStructs(buffer, kMaxEngineEventInternalCount);
  515. fBuffer = buffer;
  516. }
  517. return true;
  518. }
  519. bool removeCVSource(const uint32_t portIndexOffset) override
  520. {
  521. if (! fUseClient)
  522. return CarlaEngineCVSourcePorts::removeCVSource(portIndexOffset);
  523. const CarlaRecursiveMutexLocker crml(pData->rmutex);
  524. if (! CarlaEngineCVSourcePorts::removeCVSource(portIndexOffset))
  525. return false;
  526. if (pData->cvs.size() == 0 && fBuffer != nullptr)
  527. {
  528. if (fBufferToDeleteLater != nullptr)
  529. delete[] fBufferToDeleteLater;
  530. fBufferToDeleteLater = fBuffer;
  531. fBuffer = nullptr;
  532. }
  533. return true;
  534. }
  535. void initPortBuffers(const float* const* const buffers,
  536. const uint32_t frames,
  537. const bool sampleAccurate,
  538. CarlaEngineEventPort* const eventPort) override
  539. {
  540. if (! fUseClient)
  541. return CarlaEngineCVSourcePorts::initPortBuffers(buffers, frames, sampleAccurate, eventPort);
  542. CARLA_SAFE_ASSERT_RETURN(buffers != nullptr,);
  543. CARLA_SAFE_ASSERT_RETURN(eventPort != nullptr,);
  544. const CarlaRecursiveMutexTryLocker crmtl(pData->rmutex);
  545. if (! crmtl.wasLocked())
  546. return;
  547. const int numCVs = pData->cvs.size();
  548. if (numCVs == 0)
  549. return;
  550. EngineEvent* const buffer = fBuffer;
  551. CARLA_SAFE_ASSERT_RETURN(buffer != nullptr,);
  552. uint32_t eventCount = 0;
  553. float v, min, max;
  554. for (int i = 0; i < numCVs && eventCount < kMaxEngineEventInternalCount; ++i)
  555. {
  556. CarlaEngineEventCV& ecv(pData->cvs.getReference(i));
  557. CARLA_SAFE_ASSERT_CONTINUE(ecv.cvPort != nullptr);
  558. CARLA_SAFE_ASSERT_CONTINUE(buffers[i] != nullptr);
  559. float previousValue = ecv.previousValue;
  560. ecv.cvPort->getRange(min, max);
  561. v = buffers[i][0];
  562. if (carla_isNotEqual(v, previousValue))
  563. {
  564. previousValue = v;
  565. EngineEvent& event(buffer[eventCount++]);
  566. event.type = kEngineEventTypeControl;
  567. event.time = 0;
  568. event.channel = kEngineEventNonMidiChannel;
  569. event.ctrl.type = kEngineControlEventTypeParameter;
  570. event.ctrl.param = static_cast<uint16_t>(ecv.indexOffset);
  571. event.ctrl.value = carla_fixedValue(0.0f, 1.0f, (v - min) / (max - min));
  572. }
  573. ecv.previousValue = previousValue;
  574. }
  575. if (eventCount != 0)
  576. if (CarlaEngineJackEventPort* const jackEventPort = dynamic_cast<CarlaEngineJackEventPort*>(eventPort))
  577. jackEventPort->setCvSourceEvents(buffer, eventCount);
  578. }
  579. CarlaRecursiveMutex& getMutex() const noexcept
  580. {
  581. return pData->rmutex;
  582. }
  583. uint32_t getPortCount() const noexcept
  584. {
  585. return static_cast<uint32_t>(pData->cvs.size());
  586. }
  587. CarlaEngineCVPort* getPort(const uint32_t portIndexOffset) const
  588. {
  589. const int ioffset = static_cast<int>(portIndexOffset);
  590. return pData->cvs[ioffset].cvPort;
  591. }
  592. void resetGraphAndPlugin() noexcept
  593. {
  594. pData->graph = nullptr;
  595. pData->plugin.reset();
  596. }
  597. void setGraphAndPlugin(PatchbayGraph* const graph, const CarlaPluginPtr plugin) noexcept
  598. {
  599. pData->graph = graph;
  600. pData->plugin = plugin;
  601. }
  602. private:
  603. const bool fUseClient;
  604. EngineEvent* fBuffer;
  605. EngineEvent* fBufferToDeleteLater;
  606. CARLA_DECLARE_NON_COPY_CLASS(CarlaEngineJackCVSourcePorts)
  607. };
  608. #endif
  609. // -----------------------------------------------------------------------
  610. // Jack Engine client
  611. class CarlaEngineJackClient : public CarlaEngineClientForSubclassing,
  612. private JackPortDeletionCallback
  613. {
  614. public:
  615. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  616. CarlaEngineJackClient(const CarlaEngine& engine,
  617. EngineInternalGraph& egraph,
  618. CarlaRecursiveMutex& rmutex,
  619. const CarlaPluginPtr plugin,
  620. const CarlaString& mainClientName,
  621. jack_client_t* const jackClient)
  622. : CarlaEngineClientForSubclassing(engine, egraph, plugin),
  623. #else
  624. CarlaEngineJackClient(const CarlaEngine& engine,
  625. CarlaRecursiveMutex& rmutex,
  626. const CarlaString& mainClientName,
  627. jack_client_t* const jackClient)
  628. : CarlaEngineClientForSubclassing(engine),
  629. #endif
  630. fJackClient(jackClient),
  631. fUseClient(engine.getProccessMode() == ENGINE_PROCESS_MODE_SINGLE_CLIENT ||
  632. engine.getProccessMode() == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS),
  633. fAudioPorts(),
  634. fCVPorts(),
  635. fEventPorts(),
  636. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  637. fCVSourcePorts(fUseClient),
  638. fPreRenameMutex(),
  639. fPreRenameConnections(),
  640. fPreRenamePluginId(),
  641. fPreRenamePluginIcon(),
  642. fReservedPluginPtr(),
  643. #endif
  644. fThreadSafeMetadataMutex(rmutex),
  645. fMainClientName(mainClientName)
  646. {
  647. carla_debug("CarlaEngineJackClient::CarlaEngineJackClient(%p)", jackClient);
  648. if (fUseClient)
  649. {
  650. CARLA_SAFE_ASSERT(jackClient != nullptr);
  651. }
  652. else
  653. {
  654. CARLA_SAFE_ASSERT(jackClient == nullptr);
  655. }
  656. }
  657. ~CarlaEngineJackClient() noexcept override
  658. {
  659. carla_debug("CarlaEngineJackClient::~CarlaEngineJackClient()");
  660. if (getProcessMode() == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS && fJackClient != nullptr) // FIXME
  661. jackbridge_client_close(fJackClient);
  662. // ports must have been deleted by now!
  663. //fAudioPorts.clear();
  664. //fCVPorts.clear();
  665. //fEventPorts.clear();
  666. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  667. const CarlaMutexLocker cml(fPreRenameMutex);
  668. fPreRenameConnections.clear();
  669. fPreRenamePluginId.clear();
  670. fPreRenamePluginIcon.clear();
  671. #endif
  672. }
  673. void activate() noexcept override
  674. {
  675. carla_debug("CarlaEngineJackClient::activate()");
  676. if (getProcessMode() == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
  677. {
  678. CARLA_SAFE_ASSERT_RETURN(fJackClient != nullptr && ! isActive(),);
  679. try {
  680. jackbridge_activate(fJackClient);
  681. } catch(...) {}
  682. }
  683. CarlaEngineClient::activate();
  684. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  685. const CarlaMutexLocker cml(fPreRenameMutex);
  686. if (fJackClient != nullptr)
  687. {
  688. // restore pre-rename connections
  689. const char* portNameA = nullptr;
  690. const char* portNameB = nullptr;
  691. bool doConnection = false;
  692. for (CarlaStringList::Itenerator it = fPreRenameConnections.begin2(); it.valid(); it.next())
  693. {
  694. const bool connectNow = doConnection;
  695. doConnection = !doConnection;
  696. if (connectNow)
  697. portNameB = it.getValue(nullptr);
  698. else
  699. portNameA = it.getValue(nullptr);
  700. if (! connectNow)
  701. continue;
  702. CARLA_SAFE_ASSERT_CONTINUE(portNameA != nullptr && portNameA[0] != '\0');
  703. CARLA_SAFE_ASSERT_CONTINUE(portNameB != nullptr && portNameB[0] != '\0');
  704. jackbridge_connect(fJackClient, portNameA, portNameB);
  705. }
  706. if (fPreRenamePluginId.isNotEmpty())
  707. {
  708. const CarlaRecursiveMutexLocker crml(fThreadSafeMetadataMutex);
  709. if (char* const uuidstr = jackbridge_client_get_uuid(fJackClient))
  710. {
  711. jack_uuid_t uuid;
  712. if (jackbridge_uuid_parse(uuidstr, &uuid))
  713. {
  714. jackbridge_set_property(fJackClient, uuid,
  715. URI_MAIN_CLIENT_NAME,
  716. fMainClientName,
  717. URI_TYPE_STRING);
  718. jackbridge_set_property(fJackClient, uuid,
  719. URI_PLUGIN_ID,
  720. fPreRenamePluginId,
  721. URI_TYPE_INTEGER);
  722. if (fPreRenamePluginIcon.isNotEmpty())
  723. jackbridge_set_property(fJackClient, uuid,
  724. URI_PLUGIN_ICON,
  725. fPreRenamePluginIcon,
  726. URI_TYPE_STRING);
  727. }
  728. jackbridge_free(uuidstr);
  729. }
  730. }
  731. }
  732. fPreRenameConnections.clear();
  733. fPreRenamePluginId.clear();
  734. fPreRenamePluginIcon.clear();
  735. #endif
  736. }
  737. void deactivate(const bool willClose) noexcept override
  738. {
  739. carla_debug("CarlaEngineJackClient::deactivate(%s)", bool2str(willClose));
  740. if (getProcessMode() == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
  741. {
  742. if (fJackClient != nullptr && isActive())
  743. {
  744. try {
  745. jackbridge_deactivate(fJackClient);
  746. } catch(...) {}
  747. }
  748. }
  749. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  750. if (willClose)
  751. {
  752. fCVSourcePorts.resetGraphAndPlugin();
  753. fReservedPluginPtr = nullptr;
  754. }
  755. #endif
  756. CarlaEngineClient::deactivate(willClose);
  757. }
  758. bool isOk() const noexcept override
  759. {
  760. if (fUseClient)
  761. return (fJackClient != nullptr);
  762. return CarlaEngineClient::isOk();
  763. }
  764. CarlaEnginePort* addPort(const EnginePortType portType, const char* const name, const bool isInput, const uint32_t indexOffset) override
  765. {
  766. carla_debug("CarlaEngineJackClient::addPort(%i:%s, \"%s\", %s)", portType, EnginePortType2Str(portType), name, bool2str(isInput));
  767. jack_port_t* jackPort = nullptr;
  768. const char* realName = name;
  769. // Create JACK port first, if needed
  770. if (fUseClient)
  771. {
  772. CARLA_SAFE_ASSERT_RETURN(fJackClient != nullptr, nullptr);
  773. realName = pData->getUniquePortName(name);
  774. switch (portType)
  775. {
  776. case kEnginePortTypeNull:
  777. break;
  778. case kEnginePortTypeAudio:
  779. jackPort = jackbridge_port_register(fJackClient,
  780. realName,
  781. JACK_DEFAULT_AUDIO_TYPE,
  782. isInput ? JackPortIsInput : JackPortIsOutput,
  783. 0);
  784. break;
  785. case kEnginePortTypeCV:
  786. jackPort = jackbridge_port_register(fJackClient,
  787. realName,
  788. JACK_DEFAULT_AUDIO_TYPE,
  789. isInput ? JackPortIsInput : JackPortIsOutput,
  790. 0);
  791. break;
  792. case kEnginePortTypeEvent:
  793. jackPort = jackbridge_port_register(fJackClient,
  794. realName,
  795. JACK_DEFAULT_MIDI_TYPE,
  796. isInput ? JackPortIsInput : JackPortIsOutput,
  797. 0);
  798. break;
  799. }
  800. CARLA_SAFE_ASSERT_RETURN(jackPort != nullptr, nullptr);
  801. }
  802. // Create Engine port
  803. switch (portType)
  804. {
  805. case kEnginePortTypeNull:
  806. break;
  807. case kEnginePortTypeAudio: {
  808. pData->addAudioPortName(isInput, realName);
  809. if (realName != name) delete[] realName;
  810. CarlaEngineJackAudioPort* const enginePort(new CarlaEngineJackAudioPort(*this,
  811. isInput,
  812. indexOffset,
  813. fJackClient,
  814. jackPort,
  815. fThreadSafeMetadataMutex,
  816. this));
  817. fAudioPorts.append(enginePort);
  818. return enginePort;
  819. }
  820. case kEnginePortTypeCV: {
  821. pData->addCVPortName(isInput, realName);
  822. if (realName != name) delete[] realName;
  823. CarlaEngineJackCVPort* const enginePort(new CarlaEngineJackCVPort(*this,
  824. isInput,
  825. indexOffset,
  826. fJackClient,
  827. jackPort,
  828. fThreadSafeMetadataMutex,
  829. this));
  830. fCVPorts.append(enginePort);
  831. return enginePort;
  832. }
  833. case kEnginePortTypeEvent: {
  834. pData->addEventPortName(isInput, realName);
  835. if (realName != name) delete[] realName;
  836. CarlaEngineJackEventPort* const enginePort(new CarlaEngineJackEventPort(*this,
  837. isInput,
  838. indexOffset,
  839. fJackClient,
  840. jackPort,
  841. fThreadSafeMetadataMutex,
  842. this));
  843. fEventPorts.append(enginePort);
  844. return enginePort;
  845. }
  846. }
  847. carla_stderr("CarlaEngineJackClient::addPort(%i, \"%s\", %s) - invalid type", portType, name, bool2str(isInput));
  848. return nullptr;
  849. }
  850. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  851. CarlaEngineCVSourcePorts* createCVSourcePorts() override
  852. {
  853. fCVSourcePorts.setGraphAndPlugin(getPatchbayGraphOrNull(), getPlugin());
  854. return &fCVSourcePorts;
  855. }
  856. CarlaEngineJackCVSourcePorts& getCVSourcePorts() noexcept
  857. {
  858. return fCVSourcePorts;
  859. }
  860. #endif
  861. void invalidate() noexcept
  862. {
  863. for (LinkedList<CarlaEngineJackAudioPort*>::Itenerator it = fAudioPorts.begin2(); it.valid(); it.next())
  864. {
  865. CarlaEngineJackAudioPort* const port(it.getValue(nullptr));
  866. CARLA_SAFE_ASSERT_CONTINUE(port != nullptr);
  867. port->invalidate();
  868. }
  869. for (LinkedList<CarlaEngineJackCVPort*>::Itenerator it = fCVPorts.begin2(); it.valid(); it.next())
  870. {
  871. CarlaEngineJackCVPort* const port(it.getValue(nullptr));
  872. CARLA_SAFE_ASSERT_CONTINUE(port != nullptr);
  873. port->invalidate();
  874. }
  875. for (LinkedList<CarlaEngineJackEventPort*>::Itenerator it = fEventPorts.begin2(); it.valid(); it.next())
  876. {
  877. CarlaEngineJackEventPort* const port(it.getValue(nullptr));
  878. CARLA_SAFE_ASSERT_CONTINUE(port != nullptr);
  879. port->invalidate();
  880. }
  881. fJackClient = nullptr;
  882. CarlaEngineClient::deactivate(true);
  883. }
  884. const char* getJackClientName() const noexcept
  885. {
  886. CARLA_SAFE_ASSERT_RETURN(fJackClient != nullptr, nullptr);
  887. try {
  888. return jackbridge_get_client_name(fJackClient);
  889. } CARLA_SAFE_EXCEPTION_RETURN("jack_get_client_name", nullptr);
  890. }
  891. void jackAudioPortDeleted(CarlaEngineJackAudioPort* const port) noexcept override
  892. {
  893. fAudioPorts.removeAll(port);
  894. }
  895. void jackCVPortDeleted(CarlaEngineJackCVPort* const port) noexcept override
  896. {
  897. fCVPorts.removeAll(port);
  898. }
  899. void jackEventPortDeleted(CarlaEngineJackEventPort* const port) noexcept override
  900. {
  901. fEventPorts.removeAll(port);
  902. }
  903. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  904. bool renameInSingleClient(const CarlaString& newClientName)
  905. {
  906. const CarlaString clientNamePrefix(newClientName + ":");
  907. return _renamePorts(fAudioPorts, clientNamePrefix) &&
  908. _renamePorts(fCVPorts, clientNamePrefix) &&
  909. _renamePorts(fEventPorts, clientNamePrefix);
  910. }
  911. void closeForRename(jack_client_t* const newClient, const CarlaString& newClientName) noexcept
  912. {
  913. if (fJackClient != nullptr)
  914. {
  915. if (isActive())
  916. {
  917. {
  918. const CarlaString clientNamePrefix(newClientName + ":");
  919. // store current client connections
  920. const CarlaMutexLocker cml(fPreRenameMutex);
  921. fPreRenameConnections.clear();
  922. fPreRenamePluginId.clear();
  923. fPreRenamePluginIcon.clear();
  924. _savePortsConnections(fAudioPorts, clientNamePrefix);
  925. _savePortsConnections(fCVPorts, clientNamePrefix);
  926. _savePortsConnections(fEventPorts, clientNamePrefix);
  927. _saveProperties();
  928. }
  929. try {
  930. jackbridge_deactivate(fJackClient);
  931. } catch(...) {}
  932. }
  933. try {
  934. jackbridge_client_close(fJackClient);
  935. } catch(...) {}
  936. invalidate();
  937. }
  938. fAudioPorts.clear();
  939. fCVPorts.clear();
  940. fEventPorts.clear();
  941. pData->clearPorts();
  942. fJackClient = newClient;
  943. }
  944. void reservePluginPtr(CarlaPluginPtr* const pluginPtr)
  945. {
  946. fReservedPluginPtr = pluginPtr;
  947. }
  948. void setNewPluginId(const uint id) const
  949. {
  950. CARLA_SAFE_ASSERT_RETURN(fJackClient != nullptr,);
  951. // NOTE: no fThreadSafeMetadataMutex lock here, assumed done from caller
  952. if (char* const uuidstr = jackbridge_client_get_uuid(fJackClient))
  953. {
  954. jack_uuid_t uuid;
  955. if (jackbridge_uuid_parse(uuidstr, &uuid))
  956. {
  957. char buf[32];
  958. std::snprintf(buf, 31, "%u", id);
  959. buf[31] = '\0';
  960. jackbridge_set_property(fJackClient, uuid,
  961. URI_PLUGIN_ID,
  962. buf,
  963. URI_TYPE_INTEGER);
  964. }
  965. jackbridge_free(uuidstr);
  966. }
  967. }
  968. #endif
  969. private:
  970. jack_client_t* fJackClient;
  971. const bool fUseClient;
  972. LinkedList<CarlaEngineJackAudioPort*> fAudioPorts;
  973. LinkedList<CarlaEngineJackCVPort*> fCVPorts;
  974. LinkedList<CarlaEngineJackEventPort*> fEventPorts;
  975. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  976. CarlaEngineJackCVSourcePorts fCVSourcePorts;
  977. CarlaMutex fPreRenameMutex;
  978. CarlaStringList fPreRenameConnections;
  979. CarlaString fPreRenamePluginId;
  980. CarlaString fPreRenamePluginIcon;
  981. CarlaScopedPointer<CarlaPluginPtr> fReservedPluginPtr;
  982. template<typename T>
  983. bool _renamePorts(const LinkedList<T*>& t, const CarlaString& clientNamePrefix)
  984. {
  985. for (typename LinkedList<T*>::Itenerator it = t.begin2(); it.valid(); it.next())
  986. {
  987. T* const port(it.getValue(nullptr));
  988. CARLA_SAFE_ASSERT_CONTINUE(port != nullptr);
  989. CARLA_SAFE_ASSERT_CONTINUE(port->fJackPort != nullptr);
  990. const char* shortPortName(jackbridge_port_short_name(port->fJackPort));
  991. CARLA_SAFE_ASSERT_CONTINUE(shortPortName != nullptr && shortPortName[0] != '\0');
  992. const char* const oldClientNameSep(std::strstr(shortPortName, ":"));
  993. CARLA_SAFE_ASSERT_CONTINUE(oldClientNameSep != nullptr && oldClientNameSep[0] != '\0' && oldClientNameSep[1] != '\0');
  994. shortPortName += oldClientNameSep-shortPortName + 1;
  995. const CarlaString newPortName(clientNamePrefix + shortPortName);
  996. if (! jackbridge_port_rename(fJackClient, port->fJackPort, newPortName))
  997. return false;
  998. }
  999. return true;
  1000. }
  1001. template<typename T>
  1002. void _savePortsConnections(const LinkedList<T*>& t, const CarlaString& clientNamePrefix)
  1003. {
  1004. for (typename LinkedList<T*>::Itenerator it = t.begin2(); it.valid(); it.next())
  1005. {
  1006. T* const port(it.getValue(nullptr));
  1007. CARLA_SAFE_ASSERT_CONTINUE(port != nullptr);
  1008. CARLA_SAFE_ASSERT_CONTINUE(port->fJackPort != nullptr);
  1009. const char* const shortPortName(jackbridge_port_short_name(port->fJackPort));
  1010. CARLA_SAFE_ASSERT_CONTINUE(shortPortName != nullptr && shortPortName[0] != '\0');
  1011. const CarlaString portName(clientNamePrefix + shortPortName);
  1012. if (const char** const connections = jackbridge_port_get_all_connections(fJackClient, port->fJackPort))
  1013. {
  1014. for (int i=0; connections[i] != nullptr; ++i)
  1015. {
  1016. if (port->kIsInput)
  1017. {
  1018. fPreRenameConnections.append(connections[i]);
  1019. fPreRenameConnections.append(portName);
  1020. }
  1021. else
  1022. {
  1023. fPreRenameConnections.append(portName);
  1024. fPreRenameConnections.append(connections[i]);
  1025. }
  1026. }
  1027. jackbridge_free(connections);
  1028. }
  1029. }
  1030. }
  1031. void _saveProperties()
  1032. {
  1033. const CarlaRecursiveMutexLocker crml(fThreadSafeMetadataMutex);
  1034. if (char* const uuidstr = jackbridge_client_get_uuid(fJackClient))
  1035. {
  1036. jack_uuid_t uuid;
  1037. const bool parsed = jackbridge_uuid_parse(uuidstr, &uuid);
  1038. jackbridge_free(uuidstr);
  1039. CARLA_CUSTOM_SAFE_ASSERT_ONCE_RETURN("JACK meta-data support unavailable", parsed,);
  1040. char* value = nullptr;
  1041. char* type = nullptr;
  1042. CARLA_SAFE_ASSERT_RETURN(jackbridge_get_property(uuid,
  1043. URI_PLUGIN_ID,
  1044. &value,
  1045. &type),);
  1046. CARLA_SAFE_ASSERT_RETURN(type != nullptr,);
  1047. CARLA_SAFE_ASSERT_RETURN(std::strcmp(type, URI_TYPE_INTEGER) == 0,);
  1048. fPreRenamePluginId = value;
  1049. jackbridge_free(value);
  1050. jackbridge_free(type);
  1051. value = type = nullptr;
  1052. if (jackbridge_get_property(uuid, URI_PLUGIN_ICON, &value, &type))
  1053. {
  1054. CARLA_SAFE_ASSERT_RETURN(type != nullptr,);
  1055. CARLA_SAFE_ASSERT_RETURN(std::strcmp(type, URI_TYPE_STRING) == 0,);
  1056. fPreRenamePluginIcon = value;
  1057. jackbridge_free(value);
  1058. jackbridge_free(type);
  1059. }
  1060. }
  1061. }
  1062. #endif // BUILD_BRIDGE_ALTERNATIVE_ARCH
  1063. CarlaRecursiveMutex& fThreadSafeMetadataMutex;
  1064. const CarlaString& fMainClientName;
  1065. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJackClient)
  1066. };
  1067. // -----------------------------------------------------------------------
  1068. // Jack Engine
  1069. class CarlaEngineJack : public CarlaEngine
  1070. #ifndef BUILD_BRIDGE
  1071. , private CarlaThread
  1072. #endif
  1073. {
  1074. public:
  1075. CarlaEngineJack()
  1076. : CarlaEngine(),
  1077. #ifndef BUILD_BRIDGE
  1078. CarlaThread("CarlaEngineJackCallbacks"),
  1079. #endif
  1080. fClient(nullptr),
  1081. fExternalPatchbayHost(true),
  1082. fExternalPatchbayOsc(true),
  1083. fFreewheel(false),
  1084. fClientName(),
  1085. fThreadSafeMetadataMutex(),
  1086. #ifdef BUILD_BRIDGE
  1087. fIsRunning(false)
  1088. #else
  1089. fClientNamePrefix(),
  1090. fTimebaseMaster(false),
  1091. fTimebaseRolling(false),
  1092. fTimebaseUsecs(0),
  1093. fUsedGroups(),
  1094. fUsedPorts(),
  1095. fUsedConnections(),
  1096. fPatchbayProcThreadProtectionMutex(),
  1097. fRetConns(),
  1098. fLastPatchbaySetGroupPos(),
  1099. fPostPonedEvents(),
  1100. fPostPonedEventsMutex(),
  1101. fPostPonedUUIDs(),
  1102. fPostPonedUUIDsMutex(),
  1103. fIsInternalClient(false)
  1104. #endif
  1105. {
  1106. carla_debug("CarlaEngineJack::CarlaEngineJack()");
  1107. #ifdef BUILD_BRIDGE
  1108. pData->options.processMode = ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS;
  1109. #else
  1110. carla_zeroPointers(fRackPorts, kRackPortCount);
  1111. #endif
  1112. }
  1113. ~CarlaEngineJack() noexcept override
  1114. {
  1115. carla_debug("CarlaEngineJack::~CarlaEngineJack()");
  1116. CARLA_SAFE_ASSERT(fClient == nullptr);
  1117. #ifndef BUILD_BRIDGE
  1118. fUsedGroups.clear();
  1119. fUsedPorts.clear();
  1120. fUsedConnections.clear();
  1121. CARLA_SAFE_ASSERT(fPostPonedEvents.count() == 0);
  1122. #endif
  1123. }
  1124. // -------------------------------------------------------------------
  1125. // Maximum values
  1126. uint getMaxClientNameSize() const noexcept override
  1127. {
  1128. #ifndef BUILD_BRIDGE
  1129. if (pData->options.processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT ||
  1130. pData->options.processMode == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
  1131. #endif
  1132. {
  1133. try {
  1134. return static_cast<uint>(jackbridge_client_name_size()-1);
  1135. } CARLA_SAFE_EXCEPTION_RETURN("jack_client_name_size", 32);
  1136. }
  1137. return CarlaEngine::getMaxClientNameSize();
  1138. }
  1139. uint getMaxPortNameSize() const noexcept override
  1140. {
  1141. if (pData->options.processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT || pData->options.processMode == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
  1142. {
  1143. try {
  1144. return static_cast<uint>(jackbridge_port_name_size()-1);
  1145. } CARLA_SAFE_EXCEPTION_RETURN("jack_port_name_size", 255);
  1146. }
  1147. return CarlaEngine::getMaxPortNameSize();
  1148. }
  1149. // -------------------------------------------------------------------
  1150. // Virtual, per-engine type calls
  1151. bool init(const char* const clientName) override
  1152. {
  1153. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr || (clientName != nullptr && clientName[0] != '\0'), false);
  1154. CARLA_SAFE_ASSERT_RETURN(jackbridge_is_ok(), false);
  1155. carla_debug("CarlaEngineJack::init(\"%s\")", clientName);
  1156. fFreewheel = false;
  1157. fExternalPatchbayHost = true;
  1158. fExternalPatchbayOsc = true;
  1159. CarlaString truncatedClientName;
  1160. if (fClient == nullptr && clientName != nullptr)
  1161. {
  1162. truncatedClientName = clientName;
  1163. truncatedClientName.truncate(getMaxClientNameSize());
  1164. }
  1165. #ifdef BUILD_BRIDGE
  1166. fIsRunning = true;
  1167. if (! pData->init(truncatedClientName))
  1168. {
  1169. close();
  1170. setLastError("Failed to init internal data");
  1171. return false;
  1172. }
  1173. if (pData->bufferSize == 0 || carla_isEqual(pData->sampleRate, 0.0))
  1174. {
  1175. // open temp client to get initial buffer-size and sample-rate values
  1176. if (jack_client_t* const tmpClient = jackbridge_client_open(truncatedClientName, JackNoStartServer, nullptr))
  1177. {
  1178. pData->bufferSize = jackbridge_get_buffer_size(tmpClient);
  1179. pData->sampleRate = jackbridge_get_sample_rate(tmpClient);
  1180. jackbridge_client_close(tmpClient);
  1181. }
  1182. else
  1183. {
  1184. close();
  1185. setLastError("Failed to init temporary jack client");
  1186. return false;
  1187. }
  1188. }
  1189. return true;
  1190. #else // BUILD_BRIDGE
  1191. if (fClient == nullptr && clientName != nullptr)
  1192. fClient = jackbridge_client_open(truncatedClientName, JackNoStartServer, nullptr);
  1193. if (fClient == nullptr)
  1194. {
  1195. setLastError("Failed to create new JACK client");
  1196. return false;
  1197. }
  1198. const char* const jackClientName = jackbridge_get_client_name(fClient);
  1199. if (! pData->init(jackClientName))
  1200. {
  1201. jackbridge_client_close(fClient);
  1202. fClient = nullptr;
  1203. setLastError("Failed to init internal data");
  1204. return false;
  1205. }
  1206. fClientName = jackClientName;
  1207. const EngineOptions& opts(pData->options);
  1208. pData->bufferSize = jackbridge_get_buffer_size(fClient);
  1209. pData->sampleRate = jackbridge_get_sample_rate(fClient);
  1210. pData->initTime(opts.transportExtra);
  1211. jackbridge_set_thread_init_callback(fClient, carla_jack_thread_init_callback, nullptr);
  1212. jackbridge_set_buffer_size_callback(fClient, carla_jack_bufsize_callback, this);
  1213. jackbridge_set_sample_rate_callback(fClient, carla_jack_srate_callback, this);
  1214. jackbridge_set_freewheel_callback(fClient, carla_jack_freewheel_callback, this);
  1215. jackbridge_set_latency_callback(fClient, carla_jack_latency_callback, this);
  1216. jackbridge_set_process_callback(fClient, carla_jack_process_callback, this);
  1217. jackbridge_on_shutdown(fClient, carla_jack_shutdown_callback, this);
  1218. fTimebaseRolling = false;
  1219. if (opts.transportMode == ENGINE_TRANSPORT_MODE_JACK)
  1220. fTimebaseMaster = jackbridge_set_timebase_callback(fClient, true, carla_jack_timebase_callback, this);
  1221. else
  1222. fTimebaseMaster = false;
  1223. if (opts.processMode != ENGINE_PROCESS_MODE_PATCHBAY)
  1224. initJackPatchbay(true, false, jackClientName);
  1225. jackbridge_set_client_registration_callback(fClient, carla_jack_client_registration_callback, this);
  1226. jackbridge_set_port_registration_callback(fClient, carla_jack_port_registration_callback, this);
  1227. jackbridge_set_port_connect_callback(fClient, carla_jack_port_connect_callback, this);
  1228. jackbridge_set_port_rename_callback(fClient, carla_jack_port_rename_callback, this);
  1229. jackbridge_set_property_change_callback(fClient, carla_jack_property_change_callback, this);
  1230. jackbridge_set_xrun_callback(fClient, carla_jack_xrun_callback, this);
  1231. if (opts.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK || opts.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1232. {
  1233. fRackPorts[kRackPortAudioIn1] = jackbridge_port_register(fClient, "audio-in1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  1234. fRackPorts[kRackPortAudioIn2] = jackbridge_port_register(fClient, "audio-in2", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  1235. fRackPorts[kRackPortAudioOut1] = jackbridge_port_register(fClient, "audio-out1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  1236. fRackPorts[kRackPortAudioOut2] = jackbridge_port_register(fClient, "audio-out2", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  1237. fRackPorts[kRackPortEventIn] = jackbridge_port_register(fClient, "events-in", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
  1238. fRackPorts[kRackPortEventOut] = jackbridge_port_register(fClient, "events-out", JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
  1239. if (opts.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  1240. {
  1241. // FIXME?
  1242. pData->graph.create(0, 0, 0, 0);
  1243. }
  1244. else
  1245. {
  1246. pData->graph.create(2, 2, 0, 0);
  1247. // pData->graph.setUsingExternalHost(true);
  1248. // pData->graph.setUsingExternalOSC(true);
  1249. patchbayRefresh(true, false, false);
  1250. }
  1251. }
  1252. # ifdef HAVE_LIBLO
  1253. {
  1254. const CarlaString& tcp(pData->osc.getServerPathTCP());
  1255. const CarlaString& udp(pData->osc.getServerPathUDP());
  1256. if (tcp.isNotEmpty() || udp.isNotEmpty())
  1257. {
  1258. const CarlaRecursiveMutexLocker crml(fThreadSafeMetadataMutex);
  1259. if (char* const uuidstr = jackbridge_client_get_uuid(fClient))
  1260. {
  1261. jack_uuid_t uuid;
  1262. if (jackbridge_uuid_parse(uuidstr, &uuid))
  1263. {
  1264. if (tcp.isNotEmpty())
  1265. jackbridge_set_property(fClient, uuid,
  1266. "https://kx.studio/ns/carla/osc-tcp", tcp, URI_TYPE_STRING);
  1267. if (udp.isNotEmpty())
  1268. jackbridge_set_property(fClient, uuid,
  1269. "https://kx.studio/ns/carla/osc-udp", udp, URI_TYPE_STRING);
  1270. }
  1271. jackbridge_free(uuidstr);
  1272. }
  1273. }
  1274. }
  1275. # endif
  1276. if (jackbridge_activate(fClient))
  1277. {
  1278. if (opts.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1279. opts.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1280. {
  1281. if (pData->options.audioDevice != nullptr &&
  1282. std::strcmp(pData->options.audioDevice, "Auto-Connect ON") == 0 &&
  1283. std::getenv("LADISH_APP_NAME") == nullptr &&
  1284. std::getenv("NSM_URL") == nullptr)
  1285. {
  1286. char strBuf[STR_MAX];
  1287. if (jackbridge_port_by_name(fClient, "system:capture_1") != nullptr)
  1288. {
  1289. std::snprintf(strBuf, STR_MAX, "%s:audio-in1", jackClientName);
  1290. strBuf[STR_MAX-1] = '\0';
  1291. jackbridge_connect(fClient, "system:capture_1", strBuf);
  1292. std::snprintf(strBuf, STR_MAX, "%s:audio-in2", jackClientName);
  1293. strBuf[STR_MAX-1] = '\0';
  1294. if (jackbridge_port_by_name(fClient, "system:capture_2") != nullptr)
  1295. jackbridge_connect(fClient, "system:capture_2", strBuf);
  1296. else
  1297. jackbridge_connect(fClient, "system:capture_1", strBuf);
  1298. }
  1299. if (jackbridge_port_by_name(fClient, "system:playback_1") != nullptr)
  1300. {
  1301. std::snprintf(strBuf, STR_MAX, "%s:audio-out1", jackClientName);
  1302. strBuf[STR_MAX-1] = '\0';
  1303. jackbridge_connect(fClient, strBuf, "system:playback_1");
  1304. std::snprintf(strBuf, STR_MAX, "%s:audio-out2", jackClientName);
  1305. strBuf[STR_MAX-1] = '\0';
  1306. if (jackbridge_port_by_name(fClient, "system:playback_2") != nullptr)
  1307. jackbridge_connect(fClient, strBuf, "system:playback_2");
  1308. else
  1309. jackbridge_connect(fClient, strBuf, "system:playback_1");
  1310. }
  1311. }
  1312. }
  1313. startThread();
  1314. callback(true, true,
  1315. ENGINE_CALLBACK_ENGINE_STARTED, 0,
  1316. opts.processMode,
  1317. opts.transportMode,
  1318. static_cast<int>(pData->bufferSize),
  1319. static_cast<float>(pData->sampleRate),
  1320. getCurrentDriverName());
  1321. return true;
  1322. }
  1323. if (opts.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1324. opts.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1325. {
  1326. pData->graph.destroy();
  1327. }
  1328. pData->close();
  1329. jackbridge_client_close(fClient);
  1330. fClient = nullptr;
  1331. setLastError("Failed to activate the JACK client");
  1332. return false;
  1333. #endif // BUILD_BRIDGE
  1334. }
  1335. #ifndef BUILD_BRIDGE
  1336. bool initInternal(jack_client_t* const client)
  1337. {
  1338. fClient = client;
  1339. fIsInternalClient = true;
  1340. return init(nullptr);
  1341. }
  1342. #endif
  1343. bool close() override
  1344. {
  1345. carla_debug("CarlaEngineJack::close()");
  1346. #ifdef BUILD_BRIDGE
  1347. fClient = nullptr;
  1348. fIsRunning = false;
  1349. CarlaEngine::close();
  1350. return true;
  1351. #else
  1352. stopThread(-1);
  1353. fClientName.clear();
  1354. fPostPonedEvents.clear();
  1355. if (fClient != nullptr)
  1356. {
  1357. // deactivate and close client
  1358. jackbridge_deactivate(fClient);
  1359. jackbridge_client_close(fClient);
  1360. fClient = nullptr;
  1361. }
  1362. // clear engine data
  1363. CarlaEngine::close();
  1364. fUsedGroups.clear();
  1365. fUsedPorts.clear();
  1366. fUsedConnections.clear();
  1367. fPostPonedEvents.clear();
  1368. // clear rack/patchbay stuff
  1369. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1370. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1371. {
  1372. carla_zeroPointers(fRackPorts, kRackPortCount);
  1373. pData->graph.destroy();
  1374. }
  1375. return true;
  1376. #endif
  1377. }
  1378. bool isRunning() const noexcept override
  1379. {
  1380. #ifdef BUILD_BRIDGE
  1381. return (fClient != nullptr || fIsRunning);
  1382. #else
  1383. return (fClient != nullptr);
  1384. #endif
  1385. }
  1386. bool isOffline() const noexcept override
  1387. {
  1388. return fFreewheel;
  1389. }
  1390. EngineType getType() const noexcept override
  1391. {
  1392. return kEngineTypeJack;
  1393. }
  1394. const char* getCurrentDriverName() const noexcept override
  1395. {
  1396. return "JACK";
  1397. }
  1398. #ifndef BUILD_BRIDGE
  1399. float getDSPLoad() const noexcept override
  1400. {
  1401. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, 0.0f);
  1402. return jackbridge_cpu_load(fClient);
  1403. }
  1404. void callback(const bool sendHost, const bool sendOsc,
  1405. const EngineCallbackOpcode action, const uint pluginId,
  1406. const int value1, const int value2, const int value3,
  1407. const float valuef, const char* const valueStr) noexcept override
  1408. {
  1409. if (action == ENGINE_CALLBACK_PROJECT_LOAD_FINISHED)
  1410. {
  1411. if (fTimebaseMaster)
  1412. {
  1413. // project finished loading, need to set bpm here, so we force an update of timebase master
  1414. transportRelocate(pData->timeInfo.frame);
  1415. }
  1416. }
  1417. CarlaEngine::callback(sendHost, sendOsc, action, pluginId, value1, value2, value3, valuef, valueStr);
  1418. }
  1419. void idle() noexcept override
  1420. {
  1421. water::Array<jack_uuid_t> uuids;
  1422. {
  1423. const CarlaMutexLocker cml(fPostPonedUUIDsMutex);
  1424. fPostPonedUUIDs.swapWith(uuids);
  1425. }
  1426. {
  1427. const CarlaRecursiveMutexLocker crml(fThreadSafeMetadataMutex);
  1428. for (int i=0; i<uuids.size(); ++i)
  1429. {
  1430. jack_uuid_t uuid = uuids.getUnchecked(i);
  1431. char uuidstr[JACK_UUID_STRING_SIZE];
  1432. carla_zeroStruct(uuidstr);
  1433. jackbridge_uuid_unparse(uuid, uuidstr);
  1434. if (char* const clientName = jackbridge_get_client_name_by_uuid(fClient, uuidstr))
  1435. {
  1436. CARLA_SAFE_ASSERT_RETURN(clientName != nullptr && clientName[0] != '\0',);
  1437. uint groupId;
  1438. {
  1439. const CarlaMutexLocker cml(fUsedGroups.mutex);
  1440. groupId = fUsedGroups.getGroupId(clientName);
  1441. }
  1442. jackbridge_free(clientName);
  1443. CARLA_SAFE_ASSERT_RETURN(groupId != 0,);
  1444. char* value = nullptr;
  1445. char* type = nullptr;
  1446. if (jackbridge_get_property(uuid, URI_POSITION, &value, &type)
  1447. && value != nullptr
  1448. && type != nullptr
  1449. && std::strcmp(type, URI_TYPE_STRING) == 0)
  1450. {
  1451. if (char* sep1 = std::strstr(value, ":"))
  1452. {
  1453. LastPatchbaySetGroupPos pos;
  1454. *sep1++ = '\0';
  1455. pos.x1 = std::atoi(value);
  1456. if (char* sep2 = std::strstr(sep1, ":"))
  1457. {
  1458. *sep2++ = '\0';
  1459. pos.y1 = std::atoi(sep1);
  1460. if (char* sep3 = std::strstr(sep2, ":"))
  1461. {
  1462. *sep3++ = '\0';
  1463. pos.x2 = std::atoi(sep2);
  1464. pos.y2 = std::atoi(sep3);
  1465. }
  1466. if (fLastPatchbaySetGroupPos != pos)
  1467. {
  1468. fLastPatchbaySetGroupPos.clear();
  1469. callback(fExternalPatchbayHost, fExternalPatchbayOsc,
  1470. ENGINE_CALLBACK_PATCHBAY_CLIENT_POSITION_CHANGED,
  1471. groupId, pos.x1, pos.y1, pos.x2, static_cast<float>(pos.y2),
  1472. nullptr);
  1473. }
  1474. }
  1475. }
  1476. jackbridge_free(value);
  1477. jackbridge_free(type);
  1478. }
  1479. }
  1480. }
  1481. }
  1482. CarlaEngine::idle();
  1483. }
  1484. #endif
  1485. bool setBufferSizeAndSampleRate(const uint bufferSize, const double sampleRate) override
  1486. {
  1487. CARLA_SAFE_ASSERT_RETURN(carla_isEqual(pData->sampleRate, sampleRate), false);
  1488. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, false);
  1489. try {
  1490. return jackbridge_set_buffer_size(fClient, bufferSize);
  1491. } CARLA_SAFE_EXCEPTION_RETURN("setBufferSizeAndSampleRate", false);
  1492. }
  1493. EngineTimeInfo getTimeInfo() const noexcept override
  1494. {
  1495. if (pData->options.transportMode != ENGINE_TRANSPORT_MODE_JACK)
  1496. return CarlaEngine::getTimeInfo();
  1497. if (pData->options.processMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
  1498. return CarlaEngine::getTimeInfo();
  1499. jack_position_t jpos;
  1500. // invalidate
  1501. jpos.unique_1 = 1;
  1502. jpos.unique_2 = 2;
  1503. EngineTimeInfo timeInfo;
  1504. const bool playing = jackbridge_transport_query(fClient, &jpos) == JackTransportRolling;
  1505. if (jpos.unique_1 != jpos.unique_2)
  1506. {
  1507. timeInfo.playing = false;
  1508. timeInfo.frame = 0;
  1509. timeInfo.usecs = 0;
  1510. timeInfo.bbt.valid = false;
  1511. return timeInfo;
  1512. }
  1513. timeInfo.playing = playing;
  1514. timeInfo.frame = jpos.frame;
  1515. timeInfo.usecs = jpos.usecs;
  1516. if (jpos.valid & JackPositionBBT)
  1517. {
  1518. timeInfo.bbt.valid = true;
  1519. timeInfo.bbt.bar = jpos.bar;
  1520. timeInfo.bbt.beat = jpos.beat;
  1521. timeInfo.bbt.tick = jpos.tick;
  1522. timeInfo.bbt.barStartTick = jpos.bar_start_tick;
  1523. timeInfo.bbt.beatsPerBar = jpos.beats_per_bar;
  1524. timeInfo.bbt.beatType = jpos.beat_type;
  1525. timeInfo.bbt.ticksPerBeat = jpos.ticks_per_beat;
  1526. timeInfo.bbt.beatsPerMinute = jpos.beats_per_minute;
  1527. }
  1528. else
  1529. {
  1530. timeInfo.bbt.valid = false;
  1531. }
  1532. return timeInfo;
  1533. }
  1534. #ifndef BUILD_BRIDGE
  1535. void setOption(const EngineOption option, const int value, const char* const valueStr) noexcept override
  1536. {
  1537. if (option == ENGINE_OPTION_TRANSPORT_MODE && fClient != nullptr)
  1538. {
  1539. CARLA_SAFE_ASSERT_INT_RETURN(value >= ENGINE_TRANSPORT_MODE_DISABLED &&
  1540. value <= ENGINE_TRANSPORT_MODE_JACK, value,);
  1541. if (value == ENGINE_TRANSPORT_MODE_JACK)
  1542. {
  1543. fTimebaseMaster = jackbridge_set_timebase_callback(fClient, true, carla_jack_timebase_callback, this);
  1544. }
  1545. else
  1546. {
  1547. // jack transport cannot be disabled in multi-client
  1548. callback(true, true,
  1549. ENGINE_CALLBACK_TRANSPORT_MODE_CHANGED, 0,
  1550. ENGINE_TRANSPORT_MODE_JACK,
  1551. 0, 0, 0.0f,
  1552. pData->options.transportExtra);
  1553. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS,);
  1554. jackbridge_release_timebase(fClient);
  1555. fTimebaseMaster = false;
  1556. }
  1557. }
  1558. else if (option == ENGINE_OPTION_CLIENT_NAME_PREFIX)
  1559. {
  1560. fClientNamePrefix = valueStr;
  1561. if (fClientNamePrefix.isNotEmpty() && ! fClientNamePrefix.endsWith('/'))
  1562. fClientNamePrefix += "/";
  1563. }
  1564. CarlaEngine::setOption(option, value, valueStr);
  1565. }
  1566. #endif
  1567. CarlaEngineClient* addClient(CarlaPluginPtr plugin) override
  1568. {
  1569. jack_client_t* client = nullptr;
  1570. #ifndef BUILD_BRIDGE
  1571. CARLA_CUSTOM_SAFE_ASSERT_RETURN("Not connected to JACK", fClient != nullptr, nullptr);
  1572. CarlaPluginPtr* pluginReserve = nullptr;
  1573. if (pData->options.processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1574. {
  1575. client = fClient;
  1576. }
  1577. else if (pData->options.processMode == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
  1578. #endif
  1579. {
  1580. #ifndef BUILD_BRIDGE
  1581. if (fClientNamePrefix.isNotEmpty())
  1582. {
  1583. client = jackbridge_client_open(fClientNamePrefix + plugin->getName(), JackNoStartServer, nullptr);
  1584. }
  1585. else
  1586. #endif
  1587. {
  1588. client = jackbridge_client_open(plugin->getName(), JackNoStartServer, nullptr);
  1589. }
  1590. CARLA_CUSTOM_SAFE_ASSERT_RETURN("Failure to open client", client != nullptr, nullptr);
  1591. jackbridge_set_thread_init_callback(client, carla_jack_thread_init_callback, nullptr);
  1592. const CarlaRecursiveMutexLocker crml(fThreadSafeMetadataMutex);
  1593. if (char* const uuidstr = jackbridge_client_get_uuid(client))
  1594. {
  1595. jack_uuid_t uuid;
  1596. if (jackbridge_uuid_parse(uuidstr, &uuid))
  1597. {
  1598. char strBufId[24];
  1599. std::snprintf(strBufId, 23, "%u", plugin->getId());
  1600. strBufId[23] = '\0';
  1601. #ifndef BUILD_BRIDGE
  1602. jackbridge_set_property(client, uuid,
  1603. URI_MAIN_CLIENT_NAME,
  1604. fClientName,
  1605. URI_TYPE_STRING);
  1606. jackbridge_set_property(client, uuid,
  1607. URI_PLUGIN_ID,
  1608. strBufId,
  1609. URI_TYPE_INTEGER);
  1610. #endif
  1611. if (const char* const pluginIcon = plugin->getIconName())
  1612. jackbridge_set_property(client, uuid,
  1613. URI_PLUGIN_ICON,
  1614. pluginIcon,
  1615. URI_TYPE_STRING);
  1616. }
  1617. jackbridge_free(uuidstr);
  1618. }
  1619. #ifndef BUILD_BRIDGE
  1620. pluginReserve = new CarlaPluginPtr(plugin);
  1621. /*
  1622. jackbridge_set_buffer_size_callback(fClient, carla_jack_bufsize_callback_plugin, pluginReserve);
  1623. jackbridge_set_sample_rate_callback(fClient, carla_jack_srate_callback_plugin, pluginReserve);
  1624. */
  1625. jackbridge_set_latency_callback(client, carla_jack_latency_callback_plugin, pluginReserve);
  1626. jackbridge_set_process_callback(client, carla_jack_process_callback_plugin, pluginReserve);
  1627. jackbridge_on_shutdown(client, carla_jack_shutdown_callback_plugin, pluginReserve);
  1628. #else
  1629. fClient = client;
  1630. pData->bufferSize = jackbridge_get_buffer_size(client);
  1631. pData->sampleRate = jackbridge_get_sample_rate(client);
  1632. pData->initTime(nullptr);
  1633. jackbridge_set_buffer_size_callback(client, carla_jack_bufsize_callback, this);
  1634. jackbridge_set_sample_rate_callback(client, carla_jack_srate_callback, this);
  1635. jackbridge_set_freewheel_callback(client, carla_jack_freewheel_callback, this);
  1636. jackbridge_set_latency_callback(client, carla_jack_latency_callback, this);
  1637. jackbridge_set_process_callback(client, carla_jack_process_callback, this);
  1638. jackbridge_on_shutdown(client, carla_jack_shutdown_callback, this);
  1639. fClientName = jackbridge_get_client_name(client);
  1640. #endif
  1641. }
  1642. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1643. carla_debug("new CarlaEngineJackClient");
  1644. CarlaEngineJackClient* const jclient = new CarlaEngineJackClient(*this,
  1645. pData->graph,
  1646. fThreadSafeMetadataMutex,
  1647. plugin, fClientName, client);
  1648. # ifndef BUILD_BRIDGE
  1649. if (pluginReserve != nullptr)
  1650. jclient->reservePluginPtr(pluginReserve);
  1651. # endif
  1652. return jclient;
  1653. #else
  1654. return new CarlaEngineJackClient(*this, fThreadSafeMetadataMutex, fClientName, client);
  1655. #endif
  1656. }
  1657. #ifndef BUILD_BRIDGE
  1658. bool removePlugin(const uint id) override
  1659. {
  1660. if (! CarlaEngine::removePlugin(id))
  1661. return false;
  1662. const CarlaRecursiveMutexLocker crml(fThreadSafeMetadataMutex);
  1663. for (uint i=id; i < pData->curPluginCount; ++i)
  1664. {
  1665. const CarlaPluginPtr plugin = pData->plugins[i].plugin;
  1666. CARLA_SAFE_ASSERT_BREAK(plugin.get() != nullptr);
  1667. CarlaEngineJackClient* const client = dynamic_cast<CarlaEngineJackClient*>(plugin->getEngineClient());
  1668. CARLA_SAFE_ASSERT_BREAK(client != nullptr);
  1669. client->setNewPluginId(i);
  1670. }
  1671. return true;
  1672. }
  1673. bool switchPlugins(const uint idA, const uint idB) noexcept override
  1674. {
  1675. if (! CarlaEngine::switchPlugins(idA, idB))
  1676. return false;
  1677. CarlaPluginPtr newPluginA = pData->plugins[idA].plugin;
  1678. CARLA_SAFE_ASSERT_RETURN(newPluginA.get() != nullptr, true);
  1679. CarlaPluginPtr newPluginB = pData->plugins[idB].plugin;
  1680. CARLA_SAFE_ASSERT_RETURN(newPluginB.get() != nullptr, true);
  1681. CarlaEngineJackClient* const clientA = dynamic_cast<CarlaEngineJackClient*>(newPluginA->getEngineClient());
  1682. CARLA_SAFE_ASSERT_RETURN(clientA != nullptr, true);
  1683. CarlaEngineJackClient* const clientB = dynamic_cast<CarlaEngineJackClient*>(newPluginB->getEngineClient());
  1684. CARLA_SAFE_ASSERT_RETURN(clientB != nullptr, true);
  1685. {
  1686. const CarlaRecursiveMutexLocker crml(fThreadSafeMetadataMutex);
  1687. clientA->setNewPluginId(idA);
  1688. clientB->setNewPluginId(idB);
  1689. }
  1690. return true;
  1691. }
  1692. bool renamePlugin(const uint id, const char* const newName) override
  1693. {
  1694. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  1695. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1696. {
  1697. return CarlaEngine::renamePlugin(id, newName);
  1698. }
  1699. CARLA_SAFE_ASSERT_RETURN(pData->plugins != nullptr, false);
  1700. CARLA_SAFE_ASSERT_RETURN(pData->curPluginCount != 0, false);
  1701. CARLA_SAFE_ASSERT_RETURN(id < pData->curPluginCount, false);
  1702. CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0', false);
  1703. CarlaPluginPtr plugin = pData->plugins[id].plugin;
  1704. CARLA_SAFE_ASSERT_RETURN_ERR(plugin.get() != nullptr, "Could not find plugin to rename");
  1705. CARLA_SAFE_ASSERT_RETURN_ERR(plugin->getId() == id, "Invalid engine internal data");
  1706. // before we stop the engine thread we might need to get the plugin data
  1707. const bool needsReinit = (pData->options.processMode == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS);
  1708. const CarlaStateSave* saveStatePtr = nullptr;
  1709. if (needsReinit)
  1710. {
  1711. const CarlaStateSave& saveState(plugin->getStateSave());
  1712. saveStatePtr = &saveState;
  1713. }
  1714. CarlaString uniqueName;
  1715. try {
  1716. const char* const tmpName = getUniquePluginName(newName);
  1717. uniqueName = tmpName;
  1718. delete[] tmpName;
  1719. } CARLA_SAFE_EXCEPTION("JACK renamePlugin getUniquePluginName");
  1720. if (uniqueName.isEmpty())
  1721. {
  1722. setLastError("Failed to request new unique plugin name");
  1723. return false;
  1724. }
  1725. const ScopedThreadStopper sts(this);
  1726. // rename on client client mode, just rename the ports
  1727. if (pData->options.processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1728. {
  1729. CarlaEngineJackClient* const client((CarlaEngineJackClient*)plugin->getEngineClient());
  1730. if (! client->renameInSingleClient(uniqueName))
  1731. {
  1732. setLastError("Failed to rename some JACK ports, does your JACK version support proper port renaming?");
  1733. return false;
  1734. }
  1735. }
  1736. // rename in multiple client mode
  1737. else if (pData->options.processMode == ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
  1738. {
  1739. CarlaEngineJackClient* const client((CarlaEngineJackClient*)plugin->getEngineClient());
  1740. // we should not be able to do this, jack really needs to allow client rename
  1741. if (jack_client_t* const jackClient = jackbridge_client_open(fClientNamePrefix + uniqueName,
  1742. JackNoStartServer, nullptr))
  1743. {
  1744. // get new client name
  1745. fClientName = jackbridge_get_client_name(jackClient);
  1746. if (fClientNamePrefix.isNotEmpty())
  1747. uniqueName = fClientName.buffer() + fClientNamePrefix.length();
  1748. else
  1749. uniqueName = fClientName;
  1750. // close client
  1751. client->closeForRename(jackClient, fClientName);
  1752. // disable plugin
  1753. plugin->setEnabled(false);
  1754. // set new client data
  1755. CarlaPluginPtr* const pluginReserve = new CarlaPluginPtr(plugin);
  1756. client->reservePluginPtr(pluginReserve);
  1757. jackbridge_set_latency_callback(jackClient, carla_jack_latency_callback_plugin, pluginReserve);
  1758. jackbridge_set_process_callback(jackClient, carla_jack_process_callback_plugin, pluginReserve);
  1759. jackbridge_on_shutdown(jackClient, carla_jack_shutdown_callback_plugin, pluginReserve);
  1760. // NOTE: jack1 locks up here
  1761. if (jackbridge_get_version_string() != nullptr)
  1762. jackbridge_set_thread_init_callback(jackClient, carla_jack_thread_init_callback, nullptr);
  1763. /* The following code is because of a tricky situation.
  1764. We cannot lock or do jack operations during jack callbacks on jack1. jack2 events are asynchronous.
  1765. When we close the client jack will trigger unregister-port callbacks, which we handle on a separate thread ASAP.
  1766. But before that happens we already registered a new client with the same ports (the "renamed" one),
  1767. and at this point the port we receive during that callback is actually the new one from the new client..
  1768. JACK2 seems to be reusing ports to save space, which is understandable.
  1769. Anyway, this means we have to remove all our port-related data before the new client ports are created.
  1770. (we also stop the separate jack-events thread to avoid any race conditions while modying our port data) */
  1771. stopThread(-1);
  1772. LinkedList<PortNameToId> ports;
  1773. LinkedList<ConnectionToId> conns;
  1774. {
  1775. const CarlaMutexLocker cml1(fUsedGroups.mutex);
  1776. if (const uint groupId = fUsedGroups.getGroupId(plugin->getName()))
  1777. {
  1778. const CarlaMutexLocker cml2(fUsedPorts.mutex);
  1779. for (LinkedList<PortNameToId>::Itenerator it = fUsedPorts.list.begin2(); it.valid(); it.next())
  1780. {
  1781. const PortNameToId& portNameToId(it.getValue(kPortNameToIdFallback));
  1782. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group != 0);
  1783. if (portNameToId.group != groupId)
  1784. continue;
  1785. ports.append(portNameToId);
  1786. fUsedPorts.list.remove(it);
  1787. }
  1788. const CarlaMutexLocker cml3(fUsedConnections.mutex);
  1789. for (LinkedList<ConnectionToId>::Itenerator it = fUsedConnections.list.begin2(); it.valid(); it.next())
  1790. {
  1791. const ConnectionToId& connectionToId = it.getValue(kConnectionToIdFallback);
  1792. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id != 0);
  1793. if (connectionToId.groupA != groupId && connectionToId.groupB != groupId)
  1794. continue;
  1795. conns.append(connectionToId);
  1796. fUsedConnections.list.remove(it);
  1797. }
  1798. }
  1799. }
  1800. for (LinkedList<ConnectionToId>::Itenerator it = conns.begin2(); it.valid(); it.next())
  1801. {
  1802. const ConnectionToId& connectionToId = it.getValue(kConnectionToIdFallback);
  1803. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id != 0);
  1804. callback(fExternalPatchbayHost, fExternalPatchbayOsc,
  1805. ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED,
  1806. connectionToId.id,
  1807. 0, 0, 0, 0.0f, nullptr);
  1808. }
  1809. for (LinkedList<PortNameToId>::Itenerator it = ports.begin2(); it.valid(); it.next())
  1810. {
  1811. const PortNameToId& portNameToId(it.getValue(kPortNameToIdFallback));
  1812. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group != 0);
  1813. callback(fExternalPatchbayHost, fExternalPatchbayOsc,
  1814. ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED,
  1815. portNameToId.group,
  1816. static_cast<int>(portNameToId.port),
  1817. 0, 0, 0.0f, nullptr);
  1818. }
  1819. ports.clear();
  1820. conns.clear();
  1821. startThread();
  1822. }
  1823. else
  1824. {
  1825. setLastError("Failed to create new JACK client");
  1826. return false;
  1827. }
  1828. }
  1829. // Rename
  1830. plugin->setName(uniqueName);
  1831. if (needsReinit)
  1832. {
  1833. // reload plugin to recreate its ports
  1834. plugin->reload();
  1835. plugin->loadStateSave(*saveStatePtr);
  1836. plugin->setEnabled(true);
  1837. }
  1838. callback(true, true, ENGINE_CALLBACK_PLUGIN_RENAMED, id, 0, 0, 0, 0.0f, uniqueName);
  1839. return true;
  1840. }
  1841. // -------------------------------------------------------------------
  1842. // Patchbay
  1843. bool patchbayConnect(const bool external,
  1844. const uint groupA, const uint portA, const uint groupB, const uint portB) override
  1845. {
  1846. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, false);
  1847. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY && ! external)
  1848. return CarlaEngine::patchbayConnect(false, groupA, portA, groupB, portB);
  1849. const CarlaMutexLocker cml(fUsedPorts.mutex);
  1850. const char* const fullPortNameA = fUsedPorts.getFullPortName(groupA, portA);
  1851. CARLA_SAFE_ASSERT_RETURN(fullPortNameA != nullptr && fullPortNameA[0] != '\0', false);
  1852. const char* const fullPortNameB = fUsedPorts.getFullPortName(groupB, portB);
  1853. CARLA_SAFE_ASSERT_RETURN(fullPortNameB != nullptr && fullPortNameB[0] != '\0', false);
  1854. if (! jackbridge_connect(fClient, fullPortNameA, fullPortNameB))
  1855. {
  1856. setLastError("JACK operation failed");
  1857. return false;
  1858. }
  1859. return true;
  1860. }
  1861. bool patchbayDisconnect(const bool external, const uint connectionId) override
  1862. {
  1863. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, false);
  1864. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY && ! external)
  1865. return CarlaEngine::patchbayDisconnect(false, connectionId);
  1866. ConnectionToId connectionToId = { 0, 0, 0, 0, 0 };
  1867. {
  1868. const CarlaMutexLocker cml(fUsedConnections.mutex);
  1869. for (LinkedList<ConnectionToId>::Itenerator it = fUsedConnections.list.begin2(); it.valid(); it.next())
  1870. {
  1871. connectionToId = it.getValue(kConnectionToIdFallback);
  1872. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id != 0);
  1873. if (connectionToId.id == connectionId)
  1874. break;
  1875. }
  1876. }
  1877. if (connectionToId.id == 0 || connectionToId.id != connectionId)
  1878. {
  1879. setLastError("Failed to find the requested connection");
  1880. return false;
  1881. }
  1882. const CarlaMutexLocker cml(fUsedPorts.mutex);
  1883. const char* const fullPortNameA = fUsedPorts.getFullPortName(connectionToId.groupA, connectionToId.portA);
  1884. CARLA_SAFE_ASSERT_RETURN(fullPortNameA != nullptr && fullPortNameA[0] != '\0', false);
  1885. const char* const fullPortNameB = fUsedPorts.getFullPortName(connectionToId.groupB, connectionToId.portB);
  1886. CARLA_SAFE_ASSERT_RETURN(fullPortNameB != nullptr && fullPortNameB[0] != '\0', false);
  1887. if (! jackbridge_disconnect(fClient, fullPortNameA, fullPortNameB))
  1888. {
  1889. setLastError("JACK operation failed");
  1890. return false;
  1891. }
  1892. return true;
  1893. }
  1894. bool patchbaySetGroupPos(const bool sendHost, const bool sendOSC, const bool external,
  1895. const uint groupId, const int x1, const int y1, const int x2, const int y2) override
  1896. {
  1897. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, false);
  1898. CARLA_SAFE_ASSERT_RETURN(! pData->loadingProject, false);
  1899. carla_debug("CarlaEngineJack::patchbaySetGroupPos(%u, %i, %i, %i, %i)", groupId, x1, y1, x2, y2);
  1900. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY && ! external)
  1901. return CarlaEngine::patchbaySetGroupPos(sendHost, sendOSC, false, groupId, x1, y1, x2, y2);
  1902. const char* groupName;
  1903. {
  1904. const CarlaMutexLocker cml(fUsedGroups.mutex);
  1905. groupName = fUsedGroups.getGroupName(groupId);
  1906. CARLA_SAFE_ASSERT_RETURN(groupName != nullptr && groupName[0] != '\0', false);
  1907. }
  1908. bool ok;
  1909. {
  1910. const CarlaRecursiveMutexLocker crml(fThreadSafeMetadataMutex);
  1911. jack_uuid_t uuid;
  1912. {
  1913. char* const uuidstr = jackbridge_get_uuid_for_client_name(fClient, groupName);
  1914. CARLA_SAFE_ASSERT_RETURN(uuidstr != nullptr && uuidstr[0] != '\0', false);
  1915. const bool parsed = jackbridge_uuid_parse(uuidstr, &uuid);
  1916. jackbridge_free(uuidstr);
  1917. /* if parsing fails, meta-data is not available..
  1918. this could be because JACK version is old, or perhaps this is an internal client */
  1919. if (! parsed)
  1920. return false;
  1921. }
  1922. fLastPatchbaySetGroupPos.set(x1, y1, x2, y2);
  1923. char valueStr[STR_MAX];
  1924. std::snprintf(valueStr, STR_MAX-1, "%i:%i:%i:%i", x1, y1, x2, y2);
  1925. valueStr[STR_MAX-1] = '\0';
  1926. ok = jackbridge_set_property(fClient, uuid, URI_POSITION, valueStr, URI_TYPE_STRING);
  1927. }
  1928. callback(sendHost, sendOSC,
  1929. ENGINE_CALLBACK_PATCHBAY_CLIENT_POSITION_CHANGED,
  1930. groupId, x1, y1, x2, static_cast<float>(y2),
  1931. nullptr);
  1932. return ok;
  1933. }
  1934. bool patchbayRefresh(const bool sendHost, const bool sendOSC, const bool external) override
  1935. {
  1936. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, false);
  1937. carla_debug("patchbayRefresh(%s, %s, %s)", bool2str(sendHost), bool2str(sendOSC), bool2str(external));
  1938. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  1939. {
  1940. if (sendHost)
  1941. {
  1942. fExternalPatchbayHost = external;
  1943. pData->graph.setUsingExternalHost(external);
  1944. }
  1945. if (sendOSC)
  1946. {
  1947. fExternalPatchbayOsc = external;
  1948. pData->graph.setUsingExternalOSC(external);
  1949. }
  1950. if (! external)
  1951. return CarlaEngine::patchbayRefresh(sendHost, sendOSC, false);
  1952. }
  1953. {
  1954. const CarlaMutexLocker cml1(fUsedGroups.mutex);
  1955. const CarlaMutexLocker cml2(fUsedPorts.mutex);
  1956. const CarlaMutexLocker cml3(fUsedConnections.mutex);
  1957. fUsedGroups.clear();
  1958. fUsedPorts.clear();
  1959. fUsedConnections.clear();
  1960. }
  1961. initJackPatchbay(sendHost, sendOSC, jackbridge_get_client_name(fClient));
  1962. return true;
  1963. }
  1964. // -------------------------------------------------------------------
  1965. // Transport
  1966. void transportPlay() noexcept override
  1967. {
  1968. if (pData->options.transportMode != ENGINE_TRANSPORT_MODE_JACK)
  1969. return CarlaEngine::transportPlay();
  1970. if (fClient != nullptr)
  1971. {
  1972. if (! pData->timeInfo.bbt.valid)
  1973. {
  1974. // old timebase master no longer active, make ourselves master again
  1975. pData->time.setNeedsReset();
  1976. fTimebaseMaster = jackbridge_set_timebase_callback(fClient, true, carla_jack_timebase_callback, this);
  1977. }
  1978. try {
  1979. jackbridge_transport_start(fClient);
  1980. } catch(...) {}
  1981. }
  1982. }
  1983. void transportPause() noexcept override
  1984. {
  1985. if (pData->options.transportMode != ENGINE_TRANSPORT_MODE_JACK)
  1986. return CarlaEngine::transportPause();
  1987. if (fClient != nullptr)
  1988. {
  1989. try {
  1990. jackbridge_transport_stop(fClient);
  1991. } catch(...) {}
  1992. }
  1993. }
  1994. void transportBPM(const double bpm) noexcept override
  1995. {
  1996. if (pData->options.transportMode != ENGINE_TRANSPORT_MODE_JACK || fTimebaseMaster)
  1997. return CarlaEngine::transportBPM(bpm);
  1998. if (fClient == nullptr)
  1999. return;
  2000. jack_position_t jpos;
  2001. // invalidate
  2002. jpos.unique_1 = 1;
  2003. jpos.unique_2 = 2;
  2004. jackbridge_transport_query(fClient, &jpos);
  2005. if (jpos.unique_1 == jpos.unique_2 && (jpos.valid & JackPositionBBT) != 0)
  2006. {
  2007. carla_stdout("NOTE: Changing BPM without being JACK timebase master");
  2008. jpos.beats_per_minute = bpm;
  2009. try {
  2010. jackbridge_transport_reposition(fClient, &jpos);
  2011. } catch(...) {}
  2012. }
  2013. }
  2014. void transportRelocate(const uint64_t frame) noexcept override
  2015. {
  2016. if (pData->options.transportMode != ENGINE_TRANSPORT_MODE_JACK)
  2017. return CarlaEngine::transportRelocate(frame);
  2018. if (fClient != nullptr)
  2019. {
  2020. try {
  2021. jackbridge_transport_locate(fClient, static_cast<jack_nframes_t>(frame));
  2022. } catch(...) {}
  2023. }
  2024. }
  2025. // -------------------------------------------------------------------
  2026. // Patchbay stuff
  2027. const char* const* getPatchbayConnections(const bool external) const override
  2028. {
  2029. CARLA_CUSTOM_SAFE_ASSERT_RETURN("Not connected to JACK, will not save patchbay connections",
  2030. fClient != nullptr, nullptr);
  2031. carla_debug("CarlaEngineJack::getPatchbayConnections(%s)", bool2str(external));
  2032. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY && ! external)
  2033. return CarlaEngine::getPatchbayConnections(external);
  2034. CarlaStringList connList;
  2035. if (const char** const ports = jackbridge_get_ports(fClient, nullptr, nullptr, JackPortIsOutput))
  2036. {
  2037. for (int i=0; ports[i] != nullptr; ++i)
  2038. {
  2039. const jack_port_t* const jackPort(jackbridge_port_by_name(fClient, ports[i]));
  2040. const char* const fullPortName(ports[i]);
  2041. CARLA_SAFE_ASSERT_CONTINUE(jackPort != nullptr);
  2042. if (const char** const connections = jackbridge_port_get_all_connections(fClient, jackPort))
  2043. {
  2044. for (int j=0; connections[j] != nullptr; ++j)
  2045. {
  2046. connList.append(fullPortName);
  2047. connList.append(connections[j]);
  2048. }
  2049. jackbridge_free(connections);
  2050. }
  2051. }
  2052. jackbridge_free(ports);
  2053. }
  2054. if (connList.count() == 0)
  2055. return nullptr;
  2056. fRetConns = connList.toCharStringListPtr();
  2057. return fRetConns;
  2058. }
  2059. const PatchbayPosition* getPatchbayPositions(const bool external, uint& count) const override
  2060. {
  2061. CARLA_CUSTOM_SAFE_ASSERT_RETURN("Not connected to JACK, will not save patchbay positions",
  2062. fClient != nullptr, nullptr);
  2063. carla_debug("CarlaEngineJack::getPatchbayPositions(%s)", bool2str(external));
  2064. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY && ! external)
  2065. return CarlaEngine::getPatchbayPositions(external, count);
  2066. const CarlaMutexLocker cml(fUsedGroups.mutex);
  2067. const CarlaRecursiveMutexLocker crml(fThreadSafeMetadataMutex);
  2068. if (const std::size_t maxCount = fUsedGroups.list.count())
  2069. {
  2070. PatchbayPosition* ret;
  2071. try {
  2072. ret = new CarlaEngine::PatchbayPosition[maxCount];
  2073. } CARLA_SAFE_EXCEPTION_RETURN("new CarlaEngine::PatchbayPosition", nullptr);
  2074. count = 0;
  2075. GroupNameToId groupNameToId;
  2076. for (LinkedList<GroupNameToId>::Itenerator it = fUsedGroups.list.begin2(); it.valid(); it.next())
  2077. {
  2078. groupNameToId = it.getValue(kGroupNameToIdFallback);
  2079. CARLA_SAFE_ASSERT_CONTINUE(groupNameToId.group != 0);
  2080. jack_uuid_t uuid;
  2081. {
  2082. char* const uuidstr = jackbridge_get_uuid_for_client_name(fClient, groupNameToId.name);
  2083. CARLA_SAFE_ASSERT_CONTINUE(uuidstr != nullptr && uuidstr[0] != '\0');
  2084. const bool parsed = jackbridge_uuid_parse(uuidstr, &uuid);
  2085. jackbridge_free(uuidstr);
  2086. /* if parsing fails, meta-data is not available..
  2087. this could be because JACK version is old, or perhaps this is an internal client */
  2088. if (! parsed)
  2089. continue;
  2090. }
  2091. char* value = nullptr;
  2092. char* type = nullptr;
  2093. if (jackbridge_get_property(uuid, URI_POSITION, &value, &type)
  2094. && value != nullptr
  2095. && type != nullptr
  2096. && std::strcmp(type, URI_TYPE_STRING) == 0)
  2097. {
  2098. CarlaEngine::PatchbayPosition& ppos(ret[count++]);
  2099. ppos.name = carla_strdup_safe(groupNameToId.name);
  2100. ppos.dealloc = true;
  2101. ppos.pluginId = -1;
  2102. if (char* sep1 = std::strstr(value, ":"))
  2103. {
  2104. *sep1++ = '\0';
  2105. ppos.x1 = std::atoi(value);
  2106. if (char* sep2 = std::strstr(sep1, ":"))
  2107. {
  2108. *sep2++ = '\0';
  2109. ppos.y1 = std::atoi(sep1);
  2110. if (char* sep3 = std::strstr(sep2, ":"))
  2111. {
  2112. *sep3++ = '\0';
  2113. ppos.x2 = std::atoi(sep2);
  2114. ppos.y2 = std::atoi(sep3);
  2115. }
  2116. }
  2117. }
  2118. jackbridge_free(value);
  2119. jackbridge_free(type);
  2120. value = type = nullptr;
  2121. const bool clientBelongsToUs = (jackbridge_get_property(uuid, URI_MAIN_CLIENT_NAME, &value, &type)
  2122. && value != nullptr
  2123. && type != nullptr
  2124. && std::strcmp(type, URI_TYPE_STRING) == 0
  2125. && fClientName == value);
  2126. jackbridge_free(value);
  2127. jackbridge_free(type);
  2128. value = type = nullptr;
  2129. if (! clientBelongsToUs)
  2130. continue;
  2131. if (jackbridge_get_property(uuid, URI_PLUGIN_ID, &value, &type)
  2132. && value != nullptr
  2133. && type != nullptr
  2134. && std::strcmp(type, URI_TYPE_INTEGER) == 0)
  2135. {
  2136. ppos.pluginId = std::atoi(value);
  2137. }
  2138. jackbridge_free(value);
  2139. jackbridge_free(type);
  2140. }
  2141. }
  2142. return ret;
  2143. }
  2144. return nullptr;
  2145. }
  2146. void restorePatchbayConnection(const bool external,
  2147. const char* const connSource, const char* const connTarget) override
  2148. {
  2149. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr,);
  2150. CARLA_SAFE_ASSERT_RETURN(connSource != nullptr && connSource[0] != '\0',);
  2151. CARLA_SAFE_ASSERT_RETURN(connTarget != nullptr && connTarget[0] != '\0',);
  2152. carla_debug("CarlaEngineJack::restorePatchbayConnection(%s, \"%s\", \"%s\")",
  2153. bool2str(external), connSource, connTarget);
  2154. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY && ! external)
  2155. return CarlaEngine::restorePatchbayConnection(external, connSource, connTarget);
  2156. if (const jack_port_t* const port = jackbridge_port_by_name(fClient, connSource))
  2157. {
  2158. if (jackbridge_port_by_name(fClient, connTarget) == nullptr)
  2159. return;
  2160. if (! jackbridge_port_connected_to(port, connTarget))
  2161. jackbridge_connect(fClient, connSource, connTarget);
  2162. }
  2163. }
  2164. bool restorePatchbayGroupPosition(const bool external, PatchbayPosition& ppos) override
  2165. {
  2166. CARLA_SAFE_ASSERT_RETURN(fClient != nullptr, false);
  2167. carla_debug("CarlaEngineJack::restorePatchbayGroupPosition(%s, {%i, %i, %i, %i, %i, \"%s\"})",
  2168. bool2str(external), ppos.pluginId, ppos.x1, ppos.y1, ppos.x2, ppos.y2, ppos.name);
  2169. if (pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY && ! external)
  2170. return CarlaEngine::restorePatchbayGroupPosition(external, ppos);
  2171. uint groupId = 0;
  2172. const char* const orig_name = ppos.name;
  2173. /* NOTE: When loading a project, it might take a bit to receive plugins' jack client registration callbacks.
  2174. * We try to wait a little for it, but not too much.
  2175. */
  2176. if (ppos.pluginId >= 0)
  2177. {
  2178. // strip client name prefix if already in place
  2179. if (const char* const rname2 = std::strstr(ppos.name, "."))
  2180. if (const char* const rname3 = std::strstr(rname2, "/"))
  2181. ppos.name = rname3 + 1;
  2182. if (fClientNamePrefix.isNotEmpty())
  2183. {
  2184. char* nname = (char*)std::malloc(fClientNamePrefix.length() + std::strlen(ppos.name) + 1);
  2185. std::strcpy(nname, fClientNamePrefix.buffer());
  2186. std::strcat(nname, ppos.name);
  2187. ppos.name = nname;
  2188. ppos.dealloc = true;
  2189. }
  2190. for (int i=20; --i >=0;)
  2191. {
  2192. {
  2193. const CarlaMutexLocker cml(fUsedGroups.mutex);
  2194. if (fUsedGroups.list.count() == 0)
  2195. break;
  2196. groupId = fUsedGroups.getGroupId(ppos.name);
  2197. }
  2198. if (groupId != 0)
  2199. break;
  2200. carla_msleep(100);
  2201. callback(true, true, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  2202. }
  2203. }
  2204. else
  2205. {
  2206. const CarlaMutexLocker cml(fUsedGroups.mutex);
  2207. if (fUsedGroups.list.count() != 0)
  2208. groupId = fUsedGroups.getGroupId(ppos.name);
  2209. }
  2210. if (groupId == 0)
  2211. {
  2212. carla_stdout("NOTICE: Previously saved client '%s' not found", ppos.name);
  2213. }
  2214. else
  2215. {
  2216. for (;;)
  2217. {
  2218. const CarlaRecursiveMutexLocker crml(fThreadSafeMetadataMutex);
  2219. jack_uuid_t uuid;
  2220. {
  2221. char* const uuidstr = jackbridge_get_uuid_for_client_name(fClient, ppos.name);
  2222. CARLA_SAFE_ASSERT_BREAK(uuidstr != nullptr && uuidstr[0] != '\0');
  2223. const bool parsed = jackbridge_uuid_parse(uuidstr, &uuid);
  2224. jackbridge_free(uuidstr);
  2225. CARLA_CUSTOM_SAFE_ASSERT_ONCE_BREAK("JACK meta-data support unavailable", parsed);
  2226. }
  2227. char valueStr[STR_MAX];
  2228. std::snprintf(valueStr, STR_MAX-1, "%i:%i:%i:%i", ppos.x1, ppos.y1, ppos.x2, ppos.y2);
  2229. valueStr[STR_MAX-1] = '\0';
  2230. jackbridge_set_property(fClient, uuid, URI_POSITION, valueStr, URI_TYPE_STRING);
  2231. break;
  2232. }
  2233. }
  2234. # if 0
  2235. /* NOTE: This does not seem to be needed, as JACK notifies the caller about meta-data changes,
  2236. * even for the client that triggered the change. Odd..
  2237. */
  2238. if (groupId != 0)
  2239. {
  2240. callback(true, true,
  2241. ENGINE_CALLBACK_PATCHBAY_CLIENT_POSITION_CHANGED,
  2242. groupId, ppos.x1, ppos.y1, ppos.x2, static_cast<float>(ppos.y2),
  2243. nullptr);
  2244. }
  2245. # endif
  2246. return ppos.name != orig_name;
  2247. }
  2248. #endif
  2249. // -------------------------------------------------------------------
  2250. protected:
  2251. void handleJackBufferSizeCallback(const uint32_t newBufferSize)
  2252. {
  2253. if (pData->bufferSize == newBufferSize)
  2254. return;
  2255. #ifndef BUILD_BRIDGE
  2256. const CarlaMutexLocker cml(fPatchbayProcThreadProtectionMutex);
  2257. #endif
  2258. pData->bufferSize = newBufferSize;
  2259. bufferSizeChanged(newBufferSize);
  2260. }
  2261. void handleJackSampleRateCallback(const double newSampleRate)
  2262. {
  2263. if (carla_isEqual(pData->sampleRate, newSampleRate))
  2264. return;
  2265. #ifndef BUILD_BRIDGE
  2266. const CarlaMutexLocker cml(fPatchbayProcThreadProtectionMutex);
  2267. #endif
  2268. pData->sampleRate = newSampleRate;
  2269. sampleRateChanged(newSampleRate);
  2270. }
  2271. void handleJackFreewheelCallback(const bool isFreewheel)
  2272. {
  2273. if (fFreewheel == isFreewheel)
  2274. return;
  2275. #ifndef BUILD_BRIDGE
  2276. const CarlaMutexLocker cml(fPatchbayProcThreadProtectionMutex);
  2277. #endif
  2278. fFreewheel = isFreewheel;
  2279. offlineModeChanged(isFreewheel);
  2280. }
  2281. void handleJackProcessCallback(const uint32_t nframes)
  2282. {
  2283. const PendingRtEventsRunner prt(this, nframes);
  2284. CARLA_SAFE_ASSERT_INT2_RETURN(nframes == pData->bufferSize, nframes, pData->bufferSize,);
  2285. #ifdef BUILD_BRIDGE
  2286. CarlaPluginPtr plugin = pData->plugins[0].plugin;
  2287. if (plugin.get() != nullptr && plugin->isEnabled() && plugin->tryLock(fFreewheel))
  2288. {
  2289. plugin->initBuffers();
  2290. processPlugin(plugin, nframes);
  2291. plugin->unlock();
  2292. }
  2293. #else
  2294. if (pData->options.transportMode == ENGINE_TRANSPORT_MODE_JACK && !fTimebaseMaster)
  2295. {
  2296. jack_position_t jpos;
  2297. // invalidate
  2298. jpos.unique_1 = 1;
  2299. jpos.unique_2 = 2;
  2300. EngineTimeInfo timeInfo;
  2301. const bool playing = jackbridge_transport_query(fClient, &jpos) == JackTransportRolling;
  2302. if (jpos.unique_1 != jpos.unique_2)
  2303. {
  2304. timeInfo.playing = false;
  2305. timeInfo.frame = 0;
  2306. timeInfo.usecs = 0;
  2307. timeInfo.bbt.valid = false;
  2308. }
  2309. else
  2310. {
  2311. timeInfo.playing = playing;
  2312. timeInfo.frame = jpos.frame;
  2313. timeInfo.usecs = jpos.usecs;
  2314. if (jpos.valid & JackPositionBBT)
  2315. {
  2316. timeInfo.bbt.valid = true;
  2317. timeInfo.bbt.bar = jpos.bar;
  2318. timeInfo.bbt.beat = jpos.beat;
  2319. timeInfo.bbt.tick = jpos.tick;
  2320. timeInfo.bbt.barStartTick = jpos.bar_start_tick;
  2321. timeInfo.bbt.beatsPerBar = jpos.beats_per_bar;
  2322. timeInfo.bbt.beatType = jpos.beat_type;
  2323. timeInfo.bbt.ticksPerBeat = jpos.ticks_per_beat;
  2324. timeInfo.bbt.beatsPerMinute = jpos.beats_per_minute;
  2325. }
  2326. else
  2327. {
  2328. timeInfo.bbt.valid = false;
  2329. }
  2330. }
  2331. pData->timeInfo = timeInfo;
  2332. }
  2333. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2334. {
  2335. if (pData->aboutToClose)
  2336. {
  2337. if (float* const audioOut1 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioOut1], nframes))
  2338. carla_zeroFloats(audioOut1, nframes);
  2339. if (float* const audioOut2 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioOut2], nframes))
  2340. carla_zeroFloats(audioOut2, nframes);
  2341. }
  2342. else if (pData->curPluginCount == 0)
  2343. {
  2344. float* const audioIn1 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioIn1], nframes);
  2345. float* const audioIn2 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioIn2], nframes);
  2346. float* const audioOut1 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioOut1], nframes);
  2347. float* const audioOut2 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioOut2], nframes);
  2348. // assert buffers
  2349. CARLA_SAFE_ASSERT_RETURN(audioIn1 != nullptr,);
  2350. CARLA_SAFE_ASSERT_RETURN(audioIn2 != nullptr,);
  2351. CARLA_SAFE_ASSERT_RETURN(audioOut1 != nullptr,);
  2352. CARLA_SAFE_ASSERT_RETURN(audioOut2 != nullptr,);
  2353. // pass-through
  2354. carla_copyFloats(audioOut1, audioIn1, nframes);
  2355. carla_copyFloats(audioOut2, audioIn2, nframes);
  2356. // TODO pass-through MIDI as well
  2357. if (void* const eventOut = jackbridge_port_get_buffer(fRackPorts[kRackPortEventOut], nframes))
  2358. jackbridge_midi_clear_buffer(eventOut);
  2359. return;
  2360. }
  2361. }
  2362. if (pData->options.processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  2363. {
  2364. for (uint i=0; i < pData->curPluginCount; ++i)
  2365. {
  2366. if (CarlaPluginPtr plugin = pData->plugins[i].plugin)
  2367. {
  2368. if (plugin->isEnabled() && plugin->tryLock(fFreewheel))
  2369. {
  2370. plugin->initBuffers();
  2371. processPlugin(plugin, nframes);
  2372. plugin->unlock();
  2373. }
  2374. }
  2375. }
  2376. }
  2377. else if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK ||
  2378. pData->options.processMode == ENGINE_PROCESS_MODE_PATCHBAY)
  2379. {
  2380. CARLA_SAFE_ASSERT_RETURN(pData->events.in != nullptr,);
  2381. CARLA_SAFE_ASSERT_RETURN(pData->events.out != nullptr,);
  2382. // get buffers from jack
  2383. float* const audioIn1 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioIn1], nframes);
  2384. float* const audioIn2 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioIn2], nframes);
  2385. float* const audioOut1 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioOut1], nframes);
  2386. float* const audioOut2 = (float*)jackbridge_port_get_buffer(fRackPorts[kRackPortAudioOut2], nframes);
  2387. void* const eventIn = jackbridge_port_get_buffer(fRackPorts[kRackPortEventIn], nframes);
  2388. void* const eventOut = jackbridge_port_get_buffer(fRackPorts[kRackPortEventOut], nframes);
  2389. // assert buffers
  2390. CARLA_SAFE_ASSERT_RETURN(audioIn1 != nullptr,);
  2391. CARLA_SAFE_ASSERT_RETURN(audioIn2 != nullptr,);
  2392. CARLA_SAFE_ASSERT_RETURN(audioOut1 != nullptr,);
  2393. CARLA_SAFE_ASSERT_RETURN(audioOut2 != nullptr,);
  2394. // create audio buffers
  2395. const float* inBuf[2] = { audioIn1, audioIn2 };
  2396. /**/ float* outBuf[2] = { audioOut1, audioOut2 };
  2397. // initialize events
  2398. carla_zeroStructs(pData->events.in, kMaxEngineEventInternalCount);
  2399. carla_zeroStructs(pData->events.out, kMaxEngineEventInternalCount);
  2400. if (eventIn != nullptr)
  2401. {
  2402. ushort engineEventIndex = 0;
  2403. jack_midi_event_t jackEvent;
  2404. const uint32_t jackEventCount(jackbridge_midi_get_event_count(eventIn));
  2405. for (uint32_t jackEventIndex=0; jackEventIndex < jackEventCount; ++jackEventIndex)
  2406. {
  2407. if (! jackbridge_midi_event_get(&jackEvent, eventIn, jackEventIndex))
  2408. continue;
  2409. CARLA_SAFE_ASSERT_CONTINUE(jackEvent.size < 0xFF /* uint8_t max */);
  2410. EngineEvent& engineEvent(pData->events.in[engineEventIndex++]);
  2411. engineEvent.time = jackEvent.time;
  2412. engineEvent.fillFromMidiData(static_cast<uint8_t>(jackEvent.size), jackEvent.buffer, 0);
  2413. if (engineEventIndex >= kMaxEngineEventInternalCount)
  2414. break;
  2415. }
  2416. }
  2417. if (pData->options.processMode == ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  2418. {
  2419. pData->graph.processRack(pData, inBuf, outBuf, nframes);
  2420. }
  2421. else
  2422. {
  2423. const CarlaMutexLocker cml(fPatchbayProcThreadProtectionMutex);
  2424. pData->graph.process(pData, inBuf, outBuf, nframes);
  2425. }
  2426. // output control
  2427. if (eventOut != nullptr)
  2428. {
  2429. jackbridge_midi_clear_buffer(eventOut);
  2430. uint8_t size = 0;
  2431. uint8_t mdata[3] = { 0, 0, 0 };
  2432. uint8_t mdataTmp[EngineMidiEvent::kDataSize];
  2433. const uint8_t* mdataPtr;
  2434. for (ushort i=0; i < kMaxEngineEventInternalCount; ++i)
  2435. {
  2436. const EngineEvent& engineEvent(pData->events.out[i]);
  2437. /**/ if (engineEvent.type == kEngineEventTypeNull)
  2438. {
  2439. break;
  2440. }
  2441. else if (engineEvent.type == kEngineEventTypeControl)
  2442. {
  2443. const EngineControlEvent& ctrlEvent(engineEvent.ctrl);
  2444. size = ctrlEvent.convertToMidiData(engineEvent.channel, mdata);
  2445. mdataPtr = mdata;
  2446. }
  2447. else if (engineEvent.type == kEngineEventTypeMidi)
  2448. {
  2449. const EngineMidiEvent& midiEvent(engineEvent.midi);
  2450. size = midiEvent.size;
  2451. CARLA_SAFE_ASSERT_CONTINUE(size > 0);
  2452. if (size > EngineMidiEvent::kDataSize)
  2453. {
  2454. CARLA_SAFE_ASSERT_CONTINUE(midiEvent.dataExt != nullptr);
  2455. mdataPtr = midiEvent.dataExt;
  2456. }
  2457. else
  2458. {
  2459. // set first byte
  2460. mdataTmp[0] = static_cast<uint8_t>(midiEvent.data[0] | (engineEvent.channel & MIDI_CHANNEL_BIT));
  2461. if (size > 1)
  2462. {
  2463. // copy rest
  2464. carla_copy<uint8_t>(mdataTmp+1, midiEvent.data+1, size-1U);
  2465. }
  2466. // done
  2467. mdataPtr = mdataTmp;
  2468. }
  2469. }
  2470. else
  2471. {
  2472. continue;
  2473. }
  2474. if (size > 0)
  2475. jackbridge_midi_event_write(eventOut, engineEvent.time, mdataPtr, size);
  2476. }
  2477. }
  2478. }
  2479. if (pData->options.transportMode == ENGINE_TRANSPORT_MODE_JACK)
  2480. {
  2481. if (fTimebaseMaster)
  2482. {
  2483. const bool playing = jackbridge_transport_query(fClient, nullptr) == JackTransportRolling;
  2484. if (fTimebaseRolling != playing)
  2485. {
  2486. fTimebaseRolling = playing;
  2487. pData->timeInfo.playing = playing;
  2488. }
  2489. // Check if we are no longer timebase master
  2490. if (playing && fTimebaseUsecs != 0 && fTimebaseUsecs == pData->timeInfo.usecs)
  2491. {
  2492. carla_debug("No longer timerbase master");
  2493. fTimebaseMaster = false;
  2494. }
  2495. }
  2496. fTimebaseUsecs = pData->timeInfo.usecs;
  2497. }
  2498. #endif // ! BUILD_BRIDGE
  2499. }
  2500. void handleJackLatencyCallback(const jack_latency_callback_mode_t /*mode*/)
  2501. {
  2502. // TODO
  2503. }
  2504. #ifndef BUILD_BRIDGE
  2505. void handleJackTimebaseCallback(jack_nframes_t nframes, jack_position_t* const pos, const int new_pos)
  2506. {
  2507. if (new_pos)
  2508. pData->time.setNeedsReset();
  2509. pData->timeInfo.playing = fTimebaseRolling;
  2510. pData->timeInfo.frame = pos->frame;
  2511. pData->timeInfo.usecs = pos->usecs;
  2512. pData->time.fillJackTimeInfo(pos, nframes);
  2513. }
  2514. void handleJackClientUnregistrationCallback(const char* const name)
  2515. {
  2516. CARLA_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0',);
  2517. // ignore this if on internal patchbay mode
  2518. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  2519. if (! (fExternalPatchbayHost || (fExternalPatchbayOsc && pData->osc.isControlRegisteredForTCP()))) return;
  2520. #else
  2521. if (! fExternalPatchbayHost) return;
  2522. #endif
  2523. uint groupId;
  2524. {
  2525. const CarlaMutexLocker cml(fUsedGroups.mutex);
  2526. groupId = fUsedGroups.getGroupId(name);
  2527. // clients might have been registered without ports
  2528. if (groupId == 0) return;
  2529. GroupNameToId groupNameToId;
  2530. groupNameToId.setData(groupId, name);
  2531. fUsedGroups.list.removeOne(groupNameToId);
  2532. }
  2533. callback(fExternalPatchbayHost, fExternalPatchbayOsc,
  2534. ENGINE_CALLBACK_PATCHBAY_CLIENT_REMOVED,
  2535. groupId,
  2536. 0, 0, 0, 0.0f, nullptr);
  2537. }
  2538. void handleJackClientPositionChangeCallback(const jack_uuid_t uuid)
  2539. {
  2540. // ignore this if on internal patchbay mode
  2541. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  2542. if (! (fExternalPatchbayHost || (fExternalPatchbayOsc && pData->osc.isControlRegisteredForTCP()))) return;
  2543. #else
  2544. if (! fExternalPatchbayHost) return;
  2545. #endif
  2546. const CarlaMutexLocker cml(fPostPonedUUIDsMutex);
  2547. fPostPonedUUIDs.addIfNotAlreadyThere(uuid);
  2548. }
  2549. void handleJackPortRegistrationCallback(const char* const portName,
  2550. const char* const shortPortName,
  2551. const CarlaJackPortHints& jackPortHints)
  2552. {
  2553. // ignore this if on internal patchbay mode
  2554. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  2555. if (! (fExternalPatchbayHost || (fExternalPatchbayOsc && pData->osc.isControlRegisteredForTCP()))) return;
  2556. #else
  2557. if (! fExternalPatchbayHost) return;
  2558. #endif
  2559. bool groupFound;
  2560. CarlaString groupName(portName);
  2561. groupName.truncate(groupName.rfind(shortPortName, &groupFound)-1);
  2562. CARLA_SAFE_ASSERT_RETURN(groupFound,);
  2563. groupFound = false;
  2564. GroupToIdData groupData;
  2565. PortToIdData portData;
  2566. {
  2567. const CarlaMutexLocker cml1(fUsedGroups.mutex);
  2568. groupData.id = fUsedGroups.getGroupId(groupName);
  2569. if (groupData.id == 0)
  2570. {
  2571. groupData.id = ++fUsedGroups.lastId;
  2572. GroupNameToId groupNameToId;
  2573. groupNameToId.setData(groupData.id, groupName);
  2574. int pluginId = -1;
  2575. PatchbayIcon icon = jackPortHints.isHardware ? PATCHBAY_ICON_HARDWARE : PATCHBAY_ICON_APPLICATION;
  2576. findPluginIdAndIcon(groupName, pluginId, icon);
  2577. fUsedGroups.list.append(groupNameToId);
  2578. groupFound = true;
  2579. groupData.icon = icon;
  2580. groupData.pluginId = pluginId;
  2581. std::strncpy(groupData.strVal, groupName, STR_MAX-1);
  2582. groupData.strVal[STR_MAX-1] = '\0';
  2583. }
  2584. uint canvasPortFlags = 0x0;
  2585. canvasPortFlags |= jackPortHints.isInput ? PATCHBAY_PORT_IS_INPUT : 0x0;
  2586. /**/ if (jackPortHints.isCV)
  2587. canvasPortFlags |= PATCHBAY_PORT_TYPE_CV;
  2588. else if (jackPortHints.isOSC)
  2589. canvasPortFlags |= PATCHBAY_PORT_TYPE_OSC;
  2590. else if (jackPortHints.isAudio)
  2591. canvasPortFlags |= PATCHBAY_PORT_TYPE_AUDIO;
  2592. else if (jackPortHints.isMIDI)
  2593. canvasPortFlags |= PATCHBAY_PORT_TYPE_MIDI;
  2594. const CarlaMutexLocker cml2(fUsedPorts.mutex);
  2595. portData.group = groupData.id;
  2596. portData.port = ++fUsedPorts.lastId;
  2597. portData.flags = canvasPortFlags;
  2598. std::strncpy(portData.strVal, shortPortName, STR_MAX-1);
  2599. portData.strVal[STR_MAX-1] = '\0';
  2600. PortNameToId portNameToId;
  2601. portNameToId.setData(portData.group, portData.port, shortPortName, portName);
  2602. fUsedPorts.list.append(portNameToId);
  2603. }
  2604. if (groupFound)
  2605. {
  2606. callback(fExternalPatchbayHost, fExternalPatchbayOsc,
  2607. ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED,
  2608. groupData.id,
  2609. groupData.icon,
  2610. groupData.pluginId,
  2611. 0, 0.0f,
  2612. groupData.strVal);
  2613. }
  2614. callback(fExternalPatchbayHost, fExternalPatchbayOsc,
  2615. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  2616. portData.group,
  2617. static_cast<int>(portData.port),
  2618. static_cast<int>(portData.flags),
  2619. 0, 0.0f,
  2620. portData.strVal);
  2621. }
  2622. void handleJackPortUnregistrationCallback(const char* const portName)
  2623. {
  2624. // ignore this if on internal patchbay mode
  2625. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  2626. if (! (fExternalPatchbayHost || (fExternalPatchbayOsc && pData->osc.isControlRegisteredForTCP()))) return;
  2627. #else
  2628. if (! fExternalPatchbayHost) return;
  2629. #endif
  2630. uint groupId, portId;
  2631. {
  2632. const CarlaMutexLocker cml(fUsedPorts.mutex);
  2633. const PortNameToId& portNameToId(fUsedPorts.getPortNameToId(portName));
  2634. /* NOTE: Due to JACK2 async behaviour the port we get here might be the same of a previous rename-plugin request.
  2635. See the comment on CarlaEngineJack::renamePlugin() for more information. */
  2636. if (portNameToId.group <= 0 || portNameToId.port <= 0) return;
  2637. groupId = portNameToId.group;
  2638. portId = portNameToId.port;
  2639. fUsedPorts.list.removeOne(portNameToId);
  2640. }
  2641. callback(fExternalPatchbayHost, fExternalPatchbayOsc,
  2642. ENGINE_CALLBACK_PATCHBAY_PORT_REMOVED,
  2643. groupId,
  2644. static_cast<int>(portId),
  2645. 0, 0, 0.0f, nullptr);
  2646. }
  2647. void handleJackPortConnectCallback(const char* const portNameA, const char* const portNameB)
  2648. {
  2649. // ignore this if on internal patchbay mode
  2650. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  2651. if (! (fExternalPatchbayHost || (fExternalPatchbayOsc && pData->osc.isControlRegisteredForTCP()))) return;
  2652. #else
  2653. if (! fExternalPatchbayHost) return;
  2654. #endif
  2655. char strBuf[STR_MAX];
  2656. uint connectionId;
  2657. {
  2658. const CarlaMutexLocker cml1(fUsedPorts.mutex);
  2659. const PortNameToId& portNameToIdA(fUsedPorts.getPortNameToId(portNameA));
  2660. const PortNameToId& portNameToIdB(fUsedPorts.getPortNameToId(portNameB));
  2661. /* NOTE: Due to JACK2 async behaviour the port we get here might be the same of a previous rename-plugin request.
  2662. See the comment on CarlaEngineJack::renamePlugin() for more information. */
  2663. if (portNameToIdA.group <= 0 || portNameToIdA.port <= 0) return;
  2664. if (portNameToIdB.group <= 0 || portNameToIdB.port <= 0) return;
  2665. const CarlaMutexLocker cml2(fUsedConnections.mutex);
  2666. std::snprintf(strBuf, STR_MAX-1, "%i:%i:%i:%i",
  2667. portNameToIdA.group, portNameToIdA.port,
  2668. portNameToIdB.group, portNameToIdB.port);
  2669. strBuf[STR_MAX-1] = '\0';
  2670. connectionId = ++fUsedConnections.lastId;
  2671. ConnectionToId connectionToId;
  2672. connectionToId.setData(connectionId,
  2673. portNameToIdA.group, portNameToIdA.port,
  2674. portNameToIdB.group, portNameToIdB.port);
  2675. fUsedConnections.list.append(connectionToId);
  2676. }
  2677. callback(fExternalPatchbayHost, fExternalPatchbayOsc,
  2678. ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED,
  2679. connectionId,
  2680. 0, 0, 0, 0.0f,
  2681. strBuf);
  2682. }
  2683. void handleJackPortDisconnectCallback(const char* const portNameA, const char* const portNameB)
  2684. {
  2685. // ignore this if on internal patchbay mode
  2686. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  2687. if (! (fExternalPatchbayHost || (fExternalPatchbayOsc && pData->osc.isControlRegisteredForTCP()))) return;
  2688. #else
  2689. if (! fExternalPatchbayHost) return;
  2690. #endif
  2691. uint connectionId = 0;
  2692. {
  2693. const CarlaMutexLocker cml1(fUsedPorts.mutex);
  2694. const PortNameToId& portNameToIdA(fUsedPorts.getPortNameToId(portNameA));
  2695. const PortNameToId& portNameToIdB(fUsedPorts.getPortNameToId(portNameB));
  2696. /* NOTE: Due to JACK2 async behaviour the port we get here might be the same of a previous rename-plugin request.
  2697. See the comment on CarlaEngineJack::renamePlugin() for more information. */
  2698. if (portNameToIdA.group <= 0 || portNameToIdA.port <= 0) return;
  2699. if (portNameToIdB.group <= 0 || portNameToIdB.port <= 0) return;
  2700. const CarlaMutexLocker cml2(fUsedConnections.mutex);
  2701. for (LinkedList<ConnectionToId>::Itenerator it = fUsedConnections.list.begin2(); it.valid(); it.next())
  2702. {
  2703. const ConnectionToId& connectionToId = it.getValue(kConnectionToIdFallback);
  2704. CARLA_SAFE_ASSERT_CONTINUE(connectionToId.id != 0);
  2705. if (connectionToId.groupA == portNameToIdA.group && connectionToId.portA == portNameToIdA.port &&
  2706. connectionToId.groupB == portNameToIdB.group && connectionToId.portB == portNameToIdB.port)
  2707. {
  2708. connectionId = connectionToId.id;
  2709. fUsedConnections.list.remove(it);
  2710. break;
  2711. }
  2712. }
  2713. }
  2714. if (connectionId != 0) {
  2715. callback(fExternalPatchbayHost, fExternalPatchbayOsc,
  2716. ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED,
  2717. connectionId,
  2718. 0, 0, 0, 0.0f, nullptr);
  2719. }
  2720. }
  2721. void handleJackPortRenameCallback(const char* const oldFullName,
  2722. const char* const newFullName,
  2723. const char* const newShortName)
  2724. {
  2725. // ignore this if on internal patchbay mode
  2726. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  2727. if (! (fExternalPatchbayHost || (fExternalPatchbayOsc && pData->osc.isControlRegisteredForTCP()))) return;
  2728. #else
  2729. if (! fExternalPatchbayHost) return;
  2730. #endif
  2731. CARLA_SAFE_ASSERT_RETURN(oldFullName != nullptr && oldFullName[0] != '\0',);
  2732. CARLA_SAFE_ASSERT_RETURN(newFullName != nullptr && newFullName[0] != '\0',);
  2733. bool found;
  2734. CarlaString groupName(newFullName);
  2735. groupName.truncate(groupName.rfind(newShortName, &found)-1);
  2736. CARLA_SAFE_ASSERT_RETURN(found,);
  2737. uint groupId, portId = 0;
  2738. char portName[STR_MAX];
  2739. found = false;
  2740. {
  2741. const CarlaMutexLocker cml1(fUsedGroups.mutex);
  2742. groupId = fUsedGroups.getGroupId(groupName);
  2743. CARLA_SAFE_ASSERT_RETURN(groupId != 0,);
  2744. const CarlaMutexLocker cml2(fUsedPorts.mutex);
  2745. for (LinkedList<PortNameToId>::Itenerator it = fUsedPorts.list.begin2(); it.valid(); it.next())
  2746. {
  2747. PortNameToId& portNameToId(it.getValue(kPortNameToIdFallbackNC));
  2748. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group != 0);
  2749. if (std::strncmp(portNameToId.fullName, oldFullName, STR_MAX) == 0)
  2750. {
  2751. CARLA_SAFE_ASSERT_CONTINUE(portNameToId.group == groupId);
  2752. found = true;
  2753. portId = portNameToId.port;
  2754. std::strncpy(portName, newShortName, STR_MAX-1);
  2755. portName[STR_MAX-1] = '\0';
  2756. portNameToId.rename(newShortName, newFullName);
  2757. break;
  2758. }
  2759. }
  2760. }
  2761. if (found)
  2762. {
  2763. callback(fExternalPatchbayHost, fExternalPatchbayOsc,
  2764. ENGINE_CALLBACK_PATCHBAY_PORT_CHANGED,
  2765. groupId,
  2766. static_cast<int>(portId),
  2767. 0, 0, 0.0f,
  2768. portName);
  2769. }
  2770. }
  2771. #endif
  2772. void handleJackShutdownCallback()
  2773. {
  2774. #ifndef BUILD_BRIDGE
  2775. stopThread(-1);
  2776. #endif
  2777. {
  2778. const PendingRtEventsRunner prt(this, pData->bufferSize);
  2779. for (uint i=0; i < pData->curPluginCount; ++i)
  2780. {
  2781. if (const CarlaPluginPtr plugin = pData->plugins[i].plugin)
  2782. {
  2783. plugin->tryLock(true);
  2784. if (CarlaEngineJackClient* const client = (CarlaEngineJackClient*)plugin->getEngineClient())
  2785. client->invalidate();
  2786. plugin->unlock();
  2787. }
  2788. }
  2789. }
  2790. pData->thread.stopThread(500);
  2791. fClient = nullptr;
  2792. #ifdef BUILD_BRIDGE
  2793. fIsRunning = false;
  2794. #else
  2795. carla_zeroPointers(fRackPorts, kRackPortCount);
  2796. #endif
  2797. callback(true, true, ENGINE_CALLBACK_ERROR, 0, 0, 0, 0, 0.0f,
  2798. "Carla has been killed by JACK, or JACK has stopped.\n"
  2799. "You can still save if you want, but you will lose patchbay connections and positions.");
  2800. }
  2801. // -------------------------------------------------------------------
  2802. void handlePluginJackShutdownCallback(const CarlaPluginPtr plugin)
  2803. {
  2804. CarlaEngineJackClient* const engineClient((CarlaEngineJackClient*)plugin->getEngineClient());
  2805. CARLA_SAFE_ASSERT_RETURN(engineClient != nullptr,);
  2806. plugin->tryLock(true);
  2807. engineClient->invalidate();
  2808. plugin->unlock();
  2809. callback(true, true, ENGINE_CALLBACK_PLUGIN_UNAVAILABLE, plugin->getId(), 0, 0, 0, 0.0f, "Killed by JACK");
  2810. }
  2811. // -------------------------------------------------------------------
  2812. private:
  2813. jack_client_t* fClient;
  2814. bool fExternalPatchbayHost;
  2815. bool fExternalPatchbayOsc;
  2816. bool fFreewheel;
  2817. CarlaString fClientName;
  2818. CarlaRecursiveMutex fThreadSafeMetadataMutex;
  2819. // -------------------------------------------------------------------
  2820. #ifdef BUILD_BRIDGE
  2821. bool fIsRunning;
  2822. #else
  2823. CarlaString fClientNamePrefix;
  2824. enum RackPorts {
  2825. kRackPortAudioIn1 = 0,
  2826. kRackPortAudioIn2 = 1,
  2827. kRackPortAudioOut1 = 2,
  2828. kRackPortAudioOut2 = 3,
  2829. kRackPortEventIn = 4,
  2830. kRackPortEventOut = 5,
  2831. kRackPortCount = 6
  2832. };
  2833. jack_port_t* fRackPorts[kRackPortCount];
  2834. bool fTimebaseMaster;
  2835. bool fTimebaseRolling;
  2836. uint64_t fTimebaseUsecs;
  2837. PatchbayGroupList fUsedGroups;
  2838. PatchbayPortList fUsedPorts;
  2839. PatchbayConnectionList fUsedConnections;
  2840. CarlaMutex fPatchbayProcThreadProtectionMutex;
  2841. mutable CharStringListPtr fRetConns;
  2842. void findPluginIdAndIcon(const char* const clientName, int& pluginId, PatchbayIcon& icon) const noexcept
  2843. {
  2844. carla_debug("CarlaEngineJack::findPluginIdAndIcon(\"%s\", ...)", clientName);
  2845. // TODO - this currently only works in multi-client mode
  2846. if (pData->options.processMode != ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS)
  2847. return;
  2848. const CarlaRecursiveMutexLocker crml(fThreadSafeMetadataMutex);
  2849. jack_uuid_t uuid;
  2850. {
  2851. char* const uuidstr = jackbridge_get_uuid_for_client_name(fClient, clientName);
  2852. CARLA_SAFE_ASSERT_RETURN(uuidstr != nullptr && uuidstr[0] != '\0',);
  2853. const bool parsed = jackbridge_uuid_parse(uuidstr, &uuid);
  2854. jackbridge_free(uuidstr);
  2855. /* if parsing fails, meta-data is not available..
  2856. this could be because JACK version is old, or perhaps this is an internal client */
  2857. if (! parsed)
  2858. return;
  2859. }
  2860. bool clientBelongsToUs;
  2861. {
  2862. char* value = nullptr;
  2863. char* type = nullptr;
  2864. if (! jackbridge_get_property(uuid, URI_MAIN_CLIENT_NAME, &value, &type))
  2865. return;
  2866. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  2867. CARLA_SAFE_ASSERT_RETURN(value != nullptr && value[0] != '\0',);
  2868. CARLA_SAFE_ASSERT_RETURN(std::strcmp(type, URI_TYPE_STRING) == 0,);
  2869. clientBelongsToUs = fClientName == value;
  2870. jackbridge_free(value);
  2871. jackbridge_free(type);
  2872. }
  2873. {
  2874. char* value = nullptr;
  2875. char* type = nullptr;
  2876. if (! jackbridge_get_property(uuid, URI_PLUGIN_ID, &value, &type))
  2877. return;
  2878. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  2879. CARLA_SAFE_ASSERT_RETURN(value != nullptr && value[0] != '\0',);
  2880. CARLA_SAFE_ASSERT_RETURN(std::strcmp(type, URI_TYPE_INTEGER) == 0,);
  2881. if (clientBelongsToUs)
  2882. pluginId = std::atoi(value);
  2883. icon = PATCHBAY_ICON_PLUGIN;
  2884. jackbridge_free(value);
  2885. jackbridge_free(type);
  2886. }
  2887. {
  2888. char* value = nullptr;
  2889. char* type = nullptr;
  2890. if (! jackbridge_get_property(uuid, URI_PLUGIN_ICON, &value, &type))
  2891. return;
  2892. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  2893. CARLA_SAFE_ASSERT_RETURN(value != nullptr && value[0] != '\0',);
  2894. CARLA_SAFE_ASSERT_RETURN(std::strcmp(type, URI_TYPE_STRING) == 0,);
  2895. /**/ if (std::strcmp(value, "app") == 0)
  2896. icon = PATCHBAY_ICON_APPLICATION;
  2897. else if (std::strcmp(value, "application") == 0)
  2898. icon = PATCHBAY_ICON_APPLICATION;
  2899. else if (std::strcmp(value, "plugin") == 0)
  2900. icon = PATCHBAY_ICON_PLUGIN;
  2901. else if (std::strcmp(value, "hardware") == 0)
  2902. icon = PATCHBAY_ICON_HARDWARE;
  2903. else if (std::strcmp(value, "carla") == 0)
  2904. icon = PATCHBAY_ICON_CARLA;
  2905. else if (std::strcmp(value, "distrho") == 0)
  2906. icon = PATCHBAY_ICON_DISTRHO;
  2907. else if (std::strcmp(value, "file") == 0)
  2908. icon = PATCHBAY_ICON_FILE;
  2909. jackbridge_free(value);
  2910. jackbridge_free(type);
  2911. }
  2912. }
  2913. // prevent recursion on patchbay group position changes
  2914. struct LastPatchbaySetGroupPos {
  2915. int x1, y1, x2, y2;
  2916. LastPatchbaySetGroupPos()
  2917. : x1(0), y1(0), x2(0), y2(0) {}
  2918. void clear() noexcept
  2919. {
  2920. x1 = y1 = x2 = y2 = 0;
  2921. }
  2922. void set(const int _x1, const int _y1, const int _x2, const int _y2) noexcept
  2923. {
  2924. x1 = _x1;
  2925. y1 = _y1;
  2926. x2 = _x2;
  2927. y2 = _y2;
  2928. }
  2929. bool operator!=(const LastPatchbaySetGroupPos& pos) const noexcept
  2930. {
  2931. return pos.x1 != x1 || pos.y1 != y1 || pos.x2 != x2 || pos.y2 != y2;
  2932. }
  2933. } fLastPatchbaySetGroupPos;
  2934. // handy stuff only needed for initJackPatchbay
  2935. struct GroupToIdData {
  2936. uint id;
  2937. PatchbayIcon icon;
  2938. int pluginId;
  2939. char strVal[STR_MAX];
  2940. };
  2941. struct PortToIdData {
  2942. uint group;
  2943. uint port;
  2944. uint flags;
  2945. char strVal[STR_MAX];
  2946. };
  2947. struct ConnectionToIdData {
  2948. uint id;
  2949. char strVal[STR_MAX];
  2950. };
  2951. void initJackPatchbay(const bool sendHost, const bool sendOSC, const char* const ourName)
  2952. {
  2953. CARLA_SAFE_ASSERT_RETURN(pData->options.processMode != ENGINE_PROCESS_MODE_PATCHBAY ||
  2954. (fExternalPatchbayHost && sendHost) || (fExternalPatchbayOsc && sendOSC),);
  2955. CARLA_SAFE_ASSERT_RETURN(ourName != nullptr && ourName[0] != '\0',);
  2956. fLastPatchbaySetGroupPos.clear();
  2957. uint id, carlaId;
  2958. CarlaStringList parsedGroups;
  2959. LinkedList<GroupToIdData> groupCallbackData;
  2960. LinkedList<PortToIdData> portsCallbackData;
  2961. LinkedList<ConnectionToIdData> connCallbackData;
  2962. {
  2963. const CarlaMutexLocker cml1(fUsedGroups.mutex);
  2964. const CarlaMutexLocker cml2(fUsedPorts.mutex);
  2965. const CarlaMutexLocker cml3(fUsedConnections.mutex);
  2966. // add our client first
  2967. {
  2968. carlaId = ++fUsedGroups.lastId;
  2969. parsedGroups.append(ourName);
  2970. GroupNameToId groupNameToId;
  2971. groupNameToId.setData(carlaId, ourName);
  2972. fUsedGroups.list.append(groupNameToId);
  2973. }
  2974. // query all jack ports
  2975. {
  2976. const char** const ports = jackbridge_get_ports(fClient, nullptr, nullptr, 0);
  2977. CARLA_SAFE_ASSERT_RETURN(ports != nullptr,);
  2978. const CarlaRecursiveMutexLocker crml(fThreadSafeMetadataMutex);
  2979. for (int i=0; ports[i] != nullptr; ++i)
  2980. {
  2981. const char* const fullPortName(ports[i]);
  2982. CARLA_SAFE_ASSERT_CONTINUE(fullPortName != nullptr && fullPortName[0] != '\0');
  2983. const jack_port_t* const jackPort(jackbridge_port_by_name(fClient, fullPortName));
  2984. CARLA_SAFE_ASSERT_CONTINUE(jackPort != nullptr);
  2985. const char* const shortPortName(jackbridge_port_short_name(jackPort));
  2986. CARLA_SAFE_ASSERT_CONTINUE(shortPortName != nullptr && shortPortName[0] != '\0');
  2987. const CarlaJackPortHints jackPortHints(CarlaJackPortHints::fromPort(jackPort));
  2988. uint groupId = 0;
  2989. bool found;
  2990. CarlaString groupName(fullPortName);
  2991. groupName.truncate(groupName.rfind(shortPortName, &found)-1);
  2992. CARLA_SAFE_ASSERT_CONTINUE(found);
  2993. if (parsedGroups.contains(groupName))
  2994. {
  2995. groupId = fUsedGroups.getGroupId(groupName);
  2996. CARLA_SAFE_ASSERT_CONTINUE(groupId > 0);
  2997. }
  2998. else
  2999. {
  3000. groupId = ++fUsedGroups.lastId;
  3001. parsedGroups.append(groupName);
  3002. GroupNameToId groupNameToId;
  3003. groupNameToId.setData(groupId, groupName);
  3004. int pluginId = -1;
  3005. PatchbayIcon icon = jackPortHints.isHardware ? PATCHBAY_ICON_HARDWARE : PATCHBAY_ICON_APPLICATION;
  3006. findPluginIdAndIcon(groupName, pluginId, icon);
  3007. fUsedGroups.list.append(groupNameToId);
  3008. GroupToIdData groupData;
  3009. groupData.id = groupId;
  3010. groupData.icon = icon;
  3011. groupData.pluginId = pluginId;
  3012. std::strncpy(groupData.strVal, groupName, STR_MAX-1);
  3013. groupData.strVal[STR_MAX-1] = '\0';
  3014. groupCallbackData.append(groupData);
  3015. }
  3016. uint canvasPortFlags = 0x0;
  3017. canvasPortFlags |= jackPortHints.isInput ? PATCHBAY_PORT_IS_INPUT : 0x0;
  3018. /**/ if (jackPortHints.isCV)
  3019. canvasPortFlags |= PATCHBAY_PORT_TYPE_CV;
  3020. else if (jackPortHints.isOSC)
  3021. canvasPortFlags |= PATCHBAY_PORT_TYPE_OSC;
  3022. else if (jackPortHints.isAudio)
  3023. canvasPortFlags |= PATCHBAY_PORT_TYPE_AUDIO;
  3024. else if (jackPortHints.isMIDI)
  3025. canvasPortFlags |= PATCHBAY_PORT_TYPE_MIDI;
  3026. id = ++fUsedPorts.lastId;
  3027. PortNameToId portNameToId;
  3028. portNameToId.setData(groupId, id, shortPortName, fullPortName);
  3029. fUsedPorts.list.append(portNameToId);
  3030. PortToIdData portData;
  3031. portData.group = groupId;
  3032. portData.port = id;
  3033. portData.flags = canvasPortFlags;
  3034. std::strncpy(portData.strVal, shortPortName, STR_MAX-1);
  3035. portData.strVal[STR_MAX-1] = '\0';
  3036. portsCallbackData.append(portData);
  3037. }
  3038. jackbridge_free(ports);
  3039. }
  3040. // query connections, after all ports are in place
  3041. if (const char** const ports = jackbridge_get_ports(fClient, nullptr, nullptr, JackPortIsOutput))
  3042. {
  3043. for (int i=0; ports[i] != nullptr; ++i)
  3044. {
  3045. const char* const fullPortName(ports[i]);
  3046. CARLA_SAFE_ASSERT_CONTINUE(fullPortName != nullptr && fullPortName[0] != '\0');
  3047. const jack_port_t* const jackPort(jackbridge_port_by_name(fClient, fullPortName));
  3048. CARLA_SAFE_ASSERT_CONTINUE(jackPort != nullptr);
  3049. const PortNameToId& thisPort(fUsedPorts.getPortNameToId(fullPortName));
  3050. CARLA_SAFE_ASSERT_CONTINUE(thisPort.group > 0);
  3051. CARLA_SAFE_ASSERT_CONTINUE(thisPort.port > 0);
  3052. if (const char** const connections = jackbridge_port_get_all_connections(fClient, jackPort))
  3053. {
  3054. for (int j=0; connections[j] != nullptr; ++j)
  3055. {
  3056. const char* const connection(connections[j]);
  3057. CARLA_SAFE_ASSERT_CONTINUE(connection != nullptr && connection[0] != '\0');
  3058. const PortNameToId& targetPort(fUsedPorts.getPortNameToId(connection));
  3059. CARLA_SAFE_ASSERT_CONTINUE(targetPort.group > 0);
  3060. CARLA_SAFE_ASSERT_CONTINUE(targetPort.port > 0);
  3061. id = ++fUsedConnections.lastId;
  3062. ConnectionToId connectionToId;
  3063. connectionToId.setData(id, thisPort.group, thisPort.port, targetPort.group, targetPort.port);
  3064. fUsedConnections.list.append(connectionToId);
  3065. ConnectionToIdData connData;
  3066. connData.id = id;
  3067. std::snprintf(connData.strVal, STR_MAX-1, "%i:%i:%i:%i",
  3068. thisPort.group, thisPort.port, targetPort.group, targetPort.port);
  3069. connData.strVal[STR_MAX-1] = '\0';
  3070. connCallbackData.append(connData);
  3071. }
  3072. jackbridge_free(connections);
  3073. }
  3074. }
  3075. jackbridge_free(ports);
  3076. }
  3077. }
  3078. const GroupToIdData groupFallback = { 0, PATCHBAY_ICON_PLUGIN, -1, { '\0' } };
  3079. const PortToIdData portFallback = { 0, 0, 0, { '\0' } };
  3080. const ConnectionToIdData connFallback = { 0, { '\0' } };
  3081. callback(sendHost, sendOSC,
  3082. ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED,
  3083. carlaId,
  3084. PATCHBAY_ICON_CARLA,
  3085. MAIN_CARLA_PLUGIN_ID,
  3086. 0, 0.0f,
  3087. ourName);
  3088. {
  3089. const CarlaRecursiveMutexLocker crml(fThreadSafeMetadataMutex);
  3090. for (LinkedList<GroupToIdData>::Itenerator it = groupCallbackData.begin2(); it.valid(); it.next())
  3091. {
  3092. const GroupToIdData& group(it.getValue(groupFallback));
  3093. callback(sendHost, sendOSC,
  3094. ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED,
  3095. group.id,
  3096. group.icon,
  3097. group.pluginId,
  3098. 0, 0.0f,
  3099. group.strVal);
  3100. jack_uuid_t uuid;
  3101. {
  3102. char* const uuidstr = jackbridge_get_uuid_for_client_name(fClient, group.strVal);
  3103. CARLA_SAFE_ASSERT_RETURN(uuidstr != nullptr && uuidstr[0] != '\0',);
  3104. const bool parsed = jackbridge_uuid_parse(uuidstr, &uuid);
  3105. jackbridge_free(uuidstr);
  3106. /* if parsing fails, meta-data is not available..
  3107. this could be because JACK version is old, or perhaps this is an internal client */
  3108. if (! parsed)
  3109. continue;
  3110. }
  3111. char* value = nullptr;
  3112. char* type = nullptr;
  3113. if (jackbridge_get_property(uuid, URI_POSITION, &value, &type)
  3114. && value != nullptr
  3115. && type != nullptr
  3116. && std::strcmp(type, URI_TYPE_STRING) == 0)
  3117. {
  3118. if (char* sep1 = std::strstr(value, ":"))
  3119. {
  3120. int x1, y1 = 0, x2 = 0, y2 = 0;
  3121. *sep1++ = '\0';
  3122. x1 = std::atoi(value);
  3123. if (char* sep2 = std::strstr(sep1, ":"))
  3124. {
  3125. *sep2++ = '\0';
  3126. y1 = std::atoi(sep1);
  3127. if (char* sep3 = std::strstr(sep2, ":"))
  3128. {
  3129. *sep3++ = '\0';
  3130. x2 = std::atoi(sep2);
  3131. y2 = std::atoi(sep3);
  3132. }
  3133. }
  3134. jackbridge_free(value);
  3135. jackbridge_free(type);
  3136. callback(sendHost, sendOSC,
  3137. ENGINE_CALLBACK_PATCHBAY_CLIENT_POSITION_CHANGED,
  3138. group.id, x1, y1, x2, static_cast<float>(y2),
  3139. nullptr);
  3140. }
  3141. }
  3142. }
  3143. }
  3144. for (LinkedList<PortToIdData>::Itenerator it = portsCallbackData.begin2(); it.valid(); it.next())
  3145. {
  3146. const PortToIdData& port(it.getValue(portFallback));
  3147. callback(sendHost, sendOSC,
  3148. ENGINE_CALLBACK_PATCHBAY_PORT_ADDED,
  3149. port.group,
  3150. static_cast<int>(port.port),
  3151. static_cast<int>(port.flags),
  3152. 0, 0.0f,
  3153. port.strVal);
  3154. }
  3155. for (LinkedList<ConnectionToIdData>::Itenerator it = connCallbackData.begin2(); it.valid(); it.next())
  3156. {
  3157. const ConnectionToIdData& conn(it.getValue(connFallback));
  3158. callback(sendHost, sendOSC,
  3159. ENGINE_CALLBACK_PATCHBAY_CONNECTION_ADDED,
  3160. conn.id,
  3161. 0, 0, 0, 0.0f,
  3162. conn.strVal);
  3163. }
  3164. groupCallbackData.clear();
  3165. portsCallbackData.clear();
  3166. connCallbackData.clear();
  3167. }
  3168. #endif
  3169. // -------------------------------------------------------------------
  3170. void processPlugin(CarlaPluginPtr& plugin, const uint32_t nframes)
  3171. {
  3172. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  3173. CarlaEngineJackClient* const client = (CarlaEngineJackClient*)plugin->getEngineClient();
  3174. CarlaEngineJackCVSourcePorts& cvSourcePorts(client->getCVSourcePorts());
  3175. const CarlaRecursiveMutexTryLocker crmtl(cvSourcePorts.getMutex(), fFreewheel);
  3176. // const CarlaRecursiveMutexLocker crml(cvSourcePorts.getMutex());
  3177. #endif
  3178. /*
  3179. const uint32_t audioInCount = client->getPortCount(kEnginePortTypeAudio, true);
  3180. const uint32_t audioOutCount = client->getPortCount(kEnginePortTypeAudio, false);
  3181. const uint32_t cvInCount = client->getPortCount(kEnginePortTypeCV, true);
  3182. const uint32_t cvOutCount = client->getPortCount(kEnginePortTypeCV, false);
  3183. */
  3184. const uint32_t audioInCount = plugin->getAudioInCount();
  3185. const uint32_t audioOutCount = plugin->getAudioOutCount();
  3186. const uint32_t cvInCount = plugin->getCVInCount();
  3187. const uint32_t cvOutCount = plugin->getCVOutCount();
  3188. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  3189. const uint32_t cvsInCount = crmtl.wasLocked() ? cvSourcePorts.getPortCount() : 0;
  3190. #else
  3191. const uint32_t cvsInCount = 0;
  3192. #endif
  3193. const float* audioIn[audioInCount];
  3194. /* */ float* audioOut[audioOutCount];
  3195. const float* cvIn[cvInCount+cvsInCount];
  3196. /* */ float* cvOut[cvOutCount];
  3197. for (uint32_t i=0; i < audioInCount; ++i)
  3198. {
  3199. if (CarlaEngineAudioPort* const port = plugin->getAudioInPort(i))
  3200. audioIn[i] = port->getBuffer();
  3201. else
  3202. audioIn[i] = nullptr;
  3203. }
  3204. for (uint32_t i=0; i < audioOutCount; ++i)
  3205. {
  3206. if (CarlaEngineAudioPort* const port = plugin->getAudioOutPort(i))
  3207. audioOut[i] = port->getBuffer();
  3208. else
  3209. audioOut[i] = nullptr;
  3210. }
  3211. for (uint32_t i=0; i < cvInCount; ++i)
  3212. {
  3213. if (CarlaEngineCVPort* const port = plugin->getCVInPort(i))
  3214. cvIn[i] = port->getBuffer();
  3215. else
  3216. cvIn[i] = nullptr;
  3217. }
  3218. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  3219. for (uint32_t i=cvInCount, j=0; j < cvsInCount; ++i, ++j)
  3220. {
  3221. if (CarlaEngineCVPort* const port = cvSourcePorts.getPort(j))
  3222. {
  3223. port->initBuffer();
  3224. cvIn[i] = port->getBuffer();
  3225. }
  3226. else
  3227. {
  3228. cvIn[i] = nullptr;
  3229. }
  3230. }
  3231. #endif
  3232. for (uint32_t i=0; i < cvOutCount; ++i)
  3233. {
  3234. if (CarlaEngineCVPort* const port = plugin->getCVOutPort(i))
  3235. cvOut[i] = port->getBuffer();
  3236. else
  3237. cvOut[i] = nullptr;
  3238. }
  3239. float inPeaks[2] = { 0.0f };
  3240. float outPeaks[2] = { 0.0f };
  3241. for (uint32_t i=0; i < audioInCount && i < 2; ++i)
  3242. {
  3243. for (uint32_t j=0; j < nframes; ++j)
  3244. {
  3245. const float absV(std::abs(audioIn[i][j]));
  3246. if (absV > inPeaks[i])
  3247. inPeaks[i] = absV;
  3248. }
  3249. }
  3250. plugin->process(audioIn, audioOut, cvIn, cvOut, nframes);
  3251. for (uint32_t i=0; i < audioOutCount && i < 2; ++i)
  3252. {
  3253. for (uint32_t j=0; j < nframes; ++j)
  3254. {
  3255. const float absV(std::abs(audioOut[i][j]));
  3256. if (absV > outPeaks[i])
  3257. outPeaks[i] = absV;
  3258. }
  3259. }
  3260. setPluginPeaksRT(plugin->getId(), inPeaks, outPeaks);
  3261. }
  3262. #ifndef BUILD_BRIDGE
  3263. // -------------------------------------------------------------------
  3264. struct PostPonedJackEvent {
  3265. enum Type {
  3266. kTypeNull = 0,
  3267. kTypeClientUnregister,
  3268. kTypeClientPositionChange,
  3269. kTypePortRegister,
  3270. kTypePortUnregister,
  3271. kTypePortConnect,
  3272. kTypePortDisconnect,
  3273. kTypePortRename
  3274. };
  3275. Type type;
  3276. union {
  3277. struct {
  3278. char name[STR_MAX+1];
  3279. } clientUnregister;
  3280. struct {
  3281. jack_uuid_t uuid;
  3282. } clientPositionChange;
  3283. struct {
  3284. char shortName[STR_MAX+1];
  3285. char fullName[STR_MAX+1];
  3286. CarlaJackPortHints hints;
  3287. } portRegister;
  3288. struct {
  3289. char fullName[STR_MAX+1];
  3290. } portUnregister;
  3291. struct {
  3292. char oldFullName[STR_MAX+1];
  3293. char newFullName[STR_MAX+1];
  3294. char newShortName[STR_MAX+1];
  3295. } portRename;
  3296. struct {
  3297. char portNameA[STR_MAX+1];
  3298. char portNameB[STR_MAX+1];
  3299. } portConnect;
  3300. struct {
  3301. char portNameA[STR_MAX+1];
  3302. char portNameB[STR_MAX+1];
  3303. } portDisconnect;
  3304. };
  3305. };
  3306. LinkedList<PostPonedJackEvent> fPostPonedEvents;
  3307. CarlaMutex fPostPonedEventsMutex;
  3308. water::Array<jack_uuid_t> fPostPonedUUIDs;
  3309. CarlaMutex fPostPonedUUIDsMutex;
  3310. bool fIsInternalClient;
  3311. void postPoneJackCallback(PostPonedJackEvent& ev)
  3312. {
  3313. const CarlaMutexLocker cml(fPostPonedEventsMutex);
  3314. fPostPonedEvents.append(ev);
  3315. }
  3316. void run() override
  3317. {
  3318. LinkedList<PostPonedJackEvent> events;
  3319. PostPonedJackEvent nullEvent;
  3320. carla_zeroStruct(nullEvent);
  3321. for (; ! shouldThreadExit();)
  3322. {
  3323. if (fIsInternalClient)
  3324. idle();
  3325. {
  3326. const CarlaMutexLocker cml(fPostPonedEventsMutex);
  3327. if (fPostPonedEvents.count() > 0)
  3328. fPostPonedEvents.moveTo(events);
  3329. }
  3330. if (fClient == nullptr)
  3331. break;
  3332. if (events.count() == 0)
  3333. {
  3334. carla_msleep(fIsInternalClient ? 50 : 200);
  3335. continue;
  3336. }
  3337. for (LinkedList<PostPonedJackEvent>::Itenerator it = events.begin2(); it.valid(); it.next())
  3338. {
  3339. const PostPonedJackEvent& ev(it.getValue(nullEvent));
  3340. CARLA_SAFE_ASSERT_CONTINUE(ev.type != PostPonedJackEvent::kTypeNull);
  3341. switch (ev.type)
  3342. {
  3343. case PostPonedJackEvent::kTypeNull:
  3344. break;
  3345. case PostPonedJackEvent::kTypeClientUnregister:
  3346. handleJackClientUnregistrationCallback(ev.clientUnregister.name);
  3347. break;
  3348. case PostPonedJackEvent::kTypeClientPositionChange:
  3349. handleJackClientPositionChangeCallback(ev.clientPositionChange.uuid);
  3350. break;
  3351. case PostPonedJackEvent::kTypePortRegister:
  3352. handleJackPortRegistrationCallback(ev.portRegister.fullName,
  3353. ev.portRegister.shortName,
  3354. ev.portRegister.hints);
  3355. break;
  3356. case PostPonedJackEvent::kTypePortUnregister:
  3357. handleJackPortUnregistrationCallback(ev.portUnregister.fullName);
  3358. break;
  3359. case PostPonedJackEvent::kTypePortConnect:
  3360. handleJackPortConnectCallback(ev.portConnect.portNameA,
  3361. ev.portConnect.portNameB);
  3362. break;
  3363. case PostPonedJackEvent::kTypePortDisconnect:
  3364. handleJackPortDisconnectCallback(ev.portDisconnect.portNameA,
  3365. ev.portDisconnect.portNameB);
  3366. break;
  3367. case PostPonedJackEvent::kTypePortRename:
  3368. handleJackPortRenameCallback(ev.portRename.oldFullName,
  3369. ev.portRename.newFullName,
  3370. ev.portRename.newShortName);
  3371. break;
  3372. }
  3373. }
  3374. events.clear();
  3375. }
  3376. events.clear();
  3377. }
  3378. #endif // BUILD_BRIDGE
  3379. // -------------------------------------------------------------------
  3380. // disable -Wattributes warnings
  3381. #if defined(__clang__)
  3382. # pragma clang diagnostic push
  3383. # pragma clang diagnostic ignored "-Wattributes"
  3384. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  3385. # pragma GCC diagnostic push
  3386. # pragma GCC diagnostic ignored "-Wattributes"
  3387. #endif
  3388. #define handlePtr ((CarlaEngineJack*)arg)
  3389. static void JACKBRIDGE_API carla_jack_thread_init_callback(void*)
  3390. {
  3391. #ifdef __SSE2_MATH__
  3392. // Set FTZ and DAZ flags
  3393. _mm_setcsr(_mm_getcsr() | 0x8040);
  3394. #endif
  3395. }
  3396. static int JACKBRIDGE_API carla_jack_bufsize_callback(jack_nframes_t newBufferSize, void* arg)
  3397. {
  3398. handlePtr->handleJackBufferSizeCallback(newBufferSize);
  3399. return 0;
  3400. }
  3401. static int JACKBRIDGE_API carla_jack_srate_callback(jack_nframes_t newSampleRate, void* arg)
  3402. {
  3403. handlePtr->handleJackSampleRateCallback(newSampleRate);
  3404. return 0;
  3405. }
  3406. static void JACKBRIDGE_API carla_jack_freewheel_callback(int starting, void* arg)
  3407. {
  3408. handlePtr->handleJackFreewheelCallback(bool(starting));
  3409. }
  3410. static void JACKBRIDGE_API carla_jack_latency_callback(jack_latency_callback_mode_t mode, void* arg)
  3411. {
  3412. handlePtr->handleJackLatencyCallback(mode);
  3413. }
  3414. static int JACKBRIDGE_API carla_jack_process_callback(jack_nframes_t nframes, void* arg) __attribute__((annotate("realtime")))
  3415. {
  3416. handlePtr->handleJackProcessCallback(nframes);
  3417. return 0;
  3418. }
  3419. #ifndef BUILD_BRIDGE
  3420. static void JACKBRIDGE_API carla_jack_timebase_callback(jack_transport_state_t, jack_nframes_t nframes, jack_position_t* pos, int new_pos, void* arg) __attribute__((annotate("realtime")))
  3421. {
  3422. handlePtr->handleJackTimebaseCallback(nframes, pos, new_pos);
  3423. }
  3424. static void JACKBRIDGE_API carla_jack_client_registration_callback(const char* name, int reg, void* arg)
  3425. {
  3426. // ignored
  3427. if (reg != 0)
  3428. return;
  3429. PostPonedJackEvent ev;
  3430. carla_zeroStruct(ev);
  3431. ev.type = PostPonedJackEvent::kTypeClientUnregister;
  3432. std::strncpy(ev.clientUnregister.name, name, STR_MAX);
  3433. handlePtr->postPoneJackCallback(ev);
  3434. }
  3435. static void JACKBRIDGE_API carla_jack_port_registration_callback(jack_port_id_t port_id, int reg, void* arg)
  3436. {
  3437. const jack_port_t* const port = jackbridge_port_by_id(handlePtr->fClient, port_id);
  3438. CARLA_SAFE_ASSERT_RETURN(port != nullptr,);
  3439. const char* const fullName = jackbridge_port_name(port);
  3440. CARLA_SAFE_ASSERT_RETURN(fullName != nullptr && fullName[0] != '\0',);
  3441. PostPonedJackEvent ev;
  3442. carla_zeroStruct(ev);
  3443. if (reg != 0)
  3444. {
  3445. const char* const shortName = jackbridge_port_short_name(port);
  3446. CARLA_SAFE_ASSERT_RETURN(shortName != nullptr && shortName[0] != '\0',);
  3447. ev.type = PostPonedJackEvent::kTypePortRegister;
  3448. std::strncpy(ev.portRegister.fullName, fullName, STR_MAX);
  3449. std::strncpy(ev.portRegister.shortName, shortName, STR_MAX);
  3450. const CarlaRecursiveMutexLocker crml(handlePtr->fThreadSafeMetadataMutex);
  3451. ev.portRegister.hints = CarlaJackPortHints::fromPort(port);
  3452. }
  3453. else
  3454. {
  3455. ev.type = PostPonedJackEvent::kTypePortUnregister;
  3456. std::strncpy(ev.portUnregister.fullName, fullName, STR_MAX);
  3457. }
  3458. handlePtr->postPoneJackCallback(ev);
  3459. }
  3460. static void JACKBRIDGE_API carla_jack_port_connect_callback(jack_port_id_t a, jack_port_id_t b, int connect, void* arg)
  3461. {
  3462. const jack_port_t* const portA = jackbridge_port_by_id(handlePtr->fClient, a);
  3463. CARLA_SAFE_ASSERT_RETURN(portA != nullptr,);
  3464. const jack_port_t* const portB = jackbridge_port_by_id(handlePtr->fClient, b);
  3465. CARLA_SAFE_ASSERT_RETURN(portB != nullptr,);
  3466. const char* const fullNameA = jackbridge_port_name(portA);
  3467. CARLA_SAFE_ASSERT_RETURN(fullNameA != nullptr && fullNameA[0] != '\0',);
  3468. const char* const fullNameB = jackbridge_port_name(portB);
  3469. CARLA_SAFE_ASSERT_RETURN(fullNameB != nullptr && fullNameB[0] != '\0',);
  3470. PostPonedJackEvent ev;
  3471. carla_zeroStruct(ev);
  3472. if (connect != 0)
  3473. {
  3474. ev.type = PostPonedJackEvent::kTypePortConnect;
  3475. std::strncpy(ev.portConnect.portNameA, fullNameA, STR_MAX);
  3476. std::strncpy(ev.portConnect.portNameB, fullNameB, STR_MAX);
  3477. }
  3478. else
  3479. {
  3480. ev.type = PostPonedJackEvent::kTypePortDisconnect;
  3481. std::strncpy(ev.portDisconnect.portNameA, fullNameA, STR_MAX);
  3482. std::strncpy(ev.portDisconnect.portNameB, fullNameB, STR_MAX);
  3483. }
  3484. handlePtr->postPoneJackCallback(ev);
  3485. }
  3486. static void JACKBRIDGE_API carla_jack_port_rename_callback(jack_port_id_t port_id, const char* oldName, const char* newName, void* arg)
  3487. {
  3488. const jack_port_t* const port = jackbridge_port_by_id(handlePtr->fClient, port_id);
  3489. CARLA_SAFE_ASSERT_RETURN(port != nullptr,);
  3490. const char* const shortName = jackbridge_port_short_name(port);
  3491. CARLA_SAFE_ASSERT_RETURN(shortName != nullptr && shortName[0] != '\0',);
  3492. PostPonedJackEvent ev;
  3493. carla_zeroStruct(ev);
  3494. ev.type = PostPonedJackEvent::kTypePortRename;
  3495. std::strncpy(ev.portRename.oldFullName, oldName, STR_MAX);
  3496. std::strncpy(ev.portRename.newFullName, newName, STR_MAX);
  3497. std::strncpy(ev.portRename.newShortName, shortName, STR_MAX);
  3498. handlePtr->postPoneJackCallback(ev);
  3499. }
  3500. static void carla_jack_property_change_callback(jack_uuid_t subject, const char* key, jack_property_change_t change, void* arg)
  3501. {
  3502. if (change != PropertyChanged)
  3503. return;
  3504. if (std::strcmp(key, URI_POSITION) != 0)
  3505. return;
  3506. PostPonedJackEvent ev;
  3507. carla_zeroStruct(ev);
  3508. ev.type = PostPonedJackEvent::kTypeClientPositionChange;
  3509. ev.clientPositionChange.uuid = subject;
  3510. handlePtr->postPoneJackCallback(ev);
  3511. }
  3512. static int JACKBRIDGE_API carla_jack_xrun_callback(void* arg)
  3513. {
  3514. ++(handlePtr->pData->xruns);
  3515. return 0;
  3516. }
  3517. #endif
  3518. static void JACKBRIDGE_API carla_jack_shutdown_callback(void* arg)
  3519. {
  3520. handlePtr->handleJackShutdownCallback();
  3521. }
  3522. #undef handlePtr
  3523. // -------------------------------------------------------------------
  3524. #ifndef BUILD_BRIDGE
  3525. static int JACKBRIDGE_API carla_jack_process_callback_plugin(jack_nframes_t nframes, void* arg) __attribute__((annotate("realtime")))
  3526. {
  3527. CarlaPluginPtr* const pluginPtr = static_cast<CarlaPluginPtr*>(arg);
  3528. CARLA_SAFE_ASSERT_RETURN(pluginPtr != nullptr, 0);
  3529. CarlaPluginPtr plugin = *pluginPtr;
  3530. CARLA_SAFE_ASSERT_RETURN(plugin.get() != nullptr && plugin->isEnabled(), 0);
  3531. CarlaEngineJack* const engine((CarlaEngineJack*)plugin->getEngine());
  3532. CARLA_SAFE_ASSERT_RETURN(engine != nullptr, 0);
  3533. if (plugin->tryLock(engine->fFreewheel))
  3534. {
  3535. plugin->initBuffers();
  3536. engine->processPlugin(plugin, nframes);
  3537. plugin->unlock();
  3538. }
  3539. return 0;
  3540. }
  3541. /*
  3542. static int JACKBRIDGE_API carla_jack_bufsize_callback_plugin(jack_nframes_t nframes, void* arg)
  3543. {
  3544. CarlaPlugin* const plugin((CarlaPlugin*)arg);
  3545. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr && plugin->isEnabled(), 0);
  3546. plugin->bufferSizeChanged(nframes);
  3547. return 1;
  3548. }
  3549. static int JACKBRIDGE_API carla_jack_srate_callback_plugin(jack_nframes_t nframes, void* arg)
  3550. {
  3551. CarlaPlugin* const plugin((CarlaPlugin*)arg);
  3552. CARLA_SAFE_ASSERT_RETURN(plugin != nullptr && plugin->isEnabled(), 0);
  3553. plugin->sampleRateChanged(nframes);
  3554. return 1;
  3555. }
  3556. */
  3557. static void JACKBRIDGE_API carla_jack_latency_callback_plugin(jack_latency_callback_mode_t /*mode*/, void* /*arg*/)
  3558. {
  3559. // TODO
  3560. }
  3561. static void JACKBRIDGE_API carla_jack_shutdown_callback_plugin(void* arg)
  3562. {
  3563. CarlaPluginPtr* const pluginPtr = static_cast<CarlaPluginPtr*>(arg);
  3564. CARLA_SAFE_ASSERT_RETURN(pluginPtr != nullptr,);
  3565. CarlaPluginPtr plugin = *pluginPtr;
  3566. CARLA_SAFE_ASSERT_RETURN(plugin.get() != nullptr,);
  3567. CarlaEngineJack* const engine((CarlaEngineJack*)plugin->getEngine());
  3568. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  3569. engine->handlePluginJackShutdownCallback(plugin);
  3570. }
  3571. #endif
  3572. // enable -Wattributes again
  3573. #if defined(__clang__)
  3574. # pragma clang diagnostic pop
  3575. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  3576. # pragma GCC diagnostic pop
  3577. #endif
  3578. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineJack)
  3579. };
  3580. // -----------------------------------------------------------------------
  3581. namespace EngineInit {
  3582. CarlaEngine* newJack()
  3583. {
  3584. carla_debug("EngineInit::newJack()");
  3585. return new CarlaEngineJack();
  3586. }
  3587. }
  3588. // -----------------------------------------------------------------------
  3589. CARLA_BACKEND_END_NAMESPACE
  3590. #if defined(JACKBRIDGE_DIRECT) && !defined(BUILD_BRIDGE)
  3591. // -----------------------------------------------------------------------
  3592. // internal jack client
  3593. CARLA_EXPORT
  3594. int jack_initialize (jack_client_t *client, const char *load_init);
  3595. CARLA_EXPORT
  3596. void jack_finish(void *arg);
  3597. #ifdef CARLA_OS_UNIX
  3598. # include "ThreadSafeFFTW.hpp"
  3599. #endif
  3600. // -----------------------------------------------------------------------
  3601. CARLA_EXPORT
  3602. int jack_initialize(jack_client_t* const client, const char* const load_init)
  3603. {
  3604. CARLA_BACKEND_USE_NAMESPACE
  3605. #ifdef CARLA_OS_UNIX
  3606. static const ThreadSafeFFTW sThreadSafeFFTW;
  3607. #endif
  3608. EngineProcessMode mode;
  3609. if (load_init != nullptr && std::strcmp(load_init, "rack") == 0)
  3610. mode = ENGINE_PROCESS_MODE_CONTINUOUS_RACK;
  3611. else
  3612. mode = ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS;
  3613. #ifdef USING_JUCE
  3614. juce::initialiseJuce_GUI();
  3615. #endif
  3616. CarlaEngineJack* const engine = new CarlaEngineJack();
  3617. engine->setOption(ENGINE_OPTION_FORCE_STEREO, 1, nullptr);
  3618. engine->setOption(ENGINE_OPTION_AUDIO_DRIVER, 0, "JACK");
  3619. engine->setOption(ENGINE_OPTION_AUDIO_DEVICE, 0, "Auto-Connect ON");
  3620. engine->setOption(ENGINE_OPTION_OSC_ENABLED, 1, nullptr);
  3621. engine->setOption(ENGINE_OPTION_OSC_PORT_TCP, 22752, nullptr);
  3622. engine->setOption(ENGINE_OPTION_OSC_PORT_UDP, 22752, nullptr);
  3623. engine->setOption(ENGINE_OPTION_PROCESS_MODE, mode, nullptr);
  3624. engine->setOption(ENGINE_OPTION_TRANSPORT_MODE, ENGINE_TRANSPORT_MODE_JACK, nullptr);
  3625. // FIXME
  3626. engine->setOption(ENGINE_OPTION_PATH_BINARIES, 0, "/usr/lib/carla");
  3627. engine->setOption(ENGINE_OPTION_PATH_RESOURCES, 0, "/usr/share/resources");
  3628. if (engine->initInternal(client))
  3629. return 0;
  3630. delete engine;
  3631. #ifdef USING_JUCE
  3632. juce::shutdownJuce_GUI();
  3633. #endif
  3634. return 1;
  3635. }
  3636. CARLA_EXPORT
  3637. void jack_finish(void *arg)
  3638. {
  3639. CARLA_BACKEND_USE_NAMESPACE
  3640. CarlaEngineJack* const engine = (CarlaEngineJack*)arg;;
  3641. CARLA_SAFE_ASSERT_RETURN(engine != nullptr,);
  3642. engine->setAboutToClose();
  3643. engine->removeAllPlugins();
  3644. engine->close();
  3645. delete engine;
  3646. #ifdef USING_JUCE
  3647. juce::shutdownJuce_GUI();
  3648. #endif
  3649. }
  3650. // -----------------------------------------------------------------------
  3651. #endif // defined(JACKBRIDGE_DIRECT) && !defined(BUILD_BRIDGE)