2011-07-21 02:01:15 +02:00
|
|
|
/****************************************************************************
|
2021-06-16 09:22:16 +02:00
|
|
|
* apps/examples/nximage/nximage_main.c
|
2011-07-21 02:01:15 +02:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2011-07-21 02:01:15 +02:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-07-21 02:01:15 +02:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2011-07-21 02:01:15 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2015-04-01 00:20:21 +02:00
|
|
|
#include <sys/boardctl.h>
|
2011-07-21 02:01:15 +02:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2015-04-04 19:49:15 +02:00
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <nuttx/board.h>
|
|
|
|
|
2011-07-24 22:49:01 +02:00
|
|
|
#include <nuttx/nx/nx.h>
|
|
|
|
#include <nuttx/nx/nxglib.h>
|
|
|
|
#include <nuttx/nx/nxfonts.h>
|
2011-07-21 02:01:15 +02:00
|
|
|
|
|
|
|
#include "nximage.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct nximage_data_s g_nximage =
|
|
|
|
{
|
2017-10-14 23:09:43 +02:00
|
|
|
NULL, /* hnx */
|
|
|
|
NULL, /* hbkgd */
|
|
|
|
false, /* connected */
|
|
|
|
0, /* xres */
|
|
|
|
0, /* yres */
|
|
|
|
false, /* havpos */
|
|
|
|
SEM_INITIALIZER(0), /* eventsem */
|
|
|
|
NXEXIT_SUCCESS /* exit code */
|
2011-07-21 02:01:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nximage_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the LCD or framebuffer device (single user mode only), then
|
|
|
|
* open NX.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int nximage_initialize(void)
|
|
|
|
{
|
2017-10-14 23:09:43 +02:00
|
|
|
struct sched_param param;
|
|
|
|
pthread_t thread;
|
2016-04-22 21:38:06 +02:00
|
|
|
int ret;
|
2011-07-21 02:01:15 +02:00
|
|
|
|
2017-10-14 23:09:43 +02:00
|
|
|
/* Set the client task priority */
|
2011-07-21 02:01:15 +02:00
|
|
|
|
2017-10-14 23:09:43 +02:00
|
|
|
param.sched_priority = CONFIG_EXAMPLES_NXIMAGE_CLIENTPRIO;
|
|
|
|
ret = sched_setparam(0, ¶m);
|
2015-04-16 15:39:00 +02:00
|
|
|
if (ret < 0)
|
2011-07-21 02:01:15 +02:00
|
|
|
{
|
2017-10-14 23:09:43 +02:00
|
|
|
printf("nximage_initialize: sched_setparam failed: %d\n" , ret);
|
2011-07-21 02:01:15 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2017-10-14 23:09:43 +02:00
|
|
|
/* Start the NX server kernel thread */
|
2011-07-21 02:01:15 +02:00
|
|
|
|
2017-10-14 23:09:43 +02:00
|
|
|
ret = boardctl(BOARDIOC_NX_START, 0);
|
2011-07-21 02:01:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-14 23:09:43 +02:00
|
|
|
printf("nximage_initialize: Failed to start the NX server: %d\n", errno);
|
2011-07-21 02:01:15 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2017-10-14 23:09:43 +02:00
|
|
|
/* Connect to the server */
|
2011-07-21 02:01:15 +02:00
|
|
|
|
2017-10-14 23:09:43 +02:00
|
|
|
g_nximage.hnx = nx_connect();
|
|
|
|
if (g_nximage.hnx)
|
2011-07-21 02:01:15 +02:00
|
|
|
{
|
2017-10-14 23:09:43 +02:00
|
|
|
pthread_attr_t attr;
|
2011-07-21 02:01:15 +02:00
|
|
|
|
2017-10-14 23:09:43 +02:00
|
|
|
#ifdef CONFIG_VNCSERVER
|
|
|
|
/* Setup the VNC server to support keyboard/mouse inputs */
|
|
|
|
|
2019-04-02 15:06:06 +02:00
|
|
|
struct boardioc_vncstart_s vnc =
|
|
|
|
{
|
|
|
|
0, g_nximage.hnx
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = boardctl(BOARDIOC_VNC_START, (uintptr_t)&vnc);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
printf("boardctl(BOARDIOC_VNC_START) failed: %d\n", ret);
|
|
|
|
nx_disconnect(g_nximage.hnx);
|
|
|
|
return ERROR;
|
|
|
|
}
|
2011-07-21 02:01:15 +02:00
|
|
|
#endif
|
2019-04-02 15:06:06 +02:00
|
|
|
|
2017-10-14 23:09:43 +02:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
pthread_attr_init(&attr);
|
2017-10-14 23:09:43 +02:00
|
|
|
param.sched_priority = CONFIG_EXAMPLES_NXIMAGE_LISTENERPRIO;
|
2020-01-02 13:09:50 +01:00
|
|
|
pthread_attr_setschedparam(&attr, ¶m);
|
|
|
|
pthread_attr_setstacksize(&attr, CONFIG_EXAMPLES_NXIMAGE_LISTENER_STACKSIZE);
|
2017-10-14 23:09:43 +02:00
|
|
|
|
|
|
|
ret = pthread_create(&thread, &attr, nximage_listener, NULL);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
printf("nximage_initialize: pthread_create failed: %d\n", ret);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't return until we are connected to the server */
|
|
|
|
|
|
|
|
while (!g_nximage.connected)
|
|
|
|
{
|
|
|
|
/* Wait for the listener thread to wake us up when we really
|
|
|
|
* are connected.
|
|
|
|
*/
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
sem_wait(&g_nximage.eventsem);
|
2017-10-14 23:09:43 +02:00
|
|
|
}
|
2011-07-21 02:01:15 +02:00
|
|
|
}
|
2017-10-14 23:09:43 +02:00
|
|
|
else
|
2016-04-22 21:38:06 +02:00
|
|
|
{
|
2017-10-14 23:09:43 +02:00
|
|
|
printf("nximage_initialize: nx_connect failed: %d\n", errno);
|
2016-04-22 21:38:06 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2011-07-21 02:01:15 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-08-30 22:13:50 +02:00
|
|
|
* Name: nximage_main
|
2011-07-21 02:01:15 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Main entry pointer. Configures the basic display resources.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-06 17:23:23 +02:00
|
|
|
int main(int argc, FAR char *argv[])
|
2011-07-21 02:01:15 +02:00
|
|
|
{
|
|
|
|
nxgl_mxpixel_t color;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Initialize NX */
|
|
|
|
|
|
|
|
ret = nximage_initialize();
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("nximage_main: NX handle=%p\n", g_nximage.hnx);
|
2011-07-21 02:01:15 +02:00
|
|
|
if (!g_nximage.hnx || ret < 0)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("nximage_main: Failed to get NX handle: %d\n", errno);
|
2017-10-14 23:09:43 +02:00
|
|
|
g_nximage.code = NXEXIT_INIT;
|
2011-07-21 02:01:15 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the background to the configured background color */
|
|
|
|
|
|
|
|
color = nximage_bgcolor();
|
2014-11-25 17:32:54 +01:00
|
|
|
printf("nximage_main: Set background color=%u\n", color);
|
2011-07-21 02:01:15 +02:00
|
|
|
|
|
|
|
ret = nx_setbgcolor(g_nximage.hnx, &color);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("nximage_main: nx_setbgcolor failed: %d\n", errno);
|
2011-07-21 02:01:15 +02:00
|
|
|
g_nximage.code = NXEXIT_NXSETBGCOLOR;
|
|
|
|
goto errout_with_nx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the background window */
|
|
|
|
|
2012-04-27 20:29:04 +02:00
|
|
|
ret = nx_requestbkgd(g_nximage.hnx, &g_nximagecb, NULL);
|
2011-07-21 02:01:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("nximage_main: nx_setbgcolor failed: %d\n", errno);
|
2011-07-21 02:01:15 +02:00
|
|
|
g_nximage.code = NXEXIT_NXREQUESTBKGD;
|
|
|
|
goto errout_with_nx;
|
|
|
|
}
|
|
|
|
|
2017-10-16 15:13:38 +02:00
|
|
|
/* Wait until we have the screen resolution. */
|
2011-07-21 02:01:15 +02:00
|
|
|
|
|
|
|
while (!g_nximage.havepos)
|
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
sem_wait(&g_nximage.eventsem);
|
2011-07-21 02:01:15 +02:00
|
|
|
}
|
2017-10-16 15:13:38 +02:00
|
|
|
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("nximage_main: Screen resolution (%d,%d)\n", g_nximage.xres, g_nximage.yres);
|
2011-07-21 02:01:15 +02:00
|
|
|
|
2012-10-20 18:07:49 +02:00
|
|
|
/* Now, put up the NuttX logo and wait a bit so that it visible. */
|
2011-07-21 02:01:15 +02:00
|
|
|
|
|
|
|
nximage_image(g_nximage.hbkgd);
|
2012-10-20 18:07:49 +02:00
|
|
|
sleep(5);
|
2011-07-21 02:01:15 +02:00
|
|
|
|
|
|
|
/* Release background */
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
nx_releasebkgd(g_nximage.hbkgd);
|
2011-07-21 02:01:15 +02:00
|
|
|
|
|
|
|
/* Close NX */
|
|
|
|
|
|
|
|
errout_with_nx:
|
2017-10-14 23:09:43 +02:00
|
|
|
printf("nximage_main: Disconnect from the server\n");
|
|
|
|
nx_disconnect(g_nximage.hnx);
|
|
|
|
|
2011-07-21 02:01:15 +02:00
|
|
|
errout:
|
|
|
|
return g_nximage.code;
|
|
|
|
}
|