window-basic-main.cpp 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152
  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", 128);
  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 *path = config_get_string(basicConfig,
  1578. "SimpleOutput", "FilePath");
  1579. QDesktopServices::openUrl(QUrl::fromLocalFile(path));
  1580. }
  1581. void OBSBasic::on_actionRemux_triggered()
  1582. {
  1583. const char *path = config_get_string(basicConfig,
  1584. "SimpleOutput", "FilePath");
  1585. OBSRemux remux(path, this);
  1586. remux.exec();
  1587. }
  1588. void OBSBasic::on_action_Settings_triggered()
  1589. {
  1590. OBSBasicSettings settings(this);
  1591. settings.exec();
  1592. }
  1593. void OBSBasic::on_actionAdvAudioProperties_triggered()
  1594. {
  1595. if (advAudioWindow != nullptr) {
  1596. advAudioWindow->raise();
  1597. return;
  1598. }
  1599. advAudioWindow = new OBSBasicAdvAudio(this);
  1600. advAudioWindow->show();
  1601. advAudioWindow->setAttribute(Qt::WA_DeleteOnClose, true);
  1602. connect(advAudioWindow, SIGNAL(destroyed()),
  1603. this, SLOT(on_advAudioProps_destroyed()));
  1604. }
  1605. void OBSBasic::on_advAudioProps_clicked()
  1606. {
  1607. on_actionAdvAudioProperties_triggered();
  1608. }
  1609. void OBSBasic::on_advAudioProps_destroyed()
  1610. {
  1611. advAudioWindow = nullptr;
  1612. }
  1613. void OBSBasic::on_scenes_currentItemChanged(QListWidgetItem *current,
  1614. QListWidgetItem *prev)
  1615. {
  1616. obs_source_t *source = NULL;
  1617. if (sceneChanging)
  1618. return;
  1619. if (current) {
  1620. obs_scene_t *scene;
  1621. scene = current->data(Qt::UserRole).value<OBSScene>();
  1622. source = obs_scene_get_source(scene);
  1623. }
  1624. /* TODO: allow transitions */
  1625. obs_set_output_source(0, source);
  1626. UNUSED_PARAMETER(prev);
  1627. }
  1628. void OBSBasic::EditSceneName()
  1629. {
  1630. QListWidgetItem *item = ui->scenes->currentItem();
  1631. Qt::ItemFlags flags = item->flags();
  1632. item->setFlags(flags | Qt::ItemIsEditable);
  1633. ui->scenes->editItem(item);
  1634. item->setFlags(flags);
  1635. }
  1636. static void AddProjectorMenuMonitors(QMenu *parent, QObject *target,
  1637. const char *slot)
  1638. {
  1639. QAction *action;
  1640. std::vector<MonitorInfo> monitors;
  1641. GetMonitors(monitors);
  1642. for (int i = 0; (size_t)i < monitors.size(); i++) {
  1643. const MonitorInfo &monitor = monitors[i];
  1644. QString str = QString("%1 %2: %3x%4 @ %5,%6").
  1645. arg(QTStr("Display"),
  1646. QString::number(i),
  1647. QString::number((int)monitor.cx),
  1648. QString::number((int)monitor.cy),
  1649. QString::number((int)monitor.x),
  1650. QString::number((int)monitor.y));
  1651. action = parent->addAction(str, target, slot);
  1652. action->setProperty("monitor", i);
  1653. }
  1654. }
  1655. void OBSBasic::on_scenes_customContextMenuRequested(const QPoint &pos)
  1656. {
  1657. QListWidgetItem *item = ui->scenes->itemAt(pos);
  1658. QPointer<QMenu> sceneProjectorMenu;
  1659. QMenu popup(this);
  1660. popup.addAction(QTStr("Add"),
  1661. this, SLOT(on_actionAddScene_triggered()));
  1662. if (item) {
  1663. popup.addSeparator();
  1664. popup.addAction(QTStr("Rename"),
  1665. this, SLOT(EditSceneName()));
  1666. popup.addAction(QTStr("Remove"),
  1667. this, SLOT(RemoveSelectedScene()),
  1668. DeleteKeys.front());
  1669. popup.addSeparator();
  1670. sceneProjectorMenu = new QMenu(QTStr("SceneProjector"));
  1671. AddProjectorMenuMonitors(sceneProjectorMenu, this,
  1672. SLOT(OpenSceneProjector()));
  1673. popup.addMenu(sceneProjectorMenu);
  1674. popup.addSeparator();
  1675. popup.addAction(QTStr("Filters"), this,
  1676. SLOT(OpenSceneFilters()));
  1677. }
  1678. popup.exec(QCursor::pos());
  1679. }
  1680. void OBSBasic::on_actionAddScene_triggered()
  1681. {
  1682. string name;
  1683. QString format{QTStr("Basic.Main.DefaultSceneName.Text")};
  1684. int i = 1;
  1685. QString placeHolderText = format.arg(i);
  1686. obs_source_t *source = nullptr;
  1687. while ((source = obs_get_source_by_name(QT_TO_UTF8(placeHolderText)))) {
  1688. obs_source_release(source);
  1689. placeHolderText = format.arg(++i);
  1690. }
  1691. bool accepted = NameDialog::AskForName(this,
  1692. QTStr("Basic.Main.AddSceneDlg.Title"),
  1693. QTStr("Basic.Main.AddSceneDlg.Text"),
  1694. name,
  1695. placeHolderText);
  1696. if (accepted) {
  1697. if (name.empty()) {
  1698. QMessageBox::information(this,
  1699. QTStr("NoNameEntered.Title"),
  1700. QTStr("NoNameEntered.Text"));
  1701. on_actionAddScene_triggered();
  1702. return;
  1703. }
  1704. obs_source_t *source = obs_get_source_by_name(name.c_str());
  1705. if (source) {
  1706. QMessageBox::information(this,
  1707. QTStr("NameExists.Title"),
  1708. QTStr("NameExists.Text"));
  1709. obs_source_release(source);
  1710. on_actionAddScene_triggered();
  1711. return;
  1712. }
  1713. obs_scene_t *scene = obs_scene_create(name.c_str());
  1714. source = obs_scene_get_source(scene);
  1715. obs_add_source(source);
  1716. obs_scene_release(scene);
  1717. obs_set_output_source(0, source);
  1718. }
  1719. }
  1720. void OBSBasic::on_actionRemoveScene_triggered()
  1721. {
  1722. OBSScene scene = GetCurrentScene();
  1723. obs_source_t *source = obs_scene_get_source(scene);
  1724. if (source && QueryRemoveSource(source))
  1725. obs_source_remove(source);
  1726. }
  1727. void OBSBasic::on_actionSceneProperties_triggered()
  1728. {
  1729. /* TODO */
  1730. }
  1731. void OBSBasic::on_actionSceneUp_triggered()
  1732. {
  1733. /* TODO */
  1734. }
  1735. void OBSBasic::on_actionSceneDown_triggered()
  1736. {
  1737. /* TODO */
  1738. }
  1739. void OBSBasic::on_sources_currentItemChanged(QListWidgetItem *current,
  1740. QListWidgetItem *prev)
  1741. {
  1742. auto select_one = [] (obs_scene_t *scene, obs_sceneitem_t *item,
  1743. void *param)
  1744. {
  1745. obs_sceneitem_t *selectedItem =
  1746. *reinterpret_cast<OBSSceneItem*>(param);
  1747. obs_sceneitem_select(item, (selectedItem == item));
  1748. UNUSED_PARAMETER(scene);
  1749. return true;
  1750. };
  1751. if (!current)
  1752. return;
  1753. OBSSceneItem item = current->data(Qt::UserRole).value<OBSSceneItem>();
  1754. obs_source_t *source = obs_sceneitem_get_source(item);
  1755. if ((obs_source_get_output_flags(source) & OBS_SOURCE_VIDEO) == 0)
  1756. return;
  1757. obs_scene_enum_items(GetCurrentScene(), select_one, &item);
  1758. UNUSED_PARAMETER(prev);
  1759. }
  1760. void OBSBasic::EditSceneItemName()
  1761. {
  1762. QListWidgetItem *item = ui->sources->currentItem();
  1763. Qt::ItemFlags flags = item->flags();
  1764. OBSSceneItem sceneItem= item->data(Qt::UserRole).value<OBSSceneItem>();
  1765. obs_source_t *source = obs_sceneitem_get_source(sceneItem);
  1766. const char *name = obs_source_get_name(source);
  1767. item->setText(QT_UTF8(name));
  1768. item->setFlags(flags | Qt::ItemIsEditable);
  1769. ui->sources->removeItemWidget(item);
  1770. ui->sources->editItem(item);
  1771. item->setFlags(flags);
  1772. }
  1773. void OBSBasic::CreateSourcePopupMenu(QListWidgetItem *item, bool preview)
  1774. {
  1775. QMenu popup(this);
  1776. QPointer<QMenu> previewProjector;
  1777. QPointer<QMenu> sourceProjector;
  1778. if (preview) {
  1779. QAction *action = popup.addAction(
  1780. QTStr("Basic.Main.PreviewConextMenu.Enable"),
  1781. this, SLOT(TogglePreview()));
  1782. action->setCheckable(true);
  1783. action->setChecked(obs_preview_enabled());
  1784. previewProjector = new QMenu(QTStr("PreviewProjector"));
  1785. AddProjectorMenuMonitors(previewProjector, this,
  1786. SLOT(OpenPreviewProjector()));
  1787. popup.addMenu(previewProjector);
  1788. popup.addSeparator();
  1789. }
  1790. QPointer<QMenu> addSourceMenu = CreateAddSourcePopupMenu();
  1791. if (addSourceMenu)
  1792. popup.addMenu(addSourceMenu);
  1793. if (item) {
  1794. if (addSourceMenu)
  1795. popup.addSeparator();
  1796. OBSSceneItem sceneItem = GetSceneItem(item);
  1797. obs_source_t *source = obs_sceneitem_get_source(sceneItem);
  1798. QAction *action;
  1799. popup.addAction(QTStr("Rename"), this,
  1800. SLOT(EditSceneItemName()));
  1801. popup.addAction(QTStr("Remove"), this,
  1802. SLOT(on_actionRemoveSource_triggered()),
  1803. DeleteKeys.front());
  1804. popup.addSeparator();
  1805. popup.addMenu(ui->orderMenu);
  1806. popup.addMenu(ui->transformMenu);
  1807. sourceProjector = new QMenu(QTStr("SourceProjector"));
  1808. AddProjectorMenuMonitors(sourceProjector, this,
  1809. SLOT(OpenSourceProjector()));
  1810. popup.addSeparator();
  1811. popup.addMenu(sourceProjector);
  1812. popup.addSeparator();
  1813. action = popup.addAction(QTStr("Interact"), this,
  1814. SLOT(on_actionInteract_triggered()));
  1815. action->setEnabled(obs_source_get_output_flags(source) &
  1816. OBS_SOURCE_INTERACTION);
  1817. popup.addAction(QTStr("Filters"), this,
  1818. SLOT(OpenFilters()));
  1819. popup.addAction(QTStr("Properties"), this,
  1820. SLOT(on_actionSourceProperties_triggered()));
  1821. }
  1822. popup.exec(QCursor::pos());
  1823. }
  1824. void OBSBasic::on_sources_customContextMenuRequested(const QPoint &pos)
  1825. {
  1826. CreateSourcePopupMenu(ui->sources->itemAt(pos), false);
  1827. }
  1828. void OBSBasic::on_sources_itemDoubleClicked(QListWidgetItem *witem)
  1829. {
  1830. if (!witem)
  1831. return;
  1832. OBSSceneItem item = GetSceneItem(witem);
  1833. OBSSource source = obs_sceneitem_get_source(item);
  1834. if (source)
  1835. CreatePropertiesWindow(source);
  1836. }
  1837. void OBSBasic::AddSource(const char *id)
  1838. {
  1839. if (id && *id) {
  1840. OBSBasicSourceSelect sourceSelect(this, id);
  1841. sourceSelect.exec();
  1842. }
  1843. }
  1844. QMenu *OBSBasic::CreateAddSourcePopupMenu()
  1845. {
  1846. const char *type;
  1847. bool foundValues = false;
  1848. size_t idx = 0;
  1849. QMenu *popup = new QMenu(QTStr("Add"), this);
  1850. while (obs_enum_input_types(idx++, &type)) {
  1851. const char *name = obs_source_get_display_name(
  1852. OBS_SOURCE_TYPE_INPUT, type);
  1853. if (strcmp(type, "scene") == 0)
  1854. continue;
  1855. QAction *popupItem = new QAction(QT_UTF8(name), this);
  1856. popupItem->setData(QT_UTF8(type));
  1857. connect(popupItem, SIGNAL(triggered(bool)),
  1858. this, SLOT(AddSourceFromAction()));
  1859. popup->addAction(popupItem);
  1860. foundValues = true;
  1861. }
  1862. if (!foundValues) {
  1863. delete popup;
  1864. popup = nullptr;
  1865. }
  1866. return popup;
  1867. }
  1868. void OBSBasic::AddSourceFromAction()
  1869. {
  1870. QAction *action = qobject_cast<QAction*>(sender());
  1871. if (!action)
  1872. return;
  1873. AddSource(QT_TO_UTF8(action->data().toString()));
  1874. }
  1875. void OBSBasic::AddSourcePopupMenu(const QPoint &pos)
  1876. {
  1877. if (!GetCurrentScene()) {
  1878. // Tell the user he needs a scene first (help beginners).
  1879. QMessageBox::information(this,
  1880. QTStr("Basic.Main.AddSourceHelp.Title"),
  1881. QTStr("Basic.Main.AddSourceHelp.Text"));
  1882. return;
  1883. }
  1884. QPointer<QMenu> popup = CreateAddSourcePopupMenu();
  1885. if (popup)
  1886. popup->exec(pos);
  1887. }
  1888. void OBSBasic::on_actionAddSource_triggered()
  1889. {
  1890. AddSourcePopupMenu(QCursor::pos());
  1891. }
  1892. void OBSBasic::on_actionRemoveSource_triggered()
  1893. {
  1894. OBSSceneItem item = GetCurrentSceneItem();
  1895. obs_source_t *source = obs_sceneitem_get_source(item);
  1896. if (source && QueryRemoveSource(source))
  1897. obs_sceneitem_remove(item);
  1898. }
  1899. void OBSBasic::on_actionInteract_triggered()
  1900. {
  1901. OBSSceneItem item = GetCurrentSceneItem();
  1902. OBSSource source = obs_sceneitem_get_source(item);
  1903. if (source)
  1904. CreateInteractionWindow(source);
  1905. }
  1906. void OBSBasic::on_actionSourceProperties_triggered()
  1907. {
  1908. OBSSceneItem item = GetCurrentSceneItem();
  1909. OBSSource source = obs_sceneitem_get_source(item);
  1910. if (source)
  1911. CreatePropertiesWindow(source);
  1912. }
  1913. void OBSBasic::on_actionSourceUp_triggered()
  1914. {
  1915. OBSSceneItem item = GetCurrentSceneItem();
  1916. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_UP);
  1917. }
  1918. void OBSBasic::on_actionSourceDown_triggered()
  1919. {
  1920. OBSSceneItem item = GetCurrentSceneItem();
  1921. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_DOWN);
  1922. }
  1923. void OBSBasic::on_actionMoveUp_triggered()
  1924. {
  1925. OBSSceneItem item = GetCurrentSceneItem();
  1926. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_UP);
  1927. }
  1928. void OBSBasic::on_actionMoveDown_triggered()
  1929. {
  1930. OBSSceneItem item = GetCurrentSceneItem();
  1931. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_DOWN);
  1932. }
  1933. void OBSBasic::on_actionMoveToTop_triggered()
  1934. {
  1935. OBSSceneItem item = GetCurrentSceneItem();
  1936. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_TOP);
  1937. }
  1938. void OBSBasic::on_actionMoveToBottom_triggered()
  1939. {
  1940. OBSSceneItem item = GetCurrentSceneItem();
  1941. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_BOTTOM);
  1942. }
  1943. static BPtr<char> ReadLogFile(const char *log)
  1944. {
  1945. char logDir[512];
  1946. if (GetConfigPath(logDir, sizeof(logDir), "obs-studio/logs") <= 0)
  1947. return nullptr;
  1948. string path = (char*)logDir;
  1949. path += "/";
  1950. path += log;
  1951. BPtr<char> file = os_quick_read_utf8_file(path.c_str());
  1952. if (!file)
  1953. blog(LOG_WARNING, "Failed to read log file %s", path.c_str());
  1954. return file;
  1955. }
  1956. void OBSBasic::UploadLog(const char *file)
  1957. {
  1958. BPtr<char> fileString{ReadLogFile(file)};
  1959. if (!fileString)
  1960. return;
  1961. if (!*fileString)
  1962. return;
  1963. ui->menuLogFiles->setEnabled(false);
  1964. auto data_deleter = [](obs_data_t *d) { obs_data_release(d); };
  1965. using data_t = unique_ptr<struct obs_data, decltype(data_deleter)>;
  1966. data_t content{obs_data_create(), data_deleter};
  1967. data_t files{obs_data_create(), data_deleter};
  1968. data_t request{obs_data_create(), data_deleter};
  1969. obs_data_set_string(content.get(), "content", fileString);
  1970. obs_data_set_obj(files.get(), file, content.get());
  1971. stringstream ss;
  1972. ss << "OBS " << App()->GetVersionString()
  1973. << " log file uploaded at " << CurrentDateTimeString();
  1974. obs_data_set_string(request.get(), "description", ss.str().c_str());
  1975. obs_data_set_bool(request.get(), "public", false);
  1976. obs_data_set_obj(request.get(), "files", files.get());
  1977. const char *json = obs_data_get_json(request.get());
  1978. if (!json) {
  1979. blog(LOG_ERROR, "Failed to get JSON data for log upload");
  1980. return;
  1981. }
  1982. QBuffer *postData = new QBuffer();
  1983. postData->setData(json, (int) strlen(json));
  1984. if (logUploadThread) {
  1985. logUploadThread->wait();
  1986. delete logUploadThread;
  1987. }
  1988. RemoteTextThread *thread = new RemoteTextThread(
  1989. "https://api.github.com/gists",
  1990. "application/json", json);
  1991. logUploadThread = thread;
  1992. connect(thread, &RemoteTextThread::Result,
  1993. this, &OBSBasic::logUploadFinished);
  1994. logUploadThread->start();
  1995. }
  1996. void OBSBasic::on_actionShowLogs_triggered()
  1997. {
  1998. char logDir[512];
  1999. if (GetConfigPath(logDir, sizeof(logDir), "obs-studio/logs") <= 0)
  2000. return;
  2001. QUrl url = QUrl::fromLocalFile(QT_UTF8(logDir));
  2002. QDesktopServices::openUrl(url);
  2003. }
  2004. void OBSBasic::on_actionUploadCurrentLog_triggered()
  2005. {
  2006. UploadLog(App()->GetCurrentLog());
  2007. }
  2008. void OBSBasic::on_actionUploadLastLog_triggered()
  2009. {
  2010. UploadLog(App()->GetLastLog());
  2011. }
  2012. void OBSBasic::on_actionViewCurrentLog_triggered()
  2013. {
  2014. char logDir[512];
  2015. if (GetConfigPath(logDir, sizeof(logDir), "obs-studio/logs") <= 0)
  2016. return;
  2017. const char* log = App()->GetCurrentLog();
  2018. string path = (char*)logDir;
  2019. path += "/";
  2020. path += log;
  2021. QUrl url = QUrl::fromLocalFile(QT_UTF8(path.c_str()));
  2022. QDesktopServices::openUrl(url);
  2023. }
  2024. void OBSBasic::on_actionCheckForUpdates_triggered()
  2025. {
  2026. CheckForUpdates();
  2027. }
  2028. void OBSBasic::logUploadFinished(const QString &text, const QString &error)
  2029. {
  2030. ui->menuLogFiles->setEnabled(true);
  2031. if (text.isEmpty()) {
  2032. QMessageBox::information(this,
  2033. QTStr("LogReturnDialog.ErrorUploadingLog"),
  2034. error);
  2035. return;
  2036. }
  2037. obs_data_t *returnData = obs_data_create_from_json(QT_TO_UTF8(text));
  2038. QString logURL = obs_data_get_string(returnData, "html_url");
  2039. obs_data_release(returnData);
  2040. OBSLogReply logDialog(this, logURL);
  2041. logDialog.exec();
  2042. }
  2043. static void RenameListItem(OBSBasic *parent, QListWidget *listWidget,
  2044. obs_source_t *source, const string &name)
  2045. {
  2046. const char *prevName = obs_source_get_name(source);
  2047. if (name == prevName)
  2048. return;
  2049. obs_source_t *foundSource = obs_get_source_by_name(name.c_str());
  2050. QListWidgetItem *listItem = listWidget->currentItem();
  2051. if (foundSource || name.empty()) {
  2052. listItem->setText(QT_UTF8(prevName));
  2053. if (foundSource) {
  2054. QMessageBox::information(parent,
  2055. QTStr("NameExists.Title"),
  2056. QTStr("NameExists.Text"));
  2057. } else if (name.empty()) {
  2058. QMessageBox::information(parent,
  2059. QTStr("NoNameEntered.Title"),
  2060. QTStr("NoNameEntered.Text"));
  2061. }
  2062. obs_source_release(foundSource);
  2063. } else {
  2064. listItem->setText(QT_UTF8(name.c_str()));
  2065. obs_source_set_name(source, name.c_str());
  2066. }
  2067. }
  2068. void OBSBasic::SceneNameEdited(QWidget *editor,
  2069. QAbstractItemDelegate::EndEditHint endHint)
  2070. {
  2071. OBSScene scene = GetCurrentScene();
  2072. QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
  2073. string text = QT_TO_UTF8(edit->text().trimmed());
  2074. if (!scene)
  2075. return;
  2076. obs_source_t *source = obs_scene_get_source(scene);
  2077. RenameListItem(this, ui->scenes, source, text);
  2078. UNUSED_PARAMETER(endHint);
  2079. }
  2080. void OBSBasic::SceneItemNameEdited(QWidget *editor,
  2081. QAbstractItemDelegate::EndEditHint endHint)
  2082. {
  2083. OBSSceneItem item = GetCurrentSceneItem();
  2084. QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
  2085. string text = QT_TO_UTF8(edit->text().trimmed());
  2086. if (!item)
  2087. return;
  2088. obs_source_t *source = obs_sceneitem_get_source(item);
  2089. RenameListItem(this, ui->sources, source, text);
  2090. QListWidgetItem *listItem = ui->sources->currentItem();
  2091. listItem->setText(QString());
  2092. SetupVisibilityItem(ui->sources, listItem, item);
  2093. UNUSED_PARAMETER(endHint);
  2094. }
  2095. void OBSBasic::OpenFilters()
  2096. {
  2097. OBSSceneItem item = GetCurrentSceneItem();
  2098. OBSSource source = obs_sceneitem_get_source(item);
  2099. CreateFiltersWindow(source);
  2100. }
  2101. void OBSBasic::OpenSceneFilters()
  2102. {
  2103. OBSScene scene = GetCurrentScene();
  2104. OBSSource source = obs_scene_get_source(scene);
  2105. CreateFiltersWindow(source);
  2106. }
  2107. void OBSBasic::StartStreaming()
  2108. {
  2109. SaveProject();
  2110. if (outputHandler->StreamingActive())
  2111. return;
  2112. if (outputHandler->StartStreaming(service)) {
  2113. ui->streamButton->setEnabled(false);
  2114. ui->streamButton->setText(QTStr("Basic.Main.Connecting"));
  2115. }
  2116. }
  2117. void OBSBasic::StopStreaming()
  2118. {
  2119. SaveProject();
  2120. if (outputHandler->StreamingActive())
  2121. outputHandler->StopStreaming();
  2122. }
  2123. void OBSBasic::StreamingStart()
  2124. {
  2125. ui->streamButton->setText(QTStr("Basic.Main.StopStreaming"));
  2126. ui->streamButton->setEnabled(true);
  2127. ui->statusbar->StreamStarted(outputHandler->streamOutput);
  2128. }
  2129. void OBSBasic::StreamingStop(int code)
  2130. {
  2131. const char *errorMessage;
  2132. switch (code) {
  2133. case OBS_OUTPUT_BAD_PATH:
  2134. errorMessage = Str("Output.ConnectFail.BadPath");
  2135. break;
  2136. case OBS_OUTPUT_CONNECT_FAILED:
  2137. errorMessage = Str("Output.ConnectFail.ConnectFailed");
  2138. break;
  2139. case OBS_OUTPUT_INVALID_STREAM:
  2140. errorMessage = Str("Output.ConnectFail.InvalidStream");
  2141. break;
  2142. default:
  2143. case OBS_OUTPUT_ERROR:
  2144. errorMessage = Str("Output.ConnectFail.Error");
  2145. break;
  2146. case OBS_OUTPUT_DISCONNECTED:
  2147. /* doesn't happen if output is set to reconnect. note that
  2148. * reconnects are handled in the output, not in the UI */
  2149. errorMessage = Str("Output.ConnectFail.Disconnected");
  2150. }
  2151. ui->statusbar->StreamStopped();
  2152. ui->streamButton->setText(QTStr("Basic.Main.StartStreaming"));
  2153. ui->streamButton->setEnabled(true);
  2154. if (code != OBS_OUTPUT_SUCCESS)
  2155. QMessageBox::information(this,
  2156. QTStr("Output.ConnectFail.Title"),
  2157. QT_UTF8(errorMessage));
  2158. }
  2159. void OBSBasic::StartRecording()
  2160. {
  2161. SaveProject();
  2162. if (!outputHandler->RecordingActive())
  2163. outputHandler->StartRecording();
  2164. }
  2165. void OBSBasic::StopRecording()
  2166. {
  2167. SaveProject();
  2168. if (outputHandler->RecordingActive())
  2169. outputHandler->StopRecording();
  2170. }
  2171. void OBSBasic::RecordingStart()
  2172. {
  2173. ui->statusbar->RecordingStarted(outputHandler->fileOutput);
  2174. ui->recordButton->setText(QTStr("Basic.Main.StopRecording"));
  2175. }
  2176. void OBSBasic::RecordingStop()
  2177. {
  2178. ui->statusbar->RecordingStopped();
  2179. ui->recordButton->setText(QTStr("Basic.Main.StartRecording"));
  2180. }
  2181. void OBSBasic::on_streamButton_clicked()
  2182. {
  2183. if (outputHandler->StreamingActive()) {
  2184. StopStreaming();
  2185. } else {
  2186. StartStreaming();
  2187. }
  2188. }
  2189. void OBSBasic::on_recordButton_clicked()
  2190. {
  2191. if (outputHandler->RecordingActive())
  2192. StopRecording();
  2193. else
  2194. StartRecording();
  2195. }
  2196. void OBSBasic::on_settingsButton_clicked()
  2197. {
  2198. OBSBasicSettings settings(this);
  2199. settings.exec();
  2200. }
  2201. void OBSBasic::on_preview_customContextMenuRequested(const QPoint &pos)
  2202. {
  2203. CreateSourcePopupMenu(ui->sources->currentItem(), true);
  2204. UNUSED_PARAMETER(pos);
  2205. }
  2206. void OBSBasic::on_previewDisabledLabel_customContextMenuRequested(
  2207. const QPoint &pos)
  2208. {
  2209. QMenu popup(this);
  2210. QPointer<QMenu> previewProjector;
  2211. QAction *action = popup.addAction(
  2212. QTStr("Basic.Main.PreviewConextMenu.Enable"),
  2213. this, SLOT(TogglePreview()));
  2214. action->setCheckable(true);
  2215. action->setChecked(obs_preview_enabled());
  2216. previewProjector = new QMenu(QTStr("PreviewProjector"));
  2217. AddProjectorMenuMonitors(previewProjector, this,
  2218. SLOT(OpenPreviewProjector()));
  2219. popup.addMenu(previewProjector);
  2220. popup.exec(QCursor::pos());
  2221. UNUSED_PARAMETER(pos);
  2222. }
  2223. void OBSBasic::GetFPSCommon(uint32_t &num, uint32_t &den) const
  2224. {
  2225. const char *val = config_get_string(basicConfig, "Video", "FPSCommon");
  2226. if (strcmp(val, "10") == 0) {
  2227. num = 10;
  2228. den = 1;
  2229. } else if (strcmp(val, "20") == 0) {
  2230. num = 20;
  2231. den = 1;
  2232. } else if (strcmp(val, "25") == 0) {
  2233. num = 25;
  2234. den = 1;
  2235. } else if (strcmp(val, "29.97") == 0) {
  2236. num = 30000;
  2237. den = 1001;
  2238. } else if (strcmp(val, "48") == 0) {
  2239. num = 48;
  2240. den = 1;
  2241. } else if (strcmp(val, "59.94") == 0) {
  2242. num = 60000;
  2243. den = 1001;
  2244. } else if (strcmp(val, "60") == 0) {
  2245. num = 60;
  2246. den = 1;
  2247. } else {
  2248. num = 30;
  2249. den = 1;
  2250. }
  2251. }
  2252. void OBSBasic::GetFPSInteger(uint32_t &num, uint32_t &den) const
  2253. {
  2254. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSInt");
  2255. den = 1;
  2256. }
  2257. void OBSBasic::GetFPSFraction(uint32_t &num, uint32_t &den) const
  2258. {
  2259. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNum");
  2260. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSDen");
  2261. }
  2262. void OBSBasic::GetFPSNanoseconds(uint32_t &num, uint32_t &den) const
  2263. {
  2264. num = 1000000000;
  2265. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNS");
  2266. }
  2267. void OBSBasic::GetConfigFPS(uint32_t &num, uint32_t &den) const
  2268. {
  2269. uint32_t type = config_get_uint(basicConfig, "Video", "FPSType");
  2270. if (type == 1) //"Integer"
  2271. GetFPSInteger(num, den);
  2272. else if (type == 2) //"Fraction"
  2273. GetFPSFraction(num, den);
  2274. else if (false) //"Nanoseconds", currently not implemented
  2275. GetFPSNanoseconds(num, den);
  2276. else
  2277. GetFPSCommon(num, den);
  2278. }
  2279. config_t *OBSBasic::Config() const
  2280. {
  2281. return basicConfig;
  2282. }
  2283. void OBSBasic::on_actionEditTransform_triggered()
  2284. {
  2285. if (transformWindow)
  2286. transformWindow->close();
  2287. transformWindow = new OBSBasicTransform(this);
  2288. transformWindow->show();
  2289. transformWindow->setAttribute(Qt::WA_DeleteOnClose, true);
  2290. }
  2291. void OBSBasic::on_actionResetTransform_triggered()
  2292. {
  2293. auto func = [] (obs_scene_t *scene, obs_sceneitem_t *item, void *param)
  2294. {
  2295. if (!obs_sceneitem_selected(item))
  2296. return true;
  2297. obs_transform_info info;
  2298. vec2_set(&info.pos, 0.0f, 0.0f);
  2299. vec2_set(&info.scale, 1.0f, 1.0f);
  2300. info.rot = 0.0f;
  2301. info.alignment = OBS_ALIGN_TOP | OBS_ALIGN_LEFT;
  2302. info.bounds_type = OBS_BOUNDS_NONE;
  2303. info.bounds_alignment = OBS_ALIGN_CENTER;
  2304. vec2_set(&info.bounds, 0.0f, 0.0f);
  2305. obs_sceneitem_set_info(item, &info);
  2306. UNUSED_PARAMETER(scene);
  2307. UNUSED_PARAMETER(param);
  2308. return true;
  2309. };
  2310. obs_scene_enum_items(GetCurrentScene(), func, nullptr);
  2311. }
  2312. static void GetItemBox(obs_sceneitem_t *item, vec3 &tl, vec3 &br)
  2313. {
  2314. matrix4 boxTransform;
  2315. obs_sceneitem_get_box_transform(item, &boxTransform);
  2316. vec3_set(&tl, M_INFINITE, M_INFINITE, 0.0f);
  2317. vec3_set(&br, -M_INFINITE, -M_INFINITE, 0.0f);
  2318. auto GetMinPos = [&] (float x, float y)
  2319. {
  2320. vec3 pos;
  2321. vec3_set(&pos, x, y, 0.0f);
  2322. vec3_transform(&pos, &pos, &boxTransform);
  2323. vec3_min(&tl, &tl, &pos);
  2324. vec3_max(&br, &br, &pos);
  2325. };
  2326. GetMinPos(0.0f, 0.0f);
  2327. GetMinPos(1.0f, 0.0f);
  2328. GetMinPos(0.0f, 1.0f);
  2329. GetMinPos(1.0f, 1.0f);
  2330. }
  2331. static vec3 GetItemTL(obs_sceneitem_t *item)
  2332. {
  2333. vec3 tl, br;
  2334. GetItemBox(item, tl, br);
  2335. return tl;
  2336. }
  2337. static void SetItemTL(obs_sceneitem_t *item, const vec3 &tl)
  2338. {
  2339. vec3 newTL;
  2340. vec2 pos;
  2341. obs_sceneitem_get_pos(item, &pos);
  2342. newTL = GetItemTL(item);
  2343. pos.x += tl.x - newTL.x;
  2344. pos.y += tl.y - newTL.y;
  2345. obs_sceneitem_set_pos(item, &pos);
  2346. }
  2347. static bool RotateSelectedSources(obs_scene_t *scene, obs_sceneitem_t *item,
  2348. void *param)
  2349. {
  2350. if (!obs_sceneitem_selected(item))
  2351. return true;
  2352. float rot = *reinterpret_cast<float*>(param);
  2353. vec3 tl = GetItemTL(item);
  2354. rot += obs_sceneitem_get_rot(item);
  2355. if (rot >= 360.0f) rot -= 360.0f;
  2356. else if (rot <= -360.0f) rot += 360.0f;
  2357. obs_sceneitem_set_rot(item, rot);
  2358. SetItemTL(item, tl);
  2359. UNUSED_PARAMETER(scene);
  2360. UNUSED_PARAMETER(param);
  2361. return true;
  2362. };
  2363. void OBSBasic::on_actionRotate90CW_triggered()
  2364. {
  2365. float f90CW = 90.0f;
  2366. obs_scene_enum_items(GetCurrentScene(), RotateSelectedSources, &f90CW);
  2367. }
  2368. void OBSBasic::on_actionRotate90CCW_triggered()
  2369. {
  2370. float f90CCW = -90.0f;
  2371. obs_scene_enum_items(GetCurrentScene(), RotateSelectedSources, &f90CCW);
  2372. }
  2373. void OBSBasic::on_actionRotate180_triggered()
  2374. {
  2375. float f180 = 180.0f;
  2376. obs_scene_enum_items(GetCurrentScene(), RotateSelectedSources, &f180);
  2377. }
  2378. static bool MultiplySelectedItemScale(obs_scene_t *scene, obs_sceneitem_t *item,
  2379. void *param)
  2380. {
  2381. vec2 &mul = *reinterpret_cast<vec2*>(param);
  2382. if (!obs_sceneitem_selected(item))
  2383. return true;
  2384. vec3 tl = GetItemTL(item);
  2385. vec2 scale;
  2386. obs_sceneitem_get_scale(item, &scale);
  2387. vec2_mul(&scale, &scale, &mul);
  2388. obs_sceneitem_set_scale(item, &scale);
  2389. SetItemTL(item, tl);
  2390. UNUSED_PARAMETER(scene);
  2391. return true;
  2392. }
  2393. void OBSBasic::on_actionFlipHorizontal_triggered()
  2394. {
  2395. vec2 scale;
  2396. vec2_set(&scale, -1.0f, 1.0f);
  2397. obs_scene_enum_items(GetCurrentScene(), MultiplySelectedItemScale,
  2398. &scale);
  2399. }
  2400. void OBSBasic::on_actionFlipVertical_triggered()
  2401. {
  2402. vec2 scale;
  2403. vec2_set(&scale, 1.0f, -1.0f);
  2404. obs_scene_enum_items(GetCurrentScene(), MultiplySelectedItemScale,
  2405. &scale);
  2406. }
  2407. static bool CenterAlignSelectedItems(obs_scene_t *scene, obs_sceneitem_t *item,
  2408. void *param)
  2409. {
  2410. obs_bounds_type boundsType = *reinterpret_cast<obs_bounds_type*>(param);
  2411. if (!obs_sceneitem_selected(item))
  2412. return true;
  2413. obs_video_info ovi;
  2414. obs_get_video_info(&ovi);
  2415. obs_transform_info itemInfo;
  2416. vec2_set(&itemInfo.pos, 0.0f, 0.0f);
  2417. vec2_set(&itemInfo.scale, 1.0f, 1.0f);
  2418. itemInfo.alignment = OBS_ALIGN_LEFT | OBS_ALIGN_TOP;
  2419. itemInfo.rot = 0.0f;
  2420. vec2_set(&itemInfo.bounds,
  2421. float(ovi.base_width), float(ovi.base_height));
  2422. itemInfo.bounds_type = boundsType;
  2423. itemInfo.bounds_alignment = OBS_ALIGN_CENTER;
  2424. obs_sceneitem_set_info(item, &itemInfo);
  2425. UNUSED_PARAMETER(scene);
  2426. return true;
  2427. }
  2428. void OBSBasic::on_actionFitToScreen_triggered()
  2429. {
  2430. obs_bounds_type boundsType = OBS_BOUNDS_SCALE_INNER;
  2431. obs_scene_enum_items(GetCurrentScene(), CenterAlignSelectedItems,
  2432. &boundsType);
  2433. }
  2434. void OBSBasic::on_actionStretchToScreen_triggered()
  2435. {
  2436. obs_bounds_type boundsType = OBS_BOUNDS_STRETCH;
  2437. obs_scene_enum_items(GetCurrentScene(), CenterAlignSelectedItems,
  2438. &boundsType);
  2439. }
  2440. void OBSBasic::on_actionCenterToScreen_triggered()
  2441. {
  2442. auto func = [] (obs_scene_t *scene, obs_sceneitem_t *item, void *param)
  2443. {
  2444. vec3 tl, br, itemCenter, screenCenter, offset;
  2445. obs_video_info ovi;
  2446. if (!obs_sceneitem_selected(item))
  2447. return true;
  2448. obs_get_video_info(&ovi);
  2449. vec3_set(&screenCenter, float(ovi.base_width),
  2450. float(ovi.base_height), 0.0f);
  2451. vec3_mulf(&screenCenter, &screenCenter, 0.5f);
  2452. GetItemBox(item, tl, br);
  2453. vec3_sub(&itemCenter, &br, &tl);
  2454. vec3_mulf(&itemCenter, &itemCenter, 0.5f);
  2455. vec3_add(&itemCenter, &itemCenter, &tl);
  2456. vec3_sub(&offset, &screenCenter, &itemCenter);
  2457. vec3_add(&tl, &tl, &offset);
  2458. SetItemTL(item, tl);
  2459. UNUSED_PARAMETER(scene);
  2460. UNUSED_PARAMETER(param);
  2461. return true;
  2462. };
  2463. obs_scene_enum_items(GetCurrentScene(), func, nullptr);
  2464. }
  2465. void OBSBasic::TogglePreview()
  2466. {
  2467. bool enabled = !obs_preview_enabled();
  2468. obs_preview_set_enabled(enabled);
  2469. ui->preview->setVisible(enabled);
  2470. ui->previewDisabledLabel->setVisible(!enabled);
  2471. }
  2472. void OBSBasic::Nudge(int dist, MoveDir dir)
  2473. {
  2474. struct MoveInfo {
  2475. float dist;
  2476. MoveDir dir;
  2477. } info = {(float)dist, dir};
  2478. auto func = [] (obs_scene_t*, obs_sceneitem_t *item, void *param)
  2479. {
  2480. MoveInfo *info = reinterpret_cast<MoveInfo*>(param);
  2481. struct vec2 dir;
  2482. struct vec2 pos;
  2483. vec2_set(&dir, 0.0f, 0.0f);
  2484. if (!obs_sceneitem_selected(item))
  2485. return true;
  2486. switch (info->dir) {
  2487. case MoveDir::Up: dir.y = -info->dist; break;
  2488. case MoveDir::Down: dir.y = info->dist; break;
  2489. case MoveDir::Left: dir.x = -info->dist; break;
  2490. case MoveDir::Right: dir.x = info->dist; break;
  2491. }
  2492. obs_sceneitem_get_pos(item, &pos);
  2493. vec2_add(&pos, &pos, &dir);
  2494. obs_sceneitem_set_pos(item, &pos);
  2495. return true;
  2496. };
  2497. obs_scene_enum_items(GetCurrentScene(), func, &info);
  2498. }
  2499. void OBSBasic::NudgeUp() {Nudge(1, MoveDir::Up);}
  2500. void OBSBasic::NudgeDown() {Nudge(1, MoveDir::Down);}
  2501. void OBSBasic::NudgeLeft() {Nudge(1, MoveDir::Left);}
  2502. void OBSBasic::NudgeRight() {Nudge(1, MoveDir::Right);}
  2503. void OBSBasic::OpenProjector(obs_source_t *source, int monitor)
  2504. {
  2505. /* seriously? 10 monitors? */
  2506. if (monitor > 9)
  2507. return;
  2508. delete projectors[monitor];
  2509. projectors[monitor].clear();
  2510. OBSProjector *projector = new OBSProjector(this, source);
  2511. projector->Init(monitor);
  2512. projectors[monitor] = projector;
  2513. }
  2514. void OBSBasic::OpenPreviewProjector()
  2515. {
  2516. int monitor = sender()->property("monitor").toInt();
  2517. OpenProjector(nullptr, monitor);
  2518. }
  2519. void OBSBasic::OpenSourceProjector()
  2520. {
  2521. int monitor = sender()->property("monitor").toInt();
  2522. OBSSceneItem item = GetCurrentSceneItem();
  2523. if (!item)
  2524. return;
  2525. OpenProjector(obs_sceneitem_get_source(item), monitor);
  2526. }
  2527. void OBSBasic::OpenSceneProjector()
  2528. {
  2529. int monitor = sender()->property("monitor").toInt();
  2530. OBSScene scene = GetCurrentScene();
  2531. if (!scene)
  2532. return;
  2533. OpenProjector(obs_scene_get_source(scene), monitor);
  2534. }