window-basic-main.cpp 97 KB

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