MRPT  1.9.9
CBaseGUIWindow.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "gui-precomp.h" // Precompiled headers
11 
13 #include <mrpt/gui/WxSubsystem.h>
14 #include <mrpt/system/os.h>
15 #include <iostream>
16 
17 using namespace mrpt;
18 using namespace mrpt::gui;
19 using namespace mrpt::system;
20 using namespace std;
21 
22 /*---------------------------------------------------------------
23  Ctor
24  ---------------------------------------------------------------*/
26  void* winobj_voidptr, int CMD_CREATE_WIN, int CMD_DESTROY_WIN,
27  const std::string& initial_caption)
28  : m_CMD_CREATE_WIN(CMD_CREATE_WIN),
29  m_CMD_DESTROY_WIN(CMD_DESTROY_WIN),
30  m_winobj_voidptr(winobj_voidptr),
31  m_caption(initial_caption),
32  m_hwnd(nullptr),
33  m_keyPushed(false),
34  m_keyPushedCode(0),
35  m_keyPushedModifier(MRPTKMOD_NONE)
36 {
37 }
38 
39 /*---------------------------------------------------------------
40  Create the wx Window
41  ---------------------------------------------------------------*/
43  unsigned int initialWidth, unsigned int initialHeight)
44 {
45  MRPT_UNUSED_PARAM(initialWidth);
46  MRPT_UNUSED_PARAM(initialHeight);
48 #if MRPT_HAS_WXWIDGETS
49  // Create the main wxThread:
50  // -------------------------------
51  if (!WxSubsystem::createOneInstanceMainThread()) return; // Error!
52 
53  // Create window:
54  auto* REQ = new WxSubsystem::TRequestToWxMainThread[1];
55  REQ->source2D = static_cast<gui::CDisplayWindow*>(m_winobj_voidptr);
56  REQ->source3D = static_cast<gui::CDisplayWindow3D*>(m_winobj_voidptr);
57  REQ->sourcePlots = static_cast<gui::CDisplayWindowPlots*>(m_winobj_voidptr);
58  REQ->str = m_caption;
59  REQ->OPCODE = m_CMD_CREATE_WIN;
60  REQ->voidPtr = m_hwnd.getPtrToPtr();
61  REQ->x = initialWidth;
62  REQ->y = initialHeight;
63 
65 
66  // Wait for the window to realize and signal it's alive:
68  {
69  std::this_thread::sleep_for(
70  20ms); // Force at least 1-2 timer ticks for processing the event:
71  wxApp::GetInstance()->Yield(true);
72  }
73  int maxTimeout =
74 #ifdef _DEBUG
75  30000;
76 #else
77  6000;
78 #endif
79  // If we have an "MRPT_WXSUBSYS_TIMEOUT_MS" environment variable, use that
80  // timeout instead:
81  const char* envVal = getenv("MRPT_WXSUBSYS_TIMEOUT_MS");
82  if (envVal) maxTimeout = atoi(envVal);
83 
84  auto future = m_threadReady.get_future();
85  if (future.wait_for(std::chrono::milliseconds(maxTimeout)) ==
86  std::future_status::timeout) // 2 secs should be enough...
87  {
88  cerr << "[CBaseGUIWindow::ctor] Timeout waiting window creation."
89  << endl;
90  }
91 #else
92  THROW_EXCEPTION("MRPT compiled without wxWidgets!");
93 #endif
94  MRPT_END
95 }
96 
97 /*---------------------------------------------------------------
98  Dtor
99  ---------------------------------------------------------------*/
101 /*---------------------------------------------------------------
102  destroyWxWindow
103  ---------------------------------------------------------------*/
105 {
106  MRPT_START
107 #if MRPT_HAS_WXWIDGETS
108  // Send close request:
109  if (m_hwnd.get())
110  {
111  auto* REQ = new WxSubsystem::TRequestToWxMainThread[1];
112  REQ->OPCODE = m_CMD_DESTROY_WIN;
113  REQ->source2D = static_cast<gui::CDisplayWindow*>(m_winobj_voidptr);
114  REQ->source3D = static_cast<gui::CDisplayWindow3D*>(m_winobj_voidptr);
115  REQ->sourcePlots =
117 
119 
120  // Wait until the thread ends:
122  {
123  std::this_thread::sleep_for(20ms); // Force at least 1-2 timer
124  // ticks for processing the
125  // event:
126  wxApp::GetInstance()->Yield(true);
127  }
128  const int maxTimeout =
129 #ifdef _DEBUG
130  30000;
131 #else
132  6000;
133 #endif
134  if (m_windowDestroyed.get_future().wait_for(std::chrono::milliseconds(
135  maxTimeout)) == std::future_status::timeout)
136  {
137  cerr << "[CBaseGUIWindow::dtor] Timeout waiting window destruction."
138  << endl;
139  }
140  }
142 #endif
143  MRPT_END
144 }
145 
146 /*---------------------------------------------------------------
147  notifyChildWindowDestruction
148  ---------------------------------------------------------------*/
150 /*---------------------------------------------------------------
151  waitForKey
152  ---------------------------------------------------------------*/
154  bool ignoreControlKeys, mrptKeyModifier* out_pushModifier)
155 {
156  int k = 0;
157  if (out_pushModifier) *out_pushModifier = MRPTKMOD_NONE;
158  m_keyPushed = false;
159 
160  for (;;)
161  {
162  if (os::kbhit())
163  {
164  k = os::getch();
165  return k;
166  }
167  if (m_keyPushed)
168  {
169  k = m_keyPushedCode;
170  m_keyPushed = false;
171  if (m_keyPushedCode < 256 || !ignoreControlKeys)
172  {
173  if (out_pushModifier) *out_pushModifier = m_keyPushedModifier;
174  return k;
175  }
176  // Ignore and keep waiting
177  }
178  std::this_thread::sleep_for(10ms);
179  // Are we still alive?
180  if (!isOpen()) return 0;
181  }
182 }
183 
184 /*---------------------------------------------------------------
185  getPushedKey
186  ---------------------------------------------------------------*/
188 {
189  if (out_pushModifier) *out_pushModifier = MRPTKMOD_NONE;
190 
191  if (!m_keyPushed) return 0;
192 
193  int k = m_keyPushedCode;
194  m_keyPushed = false;
195  if (out_pushModifier) *out_pushModifier = m_keyPushedModifier;
196  return k;
197 }
198 
199 /*---------------------------------------------------------------
200  isOpen
201  ---------------------------------------------------------------*/
202 bool CBaseGUIWindow::isOpen() { return m_hwnd != nullptr; }
203 /*---------------------------------------------------------------
204  notifySemThreadReady
205  ---------------------------------------------------------------*/
static void pushPendingWxRequest(TRequestToWxMainThread *data)
Thread-safe method to insert a new pending request (The memory must be dinamically allocated with "ne...
const int m_CMD_CREATE_WIN
can be 200,300,400...
std::atomic_int m_keyPushedCode
#define MRPT_START
Definition: exceptions.h:241
int getPushedKey(mrptKeyModifier *out_pushModifier=nullptr)
Returns the latest pushed key, or 0 if there is no new key stroke.
The data structure for each inter-thread request:
Definition: WxSubsystem.h:189
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
Create a GUI window and display plots with MATLAB-like interfaces and commands.
int getch() noexcept
An OS-independent version of getch, which waits until a key is pushed.
Definition: os.cpp:372
mrpt::void_ptr_noncopy m_hwnd
The window handle.
std::string m_caption
The caption of the window.
int waitForKey(bool ignoreControlKeys=true, mrptKeyModifier *out_pushModifier=nullptr)
Waits for any key to be pushed on the image or the console, and returns the key code.
CBaseGUIWindow(void *winobj_voidptr, int CMD_CREATE_WIN, int CMD_DESTROY_WIN, const std::string &initial_caption=std::string())
CMD_DESTROY_WIN can be 299,399,499...
mrptKeyModifier
Definition: keycodes.h:156
STL namespace.
int OPCODE
Valid codes are: For CDisplayWindow:
Definition: WxSubsystem.h:283
void notifyChildWindowDestruction()
Called by wx main thread to set m_hwnd to NULL.
This class creates a window as a graphical user interface (GUI) for displaying images to the user...
bool isOpen()
Returns false if the user has already closed the window.
std::atomic< mrptKeyModifier > m_keyPushedModifier
const int m_CMD_DESTROY_WIN
can be 299,399,499...
std::promise< void > m_threadReady
This semaphore will be signaled when the wx window is built and ready.
static void waitWxShutdownsIfNoWindows()
This method must be called in the destructor of the user class FROM THE MAIN THREAD, in order to wait for the shutdown of the wx thread if this was the last open window.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::atomic_bool m_keyPushed
#define MRPT_END
Definition: exceptions.h:245
bool kbhit() noexcept
An OS-independent version of kbhit, which returns true if a key has been pushed.
Definition: os.cpp:394
void destroyWxWindow()
Must be called by child classes in their destructors.
static bool isConsoleApp()
Will be set to true at runtime if it&#39;s not detected a running wxApp instance.
Definition: WxSubsystem.cpp:53
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
void createWxWindow(unsigned int initialWidth, unsigned int initialHeight)
Must be called by child classes just within the constructor.
mrpt::gui::CDisplayWindow * source2D
Only one of source* can be non-nullptr, indicating the class that generated the request.
Definition: WxSubsystem.h:195
void notifySemThreadReady()
Called by wx main thread to signal the semaphore that the wx window is built and ready.
std::promise< void > m_windowDestroyed
This semaphore will be signaled when the wx window is destroyed.
static bool createOneInstanceMainThread()
Thread-safe method to create one single instance of the main wxWidgets thread: it will create the thr...
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 3a26b90fd Wed Mar 25 20:17:03 2020 +0100 at miƩ mar 25 23:05:41 CET 2020