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-28 06:22:32 -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/.
|
|
|
|
*/
|
|
|
|
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <config.h>
|
2017-03-08 10:38:22 -06:00
|
|
|
|
2015-03-28 06:22:32 -05:00
|
|
|
#include <cassert>
|
2015-03-28 06:53:44 -05:00
|
|
|
#include <cstdlib>
|
2015-03-28 06:22:32 -05:00
|
|
|
#include <cstring>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <memory>
|
2019-11-06 03:07:32 -06:00
|
|
|
#include <sysexits.h>
|
2015-03-28 06:22:32 -05:00
|
|
|
|
|
|
|
#define LOK_USE_UNSTABLE_API
|
|
|
|
#include <LibreOfficeKit/LibreOfficeKitInit.h>
|
|
|
|
|
|
|
|
#include <Poco/Buffer.h>
|
|
|
|
#include <Poco/Process.h>
|
|
|
|
#include <Poco/String.h>
|
2015-03-28 06:53:44 -05:00
|
|
|
#include <Poco/TemporaryFile.h>
|
2015-03-28 06:22:32 -05:00
|
|
|
#include <Poco/URI.h>
|
|
|
|
#include <Poco/Util/Application.h>
|
|
|
|
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <KitHelper.hpp>
|
|
|
|
#include <Png.hpp>
|
|
|
|
#include <Util.hpp>
|
2019-11-13 01:35:39 -06:00
|
|
|
#include <Protocol.hpp>
|
2015-03-28 06:22:32 -05:00
|
|
|
|
2015-03-28 06:53:44 -05:00
|
|
|
using Poco::TemporaryFile;
|
2015-03-28 06:22:32 -05:00
|
|
|
using Poco::Util::Application;
|
|
|
|
|
2015-06-12 08:58:49 -05:00
|
|
|
extern "C"
|
|
|
|
{
|
2017-01-13 06:52:08 -06:00
|
|
|
static void myCallback(int type, const char* payload, void*)
|
2015-06-12 08:58:49 -05:00
|
|
|
{
|
2019-08-16 08:24:38 -05:00
|
|
|
std::cout << "Callback: " << lokCallbackTypeToString(type)
|
|
|
|
<< " payload: " << payload << std::endl;
|
2015-06-12 08:58:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-13 23:01:13 -05:00
|
|
|
/// The application class implementing a client.
|
2015-03-28 06:53:44 -05:00
|
|
|
class LOKitClient: public Application
|
2015-03-28 06:22:32 -05:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
protected:
|
|
|
|
int main(const std::vector<std::string>& args) override
|
|
|
|
{
|
|
|
|
if (args.size() != 2)
|
|
|
|
{
|
|
|
|
logger().fatal("Usage: lokitclient /path/to/lo/installation/program /path/to/document");
|
2019-11-06 03:07:32 -06:00
|
|
|
return EX_USAGE;
|
2015-03-28 06:22:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
LibreOfficeKit *loKit;
|
|
|
|
LibreOfficeKitDocument *loKitDocument;
|
|
|
|
|
|
|
|
loKit = lok_init(args[0].c_str());
|
|
|
|
if (!loKit)
|
|
|
|
{
|
|
|
|
logger().fatal("LibreOfficeKit initialisation failed");
|
2019-11-06 03:07:32 -06:00
|
|
|
return EX_UNAVAILABLE;
|
2015-03-28 06:22:32 -05:00
|
|
|
}
|
|
|
|
|
2015-06-12 08:58:49 -05:00
|
|
|
|
2015-03-28 06:22:32 -05:00
|
|
|
loKitDocument = loKit->pClass->documentLoad(loKit, args[1].c_str());
|
|
|
|
if (!loKitDocument)
|
|
|
|
{
|
|
|
|
logger().fatal("Document loading failed: " + std::string(loKit->pClass->getError(loKit)));
|
2019-11-06 03:07:32 -06:00
|
|
|
return EX_UNAVAILABLE;
|
2015-03-28 06:22:32 -05:00
|
|
|
}
|
|
|
|
|
2016-09-10 13:51:59 -05:00
|
|
|
loKitDocument->pClass->registerCallback(loKitDocument, myCallback, nullptr);
|
2015-06-12 08:58:49 -05:00
|
|
|
|
2015-11-18 09:59:28 -06:00
|
|
|
loKitDocument->pClass->initializeForRendering(loKitDocument, nullptr);
|
2015-03-28 06:53:44 -05:00
|
|
|
|
2015-03-28 06:22:32 -05:00
|
|
|
if (isatty(0))
|
|
|
|
{
|
2015-05-23 06:02:05 -05:00
|
|
|
std::cout << "Enter LOKit \"commands\", one per line. 'help' for help. EOF to finish." << std::endl;
|
2015-03-28 06:22:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
while (!std::cin.eof())
|
|
|
|
{
|
|
|
|
std::string line;
|
|
|
|
std::getline(std::cin, line);
|
|
|
|
|
2019-11-13 01:35:39 -06:00
|
|
|
std::vector<std::string> tokens(LOOLProtocol::tokenize(line, ' '));
|
2015-03-28 06:22:32 -05:00
|
|
|
|
2019-11-13 01:35:39 -06:00
|
|
|
if (tokens.size() == 0)
|
2015-03-28 06:22:32 -05:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (tokens[0] == "?" || tokens[0] == "help")
|
|
|
|
{
|
2015-10-05 08:44:40 -05:00
|
|
|
std::cout <<
|
2015-03-28 06:22:32 -05:00
|
|
|
"Commands mimic LOOL protocol but we talk directly to LOKit:" << std::endl <<
|
|
|
|
" status" << std::endl <<
|
|
|
|
" calls LibreOfficeKitDocument::getDocumentType, getParts, getPartName, getDocumentSize" << std::endl <<
|
2015-05-23 06:01:24 -05:00
|
|
|
" tile part pixelwidth pixelheight docposx docposy doctilewidth doctileheight" << std::endl <<
|
2015-03-28 06:22:32 -05:00
|
|
|
" calls LibreOfficeKitDocument::paintTile" << std::endl;
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "status")
|
|
|
|
{
|
2019-11-13 01:35:39 -06:00
|
|
|
if (tokens.size() != 1)
|
2015-03-28 06:22:32 -05:00
|
|
|
{
|
|
|
|
std::cout << "? syntax" << std::endl;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
std::cout << LOKitHelper::documentStatus(loKitDocument) << std::endl;
|
|
|
|
for (int i = 0; i < loKitDocument->pClass->getParts(loKitDocument); i++)
|
|
|
|
{
|
|
|
|
std::cout << " " << i << ": '" << loKitDocument->pClass->getPartName(loKitDocument, i) << "'" << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "tile")
|
|
|
|
{
|
2019-11-13 01:35:39 -06:00
|
|
|
if (tokens.size() != 8)
|
2015-03-28 06:22:32 -05:00
|
|
|
{
|
|
|
|
std::cout << "? syntax" << std::endl;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-05-23 06:01:24 -05:00
|
|
|
int partNumber(std::stoi(tokens[1]));
|
|
|
|
int canvasWidth(std::stoi(tokens[2]));
|
|
|
|
int canvasHeight(std::stoi(tokens[3]));
|
|
|
|
int tilePosX(std::stoi(tokens[4]));
|
|
|
|
int tilePosY(std::stoi(tokens[5]));
|
|
|
|
int tileWidth(std::stoi(tokens[6]));
|
|
|
|
int tileHeight(std::stoi(tokens[7]));
|
2015-10-05 08:44:40 -05:00
|
|
|
|
2015-03-28 06:53:44 -05:00
|
|
|
std::vector<unsigned char> pixmap(canvasWidth*canvasHeight*4);
|
2015-05-23 06:01:24 -05:00
|
|
|
loKitDocument->pClass->setPart(loKitDocument, partNumber);
|
2015-03-28 06:53:44 -05:00
|
|
|
loKitDocument->pClass->paintTile(loKitDocument, pixmap.data(), canvasWidth, canvasHeight, tilePosX, tilePosY, tileWidth, tileHeight);
|
|
|
|
|
|
|
|
if (!Util::windowingAvailable())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::vector<char> png;
|
2016-05-21 10:31:04 -05:00
|
|
|
const auto mode = static_cast<LibreOfficeKitTileMode>(loKitDocument->pClass->getTileMode(loKitDocument));
|
2016-01-12 05:00:42 -06:00
|
|
|
|
2017-01-04 05:35:26 -06:00
|
|
|
Png::encodeBufferToPNG(pixmap.data(), canvasWidth, canvasHeight, png, mode);
|
2015-03-28 06:22:32 -05:00
|
|
|
|
2015-03-28 06:53:44 -05:00
|
|
|
TemporaryFile pngFile;
|
|
|
|
std::ofstream pngStream(pngFile.path(), std::ios::binary);
|
|
|
|
pngStream.write(png.data(), png.size());
|
|
|
|
pngStream.close();
|
2016-02-29 06:25:12 -06:00
|
|
|
|
2017-06-06 22:49:52 -05:00
|
|
|
if (std::system((std::string("display ") + pngFile.path()).c_str()) == -1)
|
2015-04-10 04:51:42 -05:00
|
|
|
{
|
2017-06-06 22:49:52 -05:00
|
|
|
// Not worth it to display a warning, this is just a throwaway test program, and
|
|
|
|
// the developer running it surely notices if nothing shows up...
|
2015-04-10 04:51:42 -05:00
|
|
|
}
|
2015-03-28 06:22:32 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cout << "? unrecognized" << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-02 17:02:10 -05:00
|
|
|
// Safest to just bluntly exit
|
2019-11-06 03:07:32 -06:00
|
|
|
std::_Exit(EX_OK);
|
2015-03-28 06:22:32 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-09-28 14:07:07 -05:00
|
|
|
namespace Util
|
|
|
|
{
|
|
|
|
|
|
|
|
void alertAllUsers(const std::string& cmd, const std::string& kind)
|
|
|
|
{
|
|
|
|
std::cout << "error: cmd=" << cmd << " kind=" << kind << std::endl;
|
|
|
|
(void) kind;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-28 06:22:32 -05:00
|
|
|
POCO_APP_MAIN(LOKitClient)
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|