1
0

window-basic-main.cpp 96 KB

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