window-basic-main.cpp 81 KB

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