utils_lilv.cpp 208 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140
  1. // SPDX-FileCopyrightText: 2012-2025 MOD Audio UG
  2. // SPDX-License-Identifier: AGPL-3.0-or-later
  3. #include "utils.h"
  4. #include <libgen.h>
  5. #include <limits.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <lilv/lilv.h>
  9. #include <lv2/core/lv2.h>
  10. #include <lv2/atom/atom.h>
  11. #include <lv2/midi/midi.h>
  12. #include <lv2/morph/morph.h>
  13. #include <lv2/patch/patch.h>
  14. #include <lv2/port-groups/port-groups.h>
  15. #include <lv2/port-props/port-props.h>
  16. #include <lv2/presets/presets.h>
  17. #include <lv2/state/state.h>
  18. #include <lv2/units/units.h>
  19. // do not enable external-ui support in embed targets
  20. #if !(defined(_MOD_DEVICE_DUO) || defined(_MOD_DEVICE_DUOX) || defined(_MOD_DEVICE_DWARF))
  21. #define WITH_EXTERNAL_UI_SUPPORT
  22. #endif
  23. #ifdef WITH_EXTERNAL_UI_SUPPORT
  24. #include <lv2/ui/ui.h>
  25. #endif
  26. #include "sha1/sha1.h"
  27. #include <algorithm>
  28. #include <cassert>
  29. #include <fstream>
  30. #include <list>
  31. #include <map>
  32. #include <string>
  33. #include <unordered_map>
  34. #include <vector>
  35. #ifdef __linux__
  36. # include <sched.h>
  37. #endif
  38. #ifdef _WIN32
  39. # include <io.h>
  40. # include <shlobj.h>
  41. # include <windows.h>
  42. typedef unsigned int uint;
  43. static char* realpath(const char* const name, char* const resolved)
  44. {
  45. if (name == nullptr)
  46. return nullptr;
  47. if (_access(name, 4) != 0)
  48. return nullptr;
  49. char* retname = nullptr;
  50. if ((retname = resolved) == nullptr)
  51. retname = static_cast<char*>(malloc(PATH_MAX + 2));
  52. if (retname == nullptr)
  53. return nullptr;
  54. return _fullpath(retname, name, PATH_MAX);
  55. }
  56. #else
  57. # include <pwd.h>
  58. # include <sys/types.h>
  59. # include <unistd.h>
  60. #endif
  61. #ifdef _WIN32
  62. # define OS_SEP '\\'
  63. #else
  64. # define OS_SEP '/'
  65. #endif
  66. #define MOD_LICENSE__interface "http://moddevices.com/ns/ext/license#interface"
  67. #ifndef HAVE_NEW_LILV
  68. #warning Your current lilv version is too old, please update it
  69. char* lilv_file_uri_parse2(const char* uri, const char*)
  70. {
  71. if (const char* const parsed = lilv_uri_to_path(uri))
  72. return strdup(parsed);
  73. return nullptr;
  74. }
  75. LilvNode* lilv_new_file_uri2(LilvWorld* world, const char*, const char* path)
  76. {
  77. const size_t pathlen = strlen(path);
  78. char* const uripath = static_cast<char*>(malloc(pathlen+12));
  79. strcpy(uripath, "file://");
  80. strcat(uripath, path);
  81. LilvNode* const ret = lilv_new_uri(world, uripath);
  82. free(uripath);
  83. return ret;
  84. }
  85. #define lilv_free(x) free(x)
  86. #define lilv_new_file_uri(x,y,z) lilv_new_file_uri2(x,y,z)
  87. #else
  88. char* lilv_file_uri_parse2(const char* uri, char** hostname)
  89. {
  90. if (char* const parsed = lilv_file_uri_parse(uri, hostname))
  91. {
  92. #ifdef _WIN32
  93. char* convertslashes = parsed;
  94. do {
  95. if (*convertslashes == '/')
  96. *convertslashes = '\\';
  97. } while (*(++convertslashes) != '\0');
  98. #endif
  99. return parsed;
  100. }
  101. return nullptr;
  102. }
  103. #endif
  104. #ifndef LV2_CORE__Parameter
  105. #define LV2_CORE__Parameter LV2_CORE_PREFIX "Parameter" // http://lv2plug.in/ns/lv2core#Parameter
  106. #endif
  107. // our lilv world
  108. LilvWorld* W = nullptr;
  109. // list of loaded bundles
  110. std::list<std::string> BUNDLES;
  111. // list of lilv plugins
  112. const LilvPlugins* PLUGINS = nullptr;
  113. // plugin info, mapped to URIs
  114. std::unordered_map<std::string, PluginInfo> PLUGNFO;
  115. std::unordered_map<std::string, const PluginInfo_Mini*> PLUGNFO_Mini;
  116. // list of plugins that need reload (preset data only)
  117. std::list<std::string> PLUGINStoReload;
  118. // static factory info (only loaded once)
  119. const PedalboardInfo_Mini** FACTORYINFO = nullptr;
  120. // read KEYS_PATH. NOTE: assumes trailing separator
  121. static const char* const KEYS_PATH = getenv("MOD_KEYS_PATH");
  122. static const size_t KEYS_PATHlen = (KEYS_PATH != NULL && *KEYS_PATH != '\0')
  123. ? strlen(KEYS_PATH)
  124. : 0;
  125. // read FACTORY_PEDALBOARDS_DIR
  126. static const char* const FACTORY_PEDALBOARDS_DIR = getenv("MOD_FACTORY_PEDALBOARDS_DIR");
  127. static const size_t FACTORY_PEDALBOARDS_DIRlen = (FACTORY_PEDALBOARDS_DIR != NULL && *FACTORY_PEDALBOARDS_DIR != '\0')
  128. ? strlen(FACTORY_PEDALBOARDS_DIR)
  129. : 0;
  130. // some other cached values
  131. static const char* getHOME()
  132. {
  133. #ifdef _WIN32
  134. WCHAR wpath[MAX_PATH + 256];
  135. if (SHGetSpecialFolderPathW(nullptr, wpath, CSIDL_MYDOCUMENTS, FALSE))
  136. {
  137. static CHAR apath[MAX_PATH + 256];
  138. if (WideCharToMultiByte(CP_UTF8, 0, wpath, -1, apath, MAX_PATH + 256, nullptr, nullptr))
  139. return apath;
  140. }
  141. #else
  142. if (const char* const home = getenv("HOME"))
  143. return home;
  144. if (struct passwd* const pwd = getpwuid(getuid()))
  145. return pwd->pw_dir;
  146. #endif
  147. return "";
  148. }
  149. static const char* const HOME = getHOME();
  150. static size_t HOMElen = strlen(HOME);
  151. // configuration
  152. static const bool kAllowRegularCV = getenv("MOD_UI_ALLOW_REGULAR_CV") != nullptr;
  153. static const bool kOnlyShowPluginsWithMODGUI = getenv("MOD_UI_ONLY_SHOW_PLUGINS_WITH_MODGUI") != nullptr;
  154. #define PluginInfo_Init { \
  155. false, \
  156. nullptr, nullptr, \
  157. nullptr, nullptr, nullptr, nullptr, nullptr, \
  158. nullptr, nullptr, 0, 0, 0, 0, 0, 0, false, \
  159. nullptr, nullptr, \
  160. { nullptr, nullptr, nullptr }, \
  161. nullptr, \
  162. { \
  163. nullptr, nullptr, nullptr, nullptr, nullptr, \
  164. nullptr, nullptr, nullptr, nullptr, \
  165. nullptr, nullptr, \
  166. nullptr, nullptr, nullptr, nullptr, \
  167. nullptr, nullptr \
  168. }, \
  169. { \
  170. { nullptr, nullptr }, \
  171. { nullptr, nullptr }, \
  172. { nullptr, nullptr }, \
  173. { nullptr, nullptr } \
  174. }, \
  175. nullptr, \
  176. nullptr, \
  177. nullptr \
  178. }
  179. // Blacklisted plugins, which don't work properly on MOD for various reasons
  180. static const std::vector<std::string> BLACKLIST = {
  181. "urn:mod:mclk",
  182. "urn:mod:gxtuner",
  183. "urn:mod:tuna",
  184. "http://calf.sourceforge.net/plugins/Analyzer",
  185. };
  186. // --------------------------------------------------------------------------------------------------------
  187. inline char* getenv_strdup_or_null(const char* const env)
  188. {
  189. if (const char* const value = getenv(env))
  190. return strdup(value);
  191. return nullptr;
  192. }
  193. template<typename T>
  194. inline bool contains(const std::map<std::string, T>& map, const std::string& value)
  195. {
  196. return map.find(value) != map.end();
  197. }
  198. template<typename T>
  199. inline bool contains(const std::unordered_map<std::string, T>& map, const std::string& value)
  200. {
  201. return map.find(value) != map.end();
  202. }
  203. inline bool ends_with(const std::string& value, const std::string& ending)
  204. {
  205. if (ending.size() > value.size())
  206. return false;
  207. return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
  208. }
  209. inline bool is_factory_pedalboard(const char* const bundle)
  210. {
  211. if (FACTORY_PEDALBOARDS_DIRlen != 0)
  212. return strncmp(bundle, FACTORY_PEDALBOARDS_DIR, FACTORY_PEDALBOARDS_DIRlen) == 0;
  213. return strncmp(bundle, "/usr/share/mod/pedalboards/", 27) == 0;
  214. }
  215. inline std::string sha1(const char* const cstring)
  216. {
  217. sha1nfo s;
  218. sha1_init(&s);
  219. sha1_write(&s, cstring, strlen(cstring));
  220. char hashdec[HASH_LENGTH*2+1];
  221. uint8_t* const hashenc = sha1_result(&s);
  222. for (int i=0; i<HASH_LENGTH; i++) {
  223. snprintf(hashdec+(i*2), 3, "%02x", hashenc[i]);
  224. }
  225. hashdec[HASH_LENGTH*2] = '\0';
  226. return std::string(hashdec);
  227. }
  228. template<class PInfo>
  229. inline void fill_iotype(PInfo* const info,
  230. const uint countAudioInput,
  231. const uint countAudioOutput,
  232. const uint countMidiInput,
  233. const uint countMidiOutput)
  234. {
  235. if (countMidiInput >= 1 && countAudioOutput >= 1 && countAudioInput == 0)
  236. info->iotype = kPluginIOInstrument;
  237. else if (countAudioOutput >= 2 && countAudioInput >= 2)
  238. info->iotype = kPluginIOAudioStereo;
  239. else if (countAudioOutput == 1 && countAudioInput == 1)
  240. info->iotype = kPluginIOAudioMono;
  241. else if (countMidiInput >= 1 && countMidiOutput >= 1)
  242. info->iotype = kPluginIOMIDI;
  243. else
  244. info->iotype = kPluginIONull;
  245. }
  246. // --------------------------------------------------------------------------------------------------------
  247. #define LILV_NS_INGEN "http://drobilla.net/ns/ingen#"
  248. #define LILV_NS_MOD "http://moddevices.com/ns/mod#"
  249. #define LILV_NS_MODGUI "http://moddevices.com/ns/modgui#"
  250. #define LILV_NS_MODPEDAL "http://moddevices.com/ns/modpedal#"
  251. #define LILV_NS_MODPERFORMANCE "http://moddevices.com/ns/modperformance#"
  252. #define LV2_NS_PORT_GROUPS "http://lv2plug.in/ns/ext/port-groups#"
  253. #define MOD__CVPort LILV_NS_MOD "CVPort"
  254. struct NamespaceDefinitions {
  255. LilvNode* doap_license;
  256. LilvNode* doap_maintainer;
  257. LilvNode* foaf_homepage;
  258. LilvNode* rdf_type;
  259. LilvNode* rdfs_comment;
  260. LilvNode* rdfs_label;
  261. LilvNode* rdfs_range;
  262. LilvNode* lv2core_designation;
  263. LilvNode* lv2core_index;
  264. LilvNode* lv2core_microVersion;
  265. LilvNode* lv2core_minorVersion;
  266. LilvNode* lv2core_name;
  267. LilvNode* lv2core_project;
  268. LilvNode* lv2core_portProperty;
  269. LilvNode* lv2core_shortName;
  270. LilvNode* lv2core_symbol;
  271. LilvNode* lv2core_default;
  272. LilvNode* lv2core_minimum;
  273. LilvNode* lv2core_maximum;
  274. LilvNode* lv2core_extensionData;
  275. LilvNode* mod_brand;
  276. LilvNode* mod_label;
  277. LilvNode* mod_default;
  278. LilvNode* mod_default_custom;
  279. LilvNode* mod_minimum;
  280. LilvNode* mod_maximum;
  281. LilvNode* mod_rangeSteps;
  282. LilvNode* mod_release;
  283. LilvNode* mod_builder;
  284. LilvNode* mod_buildEnvironment;
  285. LilvNode* mod_fileTypes;
  286. LilvNode* mod_supportedExtensions;
  287. LilvNode* mod_rawMIDIClockAccess;
  288. LilvNode* modlicense_interface;
  289. LilvNode* modgui_gui;
  290. LilvNode* modgui_resourcesDirectory;
  291. LilvNode* modgui_iconTemplate;
  292. LilvNode* modgui_settingsTemplate;
  293. LilvNode* modgui_javascript;
  294. LilvNode* modgui_stylesheet;
  295. LilvNode* modgui_screenshot;
  296. LilvNode* modgui_thumbnail;
  297. LilvNode* modgui_discussionURL;
  298. LilvNode* modgui_documentation;
  299. LilvNode* modgui_brand;
  300. LilvNode* modgui_label;
  301. LilvNode* modgui_model;
  302. LilvNode* modgui_panel;
  303. LilvNode* modgui_color;
  304. LilvNode* modgui_knob;
  305. LilvNode* modgui_port;
  306. LilvNode* modgui_monitoredOutputs;
  307. LilvNode* atom_bufferType;
  308. LilvNode* atom_Sequence;
  309. LilvNode* midi_MidiEvent;
  310. LilvNode* pgroups_group;
  311. LilvNode* pprops_rangeSteps;
  312. LilvNode* patch_readable;
  313. LilvNode* patch_writable;
  314. LilvNode* pset_Preset;
  315. LilvNode* state_state;
  316. LilvNode* units_render;
  317. LilvNode* units_symbol;
  318. LilvNode* units_unit;
  319. bool initialized;
  320. NamespaceDefinitions(LilvWorld* const w)
  321. {
  322. initialized = false;
  323. init(w);
  324. }
  325. ~NamespaceDefinitions()
  326. {
  327. cleanup();
  328. initialized = false;
  329. }
  330. void init(LilvWorld* const w)
  331. {
  332. if (initialized)
  333. return;
  334. initialized = true;
  335. doap_license = lilv_new_uri(w, LILV_NS_DOAP "license");
  336. doap_maintainer = lilv_new_uri(w, LILV_NS_DOAP "maintainer");
  337. foaf_homepage = lilv_new_uri(w, LILV_NS_FOAF "homepage");
  338. rdf_type = lilv_new_uri(w, LILV_NS_RDF "type");
  339. rdfs_comment = lilv_new_uri(w, LILV_NS_RDFS "comment");
  340. rdfs_label = lilv_new_uri(w, LILV_NS_RDFS "label");
  341. rdfs_range = lilv_new_uri(w, LILV_NS_RDFS "range");
  342. lv2core_designation = lilv_new_uri(w, LILV_NS_LV2 "designation");
  343. lv2core_index = lilv_new_uri(w, LILV_NS_LV2 "index");
  344. lv2core_microVersion = lilv_new_uri(w, LILV_NS_LV2 "microVersion");
  345. lv2core_minorVersion = lilv_new_uri(w, LILV_NS_LV2 "minorVersion");
  346. lv2core_name = lilv_new_uri(w, LILV_NS_LV2 "name");
  347. lv2core_project = lilv_new_uri(w, LILV_NS_LV2 "project");
  348. lv2core_portProperty = lilv_new_uri(w, LILV_NS_LV2 "portProperty");
  349. lv2core_shortName = lilv_new_uri(w, LILV_NS_LV2 "shortName");
  350. lv2core_symbol = lilv_new_uri(w, LILV_NS_LV2 "symbol");
  351. lv2core_default = lilv_new_uri(w, LILV_NS_LV2 "default");
  352. lv2core_minimum = lilv_new_uri(w, LILV_NS_LV2 "minimum");
  353. lv2core_maximum = lilv_new_uri(w, LILV_NS_LV2 "maximum");
  354. lv2core_extensionData = lilv_new_uri(w, LILV_NS_LV2 "extensionData");
  355. mod_brand = lilv_new_uri(w, LILV_NS_MOD "brand");
  356. mod_label = lilv_new_uri(w, LILV_NS_MOD "label");
  357. mod_default = lilv_new_uri(w, LILV_NS_MOD "default");
  358. #if defined(_MOD_DEVICE_DUO)
  359. mod_default_custom = lilv_new_uri(w, LILV_NS_MOD "default_duo");
  360. #elif defined(_MOD_DEVICE_DUOX)
  361. mod_default_custom = lilv_new_uri(w, LILV_NS_MOD "default_duox");
  362. #elif defined(_MOD_DEVICE_DWARF)
  363. mod_default_custom = lilv_new_uri(w, LILV_NS_MOD "default_dwarf");
  364. #elif defined(_MOD_DEVICE_X86_64)
  365. mod_default_custom = lilv_new_uri(w, LILV_NS_MOD "default_x64");
  366. #else
  367. mod_default_custom = nullptr;
  368. #endif
  369. mod_minimum = lilv_new_uri(w, LILV_NS_MOD "minimum");
  370. mod_maximum = lilv_new_uri(w, LILV_NS_MOD "maximum");
  371. mod_rangeSteps = lilv_new_uri(w, LILV_NS_MOD "rangeSteps");
  372. mod_release = lilv_new_uri(w, LILV_NS_MOD "releaseNumber");
  373. mod_builder = lilv_new_uri(w, LILV_NS_MOD "builderVersion");
  374. mod_buildEnvironment = lilv_new_uri(w, LILV_NS_MOD "buildEnvironment");
  375. mod_fileTypes = lilv_new_uri(w, LILV_NS_MOD "fileTypes");
  376. mod_supportedExtensions = lilv_new_uri(w, LILV_NS_MOD "supportedExtensions");
  377. mod_rawMIDIClockAccess = lilv_new_uri(w, LILV_NS_MOD "rawMIDIClockAccess");
  378. modlicense_interface = lilv_new_uri(w, MOD_LICENSE__interface);
  379. modgui_gui = lilv_new_uri(w, LILV_NS_MODGUI "gui");
  380. modgui_resourcesDirectory= lilv_new_uri(w, LILV_NS_MODGUI "resourcesDirectory");
  381. modgui_iconTemplate = lilv_new_uri(w, LILV_NS_MODGUI "iconTemplate");
  382. modgui_settingsTemplate = lilv_new_uri(w, LILV_NS_MODGUI "settingsTemplate");
  383. modgui_javascript = lilv_new_uri(w, LILV_NS_MODGUI "javascript");
  384. modgui_stylesheet = lilv_new_uri(w, LILV_NS_MODGUI "stylesheet");
  385. modgui_screenshot = lilv_new_uri(w, LILV_NS_MODGUI "screenshot");
  386. modgui_thumbnail = lilv_new_uri(w, LILV_NS_MODGUI "thumbnail");
  387. modgui_discussionURL = lilv_new_uri(w, LILV_NS_MODGUI "discussionURL");
  388. modgui_documentation = lilv_new_uri(w, LILV_NS_MODGUI "documentation");
  389. modgui_brand = lilv_new_uri(w, LILV_NS_MODGUI "brand");
  390. modgui_label = lilv_new_uri(w, LILV_NS_MODGUI "label");
  391. modgui_model = lilv_new_uri(w, LILV_NS_MODGUI "model");
  392. modgui_panel = lilv_new_uri(w, LILV_NS_MODGUI "panel");
  393. modgui_color = lilv_new_uri(w, LILV_NS_MODGUI "color");
  394. modgui_knob = lilv_new_uri(w, LILV_NS_MODGUI "knob");
  395. modgui_port = lilv_new_uri(w, LILV_NS_MODGUI "port");
  396. modgui_monitoredOutputs = lilv_new_uri(w, LILV_NS_MODGUI "monitoredOutputs");
  397. atom_bufferType = lilv_new_uri(w, LV2_ATOM__bufferType);
  398. atom_Sequence = lilv_new_uri(w, LV2_ATOM__Sequence);
  399. midi_MidiEvent = lilv_new_uri(w, LV2_MIDI__MidiEvent);
  400. pgroups_group = lilv_new_uri(w, LV2_PORT_GROUPS__group);
  401. pprops_rangeSteps = lilv_new_uri(w, LV2_PORT_PROPS__rangeSteps);
  402. patch_readable = lilv_new_uri(w, LV2_PATCH__readable);
  403. patch_writable = lilv_new_uri(w, LV2_PATCH__writable);
  404. pset_Preset = lilv_new_uri(w, LV2_PRESETS__Preset);
  405. state_state = lilv_new_uri(w, LV2_STATE__state);
  406. units_render = lilv_new_uri(w, LV2_UNITS__render);
  407. units_symbol = lilv_new_uri(w, LV2_UNITS__symbol);
  408. units_unit = lilv_new_uri(w, LV2_UNITS__unit);
  409. }
  410. void cleanup()
  411. {
  412. if (!initialized)
  413. return;
  414. initialized = false;
  415. lilv_node_free(doap_license);
  416. lilv_node_free(doap_maintainer);
  417. lilv_node_free(foaf_homepage);
  418. lilv_node_free(rdf_type);
  419. lilv_node_free(rdfs_comment);
  420. lilv_node_free(rdfs_label);
  421. lilv_node_free(rdfs_range);
  422. lilv_node_free(lv2core_designation);
  423. lilv_node_free(lv2core_index);
  424. lilv_node_free(lv2core_microVersion);
  425. lilv_node_free(lv2core_minorVersion);
  426. lilv_node_free(lv2core_name);
  427. lilv_node_free(lv2core_project);
  428. lilv_node_free(lv2core_portProperty);
  429. lilv_node_free(lv2core_shortName);
  430. lilv_node_free(lv2core_symbol);
  431. lilv_node_free(lv2core_default);
  432. lilv_node_free(lv2core_minimum);
  433. lilv_node_free(lv2core_maximum);
  434. lilv_node_free(lv2core_extensionData);
  435. lilv_node_free(mod_brand);
  436. lilv_node_free(mod_label);
  437. lilv_node_free(mod_default);
  438. lilv_node_free(mod_default_custom);
  439. lilv_node_free(mod_minimum);
  440. lilv_node_free(mod_maximum);
  441. lilv_node_free(mod_rangeSteps);
  442. lilv_node_free(mod_release);
  443. lilv_node_free(mod_builder);
  444. lilv_node_free(mod_buildEnvironment);
  445. lilv_node_free(mod_fileTypes);
  446. lilv_node_free(mod_supportedExtensions);
  447. lilv_node_free(mod_rawMIDIClockAccess);
  448. lilv_node_free(modlicense_interface);
  449. lilv_node_free(modgui_gui);
  450. lilv_node_free(modgui_resourcesDirectory);
  451. lilv_node_free(modgui_iconTemplate);
  452. lilv_node_free(modgui_settingsTemplate);
  453. lilv_node_free(modgui_javascript);
  454. lilv_node_free(modgui_stylesheet);
  455. lilv_node_free(modgui_screenshot);
  456. lilv_node_free(modgui_thumbnail);
  457. lilv_node_free(modgui_discussionURL);
  458. lilv_node_free(modgui_documentation);
  459. lilv_node_free(modgui_brand);
  460. lilv_node_free(modgui_label);
  461. lilv_node_free(modgui_model);
  462. lilv_node_free(modgui_panel);
  463. lilv_node_free(modgui_color);
  464. lilv_node_free(modgui_knob);
  465. lilv_node_free(modgui_port);
  466. lilv_node_free(modgui_monitoredOutputs);
  467. lilv_node_free(atom_bufferType);
  468. lilv_node_free(atom_Sequence);
  469. lilv_node_free(midi_MidiEvent);
  470. lilv_node_free(pgroups_group);
  471. lilv_node_free(pprops_rangeSteps);
  472. lilv_node_free(patch_readable);
  473. lilv_node_free(patch_writable);
  474. lilv_node_free(pset_Preset);
  475. lilv_node_free(state_state);
  476. lilv_node_free(units_render);
  477. lilv_node_free(units_symbol);
  478. lilv_node_free(units_unit);
  479. }
  480. static NamespaceDefinitions& getStaticInstance(LilvWorld* const w)
  481. {
  482. static NamespaceDefinitions ns(w);
  483. return ns;
  484. }
  485. };
  486. enum PortDirection {
  487. kPortDirectionNull,
  488. kPortDirectionInput,
  489. kPortDirectionOutput,
  490. };
  491. enum PortType {
  492. kPortTypeNull,
  493. kPortTypeAudio,
  494. kPortTypeControl,
  495. kPortTypeCV,
  496. kPortTypeMIDI,
  497. };
  498. enum TimePortType {
  499. kTimePortTypeNull,
  500. kTimePortTypeBeatsPerBar,
  501. kTimePortTypeBeatsPerMinute,
  502. kTimePortTypeRolling,
  503. };
  504. static const char* const kCategoryDelayPlugin[] = { "Delay", nullptr };
  505. static const char* const kCategoryDistortionPlugin[] = { "Distortion", nullptr };
  506. static const char* const kCategoryWaveshaperPlugin[] = { "Distortion", "Waveshaper", nullptr };
  507. static const char* const kCategoryDynamicsPlugin[] = { "Dynamics", nullptr };
  508. static const char* const kCategoryAmplifierPlugin[] = { "Dynamics", "Amplifier", nullptr };
  509. static const char* const kCategoryCompressorPlugin[] = { "Dynamics", "Compressor", nullptr };
  510. static const char* const kCategoryExpanderPlugin[] = { "Dynamics", "Expander", nullptr };
  511. static const char* const kCategoryGatePlugin[] = { "Dynamics", "Gate", nullptr };
  512. static const char* const kCategoryLimiterPlugin[] = { "Dynamics", "Limiter", nullptr };
  513. static const char* const kCategoryFilterPlugin[] = { "Filter", nullptr };
  514. static const char* const kCategoryAllpassPlugin[] = { "Filter", "Allpass", nullptr };
  515. static const char* const kCategoryBandpassPlugin[] = { "Filter", "Bandpass", nullptr };
  516. static const char* const kCategoryCombPlugin[] = { "Filter", "Comb", nullptr };
  517. static const char* const kCategoryEQPlugin[] = { "Filter", "Equaliser", nullptr };
  518. static const char* const kCategoryMultiEQPlugin[] = { "Filter", "Equaliser", "Multiband", nullptr };
  519. static const char* const kCategoryParaEQPlugin[] = { "Filter", "Equaliser", "Parametric", nullptr };
  520. static const char* const kCategoryHighpassPlugin[] = { "Filter", "Highpass", nullptr };
  521. static const char* const kCategoryLowpassPlugin[] = { "Filter", "Lowpass", nullptr };
  522. static const char* const kCategoryGeneratorPlugin[] = { "Generator", nullptr };
  523. static const char* const kCategoryConstantPlugin[] = { "Generator", "Constant", nullptr };
  524. static const char* const kCategoryInstrumentPlugin[] = { "Generator", "Instrument", nullptr };
  525. static const char* const kCategoryOscillatorPlugin[] = { "Generator", "Oscillator", nullptr };
  526. static const char* const kCategoryModulatorPlugin[] = { "Modulator", nullptr };
  527. static const char* const kCategoryChorusPlugin[] = { "Modulator", "Chorus", nullptr };
  528. static const char* const kCategoryFlangerPlugin[] = { "Modulator", "Flanger", nullptr };
  529. static const char* const kCategoryPhaserPlugin[] = { "Modulator", "Phaser", nullptr };
  530. static const char* const kCategoryReverbPlugin[] = { "Reverb", nullptr };
  531. static const char* const kCategorySimulatorPlugin[] = { "Simulator", nullptr };
  532. static const char* const kCategorySpatialPlugin[] = { "Spatial", nullptr };
  533. static const char* const kCategorySpectralPlugin[] = { "Spectral", nullptr };
  534. static const char* const kCategoryPitchPlugin[] = { "Spectral", "Pitch Shifter", nullptr };
  535. static const char* const kCategoryUtilityPlugin[] = { "Utility", nullptr };
  536. static const char* const kCategoryAnalyserPlugin[] = { "Utility", "Analyser", nullptr };
  537. static const char* const kCategoryConverterPlugin[] = { "Utility", "Converter", nullptr };
  538. static const char* const kCategoryFunctionPlugin[] = { "Utility", "Function", nullptr };
  539. static const char* const kCategoryMixerPlugin[] = { "Utility", "Mixer", nullptr };
  540. static const char* const kCategoryMIDIPlugin[] = { "MIDI", "Utility", nullptr };
  541. static const char* const kCategoryMIDIPluginMOD[] = { "MIDI", nullptr };
  542. static const char* const kCategoryMaxGenPluginMOD[] = { "MaxGen", nullptr };
  543. static const char* const kCategoryCamomilePluginMOD[] = { "Camomile", nullptr };
  544. static const char* const kCategoryControlVoltagePluginMOD[] = { "ControlVoltage", nullptr };
  545. static const char* const kBuildEnvironmentProd = "prod";
  546. static const char* const kBuildEnvironmentDev = "dev";
  547. static const char* const kBuildEnvironmentLabs = "labs";
  548. static const char* const kStabilityExperimental = "experimental";
  549. static const char* const kStabilityStable = "stable";
  550. static const char* const kStabilityTesting = "testing";
  551. // static const char* const kStabilityUnstable = "unstable";
  552. static const char* const kUntitled = "Untitled";
  553. // label, render, symbol
  554. static const char* const kUnit_s[] = { "seconds", "%f s", "s" };
  555. static const char* const kUnit_ms[] = { "milliseconds", "%f ms", "ms" };
  556. static const char* const kUnit_min[] = { "minutes", "%f mins", "min" };
  557. static const char* const kUnit_bar[] = { "bars", "%f bars", "bars" };
  558. static const char* const kUnit_beat[] = { "beats", "%f beats", "beats" };
  559. static const char* const kUnit_frame[] = { "audio frames", "%f frames", "frames" };
  560. static const char* const kUnit_m[] = { "metres", "%f m", "m" };
  561. static const char* const kUnit_cm[] = { "centimetres", "%f cm", "cm" };
  562. static const char* const kUnit_mm[] = { "millimetres", "%f mm", "mm" };
  563. static const char* const kUnit_km[] = { "kilometres", "%f km", "km" };
  564. static const char* const kUnit_inch[] = { "inches", """%f\"""", "in" };
  565. static const char* const kUnit_mile[] = { "miles", "%f mi", "mi" };
  566. static const char* const kUnit_db[] = { "decibels", "%f dB", "dB" };
  567. static const char* const kUnit_pc[] = { "percent", "%f%%", "%" };
  568. static const char* const kUnit_coef[] = { "coefficient", "* %f", "*" };
  569. static const char* const kUnit_hz[] = { "hertz", "%f Hz", "Hz" };
  570. static const char* const kUnit_khz[] = { "kilohertz", "%f kHz", "kHz" };
  571. static const char* const kUnit_mhz[] = { "megahertz", "%f MHz", "MHz" };
  572. static const char* const kUnit_bpm[] = { "beats per minute", "%f BPM", "BPM" };
  573. static const char* const kUnit_oct[] = { "octaves", "%f octaves", "oct" };
  574. static const char* const kUnit_cent[] = { "cents", "%f ct", "ct" };
  575. static const char* const kUnit_semitone12TET[] = { "semitones", "%f semi", "semi" };
  576. static const char* const kUnit_degree[] = { "degrees", "%f deg", "deg" };
  577. static const char* const kUnit_midiNote[] = { "MIDI note", "MIDI note %d", "note" };
  578. static const char* const kUnit_volts[] = { "volts", "%f v", "v" };
  579. static const char nc[1] = { '\0' };
  580. static bool _isalnum(const char* const string)
  581. {
  582. for (size_t i=0;; ++i)
  583. {
  584. if (string[i] == '\0')
  585. return (i != 0);
  586. if (! isalnum(string[i]))
  587. return false;
  588. }
  589. }
  590. static void _swap_preset_data(PluginPreset* preset1, PluginPreset* preset2)
  591. {
  592. std::swap(preset1->uri, preset2->uri);
  593. std::swap(preset1->label, preset2->label);
  594. std::swap(preset1->path, preset2->path);
  595. }
  596. // adjusted from https://stackoverflow.com/questions/19612152/quicksort-string-array-in-c
  597. static void _sort_presets_data(PluginPreset presets[], unsigned int count)
  598. {
  599. if (count <= 1)
  600. return;
  601. unsigned int pvt = 0;
  602. // swap a randomly selected value to the last node
  603. _swap_preset_data(presets+(rand() % count), presets+(count-1));
  604. // reset the pivot index to zero, then scan
  605. for (unsigned int i=0; i<count-1; ++i)
  606. {
  607. if (strcmp(presets[i].uri, presets[count-1].uri) < 0)
  608. _swap_preset_data(presets+i, presets+(pvt++));
  609. }
  610. // move the pivot value into its place
  611. _swap_preset_data(presets+pvt, presets+count-1);
  612. // and invoke on the subsequences. does NOT include the pivot-slot
  613. _sort_presets_data(presets, pvt++);
  614. _sort_presets_data(presets+pvt, count - pvt);
  615. }
  616. // adjust bundle safely to lilv, as it wants the last character as the separator
  617. // this also ensures paths are always written the same way
  618. // NOTE: returned value must not be freed or cached
  619. static const char* _get_safe_bundlepath(const char* const bundle, size_t& bundlepathsize)
  620. {
  621. static char tmppath[PATH_MAX+2];
  622. char* bundlepath = realpath(bundle, tmppath);
  623. if (bundlepath == nullptr)
  624. {
  625. bundlepathsize = 0;
  626. return nullptr;
  627. }
  628. bundlepathsize = strlen(bundlepath);
  629. if (bundlepathsize <= 1)
  630. return nullptr;
  631. if (bundlepath[bundlepathsize] != OS_SEP)
  632. {
  633. bundlepath[bundlepathsize ] = OS_SEP;
  634. bundlepath[bundlepathsize+1] = '\0';
  635. }
  636. return bundlepath;
  637. }
  638. // proper lilv_file_uri_parse function that returns absolute paths
  639. static char* lilv_file_abspath(const char* const path)
  640. {
  641. if (char* const lilvpath = lilv_file_uri_parse2(path, nullptr))
  642. {
  643. char* const ret = realpath(lilvpath, nullptr);
  644. lilv_free(lilvpath);
  645. return ret;
  646. }
  647. return nullptr;
  648. }
  649. // fill in `bundles` vector with all known data bundles (main bundle + local presets)
  650. static void _fill_bundles_for_plugin(std::list<std::string>& bundles, const LilvPlugin* const p, LilvNode* const pset_Preset)
  651. {
  652. char* lilvparsed;
  653. const char* bundlepath;
  654. size_t bundlepathsize;
  655. if (const LilvNodes* const datanodes = lilv_plugin_get_data_uris(p))
  656. {
  657. LILV_FOREACH(nodes, itbnds, datanodes)
  658. {
  659. const LilvNode* const bundlenode = lilv_nodes_get(datanodes, itbnds);
  660. if (bundlenode == nullptr)
  661. continue;
  662. if (! lilv_node_is_uri(bundlenode))
  663. continue;
  664. lilvparsed = lilv_file_uri_parse2(lilv_node_as_uri(bundlenode), nullptr);
  665. if (lilvparsed == nullptr)
  666. continue;
  667. bundlepath = dirname(lilvparsed);
  668. if (bundlepath == nullptr || strcmp(bundlepath, ".") == 0)
  669. {
  670. lilv_free(lilvparsed);
  671. continue;
  672. }
  673. bundlepath = _get_safe_bundlepath(bundlepath, bundlepathsize);
  674. lilv_free(lilvparsed);
  675. if (bundlepath == nullptr)
  676. continue;
  677. const std::string bundlestr = bundlepath;
  678. if (std::find(bundles.begin(), bundles.end(), bundlestr) == bundles.end())
  679. bundles.push_back(bundlestr);
  680. }
  681. }
  682. if (LilvNodes* const presetnodes = lilv_plugin_get_related(p, pset_Preset))
  683. {
  684. LILV_FOREACH(nodes, itprs, presetnodes)
  685. {
  686. const LilvNode* const presetnode = lilv_nodes_get(presetnodes, itprs);
  687. if (presetnode == nullptr)
  688. continue;
  689. if (! lilv_node_is_uri(presetnode))
  690. continue;
  691. lilvparsed = lilv_file_uri_parse2(lilv_node_as_uri(presetnode), nullptr);
  692. if (lilvparsed == nullptr)
  693. continue;
  694. bundlepath = dirname(lilvparsed);
  695. if (bundlepath == nullptr || strcmp(bundlepath, ".") == 0)
  696. {
  697. lilv_free(lilvparsed);
  698. continue;
  699. }
  700. bundlepath = _get_safe_bundlepath(bundlepath, bundlepathsize);
  701. lilv_free(lilvparsed);
  702. if (bundlepath == nullptr)
  703. continue;
  704. const std::string bundlestr = bundlepath;
  705. if (std::find(bundles.begin(), bundles.end(), bundlestr) == bundles.end())
  706. bundles.push_back(bundlestr);
  707. }
  708. lilv_nodes_free(presetnodes);
  709. }
  710. }
  711. // used below
  712. static void _fill_units(LilvWorld* const w, PluginPortUnits& portunits, LilvNode* const uunit, const NamespaceDefinitions& ns);
  713. // fill in parameter data, defined as function because writable/readable code is pretty much the same
  714. static void _fill_parameters_for_plugin(LilvWorld* const w,
  715. const LilvPlugin* const p,
  716. const NamespaceDefinitions& ns,
  717. std::map<std::string, PluginParameter>& usedParameters,
  718. const bool writable)
  719. {
  720. if (LilvNodes* const patches = lilv_plugin_get_value(p, writable ? ns.patch_writable : ns.patch_readable))
  721. {
  722. if (lilv_nodes_size(patches) != 0)
  723. {
  724. // used to fetch default values
  725. LilvNode* const statenode = lilv_world_get(w, lilv_plugin_get_uri(p), ns.state_state, nullptr);
  726. LILV_FOREACH(nodes, itpatches, patches)
  727. {
  728. const LilvNode* const patch = lilv_nodes_get(patches, itpatches);
  729. const char* const patch_uri = lilv_node_as_uri(patch);
  730. if (contains(usedParameters, patch_uri))
  731. {
  732. PluginParameter& param(usedParameters[patch_uri]);
  733. if (writable)
  734. param.writable = true;
  735. else
  736. param.readable = true;
  737. continue;
  738. }
  739. LilvNode* const typeNode = lilv_world_get(w, patch, ns.rdf_type, nullptr);
  740. if (typeNode == nullptr)
  741. continue;
  742. if (strcmp(lilv_node_as_uri(typeNode), LV2_CORE__Parameter) != 0)
  743. {
  744. lilv_node_free(typeNode);
  745. continue;
  746. }
  747. LilvNode* const rangeNode = lilv_world_get(w, patch, ns.rdfs_range, nullptr);
  748. if (rangeNode == nullptr)
  749. {
  750. lilv_node_free(typeNode);
  751. continue;
  752. }
  753. LilvNode* const labelNode = lilv_world_get(w, patch, ns.rdfs_label, nullptr);
  754. if (labelNode == nullptr)
  755. {
  756. lilv_node_free(rangeNode);
  757. lilv_node_free(typeNode);
  758. continue;
  759. }
  760. PluginParameter param;
  761. memset(&param, 0, sizeof(PluginParameter));
  762. param.valid = true;
  763. param.uri = strdup(patch_uri);
  764. param.label = strdup(lilv_node_as_string(labelNode));
  765. param.type = strdup(lilv_node_as_string(rangeNode));
  766. if (writable)
  767. param.writable = true;
  768. else
  769. param.readable = true;
  770. const char* const atomType = (strncmp(param.type, LV2_ATOM_PREFIX, strlen(LV2_ATOM_PREFIX)) == 0)
  771. ? param.type + strlen(LV2_ATOM_PREFIX)
  772. : "";
  773. // ----------------------------------------------------------------------------------------------------
  774. // ranges
  775. if (strcmp(atomType, "Path") == 0 || strcmp(atomType, "String") == 0 || strcmp(atomType, "URI") == 0)
  776. {
  777. param.ranges.type = 's';
  778. if (LilvNode* const keynode = lilv_new_uri(w, param.uri))
  779. {
  780. if (LilvNode* const valuenode = lilv_world_get(w, statenode, keynode, nullptr))
  781. {
  782. if (strcmp(atomType, "Path") == 0)
  783. param.ranges.s = lilv_file_abspath(lilv_node_as_string(valuenode));
  784. else if (strcmp(atomType, "URI") == 0)
  785. param.ranges.s = strdup(lilv_node_as_uri(valuenode));
  786. else
  787. param.ranges.s = strdup(lilv_node_as_string(valuenode));
  788. lilv_node_free(valuenode);
  789. }
  790. lilv_node_free(keynode);
  791. }
  792. }
  793. else
  794. {
  795. LilvNode* xminimum = lilv_world_get(w, patch, ns.mod_minimum, nullptr);
  796. if (xminimum == nullptr)
  797. xminimum = lilv_world_get(w, patch, ns.lv2core_minimum, nullptr);
  798. LilvNode* xmaximum = lilv_world_get(w, patch, ns.mod_maximum, nullptr);
  799. if (xmaximum == nullptr)
  800. xmaximum = lilv_world_get(w, patch, ns.lv2core_maximum, nullptr);
  801. LilvNode* xdefault = ns.mod_default_custom != nullptr
  802. ? lilv_world_get(w, patch, ns.mod_default_custom, nullptr)
  803. : nullptr;
  804. if (xdefault == nullptr)
  805. xdefault = lilv_world_get(w, patch, ns.mod_default, nullptr);
  806. if (xdefault == nullptr)
  807. xdefault = lilv_world_get(w, patch, ns.lv2core_default, nullptr);
  808. if (xminimum != nullptr && xmaximum != nullptr)
  809. {
  810. const bool isLong = strcmp(atomType, "Long") == 0;
  811. if (isLong)
  812. {
  813. param.ranges.type = 'l';
  814. param.ranges.l.min = atoll(lilv_node_as_string(xminimum));
  815. param.ranges.l.max = atoll(lilv_node_as_string(xmaximum));
  816. if (param.ranges.l.min >= param.ranges.l.max)
  817. param.ranges.l.max = param.ranges.l.min + 1;
  818. }
  819. else
  820. {
  821. param.ranges.type = 'f';
  822. param.ranges.f.min = lilv_node_as_float(xminimum);
  823. param.ranges.f.max = lilv_node_as_float(xmaximum);
  824. if (param.ranges.f.min >= param.ranges.f.max)
  825. param.ranges.f.max = param.ranges.f.min + 1.0f;
  826. }
  827. if (xdefault != nullptr)
  828. {
  829. if (isLong)
  830. param.ranges.l.def = atoll(lilv_node_as_string(xdefault));
  831. else
  832. param.ranges.f.def = lilv_node_as_float(xdefault);
  833. }
  834. else
  835. {
  836. if (isLong)
  837. param.ranges.l.def = param.ranges.l.min;
  838. else
  839. param.ranges.f.def = param.ranges.f.min;
  840. }
  841. }
  842. else if (strcmp(atomType, "Bool") == 0)
  843. {
  844. param.ranges.type = 'f';
  845. param.ranges.f.min = 0.0f;
  846. param.ranges.f.max = 1.0f;
  847. if (xdefault != nullptr)
  848. param.ranges.f.def = std::min(1.0f, std::max(0.0f, lilv_node_as_float(xdefault)));
  849. else
  850. param.ranges.f.def = param.ranges.f.min;
  851. }
  852. lilv_node_free(xminimum);
  853. lilv_node_free(xmaximum);
  854. lilv_node_free(xdefault);
  855. }
  856. // ----------------------------------------------------------------------------------------------------
  857. // units
  858. param.units.label = nc;
  859. param.units.render = nc;
  860. param.units.symbol = nc;
  861. if (LilvNode* const uunit = lilv_world_get(w, patch, ns.units_unit, nullptr))
  862. {
  863. _fill_units(w, param.units, uunit, ns);
  864. lilv_node_free(uunit);
  865. }
  866. // ----------------------------------------------------------------------------------------------------
  867. // comment
  868. if (LilvNode* const node = lilv_world_get(w, patch, ns.rdfs_comment, nullptr))
  869. {
  870. param.comment = strdup(lilv_node_as_string(node));
  871. lilv_node_free(node);
  872. }
  873. else
  874. {
  875. param.comment = nc;
  876. }
  877. // ----------------------------------------------------------------------------------------------------
  878. // short name
  879. if (LilvNode* const node = lilv_world_get(w, patch, ns.lv2core_shortName, nullptr))
  880. {
  881. param.shortName = strdup(lilv_node_as_string(node));
  882. lilv_node_free(node);
  883. }
  884. else
  885. {
  886. param.shortName = strdup(param.label);
  887. }
  888. if (strlen(param.shortName) > 16)
  889. ((char*)param.shortName)[16] = '\0';
  890. // ----------------------------------------------------------------------------------------------------
  891. // file types
  892. if (LilvNode* const fileTypesNode = lilv_world_get(w, patch, ns.mod_fileTypes, nullptr))
  893. {
  894. if (char* const fileTypes = strdup(lilv_node_as_string(fileTypesNode)))
  895. {
  896. const size_t fileTypesLen = strlen(fileTypes);
  897. uint fileTypesCount = 1;
  898. // count number of items
  899. for (size_t i=0; i<fileTypesLen; ++i)
  900. {
  901. if (fileTypes[i] == ',')
  902. ++fileTypesCount;
  903. }
  904. const char** const fileTypesArray = new const char*[fileTypesCount+1U];
  905. memset(fileTypesArray, 0, sizeof(const char*)*(fileTypesCount+1U));
  906. // assign data, reusing fileTypes string pointer
  907. fileTypesCount = 0;
  908. fileTypesArray[0] = fileTypes;
  909. for (size_t i=0; i<fileTypesLen; ++i)
  910. {
  911. if (fileTypes[i] == ',')
  912. {
  913. fileTypes[i] = '\0';
  914. fileTypesArray[++fileTypesCount] = &fileTypes[i+1];
  915. }
  916. }
  917. param.fileTypes = fileTypesArray;
  918. }
  919. lilv_node_free(fileTypesNode);
  920. }
  921. // ----------------------------------------------------------------------------------------------------
  922. // supported extensions
  923. if (LilvNode* const supportedExtensionsNode = lilv_world_get(w, patch, ns.mod_supportedExtensions, nullptr))
  924. {
  925. if (char* const supportedExtensions = strdup(lilv_node_as_string(supportedExtensionsNode)))
  926. {
  927. const size_t supportedExtensionsLen = strlen(supportedExtensions);
  928. uint supportedExtensionsCount = 1;
  929. // count number of items
  930. for (size_t i=0; i<supportedExtensionsLen; ++i)
  931. {
  932. if (supportedExtensions[i] == ',')
  933. ++supportedExtensionsCount;
  934. }
  935. const char** const supportedExtensionsArray = new const char*[supportedExtensionsCount+1U];
  936. memset(supportedExtensionsArray, 0, sizeof(const char*)*(supportedExtensionsCount+1U));
  937. // assign data, reusing supportedExtensions string pointer
  938. supportedExtensionsCount = 0;
  939. supportedExtensionsArray[0] = supportedExtensions;
  940. for (size_t i=0; i<supportedExtensionsLen; ++i)
  941. {
  942. if (supportedExtensions[i] == ',')
  943. {
  944. supportedExtensions[i] = '\0';
  945. supportedExtensionsArray[++supportedExtensionsCount] = &supportedExtensions[i+1];
  946. }
  947. }
  948. param.supportedExtensions = supportedExtensionsArray;
  949. }
  950. lilv_node_free(supportedExtensionsNode);
  951. }
  952. lilv_node_free(labelNode);
  953. lilv_node_free(rangeNode);
  954. lilv_node_free(typeNode);
  955. usedParameters[param.uri] = param;
  956. }
  957. lilv_node_free(statenode);
  958. }
  959. lilv_nodes_free(patches);
  960. }
  961. }
  962. // refresh everything
  963. // plugins are not truly scanned here, only later per request
  964. void _refresh()
  965. {
  966. LilvWorld* const w = W;
  967. BUNDLES.clear();
  968. PLUGNFO.clear();
  969. PLUGNFO_Mini.clear();
  970. PLUGINS = lilv_world_get_all_plugins(w);
  971. LilvNode* const pset_Preset = lilv_new_uri(w, LV2_PRESETS__Preset);
  972. // Make a list of all installed bundles
  973. LILV_FOREACH(plugins, itpls, PLUGINS)
  974. {
  975. const LilvPlugin* const p = lilv_plugins_get(PLUGINS, itpls);
  976. const std::string uri = lilv_node_as_uri(lilv_plugin_get_uri(p));
  977. // store empty dict for later
  978. PLUGNFO[uri] = PluginInfo_Init;
  979. PLUGNFO_Mini[uri] = nullptr;
  980. _fill_bundles_for_plugin(BUNDLES, p, pset_Preset);
  981. }
  982. lilv_node_free(pset_Preset);
  983. }
  984. // common function used in 2 places
  985. static void _place_preset_info(LilvWorld* const w,
  986. PluginInfo& info,
  987. const LilvPlugin* const p,
  988. LilvNode* const pset_Preset,
  989. LilvNode* const rdfs_label)
  990. {
  991. LilvNodes* const presetnodes = lilv_plugin_get_related(p, pset_Preset);
  992. if (presetnodes == nullptr)
  993. return;
  994. const unsigned int presetcount = lilv_nodes_size(presetnodes);
  995. unsigned int prindex = 0;
  996. PluginPreset* const presets = new PluginPreset[presetcount+1];
  997. memset(presets, 0, sizeof(PluginPreset) * (presetcount+1));
  998. char lastSeenBundle[0xff];
  999. lastSeenBundle[0] = lastSeenBundle[0xff-1] = '\0';
  1000. const char* const mainBundle(info.bundles[0]);
  1001. std::vector<const LilvNode*> loadedPresetResourceNodes;
  1002. LILV_FOREACH(nodes, itprs, presetnodes)
  1003. {
  1004. if (prindex >= presetcount)
  1005. continue;
  1006. const LilvNode* const presetnode = lilv_nodes_get(presetnodes, itprs);
  1007. // try to find label without loading the preset resource first
  1008. LilvNode* xlabel = lilv_world_get(w, presetnode, rdfs_label, nullptr);
  1009. // failed, try loading resource
  1010. if (xlabel == nullptr)
  1011. {
  1012. // if loading resource fails, skip this preset
  1013. if (lilv_world_load_resource(w, presetnode) == -1)
  1014. continue;
  1015. // ok, let's try again
  1016. xlabel = lilv_world_get(w, presetnode, rdfs_label, nullptr);
  1017. // need to unload later
  1018. loadedPresetResourceNodes.push_back(presetnode);
  1019. }
  1020. if (xlabel != nullptr)
  1021. {
  1022. const char* const preseturi = lilv_node_as_uri(presetnode);
  1023. const char* presetpath = nc;
  1024. // check if URI is a local file, to see if it's a user preset
  1025. if (strncmp(preseturi, "file://", 7) == 0)
  1026. {
  1027. if (char* const lilvparsed = lilv_file_uri_parse2(preseturi, nullptr))
  1028. {
  1029. if (const char* bundlepath = dirname(lilvparsed))
  1030. {
  1031. // cache and compare last seen bundle
  1032. // bundles with more than 1 preset are not considered 'user' (ie, modifiable)
  1033. if (lastSeenBundle[0] != '\0' && strcmp(bundlepath, lastSeenBundle) == 0)
  1034. {
  1035. // invalidate previous one
  1036. if (presets[prindex-1].path != nc)
  1037. {
  1038. free((void*)presets[prindex-1].path);
  1039. presets[prindex-1].path = nc;
  1040. }
  1041. }
  1042. else
  1043. {
  1044. strncpy(lastSeenBundle, bundlepath, 0xff-1);
  1045. size_t bundlepathsize;
  1046. bundlepath = _get_safe_bundlepath(bundlepath, bundlepathsize);
  1047. if (strcmp(mainBundle, bundlepath) != 0)
  1048. presetpath = strdup(bundlepath);
  1049. }
  1050. }
  1051. lilv_free(lilvparsed);
  1052. }
  1053. }
  1054. presets[prindex++] = {
  1055. true,
  1056. strdup(preseturi),
  1057. strdup(lilv_node_as_string(xlabel)),
  1058. presetpath
  1059. };
  1060. lilv_node_free(xlabel);
  1061. }
  1062. }
  1063. if (prindex > 1)
  1064. _sort_presets_data(presets, prindex);
  1065. #ifdef HAVE_NEW_LILV
  1066. for (const LilvNode* presetnode : loadedPresetResourceNodes)
  1067. lilv_world_unload_resource(w, presetnode);
  1068. #endif
  1069. info.presets = presets;
  1070. loadedPresetResourceNodes.clear();
  1071. lilv_nodes_free(presetnodes);
  1072. }
  1073. static const char* const* _get_plugin_categories(const LilvPlugin* const p,
  1074. LilvNode* const rdf_type,
  1075. bool* const supported = nullptr)
  1076. {
  1077. const char* const* category = nullptr;
  1078. if (LilvNodes* const nodes = lilv_plugin_get_value(p, rdf_type))
  1079. {
  1080. LILV_FOREACH(nodes, it, nodes)
  1081. {
  1082. const LilvNode* const node2 = lilv_nodes_get(nodes, it);
  1083. const char* const nodestr = lilv_node_as_string(node2);
  1084. if (nodestr == nullptr)
  1085. continue;
  1086. if (strcmp(nodestr, LILV_NS_MODPEDAL "Pedalboard") == 0)
  1087. {
  1088. if (supported != nullptr)
  1089. *supported = false;
  1090. category = nullptr;
  1091. break;
  1092. }
  1093. if (const char* cat = strstr(nodestr, "http://lv2plug.in/ns/lv2core#"))
  1094. {
  1095. cat += 29; // strlen("http://lv2plug.in/ns/lv2core#")
  1096. if (cat[0] == '\0')
  1097. continue;
  1098. if (strcmp(cat, "Plugin") == 0)
  1099. continue;
  1100. else if (strcmp(cat, "DelayPlugin") == 0)
  1101. category = kCategoryDelayPlugin;
  1102. else if (strcmp(cat, "DistortionPlugin") == 0)
  1103. category = kCategoryDistortionPlugin;
  1104. else if (strcmp(cat, "WaveshaperPlugin") == 0)
  1105. category = kCategoryWaveshaperPlugin;
  1106. else if (strcmp(cat, "DynamicsPlugin") == 0)
  1107. category = kCategoryDynamicsPlugin;
  1108. else if (strcmp(cat, "AmplifierPlugin") == 0)
  1109. category = kCategoryAmplifierPlugin;
  1110. else if (strcmp(cat, "CompressorPlugin") == 0)
  1111. category = kCategoryCompressorPlugin;
  1112. else if (strcmp(cat, "ExpanderPlugin") == 0)
  1113. category = kCategoryExpanderPlugin;
  1114. else if (strcmp(cat, "GatePlugin") == 0)
  1115. category = kCategoryGatePlugin;
  1116. else if (strcmp(cat, "LimiterPlugin") == 0)
  1117. category = kCategoryLimiterPlugin;
  1118. else if (strcmp(cat, "FilterPlugin") == 0)
  1119. category = kCategoryFilterPlugin;
  1120. else if (strcmp(cat, "AllpassPlugin") == 0)
  1121. category = kCategoryAllpassPlugin;
  1122. else if (strcmp(cat, "BandpassPlugin") == 0)
  1123. category = kCategoryBandpassPlugin;
  1124. else if (strcmp(cat, "CombPlugin") == 0)
  1125. category = kCategoryCombPlugin;
  1126. else if (strcmp(cat, "EQPlugin") == 0)
  1127. category = kCategoryEQPlugin;
  1128. else if (strcmp(cat, "MultiEQPlugin") == 0)
  1129. category = kCategoryMultiEQPlugin;
  1130. else if (strcmp(cat, "ParaEQPlugin") == 0)
  1131. category = kCategoryParaEQPlugin;
  1132. else if (strcmp(cat, "HighpassPlugin") == 0)
  1133. category = kCategoryHighpassPlugin;
  1134. else if (strcmp(cat, "LowpassPlugin") == 0)
  1135. category = kCategoryLowpassPlugin;
  1136. else if (strcmp(cat, "GeneratorPlugin") == 0)
  1137. category = kCategoryGeneratorPlugin;
  1138. else if (strcmp(cat, "ConstantPlugin") == 0)
  1139. category = kCategoryConstantPlugin;
  1140. else if (strcmp(cat, "InstrumentPlugin") == 0)
  1141. category = kCategoryInstrumentPlugin;
  1142. else if (strcmp(cat, "OscillatorPlugin") == 0)
  1143. category = kCategoryOscillatorPlugin;
  1144. else if (strcmp(cat, "ModulatorPlugin") == 0)
  1145. category = kCategoryModulatorPlugin;
  1146. else if (strcmp(cat, "ChorusPlugin") == 0)
  1147. category = kCategoryChorusPlugin;
  1148. else if (strcmp(cat, "FlangerPlugin") == 0)
  1149. category = kCategoryFlangerPlugin;
  1150. else if (strcmp(cat, "PhaserPlugin") == 0)
  1151. category = kCategoryPhaserPlugin;
  1152. else if (strcmp(cat, "ReverbPlugin") == 0)
  1153. category = kCategoryReverbPlugin;
  1154. else if (strcmp(cat, "SimulatorPlugin") == 0)
  1155. category = kCategorySimulatorPlugin;
  1156. else if (strcmp(cat, "SpatialPlugin") == 0)
  1157. category = kCategorySpatialPlugin;
  1158. else if (strcmp(cat, "SpectralPlugin") == 0)
  1159. category = kCategorySpectralPlugin;
  1160. else if (strcmp(cat, "PitchPlugin") == 0)
  1161. category = kCategoryPitchPlugin;
  1162. else if (strcmp(cat, "UtilityPlugin") == 0)
  1163. category = kCategoryUtilityPlugin;
  1164. else if (strcmp(cat, "AnalyserPlugin") == 0)
  1165. category = kCategoryAnalyserPlugin;
  1166. else if (strcmp(cat, "ConverterPlugin") == 0)
  1167. category = kCategoryConverterPlugin;
  1168. else if (strcmp(cat, "FunctionPlugin") == 0)
  1169. category = kCategoryFunctionPlugin;
  1170. else if (strcmp(cat, "MixerPlugin") == 0)
  1171. category = kCategoryMixerPlugin;
  1172. else if (strcmp(cat, "MIDIPlugin") == 0)
  1173. category = kCategoryMIDIPlugin;
  1174. }
  1175. else if (const char* cat2 = strstr(nodestr, LILV_NS_MOD))
  1176. {
  1177. cat2 += 29; // strlen("http://moddevices.com/ns/mod#")
  1178. if (cat2[0] == '\0')
  1179. continue;
  1180. else if (strcmp(cat2, "DelayPlugin") == 0)
  1181. category = kCategoryDelayPlugin;
  1182. else if (strcmp(cat2, "DistortionPlugin") == 0)
  1183. category = kCategoryDistortionPlugin;
  1184. else if (strcmp(cat2, "DynamicsPlugin") == 0)
  1185. category = kCategoryDynamicsPlugin;
  1186. else if (strcmp(cat2, "FilterPlugin") == 0)
  1187. category = kCategoryFilterPlugin;
  1188. else if (strcmp(cat2, "GeneratorPlugin") == 0)
  1189. category = kCategoryGeneratorPlugin;
  1190. else if (strcmp(cat2, "ModulatorPlugin") == 0)
  1191. category = kCategoryModulatorPlugin;
  1192. else if (strcmp(cat2, "ReverbPlugin") == 0)
  1193. category = kCategoryReverbPlugin;
  1194. else if (strcmp(cat2, "SimulatorPlugin") == 0)
  1195. category = kCategorySimulatorPlugin;
  1196. else if (strcmp(cat2, "SpatialPlugin") == 0)
  1197. category = kCategorySpatialPlugin;
  1198. else if (strcmp(cat2, "SpectralPlugin") == 0)
  1199. category = kCategorySpectralPlugin;
  1200. else if (strcmp(cat2, "UtilityPlugin") == 0)
  1201. category = kCategoryUtilityPlugin;
  1202. else if (strcmp(cat2, "MIDIPlugin") == 0)
  1203. category = kCategoryMIDIPluginMOD;
  1204. else if (strcmp(cat2, "MaxGenPlugin") == 0)
  1205. category = kCategoryMaxGenPluginMOD;
  1206. else if (strcmp(cat2, "CamomilePlugin") == 0)
  1207. category = kCategoryCamomilePluginMOD;
  1208. else if (strcmp(cat2, "ControlVoltagePlugin") == 0)
  1209. category = kCategoryControlVoltagePluginMOD;
  1210. else
  1211. continue; // invalid mod category
  1212. // if we reach this point we found a mod category.
  1213. // we need to stop now, as only 1 mod category is allowed per plugin.
  1214. break;
  1215. }
  1216. }
  1217. lilv_nodes_free(nodes);
  1218. }
  1219. return category;
  1220. }
  1221. static const char* _get_lv2_pedalboards_path()
  1222. {
  1223. static std::string path;
  1224. if (path.empty())
  1225. {
  1226. if (const char* const pbdir = getenv("MOD_USER_PEDALBOARDS_DIR"))
  1227. path = pbdir;
  1228. else
  1229. path = "~/.pedalboards";
  1230. #ifdef _WIN32
  1231. path += ";";
  1232. #else
  1233. path += ":";
  1234. #endif
  1235. if (FACTORY_PEDALBOARDS_DIR != nullptr)
  1236. {
  1237. path += FACTORY_PEDALBOARDS_DIR;
  1238. }
  1239. else
  1240. {
  1241. path += "/usr/share/mod/pedalboards";
  1242. }
  1243. }
  1244. return path.c_str();
  1245. }
  1246. const PluginInfo_Mini* _get_plugin_info_mini(LilvWorld* const w,
  1247. const LilvPlugin* const p,
  1248. const NamespaceDefinitions& ns)
  1249. {
  1250. // --------------------------------------------------------------------------------------------------------
  1251. // uri
  1252. const char* const uri = lilv_node_as_uri(lilv_plugin_get_uri(p));
  1253. // --------------------------------------------------------------------------------------------------------
  1254. // check if plugin if supported
  1255. bool supported = true;
  1256. for (unsigned int i=0, numports=lilv_plugin_get_num_ports(p); i<numports; ++i)
  1257. {
  1258. const LilvPort* const port = lilv_plugin_get_port_by_index(p, i);
  1259. if (LilvNodes* const typenodes = lilv_port_get_value(p, port, ns.rdf_type))
  1260. {
  1261. bool hasDirection = false;
  1262. bool isGood = false;
  1263. LILV_FOREACH(nodes, it, typenodes)
  1264. {
  1265. const char* const typestr = lilv_node_as_string(lilv_nodes_get(typenodes, it));
  1266. if (typestr == nullptr)
  1267. continue;
  1268. // ignore raw port type
  1269. if (strcmp(typestr, LV2_CORE__Port) == 0)
  1270. continue;
  1271. // direction (input or output) is required
  1272. if (strcmp(typestr, LV2_CORE__InputPort) == 0) {
  1273. hasDirection = true;
  1274. continue;
  1275. }
  1276. if (strcmp(typestr, LV2_CORE__OutputPort) == 0) {
  1277. hasDirection = true;
  1278. continue;
  1279. }
  1280. // ignore morph ports if base type is supported
  1281. if (strcmp(typestr, LV2_MORPH__MorphPort) == 0)
  1282. continue;
  1283. // check base type, must be supported
  1284. if (strcmp(typestr, LV2_CORE__AudioPort) == 0) {
  1285. isGood = true;
  1286. continue;
  1287. }
  1288. if (strcmp(typestr, LV2_CORE__ControlPort) == 0) {
  1289. isGood = true;
  1290. continue;
  1291. }
  1292. if (strcmp(typestr, LV2_CORE__CVPort) == 0) {
  1293. isGood = true;
  1294. continue;
  1295. }
  1296. if (strcmp(typestr, LV2_ATOM__AtomPort) == 0) {
  1297. isGood = true;
  1298. continue;
  1299. }
  1300. if (strcmp(typestr, MOD__CVPort) == 0) {
  1301. isGood = true;
  1302. continue;
  1303. }
  1304. }
  1305. lilv_nodes_free(typenodes);
  1306. if (! (hasDirection && isGood))
  1307. supported = false;
  1308. }
  1309. }
  1310. if (! supported)
  1311. {
  1312. printf("Plugin '%s' uses non-supported port types\n", uri);
  1313. return nullptr;
  1314. }
  1315. // --------------------------------------------------------------------------------------------------------
  1316. // category
  1317. const char* const* const category = _get_plugin_categories(p, ns.rdf_type, &supported);
  1318. if (! supported)
  1319. return nullptr;
  1320. // --------------------------------------------------------------------------------------------------------
  1321. PluginInfo_Mini* const info = new PluginInfo_Mini;
  1322. info->uri = uri;
  1323. info->category = category;
  1324. // --------------------------------------------------------------------------------------------------------
  1325. // name
  1326. if (LilvNode* const node = lilv_plugin_get_name(p))
  1327. {
  1328. if (const char* const name = lilv_node_as_string(node))
  1329. info->name = strdup(name);
  1330. else
  1331. info->name = nc;
  1332. lilv_node_free(node);
  1333. }
  1334. else
  1335. {
  1336. info->name = nc;
  1337. }
  1338. // --------------------------------------------------------------------------------------------------------
  1339. // brand
  1340. char brand[16+1] = {};
  1341. if (LilvNodes* const nodes = lilv_plugin_get_value(p, ns.mod_brand))
  1342. {
  1343. strncpy(brand, lilv_node_as_string(lilv_nodes_get_first(nodes)), 16);
  1344. info->brand = strdup(brand);
  1345. lilv_nodes_free(nodes);
  1346. }
  1347. else if (LilvNode* const node = lilv_plugin_get_author_name(p))
  1348. {
  1349. strncpy(brand, lilv_node_as_string(node), 16);
  1350. info->brand = strdup(brand);
  1351. lilv_node_free(node);
  1352. }
  1353. else
  1354. {
  1355. info->brand = nc;
  1356. }
  1357. // --------------------------------------------------------------------------------------------------------
  1358. // label
  1359. char label[24+1] = {};
  1360. if (LilvNodes* const nodes = lilv_plugin_get_value(p, ns.mod_label))
  1361. {
  1362. strncpy(label, lilv_node_as_string(lilv_nodes_get_first(nodes)), 24);
  1363. info->label = strdup(label);
  1364. lilv_nodes_free(nodes);
  1365. }
  1366. else if (info->name != nc)
  1367. {
  1368. if (strlen(info->name) <= 24)
  1369. {
  1370. info->label = strdup(info->name);
  1371. }
  1372. else
  1373. {
  1374. strncpy(label, info->name, 24);
  1375. info->label = strdup(label);
  1376. }
  1377. }
  1378. else
  1379. {
  1380. info->label = nc;
  1381. }
  1382. // --------------------------------------------------------------------------------------------------------
  1383. // comment
  1384. if (LilvNodes* const nodes = lilv_plugin_get_value(p, ns.rdfs_comment))
  1385. {
  1386. info->comment = strdup(lilv_node_as_string(lilv_nodes_get_first(nodes)));
  1387. lilv_nodes_free(nodes);
  1388. }
  1389. else
  1390. {
  1391. info->comment = nc;
  1392. }
  1393. // --------------------------------------------------------------------------------------------------------
  1394. // version
  1395. if (LilvNodes* const minorvers = lilv_plugin_get_value(p, ns.lv2core_minorVersion))
  1396. {
  1397. info->minorVersion = lilv_node_as_int(lilv_nodes_get_first(minorvers));
  1398. lilv_nodes_free(minorvers);
  1399. }
  1400. else
  1401. {
  1402. info->minorVersion = 0;
  1403. }
  1404. if (LilvNodes* const microvers = lilv_plugin_get_value(p, ns.lv2core_microVersion))
  1405. {
  1406. info->microVersion = lilv_node_as_int(lilv_nodes_get_first(microvers));
  1407. lilv_nodes_free(microvers);
  1408. }
  1409. else
  1410. {
  1411. info->microVersion = 0;
  1412. }
  1413. if (LilvNodes* const releasenode = lilv_plugin_get_value(p, ns.mod_release))
  1414. {
  1415. info->release = lilv_node_as_int(lilv_nodes_get_first(releasenode));
  1416. lilv_nodes_free(releasenode);
  1417. }
  1418. else
  1419. {
  1420. info->release = 0;
  1421. }
  1422. if (LilvNodes* const buildernode = lilv_plugin_get_value(p, ns.mod_builder))
  1423. {
  1424. info->builder = lilv_node_as_int(lilv_nodes_get_first(buildernode));
  1425. lilv_nodes_free(buildernode);
  1426. }
  1427. else
  1428. {
  1429. info->builder = 0;
  1430. }
  1431. // --------------------------------------------------------------------------------------------------------
  1432. // build environment
  1433. if (LilvNodes* const nodes = lilv_plugin_get_value(p, ns.mod_buildEnvironment))
  1434. {
  1435. const char* const buildEnvironment = lilv_node_as_string(lilv_nodes_get_first(nodes));
  1436. /**/ if (strcmp(buildEnvironment, "prod") == 0)
  1437. info->buildEnvironment = kBuildEnvironmentProd;
  1438. else if (strcmp(buildEnvironment, "dev") == 0)
  1439. info->buildEnvironment = kBuildEnvironmentDev;
  1440. else if (strcmp(buildEnvironment, "labs") == 0)
  1441. info->buildEnvironment = kBuildEnvironmentLabs;
  1442. else
  1443. info->buildEnvironment = strdup(buildEnvironment);
  1444. lilv_nodes_free(nodes);
  1445. }
  1446. // if cloud built the plugin, but no buildEnvironment was set, switch to prod for backwards compat
  1447. else if (info->release >= 1 && info->builder >= 1)
  1448. {
  1449. info->buildEnvironment = kBuildEnvironmentProd;
  1450. }
  1451. else
  1452. {
  1453. info->buildEnvironment = nc;
  1454. }
  1455. // --------------------------------------------------------------------------------------------------------
  1456. // licensed
  1457. if (KEYS_PATHlen > 0 && lilv_plugin_has_extension_data(p, ns.modlicense_interface))
  1458. {
  1459. const std::string licensefile(KEYS_PATH + sha1(info->uri));
  1460. info->licensed = std::ifstream(licensefile).good() ? kPluginLicensePaid : kPluginLicenseTrial;
  1461. }
  1462. else
  1463. {
  1464. info->licensed = kPluginLicenseNonCommercial;
  1465. }
  1466. // --------------------------------------------------------------------------------------------------------
  1467. // iotype
  1468. if (const uint32_t count = lilv_plugin_get_num_ports(p))
  1469. {
  1470. uint32_t countAudioInput=0, countAudioOutput=0;
  1471. uint32_t countMidiInput=0, countMidiOutput=0;
  1472. for (uint32_t i=0; i<count; ++i)
  1473. {
  1474. const LilvPort* const port = lilv_plugin_get_port_by_index(p, i);
  1475. PortDirection direction = kPortDirectionNull;
  1476. PortType type = kPortTypeNull;
  1477. if (LilvNodes* const nodes = lilv_port_get_value(p, port, ns.rdf_type))
  1478. {
  1479. LILV_FOREACH(nodes, it, nodes)
  1480. {
  1481. const LilvNode* const node2 = lilv_nodes_get(nodes, it);
  1482. const char* const nodestr = lilv_node_as_string(node2);
  1483. if (nodestr == nullptr)
  1484. continue;
  1485. else if (strcmp(nodestr, LV2_CORE__InputPort) == 0)
  1486. direction = kPortDirectionInput;
  1487. else if (strcmp(nodestr, LV2_CORE__OutputPort) == 0)
  1488. direction = kPortDirectionOutput;
  1489. else if (strcmp(nodestr, LV2_CORE__AudioPort) == 0)
  1490. type = kPortTypeAudio;
  1491. else if (strcmp(nodestr, LV2_ATOM__AtomPort) == 0 && lilv_port_supports_event(p, port, ns.midi_MidiEvent))
  1492. {
  1493. if (LilvNodes* const nodes2 = lilv_port_get_value(p, port, ns.atom_bufferType))
  1494. {
  1495. if (lilv_node_equals(lilv_nodes_get_first(nodes2), ns.atom_Sequence))
  1496. if (! lilv_port_has_property(p, port, ns.mod_rawMIDIClockAccess))
  1497. type = kPortTypeMIDI;
  1498. lilv_nodes_free(nodes2);
  1499. }
  1500. }
  1501. }
  1502. lilv_nodes_free(nodes);
  1503. }
  1504. if (direction == kPortDirectionNull || type == kPortTypeNull)
  1505. continue;
  1506. switch (type)
  1507. {
  1508. case kPortTypeAudio:
  1509. if (direction == kPortDirectionOutput)
  1510. ++countAudioOutput;
  1511. else
  1512. ++countAudioInput;
  1513. break;
  1514. case kPortTypeMIDI:
  1515. if (direction == kPortDirectionOutput)
  1516. ++countMidiOutput;
  1517. else
  1518. ++countMidiInput;
  1519. break;
  1520. default:
  1521. break;
  1522. }
  1523. }
  1524. fill_iotype(info, countAudioInput, countAudioOutput, countMidiInput, countMidiOutput);
  1525. }
  1526. else
  1527. {
  1528. info->iotype = kPluginIONull;
  1529. }
  1530. // --------------------------------------------------------------------------------------------------------
  1531. // gui
  1532. LilvNode* modguigui = nullptr;
  1533. char* resdir = nullptr;
  1534. if (LilvNodes* const nodes = lilv_plugin_get_value(p, ns.modgui_gui))
  1535. {
  1536. LILV_FOREACH(nodes, it, nodes)
  1537. {
  1538. const LilvNode* const mgui = lilv_nodes_get(nodes, it);
  1539. LilvNode* const resdirn = lilv_world_get(w, mgui, ns.modgui_resourcesDirectory, nullptr);
  1540. if (resdirn == nullptr)
  1541. continue;
  1542. free(resdir);
  1543. resdir = lilv_file_abspath(lilv_node_as_string(resdirn));
  1544. lilv_node_free(modguigui);
  1545. lilv_node_free(resdirn);
  1546. if (resdir == nullptr)
  1547. {
  1548. modguigui = nullptr;
  1549. continue;
  1550. }
  1551. modguigui = lilv_node_duplicate(mgui);
  1552. if (strncmp(resdir, HOME, HOMElen) == 0)
  1553. // found a modgui in the home dir, stop here and use it
  1554. break;
  1555. }
  1556. lilv_nodes_free(nodes);
  1557. }
  1558. if (modguigui != nullptr && resdir != nullptr)
  1559. {
  1560. info->gui.resourcesDirectory = resdir;
  1561. if (LilvNode* const modgui_scrn = lilv_world_get(w, modguigui, ns.modgui_screenshot, nullptr))
  1562. {
  1563. info->gui.screenshot = lilv_file_abspath(lilv_node_as_string(modgui_scrn));
  1564. lilv_node_free(modgui_scrn);
  1565. }
  1566. if (info->gui.screenshot == nullptr)
  1567. info->gui.screenshot = nc;
  1568. if (LilvNode* const modgui_thumb = lilv_world_get(w, modguigui, ns.modgui_thumbnail, nullptr))
  1569. {
  1570. info->gui.thumbnail = lilv_file_abspath(lilv_node_as_string(modgui_thumb));
  1571. lilv_node_free(modgui_thumb);
  1572. }
  1573. if (info->gui.thumbnail == nullptr)
  1574. info->gui.thumbnail = nc;
  1575. lilv_node_free(modguigui);
  1576. }
  1577. else
  1578. {
  1579. info->gui.resourcesDirectory = nc;
  1580. info->gui.screenshot = nc;
  1581. info->gui.thumbnail = nc;
  1582. }
  1583. // --------------------------------------------------------------------------------------------------------
  1584. return info;
  1585. }
  1586. static void _fill_units(LilvWorld* const w,
  1587. PluginPortUnits& portunits,
  1588. LilvNode* const uunit,
  1589. const NamespaceDefinitions& ns)
  1590. {
  1591. const char* uuri = lilv_node_as_uri(uunit);
  1592. // using pre-existing lv2 unit
  1593. if (uuri != nullptr && strncmp(uuri, LV2_UNITS_PREFIX, 38) == 0)
  1594. {
  1595. uuri += 38; // strlen(LV2_UNITS_PREFIX)
  1596. if (_isalnum(uuri))
  1597. {
  1598. const char* const* unittable;
  1599. if (strcmp(uuri, "s") == 0)
  1600. unittable = kUnit_s;
  1601. else if (strcmp(uuri, "ms") == 0)
  1602. unittable = kUnit_ms;
  1603. else if (strcmp(uuri, "min") == 0)
  1604. unittable = kUnit_min;
  1605. else if (strcmp(uuri, "bar") == 0)
  1606. unittable = kUnit_bar;
  1607. else if (strcmp(uuri, "beat") == 0)
  1608. unittable = kUnit_beat;
  1609. else if (strcmp(uuri, "frame") == 0)
  1610. unittable = kUnit_frame;
  1611. else if (strcmp(uuri, "m") == 0)
  1612. unittable = kUnit_m;
  1613. else if (strcmp(uuri, "cm") == 0)
  1614. unittable = kUnit_cm;
  1615. else if (strcmp(uuri, "mm") == 0)
  1616. unittable = kUnit_mm;
  1617. else if (strcmp(uuri, "km") == 0)
  1618. unittable = kUnit_km;
  1619. else if (strcmp(uuri, "inch") == 0)
  1620. unittable = kUnit_inch;
  1621. else if (strcmp(uuri, "mile") == 0)
  1622. unittable = kUnit_mile;
  1623. else if (strcmp(uuri, "db") == 0)
  1624. unittable = kUnit_db;
  1625. else if (strcmp(uuri, "pc") == 0)
  1626. unittable = kUnit_pc;
  1627. else if (strcmp(uuri, "coef") == 0)
  1628. unittable = kUnit_coef;
  1629. else if (strcmp(uuri, "hz") == 0)
  1630. unittable = kUnit_hz;
  1631. else if (strcmp(uuri, "khz") == 0)
  1632. unittable = kUnit_khz;
  1633. else if (strcmp(uuri, "mhz") == 0)
  1634. unittable = kUnit_mhz;
  1635. else if (strcmp(uuri, "bpm") == 0)
  1636. unittable = kUnit_bpm;
  1637. else if (strcmp(uuri, "oct") == 0)
  1638. unittable = kUnit_oct;
  1639. else if (strcmp(uuri, "cent") == 0)
  1640. unittable = kUnit_cent;
  1641. else if (strcmp(uuri, "semitone12TET") == 0)
  1642. unittable = kUnit_semitone12TET;
  1643. else if (strcmp(uuri, "degree") == 0)
  1644. unittable = kUnit_degree;
  1645. else if (strcmp(uuri, "midiNote") == 0)
  1646. unittable = kUnit_midiNote;
  1647. else
  1648. unittable = nullptr;
  1649. if (unittable != nullptr)
  1650. {
  1651. portunits.label = unittable[0];
  1652. portunits.render = unittable[1];
  1653. portunits.symbol = unittable[2];
  1654. }
  1655. }
  1656. }
  1657. // using mod:volts unit
  1658. else if (uuri != nullptr && strcmp(uuri, LILV_NS_MOD "volts") == 0)
  1659. {
  1660. portunits.label = kUnit_volts[0];
  1661. portunits.render = kUnit_volts[1];
  1662. portunits.symbol = kUnit_volts[2];
  1663. }
  1664. // using custom unit
  1665. else
  1666. {
  1667. if (LilvNode* const node = lilv_world_get(w, uunit, ns.rdfs_label, nullptr))
  1668. {
  1669. portunits.label = strdup(lilv_node_as_string(node));
  1670. lilv_node_free(node);
  1671. }
  1672. if (LilvNode* const node = lilv_world_get(w, uunit, ns.units_render, nullptr))
  1673. {
  1674. portunits.render = strdup(lilv_node_as_string(node));
  1675. lilv_node_free(node);
  1676. }
  1677. if (LilvNode* const node = lilv_world_get(w, uunit, ns.units_symbol, nullptr))
  1678. {
  1679. portunits.symbol = strdup(lilv_node_as_string(node));
  1680. lilv_node_free(node);
  1681. }
  1682. portunits._custom = true;
  1683. }
  1684. }
  1685. const PluginInfo& _get_plugin_info(LilvWorld* const w,
  1686. const LilvPlugin* const p,
  1687. const NamespaceDefinitions& ns)
  1688. {
  1689. static PluginInfo info;
  1690. memset(&info, 0, sizeof(PluginInfo));
  1691. const char* const bundleuri = lilv_node_as_uri(lilv_plugin_get_bundle_uri(p));
  1692. const char* const bundle = lilv_file_uri_parse2(bundleuri, nullptr);
  1693. const size_t bundleurilen = strlen(bundleuri);
  1694. // --------------------------------------------------------------------------------------------------------
  1695. // uri
  1696. info.uri = lilv_node_as_uri(lilv_plugin_get_uri(p));
  1697. printf("NOTICE: Now scanning plugin '%s'...\n", info.uri);
  1698. // --------------------------------------------------------------------------------------------------------
  1699. // name
  1700. if (LilvNode* const node = lilv_plugin_get_name(p))
  1701. {
  1702. const char* const name = lilv_node_as_string(node);
  1703. info.name = (name != nullptr) ? strdup(name) : nc;
  1704. lilv_node_free(node);
  1705. }
  1706. else
  1707. {
  1708. info.name = nc;
  1709. }
  1710. // --------------------------------------------------------------------------------------------------------
  1711. // binary
  1712. info.binary = lilv_node_as_string(lilv_plugin_get_library_uri(p));
  1713. if (info.binary != nullptr)
  1714. info.binary = lilv_file_abspath(info.binary);
  1715. if (info.binary == nullptr)
  1716. info.binary = nc;
  1717. // --------------------------------------------------------------------------------------------------------
  1718. // license
  1719. if (LilvNodes* const nodes = lilv_plugin_get_value(p, ns.doap_license))
  1720. {
  1721. const char* license = lilv_node_as_string(lilv_nodes_get_first(nodes));
  1722. if (strncmp(license, bundleuri, bundleurilen) == 0)
  1723. license += bundleurilen;
  1724. info.license = strdup(license);
  1725. lilv_nodes_free(nodes);
  1726. }
  1727. else if (LilvNodes* const nodes2 = lilv_plugin_get_value(p, ns.lv2core_project))
  1728. {
  1729. if (LilvNode* const lcsnode = lilv_world_get(w, lilv_nodes_get_first(nodes2), ns.doap_license, nullptr))
  1730. {
  1731. const char* license = lilv_node_as_string(lcsnode);
  1732. if (strncmp(license, bundleuri, bundleurilen) == 0)
  1733. license += bundleurilen;
  1734. info.license = strdup(license);
  1735. lilv_node_free(lcsnode);
  1736. }
  1737. else
  1738. {
  1739. info.license = nc;
  1740. }
  1741. lilv_nodes_free(nodes2);
  1742. }
  1743. else
  1744. {
  1745. info.license = nc;
  1746. }
  1747. // --------------------------------------------------------------------------------------------------------
  1748. // comment
  1749. if (LilvNodes* const nodes = lilv_plugin_get_value(p, ns.rdfs_comment))
  1750. {
  1751. info.comment = strdup(lilv_node_as_string(lilv_nodes_get_first(nodes)));
  1752. lilv_nodes_free(nodes);
  1753. }
  1754. else
  1755. {
  1756. info.comment = nc;
  1757. }
  1758. // --------------------------------------------------------------------------------------------------------
  1759. // category
  1760. info.category = _get_plugin_categories(p, ns.rdf_type);
  1761. // --------------------------------------------------------------------------------------------------------
  1762. // version
  1763. if (LilvNodes* const minorvers = lilv_plugin_get_value(p, ns.lv2core_minorVersion))
  1764. {
  1765. info.minorVersion = lilv_node_as_int(lilv_nodes_get_first(minorvers));
  1766. lilv_nodes_free(minorvers);
  1767. }
  1768. if (LilvNodes* const microvers = lilv_plugin_get_value(p, ns.lv2core_microVersion))
  1769. {
  1770. info.microVersion = lilv_node_as_int(lilv_nodes_get_first(microvers));
  1771. lilv_nodes_free(microvers);
  1772. }
  1773. if (LilvNodes* const releasenode = lilv_plugin_get_value(p, ns.mod_release))
  1774. {
  1775. info.release = lilv_node_as_int(lilv_nodes_get_first(releasenode));
  1776. lilv_nodes_free(releasenode);
  1777. }
  1778. if (LilvNodes* const buildernode = lilv_plugin_get_value(p, ns.mod_builder))
  1779. {
  1780. info.builder = lilv_node_as_int(lilv_nodes_get_first(buildernode));
  1781. lilv_nodes_free(buildernode);
  1782. }
  1783. {
  1784. char versiontmpstr[32+1] = {};
  1785. snprintf(versiontmpstr, 32, "%d.%d", info.minorVersion, info.microVersion);
  1786. info.version = strdup(versiontmpstr);
  1787. }
  1788. // 0.x is experimental
  1789. if (info.minorVersion == 0)
  1790. info.stability = kStabilityExperimental;
  1791. // odd x.2 or 2.x is testing/development
  1792. else if (info.minorVersion % 2 != 0 || info.microVersion % 2 != 0)
  1793. info.stability = kStabilityTesting;
  1794. // otherwise it's stable
  1795. else
  1796. info.stability = kStabilityStable;
  1797. // --------------------------------------------------------------------------------------------------------
  1798. // build environment
  1799. if (LilvNodes* const nodes = lilv_plugin_get_value(p, ns.mod_buildEnvironment))
  1800. {
  1801. const char* const buildEnvironment = lilv_node_as_string(lilv_nodes_get_first(nodes));
  1802. /**/ if (strcmp(buildEnvironment, "prod") == 0)
  1803. info.buildEnvironment = kBuildEnvironmentProd;
  1804. else if (strcmp(buildEnvironment, "dev") == 0)
  1805. info.buildEnvironment = kBuildEnvironmentDev;
  1806. else if (strcmp(buildEnvironment, "labs") == 0)
  1807. info.buildEnvironment = kBuildEnvironmentLabs;
  1808. else
  1809. info.buildEnvironment = strdup(buildEnvironment);
  1810. lilv_nodes_free(nodes);
  1811. }
  1812. // if cloud built the plugin, but no buildEnvironment was set, switch to prod for backwards compat
  1813. else if (info.release >= 1 && info.builder >= 1)
  1814. {
  1815. info.buildEnvironment = kBuildEnvironmentProd;
  1816. }
  1817. else
  1818. {
  1819. info.buildEnvironment = nc;
  1820. }
  1821. // --------------------------------------------------------------------------------------------------------
  1822. // licensed
  1823. if (KEYS_PATHlen > 0 && lilv_plugin_has_extension_data(p, ns.modlicense_interface))
  1824. {
  1825. const std::string licensefile(KEYS_PATH + sha1(info.uri));
  1826. info.licensed = std::ifstream(licensefile).good() ? kPluginLicensePaid : kPluginLicenseTrial;
  1827. }
  1828. // --------------------------------------------------------------------------------------------------------
  1829. // hasExternalUI
  1830. #ifdef WITH_EXTERNAL_UI_SUPPORT
  1831. if (LilvUIs* const uis = lilv_plugin_get_uis(p))
  1832. {
  1833. LILV_FOREACH(nodes, i, uis)
  1834. {
  1835. const LilvUI* const ui = lilv_uis_get(uis, i);
  1836. const LilvNode* const uinode = lilv_ui_get_uri(ui);
  1837. if (lilv_world_load_resource(W, uinode) == -1)
  1838. continue;
  1839. if (LilvNodes* const uiexts = lilv_world_find_nodes(W, uinode, ns.lv2core_extensionData, nullptr))
  1840. {
  1841. bool hasIdle = false;
  1842. bool hasShow = false;
  1843. LILV_FOREACH(nodes, j, uiexts)
  1844. {
  1845. const LilvNode* const uiext = lilv_nodes_get(uiexts, j);
  1846. const char* const uiexturi = lilv_node_as_uri(uiext);
  1847. /**/ if (strcmp(uiexturi, LV2_UI__idleInterface))
  1848. hasIdle = true;
  1849. else if (strcmp(uiexturi, LV2_UI__showInterface))
  1850. hasShow = true;
  1851. }
  1852. lilv_nodes_free(uiexts);
  1853. if (hasIdle && hasShow)
  1854. info.hasExternalUI = true;
  1855. }
  1856. if (info.hasExternalUI)
  1857. break;
  1858. }
  1859. lilv_uis_free(uis);
  1860. }
  1861. #endif
  1862. // --------------------------------------------------------------------------------------------------------
  1863. // author name
  1864. if (LilvNode* const node = lilv_plugin_get_author_name(p))
  1865. {
  1866. info.author.name = strdup(lilv_node_as_string(node));
  1867. lilv_node_free(node);
  1868. }
  1869. else
  1870. {
  1871. info.author.name = nc;
  1872. }
  1873. // --------------------------------------------------------------------------------------------------------
  1874. // author homepage
  1875. if (LilvNode* const node = lilv_plugin_get_author_homepage(p))
  1876. {
  1877. info.author.homepage = strdup(lilv_node_as_string(node));
  1878. lilv_node_free(node);
  1879. }
  1880. else if (LilvNodes* const nodes2 = lilv_plugin_get_value(p, ns.lv2core_project))
  1881. {
  1882. if (LilvNode* const mntnr = lilv_world_get(w, lilv_nodes_get_first(nodes2), ns.doap_maintainer, nullptr))
  1883. {
  1884. if (LilvNode* const hmpg = lilv_world_get(w, mntnr, ns.foaf_homepage, nullptr))
  1885. {
  1886. info.author.homepage = strdup(lilv_node_as_string(hmpg));
  1887. lilv_node_free(hmpg);
  1888. }
  1889. else
  1890. {
  1891. info.author.homepage = nc;
  1892. }
  1893. lilv_node_free(mntnr);
  1894. }
  1895. else
  1896. {
  1897. info.author.homepage = nc;
  1898. }
  1899. lilv_nodes_free(nodes2);
  1900. }
  1901. else
  1902. {
  1903. info.author.homepage = nc;
  1904. }
  1905. // --------------------------------------------------------------------------------------------------------
  1906. // author email
  1907. if (LilvNode* const node = lilv_plugin_get_author_email(p))
  1908. {
  1909. info.author.email = strdup(lilv_node_as_string(node));
  1910. lilv_node_free(node);
  1911. }
  1912. else
  1913. {
  1914. info.author.email = nc;
  1915. }
  1916. // --------------------------------------------------------------------------------------------------------
  1917. // brand
  1918. if (LilvNodes* const nodes = lilv_plugin_get_value(p, ns.mod_brand))
  1919. {
  1920. char* const brand = strdup(lilv_node_as_string(lilv_nodes_get_first(nodes)));
  1921. /* NOTE: this gives a false positive on valgrind.
  1922. see https://bugzilla.redhat.com/show_bug.cgi?id=678518 */
  1923. if (strlen(brand) > 16)
  1924. brand[16] = '\0';
  1925. info.brand = brand;
  1926. lilv_nodes_free(nodes);
  1927. }
  1928. else if (info.author.name == nc)
  1929. {
  1930. info.brand = nc;
  1931. }
  1932. else
  1933. {
  1934. if (strlen(info.author.name) <= 16)
  1935. {
  1936. info.brand = strdup(info.author.name);
  1937. }
  1938. else
  1939. {
  1940. char brand[16+1] = {};
  1941. strncpy(brand, info.author.name, 16);
  1942. info.brand = strdup(brand);
  1943. }
  1944. }
  1945. // --------------------------------------------------------------------------------------------------------
  1946. // label
  1947. if (LilvNodes* const nodes = lilv_plugin_get_value(p, ns.mod_label))
  1948. {
  1949. char* const label = strdup(lilv_node_as_string(lilv_nodes_get_first(nodes)));
  1950. /* NOTE: this gives a false positive on valgrind.
  1951. see https://bugzilla.redhat.com/show_bug.cgi?id=678518 */
  1952. if (strlen(label) > 24)
  1953. label[24] = '\0';
  1954. info.label = label;
  1955. lilv_nodes_free(nodes);
  1956. }
  1957. else if (info.name == nc)
  1958. {
  1959. info.label = nc;
  1960. }
  1961. else
  1962. {
  1963. if (strlen(info.name) <= 24)
  1964. {
  1965. info.label = strdup(info.name);
  1966. }
  1967. else
  1968. {
  1969. char label[24+1] = {};
  1970. strncpy(label, info.name, 24);
  1971. info.label = strdup(label);
  1972. }
  1973. }
  1974. // --------------------------------------------------------------------------------------------------------
  1975. // bundles
  1976. {
  1977. std::list<std::string> bundles;
  1978. // make sure the main bundle is the first in the list
  1979. size_t bundlepathsize;
  1980. const char* bundlepath = _get_safe_bundlepath(bundle, bundlepathsize);
  1981. if (bundlepath != nullptr)
  1982. {
  1983. const std::string bundlestr = bundlepath;
  1984. bundles.push_back(bundlestr);
  1985. }
  1986. // now add everything else
  1987. _fill_bundles_for_plugin(bundles, p, ns.pset_Preset);
  1988. size_t count = bundles.size();
  1989. const char** const cbundles = new const char*[count+1];
  1990. memset(cbundles, 0, sizeof(const char*) * (count+1));
  1991. count = 0;
  1992. for (const std::string& b : bundles)
  1993. cbundles[count++] = strdup(b.c_str());
  1994. info.bundles = cbundles;
  1995. }
  1996. // --------------------------------------------------------------------------------------------------------
  1997. // gui
  1998. LilvNode* modguigui = nullptr;
  1999. char* resdir = nullptr;
  2000. if (LilvNodes* const nodes = lilv_plugin_get_value(p, ns.modgui_gui))
  2001. {
  2002. LILV_FOREACH(nodes, it, nodes)
  2003. {
  2004. const LilvNode* const mgui = lilv_nodes_get(nodes, it);
  2005. LilvNode* const resdirn = lilv_world_get(w, mgui, ns.modgui_resourcesDirectory, nullptr);
  2006. if (resdirn == nullptr)
  2007. continue;
  2008. free(resdir);
  2009. resdir = lilv_file_abspath(lilv_node_as_string(resdirn));
  2010. lilv_node_free(modguigui);
  2011. lilv_node_free(resdirn);
  2012. if (resdir == nullptr)
  2013. {
  2014. modguigui = nullptr;
  2015. continue;
  2016. }
  2017. modguigui = lilv_node_duplicate(mgui);
  2018. if (strncmp(resdir, HOME, HOMElen) == 0)
  2019. // found a modgui in the home dir, stop here and use it
  2020. break;
  2021. }
  2022. lilv_nodes_free(nodes);
  2023. }
  2024. if (modguigui != nullptr && resdir != nullptr)
  2025. {
  2026. info.gui.resourcesDirectory = resdir;
  2027. // icon and settings templates
  2028. if (LilvNode* const modgui_icon = lilv_world_get(w, modguigui, ns.modgui_iconTemplate, nullptr))
  2029. {
  2030. info.gui.iconTemplate = lilv_file_abspath(lilv_node_as_string(modgui_icon));
  2031. lilv_node_free(modgui_icon);
  2032. }
  2033. if (info.gui.iconTemplate == nullptr)
  2034. info.gui.iconTemplate = nc;
  2035. if (LilvNode* const modgui_setts = lilv_world_get(w, modguigui, ns.modgui_settingsTemplate, nullptr))
  2036. {
  2037. info.gui.settingsTemplate = lilv_file_abspath(lilv_node_as_string(modgui_setts));
  2038. lilv_node_free(modgui_setts);
  2039. }
  2040. if (info.gui.settingsTemplate == nullptr)
  2041. info.gui.settingsTemplate = nc;
  2042. // javascript and stylesheet files
  2043. if (LilvNode* const modgui_script = lilv_world_get(w, modguigui, ns.modgui_javascript, nullptr))
  2044. {
  2045. info.gui.javascript = lilv_file_abspath(lilv_node_as_string(modgui_script));
  2046. lilv_node_free(modgui_script);
  2047. }
  2048. if (info.gui.javascript == nullptr)
  2049. info.gui.javascript = nc;
  2050. if (LilvNode* const modgui_style = lilv_world_get(w, modguigui, ns.modgui_stylesheet, nullptr))
  2051. {
  2052. info.gui.stylesheet = lilv_file_abspath(lilv_node_as_string(modgui_style));
  2053. lilv_node_free(modgui_style);
  2054. }
  2055. if (info.gui.stylesheet == nullptr)
  2056. info.gui.stylesheet = nc;
  2057. // screenshot and thumbnail
  2058. if (LilvNode* const modgui_scrn = lilv_world_get(w, modguigui, ns.modgui_screenshot, nullptr))
  2059. {
  2060. info.gui.screenshot = lilv_file_abspath(lilv_node_as_string(modgui_scrn));
  2061. lilv_node_free(modgui_scrn);
  2062. }
  2063. if (info.gui.screenshot == nullptr)
  2064. info.gui.screenshot = nc;
  2065. if (LilvNode* const modgui_thumb = lilv_world_get(w, modguigui, ns.modgui_thumbnail, nullptr))
  2066. {
  2067. info.gui.thumbnail = lilv_file_abspath(lilv_node_as_string(modgui_thumb));
  2068. lilv_node_free(modgui_thumb);
  2069. }
  2070. if (info.gui.thumbnail == nullptr)
  2071. info.gui.thumbnail = nc;
  2072. // discussionURL, optional
  2073. if (LilvNode* const modgui_discussionURL = lilv_world_get(w, modguigui, ns.modgui_discussionURL, nullptr))
  2074. {
  2075. info.gui.discussionURL = strdup(lilv_node_as_uri(modgui_discussionURL));
  2076. lilv_node_free(modgui_discussionURL);
  2077. }
  2078. if (info.gui.discussionURL == nullptr)
  2079. info.gui.discussionURL = nc;
  2080. // documentation, optional
  2081. if (LilvNode* const modgui_documentation = lilv_world_get(w, modguigui, ns.modgui_documentation, nullptr))
  2082. {
  2083. info.gui.documentation = lilv_file_abspath(lilv_node_as_string(modgui_documentation));
  2084. lilv_node_free(modgui_documentation);
  2085. }
  2086. if (info.gui.documentation == nullptr)
  2087. info.gui.documentation = nc;
  2088. // extra stuff, all optional
  2089. if (LilvNode* const modgui_brand = lilv_world_get(w, modguigui, ns.modgui_brand, nullptr))
  2090. {
  2091. info.gui.brand = strdup(lilv_node_as_string(modgui_brand));
  2092. lilv_node_free(modgui_brand);
  2093. }
  2094. if (info.gui.brand == nullptr)
  2095. info.gui.brand = nc;
  2096. if (LilvNode* const modgui_label = lilv_world_get(w, modguigui, ns.modgui_label, nullptr))
  2097. {
  2098. info.gui.label = strdup(lilv_node_as_string(modgui_label));
  2099. lilv_node_free(modgui_label);
  2100. }
  2101. if (info.gui.label == nullptr)
  2102. info.gui.label = nc;
  2103. if (LilvNode* const modgui_model = lilv_world_get(w, modguigui, ns.modgui_model, nullptr))
  2104. {
  2105. info.gui.model = strdup(lilv_node_as_string(modgui_model));
  2106. lilv_node_free(modgui_model);
  2107. }
  2108. if (info.gui.model == nullptr)
  2109. info.gui.model = nc;
  2110. if (LilvNode* const modgui_panel = lilv_world_get(w, modguigui, ns.modgui_panel, nullptr))
  2111. {
  2112. info.gui.panel = strdup(lilv_node_as_string(modgui_panel));
  2113. lilv_node_free(modgui_panel);
  2114. }
  2115. if (info.gui.panel == nullptr)
  2116. info.gui.panel = nc;
  2117. if (LilvNode* const modgui_color = lilv_world_get(w, modguigui, ns.modgui_color, nullptr))
  2118. {
  2119. info.gui.color = strdup(lilv_node_as_string(modgui_color));
  2120. lilv_node_free(modgui_color);
  2121. }
  2122. if (info.gui.color == nullptr)
  2123. info.gui.color = nc;
  2124. if (LilvNode* const modgui_knob = lilv_world_get(w, modguigui, ns.modgui_knob, nullptr))
  2125. {
  2126. info.gui.knob = strdup(lilv_node_as_string(modgui_knob));
  2127. lilv_node_free(modgui_knob);
  2128. }
  2129. if (info.gui.knob == nullptr)
  2130. info.gui.knob = nc;
  2131. if (LilvNodes* const modgui_ports = lilv_world_find_nodes(w, modguigui, ns.modgui_port, nullptr))
  2132. {
  2133. const unsigned int guiportscount = lilv_nodes_size(modgui_ports);
  2134. PluginGUIPort* const guiports = new PluginGUIPort[guiportscount+1];
  2135. memset(guiports, 0, sizeof(PluginGUIPort) * (guiportscount+1));
  2136. for (unsigned int i=0; i<guiportscount; ++i)
  2137. guiports[i] = { true, i, nc, nc };
  2138. int index;
  2139. LILV_FOREACH(nodes, it, modgui_ports)
  2140. {
  2141. const LilvNode* const modgui_port = lilv_nodes_get(modgui_ports, it);
  2142. if (LilvNode* const guiports_index = lilv_world_get(w, modgui_port, ns.lv2core_index, nullptr))
  2143. {
  2144. index = lilv_node_as_int(guiports_index);
  2145. lilv_node_free(guiports_index);
  2146. }
  2147. else
  2148. {
  2149. continue;
  2150. }
  2151. if (index < 0 || index >= (int)guiportscount)
  2152. continue;
  2153. PluginGUIPort& guiport(guiports[index]);
  2154. if (LilvNode* const guiports_symbol = lilv_world_get(w, modgui_port, ns.lv2core_symbol, nullptr))
  2155. {
  2156. // in case of duplicated indexes
  2157. if (guiport.symbol != nullptr && guiport.symbol != nc)
  2158. free((void*)guiport.symbol);
  2159. guiport.symbol = strdup(lilv_node_as_string(guiports_symbol));
  2160. lilv_node_free(guiports_symbol);
  2161. }
  2162. if (LilvNode* const guiports_name = lilv_world_get(w, modgui_port, ns.lv2core_name, nullptr))
  2163. {
  2164. // in case of duplicated indexes
  2165. if (guiport.name != nullptr && guiport.name != nc)
  2166. free((void*)guiport.name);
  2167. guiport.name = strdup(lilv_node_as_string(guiports_name));
  2168. lilv_node_free(guiports_name);
  2169. }
  2170. }
  2171. info.gui.ports = guiports;
  2172. lilv_nodes_free(modgui_ports);
  2173. }
  2174. if (LilvNodes* const modgui_monitors = lilv_world_find_nodes(w, modguigui, ns.modgui_monitoredOutputs, nullptr))
  2175. {
  2176. unsigned int monitorcount = lilv_nodes_size(modgui_monitors);
  2177. const char** monitors = new const char*[monitorcount+1];
  2178. memset(monitors, 0, sizeof(const char*) * (monitorcount+1));
  2179. monitorcount = 0;
  2180. LILV_FOREACH(nodes, it, modgui_monitors)
  2181. {
  2182. const LilvNode* const modgui_monitor = lilv_nodes_get(modgui_monitors, it);
  2183. if (LilvNode* const monitor_symbol = lilv_world_get(w, modgui_monitor, ns.lv2core_symbol, nullptr))
  2184. {
  2185. monitors[monitorcount++] = strdup(lilv_node_as_string(monitor_symbol));
  2186. lilv_node_free(monitor_symbol);
  2187. }
  2188. }
  2189. info.gui.monitoredOutputs = monitors;
  2190. lilv_nodes_free(modgui_monitors);
  2191. }
  2192. lilv_node_free(modguigui);
  2193. }
  2194. else
  2195. {
  2196. info.gui.resourcesDirectory = nc;
  2197. info.gui.iconTemplate = nc;
  2198. info.gui.settingsTemplate = nc;
  2199. info.gui.javascript = nc;
  2200. info.gui.stylesheet = nc;
  2201. info.gui.screenshot = nc;
  2202. info.gui.thumbnail = nc;
  2203. info.gui.discussionURL = nc;
  2204. info.gui.documentation = nc;
  2205. info.gui.brand = nc;
  2206. info.gui.label = nc;
  2207. info.gui.model = nc;
  2208. info.gui.panel = nc;
  2209. info.gui.color = nc;
  2210. info.gui.knob = nc;
  2211. }
  2212. // --------------------------------------------------------------------------------------------------------
  2213. // ports
  2214. std::unordered_map<std::string, PluginPortGroup> portGroups;
  2215. if (const uint32_t count = lilv_plugin_get_num_ports(p))
  2216. {
  2217. uint32_t countAudioInput=0, countAudioOutput=0;
  2218. uint32_t countControlInput=0, countControlOutput=0;
  2219. uint32_t countCvInput=0, countCvOutput=0;
  2220. uint32_t countMidiInput=0, countMidiOutput=0;
  2221. // precalculate port counts first
  2222. for (uint32_t i=0; i<count; ++i)
  2223. {
  2224. const LilvPort* const port = lilv_plugin_get_port_by_index(p, i);
  2225. PortDirection direction = kPortDirectionNull;
  2226. PortType type = kPortTypeNull;
  2227. if (LilvNodes* const nodes = lilv_port_get_value(p, port, ns.rdf_type))
  2228. {
  2229. LILV_FOREACH(nodes, it, nodes)
  2230. {
  2231. const LilvNode* const node2 = lilv_nodes_get(nodes, it);
  2232. const char* const nodestr = lilv_node_as_string(node2);
  2233. if (nodestr == nullptr)
  2234. continue;
  2235. else if (strcmp(nodestr, LV2_CORE__InputPort) == 0)
  2236. direction = kPortDirectionInput;
  2237. else if (strcmp(nodestr, LV2_CORE__OutputPort) == 0)
  2238. direction = kPortDirectionOutput;
  2239. else if (strcmp(nodestr, LV2_CORE__AudioPort) == 0)
  2240. type = kPortTypeAudio;
  2241. else if (strcmp(nodestr, LV2_CORE__ControlPort) == 0)
  2242. type = kPortTypeControl;
  2243. else if (strcmp(nodestr, MOD__CVPort) == 0 || (kAllowRegularCV && strcmp(nodestr, LV2_CORE__CVPort) == 0))
  2244. type = kPortTypeCV;
  2245. else if (strcmp(nodestr, LV2_ATOM__AtomPort) == 0 && lilv_port_supports_event(p, port, ns.midi_MidiEvent))
  2246. {
  2247. if (LilvNodes* const nodes2 = lilv_port_get_value(p, port, ns.atom_bufferType))
  2248. {
  2249. if (lilv_node_equals(lilv_nodes_get_first(nodes2), ns.atom_Sequence))
  2250. if (! lilv_port_has_property(p, port, ns.mod_rawMIDIClockAccess))
  2251. type = kPortTypeMIDI;
  2252. lilv_nodes_free(nodes2);
  2253. }
  2254. }
  2255. }
  2256. lilv_nodes_free(nodes);
  2257. }
  2258. if (direction == kPortDirectionNull || type == kPortTypeNull)
  2259. continue;
  2260. switch (type)
  2261. {
  2262. case kPortTypeNull:
  2263. break;
  2264. case kPortTypeAudio:
  2265. if (direction == kPortDirectionOutput)
  2266. ++countAudioOutput;
  2267. else
  2268. ++countAudioInput;
  2269. break;
  2270. case kPortTypeControl:
  2271. if (direction == kPortDirectionOutput)
  2272. ++countControlOutput;
  2273. else
  2274. ++countControlInput;
  2275. break;
  2276. case kPortTypeCV:
  2277. if (direction == kPortDirectionOutput)
  2278. ++countCvOutput;
  2279. else
  2280. ++countCvInput;
  2281. break;
  2282. case kPortTypeMIDI:
  2283. if (direction == kPortDirectionOutput)
  2284. ++countMidiOutput;
  2285. else
  2286. ++countMidiInput;
  2287. break;
  2288. }
  2289. }
  2290. // allocate stuff
  2291. if (countAudioInput > 0)
  2292. {
  2293. info.ports.audio.input = new PluginPort[countAudioInput+1];
  2294. memset(info.ports.audio.input, 0, sizeof(PluginPort) * (countAudioInput+1));
  2295. }
  2296. if (countAudioOutput > 0)
  2297. {
  2298. info.ports.audio.output = new PluginPort[countAudioOutput+1];
  2299. memset(info.ports.audio.output, 0, sizeof(PluginPort) * (countAudioOutput+1));
  2300. }
  2301. if (countControlInput > 0)
  2302. {
  2303. info.ports.control.input = new PluginPort[countControlInput+1];
  2304. memset(info.ports.control.input, 0, sizeof(PluginPort) * (countControlInput+1));
  2305. }
  2306. if (countControlOutput > 0)
  2307. {
  2308. info.ports.control.output = new PluginPort[countControlOutput+1];
  2309. memset(info.ports.control.output, 0, sizeof(PluginPort) * (countControlOutput+1));
  2310. }
  2311. if (countCvInput > 0)
  2312. {
  2313. info.ports.cv.input = new PluginPort[countCvInput+1];
  2314. memset(info.ports.cv.input, 0, sizeof(PluginPort) * (countCvInput+1));
  2315. }
  2316. if (countCvOutput > 0)
  2317. {
  2318. info.ports.cv.output = new PluginPort[countCvOutput+1];
  2319. memset(info.ports.cv.output, 0, sizeof(PluginPort) * (countCvOutput+1));
  2320. }
  2321. if (countMidiInput > 0)
  2322. {
  2323. info.ports.midi.input = new PluginPort[countMidiInput+1];
  2324. memset(info.ports.midi.input, 0, sizeof(PluginPort) * (countMidiInput+1));
  2325. }
  2326. if (countMidiOutput > 0)
  2327. {
  2328. info.ports.midi.output = new PluginPort[countMidiOutput+1];
  2329. memset(info.ports.midi.output, 0, sizeof(PluginPort) * (countMidiOutput+1));
  2330. }
  2331. // use counters as indexes now
  2332. countAudioInput = countAudioOutput = countControlInput = countControlOutput = 0;
  2333. countCvInput = countCvOutput = countMidiInput = countMidiOutput = 0;
  2334. // now fill info
  2335. for (uint32_t i=0; i<count; ++i)
  2336. {
  2337. const LilvPort* const port = lilv_plugin_get_port_by_index(p, i);
  2338. // ----------------------------------------------------------------------------------------------------
  2339. PortDirection direction = kPortDirectionNull;
  2340. PortType type = kPortTypeNull;
  2341. if (LilvNodes* const nodes = lilv_port_get_value(p, port, ns.rdf_type))
  2342. {
  2343. LILV_FOREACH(nodes, it, nodes)
  2344. {
  2345. const LilvNode* const node2 = lilv_nodes_get(nodes, it);
  2346. const char* const nodestr = lilv_node_as_string(node2);
  2347. if (nodestr == nullptr)
  2348. continue;
  2349. else if (strcmp(nodestr, LV2_CORE__InputPort) == 0)
  2350. direction = kPortDirectionInput;
  2351. else if (strcmp(nodestr, LV2_CORE__OutputPort) == 0)
  2352. direction = kPortDirectionOutput;
  2353. else if (strcmp(nodestr, LV2_CORE__AudioPort) == 0)
  2354. type = kPortTypeAudio;
  2355. else if (strcmp(nodestr, LV2_CORE__ControlPort) == 0)
  2356. type = kPortTypeControl;
  2357. else if (strcmp(nodestr, MOD__CVPort) == 0 || (kAllowRegularCV && strcmp(nodestr, LV2_CORE__CVPort) == 0))
  2358. type = kPortTypeCV;
  2359. else if (strcmp(nodestr, LV2_ATOM__AtomPort) == 0 && lilv_port_supports_event(p, port, ns.midi_MidiEvent))
  2360. {
  2361. if (LilvNodes* const nodes2 = lilv_port_get_value(p, port, ns.atom_bufferType))
  2362. {
  2363. if (lilv_node_equals(lilv_nodes_get_first(nodes2), ns.atom_Sequence))
  2364. if (! lilv_port_has_property(p, port, ns.mod_rawMIDIClockAccess))
  2365. type = kPortTypeMIDI;
  2366. lilv_nodes_free(nodes2);
  2367. }
  2368. }
  2369. }
  2370. lilv_nodes_free(nodes);
  2371. }
  2372. if (direction == kPortDirectionNull || type == kPortTypeNull)
  2373. continue;
  2374. // ----------------------------------------------------------------------------------------------------
  2375. PluginPort portinfo;
  2376. memset(&portinfo, 0, sizeof(PluginPort));
  2377. portinfo.index = i;
  2378. // ----------------------------------------------------------------------------------------------------
  2379. // name
  2380. if (LilvNode* const node = lilv_port_get_name(p, port))
  2381. {
  2382. portinfo.name = strdup(lilv_node_as_string(node));
  2383. lilv_node_free(node);
  2384. }
  2385. else
  2386. {
  2387. portinfo.name = nc;
  2388. }
  2389. // ----------------------------------------------------------------------------------------------------
  2390. // symbol
  2391. if (const LilvNode* const symbolnode = lilv_port_get_symbol(p, port))
  2392. portinfo.symbol = strdup(lilv_node_as_string(symbolnode));
  2393. else
  2394. portinfo.symbol = nc;
  2395. // ----------------------------------------------------------------------------------------------------
  2396. // short name
  2397. if (LilvNodes* const nodes = lilv_port_get_value(p, port, ns.lv2core_shortName))
  2398. {
  2399. portinfo.shortName = strdup(lilv_node_as_string(lilv_nodes_get_first(nodes)));
  2400. lilv_nodes_free(nodes);
  2401. }
  2402. else
  2403. {
  2404. portinfo.shortName = strdup(portinfo.name);
  2405. }
  2406. if (strlen(portinfo.shortName) > 16)
  2407. ((char*)portinfo.shortName)[16] = '\0';
  2408. // ----------------------------------------------------------------------------------------------------
  2409. // comment
  2410. if (LilvNodes* const nodes = lilv_port_get_value(p, port, ns.rdfs_comment))
  2411. {
  2412. portinfo.comment = strdup(lilv_node_as_string(lilv_nodes_get_first(nodes)));
  2413. lilv_nodes_free(nodes);
  2414. }
  2415. else
  2416. {
  2417. portinfo.comment = nc;
  2418. }
  2419. // ----------------------------------------------------------------------------------------------------
  2420. // designation
  2421. if (LilvNodes* const nodes = lilv_port_get_value(p, port, ns.lv2core_designation))
  2422. {
  2423. portinfo.designation = strdup(lilv_node_as_string(lilv_nodes_get_first(nodes)));
  2424. lilv_nodes_free(nodes);
  2425. }
  2426. else
  2427. {
  2428. portinfo.designation = nc;
  2429. }
  2430. // ----------------------------------------------------------------------------------------------------
  2431. // group
  2432. if (LilvNodes* const nodes = lilv_port_get_value(p, port, ns.pgroups_group))
  2433. {
  2434. LilvNode* const group = lilv_nodes_get_first(nodes);
  2435. portinfo.group = strdup(lilv_node_as_string(group));
  2436. if (! contains(portGroups, portinfo.group))
  2437. {
  2438. PluginPortGroup& portGroup = portGroups[portinfo.group] = {
  2439. true,
  2440. portinfo.group,
  2441. nc,
  2442. nc,
  2443. 0
  2444. };
  2445. if (LilvNode* const group_symbol = lilv_world_get(w, group, ns.lv2core_symbol, nullptr))
  2446. {
  2447. if (const char* const symbolstr = lilv_node_as_string(group_symbol))
  2448. portGroup.symbol = strdup(symbolstr);
  2449. lilv_node_free(group_symbol);
  2450. }
  2451. if (LilvNode* const group_name = lilv_world_get(w, group, ns.lv2core_name, nullptr))
  2452. {
  2453. if (const char* const namestr = lilv_node_as_string(group_name))
  2454. portGroup.name = strdup(namestr);
  2455. lilv_node_free(group_name);
  2456. }
  2457. if (LilvNode* const node = lilv_world_get(w, group, ns.lv2core_index, nullptr))
  2458. {
  2459. portGroup.index = lilv_node_as_int(node);
  2460. lilv_node_free(node);
  2461. }
  2462. }
  2463. lilv_nodes_free(nodes);
  2464. }
  2465. else
  2466. {
  2467. portinfo.group = nc;
  2468. }
  2469. // ----------------------------------------------------------------------------------------------------
  2470. // range steps
  2471. if (LilvNodes* const nodes = lilv_port_get_value(p, port, ns.mod_rangeSteps))
  2472. {
  2473. portinfo.rangeSteps = lilv_node_as_int(lilv_nodes_get_first(nodes));
  2474. lilv_nodes_free(nodes);
  2475. }
  2476. else if (LilvNodes* const nodes2 = lilv_port_get_value(p, port, ns.pprops_rangeSteps))
  2477. {
  2478. portinfo.rangeSteps = lilv_node_as_int(lilv_nodes_get_first(nodes2));
  2479. lilv_nodes_free(nodes2);
  2480. }
  2481. // ----------------------------------------------------------------------------------------------------
  2482. // port properties
  2483. if (LilvNodes* const nodes = lilv_port_get_value(p, port, ns.lv2core_portProperty))
  2484. {
  2485. const unsigned int propcount = lilv_nodes_size(nodes);
  2486. unsigned int pindex = 0;
  2487. const char** const props = new const char*[propcount+1];
  2488. memset(props, 0, sizeof(const char*) * (propcount+1));
  2489. LILV_FOREACH(nodes, itprop, nodes)
  2490. {
  2491. if (pindex >= propcount)
  2492. continue;
  2493. if (const char* prop = strrchr(lilv_node_as_string(lilv_nodes_get(nodes, itprop)), '#'))
  2494. {
  2495. prop += 1;
  2496. if (prop[0] != '\0')
  2497. props[pindex++] = strdup(prop);
  2498. }
  2499. }
  2500. portinfo.properties = props;
  2501. lilv_nodes_free(nodes);
  2502. }
  2503. // ----------------------------------------------------------------------------------------------------
  2504. if (type == kPortTypeControl || type == kPortTypeCV)
  2505. {
  2506. LilvNodes* xminimum = lilv_port_get_value(p, port, ns.mod_minimum);
  2507. if (xminimum == nullptr)
  2508. xminimum = lilv_port_get_value(p, port, ns.lv2core_minimum);
  2509. LilvNodes* xmaximum = lilv_port_get_value(p, port, ns.mod_maximum);
  2510. if (xmaximum == nullptr)
  2511. xmaximum = lilv_port_get_value(p, port, ns.lv2core_maximum);
  2512. LilvNodes* xdefault = ns.mod_default_custom != nullptr
  2513. ? lilv_port_get_value(p, port, ns.mod_default_custom)
  2514. : nullptr;
  2515. if (xdefault == nullptr)
  2516. xdefault = lilv_port_get_value(p, port, ns.mod_default);
  2517. if (xdefault == nullptr)
  2518. xdefault = lilv_port_get_value(p, port, ns.lv2core_default);
  2519. if (xminimum != nullptr && xmaximum != nullptr)
  2520. {
  2521. portinfo.ranges.min = lilv_node_as_float(lilv_nodes_get_first(xminimum));
  2522. portinfo.ranges.max = lilv_node_as_float(lilv_nodes_get_first(xmaximum));
  2523. if (portinfo.ranges.min >= portinfo.ranges.max)
  2524. portinfo.ranges.max = portinfo.ranges.min + 1.0f;
  2525. if (xdefault != nullptr)
  2526. portinfo.ranges.def = lilv_node_as_float(lilv_nodes_get_first(xdefault));
  2527. else
  2528. portinfo.ranges.def = portinfo.ranges.min;
  2529. if (portinfo.properties != nullptr)
  2530. {
  2531. for (int j=0; portinfo.properties[j] != nullptr; ++j)
  2532. {
  2533. if (strcmp(portinfo.properties[j], "preferMomentaryOffByDefault") == 0)
  2534. {
  2535. portinfo.ranges.def = portinfo.ranges.max;
  2536. break;
  2537. }
  2538. if (strcmp(portinfo.properties[j], "preferMomentaryOnByDefault") == 0)
  2539. {
  2540. portinfo.ranges.def = portinfo.ranges.min;
  2541. break;
  2542. }
  2543. }
  2544. }
  2545. }
  2546. else
  2547. {
  2548. if (type == kPortTypeCV)
  2549. {
  2550. portinfo.ranges.min = -5.0f;
  2551. portinfo.ranges.max = 5.0f;
  2552. }
  2553. else
  2554. {
  2555. portinfo.ranges.min = 0.0f;
  2556. portinfo.ranges.max = 1.0f;
  2557. }
  2558. portinfo.ranges.def = 0.0f;
  2559. }
  2560. lilv_nodes_free(xminimum);
  2561. lilv_nodes_free(xmaximum);
  2562. lilv_nodes_free(xdefault);
  2563. if (LilvScalePoints* const scalepoints = lilv_port_get_scale_points(p, port))
  2564. {
  2565. if (const unsigned int scalepointcount = lilv_scale_points_size(scalepoints))
  2566. {
  2567. PluginPortScalePoint* const portsps = new PluginPortScalePoint[scalepointcount+1];
  2568. memset(portsps, 0, sizeof(PluginPortScalePoint) * (scalepointcount+1));
  2569. // get all scalepoints and sort them by value
  2570. std::map<double,const LilvScalePoint*> sortedpoints;
  2571. LILV_FOREACH(scale_points, itscl, scalepoints)
  2572. {
  2573. const LilvScalePoint* const scalepoint = lilv_scale_points_get(scalepoints, itscl);
  2574. const LilvNode* const xlabel = lilv_scale_point_get_label(scalepoint);
  2575. const LilvNode* const xvalue = lilv_scale_point_get_value(scalepoint);
  2576. if (xlabel == nullptr || xvalue == nullptr)
  2577. continue;
  2578. const double valueid = lilv_node_as_float(xvalue);
  2579. sortedpoints[valueid] = scalepoint;
  2580. }
  2581. // now store them sorted
  2582. unsigned int spindex = 0;
  2583. for (auto& scalepoint : sortedpoints)
  2584. {
  2585. if (spindex >= scalepointcount)
  2586. continue;
  2587. const LilvNode* const xlabel = lilv_scale_point_get_label(scalepoint.second);
  2588. const LilvNode* const xvalue = lilv_scale_point_get_value(scalepoint.second);
  2589. portsps[spindex++] = {
  2590. true,
  2591. lilv_node_as_float(xvalue),
  2592. strdup(lilv_node_as_string(xlabel)),
  2593. };
  2594. }
  2595. portinfo.scalePoints = portsps;
  2596. }
  2597. lilv_scale_points_free(scalepoints);
  2598. }
  2599. }
  2600. // ----------------------------------------------------------------------------------------------------
  2601. // control ports might contain unit
  2602. portinfo.units.label = nc;
  2603. portinfo.units.render = nc;
  2604. portinfo.units.symbol = nc;
  2605. if (type == 2)
  2606. {
  2607. if (LilvNodes* const uunits = lilv_port_get_value(p, port, ns.units_unit))
  2608. {
  2609. LilvNode* const uunit = lilv_nodes_get_first(uunits);
  2610. _fill_units(w, portinfo.units, uunit, ns);
  2611. lilv_nodes_free(uunits);
  2612. }
  2613. }
  2614. // ----------------------------------------------------------------------------------------------------
  2615. portinfo.valid = true;
  2616. switch (type)
  2617. {
  2618. case kPortTypeNull:
  2619. break;
  2620. case kPortTypeAudio:
  2621. if (direction == kPortDirectionOutput)
  2622. info.ports.audio.output[countAudioOutput++] = portinfo;
  2623. else
  2624. info.ports.audio.input[countAudioInput++] = portinfo;
  2625. break;
  2626. case kPortTypeControl:
  2627. if (direction == kPortDirectionOutput)
  2628. info.ports.control.output[countControlOutput++] = portinfo;
  2629. else
  2630. info.ports.control.input[countControlInput++] = portinfo;
  2631. break;
  2632. case kPortTypeCV:
  2633. if (direction == kPortDirectionOutput)
  2634. info.ports.cv.output[countCvOutput++] = portinfo;
  2635. else
  2636. info.ports.cv.input[countCvInput++] = portinfo;
  2637. break;
  2638. case kPortTypeMIDI:
  2639. if (direction == kPortDirectionOutput)
  2640. info.ports.midi.output[countMidiOutput++] = portinfo;
  2641. else
  2642. info.ports.midi.input[countMidiInput++] = portinfo;
  2643. break;
  2644. }
  2645. }
  2646. // also iotype
  2647. fill_iotype(&info, countAudioInput, countAudioOutput, countMidiInput, countMidiOutput);
  2648. }
  2649. else
  2650. {
  2651. info.iotype = kPluginIONull;
  2652. }
  2653. // --------------------------------------------------------------------------------------------------------
  2654. // port groups
  2655. if (size_t count = portGroups.size())
  2656. {
  2657. PluginPortGroup* const groups = new PluginPortGroup[count + 1];
  2658. size_t index = 0;
  2659. for (const auto& kv : portGroups)
  2660. {
  2661. groups[index++] = kv.second;
  2662. }
  2663. memset(&groups[count], 0, sizeof(PluginPortGroup));
  2664. // Sort the final array directly in-place
  2665. std::sort(groups, groups + count, [](const PluginPortGroup& a, const PluginPortGroup& b) {
  2666. if (a.index != b.index)
  2667. return a.index < b.index;
  2668. int cmpName = strcmp(a.name, b.name);
  2669. if (cmpName != 0)
  2670. return cmpName < 0;
  2671. return strcmp(a.symbol, b.symbol) < 0;
  2672. });
  2673. info.portGroups = groups;
  2674. }
  2675. // --------------------------------------------------------------------------------------------------------
  2676. // parameters
  2677. std::map<std::string, PluginParameter> usedParameters;
  2678. _fill_parameters_for_plugin(w, p, ns, usedParameters, true);
  2679. _fill_parameters_for_plugin(w, p, ns, usedParameters, false);
  2680. if (size_t count = usedParameters.size())
  2681. {
  2682. PluginParameter* const params = new PluginParameter[count+1];
  2683. count = 0;
  2684. for (auto& param : usedParameters)
  2685. params[count++] = param.second;
  2686. memset(&params[count], 0, sizeof(PluginParameter));
  2687. info.parameters = params;
  2688. }
  2689. // --------------------------------------------------------------------------------------------------------
  2690. // presets
  2691. _place_preset_info(w, info, p, ns.pset_Preset, ns.rdfs_label);
  2692. // --------------------------------------------------------------------------------------------------------
  2693. lilv_free((void*)bundle);
  2694. info.valid = true;
  2695. printf("NOTICE: Finished scanning '%s'\n", info.uri);
  2696. return info;
  2697. }
  2698. // --------------------------------------------------------------------------------------------------------
  2699. static void _clear_plugin_info_mini(const PluginInfo_Mini* const info)
  2700. {
  2701. if (info->brand != nc)
  2702. free((void*)info->brand);
  2703. if (info->label != nc)
  2704. free((void*)info->label);
  2705. if (info->name != nc)
  2706. free((void*)info->name);
  2707. if (info->comment != nc)
  2708. free((void*)info->comment);
  2709. if (info->buildEnvironment != nc &&
  2710. info->buildEnvironment != kBuildEnvironmentProd &&
  2711. info->buildEnvironment != kBuildEnvironmentDev &&
  2712. info->buildEnvironment != kBuildEnvironmentLabs)
  2713. free((void*)info->buildEnvironment);
  2714. if (info->gui.resourcesDirectory != nc)
  2715. free((void*)info->gui.resourcesDirectory);
  2716. if (info->gui.screenshot != nc)
  2717. free((void*)info->gui.screenshot);
  2718. if (info->gui.thumbnail != nc)
  2719. free((void*)info->gui.thumbnail);
  2720. delete info;
  2721. }
  2722. static void _fill_plugin_info_mini_from_full(const PluginInfo& info2, const PluginInfo_Mini** const miniInfo)
  2723. {
  2724. if (*miniInfo != nullptr || !info2.valid)
  2725. return;
  2726. PluginInfo_Mini* const info = new PluginInfo_Mini;
  2727. *miniInfo = info;
  2728. info->uri = info2.uri;
  2729. info->name = info2.name != nc ? strdup(info2.name) : nc;
  2730. info->brand = info2.brand != nc ? strdup(info2.brand) : nc;
  2731. info->label = info2.label != nc ? strdup(info2.label) : nc;
  2732. info->comment = info2.comment != nc ? strdup(info2.comment) : nc;
  2733. info->buildEnvironment = info2.buildEnvironment == kBuildEnvironmentProd ? kBuildEnvironmentProd :
  2734. info2.buildEnvironment == kBuildEnvironmentDev ? kBuildEnvironmentDev :
  2735. info2.buildEnvironment == kBuildEnvironmentLabs ? kBuildEnvironmentLabs :
  2736. info2.buildEnvironment != nc ? strdup(info2.buildEnvironment) : nc;
  2737. info->category = info2.category;
  2738. info->microVersion = info2.microVersion;
  2739. info->minorVersion = info2.minorVersion;
  2740. info->release = info2.release;
  2741. info->builder = info2.builder;
  2742. info->licensed = info2.licensed;
  2743. info->gui.resourcesDirectory = info2.gui.resourcesDirectory != nc ? strdup(info2.gui.resourcesDirectory) : nc;
  2744. info->gui.screenshot = info2.gui.screenshot != nc ? strdup(info2.gui.screenshot) : nc;
  2745. info->gui.thumbnail = info2.gui.thumbnail != nc ? strdup(info2.gui.thumbnail) : nc;
  2746. }
  2747. // --------------------------------------------------------------------------------------------------------
  2748. static const PedalboardInfo_Mini* _get_pedalboard_info_mini(LilvWorld* const w,
  2749. const LilvPlugin* const p,
  2750. const LilvNode* const versiontypenode,
  2751. const LilvNode* const rdftypenode,
  2752. const LilvNode* const ingenblocknode,
  2753. const LilvNode* const lv2protonode,
  2754. const PedalboardInfoType ptype)
  2755. {
  2756. // --------------------------------------------------------------------------------------------------------
  2757. // check if plugin is pedalboard
  2758. bool isPedalboard = false;
  2759. if (LilvNodes* const nodes = lilv_plugin_get_value(p, rdftypenode))
  2760. {
  2761. LILV_FOREACH(nodes, it, nodes)
  2762. {
  2763. const LilvNode* const node = lilv_nodes_get(nodes, it);
  2764. if (const char* const nodestr = lilv_node_as_string(node))
  2765. {
  2766. if (strcmp(nodestr, LILV_NS_MODPEDAL "Pedalboard") == 0)
  2767. {
  2768. isPedalboard = true;
  2769. break;
  2770. }
  2771. }
  2772. }
  2773. lilv_nodes_free(nodes);
  2774. }
  2775. if (! isPedalboard)
  2776. return nullptr;
  2777. // --------------------------------------------------------------------------------------------------------
  2778. // bundle (required)
  2779. char* bundle;
  2780. if (const LilvNode* const node = lilv_plugin_get_bundle_uri(p))
  2781. {
  2782. bundle = lilv_file_abspath(lilv_node_as_string(node));
  2783. if (bundle == nullptr)
  2784. return nullptr;
  2785. }
  2786. else
  2787. {
  2788. return nullptr;
  2789. }
  2790. // --------------------------------------------------------------------------------------------------------
  2791. // title (required)
  2792. const char* title;
  2793. if (LilvNode* const node = lilv_plugin_get_name(p))
  2794. {
  2795. if (const char* const name = lilv_node_as_string(node))
  2796. title = name[0] != '\0' ? strdup(name) : kUntitled;
  2797. else
  2798. title = kUntitled;
  2799. lilv_node_free(node);
  2800. }
  2801. else
  2802. {
  2803. free(bundle);
  2804. return nullptr;
  2805. }
  2806. // --------------------------------------------------------------------------------------------------------
  2807. PedalboardInfo_Mini* const info = new PedalboardInfo_Mini;
  2808. info->bundle = bundle;
  2809. info->title = title;
  2810. // --------------------------------------------------------------------------------------------------------
  2811. // uri
  2812. info->uri = strdup(lilv_node_as_uri(lilv_plugin_get_uri(p)));
  2813. // --------------------------------------------------------------------------------------------------------
  2814. // factory
  2815. switch (ptype)
  2816. {
  2817. case kPedalboardInfoUserOnly:
  2818. info->factory = false;
  2819. break;
  2820. case kPedalboardInfoFactoryOnly:
  2821. info->factory = true;
  2822. break;
  2823. default:
  2824. info->factory = is_factory_pedalboard(bundle);
  2825. break;
  2826. }
  2827. // --------------------------------------------------------------------------------------------------------
  2828. // check if all plugins in the pedalboard exist in our world, also if licensed
  2829. info->broken = info->hasTrialPlugins = false;
  2830. if (LilvNodes* const blocks = lilv_plugin_get_value(p, ingenblocknode))
  2831. {
  2832. LilvWorld* const w2 = W;
  2833. const NamespaceDefinitions& ns(NamespaceDefinitions::getStaticInstance(w2));
  2834. LILV_FOREACH(nodes, itblocks, blocks)
  2835. {
  2836. const LilvNode* const block = lilv_nodes_get(blocks, itblocks);
  2837. if (LilvNode* const proto = lilv_world_get(w, block, lv2protonode, nullptr))
  2838. {
  2839. const std::string uri = lilv_node_as_uri(proto);
  2840. if (! contains(PLUGNFO_Mini, uri))
  2841. {
  2842. info->broken = true;
  2843. lilv_node_free(proto);
  2844. break;
  2845. }
  2846. // checking for trial plugins, only needs to be done once
  2847. if (!info->hasTrialPlugins && !info->factory)
  2848. {
  2849. if (PLUGNFO_Mini[uri] == nullptr)
  2850. {
  2851. LilvNode* const urinode = lilv_new_uri(w2, uri.c_str());
  2852. PLUGNFO_Mini[uri] = _get_plugin_info_mini(w2, lilv_plugins_get_by_uri(PLUGINS, urinode), ns);
  2853. lilv_node_free(urinode);
  2854. }
  2855. if (const PluginInfo_Mini* const miniInfo = PLUGNFO_Mini[uri])
  2856. if (miniInfo->licensed == kPluginLicenseTrial)
  2857. info->hasTrialPlugins = true;
  2858. }
  2859. lilv_node_free(proto);
  2860. }
  2861. }
  2862. lilv_nodes_free(blocks);
  2863. }
  2864. // --------------------------------------------------------------------------------------------------------
  2865. // version
  2866. info->version = 0;
  2867. if (!info->factory)
  2868. {
  2869. if (LilvNodes* const nodes = lilv_plugin_get_value(p, versiontypenode))
  2870. {
  2871. info->version = lilv_node_as_int(lilv_nodes_get_first(nodes));
  2872. lilv_nodes_free(nodes);
  2873. }
  2874. }
  2875. return info;
  2876. }
  2877. bool _is_pedalboard_broken(const LilvPlugin* const p,
  2878. LilvWorld* const w,
  2879. const LilvNode* const ingenblocknode,
  2880. const LilvNode* const lv2protonode)
  2881. {
  2882. bool broken = false;
  2883. if (LilvNodes* const blocks = lilv_plugin_get_value(p, ingenblocknode))
  2884. {
  2885. LILV_FOREACH(nodes, itblocks, blocks)
  2886. {
  2887. const LilvNode* const block = lilv_nodes_get(blocks, itblocks);
  2888. if (LilvNode* const proto = lilv_world_get(w, block, lv2protonode, nullptr))
  2889. {
  2890. const std::string uri = lilv_node_as_uri(proto);
  2891. lilv_node_free(proto);
  2892. if (! contains(PLUGNFO, uri))
  2893. {
  2894. broken = true;
  2895. break;
  2896. }
  2897. }
  2898. }
  2899. lilv_nodes_free(blocks);
  2900. }
  2901. return broken;
  2902. }
  2903. // --------------------------------------------------------------------------------------------------------
  2904. // get_plugin_list
  2905. static const char** _get_plug_list_ret = nullptr;
  2906. static int _get_plug_list_lastsize = 0;
  2907. // get_all_plugins
  2908. static const PluginInfo_Mini** _get_plugs_mini_ret = nullptr;
  2909. static int _get_plugs_mini_lastsize = 0;
  2910. // get_all_pedalboards
  2911. static const PedalboardInfo_Mini** _get_pedals_mini_ret = nullptr;
  2912. // get_broken_pedalboards
  2913. static const char** _get_broken_pedals_ret = nullptr;
  2914. // get_pedalboard_info
  2915. static PedalboardInfo* _get_pedal_info_ret;
  2916. // add_bundle_to_lilv_world/remove_bundle_from_lilv_world
  2917. static const char** _add_remove_bundles_ret = nullptr;
  2918. // get_pedalboard_plugin_values
  2919. static const PedalboardPluginValues* _get_pedal_values_ret = nullptr;
  2920. // get_state_port_values
  2921. static StatePortValue* _get_state_values_ret = nullptr;
  2922. // file_uri_parse
  2923. static char* _file_uri_parse_ret = nullptr;
  2924. // --------------------------------------------------------------------------------------------------------
  2925. static void _clear_gui_port_info(PluginGUIPort& guiportinfo)
  2926. {
  2927. if (guiportinfo.name != nc)
  2928. free((void*)guiportinfo.name);
  2929. if (guiportinfo.symbol != nc)
  2930. free((void*)guiportinfo.symbol);
  2931. memset(&guiportinfo, 0, sizeof(PluginGUIPort));
  2932. }
  2933. static void _clear_port_info(PluginPort& portinfo)
  2934. {
  2935. if (portinfo.name != nc)
  2936. free((void*)portinfo.name);
  2937. if (portinfo.symbol != nc)
  2938. free((void*)portinfo.symbol);
  2939. if (portinfo.comment != nc)
  2940. free((void*)portinfo.comment);
  2941. if (portinfo.designation != nc)
  2942. free((void*)portinfo.designation);
  2943. if (portinfo.group != nc)
  2944. free((void*)portinfo.group);
  2945. if (portinfo.shortName != nc)
  2946. free((void*)portinfo.shortName);
  2947. if (portinfo.properties != nullptr)
  2948. {
  2949. for (int i=0; portinfo.properties[i] != nullptr; ++i)
  2950. free((void*)portinfo.properties[i]);
  2951. delete[] portinfo.properties;
  2952. }
  2953. if (portinfo.scalePoints != nullptr)
  2954. {
  2955. for (int i=0; portinfo.scalePoints[i].valid; ++i)
  2956. free((void*)portinfo.scalePoints[i].label);
  2957. delete[] portinfo.scalePoints;
  2958. }
  2959. if (portinfo.units._custom)
  2960. {
  2961. if (portinfo.units.label != nc)
  2962. free((void*)portinfo.units.label);
  2963. if (portinfo.units.render != nc)
  2964. free((void*)portinfo.units.render);
  2965. if (portinfo.units.symbol != nc)
  2966. free((void*)portinfo.units.symbol);
  2967. }
  2968. memset(&portinfo, 0, sizeof(PluginPort));
  2969. }
  2970. static void _clear_port_group_info(const PluginPortGroup& portGroup)
  2971. {
  2972. // NOTE portGroup.uri points to port.group
  2973. if (portGroup.symbol != nc)
  2974. free((void*)portGroup.symbol);
  2975. if (portGroup.name != nc)
  2976. free((void*)portGroup.name);
  2977. }
  2978. static void _clear_parameter_info(const PluginParameter& parameter)
  2979. {
  2980. free((void*)parameter.uri);
  2981. free((void*)parameter.label);
  2982. free((void*)parameter.type);
  2983. if (parameter.comment != nc)
  2984. free((void*)parameter.comment);
  2985. if (parameter.shortName != nc)
  2986. free((void*)parameter.shortName);
  2987. if (parameter.fileTypes != nullptr)
  2988. {
  2989. free((void*)parameter.fileTypes[0]);
  2990. delete[] parameter.fileTypes;
  2991. }
  2992. if (parameter.supportedExtensions != nullptr)
  2993. {
  2994. free((void*)parameter.supportedExtensions[0]);
  2995. delete[] parameter.supportedExtensions;
  2996. }
  2997. if (parameter.ranges.type == 's')
  2998. {
  2999. free((void*)parameter.ranges.s);
  3000. }
  3001. if (parameter.units._custom)
  3002. {
  3003. if (parameter.units.label != nc)
  3004. free((void*)parameter.units.label);
  3005. if (parameter.units.render != nc)
  3006. free((void*)parameter.units.render);
  3007. if (parameter.units.symbol != nc)
  3008. free((void*)parameter.units.symbol);
  3009. }
  3010. // memset(&parameter, 0, sizeof(PluginParameter));
  3011. }
  3012. static void _clear_plugin_info(PluginInfo& info)
  3013. {
  3014. if (info.name != nc)
  3015. lilv_free((void*)info.name);
  3016. if (info.binary != nc)
  3017. free((void*)info.binary);
  3018. if (info.license != nc)
  3019. free((void*)info.license);
  3020. if (info.comment != nc)
  3021. free((void*)info.comment);
  3022. if (info.buildEnvironment != nc &&
  3023. info.buildEnvironment != kBuildEnvironmentProd &&
  3024. info.buildEnvironment != kBuildEnvironmentDev &&
  3025. info.buildEnvironment != kBuildEnvironmentLabs)
  3026. free((void*)info.buildEnvironment);
  3027. if (info.version != nc)
  3028. free((void*)info.version);
  3029. if (info.brand != nc)
  3030. free((void*)info.brand);
  3031. if (info.label != nc)
  3032. free((void*)info.label);
  3033. if (info.author.name != nc)
  3034. free((void*)info.author.name);
  3035. if (info.author.homepage != nc)
  3036. free((void*)info.author.homepage);
  3037. if (info.author.email != nc)
  3038. free((void*)info.author.email);
  3039. if (info.gui.resourcesDirectory != nc)
  3040. free((void*)info.gui.resourcesDirectory);
  3041. if (info.gui.iconTemplate != nc)
  3042. free((void*)info.gui.iconTemplate);
  3043. if (info.gui.settingsTemplate != nc)
  3044. free((void*)info.gui.settingsTemplate);
  3045. if (info.gui.javascript != nc)
  3046. free((void*)info.gui.javascript);
  3047. if (info.gui.stylesheet != nc)
  3048. free((void*)info.gui.stylesheet);
  3049. if (info.gui.screenshot != nc)
  3050. free((void*)info.gui.screenshot);
  3051. if (info.gui.thumbnail != nc)
  3052. free((void*)info.gui.thumbnail);
  3053. if (info.gui.discussionURL != nc)
  3054. free((void*)info.gui.discussionURL);
  3055. if (info.gui.documentation != nc)
  3056. free((void*)info.gui.documentation);
  3057. if (info.gui.brand != nc)
  3058. free((void*)info.gui.brand);
  3059. if (info.gui.label != nc)
  3060. free((void*)info.gui.label);
  3061. if (info.gui.model != nc)
  3062. free((void*)info.gui.model);
  3063. if (info.gui.panel != nc)
  3064. free((void*)info.gui.panel);
  3065. if (info.gui.color != nc)
  3066. free((void*)info.gui.color);
  3067. if (info.gui.knob != nc)
  3068. free((void*)info.gui.knob);
  3069. if (info.gui.ports != nullptr)
  3070. {
  3071. for (int i=0; info.gui.ports[i].valid; ++i)
  3072. _clear_gui_port_info(info.gui.ports[i]);
  3073. delete[] info.gui.ports;
  3074. }
  3075. if (info.gui.monitoredOutputs != nullptr)
  3076. {
  3077. for (int i=0; info.gui.monitoredOutputs[i]; ++i)
  3078. free((void*)info.gui.monitoredOutputs[i]);
  3079. delete[] info.gui.monitoredOutputs;
  3080. }
  3081. if (info.bundles != nullptr)
  3082. {
  3083. for (int i=0; info.bundles[i]; ++i)
  3084. free((void*)info.bundles[i]);
  3085. delete[] info.bundles;
  3086. }
  3087. if (info.ports.audio.input != nullptr)
  3088. {
  3089. for (int i=0; info.ports.audio.input[i].valid; ++i)
  3090. _clear_port_info(info.ports.audio.input[i]);
  3091. delete[] info.ports.audio.input;
  3092. }
  3093. if (info.ports.audio.output != nullptr)
  3094. {
  3095. for (int i=0; info.ports.audio.output[i].valid; ++i)
  3096. _clear_port_info(info.ports.audio.output[i]);
  3097. delete[] info.ports.audio.output;
  3098. }
  3099. if (info.ports.control.input != nullptr)
  3100. {
  3101. for (int i=0; info.ports.control.input[i].valid; ++i)
  3102. _clear_port_info(info.ports.control.input[i]);
  3103. delete[] info.ports.control.input;
  3104. }
  3105. if (info.ports.control.output != nullptr)
  3106. {
  3107. for (int i=0; info.ports.control.output[i].valid; ++i)
  3108. _clear_port_info(info.ports.control.output[i]);
  3109. delete[] info.ports.control.output;
  3110. }
  3111. if (info.ports.cv.input != nullptr)
  3112. {
  3113. for (int i=0; info.ports.cv.input[i].valid; ++i)
  3114. _clear_port_info(info.ports.cv.input[i]);
  3115. delete[] info.ports.cv.input;
  3116. }
  3117. if (info.ports.cv.output != nullptr)
  3118. {
  3119. for (int i=0; info.ports.cv.output[i].valid; ++i)
  3120. _clear_port_info(info.ports.cv.output[i]);
  3121. delete[] info.ports.cv.output;
  3122. }
  3123. if (info.ports.midi.input != nullptr)
  3124. {
  3125. for (int i=0; info.ports.midi.input[i].valid; ++i)
  3126. _clear_port_info(info.ports.midi.input[i]);
  3127. delete[] info.ports.midi.input;
  3128. }
  3129. if (info.ports.midi.output != nullptr)
  3130. {
  3131. for (int i=0; info.ports.midi.output[i].valid; ++i)
  3132. _clear_port_info(info.ports.midi.output[i]);
  3133. delete[] info.ports.midi.output;
  3134. }
  3135. if (info.portGroups != nullptr)
  3136. {
  3137. for (int i=0; info.portGroups[i].valid; ++i)
  3138. _clear_port_group_info(info.portGroups[i]);
  3139. delete[] info.portGroups;
  3140. }
  3141. if (info.parameters != nullptr)
  3142. {
  3143. for (int i=0; info.parameters[i].valid; ++i)
  3144. _clear_parameter_info(info.parameters[i]);
  3145. delete[] info.parameters;
  3146. }
  3147. if (info.presets != nullptr)
  3148. {
  3149. for (int i=0; info.presets[i].valid; ++i)
  3150. {
  3151. free((void*)info.presets[i].uri);
  3152. free((void*)info.presets[i].label);
  3153. if (info.presets[i].path != nc)
  3154. free((void*)info.presets[i].path);
  3155. }
  3156. delete[] info.presets;
  3157. }
  3158. memset(&info, 0, sizeof(PluginInfo));
  3159. }
  3160. static void _clear_pedalboard_info(PedalboardInfo& info)
  3161. {
  3162. if (info.title != nc && info.title != kUntitled)
  3163. free((void*)info.title);
  3164. if (info.connections != nullptr)
  3165. {
  3166. for (int i=0; info.connections[i].valid; ++i)
  3167. {
  3168. lilv_free((void*)info.connections[i].source);
  3169. lilv_free((void*)info.connections[i].target);
  3170. }
  3171. delete[] info.connections;
  3172. }
  3173. if (info.plugins != nullptr)
  3174. {
  3175. for (int i=0; info.plugins[i].valid; ++i)
  3176. {
  3177. const PedalboardPlugin& p(info.plugins[i]);
  3178. free((void*)p.instance);
  3179. free((void*)p.uri);
  3180. if (p.label != nc)
  3181. free((void*)p.label);
  3182. if (p.preset != nc)
  3183. free((void*)p.preset);
  3184. if (p.ports != nullptr)
  3185. {
  3186. for (int j=0; p.ports[j].valid; ++j)
  3187. lilv_free((void*)p.ports[j].symbol);
  3188. delete[] p.ports;
  3189. }
  3190. }
  3191. delete[] info.plugins;
  3192. }
  3193. if (info.hardware.midi_ins != nullptr)
  3194. {
  3195. for (int i=0; info.hardware.midi_ins[i].valid; ++i)
  3196. {
  3197. lilv_free((void*)info.hardware.midi_ins[i].symbol);
  3198. if (info.hardware.midi_ins[i].name != nc)
  3199. free((void*)info.hardware.midi_ins[i].name);
  3200. }
  3201. delete[] info.hardware.midi_ins;
  3202. }
  3203. if (info.hardware.midi_outs != nullptr)
  3204. {
  3205. for (int i=0; info.hardware.midi_outs[i].valid; ++i)
  3206. {
  3207. lilv_free((void*)info.hardware.midi_outs[i].symbol);
  3208. if (info.hardware.midi_outs[i].name != nc)
  3209. free((void*)info.hardware.midi_outs[i].name);
  3210. }
  3211. delete[] info.hardware.midi_outs;
  3212. }
  3213. memset(&info, 0, sizeof(PedalboardInfo));
  3214. }
  3215. // --------------------------------------------------------------------------------------------------------
  3216. static void _clear_pedalboards(const PedalboardInfo_Mini** const pedalboards)
  3217. {
  3218. if (pedalboards == nullptr)
  3219. return;
  3220. const PedalboardInfo_Mini* info;
  3221. for (int i=0;; ++i)
  3222. {
  3223. info = pedalboards[i];
  3224. if (info == nullptr)
  3225. break;
  3226. free((void*)info->uri);
  3227. free((void*)info->bundle);
  3228. if (info->title != nc && info->title != kUntitled)
  3229. free((void*)info->title);
  3230. delete info;
  3231. }
  3232. delete[] pedalboards;
  3233. }
  3234. static void _clear_pedalboard_plugin_values()
  3235. {
  3236. if (_get_pedal_values_ret == nullptr)
  3237. return;
  3238. for (int i=0; _get_pedal_values_ret[i].valid; ++i)
  3239. {
  3240. const PedalboardPluginValues& pvals(_get_pedal_values_ret[i]);
  3241. free((void*)pvals.instance);
  3242. if (pvals.preset != nc)
  3243. free((void*)pvals.preset);
  3244. if (pvals.ports != nullptr)
  3245. {
  3246. for (int j=0; pvals.ports[j].valid; ++j)
  3247. lilv_free((void*)pvals.ports[j].symbol);
  3248. delete[] pvals.ports;
  3249. }
  3250. }
  3251. delete[] _get_pedal_values_ret;
  3252. _get_pedal_values_ret = nullptr;
  3253. }
  3254. static void _clear_state_values()
  3255. {
  3256. if (_get_state_values_ret == nullptr)
  3257. return;
  3258. for (int i=0; _get_state_values_ret[i].valid; ++i)
  3259. free((void*)_get_state_values_ret[i].symbol);
  3260. delete[] _get_state_values_ret;
  3261. _get_state_values_ret = nullptr;
  3262. }
  3263. // --------------------------------------------------------------------------------------------------------
  3264. static const PluginInfo* _fill_plugin_info_with_presets(LilvWorld* const w, PluginInfo& info, const std::string& uri)
  3265. {
  3266. if (std::find(PLUGINStoReload.begin(), PLUGINStoReload.end(), uri) == PLUGINStoReload.end())
  3267. return &info;
  3268. PLUGINStoReload.remove(uri);
  3269. LilvNode* const node = lilv_new_uri(w, uri.c_str());
  3270. if (node == nullptr)
  3271. return &info;
  3272. const LilvPlugin* const p = lilv_plugins_get_by_uri(PLUGINS, node);
  3273. lilv_node_free(node);
  3274. if (p == nullptr)
  3275. return &info;
  3276. if (info.presets != nullptr)
  3277. {
  3278. for (int i=0; info.presets[i].valid; ++i)
  3279. {
  3280. free((void*)info.presets[i].uri);
  3281. free((void*)info.presets[i].label);
  3282. if (info.presets[i].path != nc)
  3283. free((void*)info.presets[i].path);
  3284. }
  3285. delete[] info.presets;
  3286. info.presets = nullptr;
  3287. }
  3288. LilvNode* const pset_Preset = lilv_new_uri(w, LV2_PRESETS__Preset);
  3289. LilvNode* const rdfs_label = lilv_new_uri(w, LILV_NS_RDFS "label");
  3290. _place_preset_info(w, info, p, pset_Preset, rdfs_label);
  3291. lilv_node_free(pset_Preset);
  3292. lilv_node_free(rdfs_label);
  3293. return &info;
  3294. }
  3295. // --------------------------------------------------------------------------------------------------------
  3296. void init(void)
  3297. {
  3298. lilv_world_free(W);
  3299. W = lilv_world_new();
  3300. lilv_world_load_all(W);
  3301. NamespaceDefinitions::getStaticInstance(W).init(W);
  3302. _refresh();
  3303. }
  3304. void cleanup(void)
  3305. {
  3306. if (_add_remove_bundles_ret != nullptr)
  3307. {
  3308. for (int i=0; _add_remove_bundles_ret[i] != nullptr; ++i)
  3309. free((void*)_add_remove_bundles_ret[i]);
  3310. delete[] _add_remove_bundles_ret;
  3311. _add_remove_bundles_ret = nullptr;
  3312. }
  3313. if (_get_broken_pedals_ret != nullptr)
  3314. {
  3315. for (int i=0; _get_broken_pedals_ret[i] != nullptr; ++i)
  3316. free((void*)_get_broken_pedals_ret[i]);
  3317. delete[] _get_broken_pedals_ret;
  3318. _get_broken_pedals_ret = nullptr;
  3319. }
  3320. if (_get_plug_list_ret != nullptr)
  3321. {
  3322. delete[] _get_plug_list_ret;
  3323. _get_plug_list_ret = nullptr;
  3324. }
  3325. if (_get_plugs_mini_ret != nullptr)
  3326. {
  3327. delete[] _get_plugs_mini_ret;
  3328. _get_plugs_mini_ret = nullptr;
  3329. }
  3330. _get_plug_list_lastsize = 0;
  3331. _get_plugs_mini_lastsize = 0;
  3332. PLUGINS = nullptr;
  3333. BUNDLES.clear();
  3334. for (auto& map : PLUGNFO_Mini)
  3335. {
  3336. if (const PluginInfo_Mini* const info = map.second)
  3337. _clear_plugin_info_mini(info);
  3338. }
  3339. for (auto& map : PLUGNFO)
  3340. {
  3341. PluginInfo& info = map.second;
  3342. _clear_plugin_info(info);
  3343. }
  3344. PLUGNFO_Mini.clear();
  3345. PLUGNFO.clear();
  3346. NamespaceDefinitions::getStaticInstance(W).cleanup();
  3347. lilv_world_free(W);
  3348. W = nullptr;
  3349. if (_file_uri_parse_ret != nullptr)
  3350. {
  3351. free(_file_uri_parse_ret);
  3352. _file_uri_parse_ret = nullptr;
  3353. }
  3354. _clear_pedalboards(_get_pedals_mini_ret);
  3355. _get_pedals_mini_ret = nullptr;
  3356. _clear_pedalboards(FACTORYINFO);
  3357. FACTORYINFO = nullptr;
  3358. if (_get_pedal_info_ret != nullptr)
  3359. {
  3360. _clear_pedalboard_info(*_get_pedal_info_ret);
  3361. _get_pedal_info_ret = nullptr;
  3362. }
  3363. _clear_pedalboard_plugin_values();
  3364. _clear_state_values();
  3365. }
  3366. // --------------------------------------------------------------------------------------------------------
  3367. bool is_bundle_loaded(const char* const bundle)
  3368. {
  3369. // lilv wants the last character as the separator
  3370. char tmppath[PATH_MAX+2];
  3371. char* cbundlepath = realpath(bundle, tmppath);
  3372. if (cbundlepath == nullptr)
  3373. return false;
  3374. {
  3375. const size_t size = strlen(cbundlepath);
  3376. if (size <= 1)
  3377. return false;
  3378. if (cbundlepath[size] != OS_SEP)
  3379. {
  3380. cbundlepath[size ] = OS_SEP;
  3381. cbundlepath[size+1] = '\0';
  3382. }
  3383. }
  3384. std::string bundlepath(cbundlepath);
  3385. return (std::find(BUNDLES.begin(), BUNDLES.end(), bundlepath) != BUNDLES.end());
  3386. }
  3387. const char* const* add_bundle_to_lilv_world(const char* const bundle)
  3388. {
  3389. #ifdef HAVE_NEW_LILV
  3390. // lilv wants the last character as the separator
  3391. char tmppath[PATH_MAX+2];
  3392. char* cbundlepath = realpath(bundle, tmppath);
  3393. if (cbundlepath == nullptr)
  3394. return nullptr;
  3395. {
  3396. const size_t size = strlen(cbundlepath);
  3397. if (size <= 1)
  3398. return nullptr;
  3399. if (cbundlepath[size] != OS_SEP)
  3400. {
  3401. cbundlepath[size ] = OS_SEP;
  3402. cbundlepath[size+1] = '\0';
  3403. }
  3404. }
  3405. std::string bundlepath(cbundlepath);
  3406. // stop now if bundle is already loaded
  3407. if (std::find(BUNDLES.begin(), BUNDLES.end(), bundlepath) != BUNDLES.end())
  3408. return nullptr;
  3409. LilvWorld* const w = W;
  3410. // convert bundle string into a lilv node
  3411. LilvNode* const bundlenode = lilv_new_file_uri(w, nullptr, cbundlepath);
  3412. // load the bundle
  3413. lilv_world_load_bundle(w, bundlenode);
  3414. // free bundlenode, no longer needed
  3415. lilv_node_free(bundlenode);
  3416. // refresh PLUGINS
  3417. PLUGINS = lilv_world_get_all_plugins(w);
  3418. // add to loaded list
  3419. BUNDLES.push_back(bundlepath);
  3420. // fill in for any new plugins that appeared
  3421. std::vector<const char*> addedPlugins;
  3422. // check plugins provided by this bundle
  3423. if (LilvWorld* const w2 = lilv_world_new())
  3424. {
  3425. #ifdef HAVE_NEW_LILV
  3426. lilv_world_load_specifications(w2);
  3427. lilv_world_load_plugin_classes(w2);
  3428. #endif
  3429. LilvNode* const b = lilv_new_file_uri(w2, nullptr, cbundlepath);
  3430. lilv_world_load_bundle(w2, b);
  3431. lilv_node_free(b);
  3432. const LilvPlugins* const plugins = lilv_world_get_all_plugins(w2);
  3433. LILV_FOREACH(plugins, itpls, plugins)
  3434. {
  3435. const LilvPlugin* const p = lilv_plugins_get(plugins, itpls);
  3436. const std::string uri = lilv_node_as_uri(lilv_plugin_get_uri(p));
  3437. if (std::find(BLACKLIST.begin(), BLACKLIST.end(), uri) != BLACKLIST.end())
  3438. continue;
  3439. // store new empty data
  3440. PLUGNFO[uri] = PluginInfo_Init;
  3441. PLUGNFO_Mini[uri] = nullptr;
  3442. addedPlugins.push_back(strdup(uri.c_str()));
  3443. }
  3444. lilv_world_free(w2);
  3445. }
  3446. if (size_t plugCount = addedPlugins.size())
  3447. {
  3448. if (_add_remove_bundles_ret != nullptr)
  3449. {
  3450. for (int i=0; _add_remove_bundles_ret[i] != nullptr; ++i)
  3451. free((void*)_add_remove_bundles_ret[i]);
  3452. delete[] _add_remove_bundles_ret;
  3453. }
  3454. _add_remove_bundles_ret = new const char*[plugCount+1];
  3455. plugCount = 0;
  3456. for (const char* uri : addedPlugins)
  3457. _add_remove_bundles_ret[plugCount++] = uri;
  3458. _add_remove_bundles_ret[plugCount] = nullptr;
  3459. return _add_remove_bundles_ret;
  3460. }
  3461. #endif
  3462. return nullptr;
  3463. #ifndef HAVE_NEW_LILV
  3464. // unused
  3465. (void)bundle;
  3466. #endif
  3467. }
  3468. const char* const* remove_bundle_from_lilv_world(const char* const bundle, const char* const resource)
  3469. {
  3470. #ifdef HAVE_NEW_LILV
  3471. // lilv wants the last character as the separator
  3472. char tmppath[PATH_MAX+2];
  3473. char* cbundlepath = realpath(bundle, tmppath);
  3474. if (cbundlepath == nullptr)
  3475. return nullptr;
  3476. {
  3477. const size_t size = strlen(cbundlepath);
  3478. if (size <= 1)
  3479. return nullptr;
  3480. if (cbundlepath[size] != OS_SEP)
  3481. {
  3482. cbundlepath[size ] = OS_SEP;
  3483. cbundlepath[size+1] = '\0';
  3484. }
  3485. }
  3486. std::string bundlepath(cbundlepath);
  3487. // stop now if bundle is not loaded
  3488. if (std::find(BUNDLES.begin(), BUNDLES.end(), bundlepath) == BUNDLES.end())
  3489. return nullptr;
  3490. LilvWorld* const w = W;
  3491. // unload resource if requested
  3492. if (resource != NULL && resource[0] != '\0')
  3493. {
  3494. if (LilvNode* const resourcenode = lilv_new_uri(w, resource))
  3495. {
  3496. lilv_world_unload_resource(w, resourcenode);
  3497. lilv_node_free(resourcenode);
  3498. }
  3499. }
  3500. // remove from loaded list
  3501. BUNDLES.remove(bundlepath);
  3502. std::vector<const char*> removedPlugins;
  3503. // remove all plugins that are present on that bundle
  3504. LILV_FOREACH(plugins, itpls, PLUGINS)
  3505. {
  3506. const LilvPlugin* const p = lilv_plugins_get(PLUGINS, itpls);
  3507. const std::string uri = lilv_node_as_uri(lilv_plugin_get_uri(p));
  3508. if (! contains(PLUGNFO, uri))
  3509. continue;
  3510. const LilvNodes* const bundles = lilv_plugin_get_data_uris(p);
  3511. LILV_FOREACH(nodes, itbnds, bundles)
  3512. {
  3513. const LilvNode* const bundlenode = lilv_nodes_get(bundles, itbnds);
  3514. if (bundlenode == nullptr)
  3515. continue;
  3516. if (! lilv_node_is_uri(bundlenode))
  3517. continue;
  3518. char* bundleparsed;
  3519. char* tmp;
  3520. tmp = lilv_file_uri_parse2(lilv_node_as_uri(bundlenode), nullptr);
  3521. if (tmp == nullptr)
  3522. continue;
  3523. bundleparsed = dirname(tmp);
  3524. if (bundleparsed == nullptr)
  3525. {
  3526. lilv_free(tmp);
  3527. continue;
  3528. }
  3529. bundleparsed = realpath(bundleparsed, tmppath); // note: this invalidates cbundlepath
  3530. lilv_free(tmp);
  3531. if (bundleparsed == nullptr)
  3532. continue;
  3533. const size_t size = strlen(bundleparsed);
  3534. if (size <= 1)
  3535. continue;
  3536. if (bundleparsed[size] != OS_SEP)
  3537. {
  3538. bundleparsed[size ] = OS_SEP;
  3539. bundleparsed[size+1] = '\0';
  3540. }
  3541. if (bundlepath != bundleparsed)
  3542. continue;
  3543. _clear_plugin_info(PLUGNFO[uri]);
  3544. PLUGNFO.erase(uri);
  3545. if (const PluginInfo_Mini* const info = PLUGNFO_Mini[uri])
  3546. _clear_plugin_info_mini(info);
  3547. PLUGNFO_Mini.erase(uri);
  3548. removedPlugins.push_back(strdup(uri.c_str()));
  3549. break;
  3550. }
  3551. }
  3552. // convert bundle string into a lilv node
  3553. LilvNode* const bundlenode = lilv_new_file_uri(w, nullptr, bundlepath.c_str());
  3554. // unload the bundle
  3555. lilv_world_unload_bundle(w, bundlenode);
  3556. // free bundlenode, no longer needed
  3557. lilv_node_free(bundlenode);
  3558. // refresh PLUGINS
  3559. PLUGINS = lilv_world_get_all_plugins(w);
  3560. if (size_t plugCount = removedPlugins.size())
  3561. {
  3562. if (_add_remove_bundles_ret != nullptr)
  3563. {
  3564. for (int i=0; _add_remove_bundles_ret[i] != nullptr; ++i)
  3565. free((void*)_add_remove_bundles_ret[i]);
  3566. delete[] _add_remove_bundles_ret;
  3567. }
  3568. _add_remove_bundles_ret = new const char*[plugCount+1];
  3569. plugCount = 0;
  3570. for (const char* uri : removedPlugins)
  3571. _add_remove_bundles_ret[plugCount++] = uri;
  3572. _add_remove_bundles_ret[plugCount] = nullptr;
  3573. // force get_plugin_list/get_all_plugins to reload info next time
  3574. _get_plug_list_lastsize = -1;
  3575. _get_plugs_mini_lastsize = -1;
  3576. return _add_remove_bundles_ret;
  3577. }
  3578. #endif
  3579. return nullptr;
  3580. #ifndef HAVE_NEW_LILV
  3581. // unused
  3582. (void)bundle;
  3583. #endif
  3584. }
  3585. const char* const* get_plugin_list(void)
  3586. {
  3587. const int newsize = (int)lilv_plugins_size(PLUGINS);
  3588. if (newsize == 0)
  3589. {
  3590. if (_get_plug_list_ret != nullptr)
  3591. {
  3592. delete[] _get_plug_list_ret;
  3593. _get_plug_list_ret = nullptr;
  3594. }
  3595. _get_plug_list_lastsize = 0;
  3596. return nullptr;
  3597. }
  3598. if (newsize > _get_plug_list_lastsize)
  3599. {
  3600. _get_plug_list_lastsize = newsize;
  3601. if (_get_plug_list_ret != nullptr)
  3602. delete[] _get_plug_list_ret;
  3603. _get_plug_list_ret = new const char*[newsize+1];
  3604. memset(_get_plug_list_ret, 0, sizeof(void*) * (newsize+1));
  3605. }
  3606. else if (newsize < _get_plug_list_lastsize)
  3607. {
  3608. memset(_get_plug_list_ret, 0, sizeof(void*) * (newsize+1));
  3609. }
  3610. int curIndex = 0;
  3611. LILV_FOREACH(plugins, itpls, PLUGINS)
  3612. {
  3613. if (curIndex >= newsize)
  3614. break;
  3615. const LilvPlugin* const p = lilv_plugins_get(PLUGINS, itpls);
  3616. const char* const uri = lilv_node_as_uri(lilv_plugin_get_uri(p));
  3617. const std::string uri2(uri);
  3618. if (std::find(BLACKLIST.begin(), BLACKLIST.end(), uri2) != BLACKLIST.end())
  3619. continue;
  3620. _get_plug_list_ret[curIndex++] = uri;
  3621. }
  3622. return _get_plug_list_ret;
  3623. }
  3624. const PluginInfo_Mini* const* get_all_plugins(void)
  3625. {
  3626. const int newsize = (int)lilv_plugins_size(PLUGINS);
  3627. if (newsize == 0)
  3628. {
  3629. if (_get_plugs_mini_ret != nullptr)
  3630. {
  3631. delete[] _get_plugs_mini_ret;
  3632. _get_plugs_mini_ret = nullptr;
  3633. }
  3634. _get_plugs_mini_lastsize = 0;
  3635. return nullptr;
  3636. }
  3637. if (newsize > _get_plugs_mini_lastsize)
  3638. {
  3639. _get_plugs_mini_lastsize = newsize;
  3640. if (_get_plugs_mini_ret != nullptr)
  3641. delete[] _get_plugs_mini_ret;
  3642. _get_plugs_mini_ret = new const PluginInfo_Mini*[newsize+1];
  3643. memset(_get_plugs_mini_ret, 0, sizeof(void*) * (newsize+1));
  3644. }
  3645. else if (newsize < _get_plugs_mini_lastsize)
  3646. {
  3647. memset(_get_plugs_mini_ret, 0, sizeof(void*) * (newsize+1));
  3648. }
  3649. LilvWorld* const w = W;
  3650. const NamespaceDefinitions& ns(NamespaceDefinitions::getStaticInstance(w));
  3651. int curIndex = 0;
  3652. LILV_FOREACH(plugins, itpls, PLUGINS)
  3653. {
  3654. if (curIndex >= newsize)
  3655. break;
  3656. const LilvPlugin* const p = lilv_plugins_get(PLUGINS, itpls);
  3657. const std::string uri = lilv_node_as_uri(lilv_plugin_get_uri(p));
  3658. if (std::find(BLACKLIST.begin(), BLACKLIST.end(), uri) != BLACKLIST.end())
  3659. continue;
  3660. // check if it's already cached
  3661. if (! contains(PLUGNFO_Mini, uri) || PLUGNFO_Mini[uri] == nullptr)
  3662. PLUGNFO_Mini[uri] = _get_plugin_info_mini(w, p, ns);
  3663. if (const PluginInfo_Mini* const miniInfo = PLUGNFO_Mini[uri])
  3664. {
  3665. if (kOnlyShowPluginsWithMODGUI && miniInfo->gui.resourcesDirectory == nc)
  3666. continue;
  3667. _get_plugs_mini_ret[curIndex++] = PLUGNFO_Mini[uri];
  3668. }
  3669. }
  3670. return _get_plugs_mini_ret;
  3671. }
  3672. const PluginInfo* get_plugin_info(const char* const uri_)
  3673. {
  3674. const std::string uri = uri_;
  3675. // check if it exists
  3676. if (! contains(PLUGNFO, uri))
  3677. return nullptr;
  3678. LilvWorld* const w = W;
  3679. // check if it's already cached
  3680. if (PLUGNFO[uri].valid)
  3681. return _fill_plugin_info_with_presets(w, PLUGNFO[uri], uri);
  3682. LilvNode* const urinode = lilv_new_uri(w, uri_);
  3683. if (urinode == nullptr)
  3684. return nullptr;
  3685. const LilvPlugin* const p = lilv_plugins_get_by_uri(PLUGINS, urinode);
  3686. lilv_node_free(urinode);
  3687. if (p != nullptr)
  3688. {
  3689. const NamespaceDefinitions& ns(NamespaceDefinitions::getStaticInstance(w));
  3690. const PluginInfo& pInfo(_get_plugin_info(w, p, ns));
  3691. PLUGNFO[uri] = pInfo;
  3692. _fill_plugin_info_mini_from_full(pInfo, &PLUGNFO_Mini[uri]);
  3693. return &PLUGNFO[uri];
  3694. }
  3695. return nullptr;
  3696. }
  3697. const NonCachedPluginInfo* get_non_cached_plugin_info(const char* uri)
  3698. {
  3699. const PluginInfo* const info = get_plugin_info(uri);
  3700. if (info == nullptr || ! info->valid)
  3701. return nullptr;
  3702. static NonCachedPluginInfo ncInfo;
  3703. ncInfo.licensed = info->licensed;
  3704. ncInfo.presets = info->presets;
  3705. return &ncInfo;
  3706. }
  3707. const PluginGUI* get_plugin_gui(const char* uri_)
  3708. {
  3709. const std::string uri = uri_;
  3710. // check if it exists
  3711. if (! contains(PLUGNFO, uri))
  3712. return nullptr;
  3713. // check if it's already cached
  3714. if (PLUGNFO[uri].valid)
  3715. return &PLUGNFO[uri].gui;
  3716. LilvWorld* const w = W;
  3717. LilvNode* const urinode = lilv_new_uri(w, uri_);
  3718. if (urinode == nullptr)
  3719. return nullptr;
  3720. const LilvPlugin* const p = lilv_plugins_get_by_uri(PLUGINS, urinode);
  3721. lilv_node_free(urinode);
  3722. if (p != nullptr)
  3723. {
  3724. const NamespaceDefinitions& ns(NamespaceDefinitions::getStaticInstance(w));
  3725. const PluginInfo& pInfo(_get_plugin_info(w, p, ns));
  3726. PLUGNFO[uri] = pInfo;
  3727. _fill_plugin_info_mini_from_full(pInfo, &PLUGNFO_Mini[uri]);
  3728. return &PLUGNFO[uri].gui;
  3729. }
  3730. // not found
  3731. return nullptr;
  3732. }
  3733. const PluginGUI_Mini* get_plugin_gui_mini(const char* uri_)
  3734. {
  3735. const std::string uri = uri_;
  3736. // check if it exists
  3737. if (! contains(PLUGNFO_Mini, uri))
  3738. return nullptr;
  3739. // check if it's already cached
  3740. if (const PluginInfo_Mini* const miniInfo = PLUGNFO_Mini[uri])
  3741. return &miniInfo->gui;
  3742. LilvWorld* const w = W;
  3743. const NamespaceDefinitions& ns(NamespaceDefinitions::getStaticInstance(w));
  3744. // look for it
  3745. LILV_FOREACH(plugins, itpls, PLUGINS)
  3746. {
  3747. const LilvPlugin* const p = lilv_plugins_get(PLUGINS, itpls);
  3748. std::string uri2 = lilv_node_as_uri(lilv_plugin_get_uri(p));
  3749. if (uri2 != uri)
  3750. continue;
  3751. // found it
  3752. printf("NOTICE: Plugin '%s' was not (small) cached, scanning it now...\n", uri_);
  3753. if (const PluginInfo_Mini* const miniInfo = _get_plugin_info_mini(w, p, ns))
  3754. {
  3755. PLUGNFO_Mini[uri] = miniInfo;
  3756. return &miniInfo->gui;
  3757. }
  3758. // error
  3759. return nullptr;
  3760. }
  3761. // not found
  3762. return nullptr;
  3763. }
  3764. // --------------------------------------------------------------------------------------------------------
  3765. const PluginPort* get_plugin_control_inputs(const char* const uri_)
  3766. {
  3767. const std::string uri = uri_;
  3768. // check if plugin exists
  3769. if (! contains(PLUGNFO, uri))
  3770. return nullptr;
  3771. // return right-away if already cached
  3772. if (PLUGNFO[uri].valid)
  3773. {
  3774. const PluginInfo& pInfo = PLUGNFO[uri];
  3775. return pInfo.ports.control.input;
  3776. }
  3777. LilvWorld* const w = W;
  3778. // look for it
  3779. LILV_FOREACH(plugins, itpls, PLUGINS)
  3780. {
  3781. const LilvPlugin* const p = lilv_plugins_get(PLUGINS, itpls);
  3782. const char* const uri2 = lilv_node_as_uri(lilv_plugin_get_uri(p));
  3783. if (uri != uri2)
  3784. continue;
  3785. // found the plugin
  3786. const NamespaceDefinitions& ns(NamespaceDefinitions::getStaticInstance(w));
  3787. const PluginInfo& pInfo = _get_plugin_info(w, p, ns);
  3788. PLUGNFO[uri] = pInfo;
  3789. _fill_plugin_info_mini_from_full(pInfo, &PLUGNFO_Mini[uri]);
  3790. return pInfo.ports.control.input;
  3791. }
  3792. // plugin not found
  3793. return nullptr;
  3794. }
  3795. const PluginInfo_Essentials* get_plugin_info_essentials(const char* const uri_)
  3796. {
  3797. static PluginInfo_Essentials info;
  3798. const std::string uri = uri_;
  3799. // check if plugin exists
  3800. if (! contains(PLUGNFO, uri))
  3801. return nullptr;
  3802. // check if plugin is already cached
  3803. if (PLUGNFO[uri].valid)
  3804. {
  3805. const PluginInfo& pInfo = PLUGNFO[uri];
  3806. info.controlInputs = pInfo.ports.control.input;
  3807. info.monitoredOutputs = pInfo.gui.monitoredOutputs;
  3808. info.parameters = pInfo.parameters;
  3809. info.buildEnvironment = pInfo.buildEnvironment;
  3810. info.microVersion = pInfo.microVersion;
  3811. info.minorVersion = pInfo.minorVersion;
  3812. info.release = pInfo.release;
  3813. info.builder = pInfo.builder;
  3814. return &info;
  3815. }
  3816. LilvWorld* const w = W;
  3817. // look for it
  3818. LILV_FOREACH(plugins, itpls, PLUGINS)
  3819. {
  3820. const LilvPlugin* const p = lilv_plugins_get(PLUGINS, itpls);
  3821. const char* const uri2 = lilv_node_as_uri(lilv_plugin_get_uri(p));
  3822. if (uri != uri2)
  3823. continue;
  3824. // found the plugin
  3825. const NamespaceDefinitions& ns(NamespaceDefinitions::getStaticInstance(w));
  3826. const PluginInfo& pInfo = _get_plugin_info(w, p, ns);
  3827. PLUGNFO[uri] = pInfo;
  3828. _fill_plugin_info_mini_from_full(pInfo, &PLUGNFO_Mini[uri]);
  3829. info.controlInputs = pInfo.ports.control.input;
  3830. info.monitoredOutputs = pInfo.gui.monitoredOutputs;
  3831. info.parameters = pInfo.parameters;
  3832. info.buildEnvironment = pInfo.buildEnvironment;
  3833. info.microVersion = pInfo.microVersion;
  3834. info.minorVersion = pInfo.minorVersion;
  3835. info.release = pInfo.release;
  3836. info.builder = pInfo.builder;
  3837. return &info;
  3838. }
  3839. // plugin not found
  3840. return nullptr;
  3841. }
  3842. // --------------------------------------------------------------------------------------------------------
  3843. // check if a plugin preset is valid (must exist)
  3844. bool is_plugin_preset_valid(const char* const plugin_, const char* const preset)
  3845. {
  3846. const std::string plugin(plugin_);
  3847. // check if plugin exists
  3848. if (! contains(PLUGNFO, plugin))
  3849. return false;
  3850. // plugin must have been cached before
  3851. if (! PLUGNFO[plugin].valid)
  3852. return false;
  3853. const PluginInfo& pInfo = PLUGNFO[plugin];
  3854. if (pInfo.presets == nullptr)
  3855. return false;
  3856. for (int i=0;; ++i)
  3857. {
  3858. const PluginPreset& prInfo(pInfo.presets[i]);
  3859. if (! prInfo.valid)
  3860. return false;
  3861. if (strcmp(prInfo.uri, preset) == 0)
  3862. return true;
  3863. }
  3864. return false;
  3865. }
  3866. // trigger a preset rescan for a plugin the next time it's loaded
  3867. void rescan_plugin_presets(const char* const uri_)
  3868. {
  3869. const std::string uri(uri_);
  3870. if (std::find(PLUGINStoReload.begin(), PLUGINStoReload.end(), uri) == PLUGINStoReload.end())
  3871. PLUGINStoReload.push_back(uri);
  3872. }
  3873. // --------------------------------------------------------------------------------------------------------
  3874. const PedalboardInfo_Mini* const* get_all_pedalboards(const int ptype)
  3875. {
  3876. // factory pedalboards do not change, so return cached value directly
  3877. if (ptype == kPedalboardInfoFactoryOnly && FACTORYINFO != nullptr)
  3878. return FACTORYINFO;
  3879. #ifndef LILV_OPTION_LV2_PATH
  3880. char* const oldlv2path = getenv_strdup_or_null("LV2_PATH");
  3881. #endif
  3882. const char* pedalboard_lv2_path;
  3883. switch (ptype)
  3884. {
  3885. case kPedalboardInfoUserOnly:
  3886. if (const char* const pbdir = getenv("MOD_USER_PEDALBOARDS_DIR"))
  3887. pedalboard_lv2_path = pbdir;
  3888. else
  3889. pedalboard_lv2_path = "~/.pedalboards";
  3890. break;
  3891. case kPedalboardInfoFactoryOnly:
  3892. if (FACTORY_PEDALBOARDS_DIRlen != 0)
  3893. pedalboard_lv2_path = FACTORY_PEDALBOARDS_DIR;
  3894. else
  3895. pedalboard_lv2_path = "/usr/share/mod/pedalboards";
  3896. break;
  3897. default:
  3898. pedalboard_lv2_path = _get_lv2_pedalboards_path();
  3899. break;
  3900. }
  3901. LilvWorld* const w = lilv_world_new();
  3902. #ifdef LILV_OPTION_LV2_PATH
  3903. LilvNode* const lv2path = lilv_new_string(w, pedalboard_lv2_path);
  3904. lilv_world_set_option(w, LILV_OPTION_LV2_PATH, lv2path);
  3905. lilv_free(lv2path);
  3906. #else
  3907. setenv("LV2_PATH", pedalboard_lv2_path, 1);
  3908. #endif
  3909. lilv_world_load_all(w);
  3910. #ifndef LILV_OPTION_LV2_PATH
  3911. if (oldlv2path != nullptr)
  3912. {
  3913. setenv("LV2_PATH", oldlv2path, 1);
  3914. free(oldlv2path);
  3915. }
  3916. else
  3917. {
  3918. unsetenv("LV2_PATH");
  3919. }
  3920. #endif
  3921. LilvNode* const versiontypenode = lilv_new_uri(w, LILV_NS_MODPEDAL "version");
  3922. LilvNode* const rdftypenode = lilv_new_uri(w, LILV_NS_RDF "type");
  3923. LilvNode* const ingenblocknode = lilv_new_uri(w, LILV_NS_INGEN "block");
  3924. LilvNode* const lv2protonode = lilv_new_uri(w, LILV_NS_LV2 "prototype");
  3925. const LilvPlugins* const plugins = lilv_world_get_all_plugins(w);
  3926. std::vector<const PedalboardInfo_Mini*> allpedals;
  3927. allpedals.reserve(lilv_plugins_size(plugins));
  3928. LILV_FOREACH(plugins, itpls, plugins)
  3929. {
  3930. const LilvPlugin* const p = lilv_plugins_get(plugins, itpls);
  3931. if (const PedalboardInfo_Mini* info = _get_pedalboard_info_mini(w, p,
  3932. versiontypenode, rdftypenode, ingenblocknode, lv2protonode,
  3933. static_cast<PedalboardInfoType>(ptype)))
  3934. {
  3935. allpedals.push_back(info);
  3936. }
  3937. }
  3938. lilv_free(versiontypenode);
  3939. lilv_free(rdftypenode);
  3940. lilv_free(ingenblocknode);
  3941. lilv_free(lv2protonode);
  3942. lilv_world_free(w);
  3943. if (size_t pbcount = allpedals.size())
  3944. {
  3945. const PedalboardInfo_Mini** pedals = new const PedalboardInfo_Mini*[pbcount+1];
  3946. pbcount = 0;
  3947. for (const PedalboardInfo_Mini* info : allpedals)
  3948. pedals[pbcount++] = info;
  3949. pedals[pbcount] = nullptr;
  3950. if (ptype == kPedalboardInfoFactoryOnly)
  3951. {
  3952. FACTORYINFO = pedals;
  3953. return pedals;
  3954. }
  3955. _clear_pedalboards(_get_pedals_mini_ret);
  3956. _get_pedals_mini_ret = pedals;
  3957. return _get_pedals_mini_ret;
  3958. }
  3959. return nullptr;
  3960. }
  3961. const char* const* get_broken_pedalboards(void)
  3962. {
  3963. LilvWorld* const w = lilv_world_new();
  3964. #ifdef LILV_OPTION_LV2_PATH
  3965. LilvNode* const lv2path = lilv_new_string(w, _get_lv2_pedalboards_path());
  3966. lilv_world_set_option(w, LILV_OPTION_LV2_PATH, lv2path);
  3967. lilv_free(lv2path);
  3968. #else
  3969. char* const oldlv2path = getenv_strdup_or_null("LV2_PATH");
  3970. setenv("LV2_PATH", _get_lv2_pedalboards_path(), 1);
  3971. #endif
  3972. lilv_world_load_all(w);
  3973. #ifndef LILV_OPTION_LV2_PATH
  3974. if (oldlv2path != nullptr)
  3975. {
  3976. setenv("LV2_PATH", oldlv2path, 1);
  3977. free(oldlv2path);
  3978. }
  3979. else
  3980. {
  3981. unsetenv("LV2_PATH");
  3982. }
  3983. #endif
  3984. LilvNode* const ingenblocknode = lilv_new_uri(w, LILV_NS_INGEN "block");
  3985. LilvNode* const lv2protonode = lilv_new_uri(w, LILV_NS_LV2 "prototype");
  3986. const LilvPlugins* const plugins = lilv_world_get_all_plugins(w);
  3987. std::vector<const char*> brokenpedals;
  3988. brokenpedals.reserve(lilv_plugins_size(plugins));
  3989. LILV_FOREACH(plugins, itpls, plugins)
  3990. {
  3991. const LilvPlugin* const p = lilv_plugins_get(plugins, itpls);
  3992. // get new info
  3993. if (_is_pedalboard_broken(p, w, ingenblocknode, lv2protonode))
  3994. brokenpedals.push_back(strdup(lilv_node_as_uri(lilv_plugin_get_uri(p))));
  3995. }
  3996. lilv_free(ingenblocknode);
  3997. lilv_free(lv2protonode);
  3998. lilv_world_free(w);
  3999. if (size_t pbcount = brokenpedals.size())
  4000. {
  4001. if (_get_broken_pedals_ret != nullptr)
  4002. {
  4003. for (int i=0; _get_broken_pedals_ret[i] != nullptr; ++i)
  4004. free((void*)_get_broken_pedals_ret[i]);
  4005. delete[] _get_broken_pedals_ret;
  4006. }
  4007. _get_broken_pedals_ret = new const char*[pbcount+1];
  4008. pbcount = 0;
  4009. for (const char* pedal : brokenpedals)
  4010. _get_broken_pedals_ret[pbcount++] = pedal;
  4011. _get_broken_pedals_ret[pbcount] = nullptr;
  4012. return _get_broken_pedals_ret;
  4013. }
  4014. return nullptr;
  4015. }
  4016. // value of predicate on subject, or of legacyPredicate when predicate is absent
  4017. static LilvNode* lilv_world_get_with_fallback(LilvWorld* const w, const LilvNode* const subject,
  4018. const LilvNode* const predicate, const LilvNode* const legacyPredicate)
  4019. {
  4020. if (LilvNode* const node = lilv_world_get(w, subject, predicate, nullptr))
  4021. return node;
  4022. return lilv_world_get(w, subject, legacyPredicate, nullptr);
  4023. }
  4024. const PedalboardInfo* get_pedalboard_info(const char* const bundle)
  4025. {
  4026. static PedalboardInfo info;
  4027. size_t bundlepathsize;
  4028. const char* const bundlepath = _get_safe_bundlepath(bundle, bundlepathsize);
  4029. if (bundlepath == nullptr)
  4030. return nullptr;
  4031. LilvWorld* const w = lilv_world_new();
  4032. #ifdef HAVE_NEW_LILV
  4033. lilv_world_load_specifications(w);
  4034. lilv_world_load_plugin_classes(w);
  4035. #endif
  4036. LilvNode* const b = lilv_new_file_uri(w, nullptr, bundlepath);
  4037. lilv_world_load_bundle(w, b);
  4038. lilv_node_free(b);
  4039. const LilvPlugins* const plugins = lilv_world_get_all_plugins(w);
  4040. if (lilv_plugins_size(plugins) != 1)
  4041. {
  4042. lilv_world_free(w);
  4043. return nullptr;
  4044. }
  4045. const LilvPlugin* p = nullptr;
  4046. LILV_FOREACH(plugins, itpls, plugins) {
  4047. p = lilv_plugins_get(plugins, itpls);
  4048. break;
  4049. }
  4050. if (p == nullptr)
  4051. {
  4052. lilv_world_free(w);
  4053. return nullptr;
  4054. }
  4055. bool isPedalboard = false;
  4056. LilvNode* const rdftypenode = lilv_new_uri(w, LILV_NS_RDF "type");
  4057. if (LilvNodes* const nodes = lilv_plugin_get_value(p, rdftypenode))
  4058. {
  4059. LILV_FOREACH(nodes, it, nodes)
  4060. {
  4061. const LilvNode* const node = lilv_nodes_get(nodes, it);
  4062. if (const char* const nodestr = lilv_node_as_string(node))
  4063. {
  4064. if (strcmp(nodestr, LILV_NS_MODPEDAL "Pedalboard") == 0)
  4065. {
  4066. isPedalboard = true;
  4067. break;
  4068. }
  4069. }
  4070. }
  4071. lilv_nodes_free(nodes);
  4072. }
  4073. if (! isPedalboard)
  4074. {
  4075. lilv_node_free(rdftypenode);
  4076. lilv_world_free(w);
  4077. return nullptr;
  4078. }
  4079. if (_get_pedal_info_ret != nullptr)
  4080. {
  4081. _clear_pedalboard_info(*_get_pedal_info_ret);
  4082. _get_pedal_info_ret = nullptr;
  4083. }
  4084. memset(&info, 0, sizeof(PedalboardInfo));
  4085. info.midi_separated_mode = true;
  4086. // define the needed stuff
  4087. LilvNode* const ingen_arc = lilv_new_uri(w, LILV_NS_INGEN "arc");
  4088. LilvNode* const ingen_block = lilv_new_uri(w, LILV_NS_INGEN "block");
  4089. LilvNode* const ingen_canvasX = lilv_new_uri(w, LILV_NS_INGEN "canvasX");
  4090. LilvNode* const ingen_canvasY = lilv_new_uri(w, LILV_NS_INGEN "canvasY");
  4091. LilvNode* const ingen_enabled = lilv_new_uri(w, LILV_NS_INGEN "enabled");
  4092. LilvNode* const ingen_head = lilv_new_uri(w, LILV_NS_INGEN "head");
  4093. LilvNode* const ingen_tail = lilv_new_uri(w, LILV_NS_INGEN "tail");
  4094. LilvNode* const ingen_value = lilv_new_uri(w, LILV_NS_INGEN "value");
  4095. LilvNode* const ingen_poly = lilv_new_uri(w, LILV_NS_INGEN "polyphonic");
  4096. LilvNode* const lv2_maximum = lilv_new_uri(w, LV2_CORE__maximum);
  4097. LilvNode* const lv2_minimum = lilv_new_uri(w, LV2_CORE__minimum);
  4098. LilvNode* const lv2_name = lilv_new_uri(w, LV2_CORE__name);
  4099. LilvNode* const lv2_port = lilv_new_uri(w, LV2_CORE__port);
  4100. LilvNode* const lv2_prototype = lilv_new_uri(w, LV2_CORE__prototype);
  4101. LilvNode* const midi_binding = lilv_new_uri(w, LV2_MIDI__binding);
  4102. LilvNode* const midi_channel = lilv_new_uri(w, LV2_MIDI__channel);
  4103. LilvNode* const midi_controlNum = lilv_new_uri(w, LV2_MIDI__controllerNumber);
  4104. LilvNode* const modpedal_preset = lilv_new_uri(w, LILV_NS_MODPEDAL "preset");
  4105. LilvNode* const modpedal_width = lilv_new_uri(w, LILV_NS_MODPEDAL "width");
  4106. LilvNode* const modpedal_height = lilv_new_uri(w, LILV_NS_MODPEDAL "height");
  4107. LilvNode* const modpedal_version = lilv_new_uri(w, LILV_NS_MODPEDAL "version");
  4108. LilvNode* const modpedal_formatVersion = lilv_new_uri(w, LILV_NS_MODPEDAL "formatVersion");
  4109. LilvNode* const modpedal_instanceNumber = lilv_new_uri(w, LILV_NS_MODPEDAL "instanceNumber");
  4110. LilvNode* const modpedal_performanceVisible = lilv_new_uri(w, LILV_NS_MODPEDAL "performanceVisible");
  4111. LilvNode* const modpedal_performanceIndex = lilv_new_uri(w, LILV_NS_MODPEDAL "performanceIndex");
  4112. LilvNode* const modpedal_instanceLabel = lilv_new_uri(w, LILV_NS_MODPEDAL "instanceLabel");
  4113. // legacy terms written by alabs builds before pedal:formatVersion 2, read only as fallback
  4114. LilvNode* const modperf_visible = lilv_new_uri(w, LILV_NS_MODPERFORMANCE "visible");
  4115. LilvNode* const modperf_index = lilv_new_uri(w, LILV_NS_MODPERFORMANCE "index");
  4116. LilvNode* const mod_label = lilv_new_uri(w, LILV_NS_MOD "label");
  4117. // --------------------------------------------------------------------------------------------------------
  4118. // uri node (ie, "this")
  4119. const LilvNode* const urinode = lilv_plugin_get_uri(p);
  4120. // --------------------------------------------------------------------------------------------------------
  4121. // title
  4122. if (LilvNode* const node = lilv_plugin_get_name(p))
  4123. {
  4124. if (const char* const name = lilv_node_as_string(node))
  4125. info.title = name[0] != '\0' ? strdup(name) : kUntitled;
  4126. else
  4127. info.title = kUntitled;
  4128. lilv_node_free(node);
  4129. }
  4130. else
  4131. {
  4132. info.title = kUntitled;
  4133. }
  4134. // --------------------------------------------------------------------------------------------------------
  4135. // size
  4136. if (LilvNodes* const widthnodes = lilv_plugin_get_value(p, modpedal_width))
  4137. {
  4138. if (LilvNodes* const heightnodes = lilv_plugin_get_value(p, modpedal_height))
  4139. {
  4140. info.width = lilv_node_as_int(lilv_nodes_get_first(widthnodes));
  4141. info.height = lilv_node_as_int(lilv_nodes_get_first(heightnodes));
  4142. lilv_nodes_free(heightnodes);
  4143. }
  4144. lilv_nodes_free(widthnodes);
  4145. }
  4146. // --------------------------------------------------------------------------------------------------------
  4147. // factory
  4148. if (const LilvNode* const bundlenode = lilv_plugin_get_bundle_uri(p))
  4149. {
  4150. if (char* const bundleabspath = lilv_file_abspath(lilv_node_as_string(bundlenode)))
  4151. {
  4152. info.factory = is_factory_pedalboard(bundleabspath);
  4153. free(bundleabspath);
  4154. }
  4155. }
  4156. // --------------------------------------------------------------------------------------------------------
  4157. // plugins
  4158. if (LilvNodes* const blocks = lilv_plugin_get_value(p, ingen_block))
  4159. {
  4160. if (unsigned int count = lilv_nodes_size(blocks))
  4161. {
  4162. PedalboardPlugin* const plugs = new PedalboardPlugin[count+1];
  4163. memset(plugs, 0, sizeof(PedalboardPlugin) * (count+1));
  4164. count = 0;
  4165. LILV_FOREACH(nodes, itblocks, blocks)
  4166. {
  4167. const LilvNode* const block = lilv_nodes_get(blocks, itblocks);
  4168. if (LilvNode* const proto = lilv_world_get(w, block, lv2_prototype, nullptr))
  4169. {
  4170. bool visible = true;
  4171. int perfview_index = -1;
  4172. const char* const uri = lilv_node_as_uri(proto);
  4173. char* instance;
  4174. char* full_instance = lilv_file_uri_parse2(lilv_node_as_string(block), nullptr);
  4175. char* label = nullptr;
  4176. // instance label and performance view settings: modpedal# terms (format 2),
  4177. // falling back to the legacy ones when absent
  4178. if (LilvNode* const node = lilv_world_get_with_fallback(w, block, modpedal_instanceLabel, mod_label))
  4179. {
  4180. label = strdup(lilv_node_as_string(node));
  4181. lilv_node_free(node);
  4182. }
  4183. if (LilvNode* const node = lilv_world_get_with_fallback(w, block, modpedal_performanceVisible, modperf_visible))
  4184. {
  4185. visible = lilv_node_as_bool(node);
  4186. lilv_node_free(node);
  4187. }
  4188. if (LilvNode* const node = lilv_world_get_with_fallback(w, block, modpedal_performanceIndex, modperf_index))
  4189. {
  4190. perfview_index = lilv_node_as_int(node);
  4191. lilv_node_free(node);
  4192. }
  4193. if (strstr(full_instance, bundlepath) != nullptr)
  4194. instance = strdup(full_instance+(bundlepathsize+1));
  4195. else
  4196. instance = strdup(full_instance);
  4197. LilvNode* const enabled = lilv_world_get(w, block, ingen_enabled, nullptr);
  4198. LilvNode* const x = lilv_world_get(w, block, ingen_canvasX, nullptr);
  4199. LilvNode* const y = lilv_world_get(w, block, ingen_canvasY, nullptr);
  4200. LilvNode* const preset = lilv_world_get(w, block, modpedal_preset, nullptr);
  4201. LilvNode* const instId = lilv_world_get(w, block, modpedal_instanceNumber, nullptr);
  4202. PedalboardMidiControl bypassCC = { -1, 0, false, 0.0f, 1.0f };
  4203. PedalboardPluginPort* ports = nullptr;
  4204. if (LilvNodes* const portnodes = lilv_world_find_nodes(w, block, lv2_port, nullptr))
  4205. {
  4206. unsigned int portcount = lilv_nodes_size(portnodes);
  4207. ports = new PedalboardPluginPort[portcount+1];
  4208. memset(ports, 0, sizeof(PedalboardPluginPort) * (portcount+1));
  4209. const size_t full_instance_size = strlen(full_instance);
  4210. portcount = 0;
  4211. LILV_FOREACH(nodes, itport, portnodes)
  4212. {
  4213. const LilvNode* const portnode = lilv_nodes_get(portnodes, itport);
  4214. LilvNode* const portvalue = lilv_world_get(w, portnode, ingen_value, nullptr);
  4215. if (portvalue == nullptr)
  4216. continue;
  4217. int8_t mchan = -1;
  4218. uint8_t mctrl = 0;
  4219. float minimum = 0.0f, maximum = 1.0f;
  4220. bool hasRanges = false;
  4221. if (LilvNode* const bind = lilv_world_get(w, portnode, midi_binding, nullptr))
  4222. {
  4223. LilvNode* const bindChan = lilv_world_get(w, bind, midi_channel, nullptr);
  4224. LilvNode* const bindCtrl = lilv_world_get(w, bind, midi_controlNum, nullptr);
  4225. if (bindChan != nullptr && bindCtrl != nullptr)
  4226. {
  4227. const int mchantest = lilv_node_as_int(bindChan);
  4228. const int mctrltest = lilv_node_as_int(bindCtrl);
  4229. if (mchantest >= 0 && mchantest < 16 && mctrltest >= 0 && mctrltest < 255)
  4230. {
  4231. mchan = (int8_t)mchantest;
  4232. mctrl = (uint8_t)mctrltest;
  4233. LilvNode* const bindMin = lilv_world_get(w, bind, lv2_minimum, nullptr);
  4234. LilvNode* const bindMax = lilv_world_get(w, bind, lv2_maximum, nullptr);
  4235. if (bindMin != nullptr && bindMax != nullptr)
  4236. {
  4237. hasRanges = true;
  4238. minimum = lilv_node_as_float(bindMin);
  4239. maximum = lilv_node_as_float(bindMax);
  4240. }
  4241. lilv_node_free(bindMin);
  4242. lilv_node_free(bindMax);
  4243. }
  4244. }
  4245. lilv_node_free(bindCtrl);
  4246. lilv_node_free(bindChan);
  4247. lilv_node_free(bind);
  4248. }
  4249. char* portsymbol = lilv_file_uri_parse2(lilv_node_as_string(portnode), nullptr);
  4250. if (strstr(portsymbol, full_instance) != nullptr)
  4251. memmove(portsymbol, portsymbol+(full_instance_size+1), strlen(portsymbol)-full_instance_size);
  4252. if (strcmp(portsymbol, ":bypass") == 0)
  4253. {
  4254. bypassCC.channel = mchan;
  4255. bypassCC.control = mctrl;
  4256. lilv_free(portsymbol);
  4257. }
  4258. else
  4259. {
  4260. ports[portcount++] = {
  4261. true,
  4262. portsymbol,
  4263. lilv_node_as_float(portvalue),
  4264. { mchan, mctrl, hasRanges, minimum, maximum }
  4265. };
  4266. }
  4267. lilv_node_free(portvalue);
  4268. }
  4269. lilv_nodes_free(portnodes);
  4270. }
  4271. PerformancePluginInfo performanceInfo = {
  4272. visible,
  4273. perfview_index,
  4274. };
  4275. plugs[count++] = {
  4276. true,
  4277. enabled != nullptr ? !lilv_node_as_bool(enabled) : true,
  4278. instId != nullptr ? lilv_node_as_int(instId) : -1,
  4279. instance,
  4280. strdup(uri),
  4281. bypassCC,
  4282. x != nullptr ? lilv_node_as_float(x) : 0.0f,
  4283. y != nullptr ? lilv_node_as_float(y) : 0.0f,
  4284. ports,
  4285. (preset != nullptr && !lilv_node_equals(preset, urinode)) ? strdup(lilv_node_as_uri(preset)) : nc,
  4286. label == nullptr ? nc : label,
  4287. performanceInfo
  4288. };
  4289. lilv_free(full_instance);
  4290. lilv_node_free(enabled);
  4291. lilv_node_free(x);
  4292. lilv_node_free(y);
  4293. lilv_node_free(proto);
  4294. lilv_node_free(preset);
  4295. lilv_node_free(instId);
  4296. }
  4297. }
  4298. info.plugins = plugs;
  4299. }
  4300. lilv_nodes_free(blocks);
  4301. }
  4302. // --------------------------------------------------------------------------------------------------------
  4303. // connections
  4304. if (LilvNodes* const arcs = lilv_plugin_get_value(p, ingen_arc))
  4305. {
  4306. if (unsigned int count = lilv_nodes_size(arcs))
  4307. {
  4308. PedalboardConnection* conns = new PedalboardConnection[count+1];
  4309. memset(conns, 0, sizeof(PedalboardConnection) * (count+1));
  4310. count = 0;
  4311. LILV_FOREACH(nodes, itarcs, arcs)
  4312. {
  4313. const LilvNode* const arc = lilv_nodes_get(arcs, itarcs);
  4314. LilvNode* const head = lilv_world_get(w, arc, ingen_head, nullptr);
  4315. if (head == nullptr)
  4316. continue;
  4317. LilvNode* const tail = lilv_world_get(w, arc, ingen_tail, nullptr);
  4318. if (head == nullptr)
  4319. {
  4320. lilv_node_free(head);
  4321. continue;
  4322. }
  4323. char* tailstr = lilv_file_uri_parse2(lilv_node_as_string(tail), nullptr);
  4324. char* headstr = lilv_file_uri_parse2(lilv_node_as_string(head), nullptr);
  4325. if (strstr(tailstr, bundlepath) != nullptr)
  4326. memmove(tailstr, tailstr+(bundlepathsize+1), strlen(tailstr)-bundlepathsize);
  4327. if (strstr(headstr, bundlepath) != nullptr)
  4328. memmove(headstr, headstr+(bundlepathsize+1), strlen(headstr)-bundlepathsize);
  4329. #ifdef _WIN32
  4330. for (uint32_t i=0; tailstr[i] != '\0'; ++i)
  4331. {
  4332. if (tailstr[i] == '\\')
  4333. {
  4334. tailstr[i] = '/';
  4335. break;
  4336. }
  4337. }
  4338. for (uint32_t i=0; headstr[i] != '\0'; ++i)
  4339. {
  4340. if (headstr[i] == '\\')
  4341. {
  4342. headstr[i] = '/';
  4343. break;
  4344. }
  4345. }
  4346. #endif
  4347. conns[count++] = {
  4348. true,
  4349. tailstr,
  4350. headstr
  4351. };
  4352. lilv_node_free(head);
  4353. lilv_node_free(tail);
  4354. }
  4355. info.connections = conns;
  4356. }
  4357. lilv_nodes_free(arcs);
  4358. }
  4359. // --------------------------------------------------------------------------------------------------------
  4360. // hardware ports and time info
  4361. if (LilvNodes* const hwports = lilv_plugin_get_value(p, lv2_port))
  4362. {
  4363. #if 0
  4364. std::vector<std::string> handled_port_uris;
  4365. #endif
  4366. std::vector<PedalboardHardwareMidiPort> midi_ins;
  4367. std::vector<PedalboardHardwareMidiPort> midi_outs;
  4368. LILV_FOREACH(nodes, ithwp, hwports)
  4369. {
  4370. const LilvNode* const hwport = lilv_nodes_get(hwports, ithwp);
  4371. char* portsym = lilv_file_uri_parse2(lilv_node_as_uri(hwport), nullptr);
  4372. if (portsym == nullptr)
  4373. continue;
  4374. if (strstr(portsym, bundlepath) != nullptr)
  4375. memmove(portsym, portsym+(bundlepathsize+1), strlen(portsym)-bundlepathsize);
  4376. // check if we already handled this port
  4377. if (strcmp(portsym, "control_in") == 0 || strcmp(portsym, "control_out") == 0)
  4378. {
  4379. lilv_free(portsym);
  4380. continue;
  4381. }
  4382. #if 0
  4383. {
  4384. const std::string portsym_s = portsym;
  4385. if (std::find(handled_port_uris.begin(), handled_port_uris.end(), portsym_s) != handled_port_uris.end())
  4386. {
  4387. lilv_free(portsym);
  4388. continue;
  4389. }
  4390. handled_port_uris.push_back(portsym_s);
  4391. }
  4392. #endif
  4393. // we began calling it legacy first, but separated makes more sense
  4394. if (strcmp(portsym, "midi_legacy_mode") == 0 || strcmp(portsym, "midi_separated_mode") == 0)
  4395. {
  4396. if (LilvNode* const separated = lilv_world_get(w, hwport, ingen_value, nullptr))
  4397. {
  4398. info.midi_separated_mode = lilv_node_as_int(separated) != 0;
  4399. lilv_node_free(separated);
  4400. }
  4401. lilv_free(portsym);
  4402. continue;
  4403. }
  4404. if (strcmp(portsym, "midi_loopback") == 0)
  4405. {
  4406. if (LilvNode* const loopback = lilv_world_get(w, hwport, ingen_value, nullptr))
  4407. {
  4408. info.midi_loopback = lilv_node_as_int(loopback) != 0;
  4409. lilv_node_free(loopback);
  4410. }
  4411. lilv_free(portsym);
  4412. continue;
  4413. }
  4414. TimePortType timePortType;
  4415. /**/ if (strcmp(portsym, ":bpb") == 0)
  4416. timePortType = kTimePortTypeBeatsPerBar;
  4417. else if (strcmp(portsym, ":bpm") == 0)
  4418. timePortType = kTimePortTypeBeatsPerMinute;
  4419. else if (strcmp(portsym, ":rolling") == 0)
  4420. timePortType = kTimePortTypeRolling;
  4421. else
  4422. timePortType = kTimePortTypeNull;
  4423. if (timePortType != kTimePortTypeNull)
  4424. {
  4425. if (LilvNode* const portvalue = lilv_world_get(w, hwport, ingen_value, nullptr))
  4426. {
  4427. float value;
  4428. int8_t mchan = -1;
  4429. uint8_t mctrl = 0;
  4430. float minimum = 0.0f, maximum = 0.0f;
  4431. bool hasRanges = false;
  4432. if (LilvNode* const bind = lilv_world_get(w, hwport, midi_binding, nullptr))
  4433. {
  4434. LilvNode* const bindChan = lilv_world_get(w, bind, midi_channel, nullptr);
  4435. LilvNode* const bindCtrl = lilv_world_get(w, bind, midi_controlNum, nullptr);
  4436. if (bindChan != nullptr && bindCtrl != nullptr)
  4437. {
  4438. const int mchantest = lilv_node_as_int(bindChan);
  4439. const int mctrltest = lilv_node_as_int(bindCtrl);
  4440. if (mchantest >= 0 && mchantest < 16 && mctrltest >= 0 && mctrltest < 255)
  4441. {
  4442. mchan = (int8_t)mchantest;
  4443. mctrl = (uint8_t)mctrltest;
  4444. LilvNode* const bindMin = lilv_world_get(w, bind, lv2_minimum, nullptr);
  4445. LilvNode* const bindMax = lilv_world_get(w, bind, lv2_maximum, nullptr);
  4446. if (bindMin != nullptr && bindMax != nullptr)
  4447. {
  4448. hasRanges = true;
  4449. minimum = lilv_node_as_float(bindMin);
  4450. maximum = lilv_node_as_float(bindMax);
  4451. }
  4452. lilv_node_free(bindMin);
  4453. lilv_node_free(bindMax);
  4454. }
  4455. }
  4456. lilv_node_free(bindCtrl);
  4457. lilv_node_free(bindChan);
  4458. lilv_node_free(bind);
  4459. }
  4460. switch (timePortType)
  4461. {
  4462. case kTimePortTypeNull:
  4463. break;
  4464. case kTimePortTypeBeatsPerBar:
  4465. value = lilv_node_as_float(portvalue);
  4466. if (value >= 1.0f && value <= 16.0f)
  4467. {
  4468. info.timeInfo.bpb = value;
  4469. info.timeInfo.bpbCC = { mchan, mctrl, hasRanges, minimum, maximum };
  4470. info.timeInfo.available |= kPedalboardTimeAvailableBPB;
  4471. }
  4472. break;
  4473. case kTimePortTypeBeatsPerMinute:
  4474. value = lilv_node_as_float(portvalue);
  4475. if (value >= 20.0f && value <= 280.0f)
  4476. {
  4477. info.timeInfo.bpm = value;
  4478. info.timeInfo.bpmCC = { mchan, mctrl, hasRanges, minimum, maximum };
  4479. info.timeInfo.available |= kPedalboardTimeAvailableBPM;
  4480. }
  4481. break;
  4482. case kTimePortTypeRolling:
  4483. info.timeInfo.rolling = lilv_node_as_int(portvalue) != 0;
  4484. info.timeInfo.rollingCC = { mchan, mctrl, hasRanges, minimum, maximum };
  4485. info.timeInfo.available |= kPedalboardTimeAvailableRolling;
  4486. break;
  4487. }
  4488. lilv_node_free(portvalue);
  4489. }
  4490. lilv_free(portsym);
  4491. continue;
  4492. }
  4493. // get types
  4494. if (LilvNodes* const port_types = lilv_world_find_nodes(w, hwport, rdftypenode, NULL))
  4495. {
  4496. PortDirection direction = kPortDirectionNull;
  4497. PortType portType = kPortTypeNull;
  4498. LILV_FOREACH(nodes, itptyp, port_types)
  4499. {
  4500. const LilvNode* const ptyp = lilv_nodes_get(port_types, itptyp);
  4501. if (const char* port_type_uri = lilv_node_as_uri(ptyp))
  4502. {
  4503. if (strcmp(port_type_uri, MOD__CVPort) == 0)
  4504. {
  4505. portType = kPortTypeCV;
  4506. }
  4507. else if (strncmp(port_type_uri, "http://lv2plug.in/ns/", 21) == 0)
  4508. {
  4509. port_type_uri += 21; // http://lv2plug.in/ns/
  4510. /**/ if (strcmp(port_type_uri, "lv2core#InputPort") == 0)
  4511. direction = kPortDirectionInput;
  4512. else if (strcmp(port_type_uri, "lv2core#OutputPort") == 0)
  4513. direction = kPortDirectionOutput;
  4514. else if (strcmp(port_type_uri, "lv2core#AudioPort") == 0)
  4515. portType = kPortTypeAudio;
  4516. else if (strcmp(port_type_uri, "lv2core#CVPort") == 0)
  4517. portType = kPortTypeCV;
  4518. else if (strcmp(port_type_uri, "ext/atom#AtomPort") == 0)
  4519. portType = kPortTypeMIDI;
  4520. }
  4521. }
  4522. }
  4523. if (direction == kPortDirectionNull || portType == kPortTypeNull)
  4524. {
  4525. lilv_free(portsym);
  4526. continue;
  4527. }
  4528. switch (portType)
  4529. {
  4530. case kPortTypeNull:
  4531. case kPortTypeControl:
  4532. break;
  4533. case kPortTypeAudio:
  4534. if (direction == kPortDirectionInput)
  4535. ++info.hardware.audio_ins;
  4536. else
  4537. ++info.hardware.audio_outs;
  4538. break;
  4539. case kPortTypeMIDI:
  4540. if (direction == kPortDirectionInput)
  4541. {
  4542. if (strcmp(portsym, "serial_midi_in") == 0)
  4543. {
  4544. info.hardware.serial_midi_in = true;
  4545. }
  4546. else
  4547. {
  4548. LilvNode* const hwportname = lilv_world_get(w, hwport, lv2_name, nullptr);
  4549. PedalboardHardwareMidiPort mport = {
  4550. true,
  4551. portsym,
  4552. nc,
  4553. };
  4554. if (hwportname != nullptr && lilv_node_is_string(hwportname))
  4555. mport.name = strdup(lilv_node_as_string(hwportname));
  4556. lilv_node_free(hwportname);
  4557. portsym = nullptr;
  4558. midi_ins.push_back(mport);
  4559. }
  4560. }
  4561. else
  4562. {
  4563. if (strcmp(portsym, "serial_midi_out") == 0)
  4564. {
  4565. info.hardware.serial_midi_out = true;
  4566. }
  4567. else
  4568. {
  4569. LilvNode* const hwportname = lilv_world_get(w, hwport, lv2_name, nullptr);
  4570. PedalboardHardwareMidiPort mport = {
  4571. true,
  4572. portsym,
  4573. nc,
  4574. };
  4575. if (hwportname != nullptr && lilv_node_is_string(hwportname))
  4576. mport.name = strdup(lilv_node_as_string(hwportname));
  4577. lilv_node_free(hwportname);
  4578. portsym = nullptr;
  4579. midi_outs.push_back(mport);
  4580. }
  4581. }
  4582. break;
  4583. case kPortTypeCV:
  4584. if (direction == kPortDirectionInput)
  4585. ++info.hardware.cv_ins;
  4586. else
  4587. ++info.hardware.cv_outs;
  4588. break;
  4589. }
  4590. lilv_free(portsym);
  4591. lilv_nodes_free(port_types);
  4592. }
  4593. }
  4594. if (size_t count = midi_ins.size())
  4595. {
  4596. PedalboardHardwareMidiPort* mins = new PedalboardHardwareMidiPort[count+1];
  4597. count = 0;
  4598. for (const PedalboardHardwareMidiPort& min : midi_ins)
  4599. mins[count++] = min;
  4600. memset(&mins[count], 0, sizeof(PedalboardHardwareMidiPort));
  4601. info.hardware.midi_ins = mins;
  4602. }
  4603. if (size_t count = midi_outs.size())
  4604. {
  4605. PedalboardHardwareMidiPort* mouts = new PedalboardHardwareMidiPort[count+1];
  4606. count = 0;
  4607. for (const PedalboardHardwareMidiPort& mout : midi_outs)
  4608. mouts[count++] = mout;
  4609. memset(&mouts[count], 0, sizeof(PedalboardHardwareMidiPort));
  4610. info.hardware.midi_outs = mouts;
  4611. }
  4612. lilv_nodes_free(hwports);
  4613. }
  4614. // --------------------------------------------------------------------------------------------------------
  4615. // version
  4616. if (LilvNodes* const nodes = lilv_plugin_get_value(p, modpedal_version))
  4617. {
  4618. info.version = lilv_node_as_int(lilv_nodes_get_first(nodes));
  4619. lilv_nodes_free(nodes);
  4620. }
  4621. // --------------------------------------------------------------------------------------------------------
  4622. // format version (absent = 1, see http://moddevices.com/ns/modpedal#formatVersion)
  4623. info.formatVersion = 1;
  4624. if (LilvNodes* const nodes = lilv_plugin_get_value(p, modpedal_formatVersion))
  4625. {
  4626. const int formatVersion = lilv_node_as_int(lilv_nodes_get_first(nodes));
  4627. if (formatVersion > 0)
  4628. info.formatVersion = static_cast<unsigned int>(formatVersion);
  4629. lilv_nodes_free(nodes);
  4630. }
  4631. // --------------------------------------------------------------------------------------------------------
  4632. lilv_node_free(ingen_arc);
  4633. lilv_node_free(ingen_block);
  4634. lilv_node_free(ingen_canvasX);
  4635. lilv_node_free(ingen_canvasY);
  4636. lilv_node_free(ingen_enabled);
  4637. lilv_node_free(ingen_head);
  4638. lilv_node_free(ingen_tail);
  4639. lilv_node_free(ingen_value);
  4640. lilv_node_free(ingen_poly);
  4641. lilv_node_free(lv2_maximum);
  4642. lilv_node_free(lv2_minimum);
  4643. lilv_node_free(lv2_name);
  4644. lilv_node_free(lv2_port);
  4645. lilv_node_free(lv2_prototype);
  4646. lilv_node_free(midi_binding);
  4647. lilv_node_free(midi_channel);
  4648. lilv_node_free(midi_controlNum);
  4649. lilv_node_free(modpedal_preset);
  4650. lilv_node_free(modpedal_width);
  4651. lilv_node_free(modpedal_height);
  4652. lilv_node_free(modpedal_version);
  4653. lilv_node_free(modpedal_formatVersion);
  4654. lilv_node_free(modpedal_instanceNumber);
  4655. lilv_node_free(modpedal_performanceVisible);
  4656. lilv_node_free(modpedal_performanceIndex);
  4657. lilv_node_free(modpedal_instanceLabel);
  4658. lilv_node_free(modperf_visible);
  4659. lilv_node_free(modperf_index);
  4660. lilv_node_free(mod_label);
  4661. lilv_node_free(rdftypenode);
  4662. lilv_world_free(w);
  4663. _get_pedal_info_ret = &info;
  4664. return &info;
  4665. }
  4666. int* get_pedalboard_size(const char* const bundle)
  4667. {
  4668. size_t bundlepathsize;
  4669. const char* const bundlepath = _get_safe_bundlepath(bundle, bundlepathsize);
  4670. if (bundlepath == nullptr)
  4671. return nullptr;
  4672. LilvWorld* const w = lilv_world_new();
  4673. LilvNode* const b = lilv_new_file_uri(w, nullptr, bundlepath);
  4674. lilv_world_load_bundle(w, b);
  4675. lilv_node_free(b);
  4676. const LilvPlugins* const plugins = lilv_world_get_all_plugins(w);
  4677. if (lilv_plugins_size(plugins) != 1)
  4678. {
  4679. lilv_world_free(w);
  4680. return nullptr;
  4681. }
  4682. const LilvPlugin* p = nullptr;
  4683. LILV_FOREACH(plugins, itpls, plugins) {
  4684. p = lilv_plugins_get(plugins, itpls);
  4685. break;
  4686. }
  4687. if (p == nullptr)
  4688. {
  4689. lilv_world_free(w);
  4690. return nullptr;
  4691. }
  4692. LilvNode* const widthnode = lilv_new_uri(w, LILV_NS_MODPEDAL "width");
  4693. LilvNode* const heightnode = lilv_new_uri(w, LILV_NS_MODPEDAL "height");
  4694. LilvNodes* const widthnodes = lilv_plugin_get_value(p, widthnode);
  4695. LilvNodes* const heightnodes = lilv_plugin_get_value(p, heightnode);
  4696. if (widthnodes == nullptr || heightnodes == nullptr)
  4697. {
  4698. lilv_nodes_free(widthnodes);
  4699. lilv_nodes_free(heightnodes);
  4700. lilv_node_free(widthnode);
  4701. lilv_node_free(heightnode);
  4702. lilv_world_free(w);
  4703. return nullptr;
  4704. }
  4705. static int size[2];
  4706. size[0] = lilv_node_as_int(lilv_nodes_get_first(widthnodes));
  4707. size[1] = lilv_node_as_int(lilv_nodes_get_first(heightnodes));
  4708. lilv_nodes_free(widthnodes);
  4709. lilv_nodes_free(heightnodes);
  4710. lilv_node_free(widthnode);
  4711. lilv_node_free(heightnode);
  4712. lilv_world_free(w);
  4713. return size;
  4714. }
  4715. const PedalboardPluginValues* get_pedalboard_plugin_values(const char* bundle)
  4716. {
  4717. // NOTE: most of this code is duplicated from get_pedalboard_info
  4718. size_t bundlepathsize;
  4719. const char* const bundlepath = _get_safe_bundlepath(bundle, bundlepathsize);
  4720. if (bundlepath == nullptr)
  4721. return nullptr;
  4722. LilvWorld* const w = lilv_world_new();
  4723. #ifdef HAVE_NEW_LILV
  4724. lilv_world_load_specifications(w);
  4725. lilv_world_load_plugin_classes(w);
  4726. #endif
  4727. LilvNode* const b = lilv_new_file_uri(w, nullptr, bundlepath);
  4728. lilv_world_load_bundle(w, b);
  4729. lilv_node_free(b);
  4730. const LilvPlugins* const plugins = lilv_world_get_all_plugins(w);
  4731. if (lilv_plugins_size(plugins) != 1)
  4732. {
  4733. lilv_world_free(w);
  4734. return nullptr;
  4735. }
  4736. const LilvPlugin* p = nullptr;
  4737. LILV_FOREACH(plugins, itpls, plugins) {
  4738. p = lilv_plugins_get(plugins, itpls);
  4739. break;
  4740. }
  4741. if (p == nullptr)
  4742. {
  4743. lilv_world_free(w);
  4744. return nullptr;
  4745. }
  4746. bool isPedalboard = false;
  4747. LilvNode* const rdftypenode = lilv_new_uri(w, LILV_NS_RDF "type");
  4748. if (LilvNodes* const nodes = lilv_plugin_get_value(p, rdftypenode))
  4749. {
  4750. LILV_FOREACH(nodes, it, nodes)
  4751. {
  4752. const LilvNode* const node = lilv_nodes_get(nodes, it);
  4753. if (const char* const nodestr = lilv_node_as_string(node))
  4754. {
  4755. if (strcmp(nodestr, LILV_NS_MODPEDAL "Pedalboard") == 0)
  4756. {
  4757. isPedalboard = true;
  4758. break;
  4759. }
  4760. }
  4761. }
  4762. lilv_nodes_free(nodes);
  4763. }
  4764. if (! isPedalboard)
  4765. {
  4766. lilv_node_free(rdftypenode);
  4767. lilv_world_free(w);
  4768. return nullptr;
  4769. }
  4770. // --------------------------------------------------------------------------------------------------------
  4771. // plugins
  4772. LilvNode* const ingen_block = lilv_new_uri(w, LILV_NS_INGEN "block");
  4773. LilvNodes* const blocks = lilv_plugin_get_value(p, ingen_block);
  4774. if (blocks == nullptr)
  4775. {
  4776. lilv_node_free(ingen_block);
  4777. lilv_node_free(rdftypenode);
  4778. lilv_world_free(w);
  4779. return nullptr;
  4780. }
  4781. unsigned int blockCount = lilv_nodes_size(blocks);
  4782. if (blockCount == 0)
  4783. {
  4784. lilv_nodes_free(blocks);
  4785. lilv_node_free(ingen_block);
  4786. lilv_node_free(rdftypenode);
  4787. lilv_world_free(w);
  4788. return nullptr;
  4789. }
  4790. LilvNode* const ingen_enabled = lilv_new_uri(w, LILV_NS_INGEN "enabled");
  4791. LilvNode* const ingen_value = lilv_new_uri(w, LILV_NS_INGEN "value");
  4792. LilvNode* const lv2_port = lilv_new_uri(w, LV2_CORE__port);
  4793. LilvNode* const modpedal_preset = lilv_new_uri(w, LILV_NS_MODPEDAL "preset");
  4794. // --------------------------------------------------------------------------------------------------------
  4795. // uri node (ie, "this")
  4796. const LilvNode* const urinode = lilv_plugin_get_uri(p);
  4797. // --------------------------------------------------------------------------------------------------------
  4798. // ready to parse
  4799. _clear_pedalboard_plugin_values();
  4800. PedalboardPluginValues* const plugs = new PedalboardPluginValues[blockCount+1];
  4801. memset(plugs, 0, sizeof(PedalboardPluginValues) * (blockCount+1));
  4802. blockCount = 0;
  4803. LILV_FOREACH(nodes, itblocks, blocks)
  4804. {
  4805. const LilvNode* const block = lilv_nodes_get(blocks, itblocks);
  4806. char* full_instance = lilv_file_uri_parse2(lilv_node_as_string(block), nullptr);
  4807. char* instance;
  4808. if (strstr(full_instance, bundlepath) != nullptr)
  4809. instance = strdup(full_instance+(bundlepathsize+1));
  4810. else
  4811. instance = strdup(full_instance);
  4812. LilvNode* const enabled = lilv_world_get(w, block, ingen_enabled, nullptr);
  4813. LilvNode* const preset = lilv_world_get(w, block, modpedal_preset, nullptr);
  4814. StatePortValue* ports = nullptr;
  4815. if (LilvNodes* const portnodes = lilv_world_find_nodes(w, block, lv2_port, nullptr))
  4816. {
  4817. unsigned int portCount = lilv_nodes_size(portnodes);
  4818. ports = new StatePortValue[portCount+1];
  4819. memset(ports, 0, sizeof(StatePortValue) * (portCount+1));
  4820. const size_t full_instance_size = strlen(full_instance);
  4821. portCount = 0;
  4822. LILV_FOREACH(nodes, itport, portnodes)
  4823. {
  4824. const LilvNode* const portnode = lilv_nodes_get(portnodes, itport);
  4825. LilvNode* const portvalue = lilv_world_get(w, portnode, ingen_value, nullptr);
  4826. if (portvalue == nullptr)
  4827. continue;
  4828. char* portsymbol = lilv_file_uri_parse2(lilv_node_as_string(portnode), nullptr);
  4829. if (strstr(portsymbol, full_instance) != nullptr)
  4830. memmove(portsymbol, portsymbol+(full_instance_size+1), strlen(portsymbol)-full_instance_size);
  4831. if (strcmp(portsymbol, ":bypass") == 0)
  4832. {
  4833. lilv_free(portsymbol);
  4834. }
  4835. else
  4836. {
  4837. ports[portCount++] = {
  4838. true,
  4839. portsymbol,
  4840. lilv_node_as_float(portvalue)
  4841. };
  4842. }
  4843. lilv_node_free(portvalue);
  4844. }
  4845. lilv_nodes_free(portnodes);
  4846. }
  4847. plugs[blockCount++] = {
  4848. true,
  4849. enabled != nullptr ? !lilv_node_as_bool(enabled) : true,
  4850. instance,
  4851. (preset != nullptr && !lilv_node_equals(preset, urinode)) ? strdup(lilv_node_as_uri(preset)) : nc,
  4852. ports,
  4853. };
  4854. lilv_free(full_instance);
  4855. lilv_node_free(enabled);
  4856. lilv_node_free(preset);
  4857. }
  4858. lilv_nodes_free(blocks);
  4859. lilv_node_free(ingen_block);
  4860. lilv_node_free(ingen_enabled);
  4861. lilv_node_free(ingen_value);
  4862. lilv_node_free(lv2_port);
  4863. lilv_node_free(modpedal_preset);
  4864. lilv_node_free(rdftypenode);
  4865. lilv_world_free(w);
  4866. _get_pedal_values_ret = plugs;
  4867. return plugs;
  4868. }
  4869. void reset_get_all_pedalboards_cache(int ptype)
  4870. {
  4871. if (ptype == kPedalboardInfoUserOnly || ptype == kPedalboardInfoBoth)
  4872. {
  4873. _clear_pedalboards(_get_pedals_mini_ret);
  4874. _get_pedals_mini_ret = nullptr;
  4875. }
  4876. if (ptype == kPedalboardInfoUserOnly || ptype == kPedalboardInfoBoth)
  4877. {
  4878. _clear_pedalboards(FACTORYINFO);
  4879. FACTORYINFO = nullptr;
  4880. }
  4881. }
  4882. // --------------------------------------------------------------------------------------------------------
  4883. #ifdef HAVE_NEW_LILV
  4884. // note: these ids must match the ones on the mapping (see 'kMapping')
  4885. static const uint32_t k_urid_null = 0;
  4886. static const uint32_t k_urid_atom_bool = 1;
  4887. static const uint32_t k_urid_atom_int = 2;
  4888. static const uint32_t k_urid_atom_long = 3;
  4889. static const uint32_t k_urid_atom_float = 4;
  4890. static const uint32_t k_urid_atom_double = 5;
  4891. static LV2_URID lv2_urid_map(LV2_URID_Map_Handle, const char* const uri_)
  4892. {
  4893. if (uri_ == nullptr || uri_[0] == '\0')
  4894. return k_urid_null;
  4895. static std::vector<std::string> kMapping = {
  4896. LV2_ATOM__Bool,
  4897. LV2_ATOM__Int,
  4898. LV2_ATOM__Long,
  4899. LV2_ATOM__Float,
  4900. LV2_ATOM__Double,
  4901. };
  4902. const std::string uri(uri_);
  4903. LV2_URID urid = 1;
  4904. for (const std::string& uri2 : kMapping)
  4905. {
  4906. if (uri2 == uri)
  4907. return urid;
  4908. ++urid;
  4909. }
  4910. kMapping.push_back(uri);
  4911. return urid;
  4912. }
  4913. static void lilv_set_port_value(const char* const portSymbol, void* const userData,
  4914. const void* const value, const uint32_t size, const uint32_t type)
  4915. {
  4916. std::vector<StatePortValue>* const values = (std::vector<StatePortValue>*)userData;
  4917. switch (type)
  4918. {
  4919. case k_urid_atom_bool:
  4920. case k_urid_atom_int:
  4921. if (size == sizeof(int32_t))
  4922. {
  4923. int32_t ivalue = *(const int32_t*)value;
  4924. values->push_back({ true, strdup(portSymbol), (float)ivalue });
  4925. return;
  4926. }
  4927. break;
  4928. case k_urid_atom_long:
  4929. if (size == sizeof(int64_t))
  4930. {
  4931. int64_t ivalue = *(const int64_t*)value;
  4932. values->push_back({ true, strdup(portSymbol), (float)ivalue });
  4933. return;
  4934. }
  4935. break;
  4936. case k_urid_atom_float:
  4937. if (size == sizeof(float))
  4938. {
  4939. float fvalue = *(const float*)value;
  4940. values->push_back({ true, strdup(portSymbol), fvalue });
  4941. return;
  4942. }
  4943. break;
  4944. case k_urid_atom_double:
  4945. if (size == sizeof(double))
  4946. {
  4947. double dvalue = *(const double*)value;
  4948. values->push_back({ true, strdup(portSymbol), (float)dvalue });
  4949. return;
  4950. }
  4951. break;
  4952. }
  4953. printf("lilv_set_port_value called with unknown type: %u %u\n", type, size);
  4954. }
  4955. #endif
  4956. const StatePortValue* get_state_port_values(const char* const state)
  4957. {
  4958. #ifdef HAVE_NEW_LILV
  4959. static LV2_URID_Map uridMap = {
  4960. (void*)0x1, // non-null
  4961. lv2_urid_map
  4962. };
  4963. LilvWorld* const w = W;
  4964. #ifdef _WIN32
  4965. putenv("LILV_STATE_SKIP_PROPERTIES=2");
  4966. #else
  4967. setenv("LILV_STATE_SKIP_PROPERTIES", "2", 1);
  4968. #endif
  4969. LilvState* const lstate = lilv_state_new_from_string(w, &uridMap, state);
  4970. #ifdef _WIN32
  4971. putenv("LILV_STATE_SKIP_PROPERTIES=");
  4972. #else
  4973. unsetenv("LILV_STATE_SKIP_PROPERTIES");
  4974. #endif
  4975. if (lstate != nullptr)
  4976. {
  4977. std::vector<StatePortValue> values;
  4978. lilv_state_emit_port_values(lstate, lilv_set_port_value, &values);
  4979. lilv_state_free(lstate);
  4980. if (size_t count = values.size())
  4981. {
  4982. _clear_state_values();
  4983. _get_state_values_ret = new StatePortValue[count+1];
  4984. count = 0;
  4985. for (const StatePortValue& v : values)
  4986. _get_state_values_ret[count++] = v;
  4987. memset(&_get_state_values_ret[count], 0, sizeof(StatePortValue));
  4988. return _get_state_values_ret;
  4989. }
  4990. }
  4991. #endif
  4992. return nullptr;
  4993. #ifndef HAVE_NEW_LILV
  4994. // unused
  4995. (void)state;
  4996. #endif
  4997. }
  4998. // --------------------------------------------------------------------------------------------------------
  4999. const char* const* list_plugins_in_bundle(const char* bundle)
  5000. {
  5001. size_t bundlepathsize;
  5002. const char* const bundlepath = _get_safe_bundlepath(bundle, bundlepathsize);
  5003. if (bundlepath == nullptr)
  5004. return nullptr;
  5005. LilvWorld* const w = lilv_world_new();
  5006. LilvNode* const b = lilv_new_file_uri(w, nullptr, bundlepath);
  5007. lilv_world_load_bundle(w, b);
  5008. lilv_node_free(b);
  5009. const LilvPlugins* const plugins = lilv_world_get_all_plugins(w);
  5010. if (lilv_plugins_size(plugins) == 0)
  5011. {
  5012. lilv_world_free(w);
  5013. return nullptr;
  5014. }
  5015. const LilvPlugin* p;
  5016. std::vector<const char*> pluginURIs;
  5017. LILV_FOREACH(plugins, itpls, plugins)
  5018. {
  5019. p = lilv_plugins_get(plugins, itpls);
  5020. pluginURIs.push_back(strdup(lilv_node_as_uri(lilv_plugin_get_uri(p))));
  5021. }
  5022. lilv_world_free(w);
  5023. if (size_t count = pluginURIs.size())
  5024. {
  5025. if (_add_remove_bundles_ret != nullptr)
  5026. {
  5027. for (int i=0; _add_remove_bundles_ret[i] != nullptr; ++i)
  5028. free((void*)_add_remove_bundles_ret[i]);
  5029. delete[] _add_remove_bundles_ret;
  5030. }
  5031. _add_remove_bundles_ret = new const char*[count+1];
  5032. count = 0;
  5033. for (const char* uri : pluginURIs)
  5034. _add_remove_bundles_ret[count++] = uri;
  5035. _add_remove_bundles_ret[count] = nullptr;
  5036. return _add_remove_bundles_ret;
  5037. }
  5038. return nullptr;
  5039. }
  5040. const char* file_uri_parse(const char* const fileuri)
  5041. {
  5042. if (_file_uri_parse_ret)
  5043. free(_file_uri_parse_ret);
  5044. _file_uri_parse_ret = lilv_file_abspath(fileuri);
  5045. return _file_uri_parse_ret != nullptr ? _file_uri_parse_ret : nc;
  5046. }
  5047. // --------------------------------------------------------------------------------------------------------