2015-11-09 04:36:37 -06:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* 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-11-09 04:36:37 -06:00
|
|
|
#include "MessageQueue.hpp"
|
2020-04-18 05:55:50 -05:00
|
|
|
#include <climits>
|
2015-11-09 04:36:37 -06:00
|
|
|
#include <algorithm>
|
2021-05-12 11:55:16 -05:00
|
|
|
#include <iostream>
|
2015-11-09 04:36:37 -06:00
|
|
|
|
2017-01-20 08:56:47 -06:00
|
|
|
#include <Poco/JSON/JSON.h>
|
|
|
|
#include <Poco/JSON/Object.h>
|
|
|
|
#include <Poco/JSON/Parser.h>
|
2016-09-20 18:59:06 -05:00
|
|
|
|
2017-12-20 07:06:26 -06:00
|
|
|
#include "Protocol.hpp"
|
|
|
|
#include "Log.hpp"
|
Get rid of some fragile string literal length counts
Basically, instead of foo.compare(0, 3, "bar") use
LOOLProtocol::getFirstToken(foo)!="bar". Using a separate length of a
string literal is very fragile, it is easy to get the length wrong, or
forget to change it in case the string changes. See
137e677eb0d6af1996701b2bd1b27a6eb3822613.
It isn't entirely sure to me whether was is kept in a message queue
follows LOOL protocol message syntax, but it seems to be so in
practice, so we can use LOOLProtocol::getFirstToken().
Also, calls like msg.compare(0, 4, "tile") add undocumented implicit
requirements that the token in question must be unique as an initial
substring of the first tokens of all messages. It actually isn't,
there is also "tilecombine". But in this case, that was taken care of
by checking for "tilecombine" first. Ugh. Whether that was by accident
or design is hard to say, but at least there was no comment indicating
intentionality.
We still have lots of similar fragile mis-use of std::string::find().
2016-09-26 12:13:50 -05:00
|
|
|
#include <TileDesc.hpp>
|
2016-09-01 15:45:04 -05:00
|
|
|
|
2016-03-26 21:32:29 -05:00
|
|
|
void TileQueue::put_impl(const Payload& value)
|
2015-11-09 04:36:37 -06:00
|
|
|
{
|
2021-11-18 06:08:14 -06:00
|
|
|
const std::string firstToken = COOLProtocol::getFirstToken(value);
|
2016-09-20 18:59:06 -05:00
|
|
|
|
2023-06-10 11:54:23 -05:00
|
|
|
if (firstToken == "tilecombine")
|
2016-09-25 15:04:27 -05:00
|
|
|
{
|
2016-09-26 03:35:20 -05:00
|
|
|
// Breakup tilecombine and deduplicate (we are re-combining the tiles
|
|
|
|
// in the get_impl() again)
|
2021-03-07 16:14:19 -06:00
|
|
|
const std::string msg = std::string(value.data(), value.size());
|
2018-02-07 03:17:19 -06:00
|
|
|
const TileCombined tileCombined = TileCombined::parse(msg);
|
2021-03-07 16:14:19 -06:00
|
|
|
for (const auto& tile : tileCombined.getTiles())
|
2016-09-25 15:04:27 -05:00
|
|
|
{
|
2016-09-26 03:35:20 -05:00
|
|
|
const std::string newMsg = tile.serialize("tile");
|
|
|
|
|
2017-01-20 08:56:47 -06:00
|
|
|
removeTileDuplicate(newMsg);
|
2016-09-26 03:35:20 -05:00
|
|
|
|
|
|
|
MessageQueue::put_impl(Payload(newMsg.data(), newMsg.data() + newMsg.size()));
|
2016-09-25 15:04:27 -05:00
|
|
|
}
|
2016-09-26 03:35:20 -05:00
|
|
|
return;
|
2016-09-25 15:04:27 -05:00
|
|
|
}
|
Get rid of some fragile string literal length counts
Basically, instead of foo.compare(0, 3, "bar") use
LOOLProtocol::getFirstToken(foo)!="bar". Using a separate length of a
string literal is very fragile, it is easy to get the length wrong, or
forget to change it in case the string changes. See
137e677eb0d6af1996701b2bd1b27a6eb3822613.
It isn't entirely sure to me whether was is kept in a message queue
follows LOOL protocol message syntax, but it seems to be so in
practice, so we can use LOOLProtocol::getFirstToken().
Also, calls like msg.compare(0, 4, "tile") add undocumented implicit
requirements that the token in question must be unique as an initial
substring of the first tokens of all messages. It actually isn't,
there is also "tilecombine". But in this case, that was taken care of
by checking for "tilecombine" first. Ugh. Whether that was by accident
or design is hard to say, but at least there was no comment indicating
intentionality.
We still have lots of similar fragile mis-use of std::string::find().
2016-09-26 12:13:50 -05:00
|
|
|
else if (firstToken == "tile")
|
2016-09-26 03:35:20 -05:00
|
|
|
{
|
2021-03-07 16:14:19 -06:00
|
|
|
removeTileDuplicate(std::string(value.data(), value.size()));
|
2016-09-23 03:18:54 -05:00
|
|
|
|
2016-09-26 03:35:20 -05:00
|
|
|
MessageQueue::put_impl(value);
|
|
|
|
return;
|
|
|
|
}
|
2017-01-20 08:56:47 -06:00
|
|
|
else if (firstToken == "callback")
|
|
|
|
{
|
2021-03-07 16:14:19 -06:00
|
|
|
const std::string newMsg = removeCallbackDuplicate(std::string(value.data(), value.size()));
|
2017-01-25 11:21:56 -06:00
|
|
|
|
|
|
|
if (newMsg.empty())
|
2017-01-29 15:29:18 -06:00
|
|
|
{
|
2017-01-25 11:21:56 -06:00
|
|
|
MessageQueue::put_impl(value);
|
2017-01-29 15:29:18 -06:00
|
|
|
}
|
2017-01-25 11:21:56 -06:00
|
|
|
else
|
|
|
|
{
|
|
|
|
MessageQueue::put_impl(Payload(newMsg.data(), newMsg.data() + newMsg.size()));
|
|
|
|
}
|
2016-09-26 03:35:20 -05:00
|
|
|
|
2017-01-20 08:56:47 -06:00
|
|
|
return;
|
|
|
|
}
|
2016-09-26 03:35:20 -05:00
|
|
|
|
|
|
|
MessageQueue::put_impl(value);
|
|
|
|
}
|
|
|
|
|
2017-01-20 08:56:47 -06:00
|
|
|
void TileQueue::removeTileDuplicate(const std::string& tileMsg)
|
2016-09-26 03:35:20 -05:00
|
|
|
{
|
2021-11-18 06:08:14 -06:00
|
|
|
assert(COOLProtocol::matchPrefix("tile", tileMsg, /*ignoreWhitespace*/ true));
|
2016-09-26 03:35:20 -05:00
|
|
|
|
2016-11-22 23:21:53 -06:00
|
|
|
// Ver is always provided at this point and it is necessary to
|
|
|
|
// return back to clients the last rendered version of a tile
|
|
|
|
// in case there are new invalidations and requests while rendering.
|
|
|
|
// Here we compare duplicates without 'ver' since that's irrelevant.
|
2018-02-07 03:17:19 -06:00
|
|
|
size_t newMsgPos = tileMsg.find(" ver");
|
2016-11-22 23:21:53 -06:00
|
|
|
if (newMsgPos == std::string::npos)
|
|
|
|
{
|
|
|
|
newMsgPos = tileMsg.size() - 1;
|
|
|
|
}
|
2016-09-26 03:35:20 -05:00
|
|
|
|
2018-11-16 02:06:08 -06:00
|
|
|
for (size_t i = 0; i < getQueue().size(); ++i)
|
2015-11-09 04:36:37 -06:00
|
|
|
{
|
2018-11-16 02:06:08 -06:00
|
|
|
auto& it = getQueue()[i];
|
2016-11-22 23:21:53 -06:00
|
|
|
if (it.size() > newMsgPos &&
|
|
|
|
strncmp(tileMsg.data(), it.data(), newMsgPos) == 0)
|
2015-11-09 04:36:37 -06:00
|
|
|
{
|
2021-11-18 06:08:14 -06:00
|
|
|
LOG_TRC("Remove duplicate tile request: " << std::string(it.data(), it.size()) << " -> " << COOLProtocol::getAbbreviatedMessage(tileMsg));
|
2018-11-16 02:06:08 -06:00
|
|
|
getQueue().erase(getQueue().begin() + i);
|
2016-09-26 03:35:20 -05:00
|
|
|
break;
|
2015-11-09 04:36:37 -06:00
|
|
|
}
|
|
|
|
}
|
2016-09-14 16:42:50 -05:00
|
|
|
}
|
|
|
|
|
2017-01-20 08:56:47 -06:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// Read the viewId from the tokens.
|
2020-02-28 03:50:58 -06:00
|
|
|
std::string extractViewId(const std::string& origMsg, const StringVector& tokens)
|
2017-01-20 08:56:47 -06:00
|
|
|
{
|
|
|
|
size_t nonJson = tokens[0].size() + tokens[1].size() + tokens[2].size() + 3; // including spaces
|
|
|
|
std::string jsonString(origMsg.data() + nonJson, origMsg.size() - nonJson);
|
|
|
|
|
|
|
|
Poco::JSON::Parser parser;
|
2018-02-07 03:17:19 -06:00
|
|
|
const Poco::Dynamic::Var result = parser.parse(jsonString);
|
2017-01-20 08:56:47 -06:00
|
|
|
const auto& json = result.extract<Poco::JSON::Object::Ptr>();
|
|
|
|
return json->get("viewId").toString();
|
|
|
|
}
|
|
|
|
|
2017-01-25 05:21:38 -06:00
|
|
|
/// Extract the .uno: command ID from the potential command.
|
|
|
|
std::string extractUnoCommand(const std::string& command)
|
|
|
|
{
|
2021-11-18 06:08:14 -06:00
|
|
|
if (!COOLProtocol::matchPrefix(".uno:", command))
|
2017-01-25 05:21:38 -06:00
|
|
|
return std::string();
|
|
|
|
|
|
|
|
size_t equalPos = command.find('=');
|
|
|
|
if (equalPos != std::string::npos)
|
|
|
|
return command.substr(0, equalPos);
|
|
|
|
|
|
|
|
return command;
|
|
|
|
}
|
|
|
|
|
2023-06-06 07:53:42 -05:00
|
|
|
bool containsUnoCommand(const std::string_view token, const std::string_view command)
|
|
|
|
{
|
|
|
|
if (!COOLProtocol::matchPrefix(".uno:", token))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
size_t equalPos = token.find('=');
|
|
|
|
if (equalPos != std::string::npos)
|
|
|
|
return token.substr(0, equalPos) == command;
|
|
|
|
|
|
|
|
return token == command;
|
|
|
|
}
|
|
|
|
|
2017-01-25 11:21:56 -06:00
|
|
|
/// Extract rectangle from the invalidation callback
|
2022-08-30 00:27:44 -05:00
|
|
|
bool extractRectangle(const StringVector& tokens, int& x, int& y, int& w, int& h, int& part, int& mode)
|
2017-01-25 11:21:56 -06:00
|
|
|
{
|
|
|
|
x = 0;
|
|
|
|
y = 0;
|
|
|
|
w = INT_MAX;
|
|
|
|
h = INT_MAX;
|
|
|
|
part = 0;
|
2022-08-30 00:27:44 -05:00
|
|
|
mode = 0;
|
2017-01-25 11:21:56 -06:00
|
|
|
|
2017-01-25 12:42:36 -06:00
|
|
|
if (tokens.size() < 5)
|
2017-01-25 11:21:56 -06:00
|
|
|
return false;
|
|
|
|
|
2020-06-02 22:17:04 -05:00
|
|
|
if (tokens.equals(3, "EMPTY,"))
|
2017-01-25 11:21:56 -06:00
|
|
|
{
|
|
|
|
part = std::atoi(tokens[4].c_str());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tokens.size() < 8)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
x = std::atoi(tokens[3].c_str());
|
|
|
|
y = std::atoi(tokens[4].c_str());
|
|
|
|
w = std::atoi(tokens[5].c_str());
|
|
|
|
h = std::atoi(tokens[6].c_str());
|
|
|
|
part = std::atoi(tokens[7].c_str());
|
|
|
|
|
2022-08-30 00:27:44 -05:00
|
|
|
if (tokens.size() == 9)
|
|
|
|
mode = std::atoi(tokens[8].c_str());
|
|
|
|
|
2017-01-25 11:21:56 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-06-03 14:10:29 -05:00
|
|
|
class isDuplicateCommand
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
const std::string& m_unoCommand;
|
|
|
|
const StringVector& m_tokens;
|
|
|
|
bool m_is_duplicate_command;
|
|
|
|
public:
|
|
|
|
isDuplicateCommand(const std::string& unoCommand, const StringVector& tokens)
|
|
|
|
: m_unoCommand(unoCommand)
|
|
|
|
, m_tokens(tokens)
|
|
|
|
, m_is_duplicate_command(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get_is_duplicate_command() const
|
|
|
|
{
|
|
|
|
return m_is_duplicate_command;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
m_is_duplicate_command = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator()(size_t nIndex, std::string_view token)
|
|
|
|
{
|
|
|
|
switch (nIndex)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
// returns true to end tokenization as one of first 3 token doesn't match
|
|
|
|
return token != m_tokens[nIndex];
|
|
|
|
case 3:
|
|
|
|
// callback, the same target, state changed; now check it's
|
|
|
|
// the same .uno: command
|
2023-06-06 07:53:42 -05:00
|
|
|
m_is_duplicate_command = containsUnoCommand(token, m_unoCommand);
|
2023-06-03 14:10:29 -05:00
|
|
|
// returns true to end tokenization as 4 is all we need
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-01-20 08:56:47 -06:00
|
|
|
}
|
|
|
|
|
2017-01-25 11:21:56 -06:00
|
|
|
std::string TileQueue::removeCallbackDuplicate(const std::string& callbackMsg)
|
2017-01-20 08:56:47 -06:00
|
|
|
{
|
2021-11-18 06:08:14 -06:00
|
|
|
assert(COOLProtocol::matchPrefix("callback", callbackMsg, /*ignoreWhitespace*/ true));
|
2017-01-20 08:56:47 -06:00
|
|
|
|
2022-03-29 20:37:57 -05:00
|
|
|
StringVector tokens = StringVector::tokenize(callbackMsg);
|
2017-01-20 08:56:47 -06:00
|
|
|
|
|
|
|
if (tokens.size() < 3)
|
2017-01-25 11:21:56 -06:00
|
|
|
return std::string();
|
2017-01-20 08:56:47 -06:00
|
|
|
|
|
|
|
// the message is "callback <view> <id> ..."
|
2020-06-02 22:16:47 -05:00
|
|
|
const auto pair = Util::i32FromString(tokens[2]);
|
|
|
|
if (!pair.second)
|
|
|
|
return std::string();
|
2017-01-24 05:40:30 -06:00
|
|
|
|
2020-06-02 22:16:47 -05:00
|
|
|
const auto callbackType = static_cast<LibreOfficeKitCallbackType>(pair.first);
|
2017-01-25 11:21:56 -06:00
|
|
|
|
2020-06-02 22:16:47 -05:00
|
|
|
switch (callbackType)
|
|
|
|
{
|
|
|
|
case LOK_CALLBACK_INVALIDATE_TILES: // invalidation
|
|
|
|
{
|
2022-08-30 00:27:44 -05:00
|
|
|
int msgX, msgY, msgW, msgH, msgPart, msgMode;
|
2017-01-25 11:21:56 -06:00
|
|
|
|
2022-08-30 00:27:44 -05:00
|
|
|
if (!extractRectangle(tokens, msgX, msgY, msgW, msgH, msgPart, msgMode))
|
2020-06-02 22:16:47 -05:00
|
|
|
return std::string();
|
2017-01-25 11:21:56 -06:00
|
|
|
|
2020-06-02 22:16:47 -05:00
|
|
|
bool performedMerge = false;
|
2017-01-24 05:40:30 -06:00
|
|
|
|
2020-06-02 22:16:47 -05:00
|
|
|
// we always travel the entire queue
|
|
|
|
std::size_t i = 0;
|
|
|
|
while (i < getQueue().size())
|
2017-01-25 11:21:56 -06:00
|
|
|
{
|
2020-06-02 22:16:47 -05:00
|
|
|
auto& it = getQueue()[i];
|
2017-01-25 11:21:56 -06:00
|
|
|
|
2022-03-29 20:37:57 -05:00
|
|
|
StringVector queuedTokens = StringVector::tokenize(it.data(), it.size());
|
2020-06-02 22:16:47 -05:00
|
|
|
if (queuedTokens.size() < 3)
|
|
|
|
{
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-25 11:21:56 -06:00
|
|
|
|
2020-06-02 22:16:47 -05:00
|
|
|
// not a invalidation callback
|
|
|
|
if (queuedTokens[0] != tokens[0] || queuedTokens[1] != tokens[1]
|
|
|
|
|| queuedTokens[2] != tokens[2])
|
|
|
|
{
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-25 11:21:56 -06:00
|
|
|
|
2022-08-30 00:27:44 -05:00
|
|
|
int queuedX, queuedY, queuedW, queuedH, queuedPart, queuedMode;
|
2017-01-25 11:21:56 -06:00
|
|
|
|
2022-08-30 00:27:44 -05:00
|
|
|
if (!extractRectangle(queuedTokens, queuedX, queuedY, queuedW, queuedH, queuedPart, queuedMode))
|
2020-06-02 22:16:47 -05:00
|
|
|
{
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-25 11:21:56 -06:00
|
|
|
|
2020-06-02 22:16:47 -05:00
|
|
|
if (msgPart != queuedPart)
|
2017-01-25 11:21:56 -06:00
|
|
|
{
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-08-30 00:27:44 -05:00
|
|
|
if (msgMode != queuedMode)
|
|
|
|
{
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-06-02 22:16:47 -05:00
|
|
|
// the invalidation in the queue is fully covered by the message,
|
|
|
|
// just remove it
|
|
|
|
if (msgX <= queuedX && queuedX + queuedW <= msgX + msgW && msgY <= queuedY
|
|
|
|
&& queuedY + queuedH <= msgY + msgH)
|
|
|
|
{
|
|
|
|
LOG_TRC("Removing smaller invalidation: "
|
|
|
|
<< std::string(it.data(), it.size()) << " -> " << tokens[0] << ' '
|
|
|
|
<< tokens[1] << ' ' << tokens[2] << ' ' << msgX << ' ' << msgY << ' '
|
2022-08-30 00:27:44 -05:00
|
|
|
<< msgW << ' ' << msgH << ' ' << msgPart << ' ' << msgMode);
|
2017-01-25 11:21:56 -06:00
|
|
|
|
2020-06-02 22:16:47 -05:00
|
|
|
// remove from the queue
|
|
|
|
getQueue().erase(getQueue().begin() + i);
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-25 11:21:56 -06:00
|
|
|
|
2020-06-02 22:16:47 -05:00
|
|
|
// the invalidation just intersects, join those (if the result is
|
|
|
|
// small)
|
|
|
|
if (TileDesc::rectanglesIntersect(msgX, msgY, msgW, msgH, queuedX, queuedY, queuedW,
|
|
|
|
queuedH))
|
|
|
|
{
|
|
|
|
int joinX = std::min(msgX, queuedX);
|
|
|
|
int joinY = std::min(msgY, queuedY);
|
|
|
|
int joinW = std::max(msgX + msgW, queuedX + queuedW) - joinX;
|
|
|
|
int joinH = std::max(msgY + msgH, queuedY + queuedH) - joinY;
|
|
|
|
|
|
|
|
const int reasonableSizeX = 4 * 3840; // 4x tile at 100% zoom
|
|
|
|
const int reasonableSizeY = 2 * 3840; // 2x tile at 100% zoom
|
|
|
|
if (joinW > reasonableSizeX || joinH > reasonableSizeY)
|
|
|
|
{
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-25 11:21:56 -06:00
|
|
|
|
2020-06-02 22:16:47 -05:00
|
|
|
LOG_TRC("Merging invalidations: "
|
2022-08-30 00:27:44 -05:00
|
|
|
<< std::string(it.data(), it.size()) << " and "
|
|
|
|
<< tokens[0] << ' ' << tokens[1] << ' ' << tokens[2] << ' '
|
|
|
|
<< msgX << ' ' << msgY << ' ' << msgW << ' ' << msgH << ' '
|
|
|
|
<< msgPart << ' ' << msgMode << " -> "
|
|
|
|
<< tokens[0] << ' ' << tokens[1] << ' ' << tokens[2] << ' '
|
|
|
|
<< joinX << ' ' << joinY << ' ' << joinW << ' ' << joinH << ' '
|
|
|
|
<< msgPart << ' ' << msgMode);
|
2017-01-25 11:21:56 -06:00
|
|
|
|
2020-06-02 22:16:47 -05:00
|
|
|
msgX = joinX;
|
|
|
|
msgY = joinY;
|
|
|
|
msgW = joinW;
|
|
|
|
msgH = joinH;
|
|
|
|
performedMerge = true;
|
2017-01-25 11:21:56 -06:00
|
|
|
|
2020-06-02 22:16:47 -05:00
|
|
|
// remove from the queue
|
|
|
|
getQueue().erase(getQueue().begin() + i);
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-25 05:21:38 -06:00
|
|
|
|
2020-06-02 22:16:47 -05:00
|
|
|
++i;
|
|
|
|
}
|
2017-01-25 05:21:38 -06:00
|
|
|
|
2020-06-02 22:16:47 -05:00
|
|
|
if (performedMerge)
|
|
|
|
{
|
|
|
|
std::size_t pre = tokens[0].size() + tokens[1].size() + tokens[2].size() + 3;
|
|
|
|
std::size_t post = pre + tokens[3].size() + tokens[4].size() + tokens[5].size()
|
|
|
|
+ tokens[6].size() + 4;
|
2017-01-25 05:21:38 -06:00
|
|
|
|
2020-06-02 22:16:47 -05:00
|
|
|
std::string result = callbackMsg.substr(0, pre) + std::to_string(msgX) + ", "
|
|
|
|
+ std::to_string(msgY) + ", " + std::to_string(msgW) + ", "
|
|
|
|
+ std::to_string(msgH) + ", " + callbackMsg.substr(post);
|
2017-01-25 05:21:38 -06:00
|
|
|
|
2020-06-02 22:16:47 -05:00
|
|
|
LOG_TRC("Merge result: " << result);
|
2017-01-25 11:21:56 -06:00
|
|
|
|
2020-06-02 22:16:47 -05:00
|
|
|
return result;
|
2017-01-25 05:21:38 -06:00
|
|
|
}
|
|
|
|
}
|
2020-06-02 22:16:47 -05:00
|
|
|
break;
|
2017-01-20 08:56:47 -06:00
|
|
|
|
2020-06-02 22:16:47 -05:00
|
|
|
case LOK_CALLBACK_STATE_CHANGED: // state changed
|
2017-01-20 08:56:47 -06:00
|
|
|
{
|
2020-06-02 22:16:47 -05:00
|
|
|
if (tokens.size() < 4)
|
|
|
|
return std::string();
|
2017-01-20 08:56:47 -06:00
|
|
|
|
2020-06-02 22:16:47 -05:00
|
|
|
std::string unoCommand = extractUnoCommand(tokens[3]);
|
|
|
|
if (unoCommand.empty())
|
|
|
|
return std::string();
|
|
|
|
|
2020-09-21 01:46:38 -05:00
|
|
|
// This is needed because otherwise it creates some problems when
|
|
|
|
// a save occurs while a cell is still edited in Calc.
|
|
|
|
if (unoCommand == ".uno:ModifiedStatus")
|
|
|
|
return std::string();
|
|
|
|
|
2023-06-03 14:10:29 -05:00
|
|
|
if (getQueue().empty())
|
|
|
|
return std::string();
|
|
|
|
|
2020-06-02 22:16:47 -05:00
|
|
|
// remove obsolete states of the same .uno: command
|
2023-06-03 14:10:29 -05:00
|
|
|
isDuplicateCommand functor(unoCommand, tokens);
|
2020-06-02 22:16:47 -05:00
|
|
|
for (std::size_t i = 0; i < getQueue().size(); ++i)
|
|
|
|
{
|
|
|
|
auto& it = getQueue()[i];
|
2017-01-20 08:56:47 -06:00
|
|
|
|
2023-06-03 14:10:29 -05:00
|
|
|
StringVector::tokenize_foreach(functor, it.data(), it.size());
|
2020-06-02 22:16:47 -05:00
|
|
|
|
2023-06-03 14:10:29 -05:00
|
|
|
if (functor.get_is_duplicate_command())
|
2020-06-02 22:16:47 -05:00
|
|
|
{
|
|
|
|
LOG_TRC("Remove obsolete uno command: "
|
|
|
|
<< std::string(it.data(), it.size()) << " -> "
|
2021-11-18 06:08:14 -06:00
|
|
|
<< COOLProtocol::getAbbreviatedMessage(callbackMsg));
|
2020-06-02 22:16:47 -05:00
|
|
|
getQueue().erase(getQueue().begin() + i);
|
|
|
|
break;
|
|
|
|
}
|
2023-06-03 14:10:29 -05:00
|
|
|
functor.reset();
|
2017-01-20 08:56:47 -06:00
|
|
|
}
|
2020-06-02 22:16:47 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOK_CALLBACK_INVALIDATE_VISIBLE_CURSOR: // the cursor has moved
|
|
|
|
case LOK_CALLBACK_CURSOR_VISIBLE: // the cursor visibility has changed
|
|
|
|
case LOK_CALLBACK_STATUS_INDICATOR_SET_VALUE: // setting the indicator value
|
|
|
|
case LOK_CALLBACK_DOCUMENT_SIZE_CHANGED: // setting the document size
|
|
|
|
case LOK_CALLBACK_CELL_CURSOR: // the cell cursor has moved
|
|
|
|
case LOK_CALLBACK_INVALIDATE_VIEW_CURSOR: // the view cursor has moved
|
|
|
|
case LOK_CALLBACK_CELL_VIEW_CURSOR: // the view cell cursor has moved
|
|
|
|
case LOK_CALLBACK_VIEW_CURSOR_VISIBLE: // the view cursor visibility has changed
|
|
|
|
{
|
|
|
|
const bool isViewCallback = (callbackType == LOK_CALLBACK_INVALIDATE_VIEW_CURSOR
|
|
|
|
|| callbackType == LOK_CALLBACK_CELL_VIEW_CURSOR
|
|
|
|
|| callbackType == LOK_CALLBACK_VIEW_CURSOR_VISIBLE);
|
|
|
|
|
|
|
|
const std::string viewId
|
|
|
|
= (isViewCallback ? extractViewId(callbackMsg, tokens) : std::string());
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < getQueue().size(); ++i)
|
2017-01-20 08:56:47 -06:00
|
|
|
{
|
2020-06-02 22:16:47 -05:00
|
|
|
const auto& it = getQueue()[i];
|
|
|
|
|
|
|
|
// skip non-callbacks quickly
|
2021-11-18 06:08:14 -06:00
|
|
|
if (!COOLProtocol::matchPrefix("callback", it))
|
2020-06-02 22:16:47 -05:00
|
|
|
continue;
|
2017-01-20 08:56:47 -06:00
|
|
|
|
2022-03-29 20:37:57 -05:00
|
|
|
StringVector queuedTokens = StringVector::tokenize(it.data(), it.size());
|
2020-06-02 22:16:47 -05:00
|
|
|
if (queuedTokens.size() < 3)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!isViewCallback
|
|
|
|
&& (queuedTokens.equals(1, tokens, 1) && queuedTokens.equals(2, tokens, 2)))
|
2017-01-20 08:56:47 -06:00
|
|
|
{
|
2020-06-02 22:16:47 -05:00
|
|
|
LOG_TRC("Remove obsolete callback: "
|
|
|
|
<< std::string(it.data(), it.size()) << " -> "
|
2021-11-18 06:08:14 -06:00
|
|
|
<< COOLProtocol::getAbbreviatedMessage(callbackMsg));
|
2018-11-16 02:06:08 -06:00
|
|
|
getQueue().erase(getQueue().begin() + i);
|
2017-01-20 08:56:47 -06:00
|
|
|
break;
|
|
|
|
}
|
2020-06-02 22:16:47 -05:00
|
|
|
else if (isViewCallback
|
|
|
|
&& (queuedTokens.equals(1, tokens, 1)
|
|
|
|
&& queuedTokens.equals(2, tokens, 2)))
|
|
|
|
{
|
|
|
|
// we additionally need to ensure that the payload is about
|
|
|
|
// the same viewid (otherwise we'd merge them all views into
|
|
|
|
// one)
|
|
|
|
const std::string queuedViewId
|
|
|
|
= extractViewId(std::string(it.data(), it.size()), queuedTokens);
|
|
|
|
|
|
|
|
if (viewId == queuedViewId)
|
|
|
|
{
|
|
|
|
LOG_TRC("Remove obsolete view callback: "
|
|
|
|
<< std::string(it.data(), it.size()) << " -> "
|
2021-11-18 06:08:14 -06:00
|
|
|
<< COOLProtocol::getAbbreviatedMessage(callbackMsg));
|
2020-06-02 22:16:47 -05:00
|
|
|
getQueue().erase(getQueue().begin() + i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-01-20 08:56:47 -06:00
|
|
|
}
|
|
|
|
}
|
2020-06-02 22:16:47 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
} // switch
|
2017-01-25 11:21:56 -06:00
|
|
|
|
|
|
|
return std::string();
|
2017-01-20 08:56:47 -06:00
|
|
|
}
|
|
|
|
|
2016-09-30 15:00:20 -05:00
|
|
|
int TileQueue::priority(const std::string& tileMsg)
|
2016-09-14 16:42:50 -05:00
|
|
|
{
|
2018-02-07 03:17:19 -06:00
|
|
|
TileDesc tile = TileDesc::parse(tileMsg); //FIXME: Expensive, avoid.
|
2016-09-14 16:42:50 -05:00
|
|
|
|
2016-09-30 15:38:20 -05:00
|
|
|
for (int i = static_cast<int>(_viewOrder.size()) - 1; i >= 0; --i)
|
2016-09-14 16:42:50 -05:00
|
|
|
{
|
2016-09-30 15:00:20 -05:00
|
|
|
auto& cursor = _cursorPositions[_viewOrder[i]];
|
2018-11-15 02:07:27 -06:00
|
|
|
if (tile.intersectsWithRect(cursor.getX(), cursor.getY(), cursor.getWidth(),
|
|
|
|
cursor.getHeight()))
|
2016-09-30 15:00:20 -05:00
|
|
|
return i;
|
2016-09-14 16:42:50 -05:00
|
|
|
}
|
|
|
|
|
2016-09-30 15:00:20 -05:00
|
|
|
return -1;
|
2015-11-09 04:36:37 -06:00
|
|
|
}
|
|
|
|
|
2017-01-25 12:14:38 -06:00
|
|
|
void TileQueue::deprioritizePreviews()
|
2016-10-04 07:23:42 -05:00
|
|
|
{
|
2018-11-16 02:06:08 -06:00
|
|
|
for (size_t i = 0; i < getQueue().size(); ++i)
|
2016-10-04 07:23:42 -05:00
|
|
|
{
|
2018-11-16 02:06:08 -06:00
|
|
|
const Payload front = getQueue().front();
|
2017-01-25 12:14:38 -06:00
|
|
|
const std::string message(front.data(), front.size());
|
2016-10-04 07:23:42 -05:00
|
|
|
|
2017-01-25 12:14:38 -06:00
|
|
|
// stop at the first non-tile or non-'id' (preview) message
|
|
|
|
std::string id;
|
2021-11-18 06:08:14 -06:00
|
|
|
if (!COOLProtocol::matchPrefix("tile", message) ||
|
|
|
|
!COOLProtocol::getTokenStringFromMessage(message, "id", id))
|
2016-11-16 07:47:11 -06:00
|
|
|
{
|
2016-10-04 07:23:42 -05:00
|
|
|
break;
|
2016-11-16 07:47:11 -06:00
|
|
|
}
|
2016-11-29 20:55:44 -06:00
|
|
|
|
2018-11-16 02:06:08 -06:00
|
|
|
getQueue().erase(getQueue().begin());
|
|
|
|
getQueue().push_back(front);
|
2016-11-29 20:55:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 12:38:07 -06:00
|
|
|
TileQueue::Payload TileQueue::get_impl()
|
2016-09-15 07:40:26 -05:00
|
|
|
{
|
2018-11-16 02:06:08 -06:00
|
|
|
LOG_TRC("MessageQueue depth: " << getQueue().size());
|
2017-01-27 11:31:28 -06:00
|
|
|
|
2018-11-16 02:06:08 -06:00
|
|
|
const Payload front = getQueue().front();
|
2016-09-15 07:40:26 -05:00
|
|
|
|
2018-02-07 03:17:19 -06:00
|
|
|
std::string msg(front.data(), front.size());
|
2016-10-04 07:23:42 -05:00
|
|
|
|
2017-01-25 12:14:38 -06:00
|
|
|
std::string id;
|
2021-11-18 06:08:14 -06:00
|
|
|
bool isTile = COOLProtocol::matchPrefix("tile", msg);
|
|
|
|
bool isPreview = isTile && COOLProtocol::getTokenStringFromMessage(msg, "id", id);
|
2017-01-25 12:14:38 -06:00
|
|
|
if (!isTile || isPreview)
|
2016-11-29 20:55:44 -06:00
|
|
|
{
|
2017-01-25 12:14:38 -06:00
|
|
|
// Don't combine non-tiles or tiles with id.
|
2021-11-18 06:08:14 -06:00
|
|
|
LOG_TRC("MessageQueue res: " << COOLProtocol::getAbbreviatedMessage(msg));
|
2018-11-16 02:06:08 -06:00
|
|
|
getQueue().erase(getQueue().begin());
|
2016-11-29 20:55:44 -06:00
|
|
|
|
2017-01-25 12:14:38 -06:00
|
|
|
// de-prioritize the other tiles with id - usually the previews in
|
|
|
|
// Impress
|
|
|
|
if (isPreview)
|
|
|
|
deprioritizePreviews();
|
2016-11-29 20:55:44 -06:00
|
|
|
|
2016-09-15 07:40:26 -05:00
|
|
|
return front;
|
|
|
|
}
|
|
|
|
|
2016-09-23 03:18:54 -05:00
|
|
|
// We are handling a tile; first try to find one that is at the cursor's
|
|
|
|
// position, otherwise handle the one that is at the front
|
2016-09-30 15:00:20 -05:00
|
|
|
int prioritized = 0;
|
|
|
|
int prioritySoFar = -1;
|
2018-11-16 02:06:08 -06:00
|
|
|
for (size_t i = 0; i < getQueue().size(); ++i)
|
2016-09-23 03:18:54 -05:00
|
|
|
{
|
2018-11-16 02:06:08 -06:00
|
|
|
auto& it = getQueue()[i];
|
2016-09-23 03:18:54 -05:00
|
|
|
const std::string prio(it.data(), it.size());
|
2016-09-26 03:12:45 -05:00
|
|
|
|
|
|
|
// avoid starving - stop the search when we reach a non-tile,
|
|
|
|
// otherwise we may keep growing the queue of unhandled stuff (both
|
|
|
|
// tiles and non-tiles)
|
2021-11-18 06:08:14 -06:00
|
|
|
if (!COOLProtocol::matchPrefix("tile", prio) ||
|
|
|
|
COOLProtocol::getTokenStringFromMessage(prio, "id", id))
|
2016-11-16 07:47:11 -06:00
|
|
|
{
|
2016-09-26 03:12:45 -05:00
|
|
|
break;
|
2016-11-16 07:47:11 -06:00
|
|
|
}
|
2016-09-26 03:12:45 -05:00
|
|
|
|
2016-11-16 07:47:11 -06:00
|
|
|
const int p = priority(prio);
|
2016-09-30 15:38:20 -05:00
|
|
|
if (p > prioritySoFar)
|
2016-09-23 03:18:54 -05:00
|
|
|
{
|
2016-09-30 15:00:20 -05:00
|
|
|
prioritySoFar = p;
|
|
|
|
prioritized = i;
|
2016-09-23 03:18:54 -05:00
|
|
|
msg = prio;
|
2016-09-30 16:06:26 -05:00
|
|
|
|
|
|
|
// found the highest priority already?
|
|
|
|
if (prioritySoFar == static_cast<int>(_viewOrder.size()) - 1)
|
2016-11-16 07:47:11 -06:00
|
|
|
{
|
2016-09-30 16:06:26 -05:00
|
|
|
break;
|
2016-11-16 07:47:11 -06:00
|
|
|
}
|
2016-09-23 03:18:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 02:06:08 -06:00
|
|
|
getQueue().erase(getQueue().begin() + prioritized);
|
2016-09-23 03:18:54 -05:00
|
|
|
|
2016-10-04 07:23:42 -05:00
|
|
|
std::vector<TileDesc> tiles;
|
2016-09-15 07:40:26 -05:00
|
|
|
tiles.emplace_back(TileDesc::parse(msg));
|
|
|
|
|
|
|
|
// Combine as many tiles as possible with the top one.
|
2018-11-16 02:06:08 -06:00
|
|
|
for (size_t i = 0; i < getQueue().size(); )
|
2016-09-15 07:40:26 -05:00
|
|
|
{
|
2018-11-16 02:06:08 -06:00
|
|
|
auto& it = getQueue()[i];
|
2016-09-25 20:30:56 -05:00
|
|
|
msg = std::string(it.data(), it.size());
|
2021-11-18 06:08:14 -06:00
|
|
|
if (!COOLProtocol::matchPrefix("tile", msg) ||
|
|
|
|
COOLProtocol::getTokenStringFromMessage(msg, "id", id))
|
2016-09-15 07:40:26 -05:00
|
|
|
{
|
2016-09-25 20:30:56 -05:00
|
|
|
// Don't combine non-tiles or tiles with id.
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
2016-09-15 07:40:26 -05:00
|
|
|
|
2018-02-07 03:17:19 -06:00
|
|
|
TileDesc tile2 = TileDesc::parse(msg);
|
2021-11-18 06:08:14 -06:00
|
|
|
LOG_TRC("Combining candidate: " << COOLProtocol::getAbbreviatedMessage(msg));
|
2016-09-15 07:40:26 -05:00
|
|
|
|
2016-09-25 20:30:56 -05:00
|
|
|
// Check if it's on the same row.
|
2019-07-25 13:44:50 -05:00
|
|
|
if (tiles[0].canCombine(tile2))
|
2016-09-25 20:30:56 -05:00
|
|
|
{
|
|
|
|
tiles.emplace_back(tile2);
|
2018-11-16 02:06:08 -06:00
|
|
|
getQueue().erase(getQueue().begin() + i);
|
2016-09-25 20:30:56 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++i;
|
2016-09-20 18:20:23 -05:00
|
|
|
}
|
2016-09-15 07:40:26 -05:00
|
|
|
}
|
2016-09-20 18:20:23 -05:00
|
|
|
|
2018-11-16 02:06:08 -06:00
|
|
|
LOG_TRC("Combined " << tiles.size() << " tiles, leaving " << getQueue().size() << " in queue.");
|
2016-09-15 07:40:26 -05:00
|
|
|
|
|
|
|
if (tiles.size() == 1)
|
|
|
|
{
|
|
|
|
msg = tiles[0].serialize("tile");
|
2021-11-18 06:08:14 -06:00
|
|
|
LOG_TRC("MessageQueue res: " << COOLProtocol::getAbbreviatedMessage(msg));
|
2016-09-15 07:40:26 -05:00
|
|
|
return Payload(msg.data(), msg.data() + msg.size());
|
|
|
|
}
|
|
|
|
|
2022-06-09 13:10:42 -05:00
|
|
|
// n^2 but lists are short.
|
|
|
|
for (size_t i = 0; i < tiles.size() - 1; ++i)
|
|
|
|
{
|
|
|
|
const auto &a = tiles[i];
|
|
|
|
for (size_t j = i + 1; j < tiles.size();)
|
|
|
|
{
|
|
|
|
const auto &b = tiles[j];
|
|
|
|
assert(a.getPart() == b.getPart());
|
2022-08-30 00:27:44 -05:00
|
|
|
assert(a.getEditMode() == b.getEditMode());
|
2022-06-09 13:10:42 -05:00
|
|
|
assert(a.getWidth() == b.getWidth());
|
|
|
|
assert(a.getHeight() == b.getHeight());
|
|
|
|
assert(a.getTileWidth() == b.getTileWidth());
|
|
|
|
assert(a.getTileHeight() == b.getTileHeight());
|
|
|
|
if (a.getTilePosX() == b.getTilePosX() &&
|
|
|
|
a.getTilePosY() == b.getTilePosY())
|
|
|
|
{
|
|
|
|
LOG_TRC("MessageQueue: dropping duplicate tile: " <<
|
|
|
|
j << " vs. " << i << " at: " <<
|
|
|
|
a.getTilePosX() << "," << b.getTilePosY());
|
|
|
|
tiles.erase(tiles.begin() + j);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TileCombined combined = TileCombined::create(tiles);
|
|
|
|
assert(!combined.hasDuplicates());
|
|
|
|
std::string tileCombined = combined.serialize("tilecombine");
|
2021-11-18 06:08:14 -06:00
|
|
|
LOG_TRC("MessageQueue res: " << COOLProtocol::getAbbreviatedMessage(tileCombined));
|
2016-09-15 07:40:26 -05:00
|
|
|
return Payload(tileCombined.data(), tileCombined.data() + tileCombined.size());
|
|
|
|
}
|
|
|
|
|
2021-05-12 11:55:16 -05:00
|
|
|
void TileQueue::dumpState(std::ostream& oss)
|
|
|
|
{
|
2022-06-09 13:10:42 -05:00
|
|
|
oss << "\ttileQueue:"
|
|
|
|
<< "\n\t\tcursorPositions:";
|
|
|
|
for (const auto &it : _cursorPositions)
|
|
|
|
{
|
|
|
|
oss << "\n\t\t\tviewId: "
|
|
|
|
<< it.first
|
|
|
|
<< " part: " << it.second.getPart()
|
|
|
|
<< " x: " << it.second.getX()
|
|
|
|
<< " y: " << it.second.getY()
|
|
|
|
<< " width: " << it.second.getWidth()
|
|
|
|
<< " height: " << it.second.getHeight();
|
|
|
|
}
|
2021-05-12 11:55:16 -05:00
|
|
|
|
2022-06-09 13:10:42 -05:00
|
|
|
oss << "\n\t\tviewOrder: [";
|
|
|
|
std::string separator;
|
|
|
|
for (const auto& viewId : _viewOrder)
|
|
|
|
{
|
|
|
|
oss << separator << viewId;
|
|
|
|
separator = ", ";
|
|
|
|
}
|
|
|
|
oss << "]\n";
|
2021-05-12 11:55:16 -05:00
|
|
|
}
|
|
|
|
|
2015-11-09 04:36:37 -06:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|