window-basic-main.cpp 81 KB

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