OBSBasicInteraction.cpp 11 KB

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