Debian Codegraph @

xbmc/input/mouse/MouseTranslator.cpp

raw
1 /*
2 * Copyright (C) 2017-2024 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #include "MouseTranslator.h"
10
11 #include "MouseStat.h"
12 #include "input/keyboard/KeyIDs.h"
13 #include "utils/StringUtils.h"
14 #include "utils/log.h"
15
16 #include <map>
17 #include <string>
18
19 #include <tinyxml2.h>
20
21 using namespace KODI;
22 using namespace MOUSE;
23
24 namespace
25 {
26
27 using ActionName = std::string;
28 using KeyID = uint32_t;
29
30 static const std::map<ActionName, KeyID> MouseKeys = {{"click", KEY_MOUSE_CLICK},
31 {"leftclick", KEY_MOUSE_CLICK},
32 {"rightclick", KEY_MOUSE_RIGHTCLICK},
33 {"middleclick", KEY_MOUSE_MIDDLECLICK},
34 {"doubleclick", KEY_MOUSE_DOUBLE_CLICK},
35 {"longclick", KEY_MOUSE_LONG_CLICK},
36 {"wheelup", KEY_MOUSE_WHEEL_UP},
37 {"wheeldown", KEY_MOUSE_WHEEL_DOWN},
38 {"mousemove", KEY_MOUSE_MOVE},
39 {"mousedrag", KEY_MOUSE_DRAG},
40 {"mousedragstart", KEY_MOUSE_DRAG_START},
41 {"mousedragend", KEY_MOUSE_DRAG_END},
42 {"mouserdrag", KEY_MOUSE_RDRAG},
43 {"mouserdragstart", KEY_MOUSE_RDRAG_START},
44 {"mouserdragend", KEY_MOUSE_RDRAG_END}};
45
46 } // anonymous namespace
47
48 uint32_t CMouseTranslator::TranslateCommand(const tinyxml2::XMLElement* pButton)
49 {
50 uint32_t buttonId = 0;
51
52 if (pButton != nullptr)
53 {
54 std::string szKey = pButton->Value();
55 if (!szKey.empty())
56 {
57 StringUtils::ToLower(szKey);
58
59 auto it = MouseKeys.find(szKey);
60 if (it != MouseKeys.end())
61 buttonId = it->second;
62
63 if (buttonId == 0)
64 {
65 CLog::Log(LOGERROR, "Unknown mouse action ({}), skipping", pButton->Value());
66 }
67 else
68 {
69 int id = 0;
70 if ((pButton->QueryIntAttribute("id", &id) == tinyxml2::XML_SUCCESS))
71 {
72 if (0 <= id && id < MOUSE_MAX_BUTTON)
73 buttonId += id;
74 }
75 }
76 }
77 }
78
79 return buttonId;
80 }
81
82 bool CMouseTranslator::TranslateEventID(unsigned int eventId, BUTTON_ID& buttonId)
83 {
84 switch (eventId)
85 {
86 case XBMC_BUTTON_LEFT:
87 {
88 buttonId = BUTTON_ID::LEFT;
89 return true;
90 }
91 case XBMC_BUTTON_MIDDLE:
92 {
93 buttonId = BUTTON_ID::MIDDLE;
94 return true;
95 }
96 case XBMC_BUTTON_RIGHT:
97 {
98 buttonId = BUTTON_ID::RIGHT;
99 return true;
100 }
101 case XBMC_BUTTON_WHEELUP:
102 {
103 buttonId = BUTTON_ID::WHEEL_UP;
104 return true;
105 }
106 case XBMC_BUTTON_WHEELDOWN:
107 {
108 buttonId = BUTTON_ID::WHEEL_DOWN;
109 return true;
110 }
111 case XBMC_BUTTON_X1:
112 {
113 buttonId = BUTTON_ID::BUTTON4;
114 return true;
115 }
116 case XBMC_BUTTON_X2:
117 {
118 buttonId = BUTTON_ID::BUTTON5;
119 return true;
120 }
121 case XBMC_BUTTON_X3:
122 {
123 buttonId = BUTTON_ID::HORIZ_WHEEL_LEFT;
124 return true;
125 }
126 case XBMC_BUTTON_X4:
127 {
128 buttonId = BUTTON_ID::HORIZ_WHEEL_RIGHT;
129 return true;
130 }
131 default:
132 break;
133 }
134
135 return false;
136 }