window-projector.cpp 29 KB

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