2015-04-13 04:09:02 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
2015-03-17 18:56:15 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
2015-03-28 06:53:44 -05:00
|
|
|
#include <cstdlib>
|
2015-04-09 17:25:48 -05:00
|
|
|
#include <cstring>
|
2015-03-17 18:56:15 -05:00
|
|
|
#include <string>
|
|
|
|
|
2015-03-28 06:53:44 -05:00
|
|
|
#include <png.h>
|
|
|
|
|
2015-04-24 04:49:19 -05:00
|
|
|
#include <signal.h>
|
|
|
|
|
2015-04-22 03:14:11 -05:00
|
|
|
#include <Poco/Exception.h>
|
2015-04-27 06:16:37 -05:00
|
|
|
#include <Poco/Format.h>
|
2015-04-22 03:14:11 -05:00
|
|
|
#include <Poco/Net/WebSocket.h>
|
2015-03-17 18:56:15 -05:00
|
|
|
#include <Poco/Process.h>
|
2015-04-27 06:16:37 -05:00
|
|
|
#include <Poco/Timestamp.h>
|
2015-03-17 18:56:15 -05:00
|
|
|
#include <Poco/Thread.h>
|
2015-04-22 03:14:11 -05:00
|
|
|
#include <Poco/Util/Application.h>
|
2015-03-17 18:56:15 -05:00
|
|
|
|
|
|
|
#include "Util.hpp"
|
|
|
|
|
2015-03-28 06:53:44 -05:00
|
|
|
// Callback functions for libpng
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
static void user_write_status_fn(png_structp, png_uint_32, int)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void user_write_fn(png_structp png_ptr, png_bytep data, png_size_t length)
|
|
|
|
{
|
|
|
|
std::vector<char> *outputp = (std::vector<char> *) png_get_io_ptr(png_ptr);
|
|
|
|
size_t oldsize = outputp->size();
|
|
|
|
outputp->resize(oldsize + length);
|
|
|
|
memcpy(outputp->data() + oldsize, data, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void user_flush_fn(png_structp)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 10:07:52 -05:00
|
|
|
namespace Util
|
2015-03-17 18:56:15 -05:00
|
|
|
{
|
2015-03-26 10:07:52 -05:00
|
|
|
|
|
|
|
std::string logPrefix()
|
|
|
|
{
|
2015-04-27 06:16:37 -05:00
|
|
|
static Poco::Timestamp firstTimeStamp;
|
|
|
|
Poco::Int64 now = firstTimeStamp.elapsed();
|
|
|
|
return std::to_string(Poco::Process::id()) + ":" + (Poco::Thread::current() ? std::to_string(Poco::Thread::current()->id()) : "0") + ":" + std::to_string(now / 1000000) + "." + Poco::format("%03d", (int)((now / 1000) % 1000)) + ": ";
|
2015-03-26 10:07:52 -05:00
|
|
|
}
|
2015-03-28 06:53:44 -05:00
|
|
|
|
|
|
|
bool windowingAvailable()
|
|
|
|
{
|
|
|
|
#ifdef __linux
|
|
|
|
return std::getenv("DISPLAY") != NULL;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-10 04:39:12 -05:00
|
|
|
bool encodePNGAndAppendToBuffer(unsigned char *pixmap, int width, int height, std::vector<char>& output)
|
2015-03-28 06:53:44 -05:00
|
|
|
{
|
|
|
|
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
|
|
|
|
|
|
|
png_infop info_ptr = png_create_info_struct(png_ptr);
|
|
|
|
|
|
|
|
if (setjmp(png_jmpbuf(png_ptr)))
|
|
|
|
{
|
|
|
|
png_destroy_write_struct(&png_ptr, NULL);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
|
|
|
|
|
|
|
|
png_set_write_fn(png_ptr, &output, user_write_fn, user_flush_fn);
|
|
|
|
png_set_write_status_fn(png_ptr, user_write_status_fn);
|
|
|
|
|
|
|
|
png_write_info(png_ptr, info_ptr);
|
|
|
|
|
|
|
|
for (int y = 0; y < height; ++y)
|
|
|
|
png_write_row(png_ptr, pixmap + y * width * 4);
|
|
|
|
|
|
|
|
png_write_end(png_ptr, info_ptr);
|
|
|
|
|
|
|
|
png_destroy_write_struct(&png_ptr, NULL);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2015-04-22 03:14:11 -05:00
|
|
|
|
|
|
|
void shutdownWebSocket(Poco::Net::WebSocket& ws)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ws.shutdown();
|
|
|
|
}
|
|
|
|
catch (Poco::IOException& exc)
|
|
|
|
{
|
|
|
|
Poco::Util::Application::instance().logger().error(logPrefix() + "IOException: " + exc.message());
|
|
|
|
}
|
|
|
|
}
|
2015-04-24 04:49:19 -05:00
|
|
|
|
|
|
|
std::string signalName(int signo)
|
|
|
|
{
|
|
|
|
switch (signo)
|
|
|
|
{
|
|
|
|
#define CASE(x) case SIG##x: return #x
|
|
|
|
CASE(HUP);
|
|
|
|
CASE(INT);
|
|
|
|
CASE(QUIT);
|
|
|
|
CASE(ILL);
|
|
|
|
CASE(ABRT);
|
|
|
|
CASE(FPE);
|
|
|
|
CASE(KILL);
|
|
|
|
CASE(SEGV);
|
2015-05-04 12:52:28 -05:00
|
|
|
CASE(PIPE);
|
2015-04-24 04:49:19 -05:00
|
|
|
CASE(ALRM);
|
|
|
|
CASE(TERM);
|
|
|
|
CASE(USR1);
|
|
|
|
CASE(USR2);
|
|
|
|
CASE(CHLD);
|
|
|
|
CASE(CONT);
|
|
|
|
CASE(STOP);
|
|
|
|
CASE(TSTP);
|
|
|
|
CASE(TTIN);
|
|
|
|
CASE(TTOU);
|
|
|
|
CASE(BUS);
|
2015-05-04 12:52:28 -05:00
|
|
|
#ifdef SIGPOLL
|
2015-04-24 04:49:19 -05:00
|
|
|
CASE(POLL);
|
2015-05-04 12:52:28 -05:00
|
|
|
#endif
|
2015-04-24 04:49:19 -05:00
|
|
|
CASE(PROF);
|
|
|
|
CASE(SYS);
|
|
|
|
CASE(TRAP);
|
|
|
|
CASE(URG);
|
|
|
|
CASE(VTALRM);
|
|
|
|
CASE(XCPU);
|
|
|
|
CASE(XFSZ);
|
|
|
|
#ifdef SIGEMT
|
|
|
|
CASE(EMT);
|
|
|
|
#endif
|
2015-05-04 12:52:28 -05:00
|
|
|
#ifdef SIGSTKFLT
|
2015-04-24 04:49:19 -05:00
|
|
|
CASE(STKFLT);
|
2015-05-04 12:52:28 -05:00
|
|
|
#endif
|
2015-04-24 04:49:19 -05:00
|
|
|
#if defined(SIGIO) && SIGIO != SIGPOLL
|
|
|
|
CASE(IO);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPWR
|
|
|
|
CASE(PWR);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGLOST
|
|
|
|
CASE(LOST);
|
|
|
|
#endif
|
|
|
|
CASE(WINCH);
|
2015-05-04 12:52:28 -05:00
|
|
|
#if defined(SIGINFO) && SIGINFO != SIGPWR
|
|
|
|
CASE(INFO);
|
|
|
|
#endif
|
2015-04-24 04:49:19 -05:00
|
|
|
#undef CASE
|
|
|
|
default:
|
|
|
|
return std::to_string(signo);
|
|
|
|
}
|
|
|
|
}
|
2015-03-17 18:56:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|