Skip to content
Snippets Groups Projects
nx_main.c 22.9 KiB
Newer Older
patacongo's avatar
patacongo committed
/****************************************************************************
 * examples/nx/nx_main.c
 *
 *   Copyright (C) 2008-2010 Gregory Nutt. All rights reserved.
patacongo's avatar
patacongo committed
 *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
 *
 * 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 nor the names of its contributors may 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>
patacongo's avatar
patacongo committed
#include <stdio.h>
patacongo's avatar
patacongo committed
#include <stdlib.h>
patacongo's avatar
patacongo committed
#include <unistd.h>
patacongo's avatar
patacongo committed
#include <string.h>
patacongo's avatar
patacongo committed
#include <sched.h>
#include <pthread.h>
patacongo's avatar
patacongo committed
#include <errno.h>
#include <debug.h>

#ifdef CONFIG_NX_LCDDRIVER
#  include <nuttx/lcd.h>
#else
#  include <nuttx/fb.h>
#endif

patacongo's avatar
patacongo committed
#include <nuttx/arch.h>
#include <nuttx/nx.h>
#include <nuttx/nxtk.h>
patacongo's avatar
patacongo committed
#include <nuttx/nxfonts.h>

patacongo's avatar
patacongo committed
#include "nx_internal.h"

/****************************************************************************
 * Pre-processor Definitions
patacongo's avatar
patacongo committed
 ****************************************************************************/

/* Configuration ************************************************************/
/* If not specified, assume that the hardware supports one video plane */

#ifndef CONFIG_EXAMPLES_NX_VPLANE
#  define CONFIG_EXAMPLES_NX_VPLANE 0
#endif

/* If not specified, assume that the hardware supports one LCD device */

#ifndef CONFIG_EXAMPLES_NX_DEVNO
#  define CONFIG_EXAMPLES_NX_DEVNO 0
#endif

patacongo's avatar
patacongo committed
/****************************************************************************
 * Private Types
 ****************************************************************************/

/****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/****************************************************************************
 * Private Data
 ****************************************************************************/

static int g_exitcode = NXEXIT_SUCCESS;
patacongo's avatar
patacongo committed
static struct nxeg_state_s g_wstate[2];

patacongo's avatar
patacongo committed
#ifdef CONFIG_NX_KBD
static const uint8_t g_kbdmsg1[] = "NuttX is cool!";
static const uint8_t g_kbdmsg2[] = "NuttX is fun!";
patacongo's avatar
patacongo committed
#endif

/****************************************************************************
 * Public Data
 ****************************************************************************/
patacongo's avatar
patacongo committed

patacongo's avatar
patacongo committed
/* The connecton handler */

NXHANDLE g_hnx = NULL;

/* The screen resolution */
patacongo's avatar
patacongo committed

nxgl_coord_t g_xres;
nxgl_coord_t g_yres;
patacongo's avatar
patacongo committed

patacongo's avatar
patacongo committed
#ifdef CONFIG_NX_MULTIUSER
patacongo's avatar
patacongo committed
#endif
sem_t g_semevent = {0};
patacongo's avatar
patacongo committed
/* Colors used to fill window 1 & 2 */

nxgl_mxpixel_t g_color1[CONFIG_NX_NPLANES];
nxgl_mxpixel_t g_color2[CONFIG_NX_NPLANES];
#ifndef CONFIG_EXAMPLES_NX_RAWWINDOWS
nxgl_mxpixel_t g_tbcolor[CONFIG_NX_NPLANES];
#endif
patacongo's avatar
patacongo committed
/****************************************************************************
 * Private Functions
 ****************************************************************************/

patacongo's avatar
patacongo committed
/****************************************************************************
 * Name: nxeg_drivemouse
 ****************************************************************************/

#ifdef CONFIG_NX_MOUSE
static void nxeg_drivemouse(void)
{
  nxgl_coord_t x;
  nxgl_coord_t y;
  nxgl_coord_t xstep = g_xres / 8;
  nxgl_coord_t ystep = g_yres / 8;

  for (x = 0; x < g_xres; x += xstep)
    {
      for (y = 0; y < g_yres; y += ystep)
        {
          message("nxeg_drivemouse: Mouse left button at (%d,%d)\n", x, y);
          (void)nx_mousein(g_hnx, x, y, NX_MOUSE_LEFTBUTTON);
        }
    }
patacongo's avatar
patacongo committed
}
#endif

patacongo's avatar
patacongo committed
/****************************************************************************
 * Name: nxeg_initstate
 ****************************************************************************/

static void nxeg_initstate(FAR struct nxeg_state_s *st, int wnum,
                           nxgl_mxpixel_t color)
{
#ifdef CONFIG_NX_KBD
patacongo's avatar
patacongo committed
  FAR const struct nx_font_s *fontset;
#endif

  /* Initialize the window number (used for debug output only) and color
   * (used for redrawing the window)
   */

  st->wnum     = wnum;
  st->color[0] = color;

  /* Get information about the font set being used and save this in the
   * state structure
   */

#ifdef CONFIG_NX_KBD
patacongo's avatar
patacongo committed
  fontset      = nxf_getfontset();
  st->nchars   = 0;
  st->nglyphs  = 0;
  st->height   = fontset->mxheight;
  st->width    = fontset->mxwidth;
  st->spwidth  = fontset->spwidth;
patacongo's avatar
patacongo committed
#endif
}

/****************************************************************************
 * Name: nxeg_freestate
 ****************************************************************************/

#ifndef CONFIG_EXAMPLES_NX_RAWWINDOWS
static void nxeg_freestate(FAR struct nxeg_state_s *st)
{
#ifdef CONFIG_NX_KBD
  int i;

  if (st)
    {
      for (i = 0; i < st->nglyphs; i++)
        {
           if (st->glyph[i].bitmap)
              {
                free(st->glyph[i].bitmap);
              }
           st->glyph[i].bitmap = NULL;
        }
      st->nchars = 0;
     }
#endif
}
#endif

/****************************************************************************
 * Name: nxeg_openwindow
 ****************************************************************************/

#ifdef CONFIG_EXAMPLES_NX_RAWWINDOWS
patacongo's avatar
patacongo committed
static inline NXEGWINDOW nxeg_openwindow(FAR const struct nx_callback_s *cb,
                                       FAR struct nxeg_state_s *state)
patacongo's avatar
patacongo committed
  NXEGWINDOW hwnd;
patacongo's avatar
patacongo committed
  hwnd = nx_openwindow(g_hnx, cb, (FAR void *)state);
  if (!hwnd)
    {
      message("user_start: nx_openwindow failed: %d\n", errno);
      g_exitcode = NXEXIT_NXOPENWINDOW;
    }
  return hwnd;
}
#else
patacongo's avatar
patacongo committed
static inline NXEGWINDOW nxeg_openwindow(FAR const struct nx_callback_s *cb,
                                         FAR struct nxeg_state_s *state)
patacongo's avatar
patacongo committed
  NXEGWINDOW hwnd;
patacongo's avatar
patacongo committed
  hwnd = nxtk_openwindow(g_hnx, cb, (FAR void *)state);
  if (!hwnd)
    {
      message("user_start: nxtk_openwindow failed: %d\n", errno);
      g_exitcode = NXEXIT_NXOPENWINDOW;
    }
  return hwnd;
}
#endif

/****************************************************************************
 * Name: nxeg_closewindow
 ****************************************************************************/

#ifdef CONFIG_EXAMPLES_NX_RAWWINDOWS
patacongo's avatar
patacongo committed
static inline int nxeg_closewindow(NXEGWINDOW hwnd, FAR struct nxeg_state_s *state)
{
  int ret = nx_closewindow(hwnd);
  if (ret < 0)
    {
patacongo's avatar
patacongo committed
      message("user_start: nx_closewindow failed: %d\n", errno);
      g_exitcode = NXEXIT_NXCLOSEWINDOW;
    }
  return ret;
}
#else
patacongo's avatar
patacongo committed
static inline int nxeg_closewindow(NXEGWINDOW hwnd, FAR struct nxeg_state_s *state)
{
  int ret = nxtk_closewindow(hwnd);
  if (ret < 0)
    {
patacongo's avatar
patacongo committed
      message("user_start: nxtk_closewindow failed: %d\n", errno);
      g_exitcode = NXEXIT_NXCLOSEWINDOW;
    }
patacongo's avatar
patacongo committed
  nxeg_freestate(state);
  return ret;
}
#endif

/****************************************************************************
 * Name: nxeg_setsize
 ****************************************************************************/

#ifdef CONFIG_EXAMPLES_NX_RAWWINDOWS
patacongo's avatar
patacongo committed
static inline int nxeg_setsize(NXEGWINDOW hwnd, FAR struct nxgl_size_s *size)
{
  int ret = nx_setsize(hwnd, size);
  if (ret < 0)
    {
      message("user_start: nx_setsize failed: %d\n", errno);
      g_exitcode = NXEXIT_NXSETSIZE;
    }
  return ret;
}
#else
patacongo's avatar
patacongo committed
static inline int nxeg_setsize(NXEGWINDOW hwnd, FAR struct nxgl_size_s *size)
{
  int ret = nxtk_setsize(hwnd, size);
  if (ret < 0)
    {
      message("user_start: nxtk_setsize failed: %d\n", errno);
      g_exitcode = NXEXIT_NXSETSIZE;
    }
  return ret;
}
#endif

/****************************************************************************
 * Name: nxeg_setposition
 ****************************************************************************/

#ifdef CONFIG_EXAMPLES_NX_RAWWINDOWS
patacongo's avatar
patacongo committed
static inline int nxeg_setposition(NXEGWINDOW hwnd, FAR struct nxgl_point_s *pos)
{
  int ret = nx_setposition(hwnd, pos);
  if (ret < 0)
    {
      message("user_start: nx_setposition failed: %d\n", errno);
      g_exitcode = NXEXIT_NXSETPOSITION;
    }
  return ret;
}
#else
patacongo's avatar
patacongo committed
static inline int nxeg_setposition(NXEGWINDOW hwnd, FAR struct nxgl_point_s *pos)
{
  int ret = nxtk_setposition(hwnd, pos);
  if (ret < 0)
    {
      message("user_start: nxtk_setposition failed: %d\n", errno);
      g_exitcode = NXEXIT_NXSETPOSITION;
    }
  return ret;
}
#endif

/****************************************************************************
 * Name: nxeq_opentoolbar
 ****************************************************************************/

#ifndef CONFIG_EXAMPLES_NX_RAWWINDOWS
patacongo's avatar
patacongo committed
static inline int nxeq_opentoolbar(NXEGWINDOW hwnd, nxgl_coord_t height,
                                   FAR const struct nx_callback_s *cb,
patacongo's avatar
patacongo committed
                                   FAR struct nxeg_state_s *state)
  int ret;
patacongo's avatar
patacongo committed
  ret = nxtk_opentoolbar(hwnd, height, cb, (FAR void *)state);
  if (ret < 0)
    {
      message("user_start: nxtk_opentoolbar failed: %d\n", errno);
      g_exitcode = NXEXIT_NXOPENTOOLBAR;
    }
  return ret;
}
#endif

/****************************************************************************
 * Name: nxeg_lower
 ****************************************************************************/

#ifdef CONFIG_EXAMPLES_NX_RAWWINDOWS
patacongo's avatar
patacongo committed
static inline int nxeg_lower(NXEGWINDOW hwnd)
{
  int ret = nx_lower(hwnd);
  if (ret < 0)
    {
      message("user_start: nx_lower failed: %d\n", errno);
      g_exitcode = NXEXIT_NXLOWER;
    }
  return ret;
}
#else
patacongo's avatar
patacongo committed
static inline int nxeg_lower(NXEGWINDOW hwnd)
{
  int ret = nxtk_lower(hwnd);
  if (ret < 0)
    {
      message("user_start: nxtk_lower failed: %d\n", errno);
      g_exitcode = NXEXIT_NXLOWER;
    }
  return ret;
}
#endif

/****************************************************************************
 * Name: nxeg_raise
 ****************************************************************************/

#ifdef CONFIG_EXAMPLES_NX_RAWWINDOWS
patacongo's avatar
patacongo committed
static inline int nxeg_raise(NXEGWINDOW hwnd)
{
  int ret = nx_raise(hwnd);
  if (ret < 0)
    {
      message("user_start: nx_raise failed: %d\n", errno);
      g_exitcode = NXEXIT_NXRAISE;
    }
  return ret;
}
#else
patacongo's avatar
patacongo committed
static inline int nxeg_raise(NXEGWINDOW hwnd)
{
  int ret = nxtk_raise(hwnd);
  if (ret < 0)
    {
      message("user_start: nxtk_raise failed: %d\n", errno);
      g_exitcode = NXEXIT_NXRAISE;
    }
  return ret;
}
#endif

patacongo's avatar
patacongo committed
/****************************************************************************
patacongo's avatar
patacongo committed
 * Name: nxeg_suinitialize
patacongo's avatar
patacongo committed
 ****************************************************************************/

patacongo's avatar
patacongo committed
#ifndef CONFIG_NX_MULTIUSER
static inline int nxeg_suinitialize(void)
patacongo's avatar
patacongo committed
{
  FAR NX_DRIVERTYPE *dev;
patacongo's avatar
patacongo committed

patacongo's avatar
patacongo committed
#if defined(CONFIG_EXAMPLES_NX_EXTERNINIT)
  /* Use external graphics driver initialization */

  message("nxeg_initialize: Initializing external graphics device\n");
  dev = up_nxdrvinit(CONFIG_EXAMPLES_NX_DEVNO);
  if (!dev)
    {
      message("nxeg_initialize: up_nxdrvinit failed, devno=%d\n", CONFIG_EXAMPLES_NX_DEVNO);
      g_exitcode = NXEXIT_EXTINITIALIZE;
      return ERROR;
    }

#elif defined(CONFIG_NX_LCDDRIVER)
patacongo's avatar
patacongo committed
  int ret;

  /* Initialize the LCD device */

  message("nxeg_initialize: Initializing LCD\n");
  ret = up_lcdinitialize();
  if (ret < 0)
    {
      message("nxeg_initialize: up_lcdinitialize failed: %d\n", -ret);
      g_exitcode = NXEXIT_LCDINITIALIZE;
      return ERROR;
    }

  /* Get the device instance */

  dev = up_lcdgetdev(CONFIG_EXAMPLES_NX_DEVNO);
  if (!dev)
    {
      message("nxeg_initialize: up_lcdgetdev failed, devno=%d\n", CONFIG_EXAMPLES_NX_DEVNO);
      g_exitcode = NXEXIT_LCDGETDEV;
      return ERROR;
    }

  /* Turn the LCD on at 75% power */

patacongo's avatar
patacongo committed
  (void)dev->setpower(dev, ((3*CONFIG_LCD_MAXPOWER + 3)/4));
#else
patacongo's avatar
patacongo committed
  int ret;

patacongo's avatar
patacongo committed
  /* Initialize the frame buffer device */

  message("nxeg_initialize: Initializing framebuffer\n");
  ret = up_fbinitialize();
  if (ret < 0)
    {
      message("nxeg_initialize: up_fbinitialize failed: %d\n", -ret);
      g_exitcode = NXEXIT_FBINITIALIZE;
      return ERROR;
    }

  dev = up_fbgetvplane(CONFIG_EXAMPLES_NX_VPLANE);
  if (!dev)
patacongo's avatar
patacongo committed
    {
      message("nxeg_initialize: up_fbgetvplane failed, vplane=%d\n", CONFIG_EXAMPLES_NX_VPLANE);
      g_exitcode = NXEXIT_FBGETVPLANE;
      return ERROR;
    }
#endif
patacongo's avatar
patacongo committed

  /* Then open NX */

  message("nxeg_initialize: Open NX\n");
  g_hnx = nx_open(dev);
patacongo's avatar
patacongo committed
  if (!g_hnx)
    {
      message("user_start: nx_open failed: %d\n", errno);
      g_exitcode = NXEXIT_NXOPEN;
      return ERROR;
    }
  return OK;
patacongo's avatar
patacongo committed
}
patacongo's avatar
patacongo committed
#endif
patacongo's avatar
patacongo committed

/****************************************************************************
patacongo's avatar
patacongo committed
 * Name: nxeg_initialize
patacongo's avatar
patacongo committed
 ****************************************************************************/

patacongo's avatar
patacongo committed
#ifdef CONFIG_NX_MULTIUSER
static inline int nxeg_muinitialize(void)
patacongo's avatar
patacongo committed
{
  struct sched_param param;
  pthread_t thread;
patacongo's avatar
patacongo committed
  pid_t servrid;
  int ret;
  /* Set the client task priority */
  param.sched_priority = CONFIG_EXAMPLES_NX_CLIENTPRIO;
  ret = sched_setparam(0, &param);
patacongo's avatar
patacongo committed
  if (ret < 0)
      message("nxeg_initialize: sched_setparam failed: %d\n" , ret);
      g_exitcode = NXEXIT_SCHEDSETPARAM;
patacongo's avatar
patacongo committed
      return ERROR;
patacongo's avatar
patacongo committed

  /* Start the server task */

patacongo's avatar
patacongo committed
  message("nxeg_initialize: Starting nx_servertask task\n");
  servrid = task_create("NX Server", CONFIG_EXAMPLES_NX_SERVERPRIO,
                        CONFIG_EXAMPLES_NX_STACKSIZE, nx_servertask, NULL);
patacongo's avatar
patacongo committed
  if (servrid < 0)
    {
patacongo's avatar
patacongo committed
      message("nxeg_initialize: Failed to create nx_servertask task: %d\n", errno);
      g_exitcode = NXEXIT_TASKCREATE;
      return ERROR;
patacongo's avatar
patacongo committed
    }

  /* Wait a bit to let the server get started */

patacongo's avatar
patacongo committed

  /* Connect to the server */

patacongo's avatar
patacongo committed
  g_hnx = nx_connect();
  if (g_hnx)
patacongo's avatar
patacongo committed
    {
       pthread_attr_t attr;
patacongo's avatar
patacongo committed

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

       (void)pthread_attr_init(&attr);
       param.sched_priority = CONFIG_EXAMPLES_NX_LISTENERPRIO;
       (void)pthread_attr_setschedparam(&attr, &param);
       (void)pthread_attr_setstacksize(&attr, CONFIG_EXAMPLES_NX_STACKSIZE);

       ret = pthread_create(&thread, &attr, nx_listenerthread, NULL);
       if (ret != 0)
patacongo's avatar
patacongo committed
         {
            printf("nxeg_initialize: pthread_create failed: %d\n", ret);
            g_exitcode = NXEXIT_PTHREADCREATE;
patacongo's avatar
patacongo committed
            return ERROR;
         }

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

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

           (void)sem_wait(&g_semevent);
patacongo's avatar
patacongo committed
         }
patacongo's avatar
patacongo committed
    }
patacongo's avatar
patacongo committed
  else
patacongo's avatar
patacongo committed
    {
      message("nxeg_initialize: nx_connect failed: %d\n", errno);
patacongo's avatar
patacongo committed
      g_exitcode = NXEXIT_NXCONNECT;
      return ERROR;
patacongo's avatar
patacongo committed
    }
patacongo's avatar
patacongo committed
  return OK;
}
#endif
patacongo's avatar
patacongo committed

patacongo's avatar
patacongo committed
/****************************************************************************
 * Name: nxeg_initialize
 ****************************************************************************/

static int nxeg_initialize(void)
{
  int i;

  /* Initialize window colors */

  for (i = 0; i < CONFIG_NX_NPLANES; i++)
    {
      g_color1[i]  = CONFIG_EXAMPLES_NX_COLOR1;
      g_color2[i]  = CONFIG_EXAMPLES_NX_COLOR2;
#ifndef CONFIG_EXAMPLES_NX_RAWWINDOWS
      g_tbcolor[i] = CONFIG_EXAMPLES_NX_TBCOLOR;
#endif
patacongo's avatar
patacongo committed
    }
patacongo's avatar
patacongo committed

patacongo's avatar
patacongo committed
#ifdef CONFIG_NX_MULTIUSER
  return nxeg_muinitialize();
#else
  return nxeg_suinitialize();
patacongo's avatar
patacongo committed
#endif
patacongo's avatar
patacongo committed
}

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: user_initialize
 ****************************************************************************/

void user_initialize(void)
{
}

/****************************************************************************
 * Name: user_start
 ****************************************************************************/

int user_start(int argc, char *argv[])
{
patacongo's avatar
patacongo committed
  NXEGWINDOW hwnd1;
  NXEGWINDOW hwnd2;
  struct nxgl_size_s size;
patacongo's avatar
patacongo committed
  struct nxgl_point_s pt;
  nxgl_mxpixel_t color;
  int ret;

  /* Initialize */
patacongo's avatar
patacongo committed

patacongo's avatar
patacongo committed
  ret = nxeg_initialize();
  message("user_start: NX handle=%p\n", g_hnx);
  if (!g_hnx || ret < 0)
patacongo's avatar
patacongo committed
    {
      message("user_start: Failed to get NX handle: %d\n", errno);
patacongo's avatar
patacongo committed
      g_exitcode = NXEXIT_NXOPEN;
patacongo's avatar
patacongo committed
      goto errout;
    }

  /* Set the background to the configured background color */

  message("user_start: Set background color=%d\n", CONFIG_EXAMPLES_NX_BGCOLOR);
  color = CONFIG_EXAMPLES_NX_BGCOLOR;
patacongo's avatar
patacongo committed
  ret = nx_setbgcolor(g_hnx, &color);
patacongo's avatar
patacongo committed
  if (ret < 0)
    {
      message("user_start: nx_setbgcolor failed: %d\n", errno);
patacongo's avatar
patacongo committed
      g_exitcode = NXEXIT_NXSETBGCOLOR;
patacongo's avatar
patacongo committed
      goto errout_with_nx;
    }

patacongo's avatar
patacongo committed
  /* Create window #1 */
patacongo's avatar
patacongo committed

patacongo's avatar
patacongo committed
  message("user_start: Create window #1\n");
patacongo's avatar
patacongo committed
  nxeg_initstate(&g_wstate[0], 1, CONFIG_EXAMPLES_NX_COLOR1);
  hwnd1 = nxeg_openwindow(&g_nxcb, &g_wstate[0]);
patacongo's avatar
patacongo committed
  message("user_start: hwnd1=%p\n", hwnd1);
  if (!hwnd1)
patacongo's avatar
patacongo committed
    {
      goto errout_with_nx;
    }
patacongo's avatar
patacongo committed

  /* Wait until we have the screen resolution */

  while (!b_haveresolution)
    {
      (void)sem_wait(&g_semevent);
    }
patacongo's avatar
patacongo committed
  message("user_start: Screen resolution (%d,%d)\n", g_xres, g_yres);

patacongo's avatar
patacongo committed
  /* Set the size of the window 1 */
patacongo's avatar
patacongo committed

  size.w = g_xres / 2;
  size.h = g_yres / 2;
patacongo's avatar
patacongo committed

  message("user_start: Set window #1 size to (%d,%d)\n", size.w, size.h);
  ret = nxeg_setsize(hwnd1, &size);
patacongo's avatar
patacongo committed
  if (ret < 0)
    {
      goto errout_with_hwnd1;
    }

  /* Sleep a bit -- both so that we can see the result of the above operations
   * but also, in the multi-user case, so that the server can get a chance to
   * actually do them!
   */

  message("user_start: Sleeping\n\n");
  sleep(1);

  /* Set the position of window #1 */

  pt.x = g_xres / 8;
  pt.y = g_yres / 8;
  message("user_start: Set window #1 postion to (%d,%d)\n", pt.x, pt.y);
  ret = nxeg_setposition(hwnd1, &pt);
patacongo's avatar
patacongo committed
  if (ret < 0)
    {
      goto errout_with_hwnd1;
    }

  /* Sleep a bit */
patacongo's avatar
patacongo committed

  message("user_start: Sleeping\n\n");
  sleep(1);

  /* Open the toolbar */

#ifndef CONFIG_EXAMPLES_NX_RAWWINDOWS
  message("user_start: Add toolbar to window #1\n");
patacongo's avatar
patacongo committed
  ret = nxeq_opentoolbar(hwnd1, CONFIG_TOOLBAR_HEIGHT, &g_tbcb, &g_wstate[0]);
  if (ret < 0)
    {
      goto errout_with_hwnd1;
    }

  /* Sleep a bit */

  message("user_start: Sleeping\n\n");
  sleep(1);
#endif
patacongo's avatar
patacongo committed

patacongo's avatar
patacongo committed
  /* Create window #2 */

  message("user_start: Create window #2\n");
patacongo's avatar
patacongo committed
  nxeg_initstate(&g_wstate[1], 2, CONFIG_EXAMPLES_NX_COLOR2);
  hwnd2 = nxeg_openwindow(&g_nxcb, &g_wstate[1]);
patacongo's avatar
patacongo committed
  message("user_start: hwnd2=%p\n", hwnd2);
  if (!hwnd2)
patacongo's avatar
patacongo committed
    {
      goto errout_with_hwnd1;
    }

  /* Sleep a bit */

  message("user_start: Sleeping\n\n");
  sleep(1);

patacongo's avatar
patacongo committed
  /* Set the size of the window 2 == size of window 1*/

  message("user_start: Set hwnd2 size to (%d,%d)\n", size.w, size.h);
  ret = nxeg_setsize(hwnd2, &size);
patacongo's avatar
patacongo committed
  if (ret < 0)
    {
      goto errout_with_hwnd2;
    }

  /* Sleep a bit */

  message("user_start: Sleeping\n\n");
  sleep(1);

  /* Set the position of window #2 */

  pt.x = g_xres - size.w - pt.x;
  pt.y = g_yres - size.h - pt.y;
patacongo's avatar
patacongo committed

  message("user_start: Set hwnd2 postion to (%d,%d)\n", pt.x, pt.y);
  ret = nxeg_setposition(hwnd2, &pt);
patacongo's avatar
patacongo committed
  if (ret < 0)
    {
      goto errout_with_hwnd2;
    }

patacongo's avatar
patacongo committed
  /* Sleep a bit */

  message("user_start: Sleeping\n\n");
  sleep(1);

#ifndef CONFIG_EXAMPLES_NX_RAWWINDOWS
  message("user_start: Add toolbar to window #2\n");
patacongo's avatar
patacongo committed
  ret = nxeq_opentoolbar(hwnd2, CONFIG_TOOLBAR_HEIGHT, &g_tbcb, &g_wstate[1]);
  if (ret < 0)
    {
      goto errout_with_hwnd2;
    }

  /* Sleep a bit */

patacongo's avatar
patacongo committed
  message("user_start: Sleeping\n\n");
  sleep(1);
#endif

  /* Give keyboard input to the top window -- should be window #2 */

#ifdef CONFIG_NX_KBD
  message("user_start: Send keyboard input: %s\n", g_kbdmsg1);
  ret = nx_kbdin(g_hnx, strlen((FAR const char *)g_kbdmsg1), g_kbdmsg1);
  if (ret < 0)
    {
      message("user_start: nx_kbdin failed: %d\n", errno);
      goto errout_with_hwnd2;
    }

  /* Sleep a bit */

  message("user_start: Sleeping\n\n");
  sleep(1);
#endif
patacongo's avatar
patacongo committed

patacongo's avatar
patacongo committed
  /* Lower window 2 */

  message("user_start: Lower window #2\n");
  ret = nxeg_lower(hwnd2);
patacongo's avatar
patacongo committed
  if (ret < 0)
    {
      goto errout_with_hwnd2;
    }

patacongo's avatar
patacongo committed
  /* Sleep a bit */

  message("user_start: Sleeping\n\n");
  sleep(1);

patacongo's avatar
patacongo committed
  /* Put mouse left-button clicks all over the screen and see who responds */

#ifdef CONFIG_NX_MOUSE
  nxeg_drivemouse();

  /* Sleep a bit */

patacongo's avatar
patacongo committed
  message("user_start: Sleeping\n\n");
  sleep(1);
#endif

  /* Give keyboard input to the top window -- should be window #1 */

#ifdef CONFIG_NX_KBD
  message("user_start: Send keyboard input: %s\n", g_kbdmsg2);
  ret = nx_kbdin(g_hnx, strlen((FAR const char *)g_kbdmsg2), g_kbdmsg2);
  if (ret < 0)
    {
      message("user_start: nx_kbdin failed: %d\n", errno);
      goto errout_with_hwnd2;
    }

  /* Sleep a bit */

patacongo's avatar
patacongo committed
  message("user_start: Sleeping\n\n");
  sleep(1);
#endif

  /* Raise window 2 */

  message("user_start: Raise window #2\n");
  ret = nxeg_raise(hwnd2);
  if (ret < 0)
    {
      goto errout_with_hwnd2;
    }

patacongo's avatar
patacongo committed
  /* Put mouse left-button clicks all over the screen and see who responds */

#ifdef CONFIG_NX_MOUSE
  nxeg_drivemouse();
#endif

  /* Sleep a bit */

patacongo's avatar
patacongo committed
  message("user_start: Sleeping\n\n");
  sleep(2);

patacongo's avatar
patacongo committed
  /* Close the window 2 */

errout_with_hwnd2:
  message("user_start: Close window #2\n");
  (void)nxeg_closewindow(hwnd2, &g_wstate[1]);
patacongo's avatar
patacongo committed

  /* Close the window1 */

errout_with_hwnd1:
  message("user_start: Close window #1\n");
patacongo's avatar
patacongo committed
  (void)nxeg_closewindow(hwnd1, &g_wstate[0]);
patacongo's avatar
patacongo committed

errout_with_nx:
#ifdef CONFIG_NX_MULTIUSER
  /* Disconnect from the server */

  message("user_start: Disconnect from the server\n");
patacongo's avatar
patacongo committed
  nx_disconnect(g_hnx);
patacongo's avatar
patacongo committed
#else
  /* Close the server */

  message("user_start: Close NX\n");
patacongo's avatar
patacongo committed
  nx_close(g_hnx);
patacongo's avatar
patacongo committed
#endif
errout:
patacongo's avatar
patacongo committed
  return g_exitcode;
patacongo's avatar
patacongo committed
}