summaryrefslogtreecommitdiff
path: root/loolwsd/LOKitClient.cpp
blob: a6d0d3e96e4ce9ec4e0b40d6449f29be6257393d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * 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/.
 */

#include <cassert>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <memory>

#define LOK_USE_UNSTABLE_API
#include <LibreOfficeKit/LibreOfficeKit.h>
#include <LibreOfficeKit/LibreOfficeKitInit.h>

#include <png.h>

#include <Poco/Buffer.h>
#include <Poco/Process.h>
#include <Poco/Random.h>
#include <Poco/String.h>
#include <Poco/StringTokenizer.h>
#include <Poco/TemporaryFile.h>
#include <Poco/URI.h>
#include <Poco/Util/Application.h>

#include "LOKitHelper.hpp"
#include "Util.hpp"

using Poco::StringTokenizer;
using Poco::TemporaryFile;
using Poco::Util::Application;

extern "C"
{
    static void myCallback(int /*nType*/, const char* /*pPayload*/, void*)
    {
#if 0
        std::cout << "Callback: ";
        switch ((LibreOfficeKitCallbackType) nType)
        {
#define CASE(x) case LOK_CALLBACK_##x: std::cout << #x; break
            CASE(INVALIDATE_TILES);
            CASE(INVALIDATE_VISIBLE_CURSOR);
            CASE(TEXT_SELECTION);
            CASE(TEXT_SELECTION_START);
            CASE(TEXT_SELECTION_END);
            CASE(CURSOR_VISIBLE);
            CASE(GRAPHIC_SELECTION);
            CASE(CELL_CURSOR);
            CASE(CELL_FORMULA);
            CASE(HYPERLINK_CLICKED);
            CASE(MOUSE_POINTER);
            CASE(STATE_CHANGED);
            CASE(STATUS_INDICATOR_START);
            CASE(STATUS_INDICATOR_SET_VALUE);
            CASE(STATUS_INDICATOR_FINISH);
            CASE(SEARCH_NOT_FOUND);
            CASE(SEARCH_RESULT_SELECTION);
            CASE(DOCUMENT_SIZE_CHANGED);
            CASE(SET_PART);
            CASE(UNO_COMMAND_RESULT);
#undef CASE
        }
        std::cout << " payload: " << pPayload << std::endl;
#endif
    }
}


class LOKitClient: public Application
{
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");
            return Application::EXIT_USAGE;
        }

        LibreOfficeKit *loKit;
        LibreOfficeKitDocument *loKitDocument;

        loKit = lok_init(args[0].c_str());
        if (!loKit)
        {
            logger().fatal("LibreOfficeKit initialisation failed");
            return Application::EXIT_UNAVAILABLE;
        }


        loKitDocument = loKit->pClass->documentLoad(loKit, args[1].c_str());
        if (!loKitDocument)
        {
            logger().fatal("Document loading failed: " + std::string(loKit->pClass->getError(loKit)));
            return Application::EXIT_UNAVAILABLE;
        }

        loKitDocument->pClass->registerCallback(loKitDocument, myCallback, NULL);

        loKitDocument->pClass->initializeForRendering(loKitDocument);

        if (isatty(0))
        {
            std::cout << "Enter LOKit \"commands\", one per line. 'help' for help. EOF to finish." << std::endl;
        }

        while (!std::cin.eof())
        {
            std::string line;
            std::getline(std::cin, line);

            StringTokenizer tokens(line, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);

            if (tokens.count() == 0)
                continue;

            if (tokens[0] == "?" || tokens[0] == "help")
            {
                std::cout <<
                    "Commands mimic LOOL protocol but we talk directly to LOKit:" << std::endl <<
                    "    status" << std::endl <<
                    "        calls LibreOfficeKitDocument::getDocumentType, getParts, getPartName, getDocumentSize" << std::endl <<
                    "    tile part pixelwidth pixelheight docposx docposy doctilewidth doctileheight" << std::endl <<
                    "        calls LibreOfficeKitDocument::paintTile" << std::endl;
            }
            else if (tokens[0] == "status")
            {
                if (tokens.count() != 1)
                {
                    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")
            {
                if (tokens.count() != 8)
                {
                    std::cout << "? syntax" << std::endl;
                    continue;
                }

                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]));

                std::vector<unsigned char> pixmap(canvasWidth*canvasHeight*4);
                loKitDocument->pClass->setPart(loKitDocument, partNumber);
                loKitDocument->pClass->paintTile(loKitDocument, pixmap.data(), canvasWidth, canvasHeight, tilePosX, tilePosY, tileWidth, tileHeight);

                if (!Util::windowingAvailable())
                    continue;

                std::vector<char> png;
                Util::encodePNGAndAppendToBuffer(pixmap.data(), canvasWidth, canvasHeight, png);

                TemporaryFile pngFile;
                std::ofstream pngStream(pngFile.path(), std::ios::binary);
                pngStream.write(png.data(), png.size());
                pngStream.close();
#ifdef __linux
                if (std::getenv("DISPLAY") != nullptr)
                {
                    if (std::system((std::string("display ") + pngFile.path()).c_str()) == -1)
                    {
                        // 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...
                    }
                }
#endif
            }
            else
            {
                std::cout << "? unrecognized" << std::endl;
            }
        }

        // Safest to just bluntly exit
        _Exit(Application::EXIT_OK);
    }
};

POCO_APP_MAIN(LOKitClient)

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */