Otclient  14/8/2020
platformwindow.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #include "platformwindow.h"
24 
25 #ifdef WIN32
26 #include "win32window.h"
28 #else
29 #include "x11window.h"
30 #include <framework/core/clock.h>
32 #endif
33 
34 #include <framework/core/clock.h>
36 
38 
39 int PlatformWindow::loadMouseCursor(const std::string& file, const Point& hotSpot)
40 {
41  ImagePtr image = Image::load(file);
42 
43  if(!image) {
44  g_logger.traceError(stdext::format("unable to load cursor image file %s", file));
45  return -1;
46  }
47 
48  if(image->getBpp() != 4) {
49  g_logger.error("the cursor image must have 4 channels");
50  return -1;
51  }
52 
53  if(image->getWidth() != 32 || image->getHeight() != 32) {
54  g_logger.error("the cursor image must have 32x32 dimension");
55  return -1;
56  }
57 
58  return internalLoadMouseCursor(image, hotSpot);
59 }
60 
62 {
63  if(!isMaximized() && !isFullscreen()) {
66  }
67 }
68 
70 {
71  if(keyCode == Fw::KeyUnknown)
72  return;
73 
74  if(keyCode == Fw::KeyCtrl) {
76  return;
77  } else if(keyCode == Fw::KeyAlt) {
79  return;
80  } else if(keyCode == Fw::KeyShift) {
82  return;
83  }
84 
85  if(m_keysState[keyCode])
86  return;
87 
88  m_keysState[keyCode] = true;
89  m_lastKeysPress[keyCode] = -1;
90 
93  m_inputEvent.keyCode = keyCode;
94 
95  if(m_onInputEvent) {
97 
99  m_inputEvent.keyCode = keyCode;
100  m_lastKeysPress[keyCode] = g_clock.millis();
101  m_firstKeysPress[keyCode] = g_clock.millis();
103  }
104 }
105 
107 {
108  if(keyCode == Fw::KeyUnknown)
109  return;
110 
111  if(keyCode == Fw::KeyCtrl) {
113  return;
114  } else if(keyCode == Fw::KeyAlt) {
116  return;
117  } else if(keyCode == Fw::KeyShift) {
119  return;
120  } else if(keyCode == Fw::KeyNumLock) {
121  for(uchar k = Fw::KeyNumpad0; k <= Fw::KeyNumpad9; ++k) {
122  if(m_keysState[(Fw::Key)k])
123  processKeyUp((Fw::Key)k);
124  }
125  }
126 
127  if(!m_keysState[keyCode])
128  return;
129 
130  m_keysState[keyCode] = false;
131 
132  if(m_onInputEvent) {
134  m_inputEvent.keyCode = keyCode;
136  }
137 }
138 
140 {
141  for(auto it : m_keysState) {
142  Fw::Key keyCode = it.first;
143  bool pressed = it.second;
144 
145  if(!pressed)
146  continue;
147 
148  processKeyUp(keyCode);
149  }
150 
152 
153  for(auto &mouseButtonState: m_mouseButtonStates)
154  mouseButtonState = false;
155 }
156 
158 {
159  // avoid massive checks
160  if(m_keyPressTimer.ticksElapsed() < 10)
161  return;
163 
164  for(auto it : m_keysState) {
165  Fw::Key keyCode = it.first;
166  bool pressed = it.second;
167 
168  if(!pressed)
169  continue;
170 
171  ticks_t lastPressTicks = m_lastKeysPress[keyCode];
172  ticks_t firstKeyPress = m_firstKeysPress[keyCode];
173  if(g_clock.millis() - lastPressTicks >= KEY_PRESS_REPEAT_INTERVAL) {
174  if(m_onInputEvent) {
176  m_inputEvent.keyCode = keyCode;
177  m_inputEvent.autoRepeatTicks = g_clock.millis() - firstKeyPress;
179  }
180  m_lastKeysPress[keyCode] = g_clock.millis();
181  }
182  }
183 }
184 
Fw::KeyNumpad9
@ KeyNumpad9
Definition: const.h:175
Fw::KeyUnknown
@ KeyUnknown
Definition: const.h:58
PlatformWindow::m_position
Point m_position
Definition: platformwindow.h:115
PlatformWindow::m_lastKeysPress
std::map< Fw::Key, ticks_t > m_lastKeysPress
Definition: platformwindow.h:110
Timer::ticksElapsed
ticks_t ticksElapsed()
Definition: timer.cpp:33
platformwindow.h
Image::load
static ImagePtr load(std::string file)
Definition: image.cpp:39
Logger::error
void error(const std::string &what)
Definition: logger.h:54
Fw::KeyUpInputEvent
@ KeyUpInputEvent
Definition: const.h:239
ticks_t
int64 ticks_t
Definition: types.h:43
PlatformWindow
Definition: platformwindow.h:32
Fw::Key
Key
Definition: const.h:57
PlatformWindow::m_unmaximizedPos
Point m_unmaximizedPos
Definition: platformwindow.h:117
Fw::KeyboardShiftModifier
@ KeyboardShiftModifier
Definition: const.h:263
stdext::format
std::string format()
Definition: format.h:82
PlatformWindow::updateUnmaximizedCoords
void updateUnmaximizedCoords()
Definition: platformwindow.cpp:61
PlatformWindow::m_inputEvent
InputEvent m_inputEvent
Definition: platformwindow.h:118
PlatformWindow::isFullscreen
bool isFullscreen()
Definition: platformwindow.h:90
Fw::KeyPressInputEvent
@ KeyPressInputEvent
Definition: const.h:238
x11window.h
clock.h
PlatformWindow::m_unmaximizedSize
Size m_unmaximizedSize
Definition: platformwindow.h:116
PlatformWindow::releaseAllKeys
void releaseAllKeys()
Definition: platformwindow.cpp:139
Fw::KeyShift
@ KeyShift
Definition: const.h:80
window
X11Window window
Definition: platformwindow.cpp:31
InputEvent::type
Fw::InputEventType type
Definition: inputevent.h:42
InputEvent::keyboardModifiers
int keyboardModifiers
Definition: inputevent.h:49
g_window
PlatformWindow & g_window
Definition: platformwindow.cpp:37
PlatformWindow::m_firstKeysPress
std::map< Fw::Key, ticks_t > m_firstKeysPress
Definition: platformwindow.h:109
PlatformWindow::m_onInputEvent
OnInputEventCallback m_onInputEvent
Definition: platformwindow.h:129
Fw::KeyAlt
@ KeyAlt
Definition: const.h:81
g_logger
Logger g_logger
Definition: logger.cpp:35
PlatformWindow::m_mouseButtonStates
stdext::boolean< false > m_mouseButtonStates[4]
Definition: platformwindow.h:119
PlatformWindow::processKeyDown
void processKeyDown(Fw::Key keyCode)
Definition: platformwindow.cpp:69
PlatformWindow::isMaximized
bool isMaximized()
Definition: platformwindow.h:89
image.h
Timer::restart
void restart()
Definition: timer.cpp:27
Fw::KeyboardAltModifier
@ KeyboardAltModifier
Definition: const.h:262
WIN32Window
Definition: win32window.h:41
PlatformWindow::m_size
Size m_size
Definition: platformwindow.h:113
InputEvent::keyCode
Fw::Key keyCode
Definition: inputevent.h:47
uchar
unsigned char uchar
Definition: types.h:29
stdext::shared_object_ptr
Definition: shared_object.h:39
PlatformWindow::loadMouseCursor
int loadMouseCursor(const std::string &file, const Point &hotSpot)
Definition: platformwindow.cpp:39
Fw::KeyNumpad0
@ KeyNumpad0
Definition: const.h:166
PlatformWindow::internalLoadMouseCursor
virtual int internalLoadMouseCursor(const ImagePtr &image, const Point &hotSpot)=0
Clock::millis
ticks_t millis()
Definition: clock.h:37
PlatformWindow::m_keysState
std::map< Fw::Key, stdext::boolean< false > > m_keysState
Definition: platformwindow.h:108
Fw::KeyboardCtrlModifier
@ KeyboardCtrlModifier
Definition: const.h:261
PlatformWindow::m_keyPressTimer
Timer m_keyPressTimer
Definition: platformwindow.h:111
TPoint< int >
Fw::KeyDownInputEvent
@ KeyDownInputEvent
Definition: const.h:237
win32window.h
PlatformWindow::processKeyUp
void processKeyUp(Fw::Key keyCode)
Definition: platformwindow.cpp:106
g_clock
Clock g_clock
Definition: clock.cpp:25
PlatformWindow::fireKeysPress
void fireKeysPress()
Definition: platformwindow.cpp:157
Fw::KeyCtrl
@ KeyCtrl
Definition: const.h:79
Fw
Definition: const.h:53
InputEvent::autoRepeatTicks
int autoRepeatTicks
Definition: inputevent.h:52
InputEvent::reset
void reset(Fw::InputEventType eventType=Fw::NoInputEvent)
Definition: inputevent.h:34
X11Window
Definition: x11window.h:38
Fw::KeyNumLock
@ KeyNumLock
Definition: const.h:76