window-projector.cpp 27 KB

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