window-basic-interaction.cpp 11 KB

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