window-basic-interaction.cpp 11 KB

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