window-basic-interaction.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. eventFilter (BuildEventFilter())
  32. {
  33. int cx = (int)config_get_int(App()->GlobalConfig(), "InteractionWindow",
  34. "cx");
  35. int cy = (int)config_get_int(App()->GlobalConfig(), "InteractionWindow",
  36. "cy");
  37. ui->setupUi(this);
  38. ui->preview->setMouseTracking(true);
  39. ui->preview->setFocusPolicy(Qt::StrongFocus);
  40. ui->preview->installEventFilter(eventFilter.get());
  41. if (cx > 400 && cy > 400)
  42. resize(cx, cy);
  43. OBSData settings = obs_source_get_settings(source);
  44. obs_data_release(settings);
  45. connect(windowHandle(), &QWindow::screenChanged, [this]() {
  46. if (resizeTimer)
  47. killTimer(resizeTimer);
  48. resizeTimer = startTimer(100);
  49. });
  50. const char *name = obs_source_get_name(source);
  51. setWindowTitle(QTStr("Basic.InteractionWindow").arg(QT_UTF8(name)));
  52. }
  53. OBSBasicInteraction::~OBSBasicInteraction()
  54. {
  55. // since QT fakes a mouse movement while destructing a widget
  56. // remove our event filter
  57. ui->preview->removeEventFilter(eventFilter.get());
  58. }
  59. OBSEventFilter *OBSBasicInteraction::BuildEventFilter()
  60. {
  61. return new OBSEventFilter(
  62. [this](QObject *obj, QEvent *event)
  63. {
  64. UNUSED_PARAMETER(obj);
  65. switch(event->type()) {
  66. case QEvent::MouseButtonPress:
  67. case QEvent::MouseButtonRelease:
  68. case QEvent::MouseButtonDblClick:
  69. return this->HandleMouseClickEvent(
  70. static_cast<QMouseEvent *>(event));
  71. case QEvent::MouseMove:
  72. case QEvent::Enter:
  73. case QEvent::Leave:
  74. return this->HandleMouseMoveEvent(
  75. static_cast<QMouseEvent *>(event));
  76. case QEvent::Wheel:
  77. return this->HandleMouseWheelEvent(
  78. static_cast<QWheelEvent *>(event));
  79. case QEvent::FocusIn:
  80. case QEvent::FocusOut:
  81. return this->HandleFocusEvent(
  82. static_cast<QFocusEvent *>(event));
  83. case QEvent::KeyPress:
  84. case QEvent::KeyRelease:
  85. return this->HandleKeyEvent(
  86. static_cast<QKeyEvent *>(event));
  87. default:
  88. return false;
  89. }
  90. });
  91. }
  92. void OBSBasicInteraction::SourceRemoved(void *data, calldata_t params)
  93. {
  94. QMetaObject::invokeMethod(static_cast<OBSBasicInteraction*>(data),
  95. "close");
  96. UNUSED_PARAMETER(params);
  97. }
  98. void OBSBasicInteraction::DrawPreview(void *data, uint32_t cx, uint32_t cy)
  99. {
  100. OBSBasicInteraction *window = static_cast<OBSBasicInteraction*>(data);
  101. if (!window->source)
  102. return;
  103. uint32_t sourceCX = max(obs_source_get_width(window->source), 1u);
  104. uint32_t sourceCY = max(obs_source_get_height(window->source), 1u);
  105. int x, y;
  106. int newCX, newCY;
  107. float scale;
  108. GetScaleAndCenterPos(sourceCX, sourceCY, cx, cy, x, y, scale);
  109. newCX = int(scale * float(sourceCX));
  110. newCY = int(scale * float(sourceCY));
  111. gs_viewport_push();
  112. gs_projection_push();
  113. gs_ortho(0.0f, float(sourceCX), 0.0f, float(sourceCY),
  114. -100.0f, 100.0f);
  115. gs_set_viewport(x, y, newCX, newCY);
  116. obs_source_video_render(window->source);
  117. gs_projection_pop();
  118. gs_viewport_pop();
  119. }
  120. void OBSBasicInteraction::OnInteractionResized()
  121. {
  122. if (resizeTimer)
  123. killTimer(resizeTimer);
  124. resizeTimer = startTimer(100);
  125. }
  126. void OBSBasicInteraction::resizeEvent(QResizeEvent *event)
  127. {
  128. if (isVisible()) {
  129. if (resizeTimer)
  130. killTimer(resizeTimer);
  131. resizeTimer = startTimer(100);
  132. }
  133. UNUSED_PARAMETER(event);
  134. }
  135. void OBSBasicInteraction::timerEvent(QTimerEvent *event)
  136. {
  137. if (event->timerId() == resizeTimer) {
  138. killTimer(resizeTimer);
  139. resizeTimer = 0;
  140. QSize size = GetPixelSize(ui->preview);
  141. obs_display_resize(display, size.width(), size.height());
  142. }
  143. }
  144. void OBSBasicInteraction::closeEvent(QCloseEvent *event)
  145. {
  146. QDialog::closeEvent(event);
  147. if (!event->isAccepted())
  148. return;
  149. // remove draw callback and release display in case our drawable
  150. // surfaces go away before the destructor gets called
  151. obs_display_remove_draw_callback(display,
  152. OBSBasicInteraction::DrawPreview, this);
  153. display = nullptr;
  154. config_set_int(App()->GlobalConfig(), "InteractionWindow", "cx",
  155. width());
  156. config_set_int(App()->GlobalConfig(), "InteractionWindow", "cy",
  157. height());
  158. }
  159. static int TranslateQtKeyboardEventModifiers(QInputEvent *event, bool mouseEvent) {
  160. int obsModifiers = INTERACT_NONE;
  161. if (event->modifiers().testFlag(Qt::ShiftModifier))
  162. obsModifiers |= INTERACT_SHIFT_KEY;
  163. if (event->modifiers().testFlag(Qt::AltModifier))
  164. obsModifiers |= INTERACT_ALT_KEY;
  165. #ifdef APPLE
  166. // Mac: Meta = Control, Control = Command
  167. if (event->modifiers().testFlag(Qt::ControlModifier))
  168. obsModifiers |= INTERACT_COMMAND_KEY;
  169. if (event->modifiers().testFlag(Qt::MetaModifier))
  170. obsModifiers |= INTERACT_CONTROL_KEY;
  171. #else
  172. // Handle windows key? Can a browser even trap that key?
  173. if (event->modifiers().testFlag(Qt::ControlModifier))
  174. obsModifiers |= INTERACT_CONTROL_KEY;
  175. #endif
  176. if (!mouseEvent) {
  177. if (event->modifiers().testFlag(Qt::KeypadModifier))
  178. obsModifiers |= INTERACT_IS_KEY_PAD;
  179. }
  180. return obsModifiers;
  181. }
  182. static int TranslateQtMouseEventModifiers(
  183. QMouseEvent *event)
  184. {
  185. int modifiers = TranslateQtKeyboardEventModifiers(event, true);
  186. if (event->buttons().testFlag(Qt::LeftButton))
  187. modifiers |= INTERACT_MOUSE_LEFT;
  188. if (event->buttons().testFlag(Qt::MiddleButton))
  189. modifiers |= INTERACT_MOUSE_MIDDLE;
  190. if (event->buttons().testFlag(Qt::RightButton))
  191. modifiers |= INTERACT_MOUSE_RIGHT;
  192. return modifiers;
  193. }
  194. bool OBSBasicInteraction::GetSourceRelativeXY(
  195. int mouseX, int mouseY, int &relX, int &relY)
  196. {
  197. QSize size = GetPixelSize(ui->preview);
  198. uint32_t sourceCX = max(obs_source_get_width(source), 1u);
  199. uint32_t sourceCY = max(obs_source_get_height(source), 1u);
  200. int x, y;
  201. float scale;
  202. GetScaleAndCenterPos(sourceCX, sourceCY, size.width(), size.height(),
  203. x, y, scale);
  204. if (x > 0) {
  205. relX = int(float(mouseX - x) / scale);
  206. relY = int(float(mouseY / scale));
  207. } else {
  208. relX = int(float(mouseX / scale));
  209. relY = int(float(mouseY - y) / scale);
  210. }
  211. // Confirm mouse is inside the source
  212. if (relX < 0 || relX > int(sourceCX))
  213. return false;
  214. if (relY < 0 || relY > int(sourceCY))
  215. return false;
  216. return true;
  217. }
  218. bool OBSBasicInteraction::HandleMouseClickEvent(
  219. QMouseEvent *event)
  220. {
  221. bool mouseUp = event->type() == QEvent::MouseButtonRelease;
  222. int clickCount = 1;
  223. if (event->type() == QEvent::MouseButtonDblClick)
  224. clickCount = 2;
  225. struct obs_mouse_event mouseEvent = {};
  226. mouseEvent.modifiers = TranslateQtMouseEventModifiers(event);
  227. int32_t button = 0;
  228. switch (event->button()) {
  229. case Qt::LeftButton:
  230. button = MOUSE_LEFT;
  231. break;
  232. case Qt::MiddleButton:
  233. button = MOUSE_MIDDLE;
  234. break;
  235. case Qt::RightButton:
  236. button = MOUSE_RIGHT;
  237. break;
  238. default:
  239. blog(LOG_WARNING, "unknown button type %d",
  240. event->button());
  241. return false;
  242. }
  243. // Why doesn't this work?
  244. //if (event->flags().testFlag(Qt::MouseEventCreatedDoubleClick))
  245. // clickCount = 2;
  246. bool insideSource = GetSourceRelativeXY(event->x(), event->y(),
  247. mouseEvent.x, mouseEvent.y);
  248. if (mouseUp || insideSource)
  249. obs_source_send_mouse_click(source, &mouseEvent, button,
  250. mouseUp, clickCount);
  251. return true;
  252. }
  253. bool OBSBasicInteraction::HandleMouseMoveEvent(QMouseEvent *event)
  254. {
  255. struct obs_mouse_event mouseEvent = {};
  256. bool mouseLeave = event->type() == QEvent::Leave;
  257. if (!mouseLeave) {
  258. mouseEvent.modifiers = TranslateQtMouseEventModifiers(event);
  259. mouseLeave = !GetSourceRelativeXY(event->x(), event->y(),
  260. mouseEvent.x, mouseEvent.y);
  261. }
  262. obs_source_send_mouse_move(source, &mouseEvent, mouseLeave);
  263. return true;
  264. }
  265. bool OBSBasicInteraction::HandleMouseWheelEvent(QWheelEvent *event)
  266. {
  267. struct obs_mouse_event mouseEvent = {};
  268. mouseEvent.modifiers = TranslateQtKeyboardEventModifiers(event, true);
  269. int xDelta = 0;
  270. int yDelta = 0;
  271. if (!event->pixelDelta().isNull()) {
  272. if (event->orientation() == Qt::Horizontal)
  273. xDelta = event->pixelDelta().x();
  274. else
  275. yDelta = event->pixelDelta().y();
  276. } else {
  277. if (event->orientation() == Qt::Horizontal)
  278. xDelta = event->delta();
  279. else
  280. yDelta = event->delta();
  281. }
  282. if (GetSourceRelativeXY(event->x(), event->y(), mouseEvent.x,
  283. mouseEvent.y))
  284. obs_source_send_mouse_wheel(source, &mouseEvent, xDelta,
  285. yDelta);
  286. return true;
  287. }
  288. bool OBSBasicInteraction::HandleFocusEvent(QFocusEvent *event)
  289. {
  290. bool focus = event->type() == QEvent::FocusIn;
  291. obs_source_send_focus(source, focus);
  292. return true;
  293. }
  294. bool OBSBasicInteraction::HandleKeyEvent(QKeyEvent *event)
  295. {
  296. struct obs_key_event keyEvent;
  297. QByteArray text = event->text().toUtf8();
  298. keyEvent.modifiers = TranslateQtKeyboardEventModifiers(event, false);
  299. keyEvent.text = text.data();
  300. keyEvent.native_modifiers = event->nativeModifiers();
  301. keyEvent.native_scancode = event->nativeScanCode();
  302. keyEvent.native_vkey = event->nativeVirtualKey();
  303. bool keyUp = event->type() == QEvent::KeyRelease;
  304. obs_source_send_key_click(source, &keyEvent, keyUp);
  305. return true;
  306. }
  307. void OBSBasicInteraction::Init()
  308. {
  309. gs_init_data init_data = {};
  310. show();
  311. QSize previewSize = GetPixelSize(ui->preview);
  312. init_data.cx = uint32_t(previewSize.width());
  313. init_data.cy = uint32_t(previewSize.height());
  314. init_data.format = GS_RGBA;
  315. QTToGSWindow(ui->preview->winId(), init_data.window);
  316. display = obs_display_create(&init_data);
  317. if (display)
  318. obs_display_add_draw_callback(display,
  319. OBSBasicInteraction::DrawPreview, this);
  320. }