window-basic-main.cpp 96 KB

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