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 163KB

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