1
0

window-basic-interaction.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain 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 "moc_window-basic-interaction.cpp"
  16. #include "window-basic-main.hpp"
  17. #include "display-helpers.hpp"
  18. #include <qt-wrappers.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()->GetUserConfig(),
  40. "InteractionWindow", "cx");
  41. int cy = (int)config_get_int(App()->GetUserConfig(),
  42. "InteractionWindow", "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 *, QEvent *event) {
  70. switch (event->type()) {
  71. case QEvent::MouseButtonPress:
  72. case QEvent::MouseButtonRelease:
  73. case QEvent::MouseButtonDblClick:
  74. return this->HandleMouseClickEvent(
  75. static_cast<QMouseEvent *>(event));
  76. case QEvent::MouseMove:
  77. case QEvent::Enter:
  78. case QEvent::Leave:
  79. return this->HandleMouseMoveEvent(
  80. static_cast<QMouseEvent *>(event));
  81. case QEvent::Wheel:
  82. return this->HandleMouseWheelEvent(
  83. static_cast<QWheelEvent *>(event));
  84. case QEvent::FocusIn:
  85. case QEvent::FocusOut:
  86. return this->HandleFocusEvent(
  87. static_cast<QFocusEvent *>(event));
  88. case QEvent::KeyPress:
  89. case QEvent::KeyRelease:
  90. return this->HandleKeyEvent(
  91. static_cast<QKeyEvent *>(event));
  92. default:
  93. return false;
  94. }
  95. });
  96. }
  97. void OBSBasicInteraction::SourceRemoved(void *data, calldata_t *)
  98. {
  99. QMetaObject::invokeMethod(static_cast<OBSBasicInteraction *>(data),
  100. "close");
  101. }
  102. void OBSBasicInteraction::SourceRenamed(void *data, calldata_t *params)
  103. {
  104. const char *name = calldata_string(params, "new_name");
  105. QString title = QTStr("Basic.InteractionWindow").arg(QT_UTF8(name));
  106. QMetaObject::invokeMethod(static_cast<OBSBasicProperties *>(data),
  107. "setWindowTitle", Q_ARG(QString, title));
  108. }
  109. void OBSBasicInteraction::DrawPreview(void *data, uint32_t cx, uint32_t cy)
  110. {
  111. OBSBasicInteraction *window = static_cast<OBSBasicInteraction *>(data);
  112. if (!window->source)
  113. return;
  114. uint32_t sourceCX = max(obs_source_get_width(window->source), 1u);
  115. uint32_t sourceCY = max(obs_source_get_height(window->source), 1u);
  116. int x, y;
  117. int newCX, newCY;
  118. float scale;
  119. GetScaleAndCenterPos(sourceCX, sourceCY, cx, cy, x, y, scale);
  120. newCX = int(scale * float(sourceCX));
  121. newCY = int(scale * float(sourceCY));
  122. gs_viewport_push();
  123. gs_projection_push();
  124. const bool previous = gs_set_linear_srgb(true);
  125. gs_ortho(0.0f, float(sourceCX), 0.0f, float(sourceCY), -100.0f, 100.0f);
  126. gs_set_viewport(x, y, newCX, newCY);
  127. obs_source_video_render(window->source);
  128. gs_set_linear_srgb(previous);
  129. gs_projection_pop();
  130. gs_viewport_pop();
  131. }
  132. void OBSBasicInteraction::closeEvent(QCloseEvent *event)
  133. {
  134. QDialog::closeEvent(event);
  135. if (!event->isAccepted())
  136. return;
  137. config_set_int(App()->GetAppConfig(), "InteractionWindow", "cx",
  138. width());
  139. config_set_int(App()->GetAppConfig(), "InteractionWindow", "cy",
  140. height());
  141. obs_display_remove_draw_callback(ui->preview->GetDisplay(),
  142. OBSBasicInteraction::DrawPreview,
  143. this);
  144. }
  145. bool OBSBasicInteraction::nativeEvent(const QByteArray &, void *message,
  146. qintptr *)
  147. {
  148. #ifdef _WIN32
  149. const MSG &msg = *static_cast<MSG *>(message);
  150. switch (msg.message) {
  151. case WM_MOVE:
  152. for (OBSQTDisplay *const display :
  153. findChildren<OBSQTDisplay *>()) {
  154. display->OnMove();
  155. }
  156. break;
  157. case WM_DISPLAYCHANGE:
  158. for (OBSQTDisplay *const display :
  159. findChildren<OBSQTDisplay *>()) {
  160. display->OnDisplayChange();
  161. }
  162. }
  163. #else
  164. UNUSED_PARAMETER(message);
  165. #endif
  166. return false;
  167. }
  168. static int TranslateQtKeyboardEventModifiers(QInputEvent *event,
  169. bool mouseEvent)
  170. {
  171. int obsModifiers = INTERACT_NONE;
  172. if (event->modifiers().testFlag(Qt::ShiftModifier))
  173. obsModifiers |= INTERACT_SHIFT_KEY;
  174. if (event->modifiers().testFlag(Qt::AltModifier))
  175. obsModifiers |= INTERACT_ALT_KEY;
  176. #ifdef __APPLE__
  177. // Mac: Meta = Control, Control = Command
  178. if (event->modifiers().testFlag(Qt::ControlModifier))
  179. obsModifiers |= INTERACT_COMMAND_KEY;
  180. if (event->modifiers().testFlag(Qt::MetaModifier))
  181. obsModifiers |= INTERACT_CONTROL_KEY;
  182. #else
  183. // Handle windows key? Can a browser even trap that key?
  184. if (event->modifiers().testFlag(Qt::ControlModifier))
  185. obsModifiers |= INTERACT_CONTROL_KEY;
  186. #endif
  187. if (!mouseEvent) {
  188. if (event->modifiers().testFlag(Qt::KeypadModifier))
  189. obsModifiers |= INTERACT_IS_KEY_PAD;
  190. }
  191. return obsModifiers;
  192. }
  193. static int TranslateQtMouseEventModifiers(QMouseEvent *event)
  194. {
  195. int modifiers = TranslateQtKeyboardEventModifiers(event, true);
  196. if (event->buttons().testFlag(Qt::LeftButton))
  197. modifiers |= INTERACT_MOUSE_LEFT;
  198. if (event->buttons().testFlag(Qt::MiddleButton))
  199. modifiers |= INTERACT_MOUSE_MIDDLE;
  200. if (event->buttons().testFlag(Qt::RightButton))
  201. modifiers |= INTERACT_MOUSE_RIGHT;
  202. return modifiers;
  203. }
  204. bool OBSBasicInteraction::GetSourceRelativeXY(int mouseX, int mouseY, int &relX,
  205. int &relY)
  206. {
  207. float pixelRatio = devicePixelRatioF();
  208. int mouseXscaled = (int)roundf(mouseX * pixelRatio);
  209. int mouseYscaled = (int)roundf(mouseY * pixelRatio);
  210. QSize size = GetPixelSize(ui->preview);
  211. uint32_t sourceCX = max(obs_source_get_width(source), 1u);
  212. uint32_t sourceCY = max(obs_source_get_height(source), 1u);
  213. int x, y;
  214. float scale;
  215. GetScaleAndCenterPos(sourceCX, sourceCY, size.width(), size.height(), x,
  216. y, scale);
  217. if (x > 0) {
  218. relX = int(float(mouseXscaled - x) / scale);
  219. relY = int(float(mouseYscaled / scale));
  220. } else {
  221. relX = int(float(mouseXscaled / scale));
  222. relY = int(float(mouseYscaled - y) / scale);
  223. }
  224. // Confirm mouse is inside the source
  225. if (relX < 0 || relX > int(sourceCX))
  226. return false;
  227. if (relY < 0 || relY > int(sourceCY))
  228. return false;
  229. return true;
  230. }
  231. bool OBSBasicInteraction::HandleMouseClickEvent(QMouseEvent *event)
  232. {
  233. bool mouseUp = event->type() == QEvent::MouseButtonRelease;
  234. int clickCount = 1;
  235. if (event->type() == QEvent::MouseButtonDblClick)
  236. clickCount = 2;
  237. struct obs_mouse_event mouseEvent = {};
  238. mouseEvent.modifiers = TranslateQtMouseEventModifiers(event);
  239. int32_t button = 0;
  240. switch (event->button()) {
  241. case Qt::LeftButton:
  242. button = MOUSE_LEFT;
  243. break;
  244. case Qt::MiddleButton:
  245. button = MOUSE_MIDDLE;
  246. break;
  247. case Qt::RightButton:
  248. button = MOUSE_RIGHT;
  249. break;
  250. default:
  251. blog(LOG_WARNING, "unknown button type %d", event->button());
  252. return false;
  253. }
  254. // Why doesn't this work?
  255. //if (event->flags().testFlag(Qt::MouseEventCreatedDoubleClick))
  256. // clickCount = 2;
  257. QPoint pos = event->pos();
  258. bool insideSource = GetSourceRelativeXY(pos.x(), pos.y(), mouseEvent.x,
  259. mouseEvent.y);
  260. if (mouseUp || insideSource)
  261. obs_source_send_mouse_click(source, &mouseEvent, button,
  262. mouseUp, clickCount);
  263. return true;
  264. }
  265. bool OBSBasicInteraction::HandleMouseMoveEvent(QMouseEvent *event)
  266. {
  267. struct obs_mouse_event mouseEvent = {};
  268. bool mouseLeave = event->type() == QEvent::Leave;
  269. if (!mouseLeave) {
  270. mouseEvent.modifiers = TranslateQtMouseEventModifiers(event);
  271. QPoint pos = event->pos();
  272. mouseLeave = !GetSourceRelativeXY(pos.x(), pos.y(),
  273. mouseEvent.x, mouseEvent.y);
  274. }
  275. obs_source_send_mouse_move(source, &mouseEvent, mouseLeave);
  276. return true;
  277. }
  278. bool OBSBasicInteraction::HandleMouseWheelEvent(QWheelEvent *event)
  279. {
  280. struct obs_mouse_event mouseEvent = {};
  281. mouseEvent.modifiers = TranslateQtKeyboardEventModifiers(event, true);
  282. int xDelta = 0;
  283. int yDelta = 0;
  284. const QPoint angleDelta = event->angleDelta();
  285. if (!event->pixelDelta().isNull()) {
  286. if (angleDelta.x())
  287. xDelta = event->pixelDelta().x();
  288. else
  289. yDelta = event->pixelDelta().y();
  290. } else {
  291. if (angleDelta.x())
  292. xDelta = angleDelta.x();
  293. else
  294. yDelta = angleDelta.y();
  295. }
  296. const QPointF position = event->position();
  297. const int x = position.x();
  298. const int y = position.y();
  299. if (GetSourceRelativeXY(x, y, mouseEvent.x, mouseEvent.y)) {
  300. obs_source_send_mouse_wheel(source, &mouseEvent, xDelta,
  301. yDelta);
  302. }
  303. return true;
  304. }
  305. bool OBSBasicInteraction::HandleFocusEvent(QFocusEvent *event)
  306. {
  307. bool focus = event->type() == QEvent::FocusIn;
  308. obs_source_send_focus(source, focus);
  309. return true;
  310. }
  311. bool OBSBasicInteraction::HandleKeyEvent(QKeyEvent *event)
  312. {
  313. struct obs_key_event keyEvent;
  314. QByteArray text = event->text().toUtf8();
  315. keyEvent.modifiers = TranslateQtKeyboardEventModifiers(event, false);
  316. keyEvent.text = text.data();
  317. keyEvent.native_modifiers = event->nativeModifiers();
  318. keyEvent.native_scancode = event->nativeScanCode();
  319. keyEvent.native_vkey = event->nativeVirtualKey();
  320. bool keyUp = event->type() == QEvent::KeyRelease;
  321. obs_source_send_key_click(source, &keyEvent, keyUp);
  322. return true;
  323. }
  324. void OBSBasicInteraction::Init()
  325. {
  326. show();
  327. }