2013-02-28 20:23:07 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/examples/screenshot/screenshot_main.c
|
|
|
|
*
|
2022-03-09 15:26:56 +01: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
|
2013-02-28 20:23:07 +01:00
|
|
|
*
|
2022-03-09 15:26:56 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-02-28 20:23:07 +01:00
|
|
|
*
|
2022-03-09 15:26:56 +01: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.
|
2013-02-28 20:23:07 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-04-22 21:38:06 +02:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2019-04-02 15:06:06 +02:00
|
|
|
#include <sys/boardctl.h>
|
2013-02-28 20:23:07 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
2016-04-22 21:38:06 +02:00
|
|
|
#include <semaphore.h>
|
2013-02-28 20:23:07 +01:00
|
|
|
#include <errno.h>
|
|
|
|
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "graphics/tiff.h"
|
2013-02-28 20:23:07 +01:00
|
|
|
|
|
|
|
#include <nuttx/nx/nx.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-Processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_SCREENSHOT_WIDTH
|
|
|
|
# define CONFIG_SCREENSHOT_WIDTH 320
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_SCREENSHOT_HEIGHT
|
|
|
|
# define CONFIG_SCREENSHOT_HEIGHT 240
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_SCREENSHOT_FORMAT
|
|
|
|
# define CONFIG_SCREENSHOT_FORMAT FB_FMT_RGB16_565
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void replace_extension(FAR const char *filename, FAR const char *newext,
|
|
|
|
FAR char *dest, size_t size)
|
|
|
|
{
|
|
|
|
FAR char *p = strrchr(filename, '.');
|
|
|
|
int len = strlen(filename);
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2013-02-28 20:23:07 +01:00
|
|
|
if (p != NULL)
|
|
|
|
{
|
|
|
|
len = p - filename;
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2013-02-28 20:23:07 +01:00
|
|
|
if (len > size)
|
|
|
|
{
|
|
|
|
len = size - strlen(newext);
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2013-02-28 20:23:07 +01:00
|
|
|
strncpy(dest, filename, size);
|
|
|
|
strncpy(dest + len, newext, size - len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: save_screenshot
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Takes a screenshot and saves it to a tif file.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int save_screenshot(FAR const char *filename)
|
|
|
|
{
|
|
|
|
struct tiff_info_s info;
|
2016-04-22 21:38:06 +02:00
|
|
|
struct nx_callback_s cb = {};
|
|
|
|
struct nxgl_size_s size = {CONFIG_SCREENSHOT_WIDTH, CONFIG_SCREENSHOT_HEIGHT};
|
2019-04-02 15:06:06 +02:00
|
|
|
#ifdef CONFIG_VNCSERVER
|
|
|
|
struct boardioc_vncstart_s vnc;
|
|
|
|
#endif
|
2013-02-28 20:23:07 +01:00
|
|
|
FAR uint8_t *strip;
|
|
|
|
NXHANDLE server;
|
|
|
|
NXWINDOW window;
|
2016-04-22 21:38:06 +02:00
|
|
|
char tempf1[64];
|
|
|
|
char tempf2[64];
|
|
|
|
int row;
|
|
|
|
int ret;
|
2013-02-28 20:23:07 +01:00
|
|
|
|
|
|
|
replace_extension(filename, ".tm1", tempf1, sizeof(tempf1));
|
|
|
|
replace_extension(filename, ".tm2", tempf2, sizeof(tempf2));
|
|
|
|
|
|
|
|
/* Connect to NX server */
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2013-02-28 20:23:07 +01:00
|
|
|
server = nx_connect();
|
|
|
|
if (!server)
|
2016-04-22 21:38:06 +02:00
|
|
|
{
|
|
|
|
perror("nx_connect");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_VNCSERVER
|
2019-04-02 15:06:06 +02:00
|
|
|
/* Setup the VNC server to support keyboard/mouse inputs */
|
|
|
|
|
|
|
|
vnc.display = 0;
|
|
|
|
vnc.handle = server;
|
|
|
|
|
|
|
|
ret = boardctl(BOARDIOC_VNC_START, (uintptr_t)&vnc);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
printf("boardctl(BOARDIOC_VNC_START) failed: %d\n", ret);
|
|
|
|
nx_disconnect(server);
|
|
|
|
return 1;
|
|
|
|
}
|
2016-04-22 21:38:06 +02:00
|
|
|
#endif
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2013-02-28 20:23:07 +01:00
|
|
|
/* Wait for "connected" event */
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2013-02-28 20:23:07 +01:00
|
|
|
if (nx_eventhandler(server) < 0)
|
2016-04-22 21:38:06 +02:00
|
|
|
{
|
|
|
|
perror("nx_eventhandler");
|
|
|
|
nx_disconnect(server);
|
|
|
|
return 1;
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2013-02-28 20:23:07 +01:00
|
|
|
/* Open invisible dummy window for communication */
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2019-03-13 19:32:54 +01:00
|
|
|
window = nx_openwindow(server, 0, &cb, NULL);
|
2013-02-28 20:23:07 +01:00
|
|
|
if (!window)
|
|
|
|
{
|
|
|
|
perror("nx_openwindow");
|
|
|
|
nx_disconnect(server);
|
|
|
|
return 1;
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2013-02-28 20:23:07 +01:00
|
|
|
nx_setsize(window, &size);
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2013-02-28 20:23:07 +01:00
|
|
|
/* Configure the TIFF structure */
|
|
|
|
|
|
|
|
memset(&info, 0, sizeof(struct tiff_info_s));
|
|
|
|
info.outfile = filename;
|
|
|
|
info.tmpfile1 = tempf1;
|
|
|
|
info.tmpfile2 = tempf2;
|
|
|
|
info.colorfmt = CONFIG_SCREENSHOT_FORMAT;
|
|
|
|
info.rps = 1;
|
|
|
|
info.imgwidth = size.w;
|
|
|
|
info.imgheight = size.h;
|
|
|
|
info.iobuffer = (uint8_t *)malloc(300);
|
|
|
|
info.iosize = 300;
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2013-02-28 20:23:07 +01:00
|
|
|
/* Initialize the TIFF library */
|
|
|
|
|
|
|
|
ret = tiff_initialize(&info);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
printf("tiff_initialize() failed: %d\n", ret);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add each strip to the TIFF file */
|
|
|
|
|
|
|
|
strip = malloc(size.w * 3);
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2014-11-25 22:14:28 +01:00
|
|
|
for (row = 0; row < size.h; row++)
|
2013-02-28 20:23:07 +01:00
|
|
|
{
|
2014-11-25 22:14:28 +01:00
|
|
|
struct nxgl_rect_s rect = {{0, row}, {size.w - 1, row}};
|
2013-02-28 20:23:07 +01:00
|
|
|
nx_getrectangle(window, &rect, 0, strip, 0);
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2013-02-28 20:23:07 +01:00
|
|
|
ret = tiff_addstrip(&info, strip);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-11-25 22:14:28 +01:00
|
|
|
printf("tiff_addstrip() #%d failed: %d\n", row, ret);
|
2013-02-28 20:23:07 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2013-02-28 20:23:07 +01:00
|
|
|
free(strip);
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2013-02-28 20:23:07 +01:00
|
|
|
/* Then finalize the TIFF file */
|
|
|
|
|
|
|
|
ret = tiff_finalize(&info);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
printf("tiff_finalize() failed: %d\n", ret);
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2013-02-28 20:23:07 +01:00
|
|
|
free(info.iobuffer);
|
|
|
|
nx_closewindow(window);
|
|
|
|
nx_disconnect(server);
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2013-02-28 20:23:07 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: screenshot_main
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Main entry point for the screenshot NSH application.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-06 17:23:23 +02:00
|
|
|
int main(int argc, FAR char *argv[])
|
2013-02-28 20:23:07 +01:00
|
|
|
{
|
|
|
|
if (argc != 2)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Usage: screenshot file.tif\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2013-02-28 20:23:07 +01:00
|
|
|
return save_screenshot(argv[1]);
|
|
|
|
}
|