OBSBasicInteraction.cpp 11 KB

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