NXWidgets  1.19
cwindowmessenger.cxx
Go to the documentation of this file.
1 /********************************************************************************************
2  * NxWidgets/nxwm/src/cwindowmessenger.cxx
3  *
4  * Copyright (C) 2012-2013 Gregory Nutt. All rights reserved.
5  * Author: Gregory Nutt <gnutt@nuttx.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  * 3. Neither the name NuttX, NxWidgets, nor the names of its contributors
18  * me be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  ********************************************************************************************/
35 
36 /********************************************************************************************
37  * Included Files
38  ********************************************************************************************/
39 
40 #include <nuttx/config.h>
41 
42 #include <cfcntl>
43 #include <cerrno>
44 
45 #include <debug.h>
46 
47 #include "nxwmconfig.hxx"
48 #include "cstartwindow.hxx"
49 #include "cwindowmessenger.hxx"
50 
51 /********************************************************************************************
52  * Pre-Processor Definitions
53  ********************************************************************************************/
54 
55 /********************************************************************************************
56  * CWindowMessenger Method Implementations
57  ********************************************************************************************/
58 
59 using namespace NxWM;
60 
61 /**
62  * CWindowMessenger Constructor
63  *
64  * @param style The default style that all widgets on this display
65  * should use. If this is not specified, the widget will use the
66  * values stored in the defaultCWidgetStyle object.
67  */
68 
70 : NXWidgets::CWidgetControl(style)
71 {
72  // Add ourself to the list of window event handlers
73 
75 }
76 
77 /**
78  * CWindowMessenger Destructor.
79  */
80 
82 {
83  // Remove ourself from the list of the window event handlers
84 
86 }
87 
88 /**
89  * Handle an NX window mouse input event.
90  *
91  * @param e The event data.
92  */
93 
94 #ifdef CONFIG_NX_XYINPUT
96 {
97  // The logic path here is tortuous but flexible:
98  //
99  // 1. A listener thread receives mouse or touchscreen input and injects
100  // that into NX via nx_mousein
101  // 2. In the multi-user mode, this will send a message to the NX server
102  // 3. The NX server will determine which window gets the mouse input
103  // and send a window event message to the NX listener thread.
104  // 4. The NX listener thread receives a windows event. The NX listener thread
105  // is part of CTaskBar and was created when NX server connection was
106  // established. This event may be a positional change notification, a
107  // redraw request, or mouse or keyboard input. In this case, mouse input.
108  // 5. The NX listener thread handles the message by calling nx_eventhandler().
109  // nx_eventhandler() dispatches the message by calling a method in the
110  // NXWidgets::CCallback instance associated with the window.
111  // NXWidgets::CCallback is a part of the CWidgetControl.
112  // 6. NXWidgets::CCallback calls into NXWidgets::CWidgetControl to process
113  // the event.
114  // 7. NXWidgets::CWidgetControl records the new state data and raises a
115  // window event.
116  // 8. NXWidgets::CWindowEventHandlerList will give the event to this method
117  // NxWM::CWindowMessenger.
118  // 9. This NxWM::CWindowMessenger method will schedule an entry on the work
119  // queue.
120  // 10. The work queue callback will finally call pollEvents() to execute whatever
121  // actions the input event should trigger.
122 
123  work_state_t *state = new work_state_t;
124  state->windowMessenger = this;
125  int ret = work_queue(USRWORK, &state->work, &inputWorkCallback, state, 0);
126  if (ret < 0)
127  {
128  gerr("ERROR: work_queue failed: %d\n", ret);
129  }
130 }
131 #endif
132 
133 /**
134  * Handle a NX window keyboard input event.
135  */
136 
137 #ifdef CONFIG_NX_KBD
139 {
140  work_state_t *state = new work_state_t;
141  state->windowMessenger = this;
142 
143  int ret = work_queue(USRWORK, &state->work, &inputWorkCallback, state, 0);
144  if (ret < 0)
145  {
146  gerr("ERROR: work_queue failed: %d\n", ret);
147  }
148 }
149 #endif
150 
151 /**
152  * Handle a NX window blocked event. This handler is called when we
153  * receive the BLOCKED message meaning that there are no further pending
154  * actions on the window. It is now safe to delete the window.
155  *
156  * This is handled by sending a message to the start window thread (vs just
157  * calling the destructors) because in the case where an application
158  * destroys itself (because of pressing the stop button), then we need to
159  * unwind and get out of the application logic before destroying all of its
160  * objects.
161  *
162  * @param arg - User provided argument (see nx_block or nxtk_block)
163  */
164 
166 {
167  // Send a message to destroy the window instance.
168 
169  work_state_t *state = new work_state_t;
170  state->windowMessenger = this;
171  state->instance = arg;
172 
173  int ret = work_queue(USRWORK, &state->work, &destroyWorkCallback, state, 0);
174  if (ret < 0)
175  {
176  gerr("ERROR: work_queue failed: %d\n", ret);
177  }
178 }
179 
180 /** Work queue callback functions */
181 
183 {
184  work_state_t *state = (work_state_t*)arg;
185  state->windowMessenger->pollEvents();
186  delete state;
187 }
188 
190 {
191  work_state_t *state = (work_state_t*)arg;
192 
193  // First make sure any pending input events have been handled.
194 
195  state->windowMessenger->pollEvents();
196 
197  // Then release the memory.
198 
199  ginfo("Deleting app=%p\n", state->instance);
200  IApplication *app = (IApplication *)state->instance;
201  delete app;
202  delete state;
203 }
void removeWindowEventHandler(CWindowEventHandler *eventHandler)
static void inputWorkCallback(FAR void *arg)
bool pollEvents(CNxWidget *widget=(CNxWidget *) NULL)
static void destroyWorkCallback(FAR void *arg)
CWindowMessenger(FAR const NXWidgets::CWidgetStyle *style=(const NXWidgets::CWidgetStyle *) NULL)
void handleBlockedEvent(FAR void *arg)
void addWindowEventHandler(CWindowEventHandler *eventHandler)