1
0

window-basic-main.cpp 161 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171
  1. /******************************************************************************
  2. Copyright (C) 2013-2015 by Hugh Bailey <[email protected]>
  3. Zachary Lund <[email protected]>
  4. Philippe Groarke <[email protected]>
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ******************************************************************************/
  16. #include <time.h>
  17. #include <obs.hpp>
  18. #include <QGuiApplication>
  19. #include <QMessageBox>
  20. #include <QShowEvent>
  21. #include <QDesktopServices>
  22. #include <QFileDialog>
  23. #include <QDesktopWidget>
  24. #include <QRect>
  25. #include <QScreen>
  26. #include <util/dstr.h>
  27. #include <util/util.hpp>
  28. #include <util/platform.h>
  29. #include <util/profiler.hpp>
  30. #include <util/dstr.hpp>
  31. #include <graphics/math-defs.h>
  32. #include "obs-app.hpp"
  33. #include "platform.hpp"
  34. #include "visibility-item-widget.hpp"
  35. #include "item-widget-helpers.hpp"
  36. #include "window-basic-settings.hpp"
  37. #include "window-namedialog.hpp"
  38. #include "window-basic-auto-config.hpp"
  39. #include "window-basic-source-select.hpp"
  40. #include "window-basic-main.hpp"
  41. #include "window-basic-stats.hpp"
  42. #include "window-basic-main-outputs.hpp"
  43. #include "window-basic-properties.hpp"
  44. #include "window-log-reply.hpp"
  45. #include "window-projector.hpp"
  46. #include "window-remux.hpp"
  47. #include "qt-wrappers.hpp"
  48. #include "display-helpers.hpp"
  49. #include "volume-control.hpp"
  50. #include "remote-text.hpp"
  51. #if defined(_WIN32) && defined(ENABLE_WIN_UPDATER)
  52. #include "win-update/win-update.hpp"
  53. #endif
  54. #include "ui_OBSBasic.h"
  55. #include <fstream>
  56. #include <sstream>
  57. #include <QScreen>
  58. #include <QWindow>
  59. using namespace std;
  60. namespace {
  61. template <typename OBSRef>
  62. struct SignalContainer {
  63. OBSRef ref;
  64. vector<shared_ptr<OBSSignal>> handlers;
  65. };
  66. }
  67. Q_DECLARE_METATYPE(OBSScene);
  68. Q_DECLARE_METATYPE(OBSSceneItem);
  69. Q_DECLARE_METATYPE(OBSSource);
  70. Q_DECLARE_METATYPE(obs_order_movement);
  71. Q_DECLARE_METATYPE(SignalContainer<OBSScene>);
  72. template <typename T>
  73. static T GetOBSRef(QListWidgetItem *item)
  74. {
  75. return item->data(static_cast<int>(QtDataRole::OBSRef)).value<T>();
  76. }
  77. template <typename T>
  78. static void SetOBSRef(QListWidgetItem *item, T &&val)
  79. {
  80. item->setData(static_cast<int>(QtDataRole::OBSRef),
  81. QVariant::fromValue(val));
  82. }
  83. static void AddExtraModulePaths()
  84. {
  85. char base_module_dir[512];
  86. #if defined(_WIN32) || defined(__APPLE__)
  87. int ret = GetProgramDataPath(base_module_dir, sizeof(base_module_dir),
  88. "obs-studio/plugins/%module%");
  89. #else
  90. int ret = GetConfigPath(base_module_dir, sizeof(base_module_dir),
  91. "obs-studio/plugins/%module%");
  92. #endif
  93. if (ret <= 0)
  94. return;
  95. string path = (char*)base_module_dir;
  96. #if defined(__APPLE__)
  97. obs_add_module_path((path + "/bin").c_str(), (path + "/data").c_str());
  98. BPtr<char> config_bin = os_get_config_path_ptr("obs-studio/plugins/%module%/bin");
  99. BPtr<char> config_data = os_get_config_path_ptr("obs-studio/plugins/%module%/data");
  100. obs_add_module_path(config_bin, config_data);
  101. #elif ARCH_BITS == 64
  102. obs_add_module_path((path + "/bin/64bit").c_str(),
  103. (path + "/data").c_str());
  104. #else
  105. obs_add_module_path((path + "/bin/32bit").c_str(),
  106. (path + "/data").c_str());
  107. #endif
  108. }
  109. static QList<QKeySequence> DeleteKeys;
  110. OBSBasic::OBSBasic(QWidget *parent)
  111. : OBSMainWindow (parent),
  112. ui (new Ui::OBSBasic)
  113. {
  114. setAttribute(Qt::WA_NativeWindow);
  115. projectorArray.resize(10, "");
  116. previewProjectorArray.resize(10, 0);
  117. multiviewProjectorArray.resize(10, 0);
  118. studioProgramProjectorArray.resize(10, 0);
  119. setAcceptDrops(true);
  120. ui->setupUi(this);
  121. ui->previewDisabledLabel->setVisible(false);
  122. startingDockLayout = saveState();
  123. copyActionsDynamicProperties();
  124. ui->sources->setItemDelegate(new VisibilityItemDelegate(ui->sources));
  125. char styleSheetPath[512];
  126. int ret = GetProfilePath(styleSheetPath, sizeof(styleSheetPath),
  127. "stylesheet.qss");
  128. if (ret > 0) {
  129. if (QFile::exists(styleSheetPath)) {
  130. QString path = QString("file:///") +
  131. QT_UTF8(styleSheetPath);
  132. App()->setStyleSheet(path);
  133. }
  134. }
  135. qRegisterMetaType<OBSScene> ("OBSScene");
  136. qRegisterMetaType<OBSSceneItem>("OBSSceneItem");
  137. qRegisterMetaType<OBSSource> ("OBSSource");
  138. qRegisterMetaType<obs_hotkey_id>("obs_hotkey_id");
  139. qRegisterMetaTypeStreamOperators<
  140. std::vector<std::shared_ptr<OBSSignal>>>(
  141. "std::vector<std::shared_ptr<OBSSignal>>");
  142. qRegisterMetaTypeStreamOperators<OBSScene>("OBSScene");
  143. qRegisterMetaTypeStreamOperators<OBSSceneItem>("OBSSceneItem");
  144. ui->scenes->setAttribute(Qt::WA_MacShowFocusRect, false);
  145. ui->sources->setAttribute(Qt::WA_MacShowFocusRect, false);
  146. auto displayResize = [this]() {
  147. struct obs_video_info ovi;
  148. if (obs_get_video_info(&ovi))
  149. ResizePreview(ovi.base_width, ovi.base_height);
  150. };
  151. connect(windowHandle(), &QWindow::screenChanged, displayResize);
  152. connect(ui->preview, &OBSQTDisplay::DisplayResized, displayResize);
  153. installEventFilter(CreateShortcutFilter());
  154. stringstream name;
  155. name << "OBS " << App()->GetVersionString();
  156. blog(LOG_INFO, "%s", name.str().c_str());
  157. blog(LOG_INFO, "---------------------------------");
  158. UpdateTitleBar();
  159. connect(ui->scenes->itemDelegate(),
  160. SIGNAL(closeEditor(QWidget*,
  161. QAbstractItemDelegate::EndEditHint)),
  162. this,
  163. SLOT(SceneNameEdited(QWidget*,
  164. QAbstractItemDelegate::EndEditHint)));
  165. connect(ui->sources->itemDelegate(),
  166. SIGNAL(closeEditor(QWidget*,
  167. QAbstractItemDelegate::EndEditHint)),
  168. this,
  169. SLOT(SceneItemNameEdited(QWidget*,
  170. QAbstractItemDelegate::EndEditHint)));
  171. cpuUsageInfo = os_cpu_usage_info_start();
  172. cpuUsageTimer = new QTimer(this);
  173. connect(cpuUsageTimer, SIGNAL(timeout()),
  174. ui->statusbar, SLOT(UpdateCPUUsage()));
  175. cpuUsageTimer->start(3000);
  176. DeleteKeys =
  177. #ifdef __APPLE__
  178. QList<QKeySequence>{{Qt::Key_Backspace}} <<
  179. #endif
  180. QKeySequence::keyBindings(QKeySequence::Delete);
  181. #ifdef __APPLE__
  182. ui->actionRemoveSource->setShortcuts(DeleteKeys);
  183. ui->actionRemoveScene->setShortcuts(DeleteKeys);
  184. ui->action_Settings->setMenuRole(QAction::PreferencesRole);
  185. ui->actionE_xit->setMenuRole(QAction::QuitRole);
  186. #endif
  187. auto addNudge = [this](const QKeySequence &seq, const char *s)
  188. {
  189. QAction *nudge = new QAction(ui->preview);
  190. nudge->setShortcut(seq);
  191. nudge->setShortcutContext(Qt::WidgetShortcut);
  192. ui->preview->addAction(nudge);
  193. connect(nudge, SIGNAL(triggered()), this, s);
  194. };
  195. addNudge(Qt::Key_Up, SLOT(NudgeUp()));
  196. addNudge(Qt::Key_Down, SLOT(NudgeDown()));
  197. addNudge(Qt::Key_Left, SLOT(NudgeLeft()));
  198. addNudge(Qt::Key_Right, SLOT(NudgeRight()));
  199. auto assignDockToggle = [this](QDockWidget *dock, QAction *action)
  200. {
  201. auto handleWindowToggle = [action] (bool vis)
  202. {
  203. action->blockSignals(true);
  204. action->setChecked(vis);
  205. action->blockSignals(false);
  206. };
  207. auto handleMenuToggle = [dock] (bool check)
  208. {
  209. dock->blockSignals(true);
  210. dock->setVisible(check);
  211. dock->blockSignals(false);
  212. };
  213. dock->connect(dock->toggleViewAction(), &QAction::toggled,
  214. handleWindowToggle);
  215. dock->connect(action, &QAction::toggled,
  216. handleMenuToggle);
  217. };
  218. assignDockToggle(ui->scenesDock, ui->toggleScenes);
  219. assignDockToggle(ui->sourcesDock, ui->toggleSources);
  220. assignDockToggle(ui->mixerDock, ui->toggleMixer);
  221. assignDockToggle(ui->transitionsDock, ui->toggleTransitions);
  222. assignDockToggle(ui->controlsDock, ui->toggleControls);
  223. //hide all docking panes
  224. ui->toggleScenes->setChecked(false);
  225. ui->toggleSources->setChecked(false);
  226. ui->toggleMixer->setChecked(false);
  227. ui->toggleTransitions->setChecked(false);
  228. ui->toggleControls->setChecked(false);
  229. //restore parent window geometry
  230. const char *geometry = config_get_string(App()->GlobalConfig(),
  231. "BasicWindow", "geometry");
  232. if (geometry != NULL) {
  233. QByteArray byteArray = QByteArray::fromBase64(
  234. QByteArray(geometry));
  235. restoreGeometry(byteArray);
  236. QRect windowGeometry = normalGeometry();
  237. if (!WindowPositionValid(windowGeometry)) {
  238. QRect rect = App()->desktop()->geometry();
  239. setGeometry(QStyle::alignedRect(
  240. Qt::LeftToRight,
  241. Qt::AlignCenter,
  242. size(), rect));
  243. }
  244. }
  245. }
  246. static void SaveAudioDevice(const char *name, int channel, obs_data_t *parent,
  247. vector<OBSSource> &audioSources)
  248. {
  249. obs_source_t *source = obs_get_output_source(channel);
  250. if (!source)
  251. return;
  252. audioSources.push_back(source);
  253. obs_data_t *data = obs_save_source(source);
  254. obs_data_set_obj(parent, name, data);
  255. obs_data_release(data);
  256. obs_source_release(source);
  257. }
  258. static obs_data_t *GenerateSaveData(obs_data_array_t *sceneOrder,
  259. obs_data_array_t *quickTransitionData, int transitionDuration,
  260. obs_data_array_t *transitions,
  261. OBSScene &scene, OBSSource &curProgramScene,
  262. obs_data_array_t *savedProjectorList,
  263. obs_data_array_t *savedPreviewProjectorList,
  264. obs_data_array_t *savedStudioProgramProjectorList,
  265. obs_data_array_t *savedMultiviewProjectorList)
  266. {
  267. obs_data_t *saveData = obs_data_create();
  268. vector<OBSSource> audioSources;
  269. audioSources.reserve(5);
  270. SaveAudioDevice(DESKTOP_AUDIO_1, 1, saveData, audioSources);
  271. SaveAudioDevice(DESKTOP_AUDIO_2, 2, saveData, audioSources);
  272. SaveAudioDevice(AUX_AUDIO_1, 3, saveData, audioSources);
  273. SaveAudioDevice(AUX_AUDIO_2, 4, saveData, audioSources);
  274. SaveAudioDevice(AUX_AUDIO_3, 5, saveData, audioSources);
  275. auto FilterAudioSources = [&](obs_source_t *source)
  276. {
  277. return find(begin(audioSources), end(audioSources), source) ==
  278. end(audioSources);
  279. };
  280. using FilterAudioSources_t = decltype(FilterAudioSources);
  281. obs_data_array_t *sourcesArray = obs_save_sources_filtered(
  282. [](void *data, obs_source_t *source)
  283. {
  284. return (*static_cast<FilterAudioSources_t*>(data))(source);
  285. }, static_cast<void*>(&FilterAudioSources));
  286. obs_source_t *transition = obs_get_output_source(0);
  287. obs_source_t *currentScene = obs_scene_get_source(scene);
  288. const char *sceneName = obs_source_get_name(currentScene);
  289. const char *programName = obs_source_get_name(curProgramScene);
  290. const char *sceneCollection = config_get_string(App()->GlobalConfig(),
  291. "Basic", "SceneCollection");
  292. obs_data_set_string(saveData, "current_scene", sceneName);
  293. obs_data_set_string(saveData, "current_program_scene", programName);
  294. obs_data_set_array(saveData, "scene_order", sceneOrder);
  295. obs_data_set_string(saveData, "name", sceneCollection);
  296. obs_data_set_array(saveData, "sources", sourcesArray);
  297. obs_data_set_array(saveData, "quick_transitions", quickTransitionData);
  298. obs_data_set_array(saveData, "transitions", transitions);
  299. obs_data_set_array(saveData, "saved_projectors", savedProjectorList);
  300. obs_data_set_array(saveData, "saved_preview_projectors",
  301. savedPreviewProjectorList);
  302. obs_data_set_array(saveData, "saved_studio_preview_projectors",
  303. savedStudioProgramProjectorList);
  304. obs_data_set_array(saveData, "saved_multiview_projectors",
  305. savedMultiviewProjectorList);
  306. obs_data_array_release(sourcesArray);
  307. obs_data_set_string(saveData, "current_transition",
  308. obs_source_get_name(transition));
  309. obs_data_set_int(saveData, "transition_duration", transitionDuration);
  310. obs_source_release(transition);
  311. return saveData;
  312. }
  313. void OBSBasic::copyActionsDynamicProperties()
  314. {
  315. // Themes need the QAction dynamic properties
  316. for (QAction *x : ui->scenesToolbar->actions()) {
  317. QWidget* temp = ui->scenesToolbar->widgetForAction(x);
  318. for (QByteArray &y : x->dynamicPropertyNames()) {
  319. temp->setProperty(y, x->property(y));
  320. }
  321. }
  322. for (QAction *x : ui->sourcesToolbar->actions()) {
  323. QWidget* temp = ui->sourcesToolbar->widgetForAction(x);
  324. for (QByteArray &y : x->dynamicPropertyNames()) {
  325. temp->setProperty(y, x->property(y));
  326. }
  327. }
  328. }
  329. void OBSBasic::ClearVolumeControls()
  330. {
  331. VolControl *control;
  332. for (size_t i = 0; i < volumes.size(); i++) {
  333. control = volumes[i];
  334. delete control;
  335. }
  336. volumes.clear();
  337. }
  338. obs_data_array_t *OBSBasic::SaveSceneListOrder()
  339. {
  340. obs_data_array_t *sceneOrder = obs_data_array_create();
  341. for (int i = 0; i < ui->scenes->count(); i++) {
  342. obs_data_t *data = obs_data_create();
  343. obs_data_set_string(data, "name",
  344. QT_TO_UTF8(ui->scenes->item(i)->text()));
  345. obs_data_array_push_back(sceneOrder, data);
  346. obs_data_release(data);
  347. }
  348. return sceneOrder;
  349. }
  350. obs_data_array_t *OBSBasic::SaveProjectors()
  351. {
  352. obs_data_array_t *saveProjector = obs_data_array_create();
  353. for (size_t i = 0; i < projectorArray.size(); i++) {
  354. obs_data_t *data = obs_data_create();
  355. obs_data_set_string(data, "saved_projectors",
  356. projectorArray.at(i).c_str());
  357. obs_data_array_push_back(saveProjector, data);
  358. obs_data_release(data);
  359. }
  360. return saveProjector;
  361. }
  362. obs_data_array_t *OBSBasic::SavePreviewProjectors()
  363. {
  364. obs_data_array_t *saveProjector = obs_data_array_create();
  365. for (size_t i = 0; i < previewProjectorArray.size(); i++) {
  366. obs_data_t *data = obs_data_create();
  367. obs_data_set_int(data, "saved_preview_projectors",
  368. previewProjectorArray.at(i));
  369. obs_data_array_push_back(saveProjector, data);
  370. obs_data_release(data);
  371. }
  372. return saveProjector;
  373. }
  374. obs_data_array_t *OBSBasic::SaveStudioProgramProjectors()
  375. {
  376. obs_data_array_t *saveProjector = obs_data_array_create();
  377. for (size_t i = 0; i < studioProgramProjectorArray.size(); i++) {
  378. obs_data_t *data = obs_data_create();
  379. obs_data_set_int(data, "saved_studio_preview_projectors",
  380. studioProgramProjectorArray.at(i));
  381. obs_data_array_push_back(saveProjector, data);
  382. obs_data_release(data);
  383. }
  384. return saveProjector;
  385. }
  386. obs_data_array_t *OBSBasic::SaveMultiviewProjectors()
  387. {
  388. obs_data_array_t *saveProjector = obs_data_array_create();
  389. for (size_t i = 0; i < multiviewProjectorArray.size(); i++) {
  390. obs_data_t *data = obs_data_create();
  391. obs_data_set_int(data, "saved_multiview_projectors",
  392. multiviewProjectorArray.at(i));
  393. obs_data_array_push_back(saveProjector, data);
  394. obs_data_release(data);
  395. }
  396. return saveProjector;
  397. }
  398. void OBSBasic::Save(const char *file)
  399. {
  400. OBSScene scene = GetCurrentScene();
  401. OBSSource curProgramScene = OBSGetStrongRef(programScene);
  402. if (!curProgramScene)
  403. curProgramScene = obs_scene_get_source(scene);
  404. obs_data_array_t *sceneOrder = SaveSceneListOrder();
  405. obs_data_array_t *transitions = SaveTransitions();
  406. obs_data_array_t *quickTrData = SaveQuickTransitions();
  407. obs_data_array_t *savedProjectorList = SaveProjectors();
  408. obs_data_array_t *savedPreviewProjectorList = SavePreviewProjectors();
  409. obs_data_array_t *savedStudioProgramProjectorList =
  410. SaveStudioProgramProjectors();
  411. obs_data_array_t *savedMultiviewProjectorList =
  412. SaveMultiviewProjectors();
  413. obs_data_t *saveData = GenerateSaveData(sceneOrder, quickTrData,
  414. ui->transitionDuration->value(), transitions,
  415. scene, curProgramScene, savedProjectorList,
  416. savedPreviewProjectorList,
  417. savedStudioProgramProjectorList,
  418. savedMultiviewProjectorList);
  419. obs_data_set_bool(saveData, "preview_locked", ui->preview->Locked());
  420. obs_data_set_bool(saveData, "scaling_enabled",
  421. ui->preview->IsFixedScaling());
  422. obs_data_set_int(saveData, "scaling_level",
  423. ui->preview->GetScalingLevel());
  424. obs_data_set_double(saveData, "scaling_off_x",
  425. ui->preview->GetScrollX());
  426. obs_data_set_double(saveData, "scaling_off_y",
  427. ui->preview->GetScrollY());
  428. if (api) {
  429. obs_data_t *moduleObj = obs_data_create();
  430. api->on_save(moduleObj);
  431. obs_data_set_obj(saveData, "modules", moduleObj);
  432. obs_data_release(moduleObj);
  433. }
  434. if (!obs_data_save_json_safe(saveData, file, "tmp", "bak"))
  435. blog(LOG_ERROR, "Could not save scene data to %s", file);
  436. obs_data_release(saveData);
  437. obs_data_array_release(sceneOrder);
  438. obs_data_array_release(quickTrData);
  439. obs_data_array_release(transitions);
  440. obs_data_array_release(savedProjectorList);
  441. obs_data_array_release(savedPreviewProjectorList);
  442. obs_data_array_release(savedStudioProgramProjectorList);
  443. obs_data_array_release(savedMultiviewProjectorList);
  444. }
  445. static void LoadAudioDevice(const char *name, int channel, obs_data_t *parent)
  446. {
  447. obs_data_t *data = obs_data_get_obj(parent, name);
  448. if (!data)
  449. return;
  450. obs_source_t *source = obs_load_source(data);
  451. if (source) {
  452. obs_set_output_source(channel, source);
  453. obs_source_release(source);
  454. }
  455. obs_data_release(data);
  456. }
  457. static inline bool HasAudioDevices(const char *source_id)
  458. {
  459. const char *output_id = source_id;
  460. obs_properties_t *props = obs_get_source_properties(output_id);
  461. size_t count = 0;
  462. if (!props)
  463. return false;
  464. obs_property_t *devices = obs_properties_get(props, "device_id");
  465. if (devices)
  466. count = obs_property_list_item_count(devices);
  467. obs_properties_destroy(props);
  468. return count != 0;
  469. }
  470. void OBSBasic::CreateFirstRunSources()
  471. {
  472. bool hasDesktopAudio = HasAudioDevices(App()->OutputAudioSource());
  473. bool hasInputAudio = HasAudioDevices(App()->InputAudioSource());
  474. if (hasDesktopAudio)
  475. ResetAudioDevice(App()->OutputAudioSource(), "default",
  476. Str("Basic.DesktopDevice1"), 1);
  477. if (hasInputAudio)
  478. ResetAudioDevice(App()->InputAudioSource(), "default",
  479. Str("Basic.AuxDevice1"), 3);
  480. }
  481. void OBSBasic::CreateDefaultScene(bool firstStart)
  482. {
  483. disableSaving++;
  484. ClearSceneData();
  485. InitDefaultTransitions();
  486. CreateDefaultQuickTransitions();
  487. ui->transitionDuration->setValue(300);
  488. SetTransition(fadeTransition);
  489. obs_scene_t *scene = obs_scene_create(Str("Basic.Scene"));
  490. if (firstStart)
  491. CreateFirstRunSources();
  492. AddScene(obs_scene_get_source(scene));
  493. SetCurrentScene(scene, true);
  494. obs_scene_release(scene);
  495. disableSaving--;
  496. }
  497. static void ReorderItemByName(QListWidget *lw, const char *name, int newIndex)
  498. {
  499. for (int i = 0; i < lw->count(); i++) {
  500. QListWidgetItem *item = lw->item(i);
  501. if (strcmp(name, QT_TO_UTF8(item->text())) == 0) {
  502. if (newIndex != i) {
  503. item = lw->takeItem(i);
  504. lw->insertItem(newIndex, item);
  505. }
  506. break;
  507. }
  508. }
  509. }
  510. void OBSBasic::LoadSceneListOrder(obs_data_array_t *array)
  511. {
  512. size_t num = obs_data_array_count(array);
  513. for (size_t i = 0; i < num; i++) {
  514. obs_data_t *data = obs_data_array_item(array, i);
  515. const char *name = obs_data_get_string(data, "name");
  516. ReorderItemByName(ui->scenes, name, (int)i);
  517. obs_data_release(data);
  518. }
  519. }
  520. void OBSBasic::LoadSavedProjectors(obs_data_array_t *array)
  521. {
  522. size_t num = obs_data_array_count(array);
  523. for (size_t i = 0; i < num; i++) {
  524. obs_data_t *data = obs_data_array_item(array, i);
  525. projectorArray.at(i) = obs_data_get_string(data,
  526. "saved_projectors");
  527. obs_data_release(data);
  528. }
  529. }
  530. void OBSBasic::LoadSavedPreviewProjectors(obs_data_array_t *array)
  531. {
  532. size_t num = obs_data_array_count(array);
  533. for (size_t i = 0; i < num; i++) {
  534. obs_data_t *data = obs_data_array_item(array, i);
  535. previewProjectorArray.at(i) = obs_data_get_int(data,
  536. "saved_preview_projectors");
  537. obs_data_release(data);
  538. }
  539. }
  540. void OBSBasic::LoadSavedStudioProgramProjectors(obs_data_array_t *array)
  541. {
  542. size_t num = obs_data_array_count(array);
  543. for (size_t i = 0; i < num; i++) {
  544. obs_data_t *data = obs_data_array_item(array, i);
  545. studioProgramProjectorArray.at(i) = obs_data_get_int(data,
  546. "saved_studio_preview_projectors");
  547. obs_data_release(data);
  548. }
  549. }
  550. void OBSBasic::LoadSavedMultiviewProjectors(obs_data_array_t *array)
  551. {
  552. size_t num = obs_data_array_count(array);
  553. for (size_t i = 0; i < num; i++) {
  554. obs_data_t *data = obs_data_array_item(array, i);
  555. multiviewProjectorArray.at(i) = obs_data_get_int(data,
  556. "saved_multiview_projectors");
  557. obs_data_release(data);
  558. }
  559. }
  560. static void LogFilter(obs_source_t*, obs_source_t *filter, void *v_val)
  561. {
  562. const char *name = obs_source_get_name(filter);
  563. const char *id = obs_source_get_id(filter);
  564. int val = (int)(intptr_t)v_val;
  565. string indent;
  566. for (int i = 0; i < val; i++)
  567. indent += " ";
  568. blog(LOG_INFO, "%s- filter: '%s' (%s)", indent.c_str(), name, id);
  569. }
  570. static bool LogSceneItem(obs_scene_t*, obs_sceneitem_t *item, void*)
  571. {
  572. obs_source_t *source = obs_sceneitem_get_source(item);
  573. const char *name = obs_source_get_name(source);
  574. const char *id = obs_source_get_id(source);
  575. blog(LOG_INFO, " - source: '%s' (%s)", name, id);
  576. obs_monitoring_type monitoring_type =
  577. obs_source_get_monitoring_type(source);
  578. if (monitoring_type != OBS_MONITORING_TYPE_NONE) {
  579. const char *type =
  580. (monitoring_type == OBS_MONITORING_TYPE_MONITOR_ONLY)
  581. ? "monitor only"
  582. : "monitor and output";
  583. blog(LOG_INFO, " - monitoring: %s", type);
  584. }
  585. obs_source_enum_filters(source, LogFilter, (void*)(intptr_t)2);
  586. return true;
  587. }
  588. void OBSBasic::LogScenes()
  589. {
  590. blog(LOG_INFO, "------------------------------------------------");
  591. blog(LOG_INFO, "Loaded scenes:");
  592. for (int i = 0; i < ui->scenes->count(); i++) {
  593. QListWidgetItem *item = ui->scenes->item(i);
  594. OBSScene scene = GetOBSRef<OBSScene>(item);
  595. obs_source_t *source = obs_scene_get_source(scene);
  596. const char *name = obs_source_get_name(source);
  597. blog(LOG_INFO, "- scene '%s':", name);
  598. obs_scene_enum_items(scene, LogSceneItem, nullptr);
  599. obs_source_enum_filters(source, LogFilter, (void*)(intptr_t)1);
  600. }
  601. blog(LOG_INFO, "------------------------------------------------");
  602. }
  603. void OBSBasic::Load(const char *file)
  604. {
  605. disableSaving++;
  606. obs_data_t *data = obs_data_create_from_json_file_safe(file, "bak");
  607. if (!data) {
  608. disableSaving--;
  609. blog(LOG_INFO, "No scene file found, creating default scene");
  610. CreateDefaultScene(true);
  611. SaveProject();
  612. return;
  613. }
  614. ClearSceneData();
  615. InitDefaultTransitions();
  616. obs_data_array_t *sceneOrder = obs_data_get_array(data, "scene_order");
  617. obs_data_array_t *sources = obs_data_get_array(data, "sources");
  618. obs_data_array_t *transitions= obs_data_get_array(data, "transitions");
  619. const char *sceneName = obs_data_get_string(data,
  620. "current_scene");
  621. const char *programSceneName = obs_data_get_string(data,
  622. "current_program_scene");
  623. const char *transitionName = obs_data_get_string(data,
  624. "current_transition");
  625. if (!opt_starting_scene.empty()) {
  626. programSceneName = opt_starting_scene.c_str();
  627. if (!IsPreviewProgramMode())
  628. sceneName = opt_starting_scene.c_str();
  629. }
  630. int newDuration = obs_data_get_int(data, "transition_duration");
  631. if (!newDuration)
  632. newDuration = 300;
  633. if (!transitionName)
  634. transitionName = obs_source_get_name(fadeTransition);
  635. const char *curSceneCollection = config_get_string(
  636. App()->GlobalConfig(), "Basic", "SceneCollection");
  637. obs_data_set_default_string(data, "name", curSceneCollection);
  638. const char *name = obs_data_get_string(data, "name");
  639. obs_source_t *curScene;
  640. obs_source_t *curProgramScene;
  641. obs_source_t *curTransition;
  642. if (!name || !*name)
  643. name = curSceneCollection;
  644. LoadAudioDevice(DESKTOP_AUDIO_1, 1, data);
  645. LoadAudioDevice(DESKTOP_AUDIO_2, 2, data);
  646. LoadAudioDevice(AUX_AUDIO_1, 3, data);
  647. LoadAudioDevice(AUX_AUDIO_2, 4, data);
  648. LoadAudioDevice(AUX_AUDIO_3, 5, data);
  649. obs_load_sources(sources, OBSBasic::SourceLoaded, this);
  650. if (transitions)
  651. LoadTransitions(transitions);
  652. if (sceneOrder)
  653. LoadSceneListOrder(sceneOrder);
  654. obs_data_array_release(transitions);
  655. curTransition = FindTransition(transitionName);
  656. if (!curTransition)
  657. curTransition = fadeTransition;
  658. ui->transitionDuration->setValue(newDuration);
  659. SetTransition(curTransition);
  660. /* ------------------- */
  661. obs_data_array_t *savedProjectors = obs_data_get_array(data,
  662. "saved_projectors");
  663. if (savedProjectors)
  664. LoadSavedProjectors(savedProjectors);
  665. obs_data_array_release(savedProjectors);
  666. /* ------------------- */
  667. obs_data_array_t *savedPreviewProjectors = obs_data_get_array(data,
  668. "saved_preview_projectors");
  669. if (savedPreviewProjectors)
  670. LoadSavedPreviewProjectors(savedPreviewProjectors);
  671. obs_data_array_release(savedPreviewProjectors);
  672. /* ------------------- */
  673. obs_data_array_t *savedStudioProgramProjectors = obs_data_get_array(data,
  674. "saved_studio_preview_projectors");
  675. if (savedStudioProgramProjectors)
  676. LoadSavedStudioProgramProjectors(savedStudioProgramProjectors);
  677. obs_data_array_release(savedStudioProgramProjectors);
  678. /* ------------------- */
  679. obs_data_array_t *savedMultiviewProjectors = obs_data_get_array(data,
  680. "saved_multiview_projectors");
  681. if (savedMultiviewProjectors)
  682. LoadSavedMultiviewProjectors(savedMultiviewProjectors);
  683. obs_data_array_release(savedMultiviewProjectors);
  684. retryScene:
  685. curScene = obs_get_source_by_name(sceneName);
  686. curProgramScene = obs_get_source_by_name(programSceneName);
  687. /* if the starting scene command line parameter is bad at all,
  688. * fall back to original settings */
  689. if (!opt_starting_scene.empty() && (!curScene || !curProgramScene)) {
  690. sceneName = obs_data_get_string(data, "current_scene");
  691. programSceneName = obs_data_get_string(data,
  692. "current_program_scene");
  693. obs_source_release(curScene);
  694. obs_source_release(curProgramScene);
  695. opt_starting_scene.clear();
  696. goto retryScene;
  697. }
  698. if (!curProgramScene) {
  699. curProgramScene = curScene;
  700. obs_source_addref(curScene);
  701. }
  702. SetCurrentScene(curScene, true);
  703. if (IsPreviewProgramMode())
  704. TransitionToScene(curProgramScene, true);
  705. obs_source_release(curScene);
  706. obs_source_release(curProgramScene);
  707. obs_data_array_release(sources);
  708. obs_data_array_release(sceneOrder);
  709. std::string file_base = strrchr(file, '/') + 1;
  710. file_base.erase(file_base.size() - 5, 5);
  711. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollection",
  712. name);
  713. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollectionFile",
  714. file_base.c_str());
  715. obs_data_array_t *quickTransitionData = obs_data_get_array(data,
  716. "quick_transitions");
  717. LoadQuickTransitions(quickTransitionData);
  718. obs_data_array_release(quickTransitionData);
  719. RefreshQuickTransitions();
  720. bool previewLocked = obs_data_get_bool(data, "preview_locked");
  721. ui->preview->SetLocked(previewLocked);
  722. ui->actionLockPreview->setChecked(previewLocked);
  723. /* ---------------------- */
  724. bool fixedScaling = obs_data_get_bool(data, "scaling_enabled");
  725. int scalingLevel = (int)obs_data_get_int(data, "scaling_level");
  726. float scrollOffX = (float)obs_data_get_double(data, "scaling_off_x");
  727. float scrollOffY = (float)obs_data_get_double(data, "scaling_off_y");
  728. if (fixedScaling) {
  729. ui->preview->SetScalingLevel(scalingLevel);
  730. ui->preview->SetScrollingOffset(scrollOffX, scrollOffY);
  731. }
  732. ui->preview->SetFixedScaling(fixedScaling);
  733. /* ---------------------- */
  734. if (api) {
  735. obs_data_t *modulesObj = obs_data_get_obj(data, "modules");
  736. api->on_load(modulesObj);
  737. obs_data_release(modulesObj);
  738. }
  739. obs_data_release(data);
  740. if (!opt_starting_scene.empty())
  741. opt_starting_scene.clear();
  742. if (opt_start_streaming) {
  743. blog(LOG_INFO, "Starting stream due to command line parameter");
  744. QMetaObject::invokeMethod(this, "StartStreaming",
  745. Qt::QueuedConnection);
  746. opt_start_streaming = false;
  747. }
  748. if (opt_start_recording) {
  749. blog(LOG_INFO, "Starting recording due to command line parameter");
  750. QMetaObject::invokeMethod(this, "StartRecording",
  751. Qt::QueuedConnection);
  752. opt_start_recording = false;
  753. }
  754. if (opt_start_replaybuffer) {
  755. QMetaObject::invokeMethod(this, "StartReplayBuffer",
  756. Qt::QueuedConnection);
  757. opt_start_replaybuffer = false;
  758. }
  759. LogScenes();
  760. disableSaving--;
  761. }
  762. #define SERVICE_PATH "service.json"
  763. void OBSBasic::SaveService()
  764. {
  765. if (!service)
  766. return;
  767. char serviceJsonPath[512];
  768. int ret = GetProfilePath(serviceJsonPath, sizeof(serviceJsonPath),
  769. SERVICE_PATH);
  770. if (ret <= 0)
  771. return;
  772. obs_data_t *data = obs_data_create();
  773. obs_data_t *settings = obs_service_get_settings(service);
  774. obs_data_set_string(data, "type", obs_service_get_type(service));
  775. obs_data_set_obj(data, "settings", settings);
  776. if (!obs_data_save_json_safe(data, serviceJsonPath, "tmp", "bak"))
  777. blog(LOG_WARNING, "Failed to save service");
  778. obs_data_release(settings);
  779. obs_data_release(data);
  780. }
  781. bool OBSBasic::LoadService()
  782. {
  783. const char *type;
  784. char serviceJsonPath[512];
  785. int ret = GetProfilePath(serviceJsonPath, sizeof(serviceJsonPath),
  786. SERVICE_PATH);
  787. if (ret <= 0)
  788. return false;
  789. obs_data_t *data = obs_data_create_from_json_file_safe(serviceJsonPath,
  790. "bak");
  791. obs_data_set_default_string(data, "type", "rtmp_common");
  792. type = obs_data_get_string(data, "type");
  793. obs_data_t *settings = obs_data_get_obj(data, "settings");
  794. obs_data_t *hotkey_data = obs_data_get_obj(data, "hotkeys");
  795. service = obs_service_create(type, "default_service", settings,
  796. hotkey_data);
  797. obs_service_release(service);
  798. obs_data_release(hotkey_data);
  799. obs_data_release(settings);
  800. obs_data_release(data);
  801. return !!service;
  802. }
  803. bool OBSBasic::InitService()
  804. {
  805. ProfileScope("OBSBasic::InitService");
  806. if (LoadService())
  807. return true;
  808. service = obs_service_create("rtmp_common", "default_service", nullptr,
  809. nullptr);
  810. if (!service)
  811. return false;
  812. obs_service_release(service);
  813. return true;
  814. }
  815. static const double scaled_vals[] =
  816. {
  817. 1.0,
  818. 1.25,
  819. (1.0/0.75),
  820. 1.5,
  821. (1.0/0.6),
  822. 1.75,
  823. 2.0,
  824. 2.25,
  825. 2.5,
  826. 2.75,
  827. 3.0,
  828. 0.0
  829. };
  830. bool OBSBasic::InitBasicConfigDefaults()
  831. {
  832. QList<QScreen*> screens = QGuiApplication::screens();
  833. if (!screens.size()) {
  834. OBSErrorBox(NULL, "There appears to be no monitors. Er, this "
  835. "technically shouldn't be possible.");
  836. return false;
  837. }
  838. QScreen *primaryScreen = QGuiApplication::primaryScreen();
  839. uint32_t cx = primaryScreen->size().width();
  840. uint32_t cy = primaryScreen->size().height();
  841. bool oldResolutionDefaults = config_get_bool(App()->GlobalConfig(),
  842. "General", "Pre19Defaults");
  843. /* use 1920x1080 for new default base res if main monitor is above
  844. * 1920x1080, but don't apply for people from older builds -- only to
  845. * new users */
  846. if (!oldResolutionDefaults && (cx * cy) > (1920 * 1080)) {
  847. cx = 1920;
  848. cy = 1080;
  849. }
  850. /* ----------------------------------------------------- */
  851. /* move over mixer values in advanced if older config */
  852. if (config_has_user_value(basicConfig, "AdvOut", "RecTrackIndex") &&
  853. !config_has_user_value(basicConfig, "AdvOut", "RecTracks")) {
  854. uint64_t track = config_get_uint(basicConfig, "AdvOut",
  855. "RecTrackIndex");
  856. track = 1ULL << (track - 1);
  857. config_set_uint(basicConfig, "AdvOut", "RecTracks", track);
  858. config_remove_value(basicConfig, "AdvOut", "RecTrackIndex");
  859. config_save_safe(basicConfig, "tmp", nullptr);
  860. }
  861. /* ----------------------------------------------------- */
  862. config_set_default_string(basicConfig, "Output", "Mode", "Simple");
  863. config_set_default_string(basicConfig, "SimpleOutput", "FilePath",
  864. GetDefaultVideoSavePath().c_str());
  865. config_set_default_string(basicConfig, "SimpleOutput", "RecFormat",
  866. "flv");
  867. config_set_default_uint (basicConfig, "SimpleOutput", "VBitrate",
  868. 2500);
  869. config_set_default_string(basicConfig, "SimpleOutput", "StreamEncoder",
  870. SIMPLE_ENCODER_X264);
  871. config_set_default_uint (basicConfig, "SimpleOutput", "ABitrate", 160);
  872. config_set_default_bool (basicConfig, "SimpleOutput", "UseAdvanced",
  873. false);
  874. config_set_default_bool (basicConfig, "SimpleOutput", "EnforceBitrate",
  875. true);
  876. config_set_default_string(basicConfig, "SimpleOutput", "Preset",
  877. "veryfast");
  878. config_set_default_string(basicConfig, "SimpleOutput", "RecQuality",
  879. "Stream");
  880. config_set_default_string(basicConfig, "SimpleOutput", "RecEncoder",
  881. SIMPLE_ENCODER_X264);
  882. config_set_default_bool(basicConfig, "SimpleOutput", "RecRB", false);
  883. config_set_default_int(basicConfig, "SimpleOutput", "RecRBTime", 20);
  884. config_set_default_int(basicConfig, "SimpleOutput", "RecRBSize", 512);
  885. config_set_default_string(basicConfig, "SimpleOutput", "RecRBPrefix",
  886. "Replay");
  887. config_set_default_bool (basicConfig, "AdvOut", "ApplyServiceSettings",
  888. true);
  889. config_set_default_bool (basicConfig, "AdvOut", "UseRescale", false);
  890. config_set_default_uint (basicConfig, "AdvOut", "TrackIndex", 1);
  891. config_set_default_string(basicConfig, "AdvOut", "Encoder", "obs_x264");
  892. config_set_default_string(basicConfig, "AdvOut", "RecType", "Standard");
  893. config_set_default_string(basicConfig, "AdvOut", "RecFilePath",
  894. GetDefaultVideoSavePath().c_str());
  895. config_set_default_string(basicConfig, "AdvOut", "RecFormat", "flv");
  896. config_set_default_bool (basicConfig, "AdvOut", "RecUseRescale",
  897. false);
  898. config_set_default_uint (basicConfig, "AdvOut", "RecTracks", (1<<0));
  899. config_set_default_string(basicConfig, "AdvOut", "RecEncoder",
  900. "none");
  901. config_set_default_bool (basicConfig, "AdvOut", "FFOutputToFile",
  902. true);
  903. config_set_default_string(basicConfig, "AdvOut", "FFFilePath",
  904. GetDefaultVideoSavePath().c_str());
  905. config_set_default_string(basicConfig, "AdvOut", "FFExtension", "mp4");
  906. config_set_default_uint (basicConfig, "AdvOut", "FFVBitrate", 2500);
  907. config_set_default_uint (basicConfig, "AdvOut", "FFVGOPSize", 250);
  908. config_set_default_bool (basicConfig, "AdvOut", "FFUseRescale",
  909. false);
  910. config_set_default_bool (basicConfig, "AdvOut", "FFIgnoreCompat",
  911. false);
  912. config_set_default_uint (basicConfig, "AdvOut", "FFABitrate", 160);
  913. config_set_default_uint (basicConfig, "AdvOut", "FFAudioTrack", 1);
  914. config_set_default_uint (basicConfig, "AdvOut", "Track1Bitrate", 160);
  915. config_set_default_uint (basicConfig, "AdvOut", "Track2Bitrate", 160);
  916. config_set_default_uint (basicConfig, "AdvOut", "Track3Bitrate", 160);
  917. config_set_default_uint (basicConfig, "AdvOut", "Track4Bitrate", 160);
  918. config_set_default_uint (basicConfig, "AdvOut", "Track5Bitrate", 160);
  919. config_set_default_uint (basicConfig, "AdvOut", "Track6Bitrate", 160);
  920. config_set_default_bool (basicConfig, "AdvOut", "RecRB", false);
  921. config_set_default_uint (basicConfig, "AdvOut", "RecRBTime", 20);
  922. config_set_default_int (basicConfig, "AdvOut", "RecRBSize", 512);
  923. config_set_default_uint (basicConfig, "Video", "BaseCX", cx);
  924. config_set_default_uint (basicConfig, "Video", "BaseCY", cy);
  925. /* don't allow BaseCX/BaseCY to be susceptible to defaults changing */
  926. if (!config_has_user_value(basicConfig, "Video", "BaseCX") ||
  927. !config_has_user_value(basicConfig, "Video", "BaseCY")) {
  928. config_set_uint(basicConfig, "Video", "BaseCX", cx);
  929. config_set_uint(basicConfig, "Video", "BaseCY", cy);
  930. config_save_safe(basicConfig, "tmp", nullptr);
  931. }
  932. config_set_default_string(basicConfig, "Output", "FilenameFormatting",
  933. "%CCYY-%MM-%DD %hh-%mm-%ss");
  934. config_set_default_bool (basicConfig, "Output", "DelayEnable", false);
  935. config_set_default_uint (basicConfig, "Output", "DelaySec", 20);
  936. config_set_default_bool (basicConfig, "Output", "DelayPreserve", true);
  937. config_set_default_bool (basicConfig, "Output", "Reconnect", true);
  938. config_set_default_uint (basicConfig, "Output", "RetryDelay", 10);
  939. config_set_default_uint (basicConfig, "Output", "MaxRetries", 20);
  940. config_set_default_string(basicConfig, "Output", "BindIP", "default");
  941. config_set_default_bool (basicConfig, "Output", "NewSocketLoopEnable",
  942. false);
  943. config_set_default_bool (basicConfig, "Output", "LowLatencyEnable",
  944. false);
  945. int i = 0;
  946. uint32_t scale_cx = cx;
  947. uint32_t scale_cy = cy;
  948. /* use a default scaled resolution that has a pixel count no higher
  949. * than 1280x720 */
  950. while (((scale_cx * scale_cy) > (1280 * 720)) && scaled_vals[i] > 0.0) {
  951. double scale = scaled_vals[i++];
  952. scale_cx = uint32_t(double(cx) / scale);
  953. scale_cy = uint32_t(double(cy) / scale);
  954. }
  955. config_set_default_uint (basicConfig, "Video", "OutputCX", scale_cx);
  956. config_set_default_uint (basicConfig, "Video", "OutputCY", scale_cy);
  957. /* don't allow OutputCX/OutputCY to be susceptible to defaults
  958. * changing */
  959. if (!config_has_user_value(basicConfig, "Video", "OutputCX") ||
  960. !config_has_user_value(basicConfig, "Video", "OutputCY")) {
  961. config_set_uint(basicConfig, "Video", "OutputCX", scale_cx);
  962. config_set_uint(basicConfig, "Video", "OutputCY", scale_cy);
  963. config_save_safe(basicConfig, "tmp", nullptr);
  964. }
  965. config_set_default_uint (basicConfig, "Video", "FPSType", 0);
  966. config_set_default_string(basicConfig, "Video", "FPSCommon", "30");
  967. config_set_default_uint (basicConfig, "Video", "FPSInt", 30);
  968. config_set_default_uint (basicConfig, "Video", "FPSNum", 30);
  969. config_set_default_uint (basicConfig, "Video", "FPSDen", 1);
  970. config_set_default_string(basicConfig, "Video", "ScaleType", "bicubic");
  971. config_set_default_string(basicConfig, "Video", "ColorFormat", "NV12");
  972. config_set_default_string(basicConfig, "Video", "ColorSpace", "601");
  973. config_set_default_string(basicConfig, "Video", "ColorRange",
  974. "Partial");
  975. config_set_default_string(basicConfig, "Audio", "MonitoringDeviceId",
  976. "default");
  977. config_set_default_string(basicConfig, "Audio", "MonitoringDeviceName",
  978. Str("Basic.Settings.Advanced.Audio.MonitoringDevice"
  979. ".Default"));
  980. config_set_default_uint (basicConfig, "Audio", "SampleRate", 44100);
  981. config_set_default_string(basicConfig, "Audio", "ChannelSetup",
  982. "Stereo");
  983. return true;
  984. }
  985. bool OBSBasic::InitBasicConfig()
  986. {
  987. ProfileScope("OBSBasic::InitBasicConfig");
  988. char configPath[512];
  989. int ret = GetProfilePath(configPath, sizeof(configPath), "");
  990. if (ret <= 0) {
  991. OBSErrorBox(nullptr, "Failed to get profile path");
  992. return false;
  993. }
  994. if (os_mkdir(configPath) == MKDIR_ERROR) {
  995. OBSErrorBox(nullptr, "Failed to create profile path");
  996. return false;
  997. }
  998. ret = GetProfilePath(configPath, sizeof(configPath), "basic.ini");
  999. if (ret <= 0) {
  1000. OBSErrorBox(nullptr, "Failed to get base.ini path");
  1001. return false;
  1002. }
  1003. int code = basicConfig.Open(configPath, CONFIG_OPEN_ALWAYS);
  1004. if (code != CONFIG_SUCCESS) {
  1005. OBSErrorBox(NULL, "Failed to open basic.ini: %d", code);
  1006. return false;
  1007. }
  1008. if (config_get_string(basicConfig, "General", "Name") == nullptr) {
  1009. const char *curName = config_get_string(App()->GlobalConfig(),
  1010. "Basic", "Profile");
  1011. config_set_string(basicConfig, "General", "Name", curName);
  1012. basicConfig.SaveSafe("tmp");
  1013. }
  1014. return InitBasicConfigDefaults();
  1015. }
  1016. void OBSBasic::InitOBSCallbacks()
  1017. {
  1018. ProfileScope("OBSBasic::InitOBSCallbacks");
  1019. signalHandlers.reserve(signalHandlers.size() + 6);
  1020. signalHandlers.emplace_back(obs_get_signal_handler(), "source_remove",
  1021. OBSBasic::SourceRemoved, this);
  1022. signalHandlers.emplace_back(obs_get_signal_handler(), "source_activate",
  1023. OBSBasic::SourceActivated, this);
  1024. signalHandlers.emplace_back(obs_get_signal_handler(), "source_deactivate",
  1025. OBSBasic::SourceDeactivated, this);
  1026. signalHandlers.emplace_back(obs_get_signal_handler(), "source_rename",
  1027. OBSBasic::SourceRenamed, this);
  1028. }
  1029. void OBSBasic::InitPrimitives()
  1030. {
  1031. ProfileScope("OBSBasic::InitPrimitives");
  1032. obs_enter_graphics();
  1033. gs_render_start(true);
  1034. gs_vertex2f(0.0f, 0.0f);
  1035. gs_vertex2f(0.0f, 1.0f);
  1036. gs_vertex2f(1.0f, 1.0f);
  1037. gs_vertex2f(1.0f, 0.0f);
  1038. gs_vertex2f(0.0f, 0.0f);
  1039. box = gs_render_save();
  1040. gs_render_start(true);
  1041. gs_vertex2f(0.0f, 0.0f);
  1042. gs_vertex2f(0.0f, 1.0f);
  1043. boxLeft = gs_render_save();
  1044. gs_render_start(true);
  1045. gs_vertex2f(0.0f, 0.0f);
  1046. gs_vertex2f(1.0f, 0.0f);
  1047. boxTop = gs_render_save();
  1048. gs_render_start(true);
  1049. gs_vertex2f(1.0f, 0.0f);
  1050. gs_vertex2f(1.0f, 1.0f);
  1051. boxRight = gs_render_save();
  1052. gs_render_start(true);
  1053. gs_vertex2f(0.0f, 1.0f);
  1054. gs_vertex2f(1.0f, 1.0f);
  1055. boxBottom = gs_render_save();
  1056. gs_render_start(true);
  1057. for (int i = 0; i <= 360; i += (360/20)) {
  1058. float pos = RAD(float(i));
  1059. gs_vertex2f(cosf(pos), sinf(pos));
  1060. }
  1061. circle = gs_render_save();
  1062. obs_leave_graphics();
  1063. }
  1064. void OBSBasic::ReplayBufferClicked()
  1065. {
  1066. if (outputHandler->ReplayBufferActive())
  1067. StopReplayBuffer();
  1068. else
  1069. StartReplayBuffer();
  1070. };
  1071. void OBSBasic::ResetOutputs()
  1072. {
  1073. ProfileScope("OBSBasic::ResetOutputs");
  1074. const char *mode = config_get_string(basicConfig, "Output", "Mode");
  1075. bool advOut = astrcmpi(mode, "Advanced") == 0;
  1076. if (!outputHandler || !outputHandler->Active()) {
  1077. outputHandler.reset();
  1078. outputHandler.reset(advOut ?
  1079. CreateAdvancedOutputHandler(this) :
  1080. CreateSimpleOutputHandler(this));
  1081. delete replayBufferButton;
  1082. if (outputHandler->replayBuffer) {
  1083. replayBufferButton = new QPushButton(
  1084. QTStr("Basic.Main.StartReplayBuffer"),
  1085. this);
  1086. connect(replayBufferButton.data(),
  1087. &QPushButton::clicked,
  1088. this,
  1089. &OBSBasic::ReplayBufferClicked);
  1090. replayBufferButton->setProperty("themeID", "replayBufferButton");
  1091. ui->buttonsVLayout->insertWidget(2, replayBufferButton);
  1092. }
  1093. if (sysTrayReplayBuffer)
  1094. sysTrayReplayBuffer->setEnabled(
  1095. !!outputHandler->replayBuffer);
  1096. } else {
  1097. outputHandler->Update();
  1098. }
  1099. }
  1100. static void AddProjectorMenuMonitors(QMenu *parent, QObject *target,
  1101. const char *slot);
  1102. #define STARTUP_SEPARATOR \
  1103. "==== Startup complete ==============================================="
  1104. #define SHUTDOWN_SEPARATOR \
  1105. "==== Shutting down =================================================="
  1106. extern obs_frontend_callbacks *InitializeAPIInterface(OBSBasic *main);
  1107. #define UNSUPPORTED_ERROR \
  1108. "Failed to initialize video:\n\nRequired graphics API functionality " \
  1109. "not found. Your GPU may not be supported."
  1110. #define UNKNOWN_ERROR \
  1111. "Failed to initialize video. Your GPU may not be supported, " \
  1112. "or your graphics drivers may need to be updated."
  1113. void OBSBasic::OBSInit()
  1114. {
  1115. ProfileScope("OBSBasic::OBSInit");
  1116. const char *sceneCollection = config_get_string(App()->GlobalConfig(),
  1117. "Basic", "SceneCollectionFile");
  1118. char savePath[512];
  1119. char fileName[512];
  1120. int ret;
  1121. if (!sceneCollection)
  1122. throw "Failed to get scene collection name";
  1123. ret = snprintf(fileName, 512, "obs-studio/basic/scenes/%s.json",
  1124. sceneCollection);
  1125. if (ret <= 0)
  1126. throw "Failed to create scene collection file name";
  1127. ret = GetConfigPath(savePath, sizeof(savePath), fileName);
  1128. if (ret <= 0)
  1129. throw "Failed to get scene collection json file path";
  1130. if (!InitBasicConfig())
  1131. throw "Failed to load basic.ini";
  1132. if (!ResetAudio())
  1133. throw "Failed to initialize audio";
  1134. ret = ResetVideo();
  1135. switch (ret) {
  1136. case OBS_VIDEO_MODULE_NOT_FOUND:
  1137. throw "Failed to initialize video: Graphics module not found";
  1138. case OBS_VIDEO_NOT_SUPPORTED:
  1139. throw UNSUPPORTED_ERROR;
  1140. case OBS_VIDEO_INVALID_PARAM:
  1141. throw "Failed to initialize video: Invalid parameters";
  1142. default:
  1143. if (ret != OBS_VIDEO_SUCCESS)
  1144. throw UNKNOWN_ERROR;
  1145. }
  1146. /* load audio monitoring */
  1147. #if defined(_WIN32) || defined(__APPLE__) || HAVE_PULSEAUDIO
  1148. const char *device_name = config_get_string(basicConfig, "Audio",
  1149. "MonitoringDeviceName");
  1150. const char *device_id = config_get_string(basicConfig, "Audio",
  1151. "MonitoringDeviceId");
  1152. obs_set_audio_monitoring_device(device_name, device_id);
  1153. blog(LOG_INFO, "Audio monitoring device:\n\tname: %s\n\tid: %s",
  1154. device_name, device_id);
  1155. #endif
  1156. InitOBSCallbacks();
  1157. InitHotkeys();
  1158. api = InitializeAPIInterface(this);
  1159. AddExtraModulePaths();
  1160. blog(LOG_INFO, "---------------------------------");
  1161. obs_load_all_modules();
  1162. blog(LOG_INFO, "---------------------------------");
  1163. obs_log_loaded_modules();
  1164. blog(LOG_INFO, "---------------------------------");
  1165. obs_post_load_modules();
  1166. blog(LOG_INFO, STARTUP_SEPARATOR);
  1167. ResetOutputs();
  1168. CreateHotkeys();
  1169. if (!InitService())
  1170. throw "Failed to initialize service";
  1171. InitPrimitives();
  1172. sceneDuplicationMode = config_get_bool(App()->GlobalConfig(),
  1173. "BasicWindow", "SceneDuplicationMode");
  1174. swapScenesMode = config_get_bool(App()->GlobalConfig(),
  1175. "BasicWindow", "SwapScenesMode");
  1176. editPropertiesMode = config_get_bool(App()->GlobalConfig(),
  1177. "BasicWindow", "EditPropertiesMode");
  1178. if (!opt_studio_mode) {
  1179. SetPreviewProgramMode(config_get_bool(App()->GlobalConfig(),
  1180. "BasicWindow", "PreviewProgramMode"));
  1181. } else {
  1182. SetPreviewProgramMode(true);
  1183. opt_studio_mode = false;
  1184. }
  1185. #define SET_VISIBILITY(name, control) \
  1186. do { \
  1187. if (config_has_user_value(App()->GlobalConfig(), \
  1188. "BasicWindow", name)) { \
  1189. bool visible = config_get_bool(App()->GlobalConfig(), \
  1190. "BasicWindow", name); \
  1191. ui->control->setChecked(visible); \
  1192. } \
  1193. } while (false)
  1194. SET_VISIBILITY("ShowListboxToolbars", toggleListboxToolbars);
  1195. SET_VISIBILITY("ShowStatusBar", toggleStatusBar);
  1196. #undef SET_VISIBILITY
  1197. {
  1198. ProfileScope("OBSBasic::Load");
  1199. disableSaving--;
  1200. Load(savePath);
  1201. disableSaving++;
  1202. }
  1203. TimedCheckForUpdates();
  1204. loaded = true;
  1205. previewEnabled = config_get_bool(App()->GlobalConfig(),
  1206. "BasicWindow", "PreviewEnabled");
  1207. if (!previewEnabled && !IsPreviewProgramMode())
  1208. QMetaObject::invokeMethod(this, "EnablePreviewDisplay",
  1209. Qt::QueuedConnection,
  1210. Q_ARG(bool, previewEnabled));
  1211. #ifdef _WIN32
  1212. uint32_t winVer = GetWindowsVersion();
  1213. if (winVer > 0 && winVer < 0x602) {
  1214. bool disableAero = config_get_bool(basicConfig, "Video",
  1215. "DisableAero");
  1216. SetAeroEnabled(!disableAero);
  1217. }
  1218. #endif
  1219. RefreshSceneCollections();
  1220. RefreshProfiles();
  1221. disableSaving--;
  1222. auto addDisplay = [this] (OBSQTDisplay *window)
  1223. {
  1224. obs_display_add_draw_callback(window->GetDisplay(),
  1225. OBSBasic::RenderMain, this);
  1226. struct obs_video_info ovi;
  1227. if (obs_get_video_info(&ovi))
  1228. ResizePreview(ovi.base_width, ovi.base_height);
  1229. };
  1230. connect(ui->preview, &OBSQTDisplay::DisplayCreated, addDisplay);
  1231. #ifdef _WIN32
  1232. SetWin32DropStyle(this);
  1233. show();
  1234. #endif
  1235. bool alwaysOnTop = config_get_bool(App()->GlobalConfig(), "BasicWindow",
  1236. "AlwaysOnTop");
  1237. if (alwaysOnTop || opt_always_on_top) {
  1238. SetAlwaysOnTop(this, true);
  1239. ui->actionAlwaysOnTop->setChecked(true);
  1240. }
  1241. #ifndef _WIN32
  1242. show();
  1243. #endif
  1244. const char *dockStateStr = config_get_string(App()->GlobalConfig(),
  1245. "BasicWindow", "DockState");
  1246. if (!dockStateStr) {
  1247. on_resetUI_triggered();
  1248. } else {
  1249. QByteArray dockState =
  1250. QByteArray::fromBase64(QByteArray(dockStateStr));
  1251. if (!restoreState(dockState))
  1252. on_resetUI_triggered();
  1253. }
  1254. config_set_default_bool(App()->GlobalConfig(), "BasicWindow",
  1255. "DocksLocked", true);
  1256. bool docksLocked = config_get_bool(App()->GlobalConfig(),
  1257. "BasicWindow", "DocksLocked");
  1258. on_lockUI_toggled(docksLocked);
  1259. ui->lockUI->blockSignals(true);
  1260. ui->lockUI->setChecked(docksLocked);
  1261. ui->lockUI->blockSignals(false);
  1262. SystemTray(true);
  1263. OpenSavedProjectors();
  1264. if (windowState().testFlag(Qt::WindowFullScreen))
  1265. fullscreenInterface = true;
  1266. bool has_last_version = config_has_user_value(App()->GlobalConfig(),
  1267. "General", "LastVersion");
  1268. bool first_run = config_get_bool(App()->GlobalConfig(), "General",
  1269. "FirstRun");
  1270. if (!first_run) {
  1271. config_set_bool(App()->GlobalConfig(), "General", "FirstRun",
  1272. true);
  1273. config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
  1274. }
  1275. if (!first_run && !has_last_version && !Active()) {
  1276. QString msg;
  1277. msg = QTStr("Basic.FirstStartup.RunWizard");
  1278. msg += "\n\n";
  1279. msg += QTStr("Basic.FirstStartup.RunWizard.BetaWarning");
  1280. QMessageBox::StandardButton button =
  1281. OBSMessageBox::question(this, QTStr("Basic.AutoConfig"),
  1282. msg);
  1283. if (button == QMessageBox::Yes) {
  1284. on_autoConfigure_triggered();
  1285. } else {
  1286. msg = QTStr("Basic.FirstStartup.RunWizard.NoClicked");
  1287. OBSMessageBox::information(this,
  1288. QTStr("Basic.AutoConfig"), msg);
  1289. }
  1290. }
  1291. if (config_get_bool(basicConfig, "General", "OpenStatsOnStartup"))
  1292. on_stats_triggered();
  1293. OBSBasicStats::InitializeValues();
  1294. /* ----------------------- */
  1295. /* Add multiview menu */
  1296. ui->viewMenu->addSeparator();
  1297. QMenu *multiviewProjectorMenu = new QMenu(QTStr("MultiviewProjector"));
  1298. AddProjectorMenuMonitors(multiviewProjectorMenu, this,
  1299. SLOT(OpenMultiviewProjector()));
  1300. ui->viewMenu->addMenu(multiviewProjectorMenu);
  1301. ui->viewMenu->addAction(QTStr("MultiviewWindowed"),
  1302. this, SLOT(OpenMultiviewWindow()));
  1303. }
  1304. void OBSBasic::InitHotkeys()
  1305. {
  1306. ProfileScope("OBSBasic::InitHotkeys");
  1307. struct obs_hotkeys_translations t = {};
  1308. t.insert = Str("Hotkeys.Insert");
  1309. t.del = Str("Hotkeys.Delete");
  1310. t.home = Str("Hotkeys.Home");
  1311. t.end = Str("Hotkeys.End");
  1312. t.page_up = Str("Hotkeys.PageUp");
  1313. t.page_down = Str("Hotkeys.PageDown");
  1314. t.num_lock = Str("Hotkeys.NumLock");
  1315. t.scroll_lock = Str("Hotkeys.ScrollLock");
  1316. t.caps_lock = Str("Hotkeys.CapsLock");
  1317. t.backspace = Str("Hotkeys.Backspace");
  1318. t.tab = Str("Hotkeys.Tab");
  1319. t.print = Str("Hotkeys.Print");
  1320. t.pause = Str("Hotkeys.Pause");
  1321. t.left = Str("Hotkeys.Left");
  1322. t.right = Str("Hotkeys.Right");
  1323. t.up = Str("Hotkeys.Up");
  1324. t.down = Str("Hotkeys.Down");
  1325. #ifdef _WIN32
  1326. t.meta = Str("Hotkeys.Windows");
  1327. #else
  1328. t.meta = Str("Hotkeys.Super");
  1329. #endif
  1330. t.menu = Str("Hotkeys.Menu");
  1331. t.space = Str("Hotkeys.Space");
  1332. t.numpad_num = Str("Hotkeys.NumpadNum");
  1333. t.numpad_multiply = Str("Hotkeys.NumpadMultiply");
  1334. t.numpad_divide = Str("Hotkeys.NumpadDivide");
  1335. t.numpad_plus = Str("Hotkeys.NumpadAdd");
  1336. t.numpad_minus = Str("Hotkeys.NumpadSubtract");
  1337. t.numpad_decimal = Str("Hotkeys.NumpadDecimal");
  1338. t.apple_keypad_num = Str("Hotkeys.AppleKeypadNum");
  1339. t.apple_keypad_multiply = Str("Hotkeys.AppleKeypadMultiply");
  1340. t.apple_keypad_divide = Str("Hotkeys.AppleKeypadDivide");
  1341. t.apple_keypad_plus = Str("Hotkeys.AppleKeypadAdd");
  1342. t.apple_keypad_minus = Str("Hotkeys.AppleKeypadSubtract");
  1343. t.apple_keypad_decimal = Str("Hotkeys.AppleKeypadDecimal");
  1344. t.apple_keypad_equal = Str("Hotkeys.AppleKeypadEqual");
  1345. t.mouse_num = Str("Hotkeys.MouseButton");
  1346. obs_hotkeys_set_translations(&t);
  1347. obs_hotkeys_set_audio_hotkeys_translations(Str("Mute"), Str("Unmute"),
  1348. Str("Push-to-mute"), Str("Push-to-talk"));
  1349. obs_hotkeys_set_sceneitem_hotkeys_translations(
  1350. Str("SceneItemShow"), Str("SceneItemHide"));
  1351. obs_hotkey_enable_callback_rerouting(true);
  1352. obs_hotkey_set_callback_routing_func(OBSBasic::HotkeyTriggered, this);
  1353. }
  1354. void OBSBasic::ProcessHotkey(obs_hotkey_id id, bool pressed)
  1355. {
  1356. obs_hotkey_trigger_routed_callback(id, pressed);
  1357. }
  1358. void OBSBasic::HotkeyTriggered(void *data, obs_hotkey_id id, bool pressed)
  1359. {
  1360. OBSBasic &basic = *static_cast<OBSBasic*>(data);
  1361. QMetaObject::invokeMethod(&basic, "ProcessHotkey",
  1362. Q_ARG(obs_hotkey_id, id), Q_ARG(bool, pressed));
  1363. }
  1364. void OBSBasic::CreateHotkeys()
  1365. {
  1366. ProfileScope("OBSBasic::CreateHotkeys");
  1367. auto LoadHotkeyData = [&](const char *name) -> OBSData
  1368. {
  1369. const char *info = config_get_string(basicConfig,
  1370. "Hotkeys", name);
  1371. if (!info)
  1372. return {};
  1373. obs_data_t *data = obs_data_create_from_json(info);
  1374. if (!data)
  1375. return {};
  1376. OBSData res = data;
  1377. obs_data_release(data);
  1378. return res;
  1379. };
  1380. auto LoadHotkey = [&](obs_hotkey_id id, const char *name)
  1381. {
  1382. obs_data_array_t *array =
  1383. obs_data_get_array(LoadHotkeyData(name), "bindings");
  1384. obs_hotkey_load(id, array);
  1385. obs_data_array_release(array);
  1386. };
  1387. auto LoadHotkeyPair = [&](obs_hotkey_pair_id id, const char *name0,
  1388. const char *name1)
  1389. {
  1390. obs_data_array_t *array0 =
  1391. obs_data_get_array(LoadHotkeyData(name0), "bindings");
  1392. obs_data_array_t *array1 =
  1393. obs_data_get_array(LoadHotkeyData(name1), "bindings");
  1394. obs_hotkey_pair_load(id, array0, array1);
  1395. obs_data_array_release(array0);
  1396. obs_data_array_release(array1);
  1397. };
  1398. #define MAKE_CALLBACK(pred, method, log_action) \
  1399. [](void *data, obs_hotkey_pair_id, obs_hotkey_t*, bool pressed) \
  1400. { \
  1401. OBSBasic &basic = *static_cast<OBSBasic*>(data); \
  1402. if (pred && pressed) { \
  1403. blog(LOG_INFO, log_action " due to hotkey"); \
  1404. method(); \
  1405. return true; \
  1406. } \
  1407. return false; \
  1408. }
  1409. streamingHotkeys = obs_hotkey_pair_register_frontend(
  1410. "OBSBasic.StartStreaming",
  1411. Str("Basic.Main.StartStreaming"),
  1412. "OBSBasic.StopStreaming",
  1413. Str("Basic.Main.StopStreaming"),
  1414. MAKE_CALLBACK(!basic.outputHandler->StreamingActive(),
  1415. basic.StartStreaming, "Starting stream"),
  1416. MAKE_CALLBACK(basic.outputHandler->StreamingActive(),
  1417. basic.StopStreaming, "Stopping stream"),
  1418. this, this);
  1419. LoadHotkeyPair(streamingHotkeys,
  1420. "OBSBasic.StartStreaming", "OBSBasic.StopStreaming");
  1421. auto cb = [] (void *data, obs_hotkey_id, obs_hotkey_t*, bool pressed)
  1422. {
  1423. OBSBasic &basic = *static_cast<OBSBasic*>(data);
  1424. if (basic.outputHandler->StreamingActive() && pressed) {
  1425. basic.ForceStopStreaming();
  1426. }
  1427. };
  1428. forceStreamingStopHotkey = obs_hotkey_register_frontend(
  1429. "OBSBasic.ForceStopStreaming",
  1430. Str("Basic.Main.ForceStopStreaming"),
  1431. cb, this);
  1432. LoadHotkey(forceStreamingStopHotkey,
  1433. "OBSBasic.ForceStopStreaming");
  1434. recordingHotkeys = obs_hotkey_pair_register_frontend(
  1435. "OBSBasic.StartRecording",
  1436. Str("Basic.Main.StartRecording"),
  1437. "OBSBasic.StopRecording",
  1438. Str("Basic.Main.StopRecording"),
  1439. MAKE_CALLBACK(!basic.outputHandler->RecordingActive(),
  1440. basic.StartRecording, "Starting recording"),
  1441. MAKE_CALLBACK(basic.outputHandler->RecordingActive(),
  1442. basic.StopRecording, "Stopping recording"),
  1443. this, this);
  1444. LoadHotkeyPair(recordingHotkeys,
  1445. "OBSBasic.StartRecording", "OBSBasic.StopRecording");
  1446. replayBufHotkeys = obs_hotkey_pair_register_frontend(
  1447. "OBSBasic.StartReplayBuffer",
  1448. Str("Basic.Main.StartReplayBuffer"),
  1449. "OBSBasic.StopReplayBuffer",
  1450. Str("Basic.Main.StopReplayBuffer"),
  1451. MAKE_CALLBACK(!basic.outputHandler->ReplayBufferActive(),
  1452. basic.StartReplayBuffer, "Starting replay buffer"),
  1453. MAKE_CALLBACK(basic.outputHandler->ReplayBufferActive(),
  1454. basic.StopReplayBuffer, "Stopping replay buffer"),
  1455. this, this);
  1456. LoadHotkeyPair(replayBufHotkeys,
  1457. "OBSBasic.StartReplayBuffer", "OBSBasic.StopReplayBuffer");
  1458. #undef MAKE_CALLBACK
  1459. auto togglePreviewProgram = [] (void *data, obs_hotkey_id,
  1460. obs_hotkey_t*, bool pressed)
  1461. {
  1462. if (pressed)
  1463. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  1464. "on_modeSwitch_clicked",
  1465. Qt::QueuedConnection);
  1466. };
  1467. togglePreviewProgramHotkey = obs_hotkey_register_frontend(
  1468. "OBSBasic.TogglePreviewProgram",
  1469. Str("Basic.TogglePreviewProgramMode"),
  1470. togglePreviewProgram, this);
  1471. LoadHotkey(togglePreviewProgramHotkey, "OBSBasic.TogglePreviewProgram");
  1472. auto transition = [] (void *data, obs_hotkey_id, obs_hotkey_t*,
  1473. bool pressed)
  1474. {
  1475. if (pressed)
  1476. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  1477. "TransitionClicked",
  1478. Qt::QueuedConnection);
  1479. };
  1480. transitionHotkey = obs_hotkey_register_frontend(
  1481. "OBSBasic.Transition",
  1482. Str("Transition"), transition, this);
  1483. LoadHotkey(transitionHotkey, "OBSBasic.Transition");
  1484. }
  1485. void OBSBasic::ClearHotkeys()
  1486. {
  1487. obs_hotkey_pair_unregister(streamingHotkeys);
  1488. obs_hotkey_pair_unregister(recordingHotkeys);
  1489. obs_hotkey_pair_unregister(replayBufHotkeys);
  1490. obs_hotkey_unregister(forceStreamingStopHotkey);
  1491. obs_hotkey_unregister(togglePreviewProgramHotkey);
  1492. obs_hotkey_unregister(transitionHotkey);
  1493. }
  1494. OBSBasic::~OBSBasic()
  1495. {
  1496. if (updateCheckThread && updateCheckThread->isRunning())
  1497. updateCheckThread->wait();
  1498. delete programOptions;
  1499. delete program;
  1500. /* XXX: any obs data must be released before calling obs_shutdown.
  1501. * currently, we can't automate this with C++ RAII because of the
  1502. * delicate nature of obs_shutdown needing to be freed before the UI
  1503. * can be freed, and we have no control over the destruction order of
  1504. * the Qt UI stuff, so we have to manually clear any references to
  1505. * libobs. */
  1506. delete cpuUsageTimer;
  1507. os_cpu_usage_info_destroy(cpuUsageInfo);
  1508. obs_hotkey_set_callback_routing_func(nullptr, nullptr);
  1509. ClearHotkeys();
  1510. service = nullptr;
  1511. outputHandler.reset();
  1512. if (interaction)
  1513. delete interaction;
  1514. if (properties)
  1515. delete properties;
  1516. if (filters)
  1517. delete filters;
  1518. if (transformWindow)
  1519. delete transformWindow;
  1520. if (advAudioWindow)
  1521. delete advAudioWindow;
  1522. obs_display_remove_draw_callback(ui->preview->GetDisplay(),
  1523. OBSBasic::RenderMain, this);
  1524. obs_enter_graphics();
  1525. gs_vertexbuffer_destroy(box);
  1526. gs_vertexbuffer_destroy(boxLeft);
  1527. gs_vertexbuffer_destroy(boxTop);
  1528. gs_vertexbuffer_destroy(boxRight);
  1529. gs_vertexbuffer_destroy(boxBottom);
  1530. gs_vertexbuffer_destroy(circle);
  1531. obs_leave_graphics();
  1532. /* When shutting down, sometimes source references can get in to the
  1533. * event queue, and if we don't forcibly process those events they
  1534. * won't get processed until after obs_shutdown has been called. I
  1535. * really wish there were a more elegant way to deal with this via C++,
  1536. * but Qt doesn't use C++ in a normal way, so you can't really rely on
  1537. * normal C++ behavior for your data to be freed in the order that you
  1538. * expect or want it to. */
  1539. QApplication::sendPostedEvents(this);
  1540. config_set_int(App()->GlobalConfig(), "General", "LastVersion",
  1541. LIBOBS_API_VER);
  1542. bool alwaysOnTop = IsAlwaysOnTop(this);
  1543. config_set_bool(App()->GlobalConfig(), "BasicWindow", "PreviewEnabled",
  1544. previewEnabled);
  1545. config_set_bool(App()->GlobalConfig(), "BasicWindow", "AlwaysOnTop",
  1546. alwaysOnTop);
  1547. config_set_bool(App()->GlobalConfig(), "BasicWindow",
  1548. "SceneDuplicationMode", sceneDuplicationMode);
  1549. config_set_bool(App()->GlobalConfig(), "BasicWindow",
  1550. "SwapScenesMode", swapScenesMode);
  1551. config_set_bool(App()->GlobalConfig(), "BasicWindow",
  1552. "EditPropertiesMode", editPropertiesMode);
  1553. config_set_bool(App()->GlobalConfig(), "BasicWindow",
  1554. "PreviewProgramMode", IsPreviewProgramMode());
  1555. config_set_bool(App()->GlobalConfig(), "BasicWindow",
  1556. "DocksLocked", ui->lockUI->isChecked());
  1557. config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
  1558. #ifdef _WIN32
  1559. uint32_t winVer = GetWindowsVersion();
  1560. if (winVer > 0 && winVer < 0x602) {
  1561. bool disableAero = config_get_bool(basicConfig, "Video",
  1562. "DisableAero");
  1563. if (disableAero) {
  1564. SetAeroEnabled(true);
  1565. }
  1566. }
  1567. #endif
  1568. }
  1569. void OBSBasic::SaveProjectNow()
  1570. {
  1571. if (disableSaving)
  1572. return;
  1573. projectChanged = true;
  1574. SaveProjectDeferred();
  1575. }
  1576. void OBSBasic::SaveProject()
  1577. {
  1578. if (disableSaving)
  1579. return;
  1580. projectChanged = true;
  1581. QMetaObject::invokeMethod(this, "SaveProjectDeferred",
  1582. Qt::QueuedConnection);
  1583. }
  1584. void OBSBasic::SaveProjectDeferred()
  1585. {
  1586. if (disableSaving)
  1587. return;
  1588. if (!projectChanged)
  1589. return;
  1590. projectChanged = false;
  1591. const char *sceneCollection = config_get_string(App()->GlobalConfig(),
  1592. "Basic", "SceneCollectionFile");
  1593. char savePath[512];
  1594. char fileName[512];
  1595. int ret;
  1596. if (!sceneCollection)
  1597. return;
  1598. ret = snprintf(fileName, 512, "obs-studio/basic/scenes/%s.json",
  1599. sceneCollection);
  1600. if (ret <= 0)
  1601. return;
  1602. ret = GetConfigPath(savePath, sizeof(savePath), fileName);
  1603. if (ret <= 0)
  1604. return;
  1605. Save(savePath);
  1606. }
  1607. OBSSource OBSBasic::GetProgramSource()
  1608. {
  1609. return OBSGetStrongRef(programScene);
  1610. }
  1611. OBSScene OBSBasic::GetCurrentScene()
  1612. {
  1613. QListWidgetItem *item = ui->scenes->currentItem();
  1614. return item ? GetOBSRef<OBSScene>(item) : nullptr;
  1615. }
  1616. OBSSceneItem OBSBasic::GetSceneItem(QListWidgetItem *item)
  1617. {
  1618. return item ? GetOBSRef<OBSSceneItem>(item) : nullptr;
  1619. }
  1620. OBSSceneItem OBSBasic::GetCurrentSceneItem()
  1621. {
  1622. return GetSceneItem(GetTopSelectedSourceItem());
  1623. }
  1624. void OBSBasic::UpdatePreviewScalingMenu()
  1625. {
  1626. bool fixedScaling = ui->preview->IsFixedScaling();
  1627. float scalingAmount = ui->preview->GetScalingAmount();
  1628. if (!fixedScaling) {
  1629. ui->actionScaleWindow->setChecked(true);
  1630. ui->actionScaleCanvas->setChecked(false);
  1631. ui->actionScaleOutput->setChecked(false);
  1632. return;
  1633. }
  1634. obs_video_info ovi;
  1635. obs_get_video_info(&ovi);
  1636. ui->actionScaleWindow->setChecked(false);
  1637. ui->actionScaleCanvas->setChecked(scalingAmount == 1.0f);
  1638. ui->actionScaleOutput->setChecked(
  1639. scalingAmount == float(ovi.output_width) / float(ovi.base_width));
  1640. }
  1641. void OBSBasic::UpdateSources(OBSScene scene)
  1642. {
  1643. ClearListItems(ui->sources);
  1644. obs_scene_enum_items(scene,
  1645. [] (obs_scene_t *scene, obs_sceneitem_t *item, void *p)
  1646. {
  1647. OBSBasic *window = static_cast<OBSBasic*>(p);
  1648. window->InsertSceneItem(item);
  1649. UNUSED_PARAMETER(scene);
  1650. return true;
  1651. }, this);
  1652. }
  1653. void OBSBasic::InsertSceneItem(obs_sceneitem_t *item)
  1654. {
  1655. QListWidgetItem *listItem = new QListWidgetItem();
  1656. SetOBSRef(listItem, OBSSceneItem(item));
  1657. ui->sources->insertItem(0, listItem);
  1658. ui->sources->setCurrentRow(0, QItemSelectionModel::ClearAndSelect);
  1659. SetupVisibilityItem(ui->sources, listItem, item);
  1660. }
  1661. void OBSBasic::CreateInteractionWindow(obs_source_t *source)
  1662. {
  1663. if (interaction)
  1664. interaction->close();
  1665. interaction = new OBSBasicInteraction(this, source);
  1666. interaction->Init();
  1667. interaction->setAttribute(Qt::WA_DeleteOnClose, true);
  1668. }
  1669. void OBSBasic::CreatePropertiesWindow(obs_source_t *source)
  1670. {
  1671. if (properties)
  1672. properties->close();
  1673. properties = new OBSBasicProperties(this, source);
  1674. properties->Init();
  1675. properties->setAttribute(Qt::WA_DeleteOnClose, true);
  1676. }
  1677. void OBSBasic::CreateFiltersWindow(obs_source_t *source)
  1678. {
  1679. if (filters)
  1680. filters->close();
  1681. filters = new OBSBasicFilters(this, source);
  1682. filters->Init();
  1683. filters->setAttribute(Qt::WA_DeleteOnClose, true);
  1684. }
  1685. /* Qt callbacks for invokeMethod */
  1686. void OBSBasic::AddScene(OBSSource source)
  1687. {
  1688. const char *name = obs_source_get_name(source);
  1689. obs_scene_t *scene = obs_scene_from_source(source);
  1690. QListWidgetItem *item = new QListWidgetItem(QT_UTF8(name));
  1691. SetOBSRef(item, OBSScene(scene));
  1692. ui->scenes->addItem(item);
  1693. obs_hotkey_register_source(source, "OBSBasic.SelectScene",
  1694. Str("Basic.Hotkeys.SelectScene"),
  1695. [](void *data,
  1696. obs_hotkey_id, obs_hotkey_t*, bool pressed)
  1697. {
  1698. OBSBasic *main =
  1699. reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  1700. auto potential_source = static_cast<obs_source_t*>(data);
  1701. auto source = obs_source_get_ref(potential_source);
  1702. if (source && pressed)
  1703. main->SetCurrentScene(source);
  1704. obs_source_release(source);
  1705. }, static_cast<obs_source_t*>(source));
  1706. signal_handler_t *handler = obs_source_get_signal_handler(source);
  1707. SignalContainer<OBSScene> container;
  1708. container.ref = scene;
  1709. container.handlers.assign({
  1710. std::make_shared<OBSSignal>(handler, "item_add",
  1711. OBSBasic::SceneItemAdded, this),
  1712. std::make_shared<OBSSignal>(handler, "item_remove",
  1713. OBSBasic::SceneItemRemoved, this),
  1714. std::make_shared<OBSSignal>(handler, "item_select",
  1715. OBSBasic::SceneItemSelected, this),
  1716. std::make_shared<OBSSignal>(handler, "item_deselect",
  1717. OBSBasic::SceneItemDeselected, this),
  1718. std::make_shared<OBSSignal>(handler, "reorder",
  1719. OBSBasic::SceneReordered, this),
  1720. });
  1721. item->setData(static_cast<int>(QtDataRole::OBSSignals),
  1722. QVariant::fromValue(container));
  1723. /* if the scene already has items (a duplicated scene) add them */
  1724. auto addSceneItem = [this] (obs_sceneitem_t *item)
  1725. {
  1726. AddSceneItem(item);
  1727. };
  1728. using addSceneItem_t = decltype(addSceneItem);
  1729. obs_scene_enum_items(scene,
  1730. [] (obs_scene_t*, obs_sceneitem_t *item, void *param)
  1731. {
  1732. addSceneItem_t *func;
  1733. func = reinterpret_cast<addSceneItem_t*>(param);
  1734. (*func)(item);
  1735. return true;
  1736. }, &addSceneItem);
  1737. SaveProject();
  1738. if (!disableSaving) {
  1739. obs_source_t *source = obs_scene_get_source(scene);
  1740. blog(LOG_INFO, "User added scene '%s'",
  1741. obs_source_get_name(source));
  1742. OBSProjector::UpdateMultiviewProjectors();
  1743. }
  1744. if (api)
  1745. api->on_event(OBS_FRONTEND_EVENT_SCENE_LIST_CHANGED);
  1746. }
  1747. void OBSBasic::RemoveScene(OBSSource source)
  1748. {
  1749. obs_scene_t *scene = obs_scene_from_source(source);
  1750. QListWidgetItem *sel = nullptr;
  1751. int count = ui->scenes->count();
  1752. for (int i = 0; i < count; i++) {
  1753. auto item = ui->scenes->item(i);
  1754. auto cur_scene = GetOBSRef<OBSScene>(item);
  1755. if (cur_scene != scene)
  1756. continue;
  1757. sel = item;
  1758. break;
  1759. }
  1760. if (sel != nullptr) {
  1761. if (sel == ui->scenes->currentItem())
  1762. ClearListItems(ui->sources);
  1763. delete sel;
  1764. }
  1765. SaveProject();
  1766. if (!disableSaving) {
  1767. blog(LOG_INFO, "User Removed scene '%s'",
  1768. obs_source_get_name(source));
  1769. OBSProjector::UpdateMultiviewProjectors();
  1770. }
  1771. if (api)
  1772. api->on_event(OBS_FRONTEND_EVENT_SCENE_LIST_CHANGED);
  1773. }
  1774. void OBSBasic::AddSceneItem(OBSSceneItem item)
  1775. {
  1776. obs_scene_t *scene = obs_sceneitem_get_scene(item);
  1777. if (GetCurrentScene() == scene)
  1778. InsertSceneItem(item);
  1779. SaveProject();
  1780. if (!disableSaving) {
  1781. obs_source_t *sceneSource = obs_scene_get_source(scene);
  1782. obs_source_t *itemSource = obs_sceneitem_get_source(item);
  1783. blog(LOG_INFO, "User added source '%s' (%s) to scene '%s'",
  1784. obs_source_get_name(itemSource),
  1785. obs_source_get_id(itemSource),
  1786. obs_source_get_name(sceneSource));
  1787. }
  1788. }
  1789. void OBSBasic::RemoveSceneItem(OBSSceneItem item)
  1790. {
  1791. for (int i = 0; i < ui->sources->count(); i++) {
  1792. QListWidgetItem *listItem = ui->sources->item(i);
  1793. if (GetOBSRef<OBSSceneItem>(listItem) == item) {
  1794. DeleteListItem(ui->sources, listItem);
  1795. break;
  1796. }
  1797. }
  1798. SaveProject();
  1799. if (!disableSaving) {
  1800. obs_scene_t *scene = obs_sceneitem_get_scene(item);
  1801. obs_source_t *sceneSource = obs_scene_get_source(scene);
  1802. obs_source_t *itemSource = obs_sceneitem_get_source(item);
  1803. blog(LOG_INFO, "User Removed source '%s' (%s) from scene '%s'",
  1804. obs_source_get_name(itemSource),
  1805. obs_source_get_id(itemSource),
  1806. obs_source_get_name(sceneSource));
  1807. }
  1808. }
  1809. void OBSBasic::UpdateSceneSelection(OBSSource source)
  1810. {
  1811. if (source) {
  1812. obs_scene_t *scene = obs_scene_from_source(source);
  1813. const char *name = obs_source_get_name(source);
  1814. if (!scene)
  1815. return;
  1816. QList<QListWidgetItem*> items =
  1817. ui->scenes->findItems(QT_UTF8(name), Qt::MatchExactly);
  1818. if (items.count()) {
  1819. sceneChanging = true;
  1820. ui->scenes->setCurrentItem(items.first());
  1821. sceneChanging = false;
  1822. UpdateSources(scene);
  1823. }
  1824. }
  1825. }
  1826. static void RenameListValues(QListWidget *listWidget, const QString &newName,
  1827. const QString &prevName)
  1828. {
  1829. QList<QListWidgetItem*> items =
  1830. listWidget->findItems(prevName, Qt::MatchExactly);
  1831. for (int i = 0; i < items.count(); i++)
  1832. items[i]->setText(newName);
  1833. }
  1834. void OBSBasic::RenameSources(OBSSource source, QString newName,
  1835. QString prevName)
  1836. {
  1837. RenameListValues(ui->scenes, newName, prevName);
  1838. for (size_t i = 0; i < volumes.size(); i++) {
  1839. if (volumes[i]->GetName().compare(prevName) == 0)
  1840. volumes[i]->SetName(newName);
  1841. }
  1842. std::string newText = newName.toUtf8().constData();
  1843. std::string prevText = prevName.toUtf8().constData();
  1844. for (size_t j = 0; j < projectorArray.size(); j++) {
  1845. if (projectorArray.at(j) == prevText)
  1846. projectorArray.at(j) = newText;
  1847. }
  1848. SaveProject();
  1849. obs_scene_t *scene = obs_scene_from_source(source);
  1850. if (scene)
  1851. OBSProjector::UpdateMultiviewProjectors();
  1852. }
  1853. void OBSBasic::SelectSceneItem(OBSScene scene, OBSSceneItem item, bool select)
  1854. {
  1855. SignalBlocker sourcesSignalBlocker(ui->sources);
  1856. if (scene != GetCurrentScene() || ignoreSelectionUpdate)
  1857. return;
  1858. for (int i = 0; i < ui->sources->count(); i++) {
  1859. QListWidgetItem *witem = ui->sources->item(i);
  1860. QVariant data =
  1861. witem->data(static_cast<int>(QtDataRole::OBSRef));
  1862. if (!data.canConvert<OBSSceneItem>())
  1863. continue;
  1864. if (item != data.value<OBSSceneItem>())
  1865. continue;
  1866. witem->setSelected(select);
  1867. break;
  1868. }
  1869. }
  1870. static inline bool SourceMixerHidden(obs_source_t *source)
  1871. {
  1872. obs_data_t *priv_settings = obs_source_get_private_settings(source);
  1873. bool hidden = obs_data_get_bool(priv_settings, "mixer_hidden");
  1874. obs_data_release(priv_settings);
  1875. return hidden;
  1876. }
  1877. static inline void SetSourceMixerHidden(obs_source_t *source, bool hidden)
  1878. {
  1879. obs_data_t *priv_settings = obs_source_get_private_settings(source);
  1880. obs_data_set_bool(priv_settings, "mixer_hidden", hidden);
  1881. obs_data_release(priv_settings);
  1882. }
  1883. void OBSBasic::GetAudioSourceFilters()
  1884. {
  1885. QAction *action = reinterpret_cast<QAction*>(sender());
  1886. VolControl *vol = action->property("volControl").value<VolControl*>();
  1887. obs_source_t *source = vol->GetSource();
  1888. CreateFiltersWindow(source);
  1889. }
  1890. void OBSBasic::GetAudioSourceProperties()
  1891. {
  1892. QAction *action = reinterpret_cast<QAction*>(sender());
  1893. VolControl *vol = action->property("volControl").value<VolControl*>();
  1894. obs_source_t *source = vol->GetSource();
  1895. CreatePropertiesWindow(source);
  1896. }
  1897. void OBSBasic::HideAudioControl()
  1898. {
  1899. QAction *action = reinterpret_cast<QAction*>(sender());
  1900. VolControl *vol = action->property("volControl").value<VolControl*>();
  1901. obs_source_t *source = vol->GetSource();
  1902. if (!SourceMixerHidden(source)) {
  1903. SetSourceMixerHidden(source, true);
  1904. DeactivateAudioSource(source);
  1905. }
  1906. }
  1907. void OBSBasic::UnhideAllAudioControls()
  1908. {
  1909. auto UnhideAudioMixer = [this] (obs_source_t *source) /* -- */
  1910. {
  1911. if (!obs_source_active(source))
  1912. return true;
  1913. if (!SourceMixerHidden(source))
  1914. return true;
  1915. SetSourceMixerHidden(source, false);
  1916. ActivateAudioSource(source);
  1917. return true;
  1918. };
  1919. using UnhideAudioMixer_t = decltype(UnhideAudioMixer);
  1920. auto PreEnum = [] (void *data, obs_source_t *source) -> bool /* -- */
  1921. {
  1922. return (*reinterpret_cast<UnhideAudioMixer_t *>(data))(source);
  1923. };
  1924. obs_enum_sources(PreEnum, &UnhideAudioMixer);
  1925. }
  1926. void OBSBasic::ToggleHideMixer()
  1927. {
  1928. OBSSceneItem item = GetCurrentSceneItem();
  1929. OBSSource source = obs_sceneitem_get_source(item);
  1930. if (!SourceMixerHidden(source)) {
  1931. SetSourceMixerHidden(source, true);
  1932. DeactivateAudioSource(source);
  1933. } else {
  1934. SetSourceMixerHidden(source, false);
  1935. ActivateAudioSource(source);
  1936. }
  1937. }
  1938. void OBSBasic::MixerRenameSource()
  1939. {
  1940. QAction *action = reinterpret_cast<QAction*>(sender());
  1941. VolControl *vol = action->property("volControl").value<VolControl*>();
  1942. OBSSource source = vol->GetSource();
  1943. const char *prevName = obs_source_get_name(source);
  1944. for (;;) {
  1945. string name;
  1946. bool accepted = NameDialog::AskForName(this,
  1947. QTStr("Basic.Main.MixerRename.Title"),
  1948. QTStr("Basic.Main.MixerRename.Text"),
  1949. name,
  1950. QT_UTF8(prevName));
  1951. if (!accepted)
  1952. return;
  1953. if (name.empty()) {
  1954. OBSMessageBox::information(this,
  1955. QTStr("NoNameEntered.Title"),
  1956. QTStr("NoNameEntered.Text"));
  1957. continue;
  1958. }
  1959. OBSSource sourceTest = obs_get_source_by_name(name.c_str());
  1960. obs_source_release(sourceTest);
  1961. if (sourceTest) {
  1962. OBSMessageBox::information(this,
  1963. QTStr("NameExists.Title"),
  1964. QTStr("NameExists.Text"));
  1965. continue;
  1966. }
  1967. obs_source_set_name(source, name.c_str());
  1968. break;
  1969. }
  1970. }
  1971. void OBSBasic::VolControlContextMenu()
  1972. {
  1973. VolControl *vol = reinterpret_cast<VolControl*>(sender());
  1974. /* ------------------- */
  1975. QAction hideAction(QTStr("Hide"), this);
  1976. QAction unhideAllAction(QTStr("UnhideAll"), this);
  1977. QAction mixerRenameAction(QTStr("Rename"), this);
  1978. QAction filtersAction(QTStr("Filters"), this);
  1979. QAction propertiesAction(QTStr("Properties"), this);
  1980. QAction advPropAction(QTStr("Basic.MainMenu.Edit.AdvAudio"), this);
  1981. /* ------------------- */
  1982. connect(&hideAction, &QAction::triggered,
  1983. this, &OBSBasic::HideAudioControl,
  1984. Qt::DirectConnection);
  1985. connect(&unhideAllAction, &QAction::triggered,
  1986. this, &OBSBasic::UnhideAllAudioControls,
  1987. Qt::DirectConnection);
  1988. connect(&mixerRenameAction, &QAction::triggered,
  1989. this, &OBSBasic::MixerRenameSource,
  1990. Qt::DirectConnection);
  1991. connect(&filtersAction, &QAction::triggered,
  1992. this, &OBSBasic::GetAudioSourceFilters,
  1993. Qt::DirectConnection);
  1994. connect(&propertiesAction, &QAction::triggered,
  1995. this, &OBSBasic::GetAudioSourceProperties,
  1996. Qt::DirectConnection);
  1997. connect(&advPropAction, &QAction::triggered,
  1998. this, &OBSBasic::on_actionAdvAudioProperties_triggered,
  1999. Qt::DirectConnection);
  2000. /* ------------------- */
  2001. hideAction.setProperty("volControl",
  2002. QVariant::fromValue<VolControl*>(vol));
  2003. mixerRenameAction.setProperty("volControl",
  2004. QVariant::fromValue<VolControl*>(vol));
  2005. filtersAction.setProperty("volControl",
  2006. QVariant::fromValue<VolControl*>(vol));
  2007. propertiesAction.setProperty("volControl",
  2008. QVariant::fromValue<VolControl*>(vol));
  2009. /* ------------------- */
  2010. QMenu popup(this);
  2011. popup.addAction(&unhideAllAction);
  2012. popup.addAction(&hideAction);
  2013. popup.addAction(&mixerRenameAction);
  2014. popup.addSeparator();
  2015. popup.addAction(&filtersAction);
  2016. popup.addAction(&propertiesAction);
  2017. popup.addAction(&advPropAction);
  2018. popup.exec(QCursor::pos());
  2019. }
  2020. void OBSBasic::on_mixerScrollArea_customContextMenuRequested()
  2021. {
  2022. QAction unhideAllAction(QTStr("UnhideAll"), this);
  2023. QAction advPropAction(QTStr("Basic.MainMenu.Edit.AdvAudio"), this);
  2024. /* ------------------- */
  2025. connect(&unhideAllAction, &QAction::triggered,
  2026. this, &OBSBasic::UnhideAllAudioControls,
  2027. Qt::DirectConnection);
  2028. connect(&advPropAction, &QAction::triggered,
  2029. this, &OBSBasic::on_actionAdvAudioProperties_triggered,
  2030. Qt::DirectConnection);
  2031. /* ------------------- */
  2032. QMenu popup(this);
  2033. popup.addAction(&unhideAllAction);
  2034. popup.addSeparator();
  2035. popup.addAction(&advPropAction);
  2036. popup.exec(QCursor::pos());
  2037. }
  2038. void OBSBasic::ActivateAudioSource(OBSSource source)
  2039. {
  2040. if (SourceMixerHidden(source))
  2041. return;
  2042. VolControl *vol = new VolControl(source, true);
  2043. vol->setContextMenuPolicy(Qt::CustomContextMenu);
  2044. connect(vol, &QWidget::customContextMenuRequested,
  2045. this, &OBSBasic::VolControlContextMenu);
  2046. connect(vol, &VolControl::ConfigClicked,
  2047. this, &OBSBasic::VolControlContextMenu);
  2048. volumes.push_back(vol);
  2049. ui->volumeWidgets->layout()->addWidget(vol);
  2050. }
  2051. void OBSBasic::DeactivateAudioSource(OBSSource source)
  2052. {
  2053. for (size_t i = 0; i < volumes.size(); i++) {
  2054. if (volumes[i]->GetSource() == source) {
  2055. delete volumes[i];
  2056. volumes.erase(volumes.begin() + i);
  2057. break;
  2058. }
  2059. }
  2060. }
  2061. bool OBSBasic::QueryRemoveSource(obs_source_t *source)
  2062. {
  2063. if (obs_source_get_type(source) == OBS_SOURCE_TYPE_SCENE) {
  2064. int count = ui->scenes->count();
  2065. if (count == 1) {
  2066. OBSMessageBox::information(this,
  2067. QTStr("FinalScene.Title"),
  2068. QTStr("FinalScene.Text"));
  2069. return false;
  2070. }
  2071. }
  2072. const char *name = obs_source_get_name(source);
  2073. QString text = QTStr("ConfirmRemove.Text");
  2074. text.replace("$1", QT_UTF8(name));
  2075. QMessageBox remove_source(this);
  2076. remove_source.setText(text);
  2077. QAbstractButton *Yes = remove_source.addButton(QTStr("Yes"),
  2078. QMessageBox::YesRole);
  2079. remove_source.addButton(QTStr("No"), QMessageBox::NoRole);
  2080. remove_source.setIcon(QMessageBox::Question);
  2081. remove_source.setWindowTitle(QTStr("ConfirmRemove.Title"));
  2082. remove_source.exec();
  2083. return Yes == remove_source.clickedButton();
  2084. }
  2085. #define UPDATE_CHECK_INTERVAL (60*60*24*4) /* 4 days */
  2086. #ifdef UPDATE_SPARKLE
  2087. void init_sparkle_updater(bool update_to_undeployed);
  2088. void trigger_sparkle_update();
  2089. #endif
  2090. void OBSBasic::TimedCheckForUpdates()
  2091. {
  2092. if (!config_get_bool(App()->GlobalConfig(), "General",
  2093. "EnableAutoUpdates"))
  2094. return;
  2095. #ifdef UPDATE_SPARKLE
  2096. init_sparkle_updater(config_get_bool(App()->GlobalConfig(), "General",
  2097. "UpdateToUndeployed"));
  2098. #elif ENABLE_WIN_UPDATER
  2099. long long lastUpdate = config_get_int(App()->GlobalConfig(), "General",
  2100. "LastUpdateCheck");
  2101. uint32_t lastVersion = config_get_int(App()->GlobalConfig(), "General",
  2102. "LastVersion");
  2103. if (lastVersion < LIBOBS_API_VER) {
  2104. lastUpdate = 0;
  2105. config_set_int(App()->GlobalConfig(), "General",
  2106. "LastUpdateCheck", 0);
  2107. }
  2108. long long t = (long long)time(nullptr);
  2109. long long secs = t - lastUpdate;
  2110. if (secs > UPDATE_CHECK_INTERVAL)
  2111. CheckForUpdates(false);
  2112. #endif
  2113. }
  2114. void OBSBasic::CheckForUpdates(bool manualUpdate)
  2115. {
  2116. #ifdef UPDATE_SPARKLE
  2117. trigger_sparkle_update();
  2118. #elif ENABLE_WIN_UPDATER
  2119. ui->actionCheckForUpdates->setEnabled(false);
  2120. if (updateCheckThread && updateCheckThread->isRunning())
  2121. return;
  2122. updateCheckThread = new AutoUpdateThread(manualUpdate);
  2123. updateCheckThread->start();
  2124. #endif
  2125. UNUSED_PARAMETER(manualUpdate);
  2126. }
  2127. void OBSBasic::updateCheckFinished()
  2128. {
  2129. ui->actionCheckForUpdates->setEnabled(true);
  2130. }
  2131. void OBSBasic::DuplicateSelectedScene()
  2132. {
  2133. OBSScene curScene = GetCurrentScene();
  2134. if (!curScene)
  2135. return;
  2136. OBSSource curSceneSource = obs_scene_get_source(curScene);
  2137. QString format{obs_source_get_name(curSceneSource)};
  2138. format += " %1";
  2139. int i = 2;
  2140. QString placeHolderText = format.arg(i);
  2141. obs_source_t *source = nullptr;
  2142. while ((source = obs_get_source_by_name(QT_TO_UTF8(placeHolderText)))) {
  2143. obs_source_release(source);
  2144. placeHolderText = format.arg(++i);
  2145. }
  2146. for (;;) {
  2147. string name;
  2148. bool accepted = NameDialog::AskForName(this,
  2149. QTStr("Basic.Main.AddSceneDlg.Title"),
  2150. QTStr("Basic.Main.AddSceneDlg.Text"),
  2151. name,
  2152. placeHolderText);
  2153. if (!accepted)
  2154. return;
  2155. if (name.empty()) {
  2156. OBSMessageBox::information(this,
  2157. QTStr("NoNameEntered.Title"),
  2158. QTStr("NoNameEntered.Text"));
  2159. continue;
  2160. }
  2161. obs_source_t *source = obs_get_source_by_name(name.c_str());
  2162. if (source) {
  2163. OBSMessageBox::information(this,
  2164. QTStr("NameExists.Title"),
  2165. QTStr("NameExists.Text"));
  2166. obs_source_release(source);
  2167. continue;
  2168. }
  2169. obs_scene_t *scene = obs_scene_duplicate(curScene,
  2170. name.c_str(), OBS_SCENE_DUP_REFS);
  2171. source = obs_scene_get_source(scene);
  2172. AddScene(source);
  2173. SetCurrentScene(source, true);
  2174. obs_scene_release(scene);
  2175. break;
  2176. }
  2177. }
  2178. void OBSBasic::RemoveSelectedScene()
  2179. {
  2180. OBSScene scene = GetCurrentScene();
  2181. if (scene) {
  2182. obs_source_t *source = obs_scene_get_source(scene);
  2183. if (QueryRemoveSource(source)) {
  2184. obs_source_remove(source);
  2185. if (api)
  2186. api->on_event(OBS_FRONTEND_EVENT_SCENE_LIST_CHANGED);
  2187. }
  2188. }
  2189. }
  2190. void OBSBasic::RemoveSelectedSceneItem()
  2191. {
  2192. OBSSceneItem item = GetCurrentSceneItem();
  2193. if (item) {
  2194. obs_source_t *source = obs_sceneitem_get_source(item);
  2195. if (QueryRemoveSource(source))
  2196. obs_sceneitem_remove(item);
  2197. }
  2198. }
  2199. struct ReorderInfo {
  2200. int idx = 0;
  2201. OBSBasic *window;
  2202. inline ReorderInfo(OBSBasic *window_) : window(window_) {}
  2203. };
  2204. void OBSBasic::ReorderSceneItem(obs_sceneitem_t *item, size_t idx)
  2205. {
  2206. int count = ui->sources->count();
  2207. int idx_inv = count - (int)idx - 1;
  2208. for (int i = 0; i < count; i++) {
  2209. QListWidgetItem *listItem = ui->sources->item(i);
  2210. OBSSceneItem sceneItem = GetOBSRef<OBSSceneItem>(listItem);
  2211. if (sceneItem == item) {
  2212. if ((int)idx_inv != i) {
  2213. bool sel = (ui->sources->currentRow() == i);
  2214. listItem = TakeListItem(ui->sources, i);
  2215. if (listItem) {
  2216. ui->sources->insertItem(idx_inv,
  2217. listItem);
  2218. SetupVisibilityItem(ui->sources,
  2219. listItem, item);
  2220. if (sel)
  2221. ui->sources->setCurrentRow(
  2222. idx_inv);
  2223. }
  2224. }
  2225. break;
  2226. }
  2227. }
  2228. }
  2229. void OBSBasic::ReorderSources(OBSScene scene)
  2230. {
  2231. ReorderInfo info(this);
  2232. if (scene != GetCurrentScene() || ui->sources->IgnoreReorder())
  2233. return;
  2234. obs_scene_enum_items(scene,
  2235. [] (obs_scene_t*, obs_sceneitem_t *item, void *p)
  2236. {
  2237. ReorderInfo *info =
  2238. reinterpret_cast<ReorderInfo*>(p);
  2239. info->window->ReorderSceneItem(item,
  2240. info->idx++);
  2241. return true;
  2242. }, &info);
  2243. SaveProject();
  2244. }
  2245. /* OBS Callbacks */
  2246. void OBSBasic::SceneReordered(void *data, calldata_t *params)
  2247. {
  2248. OBSBasic *window = static_cast<OBSBasic*>(data);
  2249. obs_scene_t *scene = (obs_scene_t*)calldata_ptr(params, "scene");
  2250. QMetaObject::invokeMethod(window, "ReorderSources",
  2251. Q_ARG(OBSScene, OBSScene(scene)));
  2252. }
  2253. void OBSBasic::SceneItemAdded(void *data, calldata_t *params)
  2254. {
  2255. OBSBasic *window = static_cast<OBSBasic*>(data);
  2256. obs_sceneitem_t *item = (obs_sceneitem_t*)calldata_ptr(params, "item");
  2257. QMetaObject::invokeMethod(window, "AddSceneItem",
  2258. Q_ARG(OBSSceneItem, OBSSceneItem(item)));
  2259. }
  2260. void OBSBasic::SceneItemRemoved(void *data, calldata_t *params)
  2261. {
  2262. OBSBasic *window = static_cast<OBSBasic*>(data);
  2263. obs_sceneitem_t *item = (obs_sceneitem_t*)calldata_ptr(params, "item");
  2264. QMetaObject::invokeMethod(window, "RemoveSceneItem",
  2265. Q_ARG(OBSSceneItem, OBSSceneItem(item)));
  2266. }
  2267. void OBSBasic::SceneItemSelected(void *data, calldata_t *params)
  2268. {
  2269. OBSBasic *window = static_cast<OBSBasic*>(data);
  2270. obs_scene_t *scene = (obs_scene_t*)calldata_ptr(params, "scene");
  2271. obs_sceneitem_t *item = (obs_sceneitem_t*)calldata_ptr(params, "item");
  2272. QMetaObject::invokeMethod(window, "SelectSceneItem",
  2273. Q_ARG(OBSScene, scene), Q_ARG(OBSSceneItem, item),
  2274. Q_ARG(bool, true));
  2275. window->ui->actionCopySource->setEnabled(true);
  2276. }
  2277. void OBSBasic::SceneItemDeselected(void *data, calldata_t *params)
  2278. {
  2279. OBSBasic *window = static_cast<OBSBasic*>(data);
  2280. obs_scene_t *scene = (obs_scene_t*)calldata_ptr(params, "scene");
  2281. obs_sceneitem_t *item = (obs_sceneitem_t*)calldata_ptr(params, "item");
  2282. QMetaObject::invokeMethod(window, "SelectSceneItem",
  2283. Q_ARG(OBSScene, scene), Q_ARG(OBSSceneItem, item),
  2284. Q_ARG(bool, false));
  2285. window->ui->actionCopySource->setEnabled(false);
  2286. }
  2287. void OBSBasic::SourceLoaded(void *data, obs_source_t *source)
  2288. {
  2289. OBSBasic *window = static_cast<OBSBasic*>(data);
  2290. if (obs_scene_from_source(source) != NULL)
  2291. QMetaObject::invokeMethod(window,
  2292. "AddScene",
  2293. Q_ARG(OBSSource, OBSSource(source)));
  2294. }
  2295. void OBSBasic::SourceRemoved(void *data, calldata_t *params)
  2296. {
  2297. obs_source_t *source = (obs_source_t*)calldata_ptr(params, "source");
  2298. if (obs_scene_from_source(source) != NULL)
  2299. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  2300. "RemoveScene",
  2301. Q_ARG(OBSSource, OBSSource(source)));
  2302. }
  2303. void OBSBasic::SourceActivated(void *data, calldata_t *params)
  2304. {
  2305. obs_source_t *source = (obs_source_t*)calldata_ptr(params, "source");
  2306. uint32_t flags = obs_source_get_output_flags(source);
  2307. if (flags & OBS_SOURCE_AUDIO)
  2308. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  2309. "ActivateAudioSource",
  2310. Q_ARG(OBSSource, OBSSource(source)));
  2311. }
  2312. void OBSBasic::SourceDeactivated(void *data, calldata_t *params)
  2313. {
  2314. obs_source_t *source = (obs_source_t*)calldata_ptr(params, "source");
  2315. uint32_t flags = obs_source_get_output_flags(source);
  2316. if (flags & OBS_SOURCE_AUDIO)
  2317. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  2318. "DeactivateAudioSource",
  2319. Q_ARG(OBSSource, OBSSource(source)));
  2320. }
  2321. void OBSBasic::SourceRenamed(void *data, calldata_t *params)
  2322. {
  2323. obs_source_t *source = (obs_source_t*)calldata_ptr(params, "source");
  2324. const char *newName = calldata_string(params, "new_name");
  2325. const char *prevName = calldata_string(params, "prev_name");
  2326. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  2327. "RenameSources",
  2328. Q_ARG(OBSSource, source),
  2329. Q_ARG(QString, QT_UTF8(newName)),
  2330. Q_ARG(QString, QT_UTF8(prevName)));
  2331. blog(LOG_INFO, "Source '%s' renamed to '%s'", prevName, newName);
  2332. }
  2333. void OBSBasic::DrawBackdrop(float cx, float cy)
  2334. {
  2335. if (!box)
  2336. return;
  2337. gs_effect_t *solid = obs_get_base_effect(OBS_EFFECT_SOLID);
  2338. gs_eparam_t *color = gs_effect_get_param_by_name(solid, "color");
  2339. gs_technique_t *tech = gs_effect_get_technique(solid, "Solid");
  2340. vec4 colorVal;
  2341. vec4_set(&colorVal, 0.0f, 0.0f, 0.0f, 1.0f);
  2342. gs_effect_set_vec4(color, &colorVal);
  2343. gs_technique_begin(tech);
  2344. gs_technique_begin_pass(tech, 0);
  2345. gs_matrix_push();
  2346. gs_matrix_identity();
  2347. gs_matrix_scale3f(float(cx), float(cy), 1.0f);
  2348. gs_load_vertexbuffer(box);
  2349. gs_draw(GS_TRISTRIP, 0, 0);
  2350. gs_matrix_pop();
  2351. gs_technique_end_pass(tech);
  2352. gs_technique_end(tech);
  2353. gs_load_vertexbuffer(nullptr);
  2354. }
  2355. void OBSBasic::RenderMain(void *data, uint32_t cx, uint32_t cy)
  2356. {
  2357. OBSBasic *window = static_cast<OBSBasic*>(data);
  2358. obs_video_info ovi;
  2359. obs_get_video_info(&ovi);
  2360. window->previewCX = int(window->previewScale * float(ovi.base_width));
  2361. window->previewCY = int(window->previewScale * float(ovi.base_height));
  2362. gs_viewport_push();
  2363. gs_projection_push();
  2364. /* --------------------------------------- */
  2365. gs_ortho(0.0f, float(ovi.base_width), 0.0f, float(ovi.base_height),
  2366. -100.0f, 100.0f);
  2367. gs_set_viewport(window->previewX, window->previewY,
  2368. window->previewCX, window->previewCY);
  2369. window->DrawBackdrop(float(ovi.base_width), float(ovi.base_height));
  2370. if (window->IsPreviewProgramMode()) {
  2371. OBSScene scene = window->GetCurrentScene();
  2372. obs_source_t *source = obs_scene_get_source(scene);
  2373. if (source)
  2374. obs_source_video_render(source);
  2375. } else {
  2376. obs_render_main_texture();
  2377. }
  2378. gs_load_vertexbuffer(nullptr);
  2379. /* --------------------------------------- */
  2380. QSize previewSize = GetPixelSize(window->ui->preview);
  2381. float right = float(previewSize.width()) - window->previewX;
  2382. float bottom = float(previewSize.height()) - window->previewY;
  2383. gs_ortho(-window->previewX, right,
  2384. -window->previewY, bottom,
  2385. -100.0f, 100.0f);
  2386. gs_reset_viewport();
  2387. window->ui->preview->DrawSceneEditing();
  2388. /* --------------------------------------- */
  2389. gs_projection_pop();
  2390. gs_viewport_pop();
  2391. UNUSED_PARAMETER(cx);
  2392. UNUSED_PARAMETER(cy);
  2393. }
  2394. /* Main class functions */
  2395. obs_service_t *OBSBasic::GetService()
  2396. {
  2397. if (!service) {
  2398. service = obs_service_create("rtmp_common", NULL, NULL,
  2399. nullptr);
  2400. obs_service_release(service);
  2401. }
  2402. return service;
  2403. }
  2404. void OBSBasic::SetService(obs_service_t *newService)
  2405. {
  2406. if (newService)
  2407. service = newService;
  2408. }
  2409. bool OBSBasic::StreamingActive() const
  2410. {
  2411. if (!outputHandler)
  2412. return false;
  2413. return outputHandler->StreamingActive();
  2414. }
  2415. bool OBSBasic::Active() const
  2416. {
  2417. if (!outputHandler)
  2418. return false;
  2419. return outputHandler->Active();
  2420. }
  2421. #ifdef _WIN32
  2422. #define IS_WIN32 1
  2423. #else
  2424. #define IS_WIN32 0
  2425. #endif
  2426. static inline int AttemptToResetVideo(struct obs_video_info *ovi)
  2427. {
  2428. return obs_reset_video(ovi);
  2429. }
  2430. static inline enum obs_scale_type GetScaleType(ConfigFile &basicConfig)
  2431. {
  2432. const char *scaleTypeStr = config_get_string(basicConfig,
  2433. "Video", "ScaleType");
  2434. if (astrcmpi(scaleTypeStr, "bilinear") == 0)
  2435. return OBS_SCALE_BILINEAR;
  2436. else if (astrcmpi(scaleTypeStr, "lanczos") == 0)
  2437. return OBS_SCALE_LANCZOS;
  2438. else
  2439. return OBS_SCALE_BICUBIC;
  2440. }
  2441. static inline enum video_format GetVideoFormatFromName(const char *name)
  2442. {
  2443. if (astrcmpi(name, "I420") == 0)
  2444. return VIDEO_FORMAT_I420;
  2445. else if (astrcmpi(name, "NV12") == 0)
  2446. return VIDEO_FORMAT_NV12;
  2447. else if (astrcmpi(name, "I444") == 0)
  2448. return VIDEO_FORMAT_I444;
  2449. #if 0 //currently unsupported
  2450. else if (astrcmpi(name, "YVYU") == 0)
  2451. return VIDEO_FORMAT_YVYU;
  2452. else if (astrcmpi(name, "YUY2") == 0)
  2453. return VIDEO_FORMAT_YUY2;
  2454. else if (astrcmpi(name, "UYVY") == 0)
  2455. return VIDEO_FORMAT_UYVY;
  2456. #endif
  2457. else
  2458. return VIDEO_FORMAT_RGBA;
  2459. }
  2460. void OBSBasic::ResetUI()
  2461. {
  2462. bool studioPortraitLayout = config_get_bool(GetGlobalConfig(),
  2463. "BasicWindow", "StudioPortraitLayout");
  2464. if (studioPortraitLayout)
  2465. ui->previewLayout->setDirection(QBoxLayout::TopToBottom);
  2466. else
  2467. ui->previewLayout->setDirection(QBoxLayout::LeftToRight);
  2468. }
  2469. int OBSBasic::ResetVideo()
  2470. {
  2471. if (outputHandler && outputHandler->Active())
  2472. return OBS_VIDEO_CURRENTLY_ACTIVE;
  2473. ProfileScope("OBSBasic::ResetVideo");
  2474. struct obs_video_info ovi;
  2475. int ret;
  2476. GetConfigFPS(ovi.fps_num, ovi.fps_den);
  2477. const char *colorFormat = config_get_string(basicConfig, "Video",
  2478. "ColorFormat");
  2479. const char *colorSpace = config_get_string(basicConfig, "Video",
  2480. "ColorSpace");
  2481. const char *colorRange = config_get_string(basicConfig, "Video",
  2482. "ColorRange");
  2483. ovi.graphics_module = App()->GetRenderModule();
  2484. ovi.base_width = (uint32_t)config_get_uint(basicConfig,
  2485. "Video", "BaseCX");
  2486. ovi.base_height = (uint32_t)config_get_uint(basicConfig,
  2487. "Video", "BaseCY");
  2488. ovi.output_width = (uint32_t)config_get_uint(basicConfig,
  2489. "Video", "OutputCX");
  2490. ovi.output_height = (uint32_t)config_get_uint(basicConfig,
  2491. "Video", "OutputCY");
  2492. ovi.output_format = GetVideoFormatFromName(colorFormat);
  2493. ovi.colorspace = astrcmpi(colorSpace, "601") == 0 ?
  2494. VIDEO_CS_601 : VIDEO_CS_709;
  2495. ovi.range = astrcmpi(colorRange, "Full") == 0 ?
  2496. VIDEO_RANGE_FULL : VIDEO_RANGE_PARTIAL;
  2497. ovi.adapter = config_get_uint(App()->GlobalConfig(),
  2498. "Video", "AdapterIdx");
  2499. ovi.gpu_conversion = true;
  2500. ovi.scale_type = GetScaleType(basicConfig);
  2501. if (ovi.base_width == 0 || ovi.base_height == 0) {
  2502. ovi.base_width = 1920;
  2503. ovi.base_height = 1080;
  2504. config_set_uint(basicConfig, "Video", "BaseCX", 1920);
  2505. config_set_uint(basicConfig, "Video", "BaseCY", 1080);
  2506. }
  2507. if (ovi.output_width == 0 || ovi.output_height == 0) {
  2508. ovi.output_width = ovi.base_width;
  2509. ovi.output_height = ovi.base_height;
  2510. config_set_uint(basicConfig, "Video", "OutputCX",
  2511. ovi.base_width);
  2512. config_set_uint(basicConfig, "Video", "OutputCY",
  2513. ovi.base_height);
  2514. }
  2515. ret = AttemptToResetVideo(&ovi);
  2516. if (IS_WIN32 && ret != OBS_VIDEO_SUCCESS) {
  2517. if (ret == OBS_VIDEO_CURRENTLY_ACTIVE) {
  2518. blog(LOG_WARNING, "Tried to reset when "
  2519. "already active");
  2520. return ret;
  2521. }
  2522. /* Try OpenGL if DirectX fails on windows */
  2523. if (astrcmpi(ovi.graphics_module, DL_OPENGL) != 0) {
  2524. blog(LOG_WARNING, "Failed to initialize obs video (%d) "
  2525. "with graphics_module='%s', retrying "
  2526. "with graphics_module='%s'",
  2527. ret, ovi.graphics_module,
  2528. DL_OPENGL);
  2529. ovi.graphics_module = DL_OPENGL;
  2530. ret = AttemptToResetVideo(&ovi);
  2531. }
  2532. } else if (ret == OBS_VIDEO_SUCCESS) {
  2533. ResizePreview(ovi.base_width, ovi.base_height);
  2534. if (program)
  2535. ResizeProgram(ovi.base_width, ovi.base_height);
  2536. }
  2537. if (ret == OBS_VIDEO_SUCCESS)
  2538. OBSBasicStats::InitializeValues();
  2539. return ret;
  2540. }
  2541. bool OBSBasic::ResetAudio()
  2542. {
  2543. ProfileScope("OBSBasic::ResetAudio");
  2544. struct obs_audio_info ai;
  2545. ai.samples_per_sec = config_get_uint(basicConfig, "Audio",
  2546. "SampleRate");
  2547. const char *channelSetupStr = config_get_string(basicConfig,
  2548. "Audio", "ChannelSetup");
  2549. if (strcmp(channelSetupStr, "Mono") == 0)
  2550. ai.speakers = SPEAKERS_MONO;
  2551. else if (strcmp(channelSetupStr, "2.1") == 0)
  2552. ai.speakers = SPEAKERS_2POINT1;
  2553. else if (strcmp(channelSetupStr, "4.0 Quad") == 0)
  2554. ai.speakers = SPEAKERS_QUAD;
  2555. else if (strcmp(channelSetupStr, "4.1") == 0)
  2556. ai.speakers = SPEAKERS_4POINT1;
  2557. else if (strcmp(channelSetupStr, "5.1") == 0)
  2558. ai.speakers = SPEAKERS_5POINT1;
  2559. else if (strcmp(channelSetupStr, "7.1") == 0)
  2560. ai.speakers = SPEAKERS_7POINT1;
  2561. else
  2562. ai.speakers = SPEAKERS_STEREO;
  2563. return obs_reset_audio(&ai);
  2564. }
  2565. void OBSBasic::ResetAudioDevice(const char *sourceId, const char *deviceId,
  2566. const char *deviceDesc, int channel)
  2567. {
  2568. bool disable = deviceId && strcmp(deviceId, "disabled") == 0;
  2569. obs_source_t *source;
  2570. obs_data_t *settings;
  2571. source = obs_get_output_source(channel);
  2572. if (source) {
  2573. if (disable) {
  2574. obs_set_output_source(channel, nullptr);
  2575. } else {
  2576. settings = obs_source_get_settings(source);
  2577. const char *oldId = obs_data_get_string(settings,
  2578. "device_id");
  2579. if (strcmp(oldId, deviceId) != 0) {
  2580. obs_data_set_string(settings, "device_id",
  2581. deviceId);
  2582. obs_source_update(source, settings);
  2583. }
  2584. obs_data_release(settings);
  2585. }
  2586. obs_source_release(source);
  2587. } else if (!disable) {
  2588. settings = obs_data_create();
  2589. obs_data_set_string(settings, "device_id", deviceId);
  2590. source = obs_source_create(sourceId, deviceDesc, settings,
  2591. nullptr);
  2592. obs_data_release(settings);
  2593. obs_set_output_source(channel, source);
  2594. obs_source_release(source);
  2595. }
  2596. }
  2597. void OBSBasic::ResizePreview(uint32_t cx, uint32_t cy)
  2598. {
  2599. QSize targetSize;
  2600. bool isFixedScaling;
  2601. obs_video_info ovi;
  2602. /* resize preview panel to fix to the top section of the window */
  2603. targetSize = GetPixelSize(ui->preview);
  2604. isFixedScaling = ui->preview->IsFixedScaling();
  2605. obs_get_video_info(&ovi);
  2606. if (isFixedScaling) {
  2607. previewScale = ui->preview->GetScalingAmount();
  2608. GetCenterPosFromFixedScale(int(cx), int(cy),
  2609. targetSize.width() - PREVIEW_EDGE_SIZE * 2,
  2610. targetSize.height() - PREVIEW_EDGE_SIZE * 2,
  2611. previewX, previewY, previewScale);
  2612. previewX += ui->preview->GetScrollX();
  2613. previewY += ui->preview->GetScrollY();
  2614. } else {
  2615. GetScaleAndCenterPos(int(cx), int(cy),
  2616. targetSize.width() - PREVIEW_EDGE_SIZE * 2,
  2617. targetSize.height() - PREVIEW_EDGE_SIZE * 2,
  2618. previewX, previewY, previewScale);
  2619. }
  2620. previewX += float(PREVIEW_EDGE_SIZE);
  2621. previewY += float(PREVIEW_EDGE_SIZE);
  2622. }
  2623. void OBSBasic::CloseDialogs()
  2624. {
  2625. QList<QDialog*> childDialogs = this->findChildren<QDialog *>();
  2626. if (!childDialogs.isEmpty()) {
  2627. for (int i = 0; i < childDialogs.size(); ++i) {
  2628. childDialogs.at(i)->close();
  2629. }
  2630. }
  2631. for (QPointer<QWidget> &projector : windowProjectors) {
  2632. delete projector;
  2633. projector.clear();
  2634. }
  2635. for (QPointer<QWidget> &projector : projectors) {
  2636. delete projector;
  2637. projector.clear();
  2638. }
  2639. if (!stats.isNull()) stats->close(); //call close to save Stats geometry
  2640. }
  2641. void OBSBasic::EnumDialogs()
  2642. {
  2643. visDialogs.clear();
  2644. modalDialogs.clear();
  2645. visMsgBoxes.clear();
  2646. /* fill list of Visible dialogs and Modal dialogs */
  2647. QList<QDialog*> dialogs = findChildren<QDialog*>();
  2648. for (QDialog *dialog : dialogs) {
  2649. if (dialog->isVisible())
  2650. visDialogs.append(dialog);
  2651. if (dialog->isModal())
  2652. modalDialogs.append(dialog);
  2653. }
  2654. /* fill list of Visible message boxes */
  2655. QList<QMessageBox*> msgBoxes = findChildren<QMessageBox*>();
  2656. for (QMessageBox *msgbox : msgBoxes) {
  2657. if (msgbox->isVisible())
  2658. visMsgBoxes.append(msgbox);
  2659. }
  2660. }
  2661. void OBSBasic::ClearSceneData()
  2662. {
  2663. disableSaving++;
  2664. CloseDialogs();
  2665. ClearVolumeControls();
  2666. ClearListItems(ui->scenes);
  2667. ClearListItems(ui->sources);
  2668. ClearQuickTransitions();
  2669. ui->transitions->clear();
  2670. obs_set_output_source(0, nullptr);
  2671. obs_set_output_source(1, nullptr);
  2672. obs_set_output_source(2, nullptr);
  2673. obs_set_output_source(3, nullptr);
  2674. obs_set_output_source(4, nullptr);
  2675. obs_set_output_source(5, nullptr);
  2676. lastScene = nullptr;
  2677. swapScene = nullptr;
  2678. programScene = nullptr;
  2679. auto cb = [](void *unused, obs_source_t *source)
  2680. {
  2681. obs_source_remove(source);
  2682. UNUSED_PARAMETER(unused);
  2683. return true;
  2684. };
  2685. obs_enum_sources(cb, nullptr);
  2686. disableSaving--;
  2687. blog(LOG_INFO, "All scene data cleared");
  2688. blog(LOG_INFO, "------------------------------------------------");
  2689. }
  2690. void OBSBasic::closeEvent(QCloseEvent *event)
  2691. {
  2692. if (isVisible())
  2693. config_set_string(App()->GlobalConfig(),
  2694. "BasicWindow", "geometry",
  2695. saveGeometry().toBase64().constData());
  2696. config_set_string(App()->GlobalConfig(),
  2697. "BasicWindow", "DockState",
  2698. saveState().toBase64().constData());
  2699. if (outputHandler && outputHandler->Active()) {
  2700. SetShowing(true);
  2701. QMessageBox::StandardButton button = OBSMessageBox::question(
  2702. this, QTStr("ConfirmExit.Title"),
  2703. QTStr("ConfirmExit.Text"));
  2704. if (button == QMessageBox::No) {
  2705. event->ignore();
  2706. return;
  2707. }
  2708. }
  2709. QWidget::closeEvent(event);
  2710. if (!event->isAccepted())
  2711. return;
  2712. blog(LOG_INFO, SHUTDOWN_SEPARATOR);
  2713. if (updateCheckThread)
  2714. updateCheckThread->wait();
  2715. if (logUploadThread)
  2716. logUploadThread->wait();
  2717. signalHandlers.clear();
  2718. SaveProjectNow();
  2719. if (api)
  2720. api->on_event(OBS_FRONTEND_EVENT_EXIT);
  2721. disableSaving++;
  2722. /* Clear all scene data (dialogs, widgets, widget sub-items, scenes,
  2723. * sources, etc) so that all references are released before shutdown */
  2724. ClearSceneData();
  2725. App()->quit();
  2726. }
  2727. void OBSBasic::changeEvent(QEvent *event)
  2728. {
  2729. if (event->type() == QEvent::WindowStateChange &&
  2730. isMinimized() &&
  2731. trayIcon &&
  2732. trayIcon->isVisible() &&
  2733. sysTrayMinimizeToTray()) {
  2734. ToggleShowHide();
  2735. }
  2736. }
  2737. void OBSBasic::on_actionShow_Recordings_triggered()
  2738. {
  2739. const char *mode = config_get_string(basicConfig, "Output", "Mode");
  2740. const char *path = strcmp(mode, "Advanced") ?
  2741. config_get_string(basicConfig, "SimpleOutput", "FilePath") :
  2742. config_get_string(basicConfig, "AdvOut", "RecFilePath");
  2743. QDesktopServices::openUrl(QUrl::fromLocalFile(path));
  2744. }
  2745. void OBSBasic::on_actionRemux_triggered()
  2746. {
  2747. const char *mode = config_get_string(basicConfig, "Output", "Mode");
  2748. const char *path = strcmp(mode, "Advanced") ?
  2749. config_get_string(basicConfig, "SimpleOutput", "FilePath") :
  2750. config_get_string(basicConfig, "AdvOut", "RecFilePath");
  2751. OBSRemux remux(path, this);
  2752. remux.exec();
  2753. }
  2754. void OBSBasic::on_action_Settings_triggered()
  2755. {
  2756. OBSBasicSettings settings(this);
  2757. settings.exec();
  2758. SystemTray(false);
  2759. }
  2760. void OBSBasic::on_actionAdvAudioProperties_triggered()
  2761. {
  2762. if (advAudioWindow != nullptr) {
  2763. advAudioWindow->raise();
  2764. return;
  2765. }
  2766. advAudioWindow = new OBSBasicAdvAudio(this);
  2767. advAudioWindow->show();
  2768. advAudioWindow->setAttribute(Qt::WA_DeleteOnClose, true);
  2769. connect(advAudioWindow, SIGNAL(destroyed()),
  2770. this, SLOT(on_advAudioProps_destroyed()));
  2771. }
  2772. void OBSBasic::on_advAudioProps_clicked()
  2773. {
  2774. on_actionAdvAudioProperties_triggered();
  2775. }
  2776. void OBSBasic::on_advAudioProps_destroyed()
  2777. {
  2778. advAudioWindow = nullptr;
  2779. }
  2780. void OBSBasic::on_scenes_currentItemChanged(QListWidgetItem *current,
  2781. QListWidgetItem *prev)
  2782. {
  2783. obs_source_t *source = NULL;
  2784. if (sceneChanging)
  2785. return;
  2786. if (current) {
  2787. obs_scene_t *scene;
  2788. scene = GetOBSRef<OBSScene>(current);
  2789. source = obs_scene_get_source(scene);
  2790. }
  2791. SetCurrentScene(source);
  2792. UNUSED_PARAMETER(prev);
  2793. }
  2794. void OBSBasic::EditSceneName()
  2795. {
  2796. QListWidgetItem *item = ui->scenes->currentItem();
  2797. Qt::ItemFlags flags = item->flags();
  2798. item->setFlags(flags | Qt::ItemIsEditable);
  2799. ui->scenes->editItem(item);
  2800. item->setFlags(flags);
  2801. }
  2802. static void AddProjectorMenuMonitors(QMenu *parent, QObject *target,
  2803. const char *slot)
  2804. {
  2805. QAction *action;
  2806. QList<QScreen*> screens = QGuiApplication::screens();
  2807. for (int i = 0; i < screens.size(); i++) {
  2808. QRect screenGeometry = screens[i]->geometry();
  2809. QString str = QString("%1 %2: %3x%4 @ %5,%6").
  2810. arg(QTStr("Display"),
  2811. QString::number(i),
  2812. QString::number((int)screenGeometry.width()),
  2813. QString::number((int)screenGeometry.height()),
  2814. QString::number((int)screenGeometry.x()),
  2815. QString::number((int)screenGeometry.y()));
  2816. action = parent->addAction(str, target, slot);
  2817. action->setProperty("monitor", i);
  2818. }
  2819. }
  2820. void OBSBasic::on_scenes_customContextMenuRequested(const QPoint &pos)
  2821. {
  2822. QListWidgetItem *item = ui->scenes->itemAt(pos);
  2823. QPointer<QMenu> sceneProjectorMenu;
  2824. QMenu popup(this);
  2825. QMenu order(QTStr("Basic.MainMenu.Edit.Order"), this);
  2826. popup.addAction(QTStr("Add"),
  2827. this, SLOT(on_actionAddScene_triggered()));
  2828. if (item) {
  2829. popup.addSeparator();
  2830. popup.addAction(QTStr("Duplicate"),
  2831. this, SLOT(DuplicateSelectedScene()));
  2832. popup.addAction(QTStr("Rename"),
  2833. this, SLOT(EditSceneName()));
  2834. popup.addAction(QTStr("Remove"),
  2835. this, SLOT(RemoveSelectedScene()),
  2836. DeleteKeys.front());
  2837. popup.addSeparator();
  2838. order.addAction(QTStr("Basic.MainMenu.Edit.Order.MoveUp"),
  2839. this, SLOT(on_actionSceneUp_triggered()));
  2840. order.addAction(QTStr("Basic.MainMenu.Edit.Order.MoveDown"),
  2841. this, SLOT(on_actionSceneDown_triggered()));
  2842. order.addSeparator();
  2843. order.addAction(QTStr("Basic.MainMenu.Edit.Order.MoveToTop"),
  2844. this, SLOT(MoveSceneToTop()));
  2845. order.addAction(QTStr("Basic.MainMenu.Edit.Order.MoveToBottom"),
  2846. this, SLOT(MoveSceneToBottom()));
  2847. popup.addMenu(&order);
  2848. popup.addSeparator();
  2849. sceneProjectorMenu = new QMenu(QTStr("SceneProjector"));
  2850. AddProjectorMenuMonitors(sceneProjectorMenu, this,
  2851. SLOT(OpenSceneProjector()));
  2852. popup.addMenu(sceneProjectorMenu);
  2853. QAction *sceneWindow = popup.addAction(
  2854. QTStr("SceneWindow"),
  2855. this, SLOT(OpenSceneWindow()));
  2856. popup.addAction(sceneWindow);
  2857. popup.addSeparator();
  2858. popup.addAction(QTStr("Filters"), this,
  2859. SLOT(OpenSceneFilters()));
  2860. popup.addSeparator();
  2861. QMenu *transitionMenu = CreatePerSceneTransitionMenu();
  2862. popup.addMenu(transitionMenu);
  2863. /* ---------------------- */
  2864. QAction *multiviewAction = popup.addAction(
  2865. QTStr("ShowInMultiview"));
  2866. OBSSource source = GetCurrentSceneSource();
  2867. OBSData data = obs_source_get_private_settings(source);
  2868. obs_data_release(data);
  2869. obs_data_set_default_bool(data, "show_in_multiview",
  2870. true);
  2871. bool show = obs_data_get_bool(data, "show_in_multiview");
  2872. multiviewAction->setCheckable(true);
  2873. multiviewAction->setChecked(show);
  2874. auto showInMultiview = [this] (OBSData data)
  2875. {
  2876. bool show = obs_data_get_bool(data,
  2877. "show_in_multiview");
  2878. obs_data_set_bool(data, "show_in_multiview",
  2879. !show);
  2880. OBSProjector::UpdateMultiviewProjectors();
  2881. };
  2882. connect(multiviewAction, &QAction::triggered,
  2883. std::bind(showInMultiview, data));
  2884. }
  2885. popup.exec(QCursor::pos());
  2886. }
  2887. void OBSBasic::on_actionAddScene_triggered()
  2888. {
  2889. string name;
  2890. QString format{QTStr("Basic.Main.DefaultSceneName.Text")};
  2891. int i = 2;
  2892. QString placeHolderText = format.arg(i);
  2893. obs_source_t *source = nullptr;
  2894. while ((source = obs_get_source_by_name(QT_TO_UTF8(placeHolderText)))) {
  2895. obs_source_release(source);
  2896. placeHolderText = format.arg(++i);
  2897. }
  2898. bool accepted = NameDialog::AskForName(this,
  2899. QTStr("Basic.Main.AddSceneDlg.Title"),
  2900. QTStr("Basic.Main.AddSceneDlg.Text"),
  2901. name,
  2902. placeHolderText);
  2903. if (accepted) {
  2904. if (name.empty()) {
  2905. OBSMessageBox::information(this,
  2906. QTStr("NoNameEntered.Title"),
  2907. QTStr("NoNameEntered.Text"));
  2908. on_actionAddScene_triggered();
  2909. return;
  2910. }
  2911. obs_source_t *source = obs_get_source_by_name(name.c_str());
  2912. if (source) {
  2913. OBSMessageBox::information(this,
  2914. QTStr("NameExists.Title"),
  2915. QTStr("NameExists.Text"));
  2916. obs_source_release(source);
  2917. on_actionAddScene_triggered();
  2918. return;
  2919. }
  2920. obs_scene_t *scene = obs_scene_create(name.c_str());
  2921. source = obs_scene_get_source(scene);
  2922. AddScene(source);
  2923. SetCurrentScene(source);
  2924. obs_scene_release(scene);
  2925. }
  2926. }
  2927. void OBSBasic::on_actionRemoveScene_triggered()
  2928. {
  2929. OBSScene scene = GetCurrentScene();
  2930. obs_source_t *source = obs_scene_get_source(scene);
  2931. if (source && QueryRemoveSource(source))
  2932. obs_source_remove(source);
  2933. }
  2934. void OBSBasic::ChangeSceneIndex(bool relative, int offset, int invalidIdx)
  2935. {
  2936. int idx = ui->scenes->currentRow();
  2937. if (idx == -1 || idx == invalidIdx)
  2938. return;
  2939. sceneChanging = true;
  2940. QListWidgetItem *item = ui->scenes->takeItem(idx);
  2941. if (!relative)
  2942. idx = 0;
  2943. ui->scenes->insertItem(idx + offset, item);
  2944. ui->scenes->setCurrentRow(idx + offset);
  2945. item->setSelected(true);
  2946. sceneChanging = false;
  2947. }
  2948. void OBSBasic::on_actionSceneUp_triggered()
  2949. {
  2950. ChangeSceneIndex(true, -1, 0);
  2951. }
  2952. void OBSBasic::on_actionSceneDown_triggered()
  2953. {
  2954. ChangeSceneIndex(true, 1, ui->scenes->count() - 1);
  2955. }
  2956. void OBSBasic::MoveSceneToTop()
  2957. {
  2958. ChangeSceneIndex(false, 0, 0);
  2959. }
  2960. void OBSBasic::MoveSceneToBottom()
  2961. {
  2962. ChangeSceneIndex(false, ui->scenes->count() - 1,
  2963. ui->scenes->count() - 1);
  2964. }
  2965. void OBSBasic::on_sources_itemSelectionChanged()
  2966. {
  2967. SignalBlocker sourcesSignalBlocker(ui->sources);
  2968. auto updateItemSelection = [&]()
  2969. {
  2970. ignoreSelectionUpdate = true;
  2971. for (int i = 0; i < ui->sources->count(); i++)
  2972. {
  2973. QListWidgetItem *wItem = ui->sources->item(i);
  2974. OBSSceneItem item = GetOBSRef<OBSSceneItem>(wItem);
  2975. obs_sceneitem_select(item, wItem->isSelected());
  2976. }
  2977. ignoreSelectionUpdate = false;
  2978. };
  2979. using updateItemSelection_t = decltype(updateItemSelection);
  2980. obs_scene_atomic_update(GetCurrentScene(),
  2981. [](void *data, obs_scene_t *)
  2982. {
  2983. (*static_cast<updateItemSelection_t*>(data))();
  2984. }, static_cast<void*>(&updateItemSelection));
  2985. }
  2986. void OBSBasic::EditSceneItemName()
  2987. {
  2988. QListWidgetItem *item = GetTopSelectedSourceItem();
  2989. Qt::ItemFlags flags = item->flags();
  2990. OBSSceneItem sceneItem= GetOBSRef<OBSSceneItem>(item);
  2991. obs_source_t *source = obs_sceneitem_get_source(sceneItem);
  2992. const char *name = obs_source_get_name(source);
  2993. item->setText(QT_UTF8(name));
  2994. item->setFlags(flags | Qt::ItemIsEditable);
  2995. ui->sources->removeItemWidget(item);
  2996. ui->sources->editItem(item);
  2997. item->setFlags(flags);
  2998. }
  2999. void OBSBasic::SetDeinterlacingMode()
  3000. {
  3001. QAction *action = reinterpret_cast<QAction*>(sender());
  3002. obs_deinterlace_mode mode =
  3003. (obs_deinterlace_mode)action->property("mode").toInt();
  3004. OBSSceneItem sceneItem = GetCurrentSceneItem();
  3005. obs_source_t *source = obs_sceneitem_get_source(sceneItem);
  3006. obs_source_set_deinterlace_mode(source, mode);
  3007. }
  3008. void OBSBasic::SetDeinterlacingOrder()
  3009. {
  3010. QAction *action = reinterpret_cast<QAction*>(sender());
  3011. obs_deinterlace_field_order order =
  3012. (obs_deinterlace_field_order)action->property("order").toInt();
  3013. OBSSceneItem sceneItem = GetCurrentSceneItem();
  3014. obs_source_t *source = obs_sceneitem_get_source(sceneItem);
  3015. obs_source_set_deinterlace_field_order(source, order);
  3016. }
  3017. QMenu *OBSBasic::AddDeinterlacingMenu(obs_source_t *source)
  3018. {
  3019. QMenu *menu = new QMenu(QTStr("Deinterlacing"));
  3020. obs_deinterlace_mode deinterlaceMode =
  3021. obs_source_get_deinterlace_mode(source);
  3022. obs_deinterlace_field_order deinterlaceOrder =
  3023. obs_source_get_deinterlace_field_order(source);
  3024. QAction *action;
  3025. #define ADD_MODE(name, mode) \
  3026. action = menu->addAction(QTStr("" name), this, \
  3027. SLOT(SetDeinterlacingMode())); \
  3028. action->setProperty("mode", (int)mode); \
  3029. action->setCheckable(true); \
  3030. action->setChecked(deinterlaceMode == mode);
  3031. ADD_MODE("Disable", OBS_DEINTERLACE_MODE_DISABLE);
  3032. ADD_MODE("Deinterlacing.Discard", OBS_DEINTERLACE_MODE_DISCARD);
  3033. ADD_MODE("Deinterlacing.Retro", OBS_DEINTERLACE_MODE_RETRO);
  3034. ADD_MODE("Deinterlacing.Blend", OBS_DEINTERLACE_MODE_BLEND);
  3035. ADD_MODE("Deinterlacing.Blend2x", OBS_DEINTERLACE_MODE_BLEND_2X);
  3036. ADD_MODE("Deinterlacing.Linear", OBS_DEINTERLACE_MODE_LINEAR);
  3037. ADD_MODE("Deinterlacing.Linear2x", OBS_DEINTERLACE_MODE_LINEAR_2X);
  3038. ADD_MODE("Deinterlacing.Yadif", OBS_DEINTERLACE_MODE_YADIF);
  3039. ADD_MODE("Deinterlacing.Yadif2x", OBS_DEINTERLACE_MODE_YADIF_2X);
  3040. #undef ADD_MODE
  3041. menu->addSeparator();
  3042. #define ADD_ORDER(name, order) \
  3043. action = menu->addAction(QTStr("Deinterlacing." name), this, \
  3044. SLOT(SetDeinterlacingOrder())); \
  3045. action->setProperty("order", (int)order); \
  3046. action->setCheckable(true); \
  3047. action->setChecked(deinterlaceOrder == order);
  3048. ADD_ORDER("TopFieldFirst", OBS_DEINTERLACE_FIELD_ORDER_TOP);
  3049. ADD_ORDER("BottomFieldFirst", OBS_DEINTERLACE_FIELD_ORDER_BOTTOM);
  3050. #undef ADD_ORDER
  3051. return menu;
  3052. }
  3053. void OBSBasic::SetScaleFilter()
  3054. {
  3055. QAction *action = reinterpret_cast<QAction*>(sender());
  3056. obs_scale_type mode = (obs_scale_type)action->property("mode").toInt();
  3057. OBSSceneItem sceneItem = GetCurrentSceneItem();
  3058. obs_sceneitem_set_scale_filter(sceneItem, mode);
  3059. }
  3060. QMenu *OBSBasic::AddScaleFilteringMenu(obs_sceneitem_t *item)
  3061. {
  3062. QMenu *menu = new QMenu(QTStr("ScaleFiltering"));
  3063. obs_scale_type scaleFilter = obs_sceneitem_get_scale_filter(item);
  3064. QAction *action;
  3065. #define ADD_MODE(name, mode) \
  3066. action = menu->addAction(QTStr("" name), this, \
  3067. SLOT(SetScaleFilter())); \
  3068. action->setProperty("mode", (int)mode); \
  3069. action->setCheckable(true); \
  3070. action->setChecked(scaleFilter == mode);
  3071. ADD_MODE("Disable", OBS_SCALE_DISABLE);
  3072. ADD_MODE("ScaleFiltering.Point", OBS_SCALE_POINT);
  3073. ADD_MODE("ScaleFiltering.Bilinear", OBS_SCALE_BILINEAR);
  3074. ADD_MODE("ScaleFiltering.Bicubic", OBS_SCALE_BICUBIC);
  3075. ADD_MODE("ScaleFiltering.Lanczos", OBS_SCALE_LANCZOS);
  3076. #undef ADD_MODE
  3077. return menu;
  3078. }
  3079. void OBSBasic::CreateSourcePopupMenu(QListWidgetItem *item, bool preview)
  3080. {
  3081. QMenu popup(this);
  3082. QPointer<QMenu> previewProjector;
  3083. QPointer<QMenu> sourceProjector;
  3084. if (preview) {
  3085. QAction *action = popup.addAction(
  3086. QTStr("Basic.Main.PreviewConextMenu.Enable"),
  3087. this, SLOT(TogglePreview()));
  3088. action->setCheckable(true);
  3089. action->setChecked(
  3090. obs_display_enabled(ui->preview->GetDisplay()));
  3091. if (IsPreviewProgramMode())
  3092. action->setEnabled(false);
  3093. popup.addAction(ui->actionLockPreview);
  3094. popup.addMenu(ui->scalingMenu);
  3095. previewProjector = new QMenu(QTStr("PreviewProjector"));
  3096. AddProjectorMenuMonitors(previewProjector, this,
  3097. SLOT(OpenPreviewProjector()));
  3098. popup.addMenu(previewProjector);
  3099. QAction *previewWindow = popup.addAction(
  3100. QTStr("PreviewWindow"),
  3101. this, SLOT(OpenPreviewWindow()));
  3102. popup.addAction(previewWindow);
  3103. popup.addSeparator();
  3104. }
  3105. QPointer<QMenu> addSourceMenu = CreateAddSourcePopupMenu();
  3106. if (addSourceMenu)
  3107. popup.addMenu(addSourceMenu);
  3108. ui->actionCopyFilters->setEnabled(false);
  3109. popup.addSeparator();
  3110. popup.addAction(ui->actionCopySource);
  3111. popup.addAction(ui->actionPasteRef);
  3112. popup.addAction(ui->actionPasteDup);
  3113. popup.addSeparator();
  3114. popup.addSeparator();
  3115. popup.addAction(ui->actionCopyFilters);
  3116. popup.addAction(ui->actionPasteFilters);
  3117. popup.addSeparator();
  3118. if (item) {
  3119. if (addSourceMenu)
  3120. popup.addSeparator();
  3121. OBSSceneItem sceneItem = GetSceneItem(item);
  3122. obs_source_t *source = obs_sceneitem_get_source(sceneItem);
  3123. uint32_t flags = obs_source_get_output_flags(source);
  3124. bool isAsyncVideo = (flags & OBS_SOURCE_ASYNC_VIDEO) ==
  3125. OBS_SOURCE_ASYNC_VIDEO;
  3126. bool hasAudio = (flags & OBS_SOURCE_AUDIO) ==
  3127. OBS_SOURCE_AUDIO;
  3128. QAction *action;
  3129. popup.addAction(QTStr("Rename"), this,
  3130. SLOT(EditSceneItemName()));
  3131. popup.addAction(QTStr("Remove"), this,
  3132. SLOT(on_actionRemoveSource_triggered()),
  3133. DeleteKeys.front());
  3134. popup.addSeparator();
  3135. popup.addMenu(ui->orderMenu);
  3136. popup.addMenu(ui->transformMenu);
  3137. sourceProjector = new QMenu(QTStr("SourceProjector"));
  3138. AddProjectorMenuMonitors(sourceProjector, this,
  3139. SLOT(OpenSourceProjector()));
  3140. QAction *sourceWindow = popup.addAction(
  3141. QTStr("SourceWindow"),
  3142. this, SLOT(OpenSourceWindow()));
  3143. popup.addAction(sourceWindow);
  3144. popup.addSeparator();
  3145. if (hasAudio) {
  3146. QAction *actionHideMixer = popup.addAction(
  3147. QTStr("HideMixer"),
  3148. this, SLOT(ToggleHideMixer()));
  3149. actionHideMixer->setCheckable(true);
  3150. actionHideMixer->setChecked(SourceMixerHidden(source));
  3151. }
  3152. if (isAsyncVideo) {
  3153. popup.addMenu(AddDeinterlacingMenu(source));
  3154. popup.addSeparator();
  3155. }
  3156. popup.addMenu(AddScaleFilteringMenu(sceneItem));
  3157. popup.addSeparator();
  3158. popup.addMenu(sourceProjector);
  3159. popup.addAction(sourceWindow);
  3160. popup.addSeparator();
  3161. action = popup.addAction(QTStr("Interact"), this,
  3162. SLOT(on_actionInteract_triggered()));
  3163. action->setEnabled(obs_source_get_output_flags(source) &
  3164. OBS_SOURCE_INTERACTION);
  3165. popup.addAction(QTStr("Filters"), this,
  3166. SLOT(OpenFilters()));
  3167. popup.addAction(QTStr("Properties"), this,
  3168. SLOT(on_actionSourceProperties_triggered()));
  3169. ui->actionCopyFilters->setEnabled(true);
  3170. }
  3171. popup.exec(QCursor::pos());
  3172. }
  3173. void OBSBasic::on_sources_customContextMenuRequested(const QPoint &pos)
  3174. {
  3175. if (ui->scenes->count())
  3176. CreateSourcePopupMenu(ui->sources->itemAt(pos), false);
  3177. }
  3178. void OBSBasic::on_sources_itemDoubleClicked(QListWidgetItem *witem)
  3179. {
  3180. if (!witem)
  3181. return;
  3182. OBSSceneItem item = GetSceneItem(witem);
  3183. OBSSource source = obs_sceneitem_get_source(item);
  3184. if (source)
  3185. CreatePropertiesWindow(source);
  3186. }
  3187. void OBSBasic::on_scenes_itemDoubleClicked(QListWidgetItem *witem)
  3188. {
  3189. if (!witem)
  3190. return;
  3191. if (IsPreviewProgramMode()) {
  3192. bool doubleClickSwitch = config_get_bool(App()->GlobalConfig(),
  3193. "BasicWindow", "TransitionOnDoubleClick");
  3194. if (doubleClickSwitch) {
  3195. OBSScene scene = GetCurrentScene();
  3196. if (scene)
  3197. SetCurrentScene(scene, false, true);
  3198. }
  3199. }
  3200. }
  3201. void OBSBasic::AddSource(const char *id)
  3202. {
  3203. if (id && *id) {
  3204. OBSBasicSourceSelect sourceSelect(this, id);
  3205. sourceSelect.exec();
  3206. if (sourceSelect.newSource)
  3207. CreatePropertiesWindow(sourceSelect.newSource);
  3208. }
  3209. }
  3210. QMenu *OBSBasic::CreateAddSourcePopupMenu()
  3211. {
  3212. const char *type;
  3213. bool foundValues = false;
  3214. bool foundDeprecated = false;
  3215. size_t idx = 0;
  3216. QMenu *popup = new QMenu(QTStr("Add"), this);
  3217. QMenu *deprecated = new QMenu(QTStr("Deprecated"), popup);
  3218. auto getActionAfter = [] (QMenu *menu, const QString &name)
  3219. {
  3220. QList<QAction*> actions = menu->actions();
  3221. for (QAction *menuAction : actions) {
  3222. if (menuAction->text().compare(name) >= 0)
  3223. return menuAction;
  3224. }
  3225. return (QAction*)nullptr;
  3226. };
  3227. auto addSource = [this, getActionAfter] (QMenu *popup,
  3228. const char *type, const char *name)
  3229. {
  3230. QString qname = QT_UTF8(name);
  3231. QAction *popupItem = new QAction(qname, this);
  3232. popupItem->setData(QT_UTF8(type));
  3233. connect(popupItem, SIGNAL(triggered(bool)),
  3234. this, SLOT(AddSourceFromAction()));
  3235. QAction *after = getActionAfter(popup, qname);
  3236. popup->insertAction(after, popupItem);
  3237. };
  3238. while (obs_enum_input_types(idx++, &type)) {
  3239. const char *name = obs_source_get_display_name(type);
  3240. uint32_t caps = obs_get_source_output_flags(type);
  3241. if ((caps & OBS_SOURCE_DEPRECATED) == 0) {
  3242. addSource(popup, type, name);
  3243. } else {
  3244. addSource(deprecated, type, name);
  3245. foundDeprecated = true;
  3246. }
  3247. foundValues = true;
  3248. }
  3249. addSource(popup, "scene", Str("Basic.Scene"));
  3250. if (!foundDeprecated) {
  3251. delete deprecated;
  3252. deprecated = nullptr;
  3253. }
  3254. if (!foundValues) {
  3255. delete popup;
  3256. popup = nullptr;
  3257. } else if (foundDeprecated) {
  3258. popup->addMenu(deprecated);
  3259. }
  3260. return popup;
  3261. }
  3262. void OBSBasic::AddSourceFromAction()
  3263. {
  3264. QAction *action = qobject_cast<QAction*>(sender());
  3265. if (!action)
  3266. return;
  3267. AddSource(QT_TO_UTF8(action->data().toString()));
  3268. }
  3269. void OBSBasic::AddSourcePopupMenu(const QPoint &pos)
  3270. {
  3271. if (!GetCurrentScene()) {
  3272. // Tell the user he needs a scene first (help beginners).
  3273. OBSMessageBox::information(this,
  3274. QTStr("Basic.Main.AddSourceHelp.Title"),
  3275. QTStr("Basic.Main.AddSourceHelp.Text"));
  3276. return;
  3277. }
  3278. QPointer<QMenu> popup = CreateAddSourcePopupMenu();
  3279. if (popup)
  3280. popup->exec(pos);
  3281. }
  3282. void OBSBasic::on_actionAddSource_triggered()
  3283. {
  3284. AddSourcePopupMenu(QCursor::pos());
  3285. }
  3286. void OBSBasic::on_actionRemoveSource_triggered()
  3287. {
  3288. vector<OBSSceneItem> items;
  3289. auto func = [] (obs_scene_t *, obs_sceneitem_t *item, void *param)
  3290. {
  3291. vector<OBSSceneItem> &items =
  3292. *reinterpret_cast<vector<OBSSceneItem>*>(param);
  3293. if (obs_sceneitem_selected(item))
  3294. items.emplace_back(item);
  3295. return true;
  3296. };
  3297. obs_scene_enum_items(GetCurrentScene(), func, &items);
  3298. if (!items.size())
  3299. return;
  3300. auto removeMultiple = [this] (size_t count)
  3301. {
  3302. QString text = QTStr("ConfirmRemove.TextMultiple")
  3303. .arg(QString::number(count));
  3304. QMessageBox remove_items(this);
  3305. remove_items.setText(text);
  3306. QAbstractButton *Yes = remove_items.addButton(QTStr("Yes"),
  3307. QMessageBox::YesRole);
  3308. remove_items.addButton(QTStr("No"), QMessageBox::NoRole);
  3309. remove_items.setIcon(QMessageBox::Question);
  3310. remove_items.setWindowTitle(QTStr("ConfirmRemove.Title"));
  3311. remove_items.exec();
  3312. return Yes == remove_items.clickedButton();
  3313. };
  3314. if (items.size() == 1) {
  3315. OBSSceneItem &item = items[0];
  3316. obs_source_t *source = obs_sceneitem_get_source(item);
  3317. if (source && QueryRemoveSource(source))
  3318. obs_sceneitem_remove(item);
  3319. } else {
  3320. if (removeMultiple(items.size())) {
  3321. for (auto &item : items)
  3322. obs_sceneitem_remove(item);
  3323. }
  3324. }
  3325. }
  3326. void OBSBasic::on_actionInteract_triggered()
  3327. {
  3328. OBSSceneItem item = GetCurrentSceneItem();
  3329. OBSSource source = obs_sceneitem_get_source(item);
  3330. if (source)
  3331. CreateInteractionWindow(source);
  3332. }
  3333. void OBSBasic::on_actionSourceProperties_triggered()
  3334. {
  3335. OBSSceneItem item = GetCurrentSceneItem();
  3336. OBSSource source = obs_sceneitem_get_source(item);
  3337. if (source)
  3338. CreatePropertiesWindow(source);
  3339. }
  3340. void OBSBasic::on_actionSourceUp_triggered()
  3341. {
  3342. OBSSceneItem item = GetCurrentSceneItem();
  3343. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_UP);
  3344. }
  3345. void OBSBasic::on_actionSourceDown_triggered()
  3346. {
  3347. OBSSceneItem item = GetCurrentSceneItem();
  3348. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_DOWN);
  3349. }
  3350. void OBSBasic::on_actionMoveUp_triggered()
  3351. {
  3352. OBSSceneItem item = GetCurrentSceneItem();
  3353. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_UP);
  3354. }
  3355. void OBSBasic::on_actionMoveDown_triggered()
  3356. {
  3357. OBSSceneItem item = GetCurrentSceneItem();
  3358. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_DOWN);
  3359. }
  3360. void OBSBasic::on_actionMoveToTop_triggered()
  3361. {
  3362. OBSSceneItem item = GetCurrentSceneItem();
  3363. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_TOP);
  3364. }
  3365. void OBSBasic::on_actionMoveToBottom_triggered()
  3366. {
  3367. OBSSceneItem item = GetCurrentSceneItem();
  3368. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_BOTTOM);
  3369. }
  3370. static BPtr<char> ReadLogFile(const char *log)
  3371. {
  3372. char logDir[512];
  3373. if (GetConfigPath(logDir, sizeof(logDir), "obs-studio/logs") <= 0)
  3374. return nullptr;
  3375. string path = (char*)logDir;
  3376. path += "/";
  3377. path += log;
  3378. BPtr<char> file = os_quick_read_utf8_file(path.c_str());
  3379. if (!file)
  3380. blog(LOG_WARNING, "Failed to read log file %s", path.c_str());
  3381. return file;
  3382. }
  3383. void OBSBasic::UploadLog(const char *file)
  3384. {
  3385. BPtr<char> fileString{ReadLogFile(file)};
  3386. if (!fileString)
  3387. return;
  3388. if (!*fileString)
  3389. return;
  3390. ui->menuLogFiles->setEnabled(false);
  3391. auto data_deleter = [](obs_data_t *d) { obs_data_release(d); };
  3392. using data_t = unique_ptr<struct obs_data, decltype(data_deleter)>;
  3393. data_t content{obs_data_create(), data_deleter};
  3394. data_t files{obs_data_create(), data_deleter};
  3395. data_t request{obs_data_create(), data_deleter};
  3396. obs_data_set_string(content.get(), "content", fileString);
  3397. obs_data_set_obj(files.get(), file, content.get());
  3398. stringstream ss;
  3399. ss << "OBS " << App()->GetVersionString()
  3400. << " log file uploaded at " << CurrentDateTimeString();
  3401. obs_data_set_string(request.get(), "description", ss.str().c_str());
  3402. obs_data_set_bool(request.get(), "public", false);
  3403. obs_data_set_obj(request.get(), "files", files.get());
  3404. const char *json = obs_data_get_json(request.get());
  3405. if (!json) {
  3406. blog(LOG_ERROR, "Failed to get JSON data for log upload");
  3407. return;
  3408. }
  3409. QBuffer *postData = new QBuffer();
  3410. postData->setData(json, (int) strlen(json));
  3411. if (logUploadThread) {
  3412. logUploadThread->wait();
  3413. delete logUploadThread;
  3414. }
  3415. RemoteTextThread *thread = new RemoteTextThread(
  3416. "https://api.github.com/gists",
  3417. "application/json", json);
  3418. logUploadThread = thread;
  3419. connect(thread, &RemoteTextThread::Result,
  3420. this, &OBSBasic::logUploadFinished);
  3421. logUploadThread->start();
  3422. }
  3423. void OBSBasic::on_actionShowLogs_triggered()
  3424. {
  3425. char logDir[512];
  3426. if (GetConfigPath(logDir, sizeof(logDir), "obs-studio/logs") <= 0)
  3427. return;
  3428. QUrl url = QUrl::fromLocalFile(QT_UTF8(logDir));
  3429. QDesktopServices::openUrl(url);
  3430. }
  3431. void OBSBasic::on_actionUploadCurrentLog_triggered()
  3432. {
  3433. UploadLog(App()->GetCurrentLog());
  3434. }
  3435. void OBSBasic::on_actionUploadLastLog_triggered()
  3436. {
  3437. UploadLog(App()->GetLastLog());
  3438. }
  3439. void OBSBasic::on_actionViewCurrentLog_triggered()
  3440. {
  3441. char logDir[512];
  3442. if (GetConfigPath(logDir, sizeof(logDir), "obs-studio/logs") <= 0)
  3443. return;
  3444. const char* log = App()->GetCurrentLog();
  3445. string path = (char*)logDir;
  3446. path += "/";
  3447. path += log;
  3448. QUrl url = QUrl::fromLocalFile(QT_UTF8(path.c_str()));
  3449. QDesktopServices::openUrl(url);
  3450. }
  3451. void OBSBasic::on_actionCheckForUpdates_triggered()
  3452. {
  3453. CheckForUpdates(true);
  3454. }
  3455. void OBSBasic::logUploadFinished(const QString &text, const QString &error)
  3456. {
  3457. ui->menuLogFiles->setEnabled(true);
  3458. if (text.isEmpty()) {
  3459. OBSMessageBox::information(this,
  3460. QTStr("LogReturnDialog.ErrorUploadingLog"),
  3461. error);
  3462. return;
  3463. }
  3464. obs_data_t *returnData = obs_data_create_from_json(QT_TO_UTF8(text));
  3465. QString logURL = obs_data_get_string(returnData, "html_url");
  3466. obs_data_release(returnData);
  3467. OBSLogReply logDialog(this, logURL);
  3468. logDialog.exec();
  3469. }
  3470. static void RenameListItem(OBSBasic *parent, QListWidget *listWidget,
  3471. obs_source_t *source, const string &name)
  3472. {
  3473. const char *prevName = obs_source_get_name(source);
  3474. if (name == prevName)
  3475. return;
  3476. obs_source_t *foundSource = obs_get_source_by_name(name.c_str());
  3477. QListWidgetItem *listItem = listWidget->currentItem();
  3478. if (foundSource || name.empty()) {
  3479. listItem->setText(QT_UTF8(prevName));
  3480. if (foundSource) {
  3481. OBSMessageBox::information(parent,
  3482. QTStr("NameExists.Title"),
  3483. QTStr("NameExists.Text"));
  3484. } else if (name.empty()) {
  3485. OBSMessageBox::information(parent,
  3486. QTStr("NoNameEntered.Title"),
  3487. QTStr("NoNameEntered.Text"));
  3488. }
  3489. obs_source_release(foundSource);
  3490. } else {
  3491. listItem->setText(QT_UTF8(name.c_str()));
  3492. obs_source_set_name(source, name.c_str());
  3493. }
  3494. }
  3495. void OBSBasic::SceneNameEdited(QWidget *editor,
  3496. QAbstractItemDelegate::EndEditHint endHint)
  3497. {
  3498. OBSScene scene = GetCurrentScene();
  3499. QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
  3500. string text = QT_TO_UTF8(edit->text().trimmed());
  3501. if (!scene)
  3502. return;
  3503. obs_source_t *source = obs_scene_get_source(scene);
  3504. RenameListItem(this, ui->scenes, source, text);
  3505. if (api)
  3506. api->on_event(OBS_FRONTEND_EVENT_SCENE_LIST_CHANGED);
  3507. UNUSED_PARAMETER(endHint);
  3508. }
  3509. void OBSBasic::SceneItemNameEdited(QWidget *editor,
  3510. QAbstractItemDelegate::EndEditHint endHint)
  3511. {
  3512. OBSSceneItem item = GetCurrentSceneItem();
  3513. QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
  3514. string text = QT_TO_UTF8(edit->text().trimmed());
  3515. if (!item)
  3516. return;
  3517. obs_source_t *source = obs_sceneitem_get_source(item);
  3518. RenameListItem(this, ui->sources, source, text);
  3519. QListWidgetItem *listItem = ui->sources->currentItem();
  3520. listItem->setText(QString());
  3521. SetupVisibilityItem(ui->sources, listItem, item);
  3522. UNUSED_PARAMETER(endHint);
  3523. }
  3524. void OBSBasic::OpenFilters()
  3525. {
  3526. OBSSceneItem item = GetCurrentSceneItem();
  3527. OBSSource source = obs_sceneitem_get_source(item);
  3528. CreateFiltersWindow(source);
  3529. }
  3530. void OBSBasic::OpenSceneFilters()
  3531. {
  3532. OBSScene scene = GetCurrentScene();
  3533. OBSSource source = obs_scene_get_source(scene);
  3534. CreateFiltersWindow(source);
  3535. }
  3536. #define RECORDING_START \
  3537. "==== Recording Start ==============================================="
  3538. #define RECORDING_STOP \
  3539. "==== Recording Stop ================================================"
  3540. #define REPLAY_BUFFER_START \
  3541. "==== Replay Buffer Start ==========================================="
  3542. #define REPLAY_BUFFER_STOP \
  3543. "==== Replay Buffer Stop ============================================"
  3544. #define STREAMING_START \
  3545. "==== Streaming Start ==============================================="
  3546. #define STREAMING_STOP \
  3547. "==== Streaming Stop ================================================"
  3548. void OBSBasic::StartStreaming()
  3549. {
  3550. if (outputHandler->StreamingActive())
  3551. return;
  3552. if (disableOutputsRef)
  3553. return;
  3554. if (api)
  3555. api->on_event(OBS_FRONTEND_EVENT_STREAMING_STARTING);
  3556. SaveProject();
  3557. ui->streamButton->setEnabled(false);
  3558. ui->streamButton->setText(QTStr("Basic.Main.Connecting"));
  3559. if (sysTrayStream) {
  3560. sysTrayStream->setEnabled(false);
  3561. sysTrayStream->setText(ui->streamButton->text());
  3562. }
  3563. if (!outputHandler->StartStreaming(service)) {
  3564. ui->streamButton->setText(QTStr("Basic.Main.StartStreaming"));
  3565. ui->streamButton->setEnabled(true);
  3566. ui->streamButton->setChecked(false);
  3567. if (sysTrayStream) {
  3568. sysTrayStream->setText(ui->streamButton->text());
  3569. sysTrayStream->setEnabled(true);
  3570. }
  3571. QMessageBox::critical(this,
  3572. QTStr("Output.StartStreamFailed"),
  3573. QTStr("Output.StartFailedGeneric"));
  3574. return;
  3575. }
  3576. bool recordWhenStreaming = config_get_bool(GetGlobalConfig(),
  3577. "BasicWindow", "RecordWhenStreaming");
  3578. if (recordWhenStreaming)
  3579. StartRecording();
  3580. bool replayBufferWhileStreaming = config_get_bool(GetGlobalConfig(),
  3581. "BasicWindow", "ReplayBufferWhileStreaming");
  3582. if (replayBufferWhileStreaming)
  3583. StartReplayBuffer();
  3584. }
  3585. #ifdef _WIN32
  3586. static inline void UpdateProcessPriority()
  3587. {
  3588. const char *priority = config_get_string(App()->GlobalConfig(),
  3589. "General", "ProcessPriority");
  3590. if (priority && strcmp(priority, "Normal") != 0)
  3591. SetProcessPriority(priority);
  3592. }
  3593. static inline void ClearProcessPriority()
  3594. {
  3595. const char *priority = config_get_string(App()->GlobalConfig(),
  3596. "General", "ProcessPriority");
  3597. if (priority && strcmp(priority, "Normal") != 0)
  3598. SetProcessPriority("Normal");
  3599. }
  3600. #else
  3601. #define UpdateProcessPriority() do {} while(false)
  3602. #define ClearProcessPriority() do {} while(false)
  3603. #endif
  3604. inline void OBSBasic::OnActivate()
  3605. {
  3606. if (ui->profileMenu->isEnabled()) {
  3607. ui->profileMenu->setEnabled(false);
  3608. ui->autoConfigure->setEnabled(false);
  3609. App()->IncrementSleepInhibition();
  3610. UpdateProcessPriority();
  3611. if (trayIcon)
  3612. trayIcon->setIcon(QIcon(":/res/images/tray_active.png"));
  3613. }
  3614. }
  3615. inline void OBSBasic::OnDeactivate()
  3616. {
  3617. if (!outputHandler->Active() && !ui->profileMenu->isEnabled()) {
  3618. ui->profileMenu->setEnabled(true);
  3619. ui->autoConfigure->setEnabled(true);
  3620. App()->DecrementSleepInhibition();
  3621. ClearProcessPriority();
  3622. if (trayIcon)
  3623. trayIcon->setIcon(QIcon(":/res/images/obs.png"));
  3624. }
  3625. }
  3626. void OBSBasic::StopStreaming()
  3627. {
  3628. SaveProject();
  3629. if (outputHandler->StreamingActive())
  3630. outputHandler->StopStreaming(streamingStopping);
  3631. OnDeactivate();
  3632. bool recordWhenStreaming = config_get_bool(GetGlobalConfig(),
  3633. "BasicWindow", "RecordWhenStreaming");
  3634. bool keepRecordingWhenStreamStops = config_get_bool(GetGlobalConfig(),
  3635. "BasicWindow", "KeepRecordingWhenStreamStops");
  3636. if (recordWhenStreaming && !keepRecordingWhenStreamStops)
  3637. StopRecording();
  3638. bool replayBufferWhileStreaming = config_get_bool(GetGlobalConfig(),
  3639. "BasicWindow", "ReplayBufferWhileStreaming");
  3640. bool keepReplayBufferStreamStops = config_get_bool(GetGlobalConfig(),
  3641. "BasicWindow", "KeepReplayBufferStreamStops");
  3642. if (replayBufferWhileStreaming && !keepReplayBufferStreamStops)
  3643. StopReplayBuffer();
  3644. }
  3645. void OBSBasic::ForceStopStreaming()
  3646. {
  3647. SaveProject();
  3648. if (outputHandler->StreamingActive())
  3649. outputHandler->StopStreaming(true);
  3650. OnDeactivate();
  3651. bool recordWhenStreaming = config_get_bool(GetGlobalConfig(),
  3652. "BasicWindow", "RecordWhenStreaming");
  3653. bool keepRecordingWhenStreamStops = config_get_bool(GetGlobalConfig(),
  3654. "BasicWindow", "KeepRecordingWhenStreamStops");
  3655. if (recordWhenStreaming && !keepRecordingWhenStreamStops)
  3656. StopRecording();
  3657. bool replayBufferWhileStreaming = config_get_bool(GetGlobalConfig(),
  3658. "BasicWindow", "ReplayBufferWhileStreaming");
  3659. bool keepReplayBufferStreamStops = config_get_bool(GetGlobalConfig(),
  3660. "BasicWindow", "KeepReplayBufferStreamStops");
  3661. if (replayBufferWhileStreaming && !keepReplayBufferStreamStops)
  3662. StopReplayBuffer();
  3663. }
  3664. void OBSBasic::StreamDelayStarting(int sec)
  3665. {
  3666. ui->streamButton->setText(QTStr("Basic.Main.StopStreaming"));
  3667. ui->streamButton->setEnabled(true);
  3668. ui->streamButton->setChecked(true);
  3669. if (sysTrayStream) {
  3670. sysTrayStream->setText(ui->streamButton->text());
  3671. sysTrayStream->setEnabled(true);
  3672. }
  3673. if (!startStreamMenu.isNull())
  3674. startStreamMenu->deleteLater();
  3675. startStreamMenu = new QMenu();
  3676. startStreamMenu->addAction(QTStr("Basic.Main.StopStreaming"),
  3677. this, SLOT(StopStreaming()));
  3678. startStreamMenu->addAction(QTStr("Basic.Main.ForceStopStreaming"),
  3679. this, SLOT(ForceStopStreaming()));
  3680. ui->streamButton->setMenu(startStreamMenu);
  3681. ui->statusbar->StreamDelayStarting(sec);
  3682. OnActivate();
  3683. }
  3684. void OBSBasic::StreamDelayStopping(int sec)
  3685. {
  3686. ui->streamButton->setText(QTStr("Basic.Main.StartStreaming"));
  3687. ui->streamButton->setEnabled(true);
  3688. ui->streamButton->setChecked(false);
  3689. if (sysTrayStream) {
  3690. sysTrayStream->setText(ui->streamButton->text());
  3691. sysTrayStream->setEnabled(true);
  3692. }
  3693. if (!startStreamMenu.isNull())
  3694. startStreamMenu->deleteLater();
  3695. startStreamMenu = new QMenu();
  3696. startStreamMenu->addAction(QTStr("Basic.Main.StartStreaming"),
  3697. this, SLOT(StartStreaming()));
  3698. startStreamMenu->addAction(QTStr("Basic.Main.ForceStopStreaming"),
  3699. this, SLOT(ForceStopStreaming()));
  3700. ui->streamButton->setMenu(startStreamMenu);
  3701. ui->statusbar->StreamDelayStopping(sec);
  3702. }
  3703. void OBSBasic::StreamingStart()
  3704. {
  3705. ui->streamButton->setText(QTStr("Basic.Main.StopStreaming"));
  3706. ui->streamButton->setEnabled(true);
  3707. ui->streamButton->setChecked(true);
  3708. ui->statusbar->StreamStarted(outputHandler->streamOutput);
  3709. if (sysTrayStream) {
  3710. sysTrayStream->setText(ui->streamButton->text());
  3711. sysTrayStream->setEnabled(true);
  3712. }
  3713. if (api)
  3714. api->on_event(OBS_FRONTEND_EVENT_STREAMING_STARTED);
  3715. OnActivate();
  3716. blog(LOG_INFO, STREAMING_START);
  3717. }
  3718. void OBSBasic::StreamStopping()
  3719. {
  3720. ui->streamButton->setText(QTStr("Basic.Main.StoppingStreaming"));
  3721. if (sysTrayStream)
  3722. sysTrayStream->setText(ui->streamButton->text());
  3723. streamingStopping = true;
  3724. if (api)
  3725. api->on_event(OBS_FRONTEND_EVENT_STREAMING_STOPPING);
  3726. }
  3727. void OBSBasic::StreamingStop(int code, QString last_error)
  3728. {
  3729. const char *errorDescription;
  3730. DStr errorMessage;
  3731. bool use_last_error = false;
  3732. switch (code) {
  3733. case OBS_OUTPUT_BAD_PATH:
  3734. errorDescription = Str("Output.ConnectFail.BadPath");
  3735. break;
  3736. case OBS_OUTPUT_CONNECT_FAILED:
  3737. use_last_error = true;
  3738. errorDescription = Str("Output.ConnectFail.ConnectFailed");
  3739. break;
  3740. case OBS_OUTPUT_INVALID_STREAM:
  3741. errorDescription = Str("Output.ConnectFail.InvalidStream");
  3742. break;
  3743. default:
  3744. case OBS_OUTPUT_ERROR:
  3745. use_last_error = true;
  3746. errorDescription = Str("Output.ConnectFail.Error");
  3747. break;
  3748. case OBS_OUTPUT_DISCONNECTED:
  3749. /* doesn't happen if output is set to reconnect. note that
  3750. * reconnects are handled in the output, not in the UI */
  3751. use_last_error = true;
  3752. errorDescription = Str("Output.ConnectFail.Disconnected");
  3753. }
  3754. if (use_last_error && !last_error.isEmpty())
  3755. dstr_printf(errorMessage, "%s\n\n%s", errorDescription,
  3756. QT_TO_UTF8(last_error));
  3757. else
  3758. dstr_copy(errorMessage, errorDescription);
  3759. ui->statusbar->StreamStopped();
  3760. ui->streamButton->setText(QTStr("Basic.Main.StartStreaming"));
  3761. ui->streamButton->setEnabled(true);
  3762. ui->streamButton->setChecked(false);
  3763. if (sysTrayStream) {
  3764. sysTrayStream->setText(ui->streamButton->text());
  3765. sysTrayStream->setEnabled(true);
  3766. }
  3767. streamingStopping = false;
  3768. if (api)
  3769. api->on_event(OBS_FRONTEND_EVENT_STREAMING_STOPPED);
  3770. OnDeactivate();
  3771. blog(LOG_INFO, STREAMING_STOP);
  3772. if (code != OBS_OUTPUT_SUCCESS && isVisible()) {
  3773. OBSMessageBox::information(this,
  3774. QTStr("Output.ConnectFail.Title"),
  3775. QT_UTF8(errorMessage));
  3776. } else if (code != OBS_OUTPUT_SUCCESS && !isVisible()) {
  3777. SysTrayNotify(QT_UTF8(errorDescription), QSystemTrayIcon::Warning);
  3778. }
  3779. if (!startStreamMenu.isNull()) {
  3780. ui->streamButton->setMenu(nullptr);
  3781. startStreamMenu->deleteLater();
  3782. startStreamMenu = nullptr;
  3783. }
  3784. }
  3785. void OBSBasic::StartRecording()
  3786. {
  3787. if (outputHandler->RecordingActive())
  3788. return;
  3789. if (disableOutputsRef)
  3790. return;
  3791. if (api)
  3792. api->on_event(OBS_FRONTEND_EVENT_RECORDING_STARTING);
  3793. SaveProject();
  3794. outputHandler->StartRecording();
  3795. }
  3796. void OBSBasic::RecordStopping()
  3797. {
  3798. ui->recordButton->setText(QTStr("Basic.Main.StoppingRecording"));
  3799. if (sysTrayRecord)
  3800. sysTrayRecord->setText(ui->recordButton->text());
  3801. recordingStopping = true;
  3802. if (api)
  3803. api->on_event(OBS_FRONTEND_EVENT_RECORDING_STOPPING);
  3804. }
  3805. void OBSBasic::StopRecording()
  3806. {
  3807. SaveProject();
  3808. if (outputHandler->RecordingActive())
  3809. outputHandler->StopRecording(recordingStopping);
  3810. OnDeactivate();
  3811. }
  3812. void OBSBasic::RecordingStart()
  3813. {
  3814. ui->statusbar->RecordingStarted(outputHandler->fileOutput);
  3815. ui->recordButton->setText(QTStr("Basic.Main.StopRecording"));
  3816. ui->recordButton->setChecked(true);
  3817. if (sysTrayRecord)
  3818. sysTrayRecord->setText(ui->recordButton->text());
  3819. recordingStopping = false;
  3820. if (api)
  3821. api->on_event(OBS_FRONTEND_EVENT_RECORDING_STARTED);
  3822. OnActivate();
  3823. blog(LOG_INFO, RECORDING_START);
  3824. }
  3825. void OBSBasic::RecordingStop(int code)
  3826. {
  3827. ui->statusbar->RecordingStopped();
  3828. ui->recordButton->setText(QTStr("Basic.Main.StartRecording"));
  3829. ui->recordButton->setChecked(false);
  3830. if (sysTrayRecord)
  3831. sysTrayRecord->setText(ui->recordButton->text());
  3832. blog(LOG_INFO, RECORDING_STOP);
  3833. if (code == OBS_OUTPUT_UNSUPPORTED && isVisible()) {
  3834. OBSMessageBox::information(this,
  3835. QTStr("Output.RecordFail.Title"),
  3836. QTStr("Output.RecordFail.Unsupported"));
  3837. } else if (code == OBS_OUTPUT_NO_SPACE && isVisible()) {
  3838. OBSMessageBox::information(this,
  3839. QTStr("Output.RecordNoSpace.Title"),
  3840. QTStr("Output.RecordNoSpace.Msg"));
  3841. } else if (code != OBS_OUTPUT_SUCCESS && isVisible()) {
  3842. OBSMessageBox::information(this,
  3843. QTStr("Output.RecordError.Title"),
  3844. QTStr("Output.RecordError.Msg"));
  3845. } else if (code == OBS_OUTPUT_UNSUPPORTED && !isVisible()) {
  3846. SysTrayNotify(QTStr("Output.RecordFail.Unsupported"),
  3847. QSystemTrayIcon::Warning);
  3848. } else if (code == OBS_OUTPUT_NO_SPACE && !isVisible()) {
  3849. SysTrayNotify(QTStr("Output.RecordNoSpace.Msg"),
  3850. QSystemTrayIcon::Warning);
  3851. } else if (code != OBS_OUTPUT_SUCCESS && !isVisible()) {
  3852. SysTrayNotify(QTStr("Output.RecordError.Msg"),
  3853. QSystemTrayIcon::Warning);
  3854. }
  3855. if (api)
  3856. api->on_event(OBS_FRONTEND_EVENT_RECORDING_STOPPED);
  3857. OnDeactivate();
  3858. }
  3859. #define RP_NO_HOTKEY_TITLE QTStr("Output.ReplayBuffer.NoHotkey.Title")
  3860. #define RP_NO_HOTKEY_TEXT QTStr("Output.ReplayBuffer.NoHotkey.Msg")
  3861. void OBSBasic::StartReplayBuffer()
  3862. {
  3863. if (!outputHandler || !outputHandler->replayBuffer)
  3864. return;
  3865. if (outputHandler->ReplayBufferActive())
  3866. return;
  3867. if (disableOutputsRef)
  3868. return;
  3869. obs_output_t *output = outputHandler->replayBuffer;
  3870. obs_data_t *hotkeys = obs_hotkeys_save_output(output);
  3871. obs_data_array_t *bindings = obs_data_get_array(hotkeys,
  3872. "ReplayBuffer.Save");
  3873. size_t count = obs_data_array_count(bindings);
  3874. obs_data_array_release(bindings);
  3875. obs_data_release(hotkeys);
  3876. if (!count) {
  3877. OBSMessageBox::information(this,
  3878. RP_NO_HOTKEY_TITLE,
  3879. RP_NO_HOTKEY_TEXT);
  3880. return;
  3881. }
  3882. if (api)
  3883. api->on_event(OBS_FRONTEND_EVENT_REPLAY_BUFFER_STARTING);
  3884. SaveProject();
  3885. outputHandler->StartReplayBuffer();
  3886. }
  3887. void OBSBasic::ReplayBufferStopping()
  3888. {
  3889. if (!outputHandler || !outputHandler->replayBuffer)
  3890. return;
  3891. replayBufferButton->setText(QTStr("Basic.Main.StoppingReplayBuffer"));
  3892. if (sysTrayReplayBuffer)
  3893. sysTrayReplayBuffer->setText(replayBufferButton->text());
  3894. replayBufferStopping = true;
  3895. if (api)
  3896. api->on_event(OBS_FRONTEND_EVENT_REPLAY_BUFFER_STOPPING);
  3897. }
  3898. void OBSBasic::StopReplayBuffer()
  3899. {
  3900. if (!outputHandler || !outputHandler->replayBuffer)
  3901. return;
  3902. SaveProject();
  3903. if (outputHandler->ReplayBufferActive())
  3904. outputHandler->StopReplayBuffer(replayBufferStopping);
  3905. OnDeactivate();
  3906. }
  3907. void OBSBasic::ReplayBufferStart()
  3908. {
  3909. if (!outputHandler || !outputHandler->replayBuffer)
  3910. return;
  3911. replayBufferButton->setText(QTStr("Basic.Main.StopReplayBuffer"));
  3912. if (sysTrayReplayBuffer)
  3913. sysTrayReplayBuffer->setText(replayBufferButton->text());
  3914. replayBufferStopping = false;
  3915. if (api)
  3916. api->on_event(OBS_FRONTEND_EVENT_REPLAY_BUFFER_STARTED);
  3917. OnActivate();
  3918. blog(LOG_INFO, REPLAY_BUFFER_START);
  3919. }
  3920. void OBSBasic::ReplayBufferSave()
  3921. {
  3922. if (!outputHandler || !outputHandler->replayBuffer)
  3923. return;
  3924. if (!outputHandler->ReplayBufferActive())
  3925. return;
  3926. calldata_t cd = {0};
  3927. proc_handler_t *ph = obs_output_get_proc_handler(
  3928. outputHandler->replayBuffer);
  3929. proc_handler_call(ph, "save", &cd);
  3930. calldata_free(&cd);
  3931. }
  3932. void OBSBasic::ReplayBufferStop(int code)
  3933. {
  3934. if (!outputHandler || !outputHandler->replayBuffer)
  3935. return;
  3936. replayBufferButton->setText(QTStr("Basic.Main.StartReplayBuffer"));
  3937. if (sysTrayReplayBuffer)
  3938. sysTrayReplayBuffer->setText(replayBufferButton->text());
  3939. blog(LOG_INFO, REPLAY_BUFFER_STOP);
  3940. if (code == OBS_OUTPUT_UNSUPPORTED && isVisible()) {
  3941. OBSMessageBox::information(this,
  3942. QTStr("Output.RecordFail.Title"),
  3943. QTStr("Output.RecordFail.Unsupported"));
  3944. } else if (code == OBS_OUTPUT_NO_SPACE && isVisible()) {
  3945. OBSMessageBox::information(this,
  3946. QTStr("Output.RecordNoSpace.Title"),
  3947. QTStr("Output.RecordNoSpace.Msg"));
  3948. } else if (code != OBS_OUTPUT_SUCCESS && isVisible()) {
  3949. OBSMessageBox::information(this,
  3950. QTStr("Output.RecordError.Title"),
  3951. QTStr("Output.RecordError.Msg"));
  3952. } else if (code == OBS_OUTPUT_UNSUPPORTED && !isVisible()) {
  3953. SysTrayNotify(QTStr("Output.RecordFail.Unsupported"),
  3954. QSystemTrayIcon::Warning);
  3955. } else if (code == OBS_OUTPUT_NO_SPACE && !isVisible()) {
  3956. SysTrayNotify(QTStr("Output.RecordNoSpace.Msg"),
  3957. QSystemTrayIcon::Warning);
  3958. } else if (code != OBS_OUTPUT_SUCCESS && !isVisible()) {
  3959. SysTrayNotify(QTStr("Output.RecordError.Msg"),
  3960. QSystemTrayIcon::Warning);
  3961. }
  3962. if (api)
  3963. api->on_event(OBS_FRONTEND_EVENT_REPLAY_BUFFER_STOPPED);
  3964. OnDeactivate();
  3965. }
  3966. void OBSBasic::on_streamButton_clicked()
  3967. {
  3968. if (outputHandler->StreamingActive()) {
  3969. bool confirm = config_get_bool(GetGlobalConfig(), "BasicWindow",
  3970. "WarnBeforeStoppingStream");
  3971. if (confirm && isVisible()) {
  3972. QMessageBox::StandardButton button =
  3973. OBSMessageBox::question(this,
  3974. QTStr("ConfirmStop.Title"),
  3975. QTStr("ConfirmStop.Text"));
  3976. if (button == QMessageBox::No)
  3977. return;
  3978. }
  3979. StopStreaming();
  3980. } else {
  3981. bool confirm = config_get_bool(GetGlobalConfig(), "BasicWindow",
  3982. "WarnBeforeStartingStream");
  3983. if (confirm && isVisible()) {
  3984. QMessageBox::StandardButton button =
  3985. OBSMessageBox::question(this,
  3986. QTStr("ConfirmStart.Title"),
  3987. QTStr("ConfirmStart.Text"));
  3988. if (button == QMessageBox::No)
  3989. return;
  3990. }
  3991. StartStreaming();
  3992. }
  3993. }
  3994. void OBSBasic::on_recordButton_clicked()
  3995. {
  3996. if (outputHandler->RecordingActive())
  3997. StopRecording();
  3998. else
  3999. StartRecording();
  4000. }
  4001. void OBSBasic::on_settingsButton_clicked()
  4002. {
  4003. on_action_Settings_triggered();
  4004. }
  4005. void OBSBasic::on_actionWebsite_triggered()
  4006. {
  4007. QUrl url = QUrl("https://obsproject.com", QUrl::TolerantMode);
  4008. QDesktopServices::openUrl(url);
  4009. }
  4010. void OBSBasic::on_actionShowSettingsFolder_triggered()
  4011. {
  4012. char path[512];
  4013. int ret = GetConfigPath(path, 512, "obs-studio");
  4014. if (ret <= 0)
  4015. return;
  4016. QDesktopServices::openUrl(QUrl::fromLocalFile(path));
  4017. }
  4018. void OBSBasic::on_actionShowProfileFolder_triggered()
  4019. {
  4020. char path[512];
  4021. int ret = GetProfilePath(path, 512, "");
  4022. if (ret <= 0)
  4023. return;
  4024. QDesktopServices::openUrl(QUrl::fromLocalFile(path));
  4025. }
  4026. QListWidgetItem *OBSBasic::GetTopSelectedSourceItem()
  4027. {
  4028. QList<QListWidgetItem*> selectedItems = ui->sources->selectedItems();
  4029. QListWidgetItem *topItem = nullptr;
  4030. if (selectedItems.size() != 0)
  4031. topItem = selectedItems[0];
  4032. return topItem;
  4033. }
  4034. void OBSBasic::on_preview_customContextMenuRequested(const QPoint &pos)
  4035. {
  4036. CreateSourcePopupMenu(GetTopSelectedSourceItem(), true);
  4037. UNUSED_PARAMETER(pos);
  4038. }
  4039. void OBSBasic::on_program_customContextMenuRequested(const QPoint&)
  4040. {
  4041. QMenu popup(this);
  4042. QPointer<QMenu> studioProgramProjector;
  4043. studioProgramProjector = new QMenu(
  4044. QTStr("StudioProgramProjector"));
  4045. AddProjectorMenuMonitors(studioProgramProjector, this,
  4046. SLOT(OpenStudioProgramProjector()));
  4047. popup.addMenu(studioProgramProjector);
  4048. QAction *studioProgramWindow = popup.addAction(
  4049. QTStr("StudioProgramWindow"),
  4050. this, SLOT(OpenStudioProgramWindow()));
  4051. popup.addAction(studioProgramWindow);
  4052. popup.exec(QCursor::pos());
  4053. }
  4054. void OBSBasic::on_previewDisabledLabel_customContextMenuRequested(
  4055. const QPoint &pos)
  4056. {
  4057. QMenu popup(this);
  4058. QPointer<QMenu> previewProjector;
  4059. QAction *action = popup.addAction(
  4060. QTStr("Basic.Main.PreviewConextMenu.Enable"),
  4061. this, SLOT(TogglePreview()));
  4062. action->setCheckable(true);
  4063. action->setChecked(obs_display_enabled(ui->preview->GetDisplay()));
  4064. previewProjector = new QMenu(QTStr("PreviewProjector"));
  4065. AddProjectorMenuMonitors(previewProjector, this,
  4066. SLOT(OpenPreviewProjector()));
  4067. QAction *previewWindow = popup.addAction(
  4068. QTStr("PreviewWindow"),
  4069. this, SLOT(OpenPreviewWindow()));
  4070. popup.addMenu(previewProjector);
  4071. popup.addAction(previewWindow);
  4072. popup.exec(QCursor::pos());
  4073. UNUSED_PARAMETER(pos);
  4074. }
  4075. void OBSBasic::on_actionAlwaysOnTop_triggered()
  4076. {
  4077. CloseDialogs();
  4078. /* Make sure all dialogs are safely and successfully closed before
  4079. * switching the always on top mode due to the fact that windows all
  4080. * have to be recreated, so queue the actual toggle to happen after
  4081. * all events related to closing the dialogs have finished */
  4082. QMetaObject::invokeMethod(this, "ToggleAlwaysOnTop",
  4083. Qt::QueuedConnection);
  4084. }
  4085. void OBSBasic::ToggleAlwaysOnTop()
  4086. {
  4087. bool isAlwaysOnTop = IsAlwaysOnTop(this);
  4088. ui->actionAlwaysOnTop->setChecked(!isAlwaysOnTop);
  4089. SetAlwaysOnTop(this, !isAlwaysOnTop);
  4090. show();
  4091. }
  4092. void OBSBasic::GetFPSCommon(uint32_t &num, uint32_t &den) const
  4093. {
  4094. const char *val = config_get_string(basicConfig, "Video", "FPSCommon");
  4095. if (strcmp(val, "10") == 0) {
  4096. num = 10;
  4097. den = 1;
  4098. } else if (strcmp(val, "20") == 0) {
  4099. num = 20;
  4100. den = 1;
  4101. } else if (strcmp(val, "24 NTSC") == 0) {
  4102. num = 24000;
  4103. den = 1001;
  4104. } else if (strcmp(val, "25") == 0) {
  4105. num = 25;
  4106. den = 1;
  4107. } else if (strcmp(val, "29.97") == 0) {
  4108. num = 30000;
  4109. den = 1001;
  4110. } else if (strcmp(val, "48") == 0) {
  4111. num = 48;
  4112. den = 1;
  4113. } else if (strcmp(val, "59.94") == 0) {
  4114. num = 60000;
  4115. den = 1001;
  4116. } else if (strcmp(val, "60") == 0) {
  4117. num = 60;
  4118. den = 1;
  4119. } else {
  4120. num = 30;
  4121. den = 1;
  4122. }
  4123. }
  4124. void OBSBasic::GetFPSInteger(uint32_t &num, uint32_t &den) const
  4125. {
  4126. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSInt");
  4127. den = 1;
  4128. }
  4129. void OBSBasic::GetFPSFraction(uint32_t &num, uint32_t &den) const
  4130. {
  4131. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNum");
  4132. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSDen");
  4133. }
  4134. void OBSBasic::GetFPSNanoseconds(uint32_t &num, uint32_t &den) const
  4135. {
  4136. num = 1000000000;
  4137. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNS");
  4138. }
  4139. void OBSBasic::GetConfigFPS(uint32_t &num, uint32_t &den) const
  4140. {
  4141. uint32_t type = config_get_uint(basicConfig, "Video", "FPSType");
  4142. if (type == 1) //"Integer"
  4143. GetFPSInteger(num, den);
  4144. else if (type == 2) //"Fraction"
  4145. GetFPSFraction(num, den);
  4146. else if (false) //"Nanoseconds", currently not implemented
  4147. GetFPSNanoseconds(num, den);
  4148. else
  4149. GetFPSCommon(num, den);
  4150. }
  4151. config_t *OBSBasic::Config() const
  4152. {
  4153. return basicConfig;
  4154. }
  4155. void OBSBasic::on_actionEditTransform_triggered()
  4156. {
  4157. if (transformWindow)
  4158. transformWindow->close();
  4159. transformWindow = new OBSBasicTransform(this);
  4160. transformWindow->show();
  4161. transformWindow->setAttribute(Qt::WA_DeleteOnClose, true);
  4162. }
  4163. static obs_transform_info copiedTransformInfo;
  4164. static obs_sceneitem_crop copiedCropInfo;
  4165. void OBSBasic::on_actionCopyTransform_triggered()
  4166. {
  4167. auto func = [](obs_scene_t *scene, obs_sceneitem_t *item, void *param)
  4168. {
  4169. if (!obs_sceneitem_selected(item))
  4170. return true;
  4171. obs_sceneitem_defer_update_begin(item);
  4172. obs_sceneitem_get_info(item, &copiedTransformInfo);
  4173. obs_sceneitem_get_crop(item, &copiedCropInfo);
  4174. obs_sceneitem_defer_update_end(item);
  4175. UNUSED_PARAMETER(scene);
  4176. UNUSED_PARAMETER(param);
  4177. return true;
  4178. };
  4179. obs_scene_enum_items(GetCurrentScene(), func, nullptr);
  4180. ui->actionPasteTransform->setEnabled(true);
  4181. }
  4182. void OBSBasic::on_actionPasteTransform_triggered()
  4183. {
  4184. auto func = [](obs_scene_t *scene, obs_sceneitem_t *item, void *param)
  4185. {
  4186. if (!obs_sceneitem_selected(item))
  4187. return true;
  4188. obs_sceneitem_defer_update_begin(item);
  4189. obs_sceneitem_set_info(item, &copiedTransformInfo);
  4190. obs_sceneitem_set_crop(item, &copiedCropInfo);
  4191. obs_sceneitem_defer_update_end(item);
  4192. UNUSED_PARAMETER(scene);
  4193. UNUSED_PARAMETER(param);
  4194. return true;
  4195. };
  4196. obs_scene_enum_items(GetCurrentScene(), func, nullptr);
  4197. }
  4198. void OBSBasic::on_actionResetTransform_triggered()
  4199. {
  4200. auto func = [] (obs_scene_t *scene, obs_sceneitem_t *item, void *param)
  4201. {
  4202. if (!obs_sceneitem_selected(item))
  4203. return true;
  4204. obs_sceneitem_defer_update_begin(item);
  4205. obs_transform_info info;
  4206. vec2_set(&info.pos, 0.0f, 0.0f);
  4207. vec2_set(&info.scale, 1.0f, 1.0f);
  4208. info.rot = 0.0f;
  4209. info.alignment = OBS_ALIGN_TOP | OBS_ALIGN_LEFT;
  4210. info.bounds_type = OBS_BOUNDS_NONE;
  4211. info.bounds_alignment = OBS_ALIGN_CENTER;
  4212. vec2_set(&info.bounds, 0.0f, 0.0f);
  4213. obs_sceneitem_set_info(item, &info);
  4214. obs_sceneitem_crop crop = {};
  4215. obs_sceneitem_set_crop(item, &crop);
  4216. obs_sceneitem_defer_update_end(item);
  4217. UNUSED_PARAMETER(scene);
  4218. UNUSED_PARAMETER(param);
  4219. return true;
  4220. };
  4221. obs_scene_enum_items(GetCurrentScene(), func, nullptr);
  4222. }
  4223. static void GetItemBox(obs_sceneitem_t *item, vec3 &tl, vec3 &br)
  4224. {
  4225. matrix4 boxTransform;
  4226. obs_sceneitem_get_box_transform(item, &boxTransform);
  4227. vec3_set(&tl, M_INFINITE, M_INFINITE, 0.0f);
  4228. vec3_set(&br, -M_INFINITE, -M_INFINITE, 0.0f);
  4229. auto GetMinPos = [&] (float x, float y)
  4230. {
  4231. vec3 pos;
  4232. vec3_set(&pos, x, y, 0.0f);
  4233. vec3_transform(&pos, &pos, &boxTransform);
  4234. vec3_min(&tl, &tl, &pos);
  4235. vec3_max(&br, &br, &pos);
  4236. };
  4237. GetMinPos(0.0f, 0.0f);
  4238. GetMinPos(1.0f, 0.0f);
  4239. GetMinPos(0.0f, 1.0f);
  4240. GetMinPos(1.0f, 1.0f);
  4241. }
  4242. static vec3 GetItemTL(obs_sceneitem_t *item)
  4243. {
  4244. vec3 tl, br;
  4245. GetItemBox(item, tl, br);
  4246. return tl;
  4247. }
  4248. static void SetItemTL(obs_sceneitem_t *item, const vec3 &tl)
  4249. {
  4250. vec3 newTL;
  4251. vec2 pos;
  4252. obs_sceneitem_get_pos(item, &pos);
  4253. newTL = GetItemTL(item);
  4254. pos.x += tl.x - newTL.x;
  4255. pos.y += tl.y - newTL.y;
  4256. obs_sceneitem_set_pos(item, &pos);
  4257. }
  4258. static bool RotateSelectedSources(obs_scene_t *scene, obs_sceneitem_t *item,
  4259. void *param)
  4260. {
  4261. if (!obs_sceneitem_selected(item))
  4262. return true;
  4263. float rot = *reinterpret_cast<float*>(param);
  4264. vec3 tl = GetItemTL(item);
  4265. rot += obs_sceneitem_get_rot(item);
  4266. if (rot >= 360.0f) rot -= 360.0f;
  4267. else if (rot <= -360.0f) rot += 360.0f;
  4268. obs_sceneitem_set_rot(item, rot);
  4269. SetItemTL(item, tl);
  4270. UNUSED_PARAMETER(scene);
  4271. UNUSED_PARAMETER(param);
  4272. return true;
  4273. };
  4274. void OBSBasic::on_actionRotate90CW_triggered()
  4275. {
  4276. float f90CW = 90.0f;
  4277. obs_scene_enum_items(GetCurrentScene(), RotateSelectedSources, &f90CW);
  4278. }
  4279. void OBSBasic::on_actionRotate90CCW_triggered()
  4280. {
  4281. float f90CCW = -90.0f;
  4282. obs_scene_enum_items(GetCurrentScene(), RotateSelectedSources, &f90CCW);
  4283. }
  4284. void OBSBasic::on_actionRotate180_triggered()
  4285. {
  4286. float f180 = 180.0f;
  4287. obs_scene_enum_items(GetCurrentScene(), RotateSelectedSources, &f180);
  4288. }
  4289. static bool MultiplySelectedItemScale(obs_scene_t *scene, obs_sceneitem_t *item,
  4290. void *param)
  4291. {
  4292. vec2 &mul = *reinterpret_cast<vec2*>(param);
  4293. if (!obs_sceneitem_selected(item))
  4294. return true;
  4295. vec3 tl = GetItemTL(item);
  4296. vec2 scale;
  4297. obs_sceneitem_get_scale(item, &scale);
  4298. vec2_mul(&scale, &scale, &mul);
  4299. obs_sceneitem_set_scale(item, &scale);
  4300. SetItemTL(item, tl);
  4301. UNUSED_PARAMETER(scene);
  4302. return true;
  4303. }
  4304. void OBSBasic::on_actionFlipHorizontal_triggered()
  4305. {
  4306. vec2 scale;
  4307. vec2_set(&scale, -1.0f, 1.0f);
  4308. obs_scene_enum_items(GetCurrentScene(), MultiplySelectedItemScale,
  4309. &scale);
  4310. }
  4311. void OBSBasic::on_actionFlipVertical_triggered()
  4312. {
  4313. vec2 scale;
  4314. vec2_set(&scale, 1.0f, -1.0f);
  4315. obs_scene_enum_items(GetCurrentScene(), MultiplySelectedItemScale,
  4316. &scale);
  4317. }
  4318. static bool CenterAlignSelectedItems(obs_scene_t *scene, obs_sceneitem_t *item,
  4319. void *param)
  4320. {
  4321. obs_bounds_type boundsType = *reinterpret_cast<obs_bounds_type*>(param);
  4322. if (!obs_sceneitem_selected(item))
  4323. return true;
  4324. obs_video_info ovi;
  4325. obs_get_video_info(&ovi);
  4326. obs_transform_info itemInfo;
  4327. vec2_set(&itemInfo.pos, 0.0f, 0.0f);
  4328. vec2_set(&itemInfo.scale, 1.0f, 1.0f);
  4329. itemInfo.alignment = OBS_ALIGN_LEFT | OBS_ALIGN_TOP;
  4330. itemInfo.rot = 0.0f;
  4331. vec2_set(&itemInfo.bounds,
  4332. float(ovi.base_width), float(ovi.base_height));
  4333. itemInfo.bounds_type = boundsType;
  4334. itemInfo.bounds_alignment = OBS_ALIGN_CENTER;
  4335. obs_sceneitem_set_info(item, &itemInfo);
  4336. UNUSED_PARAMETER(scene);
  4337. return true;
  4338. }
  4339. void OBSBasic::on_actionFitToScreen_triggered()
  4340. {
  4341. obs_bounds_type boundsType = OBS_BOUNDS_SCALE_INNER;
  4342. obs_scene_enum_items(GetCurrentScene(), CenterAlignSelectedItems,
  4343. &boundsType);
  4344. }
  4345. void OBSBasic::on_actionStretchToScreen_triggered()
  4346. {
  4347. obs_bounds_type boundsType = OBS_BOUNDS_STRETCH;
  4348. obs_scene_enum_items(GetCurrentScene(), CenterAlignSelectedItems,
  4349. &boundsType);
  4350. }
  4351. void OBSBasic::on_actionCenterToScreen_triggered()
  4352. {
  4353. auto func = [] (obs_scene_t *scene, obs_sceneitem_t *item, void *param)
  4354. {
  4355. vec3 tl, br, itemCenter, screenCenter, offset;
  4356. obs_video_info ovi;
  4357. if (!obs_sceneitem_selected(item))
  4358. return true;
  4359. obs_get_video_info(&ovi);
  4360. vec3_set(&screenCenter, float(ovi.base_width),
  4361. float(ovi.base_height), 0.0f);
  4362. vec3_mulf(&screenCenter, &screenCenter, 0.5f);
  4363. GetItemBox(item, tl, br);
  4364. vec3_sub(&itemCenter, &br, &tl);
  4365. vec3_mulf(&itemCenter, &itemCenter, 0.5f);
  4366. vec3_add(&itemCenter, &itemCenter, &tl);
  4367. vec3_sub(&offset, &screenCenter, &itemCenter);
  4368. vec3_add(&tl, &tl, &offset);
  4369. SetItemTL(item, tl);
  4370. UNUSED_PARAMETER(scene);
  4371. UNUSED_PARAMETER(param);
  4372. return true;
  4373. };
  4374. obs_scene_enum_items(GetCurrentScene(), func, nullptr);
  4375. }
  4376. void OBSBasic::EnablePreviewDisplay(bool enable)
  4377. {
  4378. obs_display_set_enabled(ui->preview->GetDisplay(), enable);
  4379. ui->preview->setVisible(enable);
  4380. ui->previewDisabledLabel->setVisible(!enable);
  4381. }
  4382. void OBSBasic::TogglePreview()
  4383. {
  4384. previewEnabled = !previewEnabled;
  4385. EnablePreviewDisplay(previewEnabled);
  4386. }
  4387. void OBSBasic::Nudge(int dist, MoveDir dir)
  4388. {
  4389. if (ui->preview->Locked())
  4390. return;
  4391. struct MoveInfo {
  4392. float dist;
  4393. MoveDir dir;
  4394. } info = {(float)dist, dir};
  4395. auto func = [] (obs_scene_t*, obs_sceneitem_t *item, void *param)
  4396. {
  4397. if (obs_sceneitem_locked(item))
  4398. return true;
  4399. MoveInfo *info = reinterpret_cast<MoveInfo*>(param);
  4400. struct vec2 dir;
  4401. struct vec2 pos;
  4402. vec2_set(&dir, 0.0f, 0.0f);
  4403. if (!obs_sceneitem_selected(item))
  4404. return true;
  4405. switch (info->dir) {
  4406. case MoveDir::Up: dir.y = -info->dist; break;
  4407. case MoveDir::Down: dir.y = info->dist; break;
  4408. case MoveDir::Left: dir.x = -info->dist; break;
  4409. case MoveDir::Right: dir.x = info->dist; break;
  4410. }
  4411. obs_sceneitem_get_pos(item, &pos);
  4412. vec2_add(&pos, &pos, &dir);
  4413. obs_sceneitem_set_pos(item, &pos);
  4414. return true;
  4415. };
  4416. obs_scene_enum_items(GetCurrentScene(), func, &info);
  4417. }
  4418. void OBSBasic::NudgeUp() {Nudge(1, MoveDir::Up);}
  4419. void OBSBasic::NudgeDown() {Nudge(1, MoveDir::Down);}
  4420. void OBSBasic::NudgeLeft() {Nudge(1, MoveDir::Left);}
  4421. void OBSBasic::NudgeRight() {Nudge(1, MoveDir::Right);}
  4422. void OBSBasic::OpenProjector(obs_source_t *source, int monitor, bool window,
  4423. QString title, ProjectorType type)
  4424. {
  4425. /* seriously? 10 monitors? */
  4426. if (monitor > 9 || monitor > QGuiApplication::screens().size() - 1)
  4427. return;
  4428. if (!window) {
  4429. delete projectors[monitor];
  4430. projectors[monitor].clear();
  4431. RemoveSavedProjectors(monitor);
  4432. }
  4433. OBSProjector *projector = new OBSProjector(nullptr, source, !!window);
  4434. const char *name = obs_source_get_name(source);
  4435. if (!window) {
  4436. if (type == ProjectorType::StudioProgram) {
  4437. studioProgramProjectorArray.at((size_t)monitor) = 1;
  4438. } else if (type == ProjectorType::Preview) {
  4439. previewProjectorArray.at((size_t)monitor) = 1;
  4440. } else if (type == ProjectorType::Multiview) {
  4441. multiviewProjectorArray.at((size_t)monitor) = 1;
  4442. } else {
  4443. projectorArray.at((size_t)monitor) = name;
  4444. }
  4445. }
  4446. if (!window) {
  4447. projector->Init(monitor, false, nullptr, type);
  4448. projectors[monitor] = projector;
  4449. } else {
  4450. projector->Init(monitor, true, title, type);
  4451. for (auto &projPtr : windowProjectors) {
  4452. if (!projPtr) {
  4453. projPtr = projector;
  4454. projector = nullptr;
  4455. }
  4456. }
  4457. if (projector)
  4458. windowProjectors.push_back(projector);
  4459. }
  4460. }
  4461. void OBSBasic::OpenStudioProgramProjector()
  4462. {
  4463. int monitor = sender()->property("monitor").toInt();
  4464. OpenProjector(nullptr, monitor, false, nullptr,
  4465. ProjectorType::StudioProgram);
  4466. }
  4467. void OBSBasic::OpenPreviewProjector()
  4468. {
  4469. int monitor = sender()->property("monitor").toInt();
  4470. OpenProjector(nullptr, monitor, false, nullptr, ProjectorType::Preview);
  4471. }
  4472. void OBSBasic::OpenSourceProjector()
  4473. {
  4474. int monitor = sender()->property("monitor").toInt();
  4475. OBSSceneItem item = GetCurrentSceneItem();
  4476. if (!item)
  4477. return;
  4478. OpenProjector(obs_sceneitem_get_source(item), monitor, false);
  4479. }
  4480. void OBSBasic::OpenMultiviewProjector()
  4481. {
  4482. int monitor = sender()->property("monitor").toInt();
  4483. OpenProjector(nullptr, monitor, false, nullptr,
  4484. ProjectorType::Multiview);
  4485. }
  4486. void OBSBasic::OpenSceneProjector()
  4487. {
  4488. int monitor = sender()->property("monitor").toInt();
  4489. OBSScene scene = GetCurrentScene();
  4490. if (!scene)
  4491. return;
  4492. OpenProjector(obs_scene_get_source(scene), monitor, false);
  4493. }
  4494. void OBSBasic::OpenStudioProgramWindow()
  4495. {
  4496. int monitor = sender()->property("monitor").toInt();
  4497. QString title = QTStr("StudioProgramWindow");
  4498. OpenProjector(nullptr, monitor, true, title,
  4499. ProjectorType::StudioProgram);
  4500. }
  4501. void OBSBasic::OpenPreviewWindow()
  4502. {
  4503. int monitor = sender()->property("monitor").toInt();
  4504. QString title = QTStr("PreviewWindow");
  4505. OpenProjector(nullptr, monitor, true, nullptr, ProjectorType::Preview);
  4506. }
  4507. void OBSBasic::OpenSourceWindow()
  4508. {
  4509. int monitor = sender()->property("monitor").toInt();
  4510. OBSSceneItem item = GetCurrentSceneItem();
  4511. OBSSource source = obs_sceneitem_get_source(item);
  4512. QString text = QString::fromUtf8(obs_source_get_name(source));
  4513. QString title = QTStr("SourceWindow") + " - " + text;
  4514. if (!item)
  4515. return;
  4516. OpenProjector(obs_sceneitem_get_source(item), monitor, true, title);
  4517. }
  4518. void OBSBasic::OpenMultiviewWindow()
  4519. {
  4520. int monitor = sender()->property("monitor").toInt();
  4521. OpenProjector(nullptr, monitor, true, "Multiview",
  4522. ProjectorType::Multiview);
  4523. }
  4524. void OBSBasic::OpenSceneWindow()
  4525. {
  4526. int monitor = sender()->property("monitor").toInt();
  4527. OBSScene scene = GetCurrentScene();
  4528. OBSSource source = obs_scene_get_source(scene);
  4529. QString text = QString::fromUtf8(obs_source_get_name(source));
  4530. QString title = QTStr("SceneWindow") + " - " + text;
  4531. if (!scene)
  4532. return;
  4533. OpenProjector(obs_scene_get_source(scene), monitor, true, title);
  4534. }
  4535. void OBSBasic::OpenSavedProjectors()
  4536. {
  4537. bool projectorSave = config_get_bool(GetGlobalConfig(),
  4538. "BasicWindow", "SaveProjectors");
  4539. if (projectorSave) {
  4540. for (size_t i = 0; i < projectorArray.size(); i++) {
  4541. if (projectorArray.at(i).empty() == false) {
  4542. OBSSource source = obs_get_source_by_name(
  4543. projectorArray.at(i).c_str());
  4544. if (!source) {
  4545. RemoveSavedProjectors((int)i);
  4546. obs_source_release(source);
  4547. continue;
  4548. }
  4549. OpenProjector(source, (int)i, false);
  4550. obs_source_release(source);
  4551. }
  4552. }
  4553. for (size_t i = 0; i < studioProgramProjectorArray.size(); i++) {
  4554. if (studioProgramProjectorArray.at(i) == 1) {
  4555. OpenProjector(nullptr, (int)i, false, nullptr,
  4556. ProjectorType::StudioProgram);
  4557. }
  4558. }
  4559. for (size_t i = 0; i < previewProjectorArray.size(); i++) {
  4560. if (previewProjectorArray.at(i) == 1) {
  4561. OpenProjector(nullptr, (int)i, false, nullptr,
  4562. ProjectorType::Preview);
  4563. }
  4564. }
  4565. for (size_t i = 0; i < multiviewProjectorArray.size(); i++) {
  4566. if (multiviewProjectorArray.at(i) == 1) {
  4567. OpenProjector(nullptr, (int)i, false, nullptr,
  4568. ProjectorType::Multiview);
  4569. }
  4570. }
  4571. }
  4572. }
  4573. void OBSBasic::RemoveSavedProjectors(int monitor)
  4574. {
  4575. studioProgramProjectorArray.at((size_t)monitor) = 0;
  4576. multiviewProjectorArray.at((size_t)monitor) = 0;
  4577. previewProjectorArray.at((size_t)monitor) = 0;
  4578. projectorArray.at((size_t)monitor) = "";
  4579. }
  4580. void OBSBasic::on_actionFullscreenInterface_triggered()
  4581. {
  4582. if (!fullscreenInterface)
  4583. showFullScreen();
  4584. else
  4585. showNormal();
  4586. fullscreenInterface = !fullscreenInterface;
  4587. }
  4588. void OBSBasic::UpdateTitleBar()
  4589. {
  4590. stringstream name;
  4591. const char *profile = config_get_string(App()->GlobalConfig(),
  4592. "Basic", "Profile");
  4593. const char *sceneCollection = config_get_string(App()->GlobalConfig(),
  4594. "Basic", "SceneCollection");
  4595. name << "OBS ";
  4596. if (previewProgramMode)
  4597. name << "Studio ";
  4598. name << App()->GetVersionString();
  4599. if (App()->IsPortableMode())
  4600. name << " - Portable Mode";
  4601. name << " - " << Str("TitleBar.Profile") << ": " << profile;
  4602. name << " - " << Str("TitleBar.Scenes") << ": " << sceneCollection;
  4603. setWindowTitle(QT_UTF8(name.str().c_str()));
  4604. }
  4605. int OBSBasic::GetProfilePath(char *path, size_t size, const char *file) const
  4606. {
  4607. char profiles_path[512];
  4608. const char *profile = config_get_string(App()->GlobalConfig(),
  4609. "Basic", "ProfileDir");
  4610. int ret;
  4611. if (!profile)
  4612. return -1;
  4613. if (!path)
  4614. return -1;
  4615. if (!file)
  4616. file = "";
  4617. ret = GetConfigPath(profiles_path, 512, "obs-studio/basic/profiles");
  4618. if (ret <= 0)
  4619. return ret;
  4620. if (!*file)
  4621. return snprintf(path, size, "%s/%s", profiles_path, profile);
  4622. return snprintf(path, size, "%s/%s/%s", profiles_path, profile, file);
  4623. }
  4624. void OBSBasic::on_resetUI_triggered()
  4625. {
  4626. restoreState(startingDockLayout);
  4627. #if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
  4628. int cx = width();
  4629. int cy = height();
  4630. int cx22_5 = cx * 225 / 1000;
  4631. int cx5 = cx * 5 / 100;
  4632. cy = cy * 225 / 1000;
  4633. int mixerSize = cx - (cx22_5 * 2 + cx5 * 2);
  4634. QList<QDockWidget*> docks {
  4635. ui->scenesDock,
  4636. ui->sourcesDock,
  4637. ui->mixerDock,
  4638. ui->transitionsDock,
  4639. ui->controlsDock
  4640. };
  4641. QList<int> sizes {
  4642. cx22_5,
  4643. cx22_5,
  4644. mixerSize,
  4645. cx5,
  4646. cx5
  4647. };
  4648. ui->scenesDock->setVisible(true);
  4649. ui->sourcesDock->setVisible(true);
  4650. ui->mixerDock->setVisible(true);
  4651. ui->transitionsDock->setVisible(true);
  4652. ui->controlsDock->setVisible(true);
  4653. resizeDocks(docks, {cy, cy, cy, cy, cy}, Qt::Vertical);
  4654. resizeDocks(docks, sizes, Qt::Horizontal);
  4655. #endif
  4656. }
  4657. void OBSBasic::on_lockUI_toggled(bool lock)
  4658. {
  4659. QDockWidget::DockWidgetFeatures features = lock
  4660. ? QDockWidget::NoDockWidgetFeatures
  4661. : QDockWidget::AllDockWidgetFeatures;
  4662. ui->scenesDock->setFeatures(features);
  4663. ui->sourcesDock->setFeatures(features);
  4664. ui->mixerDock->setFeatures(features);
  4665. ui->transitionsDock->setFeatures(features);
  4666. ui->controlsDock->setFeatures(features);
  4667. }
  4668. void OBSBasic::on_toggleListboxToolbars_toggled(bool visible)
  4669. {
  4670. ui->sourcesToolbar->setVisible(visible);
  4671. ui->scenesToolbar->setVisible(visible);
  4672. config_set_bool(App()->GlobalConfig(), "BasicWindow",
  4673. "ShowListboxToolbars", visible);
  4674. }
  4675. void OBSBasic::on_toggleStatusBar_toggled(bool visible)
  4676. {
  4677. ui->statusbar->setVisible(visible);
  4678. config_set_bool(App()->GlobalConfig(), "BasicWindow",
  4679. "ShowStatusBar", visible);
  4680. }
  4681. void OBSBasic::on_actionLockPreview_triggered()
  4682. {
  4683. ui->preview->ToggleLocked();
  4684. ui->actionLockPreview->setChecked(ui->preview->Locked());
  4685. }
  4686. void OBSBasic::on_scalingMenu_aboutToShow()
  4687. {
  4688. obs_video_info ovi;
  4689. obs_get_video_info(&ovi);
  4690. QAction *action = ui->actionScaleCanvas;
  4691. QString text = QTStr("Basic.MainMenu.Edit.Scale.Canvas");
  4692. text = text.arg(QString::number(ovi.base_width),
  4693. QString::number(ovi.base_height));
  4694. action->setText(text);
  4695. action = ui->actionScaleOutput;
  4696. text = QTStr("Basic.MainMenu.Edit.Scale.Output");
  4697. text = text.arg(QString::number(ovi.output_width),
  4698. QString::number(ovi.output_height));
  4699. action->setText(text);
  4700. action->setVisible(!(ovi.output_width == ovi.base_width &&
  4701. ovi.output_height == ovi.base_height));
  4702. UpdatePreviewScalingMenu();
  4703. }
  4704. void OBSBasic::on_actionScaleWindow_triggered()
  4705. {
  4706. ui->preview->SetFixedScaling(false);
  4707. ui->preview->ResetScrollingOffset();
  4708. emit ui->preview->DisplayResized();
  4709. }
  4710. void OBSBasic::on_actionScaleCanvas_triggered()
  4711. {
  4712. ui->preview->SetFixedScaling(true);
  4713. ui->preview->SetScalingLevel(0);
  4714. emit ui->preview->DisplayResized();
  4715. }
  4716. void OBSBasic::on_actionScaleOutput_triggered()
  4717. {
  4718. obs_video_info ovi;
  4719. obs_get_video_info(&ovi);
  4720. ui->preview->SetFixedScaling(true);
  4721. float scalingAmount = float(ovi.output_width) / float(ovi.base_width);
  4722. // log base ZOOM_SENSITIVITY of x = log(x) / log(ZOOM_SENSITIVITY)
  4723. int32_t approxScalingLevel = int32_t(
  4724. round(log(scalingAmount) / log(ZOOM_SENSITIVITY)));
  4725. ui->preview->SetScalingLevel(approxScalingLevel);
  4726. ui->preview->SetScalingAmount(scalingAmount);
  4727. emit ui->preview->DisplayResized();
  4728. }
  4729. void OBSBasic::SetShowing(bool showing)
  4730. {
  4731. if (!showing && isVisible()) {
  4732. config_set_string(App()->GlobalConfig(),
  4733. "BasicWindow", "geometry",
  4734. saveGeometry().toBase64().constData());
  4735. /* hide all visible child dialogs */
  4736. visDlgPositions.clear();
  4737. if (!visDialogs.isEmpty()) {
  4738. for (QDialog *dlg : visDialogs) {
  4739. visDlgPositions.append(dlg->pos());
  4740. dlg->hide();
  4741. }
  4742. }
  4743. if (showHide)
  4744. showHide->setText(QTStr("Basic.SystemTray.Show"));
  4745. QTimer::singleShot(250, this, SLOT(hide()));
  4746. if (previewEnabled)
  4747. EnablePreviewDisplay(false);
  4748. setVisible(false);
  4749. } else if (showing && !isVisible()) {
  4750. if (showHide)
  4751. showHide->setText(QTStr("Basic.SystemTray.Hide"));
  4752. QTimer::singleShot(250, this, SLOT(show()));
  4753. if (previewEnabled)
  4754. EnablePreviewDisplay(true);
  4755. setVisible(true);
  4756. /* show all child dialogs that was visible earlier */
  4757. if (!visDialogs.isEmpty()) {
  4758. for (int i = 0; i < visDialogs.size(); ++i) {
  4759. QDialog *dlg = visDialogs[i];
  4760. dlg->move(visDlgPositions[i]);
  4761. dlg->show();
  4762. }
  4763. }
  4764. /* Unminimize window if it was hidden to tray instead of task
  4765. * bar. */
  4766. if (sysTrayMinimizeToTray()) {
  4767. Qt::WindowStates state;
  4768. state = windowState() & ~Qt::WindowMinimized;
  4769. state |= Qt::WindowActive;
  4770. setWindowState(state);
  4771. }
  4772. }
  4773. }
  4774. void OBSBasic::ToggleShowHide()
  4775. {
  4776. bool showing = isVisible();
  4777. if (showing) {
  4778. /* check for modal dialogs */
  4779. EnumDialogs();
  4780. if (!modalDialogs.isEmpty() || !visMsgBoxes.isEmpty())
  4781. return;
  4782. }
  4783. SetShowing(!showing);
  4784. }
  4785. void OBSBasic::SystemTrayInit()
  4786. {
  4787. trayIcon = new QSystemTrayIcon(QIcon(":/res/images/obs.png"),
  4788. this);
  4789. trayIcon->setToolTip("OBS Studio");
  4790. showHide = new QAction(QTStr("Basic.SystemTray.Show"),
  4791. trayIcon);
  4792. sysTrayStream = new QAction(QTStr("Basic.Main.StartStreaming"),
  4793. trayIcon);
  4794. sysTrayRecord = new QAction(QTStr("Basic.Main.StartRecording"),
  4795. trayIcon);
  4796. sysTrayReplayBuffer = new QAction(QTStr("Basic.Main.StartReplayBuffer"),
  4797. trayIcon);
  4798. exit = new QAction(QTStr("Exit"),
  4799. trayIcon);
  4800. if (outputHandler && !outputHandler->replayBuffer)
  4801. sysTrayReplayBuffer->setEnabled(false);
  4802. connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
  4803. this,
  4804. SLOT(IconActivated(QSystemTrayIcon::ActivationReason)));
  4805. connect(showHide, SIGNAL(triggered()),
  4806. this, SLOT(ToggleShowHide()));
  4807. connect(sysTrayStream, SIGNAL(triggered()),
  4808. this, SLOT(on_streamButton_clicked()));
  4809. connect(sysTrayRecord, SIGNAL(triggered()),
  4810. this, SLOT(on_recordButton_clicked()));
  4811. connect(sysTrayReplayBuffer.data(), &QAction::triggered,
  4812. this, &OBSBasic::ReplayBufferClicked);
  4813. connect(exit, SIGNAL(triggered()),
  4814. this, SLOT(close()));
  4815. QMenu *previewProjector = new QMenu(QTStr("PreviewProjector"));
  4816. AddProjectorMenuMonitors(previewProjector, this,
  4817. SLOT(OpenPreviewProjector()));
  4818. QMenu *studioProgramProjector = new QMenu(
  4819. QTStr("StudioProgramProjector"));
  4820. AddProjectorMenuMonitors(studioProgramProjector, this,
  4821. SLOT(OpenStudioProgramProjector()));
  4822. trayMenu = new QMenu;
  4823. trayMenu->addAction(showHide);
  4824. trayMenu->addMenu(previewProjector);
  4825. trayMenu->addMenu(studioProgramProjector);
  4826. trayMenu->addAction(sysTrayStream);
  4827. trayMenu->addAction(sysTrayRecord);
  4828. trayMenu->addAction(sysTrayReplayBuffer);
  4829. trayMenu->addAction(exit);
  4830. trayIcon->setContextMenu(trayMenu);
  4831. }
  4832. void OBSBasic::IconActivated(QSystemTrayIcon::ActivationReason reason)
  4833. {
  4834. if (reason == QSystemTrayIcon::Trigger)
  4835. ToggleShowHide();
  4836. }
  4837. void OBSBasic::SysTrayNotify(const QString &text,
  4838. QSystemTrayIcon::MessageIcon n)
  4839. {
  4840. if (QSystemTrayIcon::supportsMessages()) {
  4841. QSystemTrayIcon::MessageIcon icon =
  4842. QSystemTrayIcon::MessageIcon(n);
  4843. trayIcon->showMessage("OBS Studio", text, icon, 10000);
  4844. }
  4845. }
  4846. void OBSBasic::SystemTray(bool firstStarted)
  4847. {
  4848. if (!QSystemTrayIcon::isSystemTrayAvailable())
  4849. return;
  4850. bool sysTrayWhenStarted = config_get_bool(GetGlobalConfig(),
  4851. "BasicWindow", "SysTrayWhenStarted");
  4852. bool sysTrayEnabled = config_get_bool(GetGlobalConfig(),
  4853. "BasicWindow", "SysTrayEnabled");
  4854. if (firstStarted)
  4855. SystemTrayInit();
  4856. if (!sysTrayWhenStarted && !sysTrayEnabled) {
  4857. trayIcon->hide();
  4858. } else if ((sysTrayWhenStarted && sysTrayEnabled)
  4859. || opt_minimize_tray) {
  4860. trayIcon->show();
  4861. if (firstStarted) {
  4862. QTimer::singleShot(50, this, SLOT(hide()));
  4863. EnablePreviewDisplay(false);
  4864. setVisible(false);
  4865. opt_minimize_tray = false;
  4866. }
  4867. } else if (sysTrayEnabled) {
  4868. trayIcon->show();
  4869. } else if (!sysTrayEnabled) {
  4870. trayIcon->hide();
  4871. } else if (!sysTrayWhenStarted && sysTrayEnabled) {
  4872. trayIcon->hide();
  4873. }
  4874. if (isVisible())
  4875. showHide->setText(QTStr("Basic.SystemTray.Hide"));
  4876. else
  4877. showHide->setText(QTStr("Basic.SystemTray.Show"));
  4878. }
  4879. bool OBSBasic::sysTrayMinimizeToTray()
  4880. {
  4881. return config_get_bool(GetGlobalConfig(),
  4882. "BasicWindow", "SysTrayMinimizeToTray");
  4883. }
  4884. void OBSBasic::on_actionCopySource_triggered()
  4885. {
  4886. on_actionCopyTransform_triggered();
  4887. OBSSceneItem item = GetCurrentSceneItem();
  4888. if (!item)
  4889. return;
  4890. OBSSource source = obs_sceneitem_get_source(item);
  4891. copyString = obs_source_get_name(source);
  4892. copyVisible = obs_sceneitem_visible(item);
  4893. ui->actionPasteRef->setEnabled(true);
  4894. uint32_t output_flags = obs_source_get_output_flags(source);
  4895. if ((output_flags & OBS_SOURCE_DO_NOT_DUPLICATE) == 0)
  4896. ui->actionPasteDup->setEnabled(true);
  4897. else
  4898. ui->actionPasteDup->setEnabled(false);
  4899. }
  4900. void OBSBasic::on_actionPasteRef_triggered()
  4901. {
  4902. OBSBasicSourceSelect::SourcePaste(copyString, copyVisible, false);
  4903. on_actionPasteTransform_triggered();
  4904. }
  4905. void OBSBasic::on_actionPasteDup_triggered()
  4906. {
  4907. OBSBasicSourceSelect::SourcePaste(copyString, copyVisible, true);
  4908. on_actionPasteTransform_triggered();
  4909. }
  4910. void OBSBasic::on_actionCopyFilters_triggered()
  4911. {
  4912. OBSSceneItem item = GetCurrentSceneItem();
  4913. if (!item)
  4914. return;
  4915. OBSSource source = obs_sceneitem_get_source(item);
  4916. copyFiltersString = obs_source_get_name(source);
  4917. ui->actionPasteFilters->setEnabled(true);
  4918. }
  4919. void OBSBasic::on_actionPasteFilters_triggered()
  4920. {
  4921. OBSSource source = obs_get_source_by_name(copyFiltersString);
  4922. OBSSceneItem sceneItem = GetCurrentSceneItem();
  4923. OBSSource dstSource = obs_sceneitem_get_source(sceneItem);
  4924. if (source == dstSource)
  4925. return;
  4926. obs_source_copy_filters(dstSource, source);
  4927. }
  4928. void OBSBasic::on_autoConfigure_triggered()
  4929. {
  4930. AutoConfig test(this);
  4931. test.setModal(true);
  4932. test.show();
  4933. test.exec();
  4934. }
  4935. void OBSBasic::on_stats_triggered()
  4936. {
  4937. if (!stats.isNull()) {
  4938. stats->show();
  4939. stats->raise();
  4940. return;
  4941. }
  4942. OBSBasicStats *statsDlg;
  4943. statsDlg = new OBSBasicStats(nullptr);
  4944. statsDlg->show();
  4945. stats = statsDlg;
  4946. }