window-projector.cpp 27 KB

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