window-projector.cpp 26 KB

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