window-basic-interaction.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /******************************************************************************
  2. Copyright (C) 2014 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "obs-app.hpp"
  15. #include "window-basic-interaction.hpp"
  16. #include "window-basic-main.hpp"
  17. #include "qt-wrappers.hpp"
  18. #include "display-helpers.hpp"
  19. #include <QCloseEvent>
  20. #include <QScreen>
  21. #include <QWindow>
  22. using namespace std;
  23. OBSBasicInteraction::OBSBasicInteraction(QWidget *parent, OBSSource source_)
  24. : QDialog (parent),
  25. main (qobject_cast<OBSBasic*>(parent)),
  26. resizeTimer (0),
  27. ui (new Ui::OBSBasicInteraction),
  28. source (source_),
  29. removedSignal (obs_source_get_signal_handler(source), "remove",
  30. OBSBasicInteraction::SourceRemoved, this)
  31. {
  32. int cx = (int)config_get_int(App()->GlobalConfig(), "InteractionWindow",
  33. "cx");
  34. int cy = (int)config_get_int(App()->GlobalConfig(), "InteractionWindow",
  35. "cy");
  36. ui->setupUi(this);
  37. ui->preview->setMouseTracking(true);
  38. ui->preview->setFocusPolicy(Qt::StrongFocus);
  39. ui->preview->installEventFilter(BuildEventFilter());
  40. if (cx > 400 && cy > 400)
  41. resize(cx, cy);
  42. OBSData settings = obs_source_get_settings(source);
  43. obs_data_release(settings);
  44. connect(windowHandle(), &QWindow::screenChanged, [this]() {
  45. if (resizeTimer)
  46. killTimer(resizeTimer);
  47. resizeTimer = startTimer(100);
  48. });
  49. const char *name = obs_source_get_name(source);
  50. setWindowTitle(QTStr("Basic.InteractionWindow").arg(QT_UTF8(name)));
  51. }
  52. OBSEventFilter *OBSBasicInteraction::BuildEventFilter()
  53. {
  54. return new OBSEventFilter(
  55. [this](QObject *obj, QEvent *event)
  56. {
  57. UNUSED_PARAMETER(obj);
  58. switch(event->type()) {
  59. case QEvent::MouseButtonPress:
  60. case QEvent::MouseButtonRelease:
  61. case QEvent::MouseButtonDblClick:
  62. return this->HandleMouseClickEvent(
  63. static_cast<QMouseEvent *>(event));
  64. case QEvent::MouseMove:
  65. case QEvent::Enter:
  66. case QEvent::Leave:
  67. return this->HandleMouseMoveEvent(
  68. static_cast<QMouseEvent *>(event));
  69. case QEvent::Wheel:
  70. return this->HandleMouseWheelEvent(
  71. static_cast<QWheelEvent *>(event));
  72. case QEvent::FocusIn:
  73. case QEvent::FocusOut:
  74. return this->HandleFocusEvent(
  75. static_cast<QFocusEvent *>(event));
  76. case QEvent::KeyPress:
  77. case QEvent::KeyRelease:
  78. return this->HandleKeyEvent(
  79. static_cast<QKeyEvent *>(event));
  80. default:
  81. return false;
  82. }
  83. });
  84. }
  85. void OBSBasicInteraction::SourceRemoved(void *data, calldata_t params)
  86. {
  87. QMetaObject::invokeMethod(static_cast<OBSBasicInteraction*>(data),
  88. "close");
  89. UNUSED_PARAMETER(params);
  90. }
  91. void OBSBasicInteraction::DrawPreview(void *data, uint32_t cx, uint32_t cy)
  92. {
  93. OBSBasicInteraction *window = static_cast<OBSBasicInteraction*>(data);
  94. if (!window->source)
  95. return;
  96. uint32_t sourceCX = max(obs_source_get_width(window->source), 1u);
  97. uint32_t sourceCY = max(obs_source_get_height(window->source), 1u);
  98. int x, y;
  99. int newCX, newCY;
  100. float scale;
  101. GetScaleAndCenterPos(sourceCX, sourceCY, cx, cy, x, y, scale);
  102. newCX = int(scale * float(sourceCX));
  103. newCY = int(scale * float(sourceCY));
  104. gs_viewport_push();
  105. gs_projection_push();
  106. gs_ortho(0.0f, float(sourceCX), 0.0f, float(sourceCY),
  107. -100.0f, 100.0f);
  108. gs_set_viewport(x, y, newCX, newCY);
  109. obs_source_video_render(window->source);
  110. gs_projection_pop();
  111. gs_viewport_pop();
  112. }
  113. void OBSBasicInteraction::OnInteractionResized()
  114. {
  115. if (resizeTimer)
  116. killTimer(resizeTimer);
  117. resizeTimer = startTimer(100);
  118. }
  119. void OBSBasicInteraction::resizeEvent(QResizeEvent *event)
  120. {
  121. if (isVisible()) {
  122. if (resizeTimer)
  123. killTimer(resizeTimer);
  124. resizeTimer = startTimer(100);
  125. }
  126. UNUSED_PARAMETER(event);
  127. }
  128. void OBSBasicInteraction::timerEvent(QTimerEvent *event)
  129. {
  130. if (event->timerId() == resizeTimer) {
  131. killTimer(resizeTimer);
  132. resizeTimer = 0;
  133. QSize size = GetPixelSize(ui->preview);
  134. obs_display_resize(display, size.width(), size.height());
  135. }
  136. }
  137. void OBSBasicInteraction::closeEvent(QCloseEvent *event)
  138. {
  139. QDialog::closeEvent(event);
  140. if (!event->isAccepted())
  141. return;
  142. // remove draw callback and release display in case our drawable
  143. // surfaces go away before the destructor gets called
  144. obs_display_remove_draw_callback(display,
  145. OBSBasicInteraction::DrawPreview, this);
  146. display = nullptr;
  147. config_set_int(App()->GlobalConfig(), "InteractionWindow", "cx",
  148. width());
  149. config_set_int(App()->GlobalConfig(), "InteractionWindow", "cy",
  150. height());
  151. }
  152. static int TranslateQtKeyboardEventModifiers(QInputEvent *event, bool mouseEvent) {
  153. int obsModifiers = INTERACT_NONE;
  154. if (event->modifiers().testFlag(Qt::ShiftModifier))
  155. obsModifiers |= INTERACT_SHIFT_KEY;
  156. if (event->modifiers().testFlag(Qt::AltModifier))
  157. obsModifiers |= INTERACT_ALT_KEY;
  158. #ifdef APPLE
  159. // Mac: Meta = Control, Control = Command
  160. if (event->modifiers().testFlag(Qt::ControlModifier))
  161. obsModifiers |= INTERACT_COMMAND_KEY;
  162. if (event->modifiers().testFlag(Qt::MetaModifier))
  163. obsModifiers |= INTERACT_CONTROL_KEY;
  164. #else
  165. // Handle windows key? Can a browser even trap that key?
  166. if (event->modifiers().testFlag(Qt::ControlModifier))
  167. obsModifiers |= INTERACT_CONTROL_KEY;
  168. #endif
  169. if (!mouseEvent) {
  170. if (event->modifiers().testFlag(Qt::KeypadModifier))
  171. obsModifiers |= INTERACT_IS_KEY_PAD;
  172. }
  173. return obsModifiers;
  174. }
  175. static int TranslateQtMouseEventModifiers(
  176. QMouseEvent *event)
  177. {
  178. int modifiers = TranslateQtKeyboardEventModifiers(event, true);
  179. if (event->buttons().testFlag(Qt::LeftButton))
  180. modifiers |= INTERACT_MOUSE_LEFT;
  181. if (event->buttons().testFlag(Qt::MiddleButton))
  182. modifiers |= INTERACT_MOUSE_MIDDLE;
  183. if (event->buttons().testFlag(Qt::RightButton))
  184. modifiers |= INTERACT_MOUSE_RIGHT;
  185. return modifiers;
  186. }
  187. bool OBSBasicInteraction::GetSourceRelativeXY(
  188. int mouseX, int mouseY, int &relX, int &relY)
  189. {
  190. QSize size = GetPixelSize(ui->preview);
  191. uint32_t sourceCX = max(obs_source_get_width(source), 1u);
  192. uint32_t sourceCY = max(obs_source_get_height(source), 1u);
  193. int x, y;
  194. float scale;
  195. GetScaleAndCenterPos(sourceCX, sourceCY, size.width(), size.height(),
  196. x, y, scale);
  197. if (x > 0) {
  198. relX = int(float(mouseX - x) / scale);
  199. relY = int(float(mouseY / scale));
  200. } else {
  201. relX = int(float(mouseX / scale));
  202. relY = int(float(mouseY - y) / scale);
  203. }
  204. // Confirm mouse is inside the source
  205. if (relX < 0 || relX > int(sourceCX))
  206. return false;
  207. if (relY < 0 || relY > int(sourceCY))
  208. return false;
  209. return true;
  210. }
  211. bool OBSBasicInteraction::HandleMouseClickEvent(
  212. QMouseEvent *event)
  213. {
  214. bool mouseUp = event->type() == QEvent::MouseButtonRelease;
  215. int clickCount = 1;
  216. if (event->type() == QEvent::MouseButtonDblClick)
  217. clickCount = 2;
  218. struct obs_mouse_event mouseEvent = {};
  219. mouseEvent.modifiers = TranslateQtMouseEventModifiers(event);
  220. int32_t button = 0;
  221. switch (event->button()) {
  222. case Qt::LeftButton:
  223. button = MOUSE_LEFT;
  224. break;
  225. case Qt::MiddleButton:
  226. button = MOUSE_MIDDLE;
  227. break;
  228. case Qt::RightButton:
  229. button = MOUSE_RIGHT;
  230. break;
  231. default:
  232. blog(LOG_WARNING, "unknown button type %d",
  233. event->button());
  234. return false;
  235. }
  236. // Why doesn't this work?
  237. //if (event->flags().testFlag(Qt::MouseEventCreatedDoubleClick))
  238. // clickCount = 2;
  239. bool insideSource = GetSourceRelativeXY(event->x(), event->y(),
  240. mouseEvent.x, mouseEvent.y);
  241. if (mouseUp || insideSource)
  242. obs_source_send_mouse_click(source, &mouseEvent, button,
  243. mouseUp, clickCount);
  244. return true;
  245. }
  246. bool OBSBasicInteraction::HandleMouseMoveEvent(QMouseEvent *event)
  247. {
  248. struct obs_mouse_event mouseEvent = {};
  249. bool mouseLeave = event->type() == QEvent::Leave;
  250. if (!mouseLeave) {
  251. mouseEvent.modifiers = TranslateQtMouseEventModifiers(event);
  252. mouseLeave = !GetSourceRelativeXY(event->x(), event->y(),
  253. mouseEvent.x, mouseEvent.y);
  254. }
  255. obs_source_send_mouse_move(source, &mouseEvent, mouseLeave);
  256. return true;
  257. }
  258. bool OBSBasicInteraction::HandleMouseWheelEvent(QWheelEvent *event)
  259. {
  260. struct obs_mouse_event mouseEvent = {};
  261. mouseEvent.modifiers = TranslateQtKeyboardEventModifiers(event, true);
  262. int xDelta = 0;
  263. int yDelta = 0;
  264. if (!event->pixelDelta().isNull()) {
  265. if (event->orientation() == Qt::Horizontal)
  266. xDelta = event->pixelDelta().x();
  267. else
  268. yDelta = event->pixelDelta().y();
  269. } else {
  270. if (event->orientation() == Qt::Horizontal)
  271. xDelta = event->delta();
  272. else
  273. yDelta = event->delta();
  274. }
  275. obs_source_send_mouse_wheel(source, &mouseEvent, xDelta, yDelta);
  276. return true;
  277. }
  278. bool OBSBasicInteraction::HandleFocusEvent(QFocusEvent *event)
  279. {
  280. bool focus = event->type() == QEvent::FocusIn;
  281. obs_source_send_focus(source, focus);
  282. return true;
  283. }
  284. bool OBSBasicInteraction::HandleKeyEvent(QKeyEvent *event)
  285. {
  286. struct obs_key_event keyEvent;
  287. QByteArray text = event->text().toUtf8();
  288. keyEvent.modifiers = TranslateQtKeyboardEventModifiers(event, false);
  289. keyEvent.text = text.data();
  290. keyEvent.native_modifiers = event->nativeModifiers();
  291. keyEvent.native_scancode = event->nativeScanCode();
  292. keyEvent.native_vkey = event->nativeVirtualKey();
  293. bool keyUp = event->type() == QEvent::KeyRelease;
  294. obs_source_send_key_click(source, &keyEvent, keyUp);
  295. return true;
  296. }
  297. void OBSBasicInteraction::Init()
  298. {
  299. gs_init_data init_data = {};
  300. show();
  301. QSize previewSize = GetPixelSize(ui->preview);
  302. init_data.cx = uint32_t(previewSize.width());
  303. init_data.cy = uint32_t(previewSize.height());
  304. init_data.format = GS_RGBA;
  305. QTToGSWindow(ui->preview->winId(), init_data.window);
  306. display = obs_display_create(&init_data);
  307. if (display)
  308. obs_display_add_draw_callback(display,
  309. OBSBasicInteraction::DrawPreview, this);
  310. }