window-projector.cpp 23 KB

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