window-basic-interaction.cpp 11 KB

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