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 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 "MessageQueue.hpp"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2016-09-20 18:59:06 -05:00
|
|
|
#include <Poco/StringTokenizer.h>
|
|
|
|
|
2016-09-14 16:42:50 -05:00
|
|
|
#include <TileDesc.hpp>
|
2016-09-01 15:45:04 -05:00
|
|
|
#include <Log.hpp>
|
|
|
|
|
2016-09-20 18:59:06 -05:00
|
|
|
using Poco::StringTokenizer;
|
|
|
|
|
2015-11-09 04:36:37 -06:00
|
|
|
MessageQueue::~MessageQueue()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2016-03-26 21:32:29 -05:00
|
|
|
void MessageQueue::put(const Payload& value)
|
2015-11-09 04:36:37 -06:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
put_impl(value);
|
|
|
|
lock.unlock();
|
|
|
|
_cv.notify_one();
|
|
|
|
}
|
|
|
|
|
2016-03-26 21:32:29 -05:00
|
|
|
MessageQueue::Payload MessageQueue::get()
|
2015-11-09 04:36:37 -06:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
_cv.wait(lock, [this] { return wait_impl(); });
|
|
|
|
return get_impl();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageQueue::clear()
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
2015-12-17 11:47:58 -06:00
|
|
|
clear_impl();
|
2015-11-09 04:36:37 -06:00
|
|
|
}
|
|
|
|
|
2016-04-27 07:58:33 -05:00
|
|
|
void MessageQueue::remove_if(const std::function<bool(const Payload&)>& pred)
|
2015-12-13 22:36:46 -06:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
std::remove_if(_queue.begin(), _queue.end(), pred);
|
|
|
|
}
|
|
|
|
|
2016-03-26 21:32:29 -05:00
|
|
|
void MessageQueue::put_impl(const Payload& value)
|
2015-11-09 04:36:37 -06:00
|
|
|
{
|
2016-09-15 07:40:26 -05:00
|
|
|
const auto msg = std::string(value.data(), value.size());
|
|
|
|
Log::trace() << "Pushing into MQ [" << msg << "]" << Log::end;
|
2015-11-09 04:36:37 -06:00
|
|
|
_queue.push_back(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MessageQueue::wait_impl() const
|
|
|
|
{
|
|
|
|
return _queue.size() > 0;
|
|
|
|
}
|
|
|
|
|
2016-03-26 21:32:29 -05:00
|
|
|
MessageQueue::Payload MessageQueue::get_impl()
|
2015-11-09 04:36:37 -06:00
|
|
|
{
|
2016-03-26 21:32:29 -05:00
|
|
|
auto result = _queue.front();
|
2015-11-09 04:36:37 -06:00
|
|
|
_queue.pop_front();
|
2016-03-29 06:36:30 -05:00
|
|
|
return result;
|
2015-11-09 04:36:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void MessageQueue::clear_impl()
|
|
|
|
{
|
|
|
|
_queue.clear();
|
|
|
|
}
|
|
|
|
|
2016-03-26 21:32:29 -05:00
|
|
|
void TileQueue::put_impl(const Payload& value)
|
2015-11-09 04:36:37 -06:00
|
|
|
{
|
2016-09-14 16:42:50 -05:00
|
|
|
const auto msg = std::string(value.data(), value.size());
|
|
|
|
Log::trace() << "Putting [" << msg << "]" << Log::end;
|
2016-09-20 18:59:06 -05:00
|
|
|
|
|
|
|
if (msg.compare(0, 11, "canceltiles") == 0)
|
|
|
|
{
|
|
|
|
Log::trace("Processing " + msg);
|
|
|
|
Log::trace() << "Before canceltiles have " << _queue.size() << " in queue." << Log::end;
|
|
|
|
const auto seqs = msg.substr(12);
|
|
|
|
StringTokenizer tokens(seqs, ",", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
|
|
|
|
_queue.erase(std::remove_if(_queue.begin(), _queue.end(),
|
|
|
|
[&tokens](const Payload& v)
|
|
|
|
{
|
|
|
|
const std::string s(v.data(), v.size());
|
|
|
|
for (size_t i = 0; i < tokens.count(); ++i)
|
|
|
|
{
|
|
|
|
if (s.find("ver=" + tokens[i]) != std::string::npos)
|
|
|
|
{
|
|
|
|
Log::trace("Matched " + tokens[i] + ", Removing [" + s + "]");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}), _queue.end());
|
|
|
|
|
|
|
|
// Don't push canceltiles into the queue.
|
|
|
|
Log::trace() << "After canceltiles have " << _queue.size() << " in queue." << Log::end;
|
|
|
|
return;
|
|
|
|
}
|
2016-09-25 15:04:27 -05:00
|
|
|
else if (msg.compare(0, 10, "tilecombine") == 0)
|
|
|
|
{
|
|
|
|
// Breakup tilecombine and deduplicate.
|
|
|
|
const auto tileCombined = TileCombined::parse(msg);
|
|
|
|
for (auto& tile : tileCombined.getTiles())
|
|
|
|
{
|
|
|
|
const auto newMsg = tile.serialize("tile");
|
|
|
|
_queue.push_back(Payload(newMsg.data(), newMsg.data() + newMsg.size()));
|
|
|
|
}
|
|
|
|
}
|
2016-09-23 03:18:54 -05:00
|
|
|
|
2016-09-01 15:45:04 -05:00
|
|
|
if (!_queue.empty())
|
2015-11-09 04:36:37 -06:00
|
|
|
{
|
2016-09-23 03:18:54 -05:00
|
|
|
// TODO probably we could do the same with the invalidation callbacks
|
|
|
|
// (later one wins).
|
2016-09-25 15:04:27 -05:00
|
|
|
if (msg.compare(0, 4, "tile") == 0)
|
2015-11-09 04:36:37 -06:00
|
|
|
{
|
2016-09-01 15:45:04 -05:00
|
|
|
const auto newMsg = msg.substr(0, msg.find(" ver"));
|
|
|
|
|
|
|
|
for (size_t i = 0; i < _queue.size(); ++i)
|
2016-03-26 21:32:29 -05:00
|
|
|
{
|
2016-09-01 15:45:04 -05:00
|
|
|
auto& it = _queue[i];
|
|
|
|
const std::string old(it.data(), it.size());
|
|
|
|
const auto oldMsg = old.substr(0, old.find(" ver"));
|
|
|
|
if (newMsg == oldMsg)
|
|
|
|
{
|
2016-09-23 03:18:54 -05:00
|
|
|
Log::debug() << "Remove duplicate message: " << old << " -> " << msg << Log::end;
|
|
|
|
_queue.erase(_queue.begin() + i);
|
|
|
|
break;
|
2016-09-01 15:45:04 -05:00
|
|
|
}
|
2016-03-26 21:32:29 -05:00
|
|
|
}
|
2015-11-09 04:36:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-25 10:50:01 -05:00
|
|
|
MessageQueue::put_impl(value);
|
2016-09-14 16:42:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TileQueue::priority(const std::string& tileMsg)
|
|
|
|
{
|
2016-09-14 17:18:08 -05:00
|
|
|
if (tileMsg.compare(0, 5, "tile ") != 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-14 16:42:50 -05:00
|
|
|
auto tile = TileDesc::parse(tileMsg); //FIXME: Expensive, avoid.
|
|
|
|
|
2016-09-23 03:37:33 -05:00
|
|
|
for (int view : _viewOrder)
|
2016-09-14 16:42:50 -05:00
|
|
|
{
|
2016-09-23 03:37:33 -05:00
|
|
|
auto& cursor = _cursorPositions[view];
|
|
|
|
if (tile.intersectsWithRect(cursor.X, cursor.Y, cursor.Width, cursor.Height))
|
2016-09-14 16:42:50 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2015-11-09 04:36:37 -06:00
|
|
|
}
|
|
|
|
|
2016-09-15 07:40:26 -05:00
|
|
|
MessageQueue::Payload TileQueue::get_impl()
|
|
|
|
{
|
|
|
|
std::vector<TileDesc> tiles;
|
|
|
|
const auto front = _queue.front();
|
|
|
|
|
|
|
|
auto msg = std::string(front.data(), front.size());
|
|
|
|
Log::trace() << "MessageQueue Get, Size: " << _queue.size() << ", Front: " << msg << Log::end;
|
|
|
|
|
|
|
|
if (msg.compare(0, 5, "tile ") != 0 || msg.find("id=") != std::string::npos)
|
|
|
|
{
|
|
|
|
// Don't combine non-tiles or tiles with id.
|
|
|
|
Log::trace() << "MessageQueue res: " << msg << Log::end;
|
2016-09-23 03:18:54 -05:00
|
|
|
_queue.pop_front();
|
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
|
|
|
|
bool foundPrioritized = false;
|
|
|
|
for (size_t i = 0; i < _queue.size(); ++i)
|
|
|
|
{
|
|
|
|
auto& it = _queue[i];
|
|
|
|
const std::string prio(it.data(), it.size());
|
|
|
|
if (priority(prio))
|
|
|
|
{
|
|
|
|
Log::debug() << "Handling a priority message: " << prio << Log::end;
|
|
|
|
_queue.erase(_queue.begin() + i);
|
|
|
|
msg = prio;
|
|
|
|
foundPrioritized = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!foundPrioritized)
|
|
|
|
_queue.pop_front();
|
|
|
|
|
2016-09-15 07:40:26 -05:00
|
|
|
tiles.emplace_back(TileDesc::parse(msg));
|
|
|
|
|
|
|
|
// Combine as many tiles as possible with the top one.
|
2016-09-25 20:30:56 -05:00
|
|
|
for (size_t i = 0; i < _queue.size(); )
|
2016-09-15 07:40:26 -05:00
|
|
|
{
|
2016-09-25 20:30:56 -05:00
|
|
|
auto& it = _queue[i];
|
|
|
|
msg = std::string(it.data(), it.size());
|
|
|
|
if (msg.compare(0, 5, "tile ") != 0 ||
|
|
|
|
msg.find("id=") != std::string::npos)
|
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
|
|
|
|
2016-09-25 20:30:56 -05:00
|
|
|
auto tile2 = TileDesc::parse(msg);
|
|
|
|
Log::trace() << "combining candidate: " << msg << Log::end;
|
2016-09-15 07:40:26 -05:00
|
|
|
|
2016-09-25 20:30:56 -05:00
|
|
|
// Check if it's on the same row.
|
|
|
|
if (tiles[0].onSameRow(tile2))
|
|
|
|
{
|
|
|
|
tiles.emplace_back(tile2);
|
|
|
|
_queue.erase(_queue.begin() + i);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
Log::trace() << "Combined " << tiles.size() << " tiles, leaving " << _queue.size() << " in queue." << Log::end;
|
2016-09-15 07:40:26 -05:00
|
|
|
|
|
|
|
if (tiles.size() == 1)
|
|
|
|
{
|
|
|
|
msg = tiles[0].serialize("tile");
|
|
|
|
return Payload(msg.data(), msg.data() + msg.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto tileCombined = TileCombined::create(tiles).serialize("tilecombine");
|
|
|
|
Log::trace() << "MessageQueue res: " << tileCombined << Log::end;
|
|
|
|
return Payload(tileCombined.data(), tileCombined.data() + tileCombined.size());
|
|
|
|
}
|
|
|
|
|
2015-11-09 04:36:37 -06:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|