window-basic-main.cpp 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764
  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 <QMessageBox>
  19. #include <QShowEvent>
  20. #include <QDesktopServices>
  21. #include <QFileDialog>
  22. #include <util/dstr.h>
  23. #include <util/util.hpp>
  24. #include <util/platform.h>
  25. #include <util/profiler.hpp>
  26. #include <graphics/math-defs.h>
  27. #include "obs-app.hpp"
  28. #include "platform.hpp"
  29. #include "visibility-item-widget.hpp"
  30. #include "item-widget-helpers.hpp"
  31. #include "window-basic-settings.hpp"
  32. #include "window-namedialog.hpp"
  33. #include "window-basic-source-select.hpp"
  34. #include "window-basic-main.hpp"
  35. #include "window-basic-main-outputs.hpp"
  36. #include "window-basic-properties.hpp"
  37. #include "window-log-reply.hpp"
  38. #include "window-projector.hpp"
  39. #include "window-remux.hpp"
  40. #include "qt-wrappers.hpp"
  41. #include "display-helpers.hpp"
  42. #include "volume-control.hpp"
  43. #include "remote-text.hpp"
  44. #include "ui_OBSBasic.h"
  45. #include <fstream>
  46. #include <sstream>
  47. #include <QScreen>
  48. #include <QWindow>
  49. #define PREVIEW_EDGE_SIZE 10
  50. using namespace std;
  51. Q_DECLARE_METATYPE(OBSScene);
  52. Q_DECLARE_METATYPE(OBSSceneItem);
  53. Q_DECLARE_METATYPE(OBSSource);
  54. Q_DECLARE_METATYPE(obs_order_movement);
  55. Q_DECLARE_METATYPE(std::vector<std::shared_ptr<OBSSignal>>);
  56. template <typename T>
  57. static T GetOBSRef(QListWidgetItem *item)
  58. {
  59. return item->data(static_cast<int>(QtDataRole::OBSRef)).value<T>();
  60. }
  61. template <typename T>
  62. static void SetOBSRef(QListWidgetItem *item, T &&val)
  63. {
  64. item->setData(static_cast<int>(QtDataRole::OBSRef),
  65. QVariant::fromValue(val));
  66. }
  67. static void AddExtraModulePaths()
  68. {
  69. char base_module_dir[512];
  70. int ret = GetConfigPath(base_module_dir, sizeof(base_module_dir),
  71. "obs-studio/plugins/%module%");
  72. if (ret <= 0)
  73. return;
  74. string path = (char*)base_module_dir;
  75. #if defined(__APPLE__)
  76. obs_add_module_path((path + "/bin").c_str(), (path + "/data").c_str());
  77. #elif ARCH_BITS == 64
  78. obs_add_module_path((path + "/bin/64bit").c_str(),
  79. (path + "/data").c_str());
  80. #else
  81. obs_add_module_path((path + "/bin/32bit").c_str(),
  82. (path + "/data").c_str());
  83. #endif
  84. }
  85. static QList<QKeySequence> DeleteKeys;
  86. OBSBasic::OBSBasic(QWidget *parent)
  87. : OBSMainWindow (parent),
  88. ui (new Ui::OBSBasic)
  89. {
  90. ui->setupUi(this);
  91. ui->previewDisabledLabel->setVisible(false);
  92. copyActionsDynamicProperties();
  93. ui->sources->setItemDelegate(new VisibilityItemDelegate(ui->sources));
  94. int width = config_get_int(App()->GlobalConfig(), "BasicWindow", "cx");
  95. // Check if no values are saved (new installation).
  96. if (width != 0) {
  97. int height = config_get_int(App()->GlobalConfig(),
  98. "BasicWindow", "cy");
  99. int posx = config_get_int(App()->GlobalConfig(), "BasicWindow",
  100. "posx");
  101. int posy = config_get_int(App()->GlobalConfig(), "BasicWindow",
  102. "posy");
  103. setGeometry(posx, posy, width, height);
  104. }
  105. char styleSheetPath[512];
  106. int ret = GetProfilePath(styleSheetPath, sizeof(styleSheetPath),
  107. "stylesheet.qss");
  108. if (ret > 0) {
  109. if (QFile::exists(styleSheetPath)) {
  110. QString path = QString("file:///") +
  111. QT_UTF8(styleSheetPath);
  112. App()->setStyleSheet(path);
  113. }
  114. }
  115. qRegisterMetaType<OBSScene> ("OBSScene");
  116. qRegisterMetaType<OBSSceneItem>("OBSSceneItem");
  117. qRegisterMetaType<OBSSource> ("OBSSource");
  118. qRegisterMetaType<obs_hotkey_id>("obs_hotkey_id");
  119. qRegisterMetaTypeStreamOperators<
  120. std::vector<std::shared_ptr<OBSSignal>>>(
  121. "std::vector<std::shared_ptr<OBSSignal>>");
  122. qRegisterMetaTypeStreamOperators<OBSScene>("OBSScene");
  123. qRegisterMetaTypeStreamOperators<OBSSceneItem>("OBSSceneItem");
  124. ui->scenes->setAttribute(Qt::WA_MacShowFocusRect, false);
  125. ui->sources->setAttribute(Qt::WA_MacShowFocusRect, false);
  126. connect(windowHandle(), &QWindow::screenChanged, [this]() {
  127. struct obs_video_info ovi;
  128. if (obs_get_video_info(&ovi))
  129. ResizePreview(ovi.base_width, ovi.base_height);
  130. });
  131. installEventFilter(CreateShortcutFilter());
  132. stringstream name;
  133. name << "OBS " << App()->GetVersionString();
  134. blog(LOG_INFO, "%s", name.str().c_str());
  135. blog(LOG_INFO, "---------------------------------");
  136. UpdateTitleBar();
  137. connect(ui->scenes->itemDelegate(),
  138. SIGNAL(closeEditor(QWidget*,
  139. QAbstractItemDelegate::EndEditHint)),
  140. this,
  141. SLOT(SceneNameEdited(QWidget*,
  142. QAbstractItemDelegate::EndEditHint)));
  143. connect(ui->sources->itemDelegate(),
  144. SIGNAL(closeEditor(QWidget*,
  145. QAbstractItemDelegate::EndEditHint)),
  146. this,
  147. SLOT(SceneItemNameEdited(QWidget*,
  148. QAbstractItemDelegate::EndEditHint)));
  149. cpuUsageInfo = os_cpu_usage_info_start();
  150. cpuUsageTimer = new QTimer(this);
  151. connect(cpuUsageTimer, SIGNAL(timeout()),
  152. ui->statusbar, SLOT(UpdateCPUUsage()));
  153. cpuUsageTimer->start(3000);
  154. DeleteKeys =
  155. #ifdef __APPLE__
  156. QList<QKeySequence>{{Qt::Key_Backspace}} <<
  157. #endif
  158. QKeySequence::keyBindings(QKeySequence::Delete);
  159. #ifdef __APPLE__
  160. ui->actionRemoveSource->setShortcuts(DeleteKeys);
  161. ui->actionRemoveScene->setShortcuts(DeleteKeys);
  162. ui->action_Settings->setMenuRole(QAction::PreferencesRole);
  163. ui->actionE_xit->setMenuRole(QAction::QuitRole);
  164. #endif
  165. auto addNudge = [this](const QKeySequence &seq, const char *s)
  166. {
  167. QAction *nudge = new QAction(ui->preview);
  168. nudge->setShortcut(seq);
  169. nudge->setShortcutContext(Qt::WidgetShortcut);
  170. ui->preview->addAction(nudge);
  171. connect(nudge, SIGNAL(triggered()), this, s);
  172. };
  173. addNudge(Qt::Key_Up, SLOT(NudgeUp()));
  174. addNudge(Qt::Key_Down, SLOT(NudgeDown()));
  175. addNudge(Qt::Key_Left, SLOT(NudgeLeft()));
  176. addNudge(Qt::Key_Right, SLOT(NudgeRight()));
  177. }
  178. static void SaveAudioDevice(const char *name, int channel, obs_data_t *parent)
  179. {
  180. obs_source_t *source = obs_get_output_source(channel);
  181. if (!source)
  182. return;
  183. obs_data_t *data = obs_save_source(source);
  184. obs_data_set_obj(parent, name, data);
  185. obs_data_release(data);
  186. obs_source_release(source);
  187. }
  188. static obs_data_t *GenerateSaveData(obs_data_array_t *sceneOrder)
  189. {
  190. obs_data_t *saveData = obs_data_create();
  191. obs_data_array_t *sourcesArray = obs_save_sources();
  192. obs_source_t *currentScene = obs_get_output_source(0);
  193. const char *sceneName = obs_source_get_name(currentScene);
  194. const char *sceneCollection = config_get_string(App()->GlobalConfig(),
  195. "Basic", "SceneCollection");
  196. SaveAudioDevice(DESKTOP_AUDIO_1, 1, saveData);
  197. SaveAudioDevice(DESKTOP_AUDIO_2, 2, saveData);
  198. SaveAudioDevice(AUX_AUDIO_1, 3, saveData);
  199. SaveAudioDevice(AUX_AUDIO_2, 4, saveData);
  200. SaveAudioDevice(AUX_AUDIO_3, 5, saveData);
  201. obs_data_set_string(saveData, "current_scene", sceneName);
  202. obs_data_set_array(saveData, "scene_order", sceneOrder);
  203. obs_data_set_string(saveData, "name", sceneCollection);
  204. obs_data_set_array(saveData, "sources", sourcesArray);
  205. obs_data_array_release(sourcesArray);
  206. obs_source_release(currentScene);
  207. return saveData;
  208. }
  209. void OBSBasic::copyActionsDynamicProperties()
  210. {
  211. // Themes need the QAction dynamic properties
  212. for (QAction *x : ui->scenesToolbar->actions()) {
  213. QWidget* temp = ui->scenesToolbar->widgetForAction(x);
  214. for (QByteArray &y : x->dynamicPropertyNames()) {
  215. temp->setProperty(y, x->property(y));
  216. }
  217. }
  218. for (QAction *x : ui->sourcesToolbar->actions()) {
  219. QWidget* temp = ui->sourcesToolbar->widgetForAction(x);
  220. for (QByteArray &y : x->dynamicPropertyNames()) {
  221. temp->setProperty(y, x->property(y));
  222. }
  223. }
  224. }
  225. void OBSBasic::ClearVolumeControls()
  226. {
  227. VolControl *control;
  228. for (size_t i = 0; i < volumes.size(); i++) {
  229. control = volumes[i];
  230. delete control;
  231. }
  232. volumes.clear();
  233. }
  234. obs_data_array_t *OBSBasic::SaveSceneListOrder()
  235. {
  236. obs_data_array_t *sceneOrder = obs_data_array_create();
  237. for (int i = 0; i < ui->scenes->count(); i++) {
  238. obs_data_t *data = obs_data_create();
  239. obs_data_set_string(data, "name",
  240. QT_TO_UTF8(ui->scenes->item(i)->text()));
  241. obs_data_array_push_back(sceneOrder, data);
  242. obs_data_release(data);
  243. }
  244. return sceneOrder;
  245. }
  246. void OBSBasic::Save(const char *file)
  247. {
  248. obs_data_array_t *sceneOrder = SaveSceneListOrder();
  249. obs_data_t *saveData = GenerateSaveData(sceneOrder);
  250. if (!obs_data_save_json_safe(saveData, file, "tmp", "bak"))
  251. blog(LOG_ERROR, "Could not save scene data to %s", file);
  252. obs_data_release(saveData);
  253. obs_data_array_release(sceneOrder);
  254. }
  255. static void LoadAudioDevice(const char *name, int channel, obs_data_t *parent)
  256. {
  257. obs_data_t *data = obs_data_get_obj(parent, name);
  258. if (!data)
  259. return;
  260. obs_source_t *source = obs_load_source(data);
  261. if (source) {
  262. obs_set_output_source(channel, source);
  263. obs_source_release(source);
  264. }
  265. obs_data_release(data);
  266. }
  267. static inline bool HasAudioDevices(const char *source_id)
  268. {
  269. const char *output_id = source_id;
  270. obs_properties_t *props = obs_get_source_properties(
  271. OBS_SOURCE_TYPE_INPUT, output_id);
  272. size_t count = 0;
  273. if (!props)
  274. return false;
  275. obs_property_t *devices = obs_properties_get(props, "device_id");
  276. if (devices)
  277. count = obs_property_list_item_count(devices);
  278. obs_properties_destroy(props);
  279. return count != 0;
  280. }
  281. void OBSBasic::CreateFirstRunSources(obs_scene_t *scene)
  282. {
  283. bool hasDesktopAudio = HasAudioDevices(App()->OutputAudioSource());
  284. bool hasInputAudio = HasAudioDevices(App()->InputAudioSource());
  285. const char *displayCaptureType = nullptr;
  286. obs_source_t *source = nullptr;
  287. #ifdef __APPLE__
  288. displayCaptureType = "display_capture";
  289. #elif _WIN32
  290. if (GetWindowsVersion() >= 0x602)
  291. displayCaptureType = "monitor_capture";
  292. #else //X11
  293. displayCaptureType = "xshm_input";
  294. #endif
  295. if (displayCaptureType) {
  296. source = obs_source_create(OBS_SOURCE_TYPE_INPUT,
  297. displayCaptureType, Str("Basic.DisplayCapture"),
  298. NULL, nullptr);
  299. }
  300. if (source) {
  301. obs_scene_add(scene, source);
  302. obs_add_source(source);
  303. obs_source_release(source);
  304. }
  305. if (hasDesktopAudio)
  306. ResetAudioDevice(App()->OutputAudioSource(), "default",
  307. Str("Basic.DesktopDevice1"), 1);
  308. if (hasInputAudio)
  309. ResetAudioDevice(App()->InputAudioSource(), "default",
  310. Str("Basic.AuxDevice1"), 3);
  311. }
  312. void OBSBasic::CreateDefaultScene()
  313. {
  314. disableSaving++;
  315. ClearSceneData();
  316. obs_scene_t *scene = obs_scene_create(Str("Basic.Scene"));
  317. obs_source_t *source = obs_scene_get_source(scene);
  318. obs_add_source(source);
  319. CreateFirstRunSources(scene);
  320. obs_set_output_source(0, obs_scene_get_source(scene));
  321. obs_scene_release(scene);
  322. disableSaving--;
  323. }
  324. static void ReorderItemByName(QListWidget *lw, const char *name, int newIndex)
  325. {
  326. for (int i = 0; i < lw->count(); i++) {
  327. QListWidgetItem *item = lw->item(i);
  328. if (strcmp(name, QT_TO_UTF8(item->text())) == 0) {
  329. if (newIndex != i) {
  330. item = lw->takeItem(i);
  331. lw->insertItem(newIndex, item);
  332. }
  333. break;
  334. }
  335. }
  336. }
  337. void OBSBasic::LoadSceneListOrder(obs_data_array_t *array)
  338. {
  339. size_t num = obs_data_array_count(array);
  340. for (size_t i = 0; i < num; i++) {
  341. obs_data_t *data = obs_data_array_item(array, i);
  342. const char *name = obs_data_get_string(data, "name");
  343. ReorderItemByName(ui->scenes, name, (int)i);
  344. obs_data_release(data);
  345. }
  346. }
  347. void OBSBasic::CleanupUnusedSources()
  348. {
  349. auto removeUnusedSources = [&](obs_source_t *source)
  350. {
  351. obs_scene_t *scene = obs_scene_from_source(source);
  352. if (scene)
  353. return;
  354. if (sourceSceneRefs[source] == 0) {
  355. sourceSceneRefs.erase(source);
  356. obs_source_remove(source);
  357. }
  358. };
  359. using func_type = decltype(removeUnusedSources);
  360. obs_enum_sources(
  361. [](void *f, obs_source_t *source)
  362. {
  363. (*static_cast<func_type*>(f))(source);
  364. return true;
  365. },
  366. static_cast<void*>(&removeUnusedSources));
  367. }
  368. void OBSBasic::Load(const char *file)
  369. {
  370. if (!file || !os_file_exists(file)) {
  371. blog(LOG_INFO, "No scene file found, creating default scene");
  372. CreateDefaultScene();
  373. SaveProject();
  374. return;
  375. }
  376. disableSaving++;
  377. obs_data_t *data = obs_data_create_from_json_file_safe(file, "bak");
  378. if (!data) {
  379. disableSaving--;
  380. blog(LOG_ERROR, "Failed to load '%s', creating default scene",
  381. file);
  382. CreateDefaultScene();
  383. SaveProject();
  384. return;
  385. }
  386. ClearSceneData();
  387. obs_data_array_t *sceneOrder = obs_data_get_array(data, "scene_order");
  388. obs_data_array_t *sources = obs_data_get_array(data, "sources");
  389. const char *sceneName = obs_data_get_string(data,
  390. "current_scene");
  391. const char *curSceneCollection = config_get_string(
  392. App()->GlobalConfig(), "Basic", "SceneCollection");
  393. obs_data_set_default_string(data, "name", curSceneCollection);
  394. const char *name = obs_data_get_string(data, "name");
  395. obs_source_t *curScene;
  396. if (!name || !*name)
  397. name = curSceneCollection;
  398. LoadAudioDevice(DESKTOP_AUDIO_1, 1, data);
  399. LoadAudioDevice(DESKTOP_AUDIO_2, 2, data);
  400. LoadAudioDevice(AUX_AUDIO_1, 3, data);
  401. LoadAudioDevice(AUX_AUDIO_2, 4, data);
  402. LoadAudioDevice(AUX_AUDIO_3, 5, data);
  403. obs_load_sources(sources);
  404. if (sceneOrder)
  405. LoadSceneListOrder(sceneOrder);
  406. curScene = obs_get_source_by_name(sceneName);
  407. obs_set_output_source(0, curScene);
  408. obs_source_release(curScene);
  409. obs_data_array_release(sources);
  410. obs_data_array_release(sceneOrder);
  411. std::string file_base = strrchr(file, '/') + 1;
  412. file_base.erase(file_base.size() - 5, 5);
  413. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollection",
  414. name);
  415. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollectionFile",
  416. file_base.c_str());
  417. obs_data_release(data);
  418. CleanupUnusedSources();
  419. disableSaving--;
  420. }
  421. #define SERVICE_PATH "service.json"
  422. void OBSBasic::SaveService()
  423. {
  424. if (!service)
  425. return;
  426. char serviceJsonPath[512];
  427. int ret = GetProfilePath(serviceJsonPath, sizeof(serviceJsonPath),
  428. SERVICE_PATH);
  429. if (ret <= 0)
  430. return;
  431. obs_data_t *data = obs_data_create();
  432. obs_data_t *settings = obs_service_get_settings(service);
  433. obs_data_set_string(data, "type", obs_service_get_type(service));
  434. obs_data_set_obj(data, "settings", settings);
  435. if (!obs_data_save_json_safe(data, serviceJsonPath, "tmp", "bak"))
  436. blog(LOG_WARNING, "Failed to save service");
  437. obs_data_release(settings);
  438. obs_data_release(data);
  439. }
  440. bool OBSBasic::LoadService()
  441. {
  442. const char *type;
  443. char serviceJsonPath[512];
  444. int ret = GetProfilePath(serviceJsonPath, sizeof(serviceJsonPath),
  445. SERVICE_PATH);
  446. if (ret <= 0)
  447. return false;
  448. obs_data_t *data = obs_data_create_from_json_file_safe(serviceJsonPath,
  449. "bak");
  450. obs_data_set_default_string(data, "type", "rtmp_common");
  451. type = obs_data_get_string(data, "type");
  452. obs_data_t *settings = obs_data_get_obj(data, "settings");
  453. obs_data_t *hotkey_data = obs_data_get_obj(data, "hotkeys");
  454. service = obs_service_create(type, "default_service", settings,
  455. hotkey_data);
  456. obs_service_release(service);
  457. obs_data_release(hotkey_data);
  458. obs_data_release(settings);
  459. obs_data_release(data);
  460. return !!service;
  461. }
  462. bool OBSBasic::InitService()
  463. {
  464. ProfileScope("OBSBasic::InitService");
  465. if (LoadService())
  466. return true;
  467. service = obs_service_create("rtmp_common", "default_service", nullptr,
  468. nullptr);
  469. if (!service)
  470. return false;
  471. obs_service_release(service);
  472. return true;
  473. }
  474. static const double scaled_vals[] =
  475. {
  476. 1.0,
  477. 1.25,
  478. (1.0/0.75),
  479. 1.5,
  480. (1.0/0.6),
  481. 1.75,
  482. 2.0,
  483. 2.25,
  484. 2.5,
  485. 2.75,
  486. 3.0,
  487. 0.0
  488. };
  489. bool OBSBasic::InitBasicConfigDefaults()
  490. {
  491. vector<MonitorInfo> monitors;
  492. GetMonitors(monitors);
  493. if (!monitors.size()) {
  494. OBSErrorBox(NULL, "There appears to be no monitors. Er, this "
  495. "technically shouldn't be possible.");
  496. return false;
  497. }
  498. uint32_t cx = monitors[0].cx;
  499. uint32_t cy = monitors[0].cy;
  500. /* ----------------------------------------------------- */
  501. /* move over mixer values in advanced if older config */
  502. if (config_has_user_value(basicConfig, "AdvOut", "RecTrackIndex") &&
  503. !config_has_user_value(basicConfig, "AdvOut", "RecTracks")) {
  504. uint64_t track = config_get_uint(basicConfig, "AdvOut",
  505. "RecTrackIndex");
  506. track = 1ULL << (track - 1);
  507. config_set_uint(basicConfig, "AdvOut", "RecTracks", track);
  508. config_remove_value(basicConfig, "AdvOut", "RecTrackIndex");
  509. config_save_safe(basicConfig, "tmp", nullptr);
  510. }
  511. /* ----------------------------------------------------- */
  512. config_set_default_string(basicConfig, "Output", "Mode", "Simple");
  513. config_set_default_string(basicConfig, "SimpleOutput", "FilePath",
  514. GetDefaultVideoSavePath().c_str());
  515. config_set_default_string(basicConfig, "SimpleOutput", "RecFormat",
  516. "flv");
  517. config_set_default_uint (basicConfig, "SimpleOutput", "VBitrate",
  518. 2500);
  519. config_set_default_uint (basicConfig, "SimpleOutput", "ABitrate", 160);
  520. config_set_default_bool (basicConfig, "SimpleOutput", "UseAdvanced",
  521. false);
  522. config_set_default_string(basicConfig, "SimpleOutput", "Preset",
  523. "veryfast");
  524. config_set_default_bool (basicConfig, "AdvOut", "ApplyServiceSettings",
  525. true);
  526. config_set_default_bool (basicConfig, "AdvOut", "UseRescale", false);
  527. config_set_default_uint (basicConfig, "AdvOut", "TrackIndex", 1);
  528. config_set_default_string(basicConfig, "AdvOut", "Encoder", "obs_x264");
  529. config_set_default_string(basicConfig, "AdvOut", "RecType", "Standard");
  530. config_set_default_string(basicConfig, "AdvOut", "RecFilePath",
  531. GetDefaultVideoSavePath().c_str());
  532. config_set_default_string(basicConfig, "AdvOut", "RecFormat", "flv");
  533. config_set_default_bool (basicConfig, "AdvOut", "RecUseRescale",
  534. false);
  535. config_set_default_uint (basicConfig, "AdvOut", "RecTracks", (1<<0));
  536. config_set_default_string(basicConfig, "AdvOut", "RecEncoder",
  537. "none");
  538. config_set_default_bool (basicConfig, "AdvOut", "FFOutputToFile",
  539. true);
  540. config_set_default_string(basicConfig, "AdvOut", "FFFilePath",
  541. GetDefaultVideoSavePath().c_str());
  542. config_set_default_string(basicConfig, "AdvOut", "FFExtension", "mp4");
  543. config_set_default_uint (basicConfig, "AdvOut", "FFVBitrate", 2500);
  544. config_set_default_bool (basicConfig, "AdvOut", "FFUseRescale",
  545. false);
  546. config_set_default_uint (basicConfig, "AdvOut", "FFABitrate", 160);
  547. config_set_default_uint (basicConfig, "AdvOut", "FFAudioTrack", 1);
  548. config_set_default_uint (basicConfig, "AdvOut", "Track1Bitrate", 160);
  549. config_set_default_uint (basicConfig, "AdvOut", "Track2Bitrate", 160);
  550. config_set_default_uint (basicConfig, "AdvOut", "Track3Bitrate", 160);
  551. config_set_default_uint (basicConfig, "AdvOut", "Track4Bitrate", 160);
  552. config_set_default_uint (basicConfig, "Video", "BaseCX", cx);
  553. config_set_default_uint (basicConfig, "Video", "BaseCY", cy);
  554. config_set_default_bool (basicConfig, "Output", "DelayEnable", false);
  555. config_set_default_uint (basicConfig, "Output", "DelaySec", 20);
  556. config_set_default_bool (basicConfig, "Output", "DelayPreserve", true);
  557. config_set_default_bool (basicConfig, "Output", "Reconnect", true);
  558. config_set_default_uint (basicConfig, "Output", "RetryDelay", 10);
  559. config_set_default_uint (basicConfig, "Output", "MaxRetries", 20);
  560. int i = 0;
  561. uint32_t scale_cx = cx;
  562. uint32_t scale_cy = cy;
  563. /* use a default scaled resolution that has a pixel count no higher
  564. * than 1280x720 */
  565. while (((scale_cx * scale_cy) > (1280 * 720)) && scaled_vals[i] > 0.0) {
  566. double scale = scaled_vals[i++];
  567. scale_cx = uint32_t(double(cx) / scale);
  568. scale_cy = uint32_t(double(cy) / scale);
  569. }
  570. config_set_default_uint (basicConfig, "Video", "OutputCX", scale_cx);
  571. config_set_default_uint (basicConfig, "Video", "OutputCY", scale_cy);
  572. config_set_default_uint (basicConfig, "Video", "FPSType", 0);
  573. config_set_default_string(basicConfig, "Video", "FPSCommon", "30");
  574. config_set_default_uint (basicConfig, "Video", "FPSInt", 30);
  575. config_set_default_uint (basicConfig, "Video", "FPSNum", 30);
  576. config_set_default_uint (basicConfig, "Video", "FPSDen", 1);
  577. config_set_default_string(basicConfig, "Video", "ScaleType", "bicubic");
  578. config_set_default_string(basicConfig, "Video", "ColorFormat", "NV12");
  579. config_set_default_string(basicConfig, "Video", "ColorSpace", "709");
  580. config_set_default_string(basicConfig, "Video", "ColorRange",
  581. "Partial");
  582. config_set_default_uint (basicConfig, "Audio", "SampleRate", 44100);
  583. config_set_default_string(basicConfig, "Audio", "ChannelSetup",
  584. "Stereo");
  585. config_set_default_uint (basicConfig, "Audio", "BufferingTime", 1000);
  586. return true;
  587. }
  588. bool OBSBasic::InitBasicConfig()
  589. {
  590. ProfileScope("OBSBasic::InitBasicConfig");
  591. char configPath[512];
  592. int ret = GetProfilePath(configPath, sizeof(configPath), "");
  593. if (ret <= 0) {
  594. OBSErrorBox(nullptr, "Failed to get profile path");
  595. return false;
  596. }
  597. if (os_mkdir(configPath) == MKDIR_ERROR) {
  598. OBSErrorBox(nullptr, "Failed to create profile path");
  599. return false;
  600. }
  601. ret = GetProfilePath(configPath, sizeof(configPath), "basic.ini");
  602. if (ret <= 0) {
  603. OBSErrorBox(nullptr, "Failed to get base.ini path");
  604. return false;
  605. }
  606. int code = basicConfig.Open(configPath, CONFIG_OPEN_ALWAYS);
  607. if (code != CONFIG_SUCCESS) {
  608. OBSErrorBox(NULL, "Failed to open basic.ini: %d", code);
  609. return false;
  610. }
  611. if (config_get_string(basicConfig, "General", "Name") == nullptr) {
  612. const char *curName = config_get_string(App()->GlobalConfig(),
  613. "Basic", "Profile");
  614. config_set_string(basicConfig, "General", "Name", curName);
  615. basicConfig.SaveSafe("tmp");
  616. }
  617. return InitBasicConfigDefaults();
  618. }
  619. void OBSBasic::InitOBSCallbacks()
  620. {
  621. ProfileScope("OBSBasic::InitOBSCallbacks");
  622. signalHandlers.reserve(signalHandlers.size() + 6);
  623. signalHandlers.emplace_back(obs_get_signal_handler(), "source_add",
  624. OBSBasic::SourceAdded, this);
  625. signalHandlers.emplace_back(obs_get_signal_handler(), "source_remove",
  626. OBSBasic::SourceRemoved, this);
  627. signalHandlers.emplace_back(obs_get_signal_handler(), "channel_change",
  628. OBSBasic::ChannelChanged, this);
  629. signalHandlers.emplace_back(obs_get_signal_handler(), "source_activate",
  630. OBSBasic::SourceActivated, this);
  631. signalHandlers.emplace_back(obs_get_signal_handler(), "source_deactivate",
  632. OBSBasic::SourceDeactivated, this);
  633. signalHandlers.emplace_back(obs_get_signal_handler(), "source_rename",
  634. OBSBasic::SourceRenamed, this);
  635. }
  636. void OBSBasic::InitPrimitives()
  637. {
  638. ProfileScope("OBSBasic::InitPrimitives");
  639. obs_enter_graphics();
  640. gs_render_start(true);
  641. gs_vertex2f(0.0f, 0.0f);
  642. gs_vertex2f(0.0f, 1.0f);
  643. gs_vertex2f(1.0f, 1.0f);
  644. gs_vertex2f(1.0f, 0.0f);
  645. gs_vertex2f(0.0f, 0.0f);
  646. box = gs_render_save();
  647. gs_render_start(true);
  648. for (int i = 0; i <= 360; i += (360/20)) {
  649. float pos = RAD(float(i));
  650. gs_vertex2f(cosf(pos), sinf(pos));
  651. }
  652. circle = gs_render_save();
  653. obs_leave_graphics();
  654. }
  655. void OBSBasic::ResetOutputs()
  656. {
  657. ProfileScope("OBSBasic::ResetOutputs");
  658. const char *mode = config_get_string(basicConfig, "Output", "Mode");
  659. bool advOut = astrcmpi(mode, "Advanced") == 0;
  660. if (!outputHandler || !outputHandler->Active()) {
  661. outputHandler.reset();
  662. outputHandler.reset(advOut ?
  663. CreateAdvancedOutputHandler(this) :
  664. CreateSimpleOutputHandler(this));
  665. } else {
  666. outputHandler->Update();
  667. }
  668. }
  669. #define MAIN_SEPARATOR \
  670. "====================================================================="
  671. void OBSBasic::OBSInit()
  672. {
  673. ProfileScope("OBSBasic::OBSInit");
  674. const char *sceneCollection = config_get_string(App()->GlobalConfig(),
  675. "Basic", "SceneCollectionFile");
  676. char savePath[512];
  677. char fileName[512];
  678. int ret;
  679. if (!sceneCollection)
  680. throw "Failed to get scene collection name";
  681. ret = snprintf(fileName, 512, "obs-studio/basic/scenes/%s.json",
  682. sceneCollection);
  683. if (ret <= 0)
  684. throw "Failed to create scene collection file name";
  685. ret = GetConfigPath(savePath, sizeof(savePath), fileName);
  686. if (ret <= 0)
  687. throw "Failed to get scene collection json file path";
  688. if (!InitBasicConfig())
  689. throw "Failed to load basic.ini";
  690. if (!ResetAudio())
  691. throw "Failed to initialize audio";
  692. ret = ResetVideo();
  693. switch (ret) {
  694. case OBS_VIDEO_MODULE_NOT_FOUND:
  695. throw "Failed to initialize video: Graphics module not found";
  696. case OBS_VIDEO_NOT_SUPPORTED:
  697. throw "Failed to initialize video: Required graphics API "
  698. "functionality not found on these drivers or "
  699. "unavailable on this equipment";
  700. case OBS_VIDEO_INVALID_PARAM:
  701. throw "Failed to initialize video: Invalid parameters";
  702. default:
  703. if (ret != OBS_VIDEO_SUCCESS)
  704. throw "Failed to initialize video: Unspecified error";
  705. }
  706. InitOBSCallbacks();
  707. InitHotkeys();
  708. AddExtraModulePaths();
  709. obs_load_all_modules();
  710. blog(LOG_INFO, MAIN_SEPARATOR);
  711. ResetOutputs();
  712. CreateHotkeys();
  713. if (!InitService())
  714. throw "Failed to initialize service";
  715. InitPrimitives();
  716. {
  717. ProfileScope("OBSBasic::Load");
  718. disableSaving--;
  719. Load(savePath);
  720. disableSaving++;
  721. }
  722. TimedCheckForUpdates();
  723. loaded = true;
  724. bool previewEnabled = config_get_bool(App()->GlobalConfig(),
  725. "BasicWindow", "PreviewEnabled");
  726. if (!previewEnabled)
  727. QMetaObject::invokeMethod(this, "TogglePreview",
  728. Qt::QueuedConnection);
  729. #ifdef _WIN32
  730. uint32_t winVer = GetWindowsVersion();
  731. if (winVer > 0 && winVer < 0x602) {
  732. bool disableAero = config_get_bool(basicConfig, "Video",
  733. "DisableAero");
  734. SetAeroEnabled(!disableAero);
  735. }
  736. #endif
  737. RefreshSceneCollections();
  738. RefreshProfiles();
  739. disableSaving--;
  740. auto addDisplay = [this] (OBSQTDisplay *window)
  741. {
  742. obs_display_add_draw_callback(window->GetDisplay(),
  743. OBSBasic::RenderMain, this);
  744. struct obs_video_info ovi;
  745. if (obs_get_video_info(&ovi))
  746. ResizePreview(ovi.base_width, ovi.base_height);
  747. };
  748. connect(ui->preview, &OBSQTDisplay::DisplayCreated, addDisplay);
  749. show();
  750. }
  751. void OBSBasic::InitHotkeys()
  752. {
  753. ProfileScope("OBSBasic::InitHotkeys");
  754. struct obs_hotkeys_translations t = {};
  755. t.insert = Str("Hotkeys.Insert");
  756. t.del = Str("Hotkeys.Delete");
  757. t.home = Str("Hotkeys.Home");
  758. t.end = Str("Hotkeys.End");
  759. t.page_up = Str("Hotkeys.PageUp");
  760. t.page_down = Str("Hotkeys.PageDown");
  761. t.num_lock = Str("Hotkeys.NumLock");
  762. t.scroll_lock = Str("Hotkeys.ScrollLock");
  763. t.caps_lock = Str("Hotkeys.CapsLock");
  764. t.backspace = Str("Hotkeys.Backspace");
  765. t.tab = Str("Hotkeys.Tab");
  766. t.print = Str("Hotkeys.Print");
  767. t.pause = Str("Hotkeys.Pause");
  768. t.left = Str("Hotkeys.Left");
  769. t.right = Str("Hotkeys.Right");
  770. t.up = Str("Hotkeys.Up");
  771. t.down = Str("Hotkeys.Down");
  772. #ifdef _WIN32
  773. t.meta = Str("Hotkeys.Windows");
  774. #else
  775. t.meta = Str("Hotkeys.Super");
  776. #endif
  777. t.menu = Str("Hotkeys.Menu");
  778. t.space = Str("Hotkeys.Space");
  779. t.numpad_num = Str("Hotkeys.NumpadNum");
  780. t.numpad_multiply = Str("Hotkeys.NumpadMultiply");
  781. t.numpad_divide = Str("Hotkeys.NumpadDivide");
  782. t.numpad_plus = Str("Hotkeys.NumpadAdd");
  783. t.numpad_minus = Str("Hotkeys.NumpadSubtract");
  784. t.numpad_decimal = Str("Hotkeys.NumpadDecimal");
  785. t.apple_keypad_num = Str("Hotkeys.AppleKeypadNum");
  786. t.apple_keypad_multiply = Str("Hotkeys.AppleKeypadMultiply");
  787. t.apple_keypad_divide = Str("Hotkeys.AppleKeypadDivide");
  788. t.apple_keypad_plus = Str("Hotkeys.AppleKeypadAdd");
  789. t.apple_keypad_minus = Str("Hotkeys.AppleKeypadSubtract");
  790. t.apple_keypad_decimal = Str("Hotkeys.AppleKeypadDecimal");
  791. t.apple_keypad_equal = Str("Hotkeys.AppleKeypadEqual");
  792. t.mouse_num = Str("Hotkeys.MouseButton");
  793. obs_hotkeys_set_translations(&t);
  794. obs_hotkeys_set_audio_hotkeys_translations(Str("Mute"), Str("Unmute"),
  795. Str("Push-to-mute"), Str("Push-to-talk"));
  796. obs_hotkeys_set_sceneitem_hotkeys_translations(
  797. Str("SceneItemShow"), Str("SceneItemHide"));
  798. obs_hotkey_enable_callback_rerouting(true);
  799. obs_hotkey_set_callback_routing_func(OBSBasic::HotkeyTriggered, this);
  800. }
  801. void OBSBasic::ProcessHotkey(obs_hotkey_id id, bool pressed)
  802. {
  803. obs_hotkey_trigger_routed_callback(id, pressed);
  804. }
  805. void OBSBasic::HotkeyTriggered(void *data, obs_hotkey_id id, bool pressed)
  806. {
  807. OBSBasic &basic = *static_cast<OBSBasic*>(data);
  808. QMetaObject::invokeMethod(&basic, "ProcessHotkey",
  809. Q_ARG(obs_hotkey_id, id), Q_ARG(bool, pressed));
  810. }
  811. void OBSBasic::CreateHotkeys()
  812. {
  813. ProfileScope("OBSBasic::CreateHotkeys");
  814. auto LoadHotkeyData = [&](const char *name) -> OBSData
  815. {
  816. const char *info = config_get_string(basicConfig,
  817. "Hotkeys", name);
  818. if (!info)
  819. return {};
  820. obs_data_t *data = obs_data_create_from_json(info);
  821. if (!data)
  822. return {};
  823. OBSData res = data;
  824. obs_data_release(data);
  825. return res;
  826. };
  827. auto LoadHotkey = [&](obs_hotkey_id id, const char *name)
  828. {
  829. obs_data_array_t *array =
  830. obs_data_get_array(LoadHotkeyData(name), "bindings");
  831. obs_hotkey_load(id, array);
  832. obs_data_array_release(array);
  833. };
  834. auto LoadHotkeyPair = [&](obs_hotkey_pair_id id, const char *name0,
  835. const char *name1)
  836. {
  837. obs_data_array_t *array0 =
  838. obs_data_get_array(LoadHotkeyData(name0), "bindings");
  839. obs_data_array_t *array1 =
  840. obs_data_get_array(LoadHotkeyData(name1), "bindings");
  841. obs_hotkey_pair_load(id, array0, array1);
  842. obs_data_array_release(array0);
  843. obs_data_array_release(array1);
  844. };
  845. #define MAKE_CALLBACK(pred, method) \
  846. [](void *data, obs_hotkey_pair_id, obs_hotkey_t*, bool pressed) \
  847. { \
  848. OBSBasic &basic = *static_cast<OBSBasic*>(data); \
  849. if (pred && pressed) { \
  850. method(); \
  851. return true; \
  852. } \
  853. return false; \
  854. }
  855. streamingHotkeys = obs_hotkey_pair_register_frontend(
  856. "OBSBasic.StartStreaming",
  857. Str("Basic.Hotkeys.StartStreaming"),
  858. "OBSBasic.StopStreaming",
  859. Str("Basic.Hotkeys.StopStreaming"),
  860. MAKE_CALLBACK(!basic.outputHandler->StreamingActive(),
  861. basic.StartStreaming),
  862. MAKE_CALLBACK(basic.outputHandler->StreamingActive(),
  863. basic.StopStreaming),
  864. this, this);
  865. LoadHotkeyPair(streamingHotkeys,
  866. "OBSBasic.StartStreaming", "OBSBasic.StopStreaming");
  867. auto cb = [] (void *data, obs_hotkey_id, obs_hotkey_t*, bool pressed)
  868. {
  869. OBSBasic &basic = *static_cast<OBSBasic*>(data);
  870. if (basic.outputHandler->StreamingActive() && pressed) {
  871. basic.ForceStopStreaming();
  872. }
  873. };
  874. forceStreamingStopHotkey = obs_hotkey_register_frontend(
  875. "OBSBasic.ForceStopStreaming",
  876. Str("Basic.Main.ForceStopStreaming"),
  877. cb, this);
  878. LoadHotkey(forceStreamingStopHotkey,
  879. "OBSBasic.ForceStopStreaming");
  880. recordingHotkeys = obs_hotkey_pair_register_frontend(
  881. "OBSBasic.StartRecording",
  882. Str("Basic.Hotkeys.StartRecording"),
  883. "OBSBasic.StopRecording",
  884. Str("Basic.Hotkeys.StopRecording"),
  885. MAKE_CALLBACK(!basic.outputHandler->RecordingActive(),
  886. basic.StartRecording),
  887. MAKE_CALLBACK(basic.outputHandler->RecordingActive(),
  888. basic.StopRecording),
  889. this, this);
  890. LoadHotkeyPair(recordingHotkeys,
  891. "OBSBasic.StartRecording", "OBSBasic.StopRecording");
  892. #undef MAKE_CALLBACK
  893. }
  894. void OBSBasic::ClearHotkeys()
  895. {
  896. obs_hotkey_pair_unregister(streamingHotkeys);
  897. obs_hotkey_pair_unregister(recordingHotkeys);
  898. obs_hotkey_unregister(forceStreamingStopHotkey);
  899. }
  900. OBSBasic::~OBSBasic()
  901. {
  902. bool previewEnabled = obs_display_enabled(ui->preview->GetDisplay());
  903. /* XXX: any obs data must be released before calling obs_shutdown.
  904. * currently, we can't automate this with C++ RAII because of the
  905. * delicate nature of obs_shutdown needing to be freed before the UI
  906. * can be freed, and we have no control over the destruction order of
  907. * the Qt UI stuff, so we have to manually clear any references to
  908. * libobs. */
  909. delete cpuUsageTimer;
  910. os_cpu_usage_info_destroy(cpuUsageInfo);
  911. obs_hotkey_set_callback_routing_func(nullptr, nullptr);
  912. ClearHotkeys();
  913. service = nullptr;
  914. outputHandler.reset();
  915. if (interaction)
  916. delete interaction;
  917. if (properties)
  918. delete properties;
  919. if (filters)
  920. delete filters;
  921. if (transformWindow)
  922. delete transformWindow;
  923. if (advAudioWindow)
  924. delete advAudioWindow;
  925. obs_display_remove_draw_callback(ui->preview->GetDisplay(),
  926. OBSBasic::RenderMain, this);
  927. obs_enter_graphics();
  928. gs_vertexbuffer_destroy(box);
  929. gs_vertexbuffer_destroy(circle);
  930. obs_leave_graphics();
  931. /* When shutting down, sometimes source references can get in to the
  932. * event queue, and if we don't forcibly process those events they
  933. * won't get processed until after obs_shutdown has been called. I
  934. * really wish there were a more elegant way to deal with this via C++,
  935. * but Qt doesn't use C++ in a normal way, so you can't really rely on
  936. * normal C++ behavior for your data to be freed in the order that you
  937. * expect or want it to. */
  938. QApplication::sendPostedEvents(this);
  939. config_set_int(App()->GlobalConfig(), "General", "LastVersion",
  940. LIBOBS_API_VER);
  941. QRect lastGeom = normalGeometry();
  942. config_set_int(App()->GlobalConfig(), "BasicWindow", "cx",
  943. lastGeom.width());
  944. config_set_int(App()->GlobalConfig(), "BasicWindow", "cy",
  945. lastGeom.height());
  946. config_set_int(App()->GlobalConfig(), "BasicWindow", "posx",
  947. lastGeom.x());
  948. config_set_int(App()->GlobalConfig(), "BasicWindow", "posy",
  949. lastGeom.y());
  950. config_set_bool(App()->GlobalConfig(), "BasicWindow", "PreviewEnabled",
  951. previewEnabled);
  952. config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
  953. #ifdef _WIN32
  954. uint32_t winVer = GetWindowsVersion();
  955. if (winVer > 0 && winVer < 0x602) {
  956. bool disableAero = config_get_bool(basicConfig, "Video",
  957. "DisableAero");
  958. if (disableAero) {
  959. SetAeroEnabled(true);
  960. }
  961. }
  962. #endif
  963. }
  964. void OBSBasic::SaveProjectNow()
  965. {
  966. if (disableSaving)
  967. return;
  968. projectChanged = true;
  969. SaveProjectDeferred();
  970. }
  971. void OBSBasic::SaveProject()
  972. {
  973. if (disableSaving)
  974. return;
  975. projectChanged = true;
  976. QMetaObject::invokeMethod(this, "SaveProjectDeferred",
  977. Qt::QueuedConnection);
  978. }
  979. void OBSBasic::SaveProjectDeferred()
  980. {
  981. if (disableSaving)
  982. return;
  983. if (!projectChanged)
  984. return;
  985. projectChanged = false;
  986. const char *sceneCollection = config_get_string(App()->GlobalConfig(),
  987. "Basic", "SceneCollectionFile");
  988. char savePath[512];
  989. char fileName[512];
  990. int ret;
  991. if (!sceneCollection)
  992. return;
  993. ret = snprintf(fileName, 512, "obs-studio/basic/scenes/%s.json",
  994. sceneCollection);
  995. if (ret <= 0)
  996. return;
  997. ret = GetConfigPath(savePath, sizeof(savePath), fileName);
  998. if (ret <= 0)
  999. return;
  1000. Save(savePath);
  1001. }
  1002. OBSScene OBSBasic::GetCurrentScene()
  1003. {
  1004. QListWidgetItem *item = ui->scenes->currentItem();
  1005. return item ? GetOBSRef<OBSScene>(item) : nullptr;
  1006. }
  1007. OBSSceneItem OBSBasic::GetSceneItem(QListWidgetItem *item)
  1008. {
  1009. return item ? GetOBSRef<OBSSceneItem>(item) : nullptr;
  1010. }
  1011. OBSSceneItem OBSBasic::GetCurrentSceneItem()
  1012. {
  1013. return GetSceneItem(ui->sources->currentItem());
  1014. }
  1015. void OBSBasic::UpdateSources(OBSScene scene)
  1016. {
  1017. ClearListItems(ui->sources);
  1018. obs_scene_enum_items(scene,
  1019. [] (obs_scene_t *scene, obs_sceneitem_t *item, void *p)
  1020. {
  1021. OBSBasic *window = static_cast<OBSBasic*>(p);
  1022. window->InsertSceneItem(item);
  1023. UNUSED_PARAMETER(scene);
  1024. return true;
  1025. }, this);
  1026. }
  1027. void OBSBasic::InsertSceneItem(obs_sceneitem_t *item)
  1028. {
  1029. obs_source_t *source = obs_sceneitem_get_source(item);
  1030. QListWidgetItem *listItem = new QListWidgetItem();
  1031. SetOBSRef(listItem, OBSSceneItem(item));
  1032. ui->sources->insertItem(0, listItem);
  1033. ui->sources->setCurrentRow(0, QItemSelectionModel::ClearAndSelect);
  1034. SetupVisibilityItem(ui->sources, listItem, item);
  1035. /* if the source was just created, open properties dialog */
  1036. if (sourceSceneRefs[source] == 0 && loaded)
  1037. CreatePropertiesWindow(source);
  1038. }
  1039. void OBSBasic::CreateInteractionWindow(obs_source_t *source)
  1040. {
  1041. if (interaction)
  1042. interaction->close();
  1043. interaction = new OBSBasicInteraction(this, source);
  1044. interaction->Init();
  1045. interaction->setAttribute(Qt::WA_DeleteOnClose, true);
  1046. }
  1047. void OBSBasic::CreatePropertiesWindow(obs_source_t *source)
  1048. {
  1049. if (properties)
  1050. properties->close();
  1051. properties = new OBSBasicProperties(this, source);
  1052. properties->Init();
  1053. properties->setAttribute(Qt::WA_DeleteOnClose, true);
  1054. }
  1055. void OBSBasic::CreateFiltersWindow(obs_source_t *source)
  1056. {
  1057. if (filters)
  1058. filters->close();
  1059. filters = new OBSBasicFilters(this, source);
  1060. filters->Init();
  1061. filters->setAttribute(Qt::WA_DeleteOnClose, true);
  1062. }
  1063. /* Qt callbacks for invokeMethod */
  1064. void OBSBasic::AddScene(OBSSource source)
  1065. {
  1066. const char *name = obs_source_get_name(source);
  1067. obs_scene_t *scene = obs_scene_from_source(source);
  1068. QListWidgetItem *item = new QListWidgetItem(QT_UTF8(name));
  1069. SetOBSRef(item, OBSScene(scene));
  1070. ui->scenes->addItem(item);
  1071. obs_hotkey_register_source(source, "OBSBasic.SelectScene",
  1072. Str("Basic.Hotkeys.SelectScene"),
  1073. [](void *data,
  1074. obs_hotkey_id, obs_hotkey_t*, bool pressed)
  1075. {
  1076. auto potential_source = static_cast<obs_source_t*>(data);
  1077. auto source = obs_source_get_ref(potential_source);
  1078. if (source && pressed)
  1079. obs_set_output_source(0, source);
  1080. obs_source_release(source);
  1081. }, static_cast<obs_source_t*>(source));
  1082. signal_handler_t *handler = obs_source_get_signal_handler(source);
  1083. std::vector<std::shared_ptr<OBSSignal>> handlers{
  1084. std::make_shared<OBSSignal>(handler, "item_add",
  1085. OBSBasic::SceneItemAdded, this),
  1086. std::make_shared<OBSSignal>(handler, "item_remove",
  1087. OBSBasic::SceneItemRemoved, this),
  1088. std::make_shared<OBSSignal>(handler, "item_select",
  1089. OBSBasic::SceneItemSelected, this),
  1090. std::make_shared<OBSSignal>(handler, "item_deselect",
  1091. OBSBasic::SceneItemDeselected, this),
  1092. std::make_shared<OBSSignal>(handler, "reorder",
  1093. OBSBasic::SceneReordered, this),
  1094. };
  1095. item->setData(static_cast<int>(QtDataRole::OBSSignals),
  1096. QVariant::fromValue(handlers));
  1097. /* if the scene already has items (a duplicated scene) add them */
  1098. auto addSceneItem = [this] (obs_sceneitem_t *item)
  1099. {
  1100. AddSceneItem(item);
  1101. };
  1102. using addSceneItem_t = decltype(addSceneItem);
  1103. obs_scene_enum_items(scene,
  1104. [] (obs_scene_t*, obs_sceneitem_t *item, void *param)
  1105. {
  1106. addSceneItem_t *func;
  1107. func = reinterpret_cast<addSceneItem_t*>(param);
  1108. (*func)(item);
  1109. return true;
  1110. }, &addSceneItem);
  1111. SaveProject();
  1112. }
  1113. void OBSBasic::RemoveScene(OBSSource source)
  1114. {
  1115. const char *name = obs_source_get_name(source);
  1116. QListWidgetItem *sel = ui->scenes->currentItem();
  1117. QList<QListWidgetItem*> items = ui->scenes->findItems(QT_UTF8(name),
  1118. Qt::MatchExactly);
  1119. if (sel != nullptr) {
  1120. if (items.contains(sel))
  1121. ClearListItems(ui->sources);
  1122. delete sel;
  1123. }
  1124. auto DeleteSceneRefs = [&](obs_sceneitem_t *si)
  1125. {
  1126. obs_source_t *source = obs_sceneitem_get_source(si);
  1127. sourceSceneRefs[source] -= 1;
  1128. if (!sourceSceneRefs[source]) {
  1129. obs_source_remove(source);
  1130. sourceSceneRefs.erase(source);
  1131. }
  1132. };
  1133. using DeleteSceneRefs_t = decltype(DeleteSceneRefs);
  1134. obs_scene_enum_items(obs_scene_from_source(source),
  1135. [](obs_scene_t *, obs_sceneitem_t *si, void *data)
  1136. {
  1137. (*static_cast<DeleteSceneRefs_t*>(data))(si);
  1138. return true;
  1139. }, static_cast<void*>(&DeleteSceneRefs));
  1140. SaveProject();
  1141. }
  1142. void OBSBasic::AddSceneItem(OBSSceneItem item)
  1143. {
  1144. obs_scene_t *scene = obs_sceneitem_get_scene(item);
  1145. obs_source_t *source = obs_sceneitem_get_source(item);
  1146. if (GetCurrentScene() == scene)
  1147. InsertSceneItem(item);
  1148. sourceSceneRefs[source] = sourceSceneRefs[source] + 1;
  1149. SaveProject();
  1150. }
  1151. void OBSBasic::RemoveSceneItem(OBSSceneItem item)
  1152. {
  1153. obs_scene_t *scene = obs_sceneitem_get_scene(item);
  1154. if (GetCurrentScene() == scene) {
  1155. for (int i = 0; i < ui->sources->count(); i++) {
  1156. QListWidgetItem *listItem = ui->sources->item(i);
  1157. if (GetOBSRef<OBSSceneItem>(listItem) == item) {
  1158. DeleteListItem(ui->sources, listItem);
  1159. break;
  1160. }
  1161. }
  1162. }
  1163. obs_source_t *source = obs_sceneitem_get_source(item);
  1164. int scenes = sourceSceneRefs[source] - 1;
  1165. sourceSceneRefs[source] = scenes;
  1166. if (scenes == 0) {
  1167. obs_source_remove(source);
  1168. sourceSceneRefs.erase(source);
  1169. }
  1170. SaveProject();
  1171. }
  1172. void OBSBasic::UpdateSceneSelection(OBSSource source)
  1173. {
  1174. if (source) {
  1175. obs_scene_t *scene = obs_scene_from_source(source);
  1176. const char *name = obs_source_get_name(source);
  1177. if (!scene)
  1178. return;
  1179. QList<QListWidgetItem*> items =
  1180. ui->scenes->findItems(QT_UTF8(name), Qt::MatchExactly);
  1181. if (items.count()) {
  1182. sceneChanging = true;
  1183. ui->scenes->setCurrentItem(items.first());
  1184. sceneChanging = false;
  1185. UpdateSources(scene);
  1186. }
  1187. }
  1188. }
  1189. static void RenameListValues(QListWidget *listWidget, const QString &newName,
  1190. const QString &prevName)
  1191. {
  1192. QList<QListWidgetItem*> items =
  1193. listWidget->findItems(prevName, Qt::MatchExactly);
  1194. for (int i = 0; i < items.count(); i++)
  1195. items[i]->setText(newName);
  1196. }
  1197. void OBSBasic::RenameSources(QString newName, QString prevName)
  1198. {
  1199. RenameListValues(ui->scenes, newName, prevName);
  1200. for (size_t i = 0; i < volumes.size(); i++) {
  1201. if (volumes[i]->GetName().compare(prevName) == 0)
  1202. volumes[i]->SetName(newName);
  1203. }
  1204. SaveProject();
  1205. }
  1206. void OBSBasic::SelectSceneItem(OBSScene scene, OBSSceneItem item, bool select)
  1207. {
  1208. if (scene != GetCurrentScene() || ignoreSelectionUpdate)
  1209. return;
  1210. for (int i = 0; i < ui->sources->count(); i++) {
  1211. QListWidgetItem *witem = ui->sources->item(i);
  1212. QVariant data =
  1213. witem->data(static_cast<int>(QtDataRole::OBSRef));
  1214. if (!data.canConvert<OBSSceneItem>())
  1215. continue;
  1216. if (item != data.value<OBSSceneItem>())
  1217. continue;
  1218. if (select)
  1219. ui->sources->setCurrentItem(witem,
  1220. QItemSelectionModel::ClearAndSelect);
  1221. break;
  1222. }
  1223. }
  1224. void OBSBasic::GetAudioSourceFilters()
  1225. {
  1226. QAction *action = reinterpret_cast<QAction*>(sender());
  1227. VolControl *vol = action->property("volControl").value<VolControl*>();
  1228. obs_source_t *source = vol->GetSource();
  1229. CreateFiltersWindow(source);
  1230. }
  1231. void OBSBasic::GetAudioSourceProperties()
  1232. {
  1233. QAction *action = reinterpret_cast<QAction*>(sender());
  1234. VolControl *vol = action->property("volControl").value<VolControl*>();
  1235. obs_source_t *source = vol->GetSource();
  1236. CreatePropertiesWindow(source);
  1237. }
  1238. void OBSBasic::VolControlContextMenu()
  1239. {
  1240. VolControl *vol = reinterpret_cast<VolControl*>(sender());
  1241. QAction filtersAction(QTStr("Filters"), this);
  1242. QAction propertiesAction(QTStr("Properties"), this);
  1243. connect(&filtersAction, &QAction::triggered,
  1244. this, &OBSBasic::GetAudioSourceFilters,
  1245. Qt::DirectConnection);
  1246. connect(&propertiesAction, &QAction::triggered,
  1247. this, &OBSBasic::GetAudioSourceProperties,
  1248. Qt::DirectConnection);
  1249. filtersAction.setProperty("volControl",
  1250. QVariant::fromValue<VolControl*>(vol));
  1251. propertiesAction.setProperty("volControl",
  1252. QVariant::fromValue<VolControl*>(vol));
  1253. QMenu popup(this);
  1254. popup.addAction(&filtersAction);
  1255. popup.addAction(&propertiesAction);
  1256. popup.exec(QCursor::pos());
  1257. }
  1258. void OBSBasic::ActivateAudioSource(OBSSource source)
  1259. {
  1260. VolControl *vol = new VolControl(source, true);
  1261. connect(vol, &VolControl::ConfigClicked,
  1262. this, &OBSBasic::VolControlContextMenu);
  1263. volumes.push_back(vol);
  1264. ui->volumeWidgets->layout()->addWidget(vol);
  1265. }
  1266. void OBSBasic::DeactivateAudioSource(OBSSource source)
  1267. {
  1268. for (size_t i = 0; i < volumes.size(); i++) {
  1269. if (volumes[i]->GetSource() == source) {
  1270. delete volumes[i];
  1271. volumes.erase(volumes.begin() + i);
  1272. break;
  1273. }
  1274. }
  1275. }
  1276. bool OBSBasic::QueryRemoveSource(obs_source_t *source)
  1277. {
  1278. const char *name = obs_source_get_name(source);
  1279. QString text = QTStr("ConfirmRemove.Text");
  1280. text.replace("$1", QT_UTF8(name));
  1281. QMessageBox remove_source(this);
  1282. remove_source.setText(text);
  1283. QAbstractButton *Yes = remove_source.addButton(QTStr("Yes"),
  1284. QMessageBox::YesRole);
  1285. remove_source.addButton(QTStr("No"), QMessageBox::NoRole);
  1286. remove_source.setIcon(QMessageBox::Question);
  1287. remove_source.setWindowTitle(QTStr("ConfirmRemove.Title"));
  1288. remove_source.exec();
  1289. return Yes == remove_source.clickedButton();
  1290. }
  1291. #define UPDATE_CHECK_INTERVAL (60*60*24*4) /* 4 days */
  1292. #ifdef UPDATE_SPARKLE
  1293. void init_sparkle_updater(bool update_to_undeployed);
  1294. void trigger_sparkle_update();
  1295. #endif
  1296. void OBSBasic::TimedCheckForUpdates()
  1297. {
  1298. #ifdef UPDATE_SPARKLE
  1299. init_sparkle_updater(config_get_bool(App()->GlobalConfig(), "General",
  1300. "UpdateToUndeployed"));
  1301. #else
  1302. long long lastUpdate = config_get_int(App()->GlobalConfig(), "General",
  1303. "LastUpdateCheck");
  1304. uint32_t lastVersion = config_get_int(App()->GlobalConfig(), "General",
  1305. "LastVersion");
  1306. if (lastVersion < LIBOBS_API_VER) {
  1307. lastUpdate = 0;
  1308. config_set_int(App()->GlobalConfig(), "General",
  1309. "LastUpdateCheck", 0);
  1310. }
  1311. long long t = (long long)time(nullptr);
  1312. long long secs = t - lastUpdate;
  1313. if (secs > UPDATE_CHECK_INTERVAL)
  1314. CheckForUpdates();
  1315. #endif
  1316. }
  1317. void OBSBasic::CheckForUpdates()
  1318. {
  1319. #ifdef UPDATE_SPARKLE
  1320. trigger_sparkle_update();
  1321. #else
  1322. ui->actionCheckForUpdates->setEnabled(false);
  1323. if (updateCheckThread) {
  1324. updateCheckThread->wait();
  1325. delete updateCheckThread;
  1326. }
  1327. RemoteTextThread *thread = new RemoteTextThread(
  1328. "https://obsproject.com/obs2_update/basic.json");
  1329. updateCheckThread = thread;
  1330. connect(thread, &RemoteTextThread::Result,
  1331. this, &OBSBasic::updateFileFinished);
  1332. updateCheckThread->start();
  1333. #endif
  1334. }
  1335. #ifdef __APPLE__
  1336. #define VERSION_ENTRY "mac"
  1337. #elif _WIN32
  1338. #define VERSION_ENTRY "windows"
  1339. #else
  1340. #define VERSION_ENTRY "other"
  1341. #endif
  1342. void OBSBasic::updateFileFinished(const QString &text, const QString &error)
  1343. {
  1344. ui->actionCheckForUpdates->setEnabled(true);
  1345. if (text.isEmpty()) {
  1346. blog(LOG_WARNING, "Update check failed: %s", QT_TO_UTF8(error));
  1347. return;
  1348. }
  1349. obs_data_t *returnData = obs_data_create_from_json(QT_TO_UTF8(text));
  1350. obs_data_t *versionData = obs_data_get_obj(returnData, VERSION_ENTRY);
  1351. const char *description = obs_data_get_string(returnData,
  1352. "description");
  1353. const char *download = obs_data_get_string(versionData, "download");
  1354. if (returnData && versionData && description && download) {
  1355. long major = obs_data_get_int(versionData, "major");
  1356. long minor = obs_data_get_int(versionData, "minor");
  1357. long patch = obs_data_get_int(versionData, "patch");
  1358. long version = MAKE_SEMANTIC_VERSION(major, minor, patch);
  1359. blog(LOG_INFO, "Update check: last known remote version "
  1360. "is %ld.%ld.%ld",
  1361. major, minor, patch);
  1362. if (version > LIBOBS_API_VER) {
  1363. QString str = QTStr("UpdateAvailable.Text");
  1364. QMessageBox messageBox(this);
  1365. str = str.arg(QString::number(major),
  1366. QString::number(minor),
  1367. QString::number(patch),
  1368. download);
  1369. messageBox.setWindowTitle(QTStr("UpdateAvailable"));
  1370. messageBox.setTextFormat(Qt::RichText);
  1371. messageBox.setText(str);
  1372. messageBox.setInformativeText(QT_UTF8(description));
  1373. messageBox.exec();
  1374. long long t = (long long)time(nullptr);
  1375. config_set_int(App()->GlobalConfig(), "General",
  1376. "LastUpdateCheck", t);
  1377. config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
  1378. }
  1379. } else {
  1380. blog(LOG_WARNING, "Bad JSON file received from server");
  1381. }
  1382. obs_data_release(versionData);
  1383. obs_data_release(returnData);
  1384. }
  1385. void OBSBasic::DuplicateSelectedScene()
  1386. {
  1387. OBSScene curScene = GetCurrentScene();
  1388. if (!curScene)
  1389. return;
  1390. OBSSource curSceneSource = obs_scene_get_source(curScene);
  1391. QString format{obs_source_get_name(curSceneSource)};
  1392. format += " %1";
  1393. int i = 2;
  1394. QString placeHolderText = format.arg(i);
  1395. obs_source_t *source = nullptr;
  1396. while ((source = obs_get_source_by_name(QT_TO_UTF8(placeHolderText)))) {
  1397. obs_source_release(source);
  1398. placeHolderText = format.arg(++i);
  1399. }
  1400. for (;;) {
  1401. string name;
  1402. bool accepted = NameDialog::AskForName(this,
  1403. QTStr("Basic.Main.AddSceneDlg.Title"),
  1404. QTStr("Basic.Main.AddSceneDlg.Text"),
  1405. name,
  1406. placeHolderText);
  1407. if (!accepted)
  1408. return;
  1409. if (name.empty()) {
  1410. QMessageBox::information(this,
  1411. QTStr("NoNameEntered.Title"),
  1412. QTStr("NoNameEntered.Text"));
  1413. continue;
  1414. }
  1415. obs_source_t *source = obs_get_source_by_name(name.c_str());
  1416. if (source) {
  1417. QMessageBox::information(this,
  1418. QTStr("NameExists.Title"),
  1419. QTStr("NameExists.Text"));
  1420. obs_source_release(source);
  1421. continue;
  1422. }
  1423. obs_scene_t *scene = obs_scene_duplicate(curScene,
  1424. name.c_str());
  1425. source = obs_scene_get_source(scene);
  1426. obs_add_source(source);
  1427. obs_scene_release(scene);
  1428. obs_set_output_source(0, source);
  1429. return;
  1430. }
  1431. }
  1432. void OBSBasic::RemoveSelectedScene()
  1433. {
  1434. OBSScene scene = GetCurrentScene();
  1435. if (scene) {
  1436. obs_source_t *source = obs_scene_get_source(scene);
  1437. if (QueryRemoveSource(source))
  1438. obs_source_remove(source);
  1439. }
  1440. }
  1441. void OBSBasic::RemoveSelectedSceneItem()
  1442. {
  1443. OBSSceneItem item = GetCurrentSceneItem();
  1444. if (item) {
  1445. obs_source_t *source = obs_sceneitem_get_source(item);
  1446. if (QueryRemoveSource(source))
  1447. obs_sceneitem_remove(item);
  1448. }
  1449. }
  1450. struct ReorderInfo {
  1451. int idx = 0;
  1452. OBSBasic *window;
  1453. inline ReorderInfo(OBSBasic *window_) : window(window_) {}
  1454. };
  1455. void OBSBasic::ReorderSceneItem(obs_sceneitem_t *item, size_t idx)
  1456. {
  1457. int count = ui->sources->count();
  1458. int idx_inv = count - (int)idx - 1;
  1459. for (int i = 0; i < count; i++) {
  1460. QListWidgetItem *listItem = ui->sources->item(i);
  1461. OBSSceneItem sceneItem = GetOBSRef<OBSSceneItem>(listItem);
  1462. if (sceneItem == item) {
  1463. if ((int)idx_inv != i) {
  1464. bool sel = (ui->sources->currentRow() == i);
  1465. listItem = TakeListItem(ui->sources, i);
  1466. if (listItem) {
  1467. ui->sources->insertItem(idx_inv,
  1468. listItem);
  1469. SetupVisibilityItem(ui->sources,
  1470. listItem, item);
  1471. if (sel)
  1472. ui->sources->setCurrentRow(
  1473. idx_inv);
  1474. }
  1475. }
  1476. break;
  1477. }
  1478. }
  1479. }
  1480. void OBSBasic::ReorderSources(OBSScene scene)
  1481. {
  1482. ReorderInfo info(this);
  1483. if (scene != GetCurrentScene() || ui->sources->IgnoreReorder())
  1484. return;
  1485. obs_scene_enum_items(scene,
  1486. [] (obs_scene_t*, obs_sceneitem_t *item, void *p)
  1487. {
  1488. ReorderInfo *info =
  1489. reinterpret_cast<ReorderInfo*>(p);
  1490. info->window->ReorderSceneItem(item,
  1491. info->idx++);
  1492. return true;
  1493. }, &info);
  1494. SaveProject();
  1495. }
  1496. /* OBS Callbacks */
  1497. void OBSBasic::SceneReordered(void *data, calldata_t *params)
  1498. {
  1499. OBSBasic *window = static_cast<OBSBasic*>(data);
  1500. obs_scene_t *scene = (obs_scene_t*)calldata_ptr(params, "scene");
  1501. QMetaObject::invokeMethod(window, "ReorderSources",
  1502. Q_ARG(OBSScene, OBSScene(scene)));
  1503. }
  1504. void OBSBasic::SceneItemAdded(void *data, calldata_t *params)
  1505. {
  1506. OBSBasic *window = static_cast<OBSBasic*>(data);
  1507. obs_sceneitem_t *item = (obs_sceneitem_t*)calldata_ptr(params, "item");
  1508. QMetaObject::invokeMethod(window, "AddSceneItem",
  1509. Q_ARG(OBSSceneItem, OBSSceneItem(item)));
  1510. }
  1511. void OBSBasic::SceneItemRemoved(void *data, calldata_t *params)
  1512. {
  1513. OBSBasic *window = static_cast<OBSBasic*>(data);
  1514. obs_sceneitem_t *item = (obs_sceneitem_t*)calldata_ptr(params, "item");
  1515. QMetaObject::invokeMethod(window, "RemoveSceneItem",
  1516. Q_ARG(OBSSceneItem, OBSSceneItem(item)));
  1517. }
  1518. void OBSBasic::SceneItemSelected(void *data, calldata_t *params)
  1519. {
  1520. OBSBasic *window = static_cast<OBSBasic*>(data);
  1521. obs_scene_t *scene = (obs_scene_t*)calldata_ptr(params, "scene");
  1522. obs_sceneitem_t *item = (obs_sceneitem_t*)calldata_ptr(params, "item");
  1523. QMetaObject::invokeMethod(window, "SelectSceneItem",
  1524. Q_ARG(OBSScene, scene), Q_ARG(OBSSceneItem, item),
  1525. Q_ARG(bool, true));
  1526. }
  1527. void OBSBasic::SceneItemDeselected(void *data, calldata_t *params)
  1528. {
  1529. OBSBasic *window = static_cast<OBSBasic*>(data);
  1530. obs_scene_t *scene = (obs_scene_t*)calldata_ptr(params, "scene");
  1531. obs_sceneitem_t *item = (obs_sceneitem_t*)calldata_ptr(params, "item");
  1532. QMetaObject::invokeMethod(window, "SelectSceneItem",
  1533. Q_ARG(OBSScene, scene), Q_ARG(OBSSceneItem, item),
  1534. Q_ARG(bool, false));
  1535. }
  1536. void OBSBasic::SourceAdded(void *data, calldata_t *params)
  1537. {
  1538. OBSBasic *window = static_cast<OBSBasic*>(data);
  1539. obs_source_t *source = (obs_source_t*)calldata_ptr(params, "source");
  1540. if (obs_scene_from_source(source) != NULL)
  1541. QMetaObject::invokeMethod(window,
  1542. "AddScene",
  1543. Q_ARG(OBSSource, OBSSource(source)));
  1544. }
  1545. void OBSBasic::SourceRemoved(void *data, calldata_t *params)
  1546. {
  1547. obs_source_t *source = (obs_source_t*)calldata_ptr(params, "source");
  1548. if (obs_scene_from_source(source) != NULL)
  1549. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  1550. "RemoveScene",
  1551. Q_ARG(OBSSource, OBSSource(source)));
  1552. }
  1553. void OBSBasic::SourceActivated(void *data, calldata_t *params)
  1554. {
  1555. obs_source_t *source = (obs_source_t*)calldata_ptr(params, "source");
  1556. uint32_t flags = obs_source_get_output_flags(source);
  1557. if (flags & OBS_SOURCE_AUDIO)
  1558. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  1559. "ActivateAudioSource",
  1560. Q_ARG(OBSSource, OBSSource(source)));
  1561. }
  1562. void OBSBasic::SourceDeactivated(void *data, calldata_t *params)
  1563. {
  1564. obs_source_t *source = (obs_source_t*)calldata_ptr(params, "source");
  1565. uint32_t flags = obs_source_get_output_flags(source);
  1566. if (flags & OBS_SOURCE_AUDIO)
  1567. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  1568. "DeactivateAudioSource",
  1569. Q_ARG(OBSSource, OBSSource(source)));
  1570. }
  1571. void OBSBasic::SourceRenamed(void *data, calldata_t *params)
  1572. {
  1573. const char *newName = calldata_string(params, "new_name");
  1574. const char *prevName = calldata_string(params, "prev_name");
  1575. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  1576. "RenameSources",
  1577. Q_ARG(QString, QT_UTF8(newName)),
  1578. Q_ARG(QString, QT_UTF8(prevName)));
  1579. }
  1580. void OBSBasic::ChannelChanged(void *data, calldata_t *params)
  1581. {
  1582. obs_source_t *source = (obs_source_t*)calldata_ptr(params, "source");
  1583. uint32_t channel = (uint32_t)calldata_int(params, "channel");
  1584. if (channel == 0)
  1585. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  1586. "UpdateSceneSelection",
  1587. Q_ARG(OBSSource, OBSSource(source)));
  1588. }
  1589. void OBSBasic::DrawBackdrop(float cx, float cy)
  1590. {
  1591. if (!box)
  1592. return;
  1593. gs_effect_t *solid = obs_get_solid_effect();
  1594. gs_eparam_t *color = gs_effect_get_param_by_name(solid, "color");
  1595. gs_technique_t *tech = gs_effect_get_technique(solid, "Solid");
  1596. vec4 colorVal;
  1597. vec4_set(&colorVal, 0.0f, 0.0f, 0.0f, 1.0f);
  1598. gs_effect_set_vec4(color, &colorVal);
  1599. gs_technique_begin(tech);
  1600. gs_technique_begin_pass(tech, 0);
  1601. gs_matrix_push();
  1602. gs_matrix_identity();
  1603. gs_matrix_scale3f(float(cx), float(cy), 1.0f);
  1604. gs_load_vertexbuffer(box);
  1605. gs_draw(GS_TRISTRIP, 0, 0);
  1606. gs_matrix_pop();
  1607. gs_technique_end_pass(tech);
  1608. gs_technique_end(tech);
  1609. gs_load_vertexbuffer(nullptr);
  1610. }
  1611. void OBSBasic::RenderMain(void *data, uint32_t cx, uint32_t cy)
  1612. {
  1613. OBSBasic *window = static_cast<OBSBasic*>(data);
  1614. obs_video_info ovi;
  1615. obs_get_video_info(&ovi);
  1616. window->previewCX = int(window->previewScale * float(ovi.base_width));
  1617. window->previewCY = int(window->previewScale * float(ovi.base_height));
  1618. gs_viewport_push();
  1619. gs_projection_push();
  1620. /* --------------------------------------- */
  1621. gs_ortho(0.0f, float(ovi.base_width), 0.0f, float(ovi.base_height),
  1622. -100.0f, 100.0f);
  1623. gs_set_viewport(window->previewX, window->previewY,
  1624. window->previewCX, window->previewCY);
  1625. window->DrawBackdrop(float(ovi.base_width), float(ovi.base_height));
  1626. obs_render_main_view();
  1627. gs_load_vertexbuffer(nullptr);
  1628. /* --------------------------------------- */
  1629. QSize previewSize = GetPixelSize(window->ui->preview);
  1630. float right = float(previewSize.width()) - window->previewX;
  1631. float bottom = float(previewSize.height()) - window->previewY;
  1632. gs_ortho(-window->previewX, right,
  1633. -window->previewY, bottom,
  1634. -100.0f, 100.0f);
  1635. gs_reset_viewport();
  1636. window->ui->preview->DrawSceneEditing();
  1637. /* --------------------------------------- */
  1638. gs_projection_pop();
  1639. gs_viewport_pop();
  1640. UNUSED_PARAMETER(cx);
  1641. UNUSED_PARAMETER(cy);
  1642. }
  1643. /* Main class functions */
  1644. obs_service_t *OBSBasic::GetService()
  1645. {
  1646. if (!service) {
  1647. service = obs_service_create("rtmp_common", NULL, NULL,
  1648. nullptr);
  1649. obs_service_release(service);
  1650. }
  1651. return service;
  1652. }
  1653. void OBSBasic::SetService(obs_service_t *newService)
  1654. {
  1655. if (newService)
  1656. service = newService;
  1657. }
  1658. bool OBSBasic::StreamingActive()
  1659. {
  1660. if (!outputHandler)
  1661. return false;
  1662. return outputHandler->StreamingActive();
  1663. }
  1664. #ifdef _WIN32
  1665. #define IS_WIN32 1
  1666. #else
  1667. #define IS_WIN32 0
  1668. #endif
  1669. static inline int AttemptToResetVideo(struct obs_video_info *ovi)
  1670. {
  1671. return obs_reset_video(ovi);
  1672. }
  1673. static inline enum obs_scale_type GetScaleType(ConfigFile &basicConfig)
  1674. {
  1675. const char *scaleTypeStr = config_get_string(basicConfig,
  1676. "Video", "ScaleType");
  1677. if (astrcmpi(scaleTypeStr, "bilinear") == 0)
  1678. return OBS_SCALE_BILINEAR;
  1679. else if (astrcmpi(scaleTypeStr, "lanczos") == 0)
  1680. return OBS_SCALE_LANCZOS;
  1681. else
  1682. return OBS_SCALE_BICUBIC;
  1683. }
  1684. static inline enum video_format GetVideoFormatFromName(const char *name)
  1685. {
  1686. if (astrcmpi(name, "I420") == 0)
  1687. return VIDEO_FORMAT_I420;
  1688. else if (astrcmpi(name, "NV12") == 0)
  1689. return VIDEO_FORMAT_NV12;
  1690. else if (astrcmpi(name, "I444") == 0)
  1691. return VIDEO_FORMAT_I444;
  1692. #if 0 //currently unsupported
  1693. else if (astrcmpi(name, "YVYU") == 0)
  1694. return VIDEO_FORMAT_YVYU;
  1695. else if (astrcmpi(name, "YUY2") == 0)
  1696. return VIDEO_FORMAT_YUY2;
  1697. else if (astrcmpi(name, "UYVY") == 0)
  1698. return VIDEO_FORMAT_UYVY;
  1699. #endif
  1700. else
  1701. return VIDEO_FORMAT_RGBA;
  1702. }
  1703. int OBSBasic::ResetVideo()
  1704. {
  1705. ProfileScope("OBSBasic::ResetVideo");
  1706. struct obs_video_info ovi;
  1707. int ret;
  1708. GetConfigFPS(ovi.fps_num, ovi.fps_den);
  1709. const char *colorFormat = config_get_string(basicConfig, "Video",
  1710. "ColorFormat");
  1711. const char *colorSpace = config_get_string(basicConfig, "Video",
  1712. "ColorSpace");
  1713. const char *colorRange = config_get_string(basicConfig, "Video",
  1714. "ColorRange");
  1715. ovi.graphics_module = App()->GetRenderModule();
  1716. ovi.base_width = (uint32_t)config_get_uint(basicConfig,
  1717. "Video", "BaseCX");
  1718. ovi.base_height = (uint32_t)config_get_uint(basicConfig,
  1719. "Video", "BaseCY");
  1720. ovi.output_width = (uint32_t)config_get_uint(basicConfig,
  1721. "Video", "OutputCX");
  1722. ovi.output_height = (uint32_t)config_get_uint(basicConfig,
  1723. "Video", "OutputCY");
  1724. ovi.output_format = GetVideoFormatFromName(colorFormat);
  1725. ovi.colorspace = astrcmpi(colorSpace, "601") == 0 ?
  1726. VIDEO_CS_601 : VIDEO_CS_709;
  1727. ovi.range = astrcmpi(colorRange, "Full") == 0 ?
  1728. VIDEO_RANGE_FULL : VIDEO_RANGE_PARTIAL;
  1729. ovi.adapter = 0;
  1730. ovi.gpu_conversion = true;
  1731. ovi.scale_type = GetScaleType(basicConfig);
  1732. ret = AttemptToResetVideo(&ovi);
  1733. if (IS_WIN32 && ret != OBS_VIDEO_SUCCESS) {
  1734. /* Try OpenGL if DirectX fails on windows */
  1735. if (astrcmpi(ovi.graphics_module, DL_OPENGL) != 0) {
  1736. blog(LOG_WARNING, "Failed to initialize obs video (%d) "
  1737. "with graphics_module='%s', retrying "
  1738. "with graphics_module='%s'",
  1739. ret, ovi.graphics_module,
  1740. DL_OPENGL);
  1741. ovi.graphics_module = DL_OPENGL;
  1742. ret = AttemptToResetVideo(&ovi);
  1743. }
  1744. } else if (ret == OBS_VIDEO_SUCCESS) {
  1745. ResizePreview(ovi.base_width, ovi.base_height);
  1746. }
  1747. return ret;
  1748. }
  1749. bool OBSBasic::ResetAudio()
  1750. {
  1751. ProfileScope("OBSBasic::ResetAudio");
  1752. struct obs_audio_info ai;
  1753. ai.samples_per_sec = config_get_uint(basicConfig, "Audio",
  1754. "SampleRate");
  1755. const char *channelSetupStr = config_get_string(basicConfig,
  1756. "Audio", "ChannelSetup");
  1757. if (strcmp(channelSetupStr, "Mono") == 0)
  1758. ai.speakers = SPEAKERS_MONO;
  1759. else
  1760. ai.speakers = SPEAKERS_STEREO;
  1761. ai.buffer_ms = config_get_uint(basicConfig, "Audio", "BufferingTime");
  1762. return obs_reset_audio(&ai);
  1763. }
  1764. void OBSBasic::ResetAudioDevice(const char *sourceId, const char *deviceId,
  1765. const char *deviceDesc, int channel)
  1766. {
  1767. obs_source_t *source;
  1768. obs_data_t *settings;
  1769. bool same = false;
  1770. source = obs_get_output_source(channel);
  1771. if (source) {
  1772. settings = obs_source_get_settings(source);
  1773. const char *curId = obs_data_get_string(settings, "device_id");
  1774. same = (strcmp(curId, deviceId) == 0);
  1775. obs_data_release(settings);
  1776. obs_source_release(source);
  1777. }
  1778. if (!same)
  1779. obs_set_output_source(channel, nullptr);
  1780. if (!same && strcmp(deviceId, "disabled") != 0) {
  1781. obs_data_t *settings = obs_data_create();
  1782. obs_data_set_string(settings, "device_id", deviceId);
  1783. source = obs_source_create(OBS_SOURCE_TYPE_INPUT,
  1784. sourceId, deviceDesc, settings, nullptr);
  1785. obs_data_release(settings);
  1786. obs_set_output_source(channel, source);
  1787. obs_source_release(source);
  1788. }
  1789. }
  1790. void OBSBasic::ResizePreview(uint32_t cx, uint32_t cy)
  1791. {
  1792. QSize targetSize;
  1793. /* resize preview panel to fix to the top section of the window */
  1794. targetSize = GetPixelSize(ui->preview);
  1795. GetScaleAndCenterPos(int(cx), int(cy),
  1796. targetSize.width() - PREVIEW_EDGE_SIZE * 2,
  1797. targetSize.height() - PREVIEW_EDGE_SIZE * 2,
  1798. previewX, previewY, previewScale);
  1799. previewX += float(PREVIEW_EDGE_SIZE);
  1800. previewY += float(PREVIEW_EDGE_SIZE);
  1801. }
  1802. void OBSBasic::CloseDialogs()
  1803. {
  1804. QList<QDialog*> childDialogs = this->findChildren<QDialog *>();
  1805. if (!childDialogs.isEmpty()) {
  1806. for (int i = 0; i < childDialogs.size(); ++i) {
  1807. childDialogs.at(i)->close();
  1808. }
  1809. }
  1810. for (QPointer<QWidget> &projector : projectors) {
  1811. delete projector;
  1812. projector.clear();
  1813. }
  1814. }
  1815. void OBSBasic::ClearSceneData()
  1816. {
  1817. disableSaving++;
  1818. CloseDialogs();
  1819. ClearVolumeControls();
  1820. ClearListItems(ui->scenes);
  1821. ClearListItems(ui->sources);
  1822. obs_set_output_source(0, nullptr);
  1823. obs_set_output_source(1, nullptr);
  1824. obs_set_output_source(2, nullptr);
  1825. obs_set_output_source(3, nullptr);
  1826. obs_set_output_source(4, nullptr);
  1827. obs_set_output_source(5, nullptr);
  1828. auto cb = [](void *unused, obs_source_t *source)
  1829. {
  1830. obs_source_remove(source);
  1831. UNUSED_PARAMETER(unused);
  1832. return true;
  1833. };
  1834. obs_enum_sources(cb, nullptr);
  1835. sourceSceneRefs.clear();
  1836. disableSaving--;
  1837. blog(LOG_INFO, "All scene data cleared");
  1838. blog(LOG_INFO, "------------------------------------------------");
  1839. }
  1840. void OBSBasic::closeEvent(QCloseEvent *event)
  1841. {
  1842. if (outputHandler && outputHandler->Active()) {
  1843. QMessageBox::StandardButton button = QMessageBox::question(
  1844. this, QTStr("ConfirmExit.Title"),
  1845. QTStr("ConfirmExit.Text"));
  1846. if (button == QMessageBox::No) {
  1847. event->ignore();
  1848. return;
  1849. }
  1850. }
  1851. QWidget::closeEvent(event);
  1852. if (!event->isAccepted())
  1853. return;
  1854. if (updateCheckThread)
  1855. updateCheckThread->wait();
  1856. if (logUploadThread)
  1857. logUploadThread->wait();
  1858. signalHandlers.clear();
  1859. SaveProjectNow();
  1860. disableSaving++;
  1861. /* Clear all scene data (dialogs, widgets, widget sub-items, scenes,
  1862. * sources, etc) so that all references are released before shutdown */
  1863. ClearSceneData();
  1864. }
  1865. void OBSBasic::changeEvent(QEvent *event)
  1866. {
  1867. /* TODO */
  1868. UNUSED_PARAMETER(event);
  1869. }
  1870. void OBSBasic::resizeEvent(QResizeEvent *event)
  1871. {
  1872. struct obs_video_info ovi;
  1873. if (obs_get_video_info(&ovi))
  1874. ResizePreview(ovi.base_width, ovi.base_height);
  1875. OBSMainWindow::resizeEvent(event);
  1876. }
  1877. void OBSBasic::on_actionShow_Recordings_triggered()
  1878. {
  1879. const char *mode = config_get_string(basicConfig, "Output", "Mode");
  1880. const char *path = strcmp(mode, "Advanced") ?
  1881. config_get_string(basicConfig, "SimpleOutput", "FilePath") :
  1882. config_get_string(basicConfig, "AdvOut", "RecFilePath");
  1883. QDesktopServices::openUrl(QUrl::fromLocalFile(path));
  1884. }
  1885. void OBSBasic::on_actionRemux_triggered()
  1886. {
  1887. const char *mode = config_get_string(basicConfig, "Output", "Mode");
  1888. const char *path = strcmp(mode, "Advanced") ?
  1889. config_get_string(basicConfig, "SimpleOutput", "FilePath") :
  1890. config_get_string(basicConfig, "AdvOut", "RecFilePath");
  1891. OBSRemux remux(path, this);
  1892. remux.exec();
  1893. }
  1894. void OBSBasic::on_action_Settings_triggered()
  1895. {
  1896. OBSBasicSettings settings(this);
  1897. settings.exec();
  1898. }
  1899. void OBSBasic::on_actionAdvAudioProperties_triggered()
  1900. {
  1901. if (advAudioWindow != nullptr) {
  1902. advAudioWindow->raise();
  1903. return;
  1904. }
  1905. advAudioWindow = new OBSBasicAdvAudio(this);
  1906. advAudioWindow->show();
  1907. advAudioWindow->setAttribute(Qt::WA_DeleteOnClose, true);
  1908. connect(advAudioWindow, SIGNAL(destroyed()),
  1909. this, SLOT(on_advAudioProps_destroyed()));
  1910. }
  1911. void OBSBasic::on_advAudioProps_clicked()
  1912. {
  1913. on_actionAdvAudioProperties_triggered();
  1914. }
  1915. void OBSBasic::on_advAudioProps_destroyed()
  1916. {
  1917. advAudioWindow = nullptr;
  1918. }
  1919. void OBSBasic::on_scenes_currentItemChanged(QListWidgetItem *current,
  1920. QListWidgetItem *prev)
  1921. {
  1922. obs_source_t *source = NULL;
  1923. if (sceneChanging)
  1924. return;
  1925. if (current) {
  1926. obs_scene_t *scene;
  1927. scene = GetOBSRef<OBSScene>(current);
  1928. source = obs_scene_get_source(scene);
  1929. }
  1930. /* TODO: allow transitions */
  1931. obs_set_output_source(0, source);
  1932. UNUSED_PARAMETER(prev);
  1933. }
  1934. void OBSBasic::EditSceneName()
  1935. {
  1936. QListWidgetItem *item = ui->scenes->currentItem();
  1937. Qt::ItemFlags flags = item->flags();
  1938. item->setFlags(flags | Qt::ItemIsEditable);
  1939. ui->scenes->editItem(item);
  1940. item->setFlags(flags);
  1941. }
  1942. static void AddProjectorMenuMonitors(QMenu *parent, QObject *target,
  1943. const char *slot)
  1944. {
  1945. QAction *action;
  1946. std::vector<MonitorInfo> monitors;
  1947. GetMonitors(monitors);
  1948. for (int i = 0; (size_t)i < monitors.size(); i++) {
  1949. const MonitorInfo &monitor = monitors[i];
  1950. QString str = QString("%1 %2: %3x%4 @ %5,%6").
  1951. arg(QTStr("Display"),
  1952. QString::number(i),
  1953. QString::number((int)monitor.cx),
  1954. QString::number((int)monitor.cy),
  1955. QString::number((int)monitor.x),
  1956. QString::number((int)monitor.y));
  1957. action = parent->addAction(str, target, slot);
  1958. action->setProperty("monitor", i);
  1959. }
  1960. }
  1961. void OBSBasic::on_scenes_customContextMenuRequested(const QPoint &pos)
  1962. {
  1963. QListWidgetItem *item = ui->scenes->itemAt(pos);
  1964. QPointer<QMenu> sceneProjectorMenu;
  1965. QMenu popup(this);
  1966. QMenu order(QTStr("Basic.MainMenu.Edit.Order"), this);
  1967. popup.addAction(QTStr("Add"),
  1968. this, SLOT(on_actionAddScene_triggered()));
  1969. if (item) {
  1970. popup.addSeparator();
  1971. popup.addAction(QTStr("Duplicate"),
  1972. this, SLOT(DuplicateSelectedScene()));
  1973. popup.addAction(QTStr("Rename"),
  1974. this, SLOT(EditSceneName()));
  1975. popup.addAction(QTStr("Remove"),
  1976. this, SLOT(RemoveSelectedScene()),
  1977. DeleteKeys.front());
  1978. popup.addSeparator();
  1979. order.addAction(QTStr("Basic.MainMenu.Edit.Order.MoveUp"),
  1980. this, SLOT(on_actionSceneUp_triggered()));
  1981. order.addAction(QTStr("Basic.MainMenu.Edit.Order.MoveDown"),
  1982. this, SLOT(on_actionSceneDown_triggered()));
  1983. order.addSeparator();
  1984. order.addAction(QTStr("Basic.MainMenu.Edit.Order.MoveToTop"),
  1985. this, SLOT(MoveSceneToTop()));
  1986. order.addAction(QTStr("Basic.MainMenu.Edit.Order.MoveToBottom"),
  1987. this, SLOT(MoveSceneToBottom()));
  1988. popup.addMenu(&order);
  1989. popup.addSeparator();
  1990. sceneProjectorMenu = new QMenu(QTStr("SceneProjector"));
  1991. AddProjectorMenuMonitors(sceneProjectorMenu, this,
  1992. SLOT(OpenSceneProjector()));
  1993. popup.addMenu(sceneProjectorMenu);
  1994. popup.addSeparator();
  1995. popup.addAction(QTStr("Filters"), this,
  1996. SLOT(OpenSceneFilters()));
  1997. }
  1998. popup.exec(QCursor::pos());
  1999. }
  2000. void OBSBasic::on_actionAddScene_triggered()
  2001. {
  2002. string name;
  2003. QString format{QTStr("Basic.Main.DefaultSceneName.Text")};
  2004. int i = 1;
  2005. QString placeHolderText = format.arg(i);
  2006. obs_source_t *source = nullptr;
  2007. while ((source = obs_get_source_by_name(QT_TO_UTF8(placeHolderText)))) {
  2008. obs_source_release(source);
  2009. placeHolderText = format.arg(++i);
  2010. }
  2011. bool accepted = NameDialog::AskForName(this,
  2012. QTStr("Basic.Main.AddSceneDlg.Title"),
  2013. QTStr("Basic.Main.AddSceneDlg.Text"),
  2014. name,
  2015. placeHolderText);
  2016. if (accepted) {
  2017. if (name.empty()) {
  2018. QMessageBox::information(this,
  2019. QTStr("NoNameEntered.Title"),
  2020. QTStr("NoNameEntered.Text"));
  2021. on_actionAddScene_triggered();
  2022. return;
  2023. }
  2024. obs_source_t *source = obs_get_source_by_name(name.c_str());
  2025. if (source) {
  2026. QMessageBox::information(this,
  2027. QTStr("NameExists.Title"),
  2028. QTStr("NameExists.Text"));
  2029. obs_source_release(source);
  2030. on_actionAddScene_triggered();
  2031. return;
  2032. }
  2033. obs_scene_t *scene = obs_scene_create(name.c_str());
  2034. source = obs_scene_get_source(scene);
  2035. obs_add_source(source);
  2036. obs_scene_release(scene);
  2037. obs_set_output_source(0, source);
  2038. }
  2039. }
  2040. void OBSBasic::on_actionRemoveScene_triggered()
  2041. {
  2042. OBSScene scene = GetCurrentScene();
  2043. obs_source_t *source = obs_scene_get_source(scene);
  2044. if (source && QueryRemoveSource(source))
  2045. obs_source_remove(source);
  2046. }
  2047. void OBSBasic::ChangeSceneIndex(bool relative, int offset, int invalidIdx)
  2048. {
  2049. int idx = ui->scenes->currentRow();
  2050. if (idx == -1 || idx == invalidIdx)
  2051. return;
  2052. sceneChanging = true;
  2053. QListWidgetItem *item = ui->scenes->takeItem(idx);
  2054. if (!relative)
  2055. idx = 0;
  2056. ui->scenes->insertItem(idx + offset, item);
  2057. ui->scenes->setCurrentRow(idx + offset);
  2058. item->setSelected(true);
  2059. sceneChanging = false;
  2060. }
  2061. void OBSBasic::on_actionSceneUp_triggered()
  2062. {
  2063. ChangeSceneIndex(true, -1, 0);
  2064. }
  2065. void OBSBasic::on_actionSceneDown_triggered()
  2066. {
  2067. ChangeSceneIndex(true, 1, ui->scenes->count() - 1);
  2068. }
  2069. void OBSBasic::MoveSceneToTop()
  2070. {
  2071. ChangeSceneIndex(false, 0, 0);
  2072. }
  2073. void OBSBasic::MoveSceneToBottom()
  2074. {
  2075. ChangeSceneIndex(false, ui->scenes->count() - 1,
  2076. ui->scenes->count() - 1);
  2077. }
  2078. void OBSBasic::on_sources_currentItemChanged(QListWidgetItem *current,
  2079. QListWidgetItem *prev)
  2080. {
  2081. auto select_one = [] (obs_scene_t *scene, obs_sceneitem_t *item,
  2082. void *param)
  2083. {
  2084. obs_sceneitem_t *selectedItem =
  2085. *reinterpret_cast<OBSSceneItem*>(param);
  2086. obs_sceneitem_select(item, (selectedItem == item));
  2087. UNUSED_PARAMETER(scene);
  2088. return true;
  2089. };
  2090. if (!current)
  2091. return;
  2092. OBSSceneItem item = GetOBSRef<OBSSceneItem>(current);
  2093. obs_source_t *source = obs_sceneitem_get_source(item);
  2094. if ((obs_source_get_output_flags(source) & OBS_SOURCE_VIDEO) == 0)
  2095. return;
  2096. auto IgnoreSelfUpdate = [&](obs_scene_t *scene)
  2097. {
  2098. ignoreSelectionUpdate = true;
  2099. obs_scene_enum_items(scene, select_one, &item);
  2100. ignoreSelectionUpdate = false;
  2101. };
  2102. using IgnoreSelfUpdate_t = decltype(IgnoreSelfUpdate);
  2103. obs_scene_atomic_update(GetCurrentScene(),
  2104. [](void *data, obs_scene_t *scene)
  2105. {
  2106. (*static_cast<IgnoreSelfUpdate_t*>(data))(scene);
  2107. }, static_cast<void*>(&IgnoreSelfUpdate));
  2108. UNUSED_PARAMETER(prev);
  2109. }
  2110. void OBSBasic::EditSceneItemName()
  2111. {
  2112. QListWidgetItem *item = ui->sources->currentItem();
  2113. Qt::ItemFlags flags = item->flags();
  2114. OBSSceneItem sceneItem= GetOBSRef<OBSSceneItem>(item);
  2115. obs_source_t *source = obs_sceneitem_get_source(sceneItem);
  2116. const char *name = obs_source_get_name(source);
  2117. item->setText(QT_UTF8(name));
  2118. item->setFlags(flags | Qt::ItemIsEditable);
  2119. ui->sources->removeItemWidget(item);
  2120. ui->sources->editItem(item);
  2121. item->setFlags(flags);
  2122. }
  2123. void OBSBasic::CreateSourcePopupMenu(QListWidgetItem *item, bool preview)
  2124. {
  2125. QMenu popup(this);
  2126. QPointer<QMenu> previewProjector;
  2127. QPointer<QMenu> sourceProjector;
  2128. if (preview) {
  2129. QAction *action = popup.addAction(
  2130. QTStr("Basic.Main.PreviewConextMenu.Enable"),
  2131. this, SLOT(TogglePreview()));
  2132. action->setCheckable(true);
  2133. action->setChecked(
  2134. obs_display_enabled(ui->preview->GetDisplay()));
  2135. previewProjector = new QMenu(QTStr("PreviewProjector"));
  2136. AddProjectorMenuMonitors(previewProjector, this,
  2137. SLOT(OpenPreviewProjector()));
  2138. popup.addMenu(previewProjector);
  2139. popup.addSeparator();
  2140. }
  2141. QPointer<QMenu> addSourceMenu = CreateAddSourcePopupMenu();
  2142. if (addSourceMenu)
  2143. popup.addMenu(addSourceMenu);
  2144. if (item) {
  2145. if (addSourceMenu)
  2146. popup.addSeparator();
  2147. OBSSceneItem sceneItem = GetSceneItem(item);
  2148. obs_source_t *source = obs_sceneitem_get_source(sceneItem);
  2149. QAction *action;
  2150. popup.addAction(QTStr("Rename"), this,
  2151. SLOT(EditSceneItemName()));
  2152. popup.addAction(QTStr("Remove"), this,
  2153. SLOT(on_actionRemoveSource_triggered()),
  2154. DeleteKeys.front());
  2155. popup.addSeparator();
  2156. popup.addMenu(ui->orderMenu);
  2157. popup.addMenu(ui->transformMenu);
  2158. sourceProjector = new QMenu(QTStr("SourceProjector"));
  2159. AddProjectorMenuMonitors(sourceProjector, this,
  2160. SLOT(OpenSourceProjector()));
  2161. popup.addSeparator();
  2162. popup.addMenu(sourceProjector);
  2163. popup.addSeparator();
  2164. action = popup.addAction(QTStr("Interact"), this,
  2165. SLOT(on_actionInteract_triggered()));
  2166. action->setEnabled(obs_source_get_output_flags(source) &
  2167. OBS_SOURCE_INTERACTION);
  2168. popup.addAction(QTStr("Filters"), this,
  2169. SLOT(OpenFilters()));
  2170. popup.addAction(QTStr("Properties"), this,
  2171. SLOT(on_actionSourceProperties_triggered()));
  2172. }
  2173. popup.exec(QCursor::pos());
  2174. }
  2175. void OBSBasic::on_sources_customContextMenuRequested(const QPoint &pos)
  2176. {
  2177. CreateSourcePopupMenu(ui->sources->itemAt(pos), false);
  2178. }
  2179. void OBSBasic::on_sources_itemDoubleClicked(QListWidgetItem *witem)
  2180. {
  2181. if (!witem)
  2182. return;
  2183. OBSSceneItem item = GetSceneItem(witem);
  2184. OBSSource source = obs_sceneitem_get_source(item);
  2185. if (source)
  2186. CreatePropertiesWindow(source);
  2187. }
  2188. void OBSBasic::AddSource(const char *id)
  2189. {
  2190. if (id && *id) {
  2191. OBSBasicSourceSelect sourceSelect(this, id);
  2192. sourceSelect.exec();
  2193. }
  2194. }
  2195. QMenu *OBSBasic::CreateAddSourcePopupMenu()
  2196. {
  2197. const char *type;
  2198. bool foundValues = false;
  2199. size_t idx = 0;
  2200. QMenu *popup = new QMenu(QTStr("Add"), this);
  2201. while (obs_enum_input_types(idx++, &type)) {
  2202. const char *name = obs_source_get_display_name(
  2203. OBS_SOURCE_TYPE_INPUT, type);
  2204. if (strcmp(type, "scene") == 0)
  2205. continue;
  2206. QAction *popupItem = new QAction(QT_UTF8(name), this);
  2207. popupItem->setData(QT_UTF8(type));
  2208. connect(popupItem, SIGNAL(triggered(bool)),
  2209. this, SLOT(AddSourceFromAction()));
  2210. popup->addAction(popupItem);
  2211. foundValues = true;
  2212. }
  2213. if (!foundValues) {
  2214. delete popup;
  2215. popup = nullptr;
  2216. }
  2217. return popup;
  2218. }
  2219. void OBSBasic::AddSourceFromAction()
  2220. {
  2221. QAction *action = qobject_cast<QAction*>(sender());
  2222. if (!action)
  2223. return;
  2224. AddSource(QT_TO_UTF8(action->data().toString()));
  2225. }
  2226. void OBSBasic::AddSourcePopupMenu(const QPoint &pos)
  2227. {
  2228. if (!GetCurrentScene()) {
  2229. // Tell the user he needs a scene first (help beginners).
  2230. QMessageBox::information(this,
  2231. QTStr("Basic.Main.AddSourceHelp.Title"),
  2232. QTStr("Basic.Main.AddSourceHelp.Text"));
  2233. return;
  2234. }
  2235. QPointer<QMenu> popup = CreateAddSourcePopupMenu();
  2236. if (popup)
  2237. popup->exec(pos);
  2238. }
  2239. void OBSBasic::on_actionAddSource_triggered()
  2240. {
  2241. AddSourcePopupMenu(QCursor::pos());
  2242. }
  2243. void OBSBasic::on_actionRemoveSource_triggered()
  2244. {
  2245. OBSSceneItem item = GetCurrentSceneItem();
  2246. obs_source_t *source = obs_sceneitem_get_source(item);
  2247. if (source && QueryRemoveSource(source))
  2248. obs_sceneitem_remove(item);
  2249. }
  2250. void OBSBasic::on_actionInteract_triggered()
  2251. {
  2252. OBSSceneItem item = GetCurrentSceneItem();
  2253. OBSSource source = obs_sceneitem_get_source(item);
  2254. if (source)
  2255. CreateInteractionWindow(source);
  2256. }
  2257. void OBSBasic::on_actionSourceProperties_triggered()
  2258. {
  2259. OBSSceneItem item = GetCurrentSceneItem();
  2260. OBSSource source = obs_sceneitem_get_source(item);
  2261. if (source)
  2262. CreatePropertiesWindow(source);
  2263. }
  2264. void OBSBasic::on_actionSourceUp_triggered()
  2265. {
  2266. OBSSceneItem item = GetCurrentSceneItem();
  2267. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_UP);
  2268. }
  2269. void OBSBasic::on_actionSourceDown_triggered()
  2270. {
  2271. OBSSceneItem item = GetCurrentSceneItem();
  2272. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_DOWN);
  2273. }
  2274. void OBSBasic::on_actionMoveUp_triggered()
  2275. {
  2276. OBSSceneItem item = GetCurrentSceneItem();
  2277. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_UP);
  2278. }
  2279. void OBSBasic::on_actionMoveDown_triggered()
  2280. {
  2281. OBSSceneItem item = GetCurrentSceneItem();
  2282. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_DOWN);
  2283. }
  2284. void OBSBasic::on_actionMoveToTop_triggered()
  2285. {
  2286. OBSSceneItem item = GetCurrentSceneItem();
  2287. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_TOP);
  2288. }
  2289. void OBSBasic::on_actionMoveToBottom_triggered()
  2290. {
  2291. OBSSceneItem item = GetCurrentSceneItem();
  2292. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_BOTTOM);
  2293. }
  2294. static BPtr<char> ReadLogFile(const char *log)
  2295. {
  2296. char logDir[512];
  2297. if (GetConfigPath(logDir, sizeof(logDir), "obs-studio/logs") <= 0)
  2298. return nullptr;
  2299. string path = (char*)logDir;
  2300. path += "/";
  2301. path += log;
  2302. BPtr<char> file = os_quick_read_utf8_file(path.c_str());
  2303. if (!file)
  2304. blog(LOG_WARNING, "Failed to read log file %s", path.c_str());
  2305. return file;
  2306. }
  2307. void OBSBasic::UploadLog(const char *file)
  2308. {
  2309. BPtr<char> fileString{ReadLogFile(file)};
  2310. if (!fileString)
  2311. return;
  2312. if (!*fileString)
  2313. return;
  2314. ui->menuLogFiles->setEnabled(false);
  2315. auto data_deleter = [](obs_data_t *d) { obs_data_release(d); };
  2316. using data_t = unique_ptr<struct obs_data, decltype(data_deleter)>;
  2317. data_t content{obs_data_create(), data_deleter};
  2318. data_t files{obs_data_create(), data_deleter};
  2319. data_t request{obs_data_create(), data_deleter};
  2320. obs_data_set_string(content.get(), "content", fileString);
  2321. obs_data_set_obj(files.get(), file, content.get());
  2322. stringstream ss;
  2323. ss << "OBS " << App()->GetVersionString()
  2324. << " log file uploaded at " << CurrentDateTimeString();
  2325. obs_data_set_string(request.get(), "description", ss.str().c_str());
  2326. obs_data_set_bool(request.get(), "public", false);
  2327. obs_data_set_obj(request.get(), "files", files.get());
  2328. const char *json = obs_data_get_json(request.get());
  2329. if (!json) {
  2330. blog(LOG_ERROR, "Failed to get JSON data for log upload");
  2331. return;
  2332. }
  2333. QBuffer *postData = new QBuffer();
  2334. postData->setData(json, (int) strlen(json));
  2335. if (logUploadThread) {
  2336. logUploadThread->wait();
  2337. delete logUploadThread;
  2338. }
  2339. RemoteTextThread *thread = new RemoteTextThread(
  2340. "https://api.github.com/gists",
  2341. "application/json", json);
  2342. logUploadThread = thread;
  2343. connect(thread, &RemoteTextThread::Result,
  2344. this, &OBSBasic::logUploadFinished);
  2345. logUploadThread->start();
  2346. }
  2347. void OBSBasic::on_actionShowLogs_triggered()
  2348. {
  2349. char logDir[512];
  2350. if (GetConfigPath(logDir, sizeof(logDir), "obs-studio/logs") <= 0)
  2351. return;
  2352. QUrl url = QUrl::fromLocalFile(QT_UTF8(logDir));
  2353. QDesktopServices::openUrl(url);
  2354. }
  2355. void OBSBasic::on_actionUploadCurrentLog_triggered()
  2356. {
  2357. UploadLog(App()->GetCurrentLog());
  2358. }
  2359. void OBSBasic::on_actionUploadLastLog_triggered()
  2360. {
  2361. UploadLog(App()->GetLastLog());
  2362. }
  2363. void OBSBasic::on_actionViewCurrentLog_triggered()
  2364. {
  2365. char logDir[512];
  2366. if (GetConfigPath(logDir, sizeof(logDir), "obs-studio/logs") <= 0)
  2367. return;
  2368. const char* log = App()->GetCurrentLog();
  2369. string path = (char*)logDir;
  2370. path += "/";
  2371. path += log;
  2372. QUrl url = QUrl::fromLocalFile(QT_UTF8(path.c_str()));
  2373. QDesktopServices::openUrl(url);
  2374. }
  2375. void OBSBasic::on_actionCheckForUpdates_triggered()
  2376. {
  2377. CheckForUpdates();
  2378. }
  2379. void OBSBasic::logUploadFinished(const QString &text, const QString &error)
  2380. {
  2381. ui->menuLogFiles->setEnabled(true);
  2382. if (text.isEmpty()) {
  2383. QMessageBox::information(this,
  2384. QTStr("LogReturnDialog.ErrorUploadingLog"),
  2385. error);
  2386. return;
  2387. }
  2388. obs_data_t *returnData = obs_data_create_from_json(QT_TO_UTF8(text));
  2389. QString logURL = obs_data_get_string(returnData, "html_url");
  2390. obs_data_release(returnData);
  2391. OBSLogReply logDialog(this, logURL);
  2392. logDialog.exec();
  2393. }
  2394. static void RenameListItem(OBSBasic *parent, QListWidget *listWidget,
  2395. obs_source_t *source, const string &name)
  2396. {
  2397. const char *prevName = obs_source_get_name(source);
  2398. if (name == prevName)
  2399. return;
  2400. obs_source_t *foundSource = obs_get_source_by_name(name.c_str());
  2401. QListWidgetItem *listItem = listWidget->currentItem();
  2402. if (foundSource || name.empty()) {
  2403. listItem->setText(QT_UTF8(prevName));
  2404. if (foundSource) {
  2405. QMessageBox::information(parent,
  2406. QTStr("NameExists.Title"),
  2407. QTStr("NameExists.Text"));
  2408. } else if (name.empty()) {
  2409. QMessageBox::information(parent,
  2410. QTStr("NoNameEntered.Title"),
  2411. QTStr("NoNameEntered.Text"));
  2412. }
  2413. obs_source_release(foundSource);
  2414. } else {
  2415. listItem->setText(QT_UTF8(name.c_str()));
  2416. obs_source_set_name(source, name.c_str());
  2417. }
  2418. }
  2419. void OBSBasic::SceneNameEdited(QWidget *editor,
  2420. QAbstractItemDelegate::EndEditHint endHint)
  2421. {
  2422. OBSScene scene = GetCurrentScene();
  2423. QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
  2424. string text = QT_TO_UTF8(edit->text().trimmed());
  2425. if (!scene)
  2426. return;
  2427. obs_source_t *source = obs_scene_get_source(scene);
  2428. RenameListItem(this, ui->scenes, source, text);
  2429. UNUSED_PARAMETER(endHint);
  2430. }
  2431. void OBSBasic::SceneItemNameEdited(QWidget *editor,
  2432. QAbstractItemDelegate::EndEditHint endHint)
  2433. {
  2434. OBSSceneItem item = GetCurrentSceneItem();
  2435. QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
  2436. string text = QT_TO_UTF8(edit->text().trimmed());
  2437. if (!item)
  2438. return;
  2439. obs_source_t *source = obs_sceneitem_get_source(item);
  2440. RenameListItem(this, ui->sources, source, text);
  2441. QListWidgetItem *listItem = ui->sources->currentItem();
  2442. listItem->setText(QString());
  2443. SetupVisibilityItem(ui->sources, listItem, item);
  2444. UNUSED_PARAMETER(endHint);
  2445. }
  2446. void OBSBasic::OpenFilters()
  2447. {
  2448. OBSSceneItem item = GetCurrentSceneItem();
  2449. OBSSource source = obs_sceneitem_get_source(item);
  2450. CreateFiltersWindow(source);
  2451. }
  2452. void OBSBasic::OpenSceneFilters()
  2453. {
  2454. OBSScene scene = GetCurrentScene();
  2455. OBSSource source = obs_scene_get_source(scene);
  2456. CreateFiltersWindow(source);
  2457. }
  2458. #define RECORDING_START \
  2459. "==== Recording Start ==============================================="
  2460. #define RECORDING_STOP \
  2461. "==== Recording Stop ================================================"
  2462. #define STREAMING_START \
  2463. "==== Streaming Start ==============================================="
  2464. #define STREAMING_STOP \
  2465. "==== Streaming Stop ================================================"
  2466. void OBSBasic::StartStreaming()
  2467. {
  2468. SaveProject();
  2469. ui->streamButton->setEnabled(false);
  2470. ui->streamButton->setText(QTStr("Basic.Main.Connecting"));
  2471. if (!outputHandler->StartStreaming(service)) {
  2472. ui->streamButton->setText(QTStr("Basic.Main.StartStreaming"));
  2473. ui->streamButton->setEnabled(true);
  2474. }
  2475. }
  2476. void OBSBasic::StopStreaming()
  2477. {
  2478. SaveProject();
  2479. if (outputHandler->StreamingActive())
  2480. outputHandler->StopStreaming();
  2481. if (!outputHandler->Active()) {
  2482. ui->profileMenu->setEnabled(true);
  2483. }
  2484. }
  2485. void OBSBasic::ForceStopStreaming()
  2486. {
  2487. SaveProject();
  2488. if (outputHandler->StreamingActive())
  2489. outputHandler->ForceStopStreaming();
  2490. if (!outputHandler->Active()) {
  2491. ui->profileMenu->setEnabled(true);
  2492. }
  2493. }
  2494. void OBSBasic::StreamDelayStarting(int sec)
  2495. {
  2496. ui->streamButton->setText(QTStr("Basic.Main.StopStreaming"));
  2497. ui->streamButton->setEnabled(true);
  2498. if (!startStreamMenu.isNull())
  2499. startStreamMenu->deleteLater();
  2500. startStreamMenu = new QMenu();
  2501. startStreamMenu->addAction(QTStr("Basic.Main.StopStreaming"),
  2502. this, SLOT(StopStreaming()));
  2503. startStreamMenu->addAction(QTStr("Basic.Main.ForceStopStreaming"),
  2504. this, SLOT(ForceStopStreaming()));
  2505. ui->streamButton->setMenu(startStreamMenu);
  2506. ui->statusbar->StreamDelayStarting(sec);
  2507. }
  2508. void OBSBasic::StreamDelayStopping(int sec)
  2509. {
  2510. ui->streamButton->setText(QTStr("Basic.Main.StartStreaming"));
  2511. ui->streamButton->setEnabled(true);
  2512. if (!startStreamMenu.isNull())
  2513. startStreamMenu->deleteLater();
  2514. startStreamMenu = new QMenu();
  2515. startStreamMenu->addAction(QTStr("Basic.Main.StartStreaming"),
  2516. this, SLOT(StartStreaming()));
  2517. startStreamMenu->addAction(QTStr("Basic.Main.ForceStopStreaming"),
  2518. this, SLOT(ForceStopStreaming()));
  2519. ui->streamButton->setMenu(startStreamMenu);
  2520. ui->statusbar->StreamDelayStopping(sec);
  2521. }
  2522. void OBSBasic::StreamingStart()
  2523. {
  2524. ui->streamButton->setText(QTStr("Basic.Main.StopStreaming"));
  2525. ui->streamButton->setEnabled(true);
  2526. ui->statusbar->StreamStarted(outputHandler->streamOutput);
  2527. ui->profileMenu->setEnabled(false);
  2528. blog(LOG_INFO, STREAMING_START);
  2529. }
  2530. void OBSBasic::StreamingStop(int code)
  2531. {
  2532. const char *errorMessage;
  2533. switch (code) {
  2534. case OBS_OUTPUT_BAD_PATH:
  2535. errorMessage = Str("Output.ConnectFail.BadPath");
  2536. break;
  2537. case OBS_OUTPUT_CONNECT_FAILED:
  2538. errorMessage = Str("Output.ConnectFail.ConnectFailed");
  2539. break;
  2540. case OBS_OUTPUT_INVALID_STREAM:
  2541. errorMessage = Str("Output.ConnectFail.InvalidStream");
  2542. break;
  2543. default:
  2544. case OBS_OUTPUT_ERROR:
  2545. errorMessage = Str("Output.ConnectFail.Error");
  2546. break;
  2547. case OBS_OUTPUT_DISCONNECTED:
  2548. /* doesn't happen if output is set to reconnect. note that
  2549. * reconnects are handled in the output, not in the UI */
  2550. errorMessage = Str("Output.ConnectFail.Disconnected");
  2551. }
  2552. ui->statusbar->StreamStopped();
  2553. ui->streamButton->setText(QTStr("Basic.Main.StartStreaming"));
  2554. ui->streamButton->setEnabled(true);
  2555. if (!outputHandler->Active())
  2556. ui->profileMenu->setEnabled(true);
  2557. blog(LOG_INFO, STREAMING_STOP);
  2558. if (code != OBS_OUTPUT_SUCCESS)
  2559. QMessageBox::information(this,
  2560. QTStr("Output.ConnectFail.Title"),
  2561. QT_UTF8(errorMessage));
  2562. if (!startStreamMenu.isNull()) {
  2563. ui->streamButton->setMenu(nullptr);
  2564. startStreamMenu->deleteLater();
  2565. startStreamMenu = nullptr;
  2566. }
  2567. }
  2568. void OBSBasic::StartRecording()
  2569. {
  2570. SaveProject();
  2571. if (!outputHandler->RecordingActive())
  2572. outputHandler->StartRecording();
  2573. }
  2574. void OBSBasic::StopRecording()
  2575. {
  2576. SaveProject();
  2577. if (outputHandler->RecordingActive())
  2578. outputHandler->StopRecording();
  2579. if (!outputHandler->Active()) {
  2580. ui->profileMenu->setEnabled(true);
  2581. }
  2582. }
  2583. void OBSBasic::RecordingStart()
  2584. {
  2585. ui->statusbar->RecordingStarted(outputHandler->fileOutput);
  2586. ui->recordButton->setText(QTStr("Basic.Main.StopRecording"));
  2587. ui->profileMenu->setEnabled(false);
  2588. blog(LOG_INFO, RECORDING_START);
  2589. }
  2590. void OBSBasic::RecordingStop(int code)
  2591. {
  2592. ui->statusbar->RecordingStopped();
  2593. ui->recordButton->setText(QTStr("Basic.Main.StartRecording"));
  2594. blog(LOG_INFO, RECORDING_STOP);
  2595. if (code == OBS_OUTPUT_UNSUPPORTED)
  2596. QMessageBox::information(this,
  2597. QTStr("Output.RecordFail.Title"),
  2598. QTStr("Output.RecordFail.Unsupported"));
  2599. if (!outputHandler->Active()) {
  2600. ui->profileMenu->setEnabled(true);
  2601. }
  2602. }
  2603. void OBSBasic::on_streamButton_clicked()
  2604. {
  2605. if (outputHandler->StreamingActive()) {
  2606. StopStreaming();
  2607. } else {
  2608. StartStreaming();
  2609. }
  2610. }
  2611. void OBSBasic::on_recordButton_clicked()
  2612. {
  2613. if (outputHandler->RecordingActive())
  2614. StopRecording();
  2615. else
  2616. StartRecording();
  2617. }
  2618. void OBSBasic::on_settingsButton_clicked()
  2619. {
  2620. OBSBasicSettings settings(this);
  2621. settings.exec();
  2622. }
  2623. void OBSBasic::on_actionWebsite_triggered()
  2624. {
  2625. QUrl url = QUrl("https://obsproject.com", QUrl::TolerantMode);
  2626. QDesktopServices::openUrl(url);
  2627. }
  2628. void OBSBasic::on_actionShowSettingsFolder_triggered()
  2629. {
  2630. char path[512];
  2631. int ret = GetConfigPath(path, 512, "obs-studio");
  2632. if (ret <= 0)
  2633. return;
  2634. QDesktopServices::openUrl(QUrl::fromLocalFile(path));
  2635. }
  2636. void OBSBasic::on_actionShowProfileFolder_triggered()
  2637. {
  2638. char path[512];
  2639. int ret = GetProfilePath(path, 512, "");
  2640. if (ret <= 0)
  2641. return;
  2642. QDesktopServices::openUrl(QUrl::fromLocalFile(path));
  2643. }
  2644. void OBSBasic::on_preview_customContextMenuRequested(const QPoint &pos)
  2645. {
  2646. CreateSourcePopupMenu(ui->sources->currentItem(), true);
  2647. UNUSED_PARAMETER(pos);
  2648. }
  2649. void OBSBasic::on_previewDisabledLabel_customContextMenuRequested(
  2650. const QPoint &pos)
  2651. {
  2652. QMenu popup(this);
  2653. QPointer<QMenu> previewProjector;
  2654. QAction *action = popup.addAction(
  2655. QTStr("Basic.Main.PreviewConextMenu.Enable"),
  2656. this, SLOT(TogglePreview()));
  2657. action->setCheckable(true);
  2658. action->setChecked(obs_display_enabled(ui->preview->GetDisplay()));
  2659. previewProjector = new QMenu(QTStr("PreviewProjector"));
  2660. AddProjectorMenuMonitors(previewProjector, this,
  2661. SLOT(OpenPreviewProjector()));
  2662. popup.addMenu(previewProjector);
  2663. popup.exec(QCursor::pos());
  2664. UNUSED_PARAMETER(pos);
  2665. }
  2666. void OBSBasic::GetFPSCommon(uint32_t &num, uint32_t &den) const
  2667. {
  2668. const char *val = config_get_string(basicConfig, "Video", "FPSCommon");
  2669. if (strcmp(val, "10") == 0) {
  2670. num = 10;
  2671. den = 1;
  2672. } else if (strcmp(val, "20") == 0) {
  2673. num = 20;
  2674. den = 1;
  2675. } else if (strcmp(val, "25") == 0) {
  2676. num = 25;
  2677. den = 1;
  2678. } else if (strcmp(val, "29.97") == 0) {
  2679. num = 30000;
  2680. den = 1001;
  2681. } else if (strcmp(val, "48") == 0) {
  2682. num = 48;
  2683. den = 1;
  2684. } else if (strcmp(val, "59.94") == 0) {
  2685. num = 60000;
  2686. den = 1001;
  2687. } else if (strcmp(val, "60") == 0) {
  2688. num = 60;
  2689. den = 1;
  2690. } else {
  2691. num = 30;
  2692. den = 1;
  2693. }
  2694. }
  2695. void OBSBasic::GetFPSInteger(uint32_t &num, uint32_t &den) const
  2696. {
  2697. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSInt");
  2698. den = 1;
  2699. }
  2700. void OBSBasic::GetFPSFraction(uint32_t &num, uint32_t &den) const
  2701. {
  2702. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNum");
  2703. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSDen");
  2704. }
  2705. void OBSBasic::GetFPSNanoseconds(uint32_t &num, uint32_t &den) const
  2706. {
  2707. num = 1000000000;
  2708. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNS");
  2709. }
  2710. void OBSBasic::GetConfigFPS(uint32_t &num, uint32_t &den) const
  2711. {
  2712. uint32_t type = config_get_uint(basicConfig, "Video", "FPSType");
  2713. if (type == 1) //"Integer"
  2714. GetFPSInteger(num, den);
  2715. else if (type == 2) //"Fraction"
  2716. GetFPSFraction(num, den);
  2717. else if (false) //"Nanoseconds", currently not implemented
  2718. GetFPSNanoseconds(num, den);
  2719. else
  2720. GetFPSCommon(num, den);
  2721. }
  2722. config_t *OBSBasic::Config() const
  2723. {
  2724. return basicConfig;
  2725. }
  2726. void OBSBasic::on_actionEditTransform_triggered()
  2727. {
  2728. if (transformWindow)
  2729. transformWindow->close();
  2730. transformWindow = new OBSBasicTransform(this);
  2731. transformWindow->show();
  2732. transformWindow->setAttribute(Qt::WA_DeleteOnClose, true);
  2733. }
  2734. void OBSBasic::on_actionResetTransform_triggered()
  2735. {
  2736. auto func = [] (obs_scene_t *scene, obs_sceneitem_t *item, void *param)
  2737. {
  2738. if (!obs_sceneitem_selected(item))
  2739. return true;
  2740. obs_transform_info info;
  2741. vec2_set(&info.pos, 0.0f, 0.0f);
  2742. vec2_set(&info.scale, 1.0f, 1.0f);
  2743. info.rot = 0.0f;
  2744. info.alignment = OBS_ALIGN_TOP | OBS_ALIGN_LEFT;
  2745. info.bounds_type = OBS_BOUNDS_NONE;
  2746. info.bounds_alignment = OBS_ALIGN_CENTER;
  2747. vec2_set(&info.bounds, 0.0f, 0.0f);
  2748. obs_sceneitem_set_info(item, &info);
  2749. UNUSED_PARAMETER(scene);
  2750. UNUSED_PARAMETER(param);
  2751. return true;
  2752. };
  2753. obs_scene_enum_items(GetCurrentScene(), func, nullptr);
  2754. }
  2755. static void GetItemBox(obs_sceneitem_t *item, vec3 &tl, vec3 &br)
  2756. {
  2757. matrix4 boxTransform;
  2758. obs_sceneitem_get_box_transform(item, &boxTransform);
  2759. vec3_set(&tl, M_INFINITE, M_INFINITE, 0.0f);
  2760. vec3_set(&br, -M_INFINITE, -M_INFINITE, 0.0f);
  2761. auto GetMinPos = [&] (float x, float y)
  2762. {
  2763. vec3 pos;
  2764. vec3_set(&pos, x, y, 0.0f);
  2765. vec3_transform(&pos, &pos, &boxTransform);
  2766. vec3_min(&tl, &tl, &pos);
  2767. vec3_max(&br, &br, &pos);
  2768. };
  2769. GetMinPos(0.0f, 0.0f);
  2770. GetMinPos(1.0f, 0.0f);
  2771. GetMinPos(0.0f, 1.0f);
  2772. GetMinPos(1.0f, 1.0f);
  2773. }
  2774. static vec3 GetItemTL(obs_sceneitem_t *item)
  2775. {
  2776. vec3 tl, br;
  2777. GetItemBox(item, tl, br);
  2778. return tl;
  2779. }
  2780. static void SetItemTL(obs_sceneitem_t *item, const vec3 &tl)
  2781. {
  2782. vec3 newTL;
  2783. vec2 pos;
  2784. obs_sceneitem_get_pos(item, &pos);
  2785. newTL = GetItemTL(item);
  2786. pos.x += tl.x - newTL.x;
  2787. pos.y += tl.y - newTL.y;
  2788. obs_sceneitem_set_pos(item, &pos);
  2789. }
  2790. static bool RotateSelectedSources(obs_scene_t *scene, obs_sceneitem_t *item,
  2791. void *param)
  2792. {
  2793. if (!obs_sceneitem_selected(item))
  2794. return true;
  2795. float rot = *reinterpret_cast<float*>(param);
  2796. vec3 tl = GetItemTL(item);
  2797. rot += obs_sceneitem_get_rot(item);
  2798. if (rot >= 360.0f) rot -= 360.0f;
  2799. else if (rot <= -360.0f) rot += 360.0f;
  2800. obs_sceneitem_set_rot(item, rot);
  2801. SetItemTL(item, tl);
  2802. UNUSED_PARAMETER(scene);
  2803. UNUSED_PARAMETER(param);
  2804. return true;
  2805. };
  2806. void OBSBasic::on_actionRotate90CW_triggered()
  2807. {
  2808. float f90CW = 90.0f;
  2809. obs_scene_enum_items(GetCurrentScene(), RotateSelectedSources, &f90CW);
  2810. }
  2811. void OBSBasic::on_actionRotate90CCW_triggered()
  2812. {
  2813. float f90CCW = -90.0f;
  2814. obs_scene_enum_items(GetCurrentScene(), RotateSelectedSources, &f90CCW);
  2815. }
  2816. void OBSBasic::on_actionRotate180_triggered()
  2817. {
  2818. float f180 = 180.0f;
  2819. obs_scene_enum_items(GetCurrentScene(), RotateSelectedSources, &f180);
  2820. }
  2821. static bool MultiplySelectedItemScale(obs_scene_t *scene, obs_sceneitem_t *item,
  2822. void *param)
  2823. {
  2824. vec2 &mul = *reinterpret_cast<vec2*>(param);
  2825. if (!obs_sceneitem_selected(item))
  2826. return true;
  2827. vec3 tl = GetItemTL(item);
  2828. vec2 scale;
  2829. obs_sceneitem_get_scale(item, &scale);
  2830. vec2_mul(&scale, &scale, &mul);
  2831. obs_sceneitem_set_scale(item, &scale);
  2832. SetItemTL(item, tl);
  2833. UNUSED_PARAMETER(scene);
  2834. return true;
  2835. }
  2836. void OBSBasic::on_actionFlipHorizontal_triggered()
  2837. {
  2838. vec2 scale;
  2839. vec2_set(&scale, -1.0f, 1.0f);
  2840. obs_scene_enum_items(GetCurrentScene(), MultiplySelectedItemScale,
  2841. &scale);
  2842. }
  2843. void OBSBasic::on_actionFlipVertical_triggered()
  2844. {
  2845. vec2 scale;
  2846. vec2_set(&scale, 1.0f, -1.0f);
  2847. obs_scene_enum_items(GetCurrentScene(), MultiplySelectedItemScale,
  2848. &scale);
  2849. }
  2850. static bool CenterAlignSelectedItems(obs_scene_t *scene, obs_sceneitem_t *item,
  2851. void *param)
  2852. {
  2853. obs_bounds_type boundsType = *reinterpret_cast<obs_bounds_type*>(param);
  2854. if (!obs_sceneitem_selected(item))
  2855. return true;
  2856. obs_video_info ovi;
  2857. obs_get_video_info(&ovi);
  2858. obs_transform_info itemInfo;
  2859. vec2_set(&itemInfo.pos, 0.0f, 0.0f);
  2860. vec2_set(&itemInfo.scale, 1.0f, 1.0f);
  2861. itemInfo.alignment = OBS_ALIGN_LEFT | OBS_ALIGN_TOP;
  2862. itemInfo.rot = 0.0f;
  2863. vec2_set(&itemInfo.bounds,
  2864. float(ovi.base_width), float(ovi.base_height));
  2865. itemInfo.bounds_type = boundsType;
  2866. itemInfo.bounds_alignment = OBS_ALIGN_CENTER;
  2867. obs_sceneitem_set_info(item, &itemInfo);
  2868. UNUSED_PARAMETER(scene);
  2869. return true;
  2870. }
  2871. void OBSBasic::on_actionFitToScreen_triggered()
  2872. {
  2873. obs_bounds_type boundsType = OBS_BOUNDS_SCALE_INNER;
  2874. obs_scene_enum_items(GetCurrentScene(), CenterAlignSelectedItems,
  2875. &boundsType);
  2876. }
  2877. void OBSBasic::on_actionStretchToScreen_triggered()
  2878. {
  2879. obs_bounds_type boundsType = OBS_BOUNDS_STRETCH;
  2880. obs_scene_enum_items(GetCurrentScene(), CenterAlignSelectedItems,
  2881. &boundsType);
  2882. }
  2883. void OBSBasic::on_actionCenterToScreen_triggered()
  2884. {
  2885. auto func = [] (obs_scene_t *scene, obs_sceneitem_t *item, void *param)
  2886. {
  2887. vec3 tl, br, itemCenter, screenCenter, offset;
  2888. obs_video_info ovi;
  2889. if (!obs_sceneitem_selected(item))
  2890. return true;
  2891. obs_get_video_info(&ovi);
  2892. vec3_set(&screenCenter, float(ovi.base_width),
  2893. float(ovi.base_height), 0.0f);
  2894. vec3_mulf(&screenCenter, &screenCenter, 0.5f);
  2895. GetItemBox(item, tl, br);
  2896. vec3_sub(&itemCenter, &br, &tl);
  2897. vec3_mulf(&itemCenter, &itemCenter, 0.5f);
  2898. vec3_add(&itemCenter, &itemCenter, &tl);
  2899. vec3_sub(&offset, &screenCenter, &itemCenter);
  2900. vec3_add(&tl, &tl, &offset);
  2901. SetItemTL(item, tl);
  2902. UNUSED_PARAMETER(scene);
  2903. UNUSED_PARAMETER(param);
  2904. return true;
  2905. };
  2906. obs_scene_enum_items(GetCurrentScene(), func, nullptr);
  2907. }
  2908. void OBSBasic::TogglePreview()
  2909. {
  2910. bool enabled = !obs_display_enabled(ui->preview->GetDisplay());
  2911. obs_display_set_enabled(ui->preview->GetDisplay(), enabled);
  2912. ui->preview->setVisible(enabled);
  2913. ui->previewDisabledLabel->setVisible(!enabled);
  2914. }
  2915. void OBSBasic::Nudge(int dist, MoveDir dir)
  2916. {
  2917. struct MoveInfo {
  2918. float dist;
  2919. MoveDir dir;
  2920. } info = {(float)dist, dir};
  2921. auto func = [] (obs_scene_t*, obs_sceneitem_t *item, void *param)
  2922. {
  2923. MoveInfo *info = reinterpret_cast<MoveInfo*>(param);
  2924. struct vec2 dir;
  2925. struct vec2 pos;
  2926. vec2_set(&dir, 0.0f, 0.0f);
  2927. if (!obs_sceneitem_selected(item))
  2928. return true;
  2929. switch (info->dir) {
  2930. case MoveDir::Up: dir.y = -info->dist; break;
  2931. case MoveDir::Down: dir.y = info->dist; break;
  2932. case MoveDir::Left: dir.x = -info->dist; break;
  2933. case MoveDir::Right: dir.x = info->dist; break;
  2934. }
  2935. obs_sceneitem_get_pos(item, &pos);
  2936. vec2_add(&pos, &pos, &dir);
  2937. obs_sceneitem_set_pos(item, &pos);
  2938. return true;
  2939. };
  2940. obs_scene_enum_items(GetCurrentScene(), func, &info);
  2941. }
  2942. void OBSBasic::NudgeUp() {Nudge(1, MoveDir::Up);}
  2943. void OBSBasic::NudgeDown() {Nudge(1, MoveDir::Down);}
  2944. void OBSBasic::NudgeLeft() {Nudge(1, MoveDir::Left);}
  2945. void OBSBasic::NudgeRight() {Nudge(1, MoveDir::Right);}
  2946. void OBSBasic::OpenProjector(obs_source_t *source, int monitor)
  2947. {
  2948. /* seriously? 10 monitors? */
  2949. if (monitor > 9)
  2950. return;
  2951. delete projectors[monitor];
  2952. projectors[monitor].clear();
  2953. OBSProjector *projector = new OBSProjector(this, source);
  2954. projector->Init(monitor);
  2955. projectors[monitor] = projector;
  2956. }
  2957. void OBSBasic::OpenPreviewProjector()
  2958. {
  2959. int monitor = sender()->property("monitor").toInt();
  2960. OpenProjector(nullptr, monitor);
  2961. }
  2962. void OBSBasic::OpenSourceProjector()
  2963. {
  2964. int monitor = sender()->property("monitor").toInt();
  2965. OBSSceneItem item = GetCurrentSceneItem();
  2966. if (!item)
  2967. return;
  2968. OpenProjector(obs_sceneitem_get_source(item), monitor);
  2969. }
  2970. void OBSBasic::OpenSceneProjector()
  2971. {
  2972. int monitor = sender()->property("monitor").toInt();
  2973. OBSScene scene = GetCurrentScene();
  2974. if (!scene)
  2975. return;
  2976. OpenProjector(obs_scene_get_source(scene), monitor);
  2977. }
  2978. void OBSBasic::UpdateTitleBar()
  2979. {
  2980. stringstream name;
  2981. const char *profile = config_get_string(App()->GlobalConfig(),
  2982. "Basic", "Profile");
  2983. const char *sceneCollection = config_get_string(App()->GlobalConfig(),
  2984. "Basic", "SceneCollection");
  2985. name << "OBS " << App()->GetVersionString();
  2986. name << " - " << Str("TitleBar.Profile") << ": " << profile;
  2987. name << " - " << Str("TitleBar.Scenes") << ": " << sceneCollection;
  2988. setWindowTitle(QT_UTF8(name.str().c_str()));
  2989. }
  2990. int OBSBasic::GetProfilePath(char *path, size_t size, const char *file) const
  2991. {
  2992. char profiles_path[512];
  2993. const char *profile = config_get_string(App()->GlobalConfig(),
  2994. "Basic", "ProfileDir");
  2995. int ret;
  2996. if (!profile)
  2997. return -1;
  2998. if (!path)
  2999. return -1;
  3000. if (!file)
  3001. file = "";
  3002. ret = GetConfigPath(profiles_path, 512, "obs-studio/basic/profiles");
  3003. if (ret <= 0)
  3004. return ret;
  3005. if (!*file)
  3006. return snprintf(path, size, "%s/%s", profiles_path, profile);
  3007. return snprintf(path, size, "%s/%s/%s", profiles_path, profile, file);
  3008. }