window-basic-main.cpp 96 KB

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