window-projector.cpp 24 KB

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