1
0

window-projector.cpp 28 KB

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