nuttx/examples/nx/nx_main.c

527 lines
14 KiB
C
Raw Normal View History

/****************************************************************************
* examples/nx/nx_main.c
*
* Copyright (C) 2008 Gregory Nutt. All rights reserved.
* 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>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sched.h>
#include <pthread.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/fb.h>
#include <nuttx/arch.h>
#include <nuttx/nx.h>
#include "nx_internal.h"
/****************************************************************************
* Definitions
****************************************************************************/
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
static int g_exitcode = NXEXIT_SUCCESS;
/****************************************************************************
* Public Data
****************************************************************************/
/* The connecton handler */
NXHANDLE g_hnx = NULL;
/* The screen resolution */
nxgl_coord_t g_xres;
nxgl_coord_t g_yres;
boolean b_haveresolution = FALSE;
#ifdef CONFIG_NX_MULTIUSER
boolean g_connected = FALSE;
#endif
sem_t g_semevent = {0};
/* Colors used to fill window 1 & 2 */
nxgl_mxpixel_t g_color1[CONFIG_NX_NPLANES];
nxgl_mxpixel_t g_color2[CONFIG_NX_NPLANES];
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* 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);
}
}
}
#endif
/****************************************************************************
* Name: nxeg_suinitialize
****************************************************************************/
#ifndef CONFIG_NX_MULTIUSER
static inline int nxeg_suinitialize(void)
{
FAR struct fb_vtable_s *fb;
int ret;
/* 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;
}
fb = up_fbgetvplane(CONFIG_EXAMPLES_NX_VPLANE);
if (!fb)
{
message("nxeg_initialize: up_fbgetvplane failed, vplane=%d\n", CONFIG_EXAMPLES_NX_VPLANE);
g_exitcode = NXEXIT_FBGETVPLANE;
return ERROR;
}
/* Then open NX */
message("nxeg_initialize: Open NX\n");
g_hnx = nx_open(fb);
if (!g_hnx)
{
message("user_start: nx_open failed: %d\n", errno);
g_exitcode = NXEXIT_NXOPEN;
return ERROR;
}
return OK;
}
#endif
/****************************************************************************
* Name: nxeg_initialize
****************************************************************************/
#ifdef CONFIG_NX_MULTIUSER
static inline int nxeg_muinitialize(void)
{
struct sched_param param;
pthread_t thread;
pid_t servrid;
int ret;
/* Set the client task priority */
param.sched_priority = CONFIG_EXAMPLES_NX_CLIENTPRIO;
ret = sched_setparam(0, &param);
if (ret < 0)
{
message("nxeg_initialize: sched_setparam failed: %d\n" , ret);
g_exitcode = NXEXIT_SCHEDSETPARAM;
return ERROR;
}
/* Start the server task */
message("nxeg_initialize: Starting nx_servertask task\n");
servrid = task_create("NX Server", CONFIG_EXAMPLES_NX_SERVERPRIO,
CONFIG_EXAMPLES_NX_STACKSIZE, nx_servertask, NULL);
if (servrid < 0)
{
message("nxeg_initialize: Failed to create nx_servertask task: %d\n", errno);
g_exitcode = NXEXIT_TASKCREATE;
return ERROR;
}
/* Wait a bit to let the server get started */
sleep(1);
/* Connect to the server */
g_hnx = nx_connect();
if (g_hnx)
{
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 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)
{
printf("nxeg_initialize: pthread_create failed: %d\n", ret);
g_exitcode = NXEXIT_PTHREADCREATE;
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);
}
}
else
{
message("nxeg_initialize: nx_connect failed: %d\n", errno);
g_exitcode = NXEXIT_NXCONNECT;
return ERROR;
}
return OK;
}
#endif
/****************************************************************************
* 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;
}
#ifdef CONFIG_NX_MULTIUSER
return nxeg_muinitialize();
#else
return nxeg_suinitialize();
#endif
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: user_initialize
****************************************************************************/
void user_initialize(void)
{
}
/****************************************************************************
* Name: user_start
****************************************************************************/
int user_start(int argc, char *argv[])
{
NXWINDOW hwnd1;
NXWINDOW hwnd2;
struct nxgl_size_s size;
struct nxgl_point_s pt;
nxgl_mxpixel_t color;
int ret;
/* Initialize */
ret = nxeg_initialize();
message("user_start: NX handle=%p\n", g_hnx);
if (!g_hnx || ret < 0)
{
message("user_start: Failed to get NX handle: %d\n", errno);
g_exitcode = NXEXIT_NXOPEN;
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;
ret = nx_setbgcolor(g_hnx, &color);
if (ret < 0)
{
message("user_start: nx_setbgcolor failed: %d\n", errno);
g_exitcode = NXEXIT_NXSETBGCOLOR;
goto errout_with_nx;
}
/* Create window #1 */
message("user_start: Create window #1\n");
hwnd1 = nx_openwindow(g_hnx, &g_nxcb1, (FAR void *)1);
message("user_start: hwnd1=%p\n", hwnd1);
if (!hwnd1)
{
message("user_start: nx_openwindow failed: %d\n", errno);
g_exitcode = NXEXIT_NXOPENWINDOW;
goto errout_with_nx;
}
/* Wait until we have the screen resolution */
while (!b_haveresolution)
{
(void)sem_wait(&g_semevent);
}
message("user_start: Screen resolution (%d,%d)\n", g_xres, g_yres);
/* Set the size of the window 1 */
size.w = g_xres / 2;
size.h = g_yres / 2;
message("user_start: Set hwnd1 size to (%d,%d)\n", size.w, size.h);
ret = nx_setsize(hwnd1, &size);
if (ret < 0)
{
message("user_start: nx_setsize failed: %d\n", errno);
g_exitcode = NXEXIT_NXSETSIZE;
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 hwnd1 postion to (%d,%d)\n", pt.x, pt.y);
ret = nx_setposition(hwnd1, &pt);
if (ret < 0)
{
message("user_start: nx_setposition failed: %d\n", errno);
g_exitcode = NXEXIT_NXSETPOSITION;
goto errout_with_hwnd1;
}
/* Sleep a bit */
message("user_start: Sleeping\n\n");
sleep(2);
/* Create window #2 */
message("user_start: Create window #2\n");
hwnd2 = nx_openwindow(g_hnx, &g_nxcb2, (FAR void *)2);
message("user_start: hwnd2=%p\n", hwnd2);
if (!hwnd2)
{
message("user_start: nx_openwindow failed: %d\n", errno);
g_exitcode = NXEXIT_NXOPENWINDOW;
goto errout_with_hwnd1;
}
/* Sleep a bit */
message("user_start: Sleeping\n\n");
sleep(1);
/* 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 = nx_setsize(hwnd2, &size);
if (ret < 0)
{
message("user_start: nx_setsize failed: %d\n", errno);
g_exitcode = NXEXIT_NXSETSIZE;
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;
message("user_start: Set hwnd2 postion to (%d,%d)\n", pt.x, pt.y);
ret = nx_setposition(hwnd2, &pt);
if (ret < 0)
{
message("user_start: nx_setposition failed: %d\n", errno);
g_exitcode = NXEXIT_NXSETPOSITION;
goto errout_with_hwnd2;
}
/* Sleep a bit */
message("user_start: Sleeping\n\n");
sleep(2);
/* Lower window 2 */
message("user_start: Lower window #2\n");
ret = nx_lower(hwnd2);
if (ret < 0)
{
message("user_start: nx_lower failed: %d\n", errno);
g_exitcode = NXEXIT_NXSETPOSITION;
goto errout_with_hwnd2;
}
/* Sleep a bit */
message("user_start: Sleeping\n\n");
sleep(1);
/* Put mouse left-button clicks all over the screen and see who responds */
#ifdef CONFIG_NX_MOUSE
nxeg_drivemouse();
/* Sleep a bit */
message("user_start: Sleeping\n\n");
sleep(1);
#endif
/* Raise window 2 */
message("user_start: Raise window #2\n");
ret = nx_raise(hwnd2);
if (ret < 0)
{
message("user_start: nx_raise failed: %d\n", errno);
g_exitcode = NXEXIT_NXSETPOSITION;
goto errout_with_hwnd2;
}
/* Put mouse left-button clicks all over the screen and see who responds */
#ifdef CONFIG_NX_MOUSE
nxeg_drivemouse();
#endif
/* Sleep a bit */
message("user_start: Sleeping\n\n");
sleep(2);
/* Close the window 2 */
errout_with_hwnd2:
message("user_start: Close window\n");
ret = nx_closewindow(hwnd2);
if (ret < 0)
{
message("user_start: nx_openwindow failed: %d\n", errno);
g_exitcode = NXEXIT_NXCLOSEWINDOW;
goto errout_with_nx;
}
/* Close the window1 */
errout_with_hwnd1:
message("user_start: Close window\n");
ret = nx_closewindow(hwnd1);
if (ret < 0)
{
message("user_start: nx_openwindow failed: %d\n", errno);
g_exitcode = NXEXIT_NXCLOSEWINDOW;
goto errout_with_nx;
}
errout_with_nx:
#ifdef CONFIG_NX_MULTIUSER
/* Disconnect from the server */
message("user_start: Disconnect from the server\n");
nx_disconnect(g_hnx);
#else
/* Close the server */
message("user_start: Close NX\n");
nx_close(g_hnx);
#endif
errout:
return g_exitcode;
}