window-projector.cpp 28 KB

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