2015-11-09 04:36:37 -06:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
2023-11-09 12:23:00 -06:00
|
|
|
* Copyright the Collabora Online contributors.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*
|
2015-11-09 04:36:37 -06:00
|
|
|
* 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/.
|
|
|
|
*/
|
|
|
|
|
2020-04-18 03:39:50 -05:00
|
|
|
#pragma once
|
2015-11-09 04:36:37 -06:00
|
|
|
|
2021-04-02 15:57:53 -05:00
|
|
|
#include <stdexcept>
|
2016-09-23 03:37:33 -05:00
|
|
|
#include <algorithm>
|
2016-11-16 07:47:11 -06:00
|
|
|
#include <functional>
|
2016-09-14 16:42:50 -05:00
|
|
|
#include <map>
|
2018-08-29 11:38:19 -05:00
|
|
|
#include <string>
|
2016-03-26 21:32:29 -05:00
|
|
|
#include <vector>
|
2015-11-09 04:36:37 -06:00
|
|
|
|
2021-07-19 08:59:17 -05:00
|
|
|
#include "Log.hpp"
|
2024-05-23 08:09:31 -05:00
|
|
|
#include "TileDesc.hpp"
|
2021-07-19 08:59:17 -05:00
|
|
|
#include "Protocol.hpp"
|
|
|
|
|
2024-05-08 11:23:26 -05:00
|
|
|
/// Queue for handling the Kit's messaging needs
|
2024-05-08 11:47:53 -05:00
|
|
|
class KitQueue
|
2015-11-09 04:36:37 -06:00
|
|
|
{
|
2024-05-08 11:47:53 -05:00
|
|
|
friend class KitQueueTests;
|
2024-05-08 11:23:26 -05:00
|
|
|
|
2015-11-09 04:36:37 -06:00
|
|
|
public:
|
2021-07-20 08:07:29 -05:00
|
|
|
typedef std::vector<char> Payload;
|
2016-03-26 21:32:29 -05:00
|
|
|
|
2024-05-08 13:47:25 -05:00
|
|
|
KitQueue() { }
|
|
|
|
~KitQueue() { }
|
2015-11-09 04:36:37 -06:00
|
|
|
|
2024-05-08 11:47:53 -05:00
|
|
|
KitQueue(const KitQueue&) = delete;
|
|
|
|
KitQueue& operator=(const KitQueue&) = delete;
|
2015-11-09 04:36:37 -06:00
|
|
|
|
2024-05-02 11:18:49 -05:00
|
|
|
/// insert the message.
|
2024-05-08 11:57:16 -05:00
|
|
|
void put(const Payload& value);
|
2016-03-26 21:56:10 -05:00
|
|
|
void put(const std::string& value)
|
|
|
|
{
|
|
|
|
put(Payload(value.data(), value.data() + value.size()));
|
|
|
|
}
|
2015-11-09 04:36:37 -06:00
|
|
|
|
2024-05-09 03:19:44 -05:00
|
|
|
struct Callback {
|
|
|
|
int _view; // -1 for all
|
|
|
|
int _type;
|
|
|
|
std::string _payload;
|
|
|
|
|
|
|
|
Callback() : _view(-1), _type(-1) { }
|
|
|
|
Callback(int view, int type, const std::string payload) :
|
|
|
|
_view(view), _type(type), _payload(payload) { }
|
|
|
|
|
|
|
|
static std::string toString(int view, int type, const std::string payload);
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Queue a LibreOfficeKit callback for later emission
|
|
|
|
void putCallback(int view, int type, const std::string &message);
|
|
|
|
|
|
|
|
/// Work back over the queue to simplify & return false if we should not queue.
|
|
|
|
bool elideDuplicateCallback(int view, int type, const std::string &message);
|
|
|
|
|
2024-05-02 11:18:49 -05:00
|
|
|
/// Obtain the next message.
|
2016-10-09 10:59:00 -05:00
|
|
|
/// timeoutMs can be 0 to signify infinity.
|
2017-01-19 22:24:23 -06:00
|
|
|
/// Returns an empty payload on timeout.
|
2024-05-23 07:40:59 -05:00
|
|
|
Payload pop();
|
|
|
|
Payload get() { return pop(); }
|
2017-01-09 12:38:07 -06:00
|
|
|
|
2024-05-23 08:09:31 -05:00
|
|
|
/// Tiles are special manage a separate queue of them
|
|
|
|
void clearTileQueue() { _tileQueue.clear(); }
|
|
|
|
void pushTileQueue(const Payload &value);
|
|
|
|
void pushTileCombineRequest(const Payload &value);
|
2024-05-23 08:40:43 -05:00
|
|
|
TileCombined popTileQueue();
|
2024-05-23 08:09:31 -05:00
|
|
|
std::vector<TileCombined> popWholeTileQueue();
|
|
|
|
size_t getTileQueueSize() const { return _tileQueue.size(); }
|
|
|
|
|
2024-05-09 03:19:44 -05:00
|
|
|
/// Obtain the next callback
|
|
|
|
Callback getCallback()
|
|
|
|
{
|
|
|
|
assert(_callbacks.size() > 0);
|
|
|
|
const Callback front = _callbacks.front();
|
|
|
|
_callbacks.erase(_callbacks.begin());
|
|
|
|
return front;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool getCallback(Callback &callback)
|
|
|
|
{
|
|
|
|
if (_callbacks.size() == 0)
|
|
|
|
return false;
|
|
|
|
callback = std::move(_callbacks.front());
|
|
|
|
_callbacks.erase(_callbacks.begin());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-04 08:51:14 -05:00
|
|
|
/// Anything in the queue ?
|
|
|
|
bool isEmpty()
|
|
|
|
{
|
2019-05-10 10:45:01 -05:00
|
|
|
return _queue.empty();
|
2019-05-04 08:51:14 -05:00
|
|
|
}
|
|
|
|
|
2024-04-18 13:00:23 -05:00
|
|
|
size_t size() const
|
2024-04-17 15:10:27 -05:00
|
|
|
{
|
|
|
|
return _queue.size();
|
|
|
|
}
|
|
|
|
|
2024-05-09 03:19:44 -05:00
|
|
|
size_t callbackSize() const
|
|
|
|
{
|
|
|
|
return _callbacks.size();
|
|
|
|
}
|
|
|
|
|
2024-05-02 11:18:49 -05:00
|
|
|
/// Removal of all the pending messages.
|
2017-01-09 12:38:07 -06:00
|
|
|
void clear()
|
|
|
|
{
|
2024-05-08 11:23:26 -05:00
|
|
|
_queue.clear();
|
2024-05-09 03:19:44 -05:00
|
|
|
_callbacks.clear();
|
2017-01-09 12:38:07 -06:00
|
|
|
}
|
2015-11-09 04:36:37 -06:00
|
|
|
|
2024-05-08 11:23:26 -05:00
|
|
|
void dumpState(std::ostream& oss);
|
2015-11-09 04:36:37 -06:00
|
|
|
|
2024-05-08 11:23:26 -05:00
|
|
|
protected:
|
2021-07-19 08:59:17 -05:00
|
|
|
/// Search the queue for a previous textinput message and if found, remove it and combine its
|
|
|
|
/// input with that in the current textinput message. We check that there aren't any interesting
|
|
|
|
/// messages inbetween that would make it wrong to merge the textinput messages.
|
|
|
|
///
|
|
|
|
/// @return New message to put into the queue. If empty, use what we got.
|
2024-05-08 11:57:16 -05:00
|
|
|
std::string combineTextInput(const StringVector& tokens);
|
2021-07-19 08:59:17 -05:00
|
|
|
|
2021-07-21 04:49:11 -05:00
|
|
|
/// Search the queue for a previous removetextcontext message (which actually means "remove text
|
2022-08-22 15:00:05 -05:00
|
|
|
/// content", the word "context" is because of some misunderstanding lost in history) and if
|
2021-07-21 04:49:11 -05:00
|
|
|
/// found, remove it and combine its input with that in the current removetextcontext message.
|
|
|
|
/// We check that there aren't any interesting messages inbetween that would make it wrong to
|
|
|
|
/// merge the removetextcontext messages.
|
|
|
|
///
|
|
|
|
/// @return New message to put into the queue. If empty, use what we got.
|
2024-05-08 11:57:16 -05:00
|
|
|
std::string combineRemoveText(const StringVector& tokens);
|
2021-07-21 04:49:11 -05:00
|
|
|
|
2016-09-14 16:42:50 -05:00
|
|
|
private:
|
|
|
|
class CursorPosition
|
|
|
|
{
|
|
|
|
public:
|
2018-11-15 02:07:27 -06:00
|
|
|
CursorPosition() {}
|
|
|
|
CursorPosition(int part, int x, int y, int width, int height)
|
|
|
|
: _part(part)
|
|
|
|
, _x(x)
|
|
|
|
, _y(y)
|
|
|
|
, _width(width)
|
|
|
|
, _height(height)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int getPart() const { return _part; }
|
|
|
|
int getX() const { return _x; }
|
|
|
|
int getY() const { return _y; }
|
|
|
|
int getWidth() const { return _width; }
|
|
|
|
int getHeight() const { return _height; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int _part = 0;
|
|
|
|
int _x = 0;
|
|
|
|
int _y = 0;
|
|
|
|
int _width = 0;
|
|
|
|
int _height = 0;
|
2016-09-14 16:42:50 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2024-05-08 11:57:16 -05:00
|
|
|
void updateCursorPosition(int viewId, int part, int x, int y, int width, int height);
|
|
|
|
void removeCursorPosition(int viewId);
|
2016-09-14 16:42:50 -05:00
|
|
|
|
|
|
|
private:
|
2016-09-26 03:35:20 -05:00
|
|
|
/// Search the queue for a duplicate tile and remove it (if present).
|
2024-05-23 08:40:43 -05:00
|
|
|
void removeTileDuplicate(const TileDesc &desc);
|
2017-01-20 08:56:47 -06:00
|
|
|
|
|
|
|
/// Search the queue for a duplicate callback and remove it (if present).
|
|
|
|
///
|
|
|
|
/// This removes also callbacks that are made invalid by the current
|
|
|
|
/// message, like the new cursor position invalidates the old one etc.
|
2017-01-25 11:21:56 -06:00
|
|
|
///
|
|
|
|
/// @return New message to put into the queue. If empty, use what was in callbackMsg.
|
|
|
|
std::string removeCallbackDuplicate(const std::string& callbackMsg);
|
2016-09-14 16:42:50 -05:00
|
|
|
|
2017-01-25 12:14:38 -06:00
|
|
|
/// De-prioritize the previews (tiles with 'id') - move them to the end of
|
|
|
|
/// the queue.
|
|
|
|
void deprioritizePreviews();
|
2016-10-04 07:23:42 -05:00
|
|
|
|
2016-09-30 15:00:20 -05:00
|
|
|
/// Priority of the given tile message.
|
2016-09-30 15:38:20 -05:00
|
|
|
/// -1 means the lowest prio (the tile does not intersect any of the cursors),
|
|
|
|
/// the higher the number, the bigger is priority [up to _viewOrder.size()-1].
|
2024-05-23 08:40:43 -05:00
|
|
|
int priority(const TileDesc &desc);
|
2016-09-14 16:42:50 -05:00
|
|
|
|
|
|
|
private:
|
2024-05-08 13:47:25 -05:00
|
|
|
/// The incoming underlying queue
|
2024-05-08 11:23:26 -05:00
|
|
|
std::vector<Payload> _queue;
|
|
|
|
|
2024-05-23 08:09:31 -05:00
|
|
|
/// Incoming tile request queue
|
2024-05-23 08:40:43 -05:00
|
|
|
std::vector<TileDesc> _tileQueue;
|
2024-05-23 08:09:31 -05:00
|
|
|
|
2024-05-09 03:19:44 -05:00
|
|
|
/// Outgoing queued callbacks
|
|
|
|
std::vector<Callback> _callbacks;
|
|
|
|
|
2016-09-14 16:42:50 -05:00
|
|
|
std::map<int, CursorPosition> _cursorPositions;
|
2016-09-23 03:37:33 -05:00
|
|
|
|
|
|
|
/// Check the views in the order of how the editing (cursor movement) has
|
2016-09-30 15:38:20 -05:00
|
|
|
/// been happening (0 == oldest, size() - 1 == newest).
|
|
|
|
std::vector<int> _viewOrder;
|
2015-11-09 04:36:37 -06:00
|
|
|
};
|
|
|
|
|
2024-05-09 03:19:44 -05:00
|
|
|
inline std::ostream& operator<<(std::ostream& os, const KitQueue::Callback &c)
|
|
|
|
{
|
|
|
|
os << KitQueue::Callback::toString(c._view, c._type, c._payload);
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2015-11-09 04:36:37 -06:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|