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