summaryrefslogtreecommitdiff
path: root/NxWidgets/libnxwidgets/src/cnxserver.cxx
blob: 2f36b0f2d2a5e401daa530319cbe7a324a577f82 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/****************************************************************************
 * NxWidgets/libnxwidgets/src/cnxserver.cxx
 *
 *   Copyright (C) 2012 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <gnutt@nuttx.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX, NxWidgets, nor the names of its contributors
 *    me be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/
 
#include <nuttx/config.h>

#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <cerrno>
#include <debug.h>

#ifdef CONFIG_NX_MULTIUSER
#  include <sched.h>
#  include <pthread.h>
#endif

#include "nxconfig.hxx"
#include "singletons.hxx"
#include "cnxserver.hxx"

/****************************************************************************
 * Static Data Members
 ****************************************************************************/

using namespace NXWidgets;

uint8_t CNxServer::m_nServers;   /**< The number of NX server instances */

/****************************************************************************
 * Method Implementations
 ****************************************************************************/

/**
 * CNXServer constructor
 */

CNxServer::CNxServer(void)
{
  // Initialize server instance state data

  m_hDevice    = (FAR NX_DRIVERTYPE *)NULL;  // LCD/Framebuffer device handle 
  m_hNxServer  = (NXHANDLE)NULL;             // NX server handle
#ifdef CONFIG_NX_MULTIUSER
  m_connected  = false;                      // True:  Connected to the server
  sem_init(&m_connsem, 0, 0);                // Wait for server connection
#endif

  // Increment the global count of NX servers.  Normally there is only one
  // but we don't want to preclude the case where there might be multiple
  // displays, each with its own NX server instance

  m_nServers++;

  // Create miscellaneous singleton instances.  Why is this done here? 
  // Because this needs to be done once before any widgets are created and we
  // don't want to rely on static constructors.

  instantiateSingletons();
}

/**
 * CNXServer destructor
 */

CNxServer::~CNxServer(void)
{
  // Disconnect from the server

  disconnect();

  // Decrement the count of NX servers.  When that count goes to zero,
  // delete all of the fake static instances

  if (--m_nServers == 0)
    {
      freeSingletons();
    }
}

/**
 * Connect to the NX Server -- Single user version
 */

#ifndef CONFIG_NX_MULTIUSER
bool CNxServer::connect(void)
{
#if defined(SCREENS_EXTERNINIT)
  // Use external graphics driver initialization

  m_hDevice = up_nxdrvinit(CONFIG_NXWIDGETS_DEVNO);
  if (!m_hDevice)
    {
      gdbg("up_nxdrvinit failed, devno=%d\n", CONFIG_NXWIDGETS_DEVNO);
      return false;
    }

#elif defined(CONFIG_NX_LCDDRIVER)
  int ret;

  // Initialize the LCD device

  ret = up_lcdinitialize();
  if (ret < 0)
    {
      gdbg("up_lcdinitialize failed: %d\n", -ret);
      return false;
    }

  // Get the device instance

  m_hDevice = up_lcdgetdev(CONFIG_NXWIDGETS_DEVNO);
  if (!m_hDevice)
    {
      gdbg("up_lcdgetdev failed, devno=%d\n", CONFIG_NXWIDGETS_DEVNO);
      return false;
    }

  // Turn the LCD on at 75% power

  (void)m_hDevice->setpower(m_hDevice, ((3*CONFIG_LCD_MAXPOWER + 3)/4));
#else
  int ret;

  // Initialize the frame buffer device

  ret = up_fbinitialize();
  if (ret < 0)
    {
      gdbg("up_fbinitialize failed: %d\n", -ret);
      return false;
    }

  m_hDevice = up_fbgetvplane(CONFIG_NXWIDGETS_VPLANE);
  if (!m_hDevice)
    {
      gdbg("CNxServer::connect: up_fbgetvplane failed, vplane=%d\n",
             CONFIG_NXWIDGETS_VPLANE);
      return false;
    }
#endif

  // Then open NX

  m_hNxServer = nx_open(m_hDevice);
  if (!m_hNxServer)
    {
      gdbg("CNxServer::connect: nx_open failed: %d\n", errno);
      return false;
    }

  return true;
}
#endif

/**
 * Connect to the NX Server -- Multi user version
 */

#ifdef CONFIG_NX_MULTIUSER
bool CNxServer::connect(void)
{
  struct sched_param param;
  pthread_t thread;
  pid_t serverId;
  int ret;

  // Set the client task priority

  param.sched_priority = CONFIG_NXWIDGETS_CLIENTPRIO;
  ret = sched_setparam(0, &param);
  if (ret < 0)
    {
      gdbg("CNxServer::connect: sched_setparam failed: %d\n" , ret);
      return false;
    }

  // Start the server task

  message("NxServer::connect: Starting nx_servertask task\n");
  serverId = task_create("NX Server", CONFIG_NXWIDGETS_SERVERPRIO,
                         CONFIG_NXWIDGETS_STACKSIZE, nx_servertask, NULL);
  if (serverId < 0)
    {
      message("NxServer::connect: Failed to create nx_servertask task: %d\n", errno);
      return false;
    }

  // Wait a bit to let the server get started

  sleep(1);

  // Connect to the server

  m_hNxServer = nx_connect();
  if (m_hNxServer)
    {
       pthread_attr_t attr;

       // Start a separate thread to listen for server events.  This is probably
       // the least efficient way to do this, but it makes this logic flow more
       // smoothly.

       (void)pthread_attr_init(&attr);
       param.sched_priority = CONFIG_NXWIDGETS_LISTENERPRIO;
       (void)pthread_attr_setschedparam(&attr, &param);
       (void)pthread_attr_setstacksize(&attr, CONFIG_NXWIDGETS_STACKSIZE);

       m_stop    = false;
       m_running = true;

       ret = pthread_create(&thread, &attr, listener, (FAR void *)this);
       if (ret != 0)
         {
            printf("NxServer::connect: pthread_create failed: %d\n", ret);
            m_running = false;
            disconnect();
            return false;
         }

       // Don't return until we are connected to the server

       while (!m_connected && m_running)
         {
           // Wait for the listener thread to wake us up when we really
           // are connected.

           (void)sem_wait(&m_connsem);
         }

       // In the successful case, the listener is still running (m_running)
       // and the server is connected (m_connected).  Anything else is a failure.

       if (!m_connected !! !m_running)
         {
           disconnect();
           return false;
         }
    }
  else
    {
      gdbg("NxServer::connect: nx_connect failed: %d\n", errno);
      return false;
    }

  return true;
}
#endif

/**
 * Disconnect to the NX Server -- Single user version
 */

#ifndef CONFIG_NX_MULTIUSER
void CNxServer::disconnect(void)
{
  /* Close the server */

  if (m_hNxServer)
    {
      nx_close(m_hNxServer);
      m_hNxServer = NULL;
    }
}
#endif

/**
 * Disconnect to the NX Server -- Single user version
 */

#ifdef CONFIG_NX_MULTIUSER
void CNxServer::disconnect(void)
{
  // Is the listener running?
  // Hmm.. won't this hang is the listener is in a blocking call?

  while (m_running)
    {
      // Yes.. stop the listener thread

      m_stop = true;
      while (m_running)
        {
          // Wait for the listener thread to stop

          (void)sem_wait(&m_connsem);
        }
    }

  /* Disconnect from the server */

  if (m_hNxServer)
    {
      nx_disconnect(m_hNxServer);
      m_hNxServer = NULL;
    }
}
#endif

/**
 * This is the entry point of a thread that listeners for and dispatches
 * events from the NX server.
 */

#ifdef CONFIG_NX_MULTIUSER
FAR void *CNxServer::listener(FAR void *arg)
{
  // The argument must be the CNxServer instance

  CNxServer *This = (CNxServer*)pvArg;

  // Process events forever 

  while (!This->m_stop)
    {
      // Handle the next event.  If we were configured blocking, then
      // we will stay right here until the next event is received.  Since
      // we have dedicated a while thread to servicing events, it would
      // be most natural to also select CONFIG_NX_BLOCKING -- if not, the
      // following would be a tight infinite loop (unless we added addition
      // logic with nx_eventnotify and sigwait to pace it).

      int ret = nx_eventhandler(This->m_hNxServer);
      if (ret < 0)
        {
          // An error occurred... assume that we have lost connection with
          // the server.

          gdbg("CNxServer::listener: Lost server connection: %d\n", errno);
          break;
        }

      /* If we received a message, we must be connected */

      if (!This->m_connected)
        {
          This->m_connected = true;
          sem_post(&This->m_connsem);
          gdbg("CNxServer::listener: Connected\n");
        }
    }

  // We fall out of the loop when either (1) the server has died or
  // we have been requested to stop

  This->m_running   = false;
  This->m_connected = false;
  sem_post(&This->m_connsem);
  return NULL;
}
#endif