window-projector.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  1. #include <QAction>
  2. #include <QGuiApplication>
  3. #include <QMouseEvent>
  4. #include <QMenu>
  5. #include <QScreen>
  6. #include "obs-app.hpp"
  7. #include "window-basic-main.hpp"
  8. #include "display-helpers.hpp"
  9. #include "qt-wrappers.hpp"
  10. #include "platform.hpp"
  11. static QList<OBSProjector *> multiviewProjectors;
  12. static QList<OBSProjector *> allProjectors;
  13. static bool updatingMultiview = false, drawLabel, drawSafeArea, mouseSwitching,
  14. transitionOnDoubleClick;
  15. static MultiviewLayout multiviewLayout;
  16. static size_t maxSrcs, numSrcs;
  17. OBSProjector::OBSProjector(QWidget *widget, obs_source_t *source_, int monitor,
  18. ProjectorType type_)
  19. : OBSQTDisplay(widget, Qt::Window),
  20. source(source_),
  21. removedSignal(obs_source_get_signal_handler(source), "remove",
  22. OBSSourceRemoved, this)
  23. {
  24. isAlwaysOnTop = config_get_bool(GetGlobalConfig(), "BasicWindow",
  25. "ProjectorAlwaysOnTop");
  26. if (isAlwaysOnTop)
  27. setWindowFlags(Qt::WindowStaysOnTopHint);
  28. type = type_;
  29. #ifdef __APPLE__
  30. setWindowIcon(
  31. QIcon::fromTheme("obs", QIcon(":/res/images/obs_256x256.png")));
  32. #else
  33. setWindowIcon(QIcon::fromTheme("obs", QIcon(":/res/images/obs.png")));
  34. #endif
  35. if (monitor == -1)
  36. resize(480, 270);
  37. else
  38. SetMonitor(monitor);
  39. UpdateProjectorTitle(QT_UTF8(obs_source_get_name(source)));
  40. QAction *action = new QAction(this);
  41. action->setShortcut(Qt::Key_Escape);
  42. addAction(action);
  43. connect(action, SIGNAL(triggered()), this, SLOT(EscapeTriggered()));
  44. setAttribute(Qt::WA_DeleteOnClose, true);
  45. //disable application quit when last window closed
  46. setAttribute(Qt::WA_QuitOnClose, false);
  47. installEventFilter(CreateShortcutFilter());
  48. auto addDrawCallback = [this]() {
  49. bool isMultiview = type == ProjectorType::Multiview;
  50. obs_display_add_draw_callback(
  51. GetDisplay(),
  52. isMultiview ? OBSRenderMultiview : OBSRender, this);
  53. obs_display_set_background_color(GetDisplay(), 0x000000);
  54. };
  55. connect(this, &OBSQTDisplay::DisplayCreated, addDrawCallback);
  56. connect(App(), &QGuiApplication::screenRemoved, this,
  57. &OBSProjector::ScreenRemoved);
  58. if (type == ProjectorType::Multiview) {
  59. InitSafeAreas(&actionSafeMargin, &graphicsSafeMargin,
  60. &fourByThreeSafeMargin, &leftLine, &topLine,
  61. &rightLine);
  62. UpdateMultiview();
  63. multiviewProjectors.push_back(this);
  64. }
  65. App()->IncrementSleepInhibition();
  66. if (source)
  67. obs_source_inc_showing(source);
  68. allProjectors.push_back(this);
  69. ready = true;
  70. show();
  71. // We need it here to allow keyboard input in X11 to listen to Escape
  72. activateWindow();
  73. }
  74. OBSProjector::~OBSProjector()
  75. {
  76. bool isMultiview = type == ProjectorType::Multiview;
  77. obs_display_remove_draw_callback(
  78. GetDisplay(), isMultiview ? OBSRenderMultiview : OBSRender,
  79. this);
  80. if (source)
  81. obs_source_dec_showing(source);
  82. if (isMultiview) {
  83. for (OBSWeakSource &weakSrc : multiviewScenes) {
  84. OBSSource src = OBSGetStrongRef(weakSrc);
  85. if (src)
  86. obs_source_dec_showing(src);
  87. }
  88. obs_enter_graphics();
  89. gs_vertexbuffer_destroy(actionSafeMargin);
  90. gs_vertexbuffer_destroy(graphicsSafeMargin);
  91. gs_vertexbuffer_destroy(fourByThreeSafeMargin);
  92. gs_vertexbuffer_destroy(leftLine);
  93. gs_vertexbuffer_destroy(topLine);
  94. gs_vertexbuffer_destroy(rightLine);
  95. obs_leave_graphics();
  96. }
  97. if (type == ProjectorType::Multiview)
  98. multiviewProjectors.removeAll(this);
  99. App()->DecrementSleepInhibition();
  100. screen = nullptr;
  101. }
  102. void OBSProjector::SetMonitor(int monitor)
  103. {
  104. savedMonitor = monitor;
  105. screen = QGuiApplication::screens()[monitor];
  106. setGeometry(screen->geometry());
  107. showFullScreen();
  108. SetHideCursor();
  109. }
  110. void OBSProjector::SetHideCursor()
  111. {
  112. if (savedMonitor == -1)
  113. return;
  114. bool hideCursor = config_get_bool(GetGlobalConfig(), "BasicWindow",
  115. "HideProjectorCursor");
  116. if (hideCursor && type != ProjectorType::Multiview)
  117. setCursor(Qt::BlankCursor);
  118. else
  119. setCursor(Qt::ArrowCursor);
  120. }
  121. static OBSSource CreateLabel(const char *name, size_t h)
  122. {
  123. obs_data_t *settings = obs_data_create();
  124. obs_data_t *font = obs_data_create();
  125. std::string text;
  126. text += " ";
  127. text += name;
  128. text += " ";
  129. #if defined(_WIN32)
  130. obs_data_set_string(font, "face", "Arial");
  131. #elif defined(__APPLE__)
  132. obs_data_set_string(font, "face", "Helvetica");
  133. #else
  134. obs_data_set_string(font, "face", "Monospace");
  135. #endif
  136. obs_data_set_int(font, "flags", 1); // Bold text
  137. obs_data_set_int(font, "size", int(h / 9.81));
  138. obs_data_set_obj(settings, "font", font);
  139. obs_data_set_string(settings, "text", text.c_str());
  140. obs_data_set_bool(settings, "outline", false);
  141. #ifdef _WIN32
  142. const char *text_source_id = "text_gdiplus";
  143. #else
  144. const char *text_source_id = "text_ft2_source";
  145. #endif
  146. OBSSource txtSource =
  147. obs_source_create_private(text_source_id, name, settings);
  148. obs_source_release(txtSource);
  149. obs_data_release(font);
  150. obs_data_release(settings);
  151. return txtSource;
  152. }
  153. static inline uint32_t labelOffset(obs_source_t *label, uint32_t cx)
  154. {
  155. uint32_t w = obs_source_get_width(label);
  156. int n; // Number of scenes per row
  157. switch (multiviewLayout) {
  158. case MultiviewLayout::HORIZONTAL_TOP_18_SCENES:
  159. n = 6;
  160. break;
  161. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  162. n = 6;
  163. break;
  164. default:
  165. n = 4;
  166. break;
  167. }
  168. w = uint32_t(w * ((1.0f) / n));
  169. return (cx / 2) - w;
  170. }
  171. static inline void startRegion(int vX, int vY, int vCX, int vCY, float oL,
  172. float oR, float oT, float oB)
  173. {
  174. gs_projection_push();
  175. gs_viewport_push();
  176. gs_set_viewport(vX, vY, vCX, vCY);
  177. gs_ortho(oL, oR, oT, oB, -100.0f, 100.0f);
  178. }
  179. static inline void endRegion()
  180. {
  181. gs_viewport_pop();
  182. gs_projection_pop();
  183. }
  184. void OBSProjector::OBSRenderMultiview(void *data, uint32_t cx, uint32_t cy)
  185. {
  186. OBSProjector *window = (OBSProjector *)data;
  187. if (updatingMultiview || !window->ready)
  188. return;
  189. OBSBasic *main = (OBSBasic *)obs_frontend_get_main_window();
  190. uint32_t targetCX, targetCY;
  191. int x, y;
  192. float scale;
  193. targetCX = (uint32_t)window->fw;
  194. targetCY = (uint32_t)window->fh;
  195. GetScaleAndCenterPos(targetCX, targetCY, cx, cy, x, y, scale);
  196. OBSSource previewSrc = main->GetCurrentSceneSource();
  197. OBSSource programSrc = main->GetProgramSource();
  198. bool studioMode = main->IsPreviewProgramMode();
  199. auto drawBox = [&](float cx, float cy, uint32_t colorVal) {
  200. gs_effect_t *solid = obs_get_base_effect(OBS_EFFECT_SOLID);
  201. gs_eparam_t *color =
  202. gs_effect_get_param_by_name(solid, "color");
  203. gs_effect_set_color(color, colorVal);
  204. while (gs_effect_loop(solid, "Solid"))
  205. gs_draw_sprite(nullptr, 0, (uint32_t)cx, (uint32_t)cy);
  206. };
  207. auto setRegion = [&](float bx, float by, float cx, float cy) {
  208. float vX = int(x + bx * scale);
  209. float vY = int(y + by * scale);
  210. float vCX = int(cx * scale);
  211. float vCY = int(cy * scale);
  212. float oL = bx;
  213. float oT = by;
  214. float oR = (bx + cx);
  215. float oB = (by + cy);
  216. startRegion(vX, vY, vCX, vCY, oL, oR, oT, oB);
  217. };
  218. auto calcBaseSource = [&](size_t i) {
  219. switch (multiviewLayout) {
  220. case MultiviewLayout::HORIZONTAL_TOP_18_SCENES:
  221. window->sourceX = (i % 6) * window->scenesCX;
  222. window->sourceY =
  223. window->pvwprgCY + (i / 6) * window->scenesCY;
  224. break;
  225. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  226. window->sourceX = (i % 6) * window->scenesCX;
  227. window->sourceY =
  228. window->pvwprgCY + (i / 6) * window->scenesCY;
  229. break;
  230. case MultiviewLayout::VERTICAL_LEFT_8_SCENES:
  231. window->sourceX = window->pvwprgCX;
  232. window->sourceY = (i / 2) * window->scenesCY;
  233. if (i % 2 != 0)
  234. window->sourceX += window->scenesCX;
  235. break;
  236. case MultiviewLayout::VERTICAL_RIGHT_8_SCENES:
  237. window->sourceX = 0;
  238. window->sourceY = (i / 2) * window->scenesCY;
  239. if (i % 2 != 0)
  240. window->sourceX = window->scenesCX;
  241. break;
  242. case MultiviewLayout::HORIZONTAL_BOTTOM_8_SCENES:
  243. if (i < 4) {
  244. window->sourceX = (float(i) * window->scenesCX);
  245. window->sourceY = 0;
  246. } else {
  247. window->sourceX =
  248. (float(i - 4) * window->scenesCX);
  249. window->sourceY = window->scenesCY;
  250. }
  251. break;
  252. default: // MultiviewLayout::HORIZONTAL_TOP_8_SCENES:
  253. if (i < 4) {
  254. window->sourceX = (float(i) * window->scenesCX);
  255. window->sourceY = window->pvwprgCY;
  256. } else {
  257. window->sourceX =
  258. (float(i - 4) * window->scenesCX);
  259. window->sourceY =
  260. window->pvwprgCY + window->scenesCY;
  261. }
  262. }
  263. window->siX = window->sourceX + window->thickness;
  264. window->siY = window->sourceY + window->thickness;
  265. };
  266. auto calcPreviewProgram = [&](bool program) {
  267. switch (multiviewLayout) {
  268. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  269. window->sourceX =
  270. window->thickness + window->pvwprgCX / 2;
  271. window->sourceY = window->thickness;
  272. window->labelX = window->offset + window->pvwprgCX / 2;
  273. window->labelY = window->pvwprgCY * 0.85f;
  274. if (program) {
  275. window->sourceX += window->pvwprgCX;
  276. window->labelX += window->pvwprgCX;
  277. }
  278. break;
  279. case MultiviewLayout::VERTICAL_LEFT_8_SCENES:
  280. window->sourceX = window->thickness;
  281. window->sourceY = window->pvwprgCY + window->thickness;
  282. window->labelX = window->offset;
  283. window->labelY = window->pvwprgCY * 1.85f;
  284. if (program) {
  285. window->sourceY = window->thickness;
  286. window->labelY = window->pvwprgCY * 0.85f;
  287. }
  288. break;
  289. case MultiviewLayout::VERTICAL_RIGHT_8_SCENES:
  290. window->sourceX = window->pvwprgCX + window->thickness;
  291. window->sourceY = window->pvwprgCY + window->thickness;
  292. window->labelX = window->pvwprgCX + window->offset;
  293. window->labelY = window->pvwprgCY * 1.85f;
  294. if (program) {
  295. window->sourceY = window->thickness;
  296. window->labelY = window->pvwprgCY * 0.85f;
  297. }
  298. break;
  299. case MultiviewLayout::HORIZONTAL_BOTTOM_8_SCENES:
  300. window->sourceX = window->thickness;
  301. window->sourceY = window->pvwprgCY + window->thickness;
  302. window->labelX = window->offset;
  303. window->labelY = window->pvwprgCY * 1.85f;
  304. if (program) {
  305. window->sourceX += window->pvwprgCX;
  306. window->labelX += window->pvwprgCX;
  307. }
  308. break;
  309. default: // MultiviewLayout::HORIZONTAL_TOP_8_SCENES and 18_SCENES
  310. window->sourceX = window->thickness;
  311. window->sourceY = window->thickness;
  312. window->labelX = window->offset;
  313. window->labelY = window->pvwprgCY * 0.85f;
  314. if (program) {
  315. window->sourceX += window->pvwprgCX;
  316. window->labelX += window->pvwprgCX;
  317. }
  318. }
  319. };
  320. auto paintAreaWithColor = [&](float tx, float ty, float cx, float cy,
  321. uint32_t color) {
  322. gs_matrix_push();
  323. gs_matrix_translate3f(tx, ty, 0.0f);
  324. drawBox(cx, cy, color);
  325. gs_matrix_pop();
  326. };
  327. // Define the whole usable region for the multiview
  328. startRegion(x, y, targetCX * scale, targetCY * scale, 0.0f, window->fw,
  329. 0.0f, window->fh);
  330. // Change the background color to highlight all sources
  331. drawBox(window->fw, window->fh, outerColor);
  332. /* ----------------------------- */
  333. /* draw sources */
  334. for (size_t i = 0; i < maxSrcs; i++) {
  335. // Handle all the offsets
  336. calcBaseSource(i);
  337. if (i >= numSrcs) {
  338. // Just paint the background and continue
  339. paintAreaWithColor(window->sourceX, window->sourceY,
  340. window->scenesCX, window->scenesCY,
  341. outerColor);
  342. paintAreaWithColor(window->siX, window->siY,
  343. window->siCX, window->siCY,
  344. backgroundColor);
  345. continue;
  346. }
  347. OBSSource src = OBSGetStrongRef(window->multiviewScenes[i]);
  348. // We have a source. Now chose the proper highlight color
  349. uint32_t colorVal = outerColor;
  350. if (src == programSrc)
  351. colorVal = programColor;
  352. else if (src == previewSrc)
  353. colorVal = studioMode ? previewColor : programColor;
  354. // Paint the background
  355. paintAreaWithColor(window->sourceX, window->sourceY,
  356. window->scenesCX, window->scenesCY,
  357. colorVal);
  358. paintAreaWithColor(window->siX, window->siY, window->siCX,
  359. window->siCY, backgroundColor);
  360. /* ----------- */
  361. // Render the source
  362. gs_matrix_push();
  363. gs_matrix_translate3f(window->siX, window->siY, 0.0f);
  364. gs_matrix_scale3f(window->siScaleX, window->siScaleY, 1.0f);
  365. setRegion(window->siX, window->siY, window->siCX, window->siCY);
  366. obs_source_video_render(src);
  367. endRegion();
  368. gs_matrix_pop();
  369. /* ----------- */
  370. // Render the label
  371. if (!drawLabel)
  372. continue;
  373. obs_source *label = window->multiviewLabels[i + 2];
  374. if (!label)
  375. continue;
  376. window->offset = labelOffset(label, window->scenesCX);
  377. gs_matrix_push();
  378. gs_matrix_translate3f(
  379. window->sourceX + window->offset,
  380. (window->scenesCY * 0.85f) + window->sourceY, 0.0f);
  381. gs_matrix_scale3f(window->ppiScaleX, window->ppiScaleY, 1.0f);
  382. drawBox(obs_source_get_width(label),
  383. obs_source_get_height(label) +
  384. int(window->sourceY * 0.015f),
  385. labelColor);
  386. obs_source_video_render(label);
  387. gs_matrix_pop();
  388. }
  389. /* ----------------------------- */
  390. /* draw preview */
  391. obs_source_t *previewLabel = window->multiviewLabels[0];
  392. window->offset = labelOffset(previewLabel, window->pvwprgCX);
  393. calcPreviewProgram(false);
  394. // Paint the background
  395. paintAreaWithColor(window->sourceX, window->sourceY, window->ppiCX,
  396. window->ppiCY, backgroundColor);
  397. // Scale and Draw the preview
  398. gs_matrix_push();
  399. gs_matrix_translate3f(window->sourceX, window->sourceY, 0.0f);
  400. gs_matrix_scale3f(window->ppiScaleX, window->ppiScaleY, 1.0f);
  401. setRegion(window->sourceX, window->sourceY, window->ppiCX,
  402. window->ppiCY);
  403. if (studioMode)
  404. obs_source_video_render(previewSrc);
  405. else
  406. obs_render_main_texture();
  407. if (drawSafeArea) {
  408. RenderSafeAreas(window->actionSafeMargin, targetCX, targetCY);
  409. RenderSafeAreas(window->graphicsSafeMargin, targetCX, targetCY);
  410. RenderSafeAreas(window->fourByThreeSafeMargin, targetCX,
  411. targetCY);
  412. RenderSafeAreas(window->leftLine, targetCX, targetCY);
  413. RenderSafeAreas(window->topLine, targetCX, targetCY);
  414. RenderSafeAreas(window->rightLine, targetCX, targetCY);
  415. }
  416. endRegion();
  417. gs_matrix_pop();
  418. /* ----------- */
  419. // Draw the Label
  420. if (drawLabel) {
  421. gs_matrix_push();
  422. gs_matrix_translate3f(window->labelX, window->labelY, 0.0f);
  423. gs_matrix_scale3f(window->ppiScaleX, window->ppiScaleY, 1.0f);
  424. drawBox(obs_source_get_width(previewLabel),
  425. obs_source_get_height(previewLabel) +
  426. int(window->pvwprgCX * 0.015f),
  427. labelColor);
  428. obs_source_video_render(previewLabel);
  429. gs_matrix_pop();
  430. }
  431. /* ----------------------------- */
  432. /* draw program */
  433. obs_source_t *programLabel = window->multiviewLabels[1];
  434. window->offset = labelOffset(programLabel, window->pvwprgCX);
  435. calcPreviewProgram(true);
  436. paintAreaWithColor(window->sourceX, window->sourceY, window->ppiCX,
  437. window->ppiCY, backgroundColor);
  438. // Scale and Draw the program
  439. gs_matrix_push();
  440. gs_matrix_translate3f(window->sourceX, window->sourceY, 0.0f);
  441. gs_matrix_scale3f(window->ppiScaleX, window->ppiScaleY, 1.0f);
  442. setRegion(window->sourceX, window->sourceY, window->ppiCX,
  443. window->ppiCY);
  444. obs_render_main_texture();
  445. endRegion();
  446. gs_matrix_pop();
  447. /* ----------- */
  448. // Draw the Label
  449. if (drawLabel) {
  450. gs_matrix_push();
  451. gs_matrix_translate3f(window->labelX, window->labelY, 0.0f);
  452. gs_matrix_scale3f(window->ppiScaleX, window->ppiScaleY, 1.0f);
  453. drawBox(obs_source_get_width(programLabel),
  454. obs_source_get_height(programLabel) +
  455. int(window->pvwprgCX * 0.015f),
  456. labelColor);
  457. obs_source_video_render(programLabel);
  458. gs_matrix_pop();
  459. }
  460. // Region for future usage with additional info.
  461. if (multiviewLayout == MultiviewLayout::HORIZONTAL_TOP_24_SCENES) {
  462. // Just paint the background for now
  463. paintAreaWithColor(window->thickness, window->thickness,
  464. window->siCX,
  465. window->siCY * 2 + window->thicknessx2,
  466. backgroundColor);
  467. paintAreaWithColor(
  468. window->thickness +
  469. 2.5 * (window->thicknessx2 + window->ppiCX),
  470. window->thickness, window->siCX,
  471. window->siCY * 2 + window->thicknessx2,
  472. backgroundColor);
  473. }
  474. endRegion();
  475. }
  476. void OBSProjector::OBSRender(void *data, uint32_t cx, uint32_t cy)
  477. {
  478. OBSProjector *window = reinterpret_cast<OBSProjector *>(data);
  479. if (!window->ready)
  480. return;
  481. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  482. OBSSource source = window->source;
  483. uint32_t targetCX;
  484. uint32_t targetCY;
  485. int x, y;
  486. int newCX, newCY;
  487. float scale;
  488. if (source) {
  489. targetCX = std::max(obs_source_get_width(source), 1u);
  490. targetCY = std::max(obs_source_get_height(source), 1u);
  491. } else {
  492. struct obs_video_info ovi;
  493. obs_get_video_info(&ovi);
  494. targetCX = ovi.base_width;
  495. targetCY = ovi.base_height;
  496. }
  497. GetScaleAndCenterPos(targetCX, targetCY, cx, cy, x, y, scale);
  498. newCX = int(scale * float(targetCX));
  499. newCY = int(scale * float(targetCY));
  500. startRegion(x, y, newCX, newCY, 0.0f, float(targetCX), 0.0f,
  501. float(targetCY));
  502. if (window->type == ProjectorType::Preview &&
  503. main->IsPreviewProgramMode()) {
  504. OBSSource curSource = main->GetCurrentSceneSource();
  505. if (source != curSource) {
  506. obs_source_dec_showing(source);
  507. obs_source_inc_showing(curSource);
  508. source = curSource;
  509. window->source = source;
  510. }
  511. } else if (window->type == ProjectorType::Preview &&
  512. !main->IsPreviewProgramMode()) {
  513. window->source = nullptr;
  514. }
  515. if (source)
  516. obs_source_video_render(source);
  517. else
  518. obs_render_main_texture();
  519. endRegion();
  520. }
  521. void OBSProjector::OBSSourceRemoved(void *data, calldata_t *params)
  522. {
  523. OBSProjector *window = reinterpret_cast<OBSProjector *>(data);
  524. window->deleteLater();
  525. UNUSED_PARAMETER(params);
  526. }
  527. static int getSourceByPosition(int x, int y, float ratio)
  528. {
  529. int pos = -1;
  530. QWidget *rec = QApplication::activeWindow();
  531. if (!rec)
  532. return pos;
  533. int cx = rec->width();
  534. int cy = rec->height();
  535. int minX = 0;
  536. int minY = 0;
  537. int maxX = cx;
  538. int maxY = cy;
  539. switch (multiviewLayout) {
  540. case MultiviewLayout::HORIZONTAL_TOP_18_SCENES:
  541. if (float(cx) / float(cy) > ratio) {
  542. int validX = cy * ratio;
  543. minX = (cx / 2) - (validX / 2);
  544. maxX = (cx / 2) + (validX / 2);
  545. } else {
  546. int validY = cx / ratio;
  547. maxY = (cy / 2) + (validY / 2);
  548. }
  549. minY = cy / 2;
  550. if (x < minX || x > maxX || y < minY || y > maxY)
  551. break;
  552. pos = (x - minX) / ((maxX - minX) / 6);
  553. pos += ((y - minY) / ((maxY - minY) / 3)) * 6;
  554. break;
  555. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  556. if (float(cx) / float(cy) > ratio) {
  557. int validX = cy * ratio;
  558. minX = (cx / 2) - (validX / 2);
  559. maxX = (cx / 2) + (validX / 2);
  560. minY = cy / 3;
  561. } else {
  562. int validY = cx / ratio;
  563. maxY = (cy / 2) + (validY / 2);
  564. minY = (cy / 2) - (validY / 6);
  565. }
  566. if (x < minX || x > maxX || y < minY || y > maxY)
  567. break;
  568. pos = (x - minX) / ((maxX - minX) / 6);
  569. pos += ((y - minY) / ((maxY - minY) / 4)) * 6;
  570. break;
  571. case MultiviewLayout::VERTICAL_LEFT_8_SCENES:
  572. if (float(cx) / float(cy) > ratio) {
  573. int validX = cy * ratio;
  574. maxX = (cx / 2) + (validX / 2);
  575. } else {
  576. int validY = cx / ratio;
  577. minY = (cy / 2) - (validY / 2);
  578. maxY = (cy / 2) + (validY / 2);
  579. }
  580. minX = cx / 2;
  581. if (x < minX || x > maxX || y < minY || y > maxY)
  582. break;
  583. pos = 2 * ((y - minY) / ((maxY - minY) / 4));
  584. if (x > minX + ((maxX - minX) / 2))
  585. pos++;
  586. break;
  587. case MultiviewLayout::VERTICAL_RIGHT_8_SCENES:
  588. if (float(cx) / float(cy) > ratio) {
  589. int validX = cy * ratio;
  590. minX = (cx / 2) - (validX / 2);
  591. } else {
  592. int validY = cx / ratio;
  593. minY = (cy / 2) - (validY / 2);
  594. maxY = (cy / 2) + (validY / 2);
  595. }
  596. maxX = (cx / 2);
  597. if (x < minX || x > maxX || y < minY || y > maxY)
  598. break;
  599. pos = 2 * ((y - minY) / ((maxY - minY) / 4));
  600. if (x > minX + ((maxX - minX) / 2))
  601. pos++;
  602. break;
  603. case MultiviewLayout::HORIZONTAL_BOTTOM_8_SCENES:
  604. if (float(cx) / float(cy) > ratio) {
  605. int validX = cy * ratio;
  606. minX = (cx / 2) - (validX / 2);
  607. maxX = (cx / 2) + (validX / 2);
  608. } else {
  609. int validY = cx / ratio;
  610. minY = (cy / 2) - (validY / 2);
  611. }
  612. maxY = (cy / 2);
  613. if (x < minX || x > maxX || y < minY || y > maxY)
  614. break;
  615. pos = (x - minX) / ((maxX - minX) / 4);
  616. if (y > minY + ((maxY - minY) / 2))
  617. pos += 4;
  618. break;
  619. default: // MultiviewLayout::HORIZONTAL_TOP_8_SCENES
  620. if (float(cx) / float(cy) > ratio) {
  621. int validX = cy * ratio;
  622. minX = (cx / 2) - (validX / 2);
  623. maxX = (cx / 2) + (validX / 2);
  624. } else {
  625. int validY = cx / ratio;
  626. maxY = (cy / 2) + (validY / 2);
  627. }
  628. minY = (cy / 2);
  629. if (x < minX || x > maxX || y < minY || y > maxY)
  630. break;
  631. pos = (x - minX) / ((maxX - minX) / 4);
  632. if (y > minY + ((maxY - minY) / 2))
  633. pos += 4;
  634. }
  635. return pos;
  636. }
  637. void OBSProjector::mouseDoubleClickEvent(QMouseEvent *event)
  638. {
  639. OBSQTDisplay::mouseDoubleClickEvent(event);
  640. if (!mouseSwitching)
  641. return;
  642. if (!transitionOnDoubleClick)
  643. return;
  644. OBSBasic *main = (OBSBasic *)obs_frontend_get_main_window();
  645. if (!main->IsPreviewProgramMode())
  646. return;
  647. if (event->button() == Qt::LeftButton) {
  648. int pos = getSourceByPosition(event->x(), event->y(), ratio);
  649. if (pos < 0 || pos >= (int)numSrcs)
  650. return;
  651. OBSSource src = OBSGetStrongRef(multiviewScenes[pos]);
  652. if (!src)
  653. return;
  654. if (main->GetProgramSource() != src)
  655. main->TransitionToScene(src);
  656. }
  657. }
  658. void OBSProjector::mousePressEvent(QMouseEvent *event)
  659. {
  660. OBSQTDisplay::mousePressEvent(event);
  661. if (event->button() == Qt::RightButton) {
  662. OBSBasic *main =
  663. reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  664. QMenu popup(this);
  665. QMenu *projectorMenu = new QMenu(QTStr("Fullscreen"));
  666. main->AddProjectorMenuMonitors(projectorMenu, this,
  667. SLOT(OpenFullScreenProjector()));
  668. popup.addMenu(projectorMenu);
  669. if (GetMonitor() > -1) {
  670. popup.addAction(QTStr("Windowed"), this,
  671. SLOT(OpenWindowedProjector()));
  672. } else if (!this->isMaximized()) {
  673. popup.addAction(QTStr("ResizeProjectorWindowToContent"),
  674. this, SLOT(ResizeToContent()));
  675. }
  676. QAction *alwaysOnTopButton =
  677. new QAction(QTStr("Basic.MainMenu.AlwaysOnTop"), this);
  678. alwaysOnTopButton->setCheckable(true);
  679. alwaysOnTopButton->setChecked(isAlwaysOnTop);
  680. connect(alwaysOnTopButton, &QAction::toggled, this,
  681. &OBSProjector::AlwaysOnTopToggled);
  682. popup.addAction(alwaysOnTopButton);
  683. popup.addAction(QTStr("Close"), this, SLOT(EscapeTriggered()));
  684. popup.exec(QCursor::pos());
  685. }
  686. if (!mouseSwitching)
  687. return;
  688. if (event->button() == Qt::LeftButton) {
  689. int pos = getSourceByPosition(event->x(), event->y(), ratio);
  690. if (pos < 0 || pos >= (int)numSrcs)
  691. return;
  692. OBSSource src = OBSGetStrongRef(multiviewScenes[pos]);
  693. if (!src)
  694. return;
  695. OBSBasic *main = (OBSBasic *)obs_frontend_get_main_window();
  696. if (main->GetCurrentSceneSource() != src)
  697. main->SetCurrentScene(src, false);
  698. }
  699. }
  700. void OBSProjector::EscapeTriggered()
  701. {
  702. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  703. main->DeleteProjector(this);
  704. allProjectors.removeAll(this);
  705. }
  706. void OBSProjector::UpdateMultiview()
  707. {
  708. multiviewScenes.clear();
  709. multiviewLabels.clear();
  710. struct obs_video_info ovi;
  711. obs_get_video_info(&ovi);
  712. uint32_t w = ovi.base_width;
  713. uint32_t h = ovi.base_height;
  714. fw = float(w);
  715. fh = float(h);
  716. ratio = fw / fh;
  717. struct obs_frontend_source_list scenes = {};
  718. obs_frontend_get_scenes(&scenes);
  719. multiviewLabels.emplace_back(
  720. CreateLabel(Str("StudioMode.Preview"), h / 2));
  721. multiviewLabels.emplace_back(
  722. CreateLabel(Str("StudioMode.Program"), h / 2));
  723. multiviewLayout = static_cast<MultiviewLayout>(config_get_int(
  724. GetGlobalConfig(), "BasicWindow", "MultiviewLayout"));
  725. drawLabel = config_get_bool(GetGlobalConfig(), "BasicWindow",
  726. "MultiviewDrawNames");
  727. drawSafeArea = config_get_bool(GetGlobalConfig(), "BasicWindow",
  728. "MultiviewDrawAreas");
  729. mouseSwitching = config_get_bool(GetGlobalConfig(), "BasicWindow",
  730. "MultiviewMouseSwitch");
  731. transitionOnDoubleClick = config_get_bool(
  732. GetGlobalConfig(), "BasicWindow", "TransitionOnDoubleClick");
  733. switch (multiviewLayout) {
  734. case MultiviewLayout::HORIZONTAL_TOP_18_SCENES:
  735. pvwprgCX = fw / 2;
  736. pvwprgCY = fh / 2;
  737. maxSrcs = 18;
  738. break;
  739. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  740. pvwprgCX = fw / 3;
  741. pvwprgCY = fh / 3;
  742. maxSrcs = 24;
  743. break;
  744. default:
  745. pvwprgCX = fw / 2;
  746. pvwprgCY = fh / 2;
  747. maxSrcs = 8;
  748. }
  749. ppiCX = pvwprgCX - thicknessx2;
  750. ppiCY = pvwprgCY - thicknessx2;
  751. ppiScaleX = (pvwprgCX - thicknessx2) / fw;
  752. ppiScaleY = (pvwprgCY - thicknessx2) / fh;
  753. switch (multiviewLayout) {
  754. case MultiviewLayout::HORIZONTAL_TOP_18_SCENES:
  755. scenesCX = pvwprgCX / 3;
  756. scenesCY = pvwprgCY / 3;
  757. break;
  758. default:
  759. scenesCX = pvwprgCX / 2;
  760. scenesCY = pvwprgCY / 2;
  761. }
  762. siCX = scenesCX - thicknessx2;
  763. siCY = scenesCY - thicknessx2;
  764. siScaleX = (scenesCX - thicknessx2) / fw;
  765. siScaleY = (scenesCY - thicknessx2) / fh;
  766. numSrcs = 0;
  767. size_t i = 0;
  768. while (i < scenes.sources.num && numSrcs < maxSrcs) {
  769. obs_source_t *src = scenes.sources.array[i++];
  770. OBSData data = obs_source_get_private_settings(src);
  771. obs_data_release(data);
  772. obs_data_set_default_bool(data, "show_in_multiview", true);
  773. if (!obs_data_get_bool(data, "show_in_multiview"))
  774. continue;
  775. // We have a displayable source.
  776. numSrcs++;
  777. multiviewScenes.emplace_back(OBSGetWeakRef(src));
  778. obs_source_inc_showing(src);
  779. std::string name = std::to_string(numSrcs) + " - " +
  780. obs_source_get_name(src);
  781. multiviewLabels.emplace_back(CreateLabel(name.c_str(), h / 3));
  782. }
  783. obs_frontend_source_list_free(&scenes);
  784. }
  785. void OBSProjector::UpdateProjectorTitle(QString name)
  786. {
  787. bool window = (GetMonitor() == -1);
  788. QString title = nullptr;
  789. switch (type) {
  790. case ProjectorType::Scene:
  791. if (!window)
  792. title = QTStr("SceneProjector") + " - " + name;
  793. else
  794. title = QTStr("SceneWindow") + " - " + name;
  795. break;
  796. case ProjectorType::Source:
  797. if (!window)
  798. title = QTStr("SourceProjector") + " - " + name;
  799. else
  800. title = QTStr("SourceWindow") + " - " + name;
  801. break;
  802. case ProjectorType::Preview:
  803. if (!window)
  804. title = QTStr("PreviewProjector");
  805. else
  806. title = QTStr("PreviewWindow");
  807. break;
  808. case ProjectorType::StudioProgram:
  809. if (!window)
  810. title = QTStr("StudioProgramProjector");
  811. else
  812. title = QTStr("StudioProgramWindow");
  813. break;
  814. case ProjectorType::Multiview:
  815. if (!window)
  816. title = QTStr("MultiviewProjector");
  817. else
  818. title = QTStr("MultiviewWindowed");
  819. break;
  820. default:
  821. title = name;
  822. break;
  823. }
  824. setWindowTitle(title);
  825. }
  826. OBSSource OBSProjector::GetSource()
  827. {
  828. return source;
  829. }
  830. ProjectorType OBSProjector::GetProjectorType()
  831. {
  832. return type;
  833. }
  834. int OBSProjector::GetMonitor()
  835. {
  836. return savedMonitor;
  837. }
  838. void OBSProjector::UpdateMultiviewProjectors()
  839. {
  840. obs_enter_graphics();
  841. updatingMultiview = true;
  842. obs_leave_graphics();
  843. for (auto &projector : multiviewProjectors)
  844. projector->UpdateMultiview();
  845. obs_enter_graphics();
  846. updatingMultiview = false;
  847. obs_leave_graphics();
  848. }
  849. void OBSProjector::RenameProjector(QString oldName, QString newName)
  850. {
  851. if (oldName == newName)
  852. return;
  853. UpdateProjectorTitle(newName);
  854. }
  855. void OBSProjector::OpenFullScreenProjector()
  856. {
  857. if (!isFullScreen())
  858. prevGeometry = geometry();
  859. int monitor = sender()->property("monitor").toInt();
  860. SetMonitor(monitor);
  861. UpdateProjectorTitle(QT_UTF8(obs_source_get_name(source)));
  862. }
  863. void OBSProjector::OpenWindowedProjector()
  864. {
  865. showFullScreen();
  866. showNormal();
  867. setCursor(Qt::ArrowCursor);
  868. if (!prevGeometry.isNull())
  869. setGeometry(prevGeometry);
  870. else
  871. resize(480, 270);
  872. savedMonitor = -1;
  873. UpdateProjectorTitle(QT_UTF8(obs_source_get_name(source)));
  874. screen = nullptr;
  875. }
  876. void OBSProjector::ResizeToContent()
  877. {
  878. OBSSource source = GetSource();
  879. uint32_t targetCX;
  880. uint32_t targetCY;
  881. int x, y, newX, newY;
  882. float scale;
  883. if (source) {
  884. targetCX = std::max(obs_source_get_width(source), 1u);
  885. targetCY = std::max(obs_source_get_height(source), 1u);
  886. } else {
  887. struct obs_video_info ovi;
  888. obs_get_video_info(&ovi);
  889. targetCX = ovi.base_width;
  890. targetCY = ovi.base_height;
  891. }
  892. QSize size = this->size();
  893. GetScaleAndCenterPos(targetCX, targetCY, size.width(), size.height(), x,
  894. y, scale);
  895. newX = size.width() - (x * 2);
  896. newY = size.height() - (y * 2);
  897. resize(newX, newY);
  898. }
  899. void OBSProjector::AlwaysOnTopToggled(bool isAlwaysOnTop)
  900. {
  901. SetIsAlwaysOnTop(isAlwaysOnTop, true);
  902. }
  903. void OBSProjector::closeEvent(QCloseEvent *event)
  904. {
  905. EscapeTriggered();
  906. event->accept();
  907. }
  908. bool OBSProjector::IsAlwaysOnTop() const
  909. {
  910. return isAlwaysOnTop;
  911. }
  912. bool OBSProjector::IsAlwaysOnTopOverridden() const
  913. {
  914. return isAlwaysOnTopOverridden;
  915. }
  916. void OBSProjector::SetIsAlwaysOnTop(bool isAlwaysOnTop, bool isOverridden)
  917. {
  918. this->isAlwaysOnTop = isAlwaysOnTop;
  919. this->isAlwaysOnTopOverridden = isOverridden;
  920. SetAlwaysOnTop(this, isAlwaysOnTop);
  921. }
  922. void OBSProjector::ScreenRemoved(QScreen *screen_)
  923. {
  924. if (GetMonitor() < 0 || !screen)
  925. return;
  926. if (screen == screen_)
  927. EscapeTriggered();
  928. }