1
0

window-projector.cpp 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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_24_SCENES:
  200. n = 6;
  201. break;
  202. default:
  203. n = 4;
  204. break;
  205. }
  206. w = uint32_t(w * ((1.0f) / n));
  207. return (cx / 2) - w;
  208. }
  209. static inline void startRegion(int vX, int vY, int vCX, int vCY, float oL,
  210. float oR, float oT, float oB)
  211. {
  212. gs_projection_push();
  213. gs_viewport_push();
  214. gs_set_viewport(vX, vY, vCX, vCY);
  215. gs_ortho(oL, oR, oT, oB, -100.0f, 100.0f);
  216. }
  217. static inline void endRegion()
  218. {
  219. gs_viewport_pop();
  220. gs_projection_pop();
  221. }
  222. void OBSProjector::OBSRenderMultiview(void *data, uint32_t cx, uint32_t cy)
  223. {
  224. OBSProjector *window = (OBSProjector *)data;
  225. if (updatingMultiview || !window->ready)
  226. return;
  227. OBSBasic *main = (OBSBasic *)obs_frontend_get_main_window();
  228. uint32_t targetCX, targetCY;
  229. int x, y;
  230. float scale;
  231. targetCX = (uint32_t)window->fw;
  232. targetCY = (uint32_t)window->fh;
  233. GetScaleAndCenterPos(targetCX, targetCY, cx, cy, x, y, scale);
  234. OBSSource previewSrc = main->GetCurrentSceneSource();
  235. OBSSource programSrc = main->GetProgramSource();
  236. bool studioMode = main->IsPreviewProgramMode();
  237. auto renderVB = [&](gs_vertbuffer_t *vb, int cx, int cy,
  238. uint32_t colorVal) {
  239. if (!vb)
  240. return;
  241. matrix4 transform;
  242. matrix4_identity(&transform);
  243. transform.x.x = cx;
  244. transform.y.y = cy;
  245. gs_load_vertexbuffer(vb);
  246. gs_matrix_push();
  247. gs_matrix_mul(&transform);
  248. gs_effect_set_color(window->color, colorVal);
  249. while (gs_effect_loop(window->solid, "Solid"))
  250. gs_draw(GS_LINESTRIP, 0, 0);
  251. gs_matrix_pop();
  252. };
  253. auto drawBox = [&](float cx, float cy, uint32_t colorVal) {
  254. gs_effect_set_color(window->color, colorVal);
  255. while (gs_effect_loop(window->solid, "Solid"))
  256. gs_draw_sprite(nullptr, 0, (uint32_t)cx, (uint32_t)cy);
  257. };
  258. auto setRegion = [&](float bx, float by, float cx, float cy) {
  259. float vX = int(x + bx * scale);
  260. float vY = int(y + by * scale);
  261. float vCX = int(cx * scale);
  262. float vCY = int(cy * scale);
  263. float oL = bx;
  264. float oT = by;
  265. float oR = (bx + cx);
  266. float oB = (by + cy);
  267. startRegion(vX, vY, vCX, vCY, oL, oR, oT, oB);
  268. };
  269. auto calcBaseSource = [&](size_t i) {
  270. switch (multiviewLayout) {
  271. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  272. window->sourceX = (i % 6) * window->scenesCX;
  273. window->sourceY =
  274. window->pvwprgCY + (i / 6) * window->scenesCY;
  275. break;
  276. case MultiviewLayout::VERTICAL_LEFT_8_SCENES:
  277. window->sourceX = window->pvwprgCX;
  278. window->sourceY = (i / 2) * window->scenesCY;
  279. if (i % 2 != 0)
  280. window->sourceX += window->scenesCX;
  281. break;
  282. case MultiviewLayout::VERTICAL_RIGHT_8_SCENES:
  283. window->sourceX = 0;
  284. window->sourceY = (i / 2) * window->scenesCY;
  285. if (i % 2 != 0)
  286. window->sourceX = window->scenesCX;
  287. break;
  288. case MultiviewLayout::HORIZONTAL_BOTTOM_8_SCENES:
  289. if (i < 4) {
  290. window->sourceX = (float(i) * window->scenesCX);
  291. window->sourceY = 0;
  292. } else {
  293. window->sourceX =
  294. (float(i - 4) * window->scenesCX);
  295. window->sourceY = window->scenesCY;
  296. }
  297. break;
  298. default: // MultiviewLayout::HORIZONTAL_TOP_8_SCENES:
  299. if (i < 4) {
  300. window->sourceX = (float(i) * window->scenesCX);
  301. window->sourceY = window->pvwprgCY;
  302. } else {
  303. window->sourceX =
  304. (float(i - 4) * window->scenesCX);
  305. window->sourceY =
  306. window->pvwprgCY + window->scenesCY;
  307. }
  308. }
  309. window->siX = window->sourceX + window->thickness;
  310. window->siY = window->sourceY + window->thickness;
  311. };
  312. auto calcPreviewProgram = [&](bool program) {
  313. switch (multiviewLayout) {
  314. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  315. window->sourceX =
  316. window->thickness + window->pvwprgCX / 2;
  317. window->sourceY = window->thickness;
  318. window->labelX = window->offset + window->pvwprgCX / 2;
  319. window->labelY = window->pvwprgCY * 0.85f;
  320. if (program) {
  321. window->sourceX += window->pvwprgCX;
  322. window->labelX += window->pvwprgCX;
  323. }
  324. break;
  325. case MultiviewLayout::VERTICAL_LEFT_8_SCENES:
  326. window->sourceX = window->thickness;
  327. window->sourceY = window->pvwprgCY + window->thickness;
  328. window->labelX = window->offset;
  329. window->labelY = window->pvwprgCY * 1.85f;
  330. if (program) {
  331. window->sourceY = window->thickness;
  332. window->labelY = window->pvwprgCY * 0.85f;
  333. }
  334. break;
  335. case MultiviewLayout::VERTICAL_RIGHT_8_SCENES:
  336. window->sourceX = window->pvwprgCX + window->thickness;
  337. window->sourceY = window->pvwprgCY + window->thickness;
  338. window->labelX = window->pvwprgCX + window->offset;
  339. window->labelY = window->pvwprgCY * 1.85f;
  340. if (program) {
  341. window->sourceY = window->thickness;
  342. window->labelY = window->pvwprgCY * 0.85f;
  343. }
  344. break;
  345. case MultiviewLayout::HORIZONTAL_BOTTOM_8_SCENES:
  346. window->sourceX = window->thickness;
  347. window->sourceY = window->pvwprgCY + window->thickness;
  348. window->labelX = window->offset;
  349. window->labelY = window->pvwprgCY * 1.85f;
  350. if (program) {
  351. window->sourceX += window->pvwprgCX;
  352. window->labelX += window->pvwprgCX;
  353. }
  354. break;
  355. default: // MultiviewLayout::HORIZONTAL_TOP_8_SCENES:
  356. window->sourceX = window->thickness;
  357. window->sourceY = window->thickness;
  358. window->labelX = window->offset;
  359. window->labelY = window->pvwprgCY * 0.85f;
  360. if (program) {
  361. window->sourceX += window->pvwprgCX;
  362. window->labelX += window->pvwprgCX;
  363. }
  364. }
  365. };
  366. auto paintAreaWithColor = [&](float tx, float ty, float cx, float cy,
  367. uint32_t color) {
  368. gs_matrix_push();
  369. gs_matrix_translate3f(tx, ty, 0.0f);
  370. drawBox(cx, cy, color);
  371. gs_matrix_pop();
  372. };
  373. // Define the whole usable region for the multiview
  374. startRegion(x, y, targetCX * scale, targetCY * scale, 0.0f, window->fw,
  375. 0.0f, window->fh);
  376. // Change the background color to highlight all sources
  377. drawBox(window->fw, window->fh, outerColor);
  378. /* ----------------------------- */
  379. /* draw sources */
  380. for (size_t i = 0; i < maxSrcs; i++) {
  381. // Handle all the offsets
  382. calcBaseSource(i);
  383. if (i >= numSrcs) {
  384. // Just paint the background and continue
  385. paintAreaWithColor(window->sourceX, window->sourceY,
  386. window->scenesCX, window->scenesCY,
  387. outerColor);
  388. paintAreaWithColor(window->siX, window->siY,
  389. window->siCX, window->siCY,
  390. backgroundColor);
  391. continue;
  392. }
  393. OBSSource src = OBSGetStrongRef(window->multiviewScenes[i]);
  394. // We have a source. Now chose the proper highlight color
  395. uint32_t colorVal = outerColor;
  396. if (src == programSrc)
  397. colorVal = programColor;
  398. else if (src == previewSrc)
  399. colorVal = studioMode ? previewColor : programColor;
  400. // Paint the background
  401. paintAreaWithColor(window->sourceX, window->sourceY,
  402. window->scenesCX, window->scenesCY,
  403. colorVal);
  404. paintAreaWithColor(window->siX, window->siY, window->siCX,
  405. window->siCY, backgroundColor);
  406. /* ----------- */
  407. // Render the source
  408. gs_matrix_push();
  409. gs_matrix_translate3f(window->siX, window->siY, 0.0f);
  410. gs_matrix_scale3f(window->siScaleX, window->siScaleY, 1.0f);
  411. setRegion(window->siX, window->siY, window->siCX, window->siCY);
  412. obs_source_video_render(src);
  413. endRegion();
  414. gs_matrix_pop();
  415. /* ----------- */
  416. // Render the label
  417. if (!drawLabel)
  418. continue;
  419. obs_source *label = window->multiviewLabels[i + 2];
  420. if (!label)
  421. continue;
  422. window->offset = labelOffset(label, window->scenesCX);
  423. gs_matrix_push();
  424. gs_matrix_translate3f(
  425. window->sourceX + window->offset,
  426. (window->scenesCY * 0.85f) + window->sourceY, 0.0f);
  427. gs_matrix_scale3f(window->ppiScaleX, window->ppiScaleY, 1.0f);
  428. drawBox(obs_source_get_width(label),
  429. obs_source_get_height(label) +
  430. int(window->sourceY * 0.015f),
  431. labelColor);
  432. obs_source_video_render(label);
  433. gs_matrix_pop();
  434. }
  435. /* ----------------------------- */
  436. /* draw preview */
  437. obs_source_t *previewLabel = window->multiviewLabels[0];
  438. window->offset = labelOffset(previewLabel, window->pvwprgCX);
  439. calcPreviewProgram(false);
  440. // Paint the background
  441. paintAreaWithColor(window->sourceX, window->sourceY, window->ppiCX,
  442. window->ppiCY, backgroundColor);
  443. // Scale and Draw the preview
  444. gs_matrix_push();
  445. gs_matrix_translate3f(window->sourceX, window->sourceY, 0.0f);
  446. gs_matrix_scale3f(window->ppiScaleX, window->ppiScaleY, 1.0f);
  447. setRegion(window->sourceX, window->sourceY, window->ppiCX,
  448. window->ppiCY);
  449. if (studioMode)
  450. obs_source_video_render(previewSrc);
  451. else
  452. obs_render_main_texture();
  453. if (drawSafeArea) {
  454. renderVB(window->actionSafeMargin, targetCX, targetCY,
  455. outerColor);
  456. renderVB(window->graphicsSafeMargin, targetCX, targetCY,
  457. outerColor);
  458. renderVB(window->fourByThreeSafeMargin, targetCX, targetCY,
  459. outerColor);
  460. renderVB(window->leftLine, targetCX, targetCY, outerColor);
  461. renderVB(window->topLine, targetCX, targetCY, outerColor);
  462. renderVB(window->rightLine, targetCX, targetCY, outerColor);
  463. }
  464. endRegion();
  465. gs_matrix_pop();
  466. /* ----------- */
  467. // Draw the Label
  468. if (drawLabel) {
  469. gs_matrix_push();
  470. gs_matrix_translate3f(window->labelX, window->labelY, 0.0f);
  471. gs_matrix_scale3f(window->ppiScaleX, window->ppiScaleY, 1.0f);
  472. drawBox(obs_source_get_width(previewLabel),
  473. obs_source_get_height(previewLabel) +
  474. int(window->pvwprgCX * 0.015f),
  475. labelColor);
  476. obs_source_video_render(previewLabel);
  477. gs_matrix_pop();
  478. }
  479. /* ----------------------------- */
  480. /* draw program */
  481. obs_source_t *programLabel = window->multiviewLabels[1];
  482. window->offset = labelOffset(programLabel, window->pvwprgCX);
  483. calcPreviewProgram(true);
  484. paintAreaWithColor(window->sourceX, window->sourceY, window->ppiCX,
  485. window->ppiCY, backgroundColor);
  486. // Scale and Draw the program
  487. gs_matrix_push();
  488. gs_matrix_translate3f(window->sourceX, window->sourceY, 0.0f);
  489. gs_matrix_scale3f(window->ppiScaleX, window->ppiScaleY, 1.0f);
  490. setRegion(window->sourceX, window->sourceY, window->ppiCX,
  491. window->ppiCY);
  492. obs_render_main_texture();
  493. endRegion();
  494. gs_matrix_pop();
  495. /* ----------- */
  496. // Draw the Label
  497. if (drawLabel) {
  498. gs_matrix_push();
  499. gs_matrix_translate3f(window->labelX, window->labelY, 0.0f);
  500. gs_matrix_scale3f(window->ppiScaleX, window->ppiScaleY, 1.0f);
  501. drawBox(obs_source_get_width(programLabel),
  502. obs_source_get_height(programLabel) +
  503. int(window->pvwprgCX * 0.015f),
  504. labelColor);
  505. obs_source_video_render(programLabel);
  506. gs_matrix_pop();
  507. }
  508. // Region for future usage with additional info.
  509. if (multiviewLayout == MultiviewLayout::HORIZONTAL_TOP_24_SCENES) {
  510. // Just paint the background for now
  511. paintAreaWithColor(window->thickness, window->thickness,
  512. window->siCX,
  513. window->siCY * 2 + window->thicknessx2,
  514. backgroundColor);
  515. paintAreaWithColor(
  516. window->thickness +
  517. 2.5 * (window->thicknessx2 + window->ppiCX),
  518. window->thickness, window->siCX,
  519. window->siCY * 2 + window->thicknessx2,
  520. backgroundColor);
  521. }
  522. endRegion();
  523. }
  524. void OBSProjector::OBSRender(void *data, uint32_t cx, uint32_t cy)
  525. {
  526. OBSProjector *window = reinterpret_cast<OBSProjector *>(data);
  527. if (!window->ready)
  528. return;
  529. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  530. OBSSource source = window->source;
  531. uint32_t targetCX;
  532. uint32_t targetCY;
  533. int x, y;
  534. int newCX, newCY;
  535. float scale;
  536. if (source) {
  537. targetCX = std::max(obs_source_get_width(source), 1u);
  538. targetCY = std::max(obs_source_get_height(source), 1u);
  539. } else {
  540. struct obs_video_info ovi;
  541. obs_get_video_info(&ovi);
  542. targetCX = ovi.base_width;
  543. targetCY = ovi.base_height;
  544. }
  545. GetScaleAndCenterPos(targetCX, targetCY, cx, cy, x, y, scale);
  546. newCX = int(scale * float(targetCX));
  547. newCY = int(scale * float(targetCY));
  548. startRegion(x, y, newCX, newCY, 0.0f, float(targetCX), 0.0f,
  549. float(targetCY));
  550. if (window->type == ProjectorType::Preview &&
  551. main->IsPreviewProgramMode()) {
  552. OBSSource curSource = main->GetCurrentSceneSource();
  553. if (source != curSource) {
  554. obs_source_dec_showing(source);
  555. obs_source_inc_showing(curSource);
  556. source = curSource;
  557. window->source = source;
  558. }
  559. } else if (window->type == ProjectorType::Preview &&
  560. !main->IsPreviewProgramMode()) {
  561. window->source = nullptr;
  562. }
  563. if (source)
  564. obs_source_video_render(source);
  565. else
  566. obs_render_main_texture();
  567. endRegion();
  568. }
  569. void OBSProjector::OBSSourceRemoved(void *data, calldata_t *params)
  570. {
  571. OBSProjector *window = reinterpret_cast<OBSProjector *>(data);
  572. window->deleteLater();
  573. UNUSED_PARAMETER(params);
  574. }
  575. static int getSourceByPosition(int x, int y, float ratio)
  576. {
  577. int pos = -1;
  578. QWidget *rec = QApplication::activeWindow();
  579. if (!rec)
  580. return pos;
  581. int cx = rec->width();
  582. int cy = rec->height();
  583. int minX = 0;
  584. int minY = 0;
  585. int maxX = cx;
  586. int maxY = cy;
  587. switch (multiviewLayout) {
  588. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  589. if (float(cx) / float(cy) > ratio) {
  590. int validX = cy * ratio;
  591. minX = (cx / 2) - (validX / 2);
  592. maxX = (cx / 2) + (validX / 2);
  593. minY = cy / 3;
  594. } else {
  595. int validY = cx / ratio;
  596. maxY = (cy / 2) + (validY / 2);
  597. minY = (cy / 2) - (validY / 6);
  598. }
  599. if (x < minX || x > maxX || y < minY || y > maxY)
  600. break;
  601. pos = (x - minX) / ((maxX - minX) / 6);
  602. pos += ((y - minY) / ((maxY - minY) / 4)) * 6;
  603. break;
  604. case MultiviewLayout::VERTICAL_LEFT_8_SCENES:
  605. if (float(cx) / float(cy) > ratio) {
  606. int validX = cy * ratio;
  607. maxX = (cx / 2) + (validX / 2);
  608. } else {
  609. int validY = cx / ratio;
  610. minY = (cy / 2) - (validY / 2);
  611. maxY = (cy / 2) + (validY / 2);
  612. }
  613. minX = cx / 2;
  614. if (x < minX || x > maxX || y < minY || y > maxY)
  615. break;
  616. pos = 2 * ((y - minY) / ((maxY - minY) / 4));
  617. if (x > minX + ((maxX - minX) / 2))
  618. pos++;
  619. break;
  620. case MultiviewLayout::VERTICAL_RIGHT_8_SCENES:
  621. if (float(cx) / float(cy) > ratio) {
  622. int validX = cy * ratio;
  623. minX = (cx / 2) - (validX / 2);
  624. } else {
  625. int validY = cx / ratio;
  626. minY = (cy / 2) - (validY / 2);
  627. maxY = (cy / 2) + (validY / 2);
  628. }
  629. maxX = (cx / 2);
  630. if (x < minX || x > maxX || y < minY || y > maxY)
  631. break;
  632. pos = 2 * ((y - minY) / ((maxY - minY) / 4));
  633. if (x > minX + ((maxX - minX) / 2))
  634. pos++;
  635. break;
  636. case MultiviewLayout::HORIZONTAL_BOTTOM_8_SCENES:
  637. if (float(cx) / float(cy) > ratio) {
  638. int validX = cy * ratio;
  639. minX = (cx / 2) - (validX / 2);
  640. maxX = (cx / 2) + (validX / 2);
  641. } else {
  642. int validY = cx / ratio;
  643. minY = (cy / 2) - (validY / 2);
  644. }
  645. maxY = (cy / 2);
  646. if (x < minX || x > maxX || y < minY || y > maxY)
  647. break;
  648. pos = (x - minX) / ((maxX - minX) / 4);
  649. if (y > minY + ((maxY - minY) / 2))
  650. pos += 4;
  651. break;
  652. default: // MultiviewLayout::HORIZONTAL_TOP_8_SCENES
  653. if (float(cx) / float(cy) > ratio) {
  654. int validX = cy * ratio;
  655. minX = (cx / 2) - (validX / 2);
  656. maxX = (cx / 2) + (validX / 2);
  657. } else {
  658. int validY = cx / ratio;
  659. maxY = (cy / 2) + (validY / 2);
  660. }
  661. minY = (cy / 2);
  662. if (x < minX || x > maxX || y < minY || y > maxY)
  663. break;
  664. pos = (x - minX) / ((maxX - minX) / 4);
  665. if (y > minY + ((maxY - minY) / 2))
  666. pos += 4;
  667. }
  668. return pos;
  669. }
  670. void OBSProjector::mouseDoubleClickEvent(QMouseEvent *event)
  671. {
  672. OBSQTDisplay::mouseDoubleClickEvent(event);
  673. if (!mouseSwitching)
  674. return;
  675. if (!transitionOnDoubleClick)
  676. return;
  677. OBSBasic *main = (OBSBasic *)obs_frontend_get_main_window();
  678. if (!main->IsPreviewProgramMode())
  679. return;
  680. if (event->button() == Qt::LeftButton) {
  681. int pos = getSourceByPosition(event->x(), event->y(), ratio);
  682. if (pos < 0 || pos >= (int)numSrcs)
  683. return;
  684. OBSSource src = OBSGetStrongRef(multiviewScenes[pos]);
  685. if (!src)
  686. return;
  687. if (main->GetProgramSource() != src)
  688. main->TransitionToScene(src);
  689. }
  690. }
  691. void OBSProjector::mousePressEvent(QMouseEvent *event)
  692. {
  693. OBSQTDisplay::mousePressEvent(event);
  694. if (event->button() == Qt::RightButton) {
  695. OBSBasic *main =
  696. reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  697. QMenu popup(this);
  698. QMenu *projectorMenu = new QMenu(QTStr("Fullscreen"));
  699. main->AddProjectorMenuMonitors(projectorMenu, this,
  700. SLOT(OpenFullScreenProjector()));
  701. popup.addMenu(projectorMenu);
  702. if (GetMonitor() > -1) {
  703. popup.addAction(QTStr("Windowed"), this,
  704. SLOT(OpenWindowedProjector()));
  705. } else if (!this->isMaximized()) {
  706. popup.addAction(QTStr("ResizeProjectorWindowToContent"),
  707. this, SLOT(ResizeToContent()));
  708. }
  709. QAction *alwaysOnTopButton =
  710. new QAction(QTStr("Basic.MainMenu.AlwaysOnTop"), this);
  711. alwaysOnTopButton->setCheckable(true);
  712. alwaysOnTopButton->setChecked(isAlwaysOnTop);
  713. connect(alwaysOnTopButton, &QAction::toggled, this,
  714. &OBSProjector::AlwaysOnTopToggled);
  715. popup.addAction(alwaysOnTopButton);
  716. popup.addAction(QTStr("Close"), this, SLOT(EscapeTriggered()));
  717. popup.exec(QCursor::pos());
  718. }
  719. if (!mouseSwitching)
  720. return;
  721. if (event->button() == Qt::LeftButton) {
  722. int pos = getSourceByPosition(event->x(), event->y(), ratio);
  723. if (pos < 0 || pos >= (int)numSrcs)
  724. return;
  725. OBSSource src = OBSGetStrongRef(multiviewScenes[pos]);
  726. if (!src)
  727. return;
  728. OBSBasic *main = (OBSBasic *)obs_frontend_get_main_window();
  729. if (main->GetCurrentSceneSource() != src)
  730. main->SetCurrentScene(src, false);
  731. }
  732. }
  733. void OBSProjector::EscapeTriggered()
  734. {
  735. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  736. main->DeleteProjector(this);
  737. allProjectors.removeAll(this);
  738. }
  739. void OBSProjector::UpdateMultiview()
  740. {
  741. multiviewScenes.clear();
  742. multiviewLabels.clear();
  743. struct obs_video_info ovi;
  744. obs_get_video_info(&ovi);
  745. uint32_t w = ovi.base_width;
  746. uint32_t h = ovi.base_height;
  747. fw = float(w);
  748. fh = float(h);
  749. ratio = fw / fh;
  750. struct obs_frontend_source_list scenes = {};
  751. obs_frontend_get_scenes(&scenes);
  752. multiviewLabels.emplace_back(
  753. CreateLabel(Str("StudioMode.Preview"), h / 2));
  754. multiviewLabels.emplace_back(
  755. CreateLabel(Str("StudioMode.Program"), h / 2));
  756. multiviewLayout = static_cast<MultiviewLayout>(config_get_int(
  757. GetGlobalConfig(), "BasicWindow", "MultiviewLayout"));
  758. drawLabel = config_get_bool(GetGlobalConfig(), "BasicWindow",
  759. "MultiviewDrawNames");
  760. drawSafeArea = config_get_bool(GetGlobalConfig(), "BasicWindow",
  761. "MultiviewDrawAreas");
  762. mouseSwitching = config_get_bool(GetGlobalConfig(), "BasicWindow",
  763. "MultiviewMouseSwitch");
  764. transitionOnDoubleClick = config_get_bool(
  765. GetGlobalConfig(), "BasicWindow", "TransitionOnDoubleClick");
  766. switch (multiviewLayout) {
  767. case MultiviewLayout::HORIZONTAL_TOP_24_SCENES:
  768. pvwprgCX = fw / 3;
  769. pvwprgCY = fh / 3;
  770. maxSrcs = 24;
  771. break;
  772. default:
  773. pvwprgCX = fw / 2;
  774. pvwprgCY = fh / 2;
  775. maxSrcs = 8;
  776. }
  777. ppiCX = pvwprgCX - thicknessx2;
  778. ppiCY = pvwprgCY - thicknessx2;
  779. ppiScaleX = (pvwprgCX - thicknessx2) / fw;
  780. ppiScaleY = (pvwprgCY - thicknessx2) / fh;
  781. scenesCX = pvwprgCX / 2;
  782. scenesCY = pvwprgCY / 2;
  783. siCX = scenesCX - thicknessx2;
  784. siCY = scenesCY - thicknessx2;
  785. siScaleX = (scenesCX - thicknessx2) / fw;
  786. siScaleY = (scenesCY - thicknessx2) / fh;
  787. numSrcs = 0;
  788. size_t i = 0;
  789. while (i < scenes.sources.num && numSrcs < maxSrcs) {
  790. obs_source_t *src = scenes.sources.array[i++];
  791. OBSData data = obs_source_get_private_settings(src);
  792. obs_data_release(data);
  793. obs_data_set_default_bool(data, "show_in_multiview", true);
  794. if (!obs_data_get_bool(data, "show_in_multiview"))
  795. continue;
  796. // We have a displayable source.
  797. numSrcs++;
  798. multiviewScenes.emplace_back(OBSGetWeakRef(src));
  799. obs_source_inc_showing(src);
  800. std::string name = std::to_string(numSrcs) + " - " +
  801. obs_source_get_name(src);
  802. multiviewLabels.emplace_back(CreateLabel(name.c_str(), h / 3));
  803. }
  804. obs_frontend_source_list_free(&scenes);
  805. }
  806. void OBSProjector::UpdateProjectorTitle(QString name)
  807. {
  808. bool window = (GetMonitor() == -1);
  809. QString title = nullptr;
  810. switch (type) {
  811. case ProjectorType::Scene:
  812. if (!window)
  813. title = QTStr("SceneProjector") + " - " + name;
  814. else
  815. title = QTStr("SceneWindow") + " - " + name;
  816. break;
  817. case ProjectorType::Source:
  818. if (!window)
  819. title = QTStr("SourceProjector") + " - " + name;
  820. else
  821. title = QTStr("SourceWindow") + " - " + name;
  822. break;
  823. case ProjectorType::Preview:
  824. if (!window)
  825. title = QTStr("PreviewProjector");
  826. else
  827. title = QTStr("PreviewWindow");
  828. break;
  829. case ProjectorType::StudioProgram:
  830. if (!window)
  831. title = QTStr("StudioProgramProjector");
  832. else
  833. title = QTStr("StudioProgramWindow");
  834. break;
  835. case ProjectorType::Multiview:
  836. if (!window)
  837. title = QTStr("MultiviewProjector");
  838. else
  839. title = QTStr("MultiviewWindowed");
  840. break;
  841. default:
  842. title = name;
  843. break;
  844. }
  845. setWindowTitle(title);
  846. }
  847. OBSSource OBSProjector::GetSource()
  848. {
  849. return source;
  850. }
  851. ProjectorType OBSProjector::GetProjectorType()
  852. {
  853. return type;
  854. }
  855. int OBSProjector::GetMonitor()
  856. {
  857. return savedMonitor;
  858. }
  859. void OBSProjector::UpdateMultiviewProjectors()
  860. {
  861. obs_enter_graphics();
  862. updatingMultiview = true;
  863. obs_leave_graphics();
  864. for (auto &projector : multiviewProjectors)
  865. projector->UpdateMultiview();
  866. obs_enter_graphics();
  867. updatingMultiview = false;
  868. obs_leave_graphics();
  869. }
  870. void OBSProjector::RenameProjector(QString oldName, QString newName)
  871. {
  872. if (oldName == newName)
  873. return;
  874. UpdateProjectorTitle(newName);
  875. }
  876. void OBSProjector::OpenFullScreenProjector()
  877. {
  878. if (!isFullScreen())
  879. prevGeometry = geometry();
  880. int monitor = sender()->property("monitor").toInt();
  881. SetMonitor(monitor);
  882. UpdateProjectorTitle(QT_UTF8(obs_source_get_name(source)));
  883. }
  884. void OBSProjector::OpenWindowedProjector()
  885. {
  886. showFullScreen();
  887. showNormal();
  888. setCursor(Qt::ArrowCursor);
  889. if (!prevGeometry.isNull())
  890. setGeometry(prevGeometry);
  891. else
  892. resize(480, 270);
  893. savedMonitor = -1;
  894. UpdateProjectorTitle(QT_UTF8(obs_source_get_name(source)));
  895. screen = nullptr;
  896. }
  897. void OBSProjector::ResizeToContent()
  898. {
  899. OBSSource source = GetSource();
  900. uint32_t targetCX;
  901. uint32_t targetCY;
  902. int x, y, newX, newY;
  903. float scale;
  904. if (source) {
  905. targetCX = std::max(obs_source_get_width(source), 1u);
  906. targetCY = std::max(obs_source_get_height(source), 1u);
  907. } else {
  908. struct obs_video_info ovi;
  909. obs_get_video_info(&ovi);
  910. targetCX = ovi.base_width;
  911. targetCY = ovi.base_height;
  912. }
  913. QSize size = this->size();
  914. GetScaleAndCenterPos(targetCX, targetCY, size.width(), size.height(), x,
  915. y, scale);
  916. newX = size.width() - (x * 2);
  917. newY = size.height() - (y * 2);
  918. resize(newX, newY);
  919. }
  920. void OBSProjector::AlwaysOnTopToggled(bool isAlwaysOnTop)
  921. {
  922. SetIsAlwaysOnTop(isAlwaysOnTop, true);
  923. }
  924. void OBSProjector::closeEvent(QCloseEvent *event)
  925. {
  926. EscapeTriggered();
  927. event->accept();
  928. }
  929. bool OBSProjector::IsAlwaysOnTop() const
  930. {
  931. return isAlwaysOnTop;
  932. }
  933. bool OBSProjector::IsAlwaysOnTopOverridden() const
  934. {
  935. return isAlwaysOnTopOverridden;
  936. }
  937. void OBSProjector::SetIsAlwaysOnTop(bool isAlwaysOnTop, bool isOverridden)
  938. {
  939. this->isAlwaysOnTop = isAlwaysOnTop;
  940. this->isAlwaysOnTopOverridden = isOverridden;
  941. SetAlwaysOnTop(this, isAlwaysOnTop);
  942. }
  943. void OBSProjector::ScreenRemoved(QScreen *screen_)
  944. {
  945. if (GetMonitor() < 0 || !screen)
  946. return;
  947. if (screen == screen_)
  948. EscapeTriggered();
  949. }