window-projector.cpp 26 KB

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