window-projector.cpp 30 KB

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