GLX/GteWindow.cpp
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.2 (2016/11/14)
7 
8 #include <GTEnginePCH.h>
10 #include <X11/Xlib.h>
11 
12 namespace gte
13 {
14 
16  :
17  display(nullptr),
18  window(0),
19  deviceCreationFlags(0)
20 {
21 }
22 
23 Window::Parameters::Parameters(std::wstring const& inTitle,
24  int inXOrigin, int inYOrigin, int inXSize, int inYSize)
25  :
26  WindowBase::Parameters(inTitle, inXOrigin, inYOrigin, inXSize, inYSize),
27  display(nullptr),
28  window(0),
30 {
31 }
32 
34 {
35 }
36 
38  :
39  WindowBase(parameters),
40  mDisplay(parameters.display),
41  mWindow(parameters.window),
42  mButtonDown{false, false, false, false, false, false, false, false},
43  mShiftDown(false),
44  mControlDown(false),
45  mAltDown(false),
46  mCommandDown(false),
47  mEngine(std::static_pointer_cast<GraphicsEngine>(mBaseEngine))
48 {
49 }
50 
52 {
53  XMapWindow(mDisplay, mWindow);
54 }
55 
57 {
58  XWarpPointer(mDisplay, 0, mWindow, 0, 0, 0, 0, x, y);
59  XFlush(mDisplay);
60 }
61 
62 void Window::GetMousePosition(int& x, int& y) const
63 {
64  XID rootWindow, childWindow;
65  int rootX, rootY;
66  unsigned int modifier;
67  XQueryPointer(mDisplay, mWindow, &rootWindow, &childWindow,
68  &rootX, &rootY, &x, &y, &modifier);
69 }
70 
72 {
73  XDestroyWindow(mDisplay, mWindow);
74 }
75 
77 {
78  if (!XPending(mDisplay))
79  {
80  return EVT_NONE_PENDING;
81  }
82 
83  XEvent evt;
84  XNextEvent(mDisplay, &evt);
85  int index;
86  bool state;
87 
88  if (evt.type == ButtonPress || evt.type == ButtonRelease)
89  {
90  OnMouseClick(evt.xbutton.button, evt.xbutton.type,
91  evt.xbutton.x, evt.xbutton.y, evt.xbutton.state);
92 
93  mButtonDown[evt.xbutton.button] = (evt.type == ButtonPress);
94  return EVT_PROCESSED;
95  }
96 
97  if (evt.type == MotionNotify)
98  {
99  int button = MOUSE_NONE;
100  for (int i = MOUSE_LEFT; i <= MOUSE_RIGHT; ++i)
101  {
102  if (mButtonDown[i])
103  {
104  button = i;
105  break;
106  }
107  }
108  OnMouseMotion(button, evt.xmotion.x, evt.xmotion.y, evt.xmotion.state);
109  return EVT_PROCESSED;
110  }
111 
112  if (evt.type == KeyPress || evt.type == KeyRelease)
113  {
114  int keysyms_per_keycode_return;
115  KeySym* pKeySym = XGetKeyboardMapping(mDisplay,
116  evt.xkey.keycode, 1, &keysyms_per_keycode_return);
117  KeySym& keySym = *pKeySym;
118  int key = (keySym & 0x00FF);
119 
120  // Quit application if the KEY_ESCAPE key is pressed.
121  if (key == KEY_ESCAPE)
122  {
123  XFree(pKeySym);
124  return EVT_QUIT;
125  }
126 
127  // Adjust for special keys from the key pad or the number pad.
128  if ((keySym & 0xFF00) != 0)
129  {
130  if (0x50 <= key && key <= 0x57)
131  {
132  // keypad Home, {L,U,R,D}Arrow, Pg{Up,Dn}, End
133  key += 0x45;
134  }
135  else if (key == 0x63)
136  {
137  // keypad Insert
138  key = 0x9e;
139  }
140  else if (key == 0xFF)
141  {
142  // keypad Delete
143  key = 0x9f;
144  }
145  else if (key == 0xE1 || key == 0xE2)
146  {
147  // L-shift or R-shift
148  key = KEY_SHIFT;
149  mShiftDown = (evt.type == KeyPress);
150  }
151  else if (key == 0xE3 || key == 0xE4)
152  {
153  // L-ctrl or R-ctrl
154  key = KEY_CONTROL;
155  mControlDown = (evt.type == KeyPress);
156  }
157  else if (key == 0xE9 || key == 0xEA)
158  {
159  // L-alt or R-alt
160  key = KEY_ALT;
161  mAltDown = (evt.type == KeyPress);
162  }
163  else if (key == 0xEB || key == 0xEC)
164  {
165  key = KEY_COMMAND;
166  mCommandDown = (evt.type == KeyPress);
167  }
168  }
169 
170  if ((KEY_HOME <= key && key <= KEY_END)
171  || (KEY_F1 <= key && key <= KEY_F12)
172  || (KEY_SHIFT <= key && key <= KEY_COMMAND))
173  {
174  if (evt.type == KeyPress)
175  {
176  OnKeyDown(key, evt.xbutton.x, evt.xbutton.y);
177  }
178  else
179  {
180  OnKeyUp(key, evt.xbutton.x, evt.xbutton.y);
181  }
182  }
183  else
184  {
185  if (evt.type == KeyPress)
186  {
187  // Get key-modifier state. Adjust for shift state.
188  unsigned char ucKey = static_cast<unsigned char>(key);
189  if (mShiftDown && 'a' <= ucKey && ucKey <= 'z')
190  {
191  ucKey = static_cast<unsigned char>(key - 32);
192  }
193  OnCharPress(ucKey, evt.xbutton.x, evt.xbutton.y);
194  }
195  }
196  XFree(pKeySym);
197  return EVT_PROCESSED;
198  }
199 
200  if (evt.type == Expose)
201  {
202  OnDisplay();
203  return EVT_PROCESSED;
204  }
205 
206  if (evt.type == ConfigureNotify)
207  {
208  OnMove(evt.xconfigure.x, evt.xconfigure.y);
209  OnResize(evt.xconfigure.width, evt.xconfigure.height);
210  return EVT_PROCESSED;
211  }
212 
213  if (evt.type == ClientMessage)
214  {
215  Atom* wmDelete = nullptr;
216  int count;
217  if (XGetWMProtocols(mDisplay, mWindow, &wmDelete, &count))
218  {
219  if (evt.xclient.data.l[0] == *wmDelete)
220  {
221  return EVT_QUIT;
222  }
223  }
224  }
225 
226  return EVT_NONE_PENDING;
227 }
228 
229 int const WindowBase::KEY_ESCAPE = 0x1B;
230 int const WindowBase::KEY_HOME = 0x95;
231 int const WindowBase::KEY_LEFT = 0x96;
232 int const WindowBase::KEY_UP = 0x97;
233 int const WindowBase::KEY_RIGHT = 0x98;
234 int const WindowBase::KEY_DOWN = 0x99;
235 int const WindowBase::KEY_PAGE_UP = 0x9A;
236 int const WindowBase::KEY_PAGE_DOWN = 0x9B;
237 int const WindowBase::KEY_END = 0x9C;
238 int const WindowBase::KEY_INSERT = 0x9E;
239 int const WindowBase::KEY_DELETE = 0x9F;
240 int const WindowBase::KEY_F1 = 0xBE;
241 int const WindowBase::KEY_F2 = 0xBF;
242 int const WindowBase::KEY_F3 = 0xC0;
243 int const WindowBase::KEY_F4 = 0xC1;
244 int const WindowBase::KEY_F5 = 0xC2;
245 int const WindowBase::KEY_F6 = 0xC3;
246 int const WindowBase::KEY_F7 = 0xC4;
247 int const WindowBase::KEY_F8 = 0xC5;
248 int const WindowBase::KEY_F9 = 0xC6;
249 int const WindowBase::KEY_F10 = 0xC7;
250 int const WindowBase::KEY_F11 = 0xC8;
251 int const WindowBase::KEY_F12 = 0xC9;
252 int const WindowBase::KEY_BACKSPACE = 0x08;
253 int const WindowBase::KEY_TAB = 0x09;
254 int const WindowBase::KEY_ENTER = 0x0D;
255 int const WindowBase::KEY_RETURN = 0x0D;
256 
257 int const WindowBase::KEY_SHIFT = 0xE1; // L-shift
258 int const WindowBase::KEY_CONTROL = 0xE3; // L-ctrl
259 int const WindowBase::KEY_ALT = 0xE9; // L-alt
260 int const WindowBase::KEY_COMMAND = 0xEB; // L-command
261 
262 int const WindowBase::MOUSE_NONE = 0x0000;
263 int const WindowBase::MOUSE_LEFT = 0x0001;
264 int const WindowBase::MOUSE_MIDDLE = 0x0002;
265 int const WindowBase::MOUSE_RIGHT = 0x0003;
266 int const WindowBase::MOUSE_DOWN = 0x0004;
267 int const WindowBase::MOUSE_UP = 0x0005;
268 
269 int const WindowBase::MODIFIER_CONTROL = 0x0004;
270 int const WindowBase::MODIFIER_LBUTTON = 0x0001;
271 int const WindowBase::MODIFIER_MBUTTON = 0x0002;
272 int const WindowBase::MODIFIER_RBUTTON = 0x0003;
273 int const WindowBase::MODIFIER_SHIFT = 0x0001;
274 
275 }
static int const KEY_LEFT
Definition: GteWindowBase.h:90
virtual void OnMove(int x, int y)
std::array< bool, 8 > mButtonDown
Definition: GLX/GteWindow.h:64
virtual bool OnMouseClick(int button, int state, int x, int y, unsigned int modifiers)
static int const MODIFIER_SHIFT
bool mControlDown
Definition: GLX/GteWindow.h:66
static int const MOUSE_MIDDLE
static int const KEY_F1
virtual bool OnKeyDown(int key, int x, int y)
static int const MODIFIER_RBUTTON
virtual void OnClose() override
static int const KEY_COMMAND
static int const KEY_F10
static int const KEY_ALT
static int const KEY_PAGE_UP
Definition: GteWindowBase.h:96
static int const MODIFIER_LBUTTON
static int const KEY_HOME
Definition: GteWindowBase.h:94
static int const KEY_RETURN
virtual void GetMousePosition(int &x, int &y) const override
static int const KEY_F7
Window(Parameters &parameters)
static int const MOUSE_RIGHT
GLint GLenum GLint x
Definition: glcorearb.h:404
static int const KEY_TAB
static int const MOUSE_UP
static int const KEY_F3
static int const MODIFIER_CONTROL
static int const KEY_F11
static int const KEY_F4
static int const KEY_BACKSPACE
static int const MOUSE_DOWN
static int const KEY_DELETE
Definition: GteWindowBase.h:99
_XDisplay * mDisplay
Definition: GLX/GteWindow.h:62
static int const MOUSE_NONE
int ProcessedEvent()
static int const KEY_ENTER
static int const KEY_F5
unsigned int deviceCreationFlags
Definition: GLX/GteWindow.h:31
GLint GLsizei count
Definition: glcorearb.h:400
static int const KEY_F12
static int const KEY_INSERT
Definition: GteWindowBase.h:98
static int const KEY_PAGE_DOWN
Definition: GteWindowBase.h:97
virtual ~Window()
static int const KEY_F6
static int const MOUSE_LEFT
static int const KEY_RIGHT
Definition: GteWindowBase.h:91
std::shared_ptr< GraphicsEngine > mEngine
Definition: GLX/GteWindow.h:69
virtual bool OnKeyUp(int key, int x, int y)
bool mCommandDown
Definition: GLX/GteWindow.h:68
virtual bool OnResize(int xSize, int ySize)
static int const KEY_END
Definition: GteWindowBase.h:95
virtual void SetMousePosition(int x, int y) override
static int const KEY_ESCAPE
Definition: GteWindowBase.h:89
static int const KEY_SHIFT
GLuint index
Definition: glcorearb.h:781
virtual bool OnMouseMotion(int button, int x, int y, unsigned int modifiers)
static int const KEY_UP
Definition: GteWindowBase.h:92
static int const KEY_DOWN
Definition: GteWindowBase.h:93
virtual void OnDisplay()
static int const KEY_F8
static int const KEY_CONTROL
std::shared_ptr< BaseEngine > mBaseEngine
unsigned long mWindow
Definition: GLX/GteWindow.h:63
static int const KEY_F2
static int const MODIFIER_MBUTTON
GLint y
Definition: glcorearb.h:98
virtual bool OnCharPress(unsigned char key, int x, int y)
void ShowWindow()
static int const KEY_F9


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:02