window-basic-interaction.cpp 9.7 KB

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