2021-05-18 10:05:59 +00:00
|
|
|
/*
|
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
|
|
|
|
* Copyright (C) 2012-2021 Symless Ltd.
|
|
|
|
|
* Copyright (C) 2008 Volker Lanz (vl@fidra.de)
|
|
|
|
|
*
|
|
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* found in the file LICENSE that should have accompanied this file.
|
|
|
|
|
*
|
|
|
|
|
* This package is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
#include "ScreenList.h"
|
|
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief getNeightborIndexes returns indexes for server neighbors
|
|
|
|
|
* @param serverIndex server index
|
|
|
|
|
* @param width of the grid
|
|
|
|
|
* @param size of the grid
|
|
|
|
|
* @return indexes for server neighbors
|
|
|
|
|
*/
|
2024-07-02 19:07:06 +00:00
|
|
|
std::array<int, 8> getNeighborsIndexes(int serverIndex, int width, int size) {
|
|
|
|
|
const int UNSET = -1;
|
|
|
|
|
const int LEFT = 0;
|
|
|
|
|
const int RIGHT = 1;
|
|
|
|
|
const int TOP = 2;
|
|
|
|
|
const int BOTTOM = 3;
|
|
|
|
|
const int TOP_LEFT = 4;
|
|
|
|
|
const int TOP_RIGHT = 5;
|
|
|
|
|
const int BOTTOM_RIGHT = 6;
|
|
|
|
|
const int BOTTOM_LEFT = 7;
|
|
|
|
|
|
|
|
|
|
std::array<int, 8> indexes = {UNSET};
|
|
|
|
|
|
|
|
|
|
if (serverIndex >= 0 && serverIndex < size) {
|
|
|
|
|
indexes[LEFT] =
|
|
|
|
|
(serverIndex - 1) % width != width - 1 ? (serverIndex - 1) : UNSET;
|
|
|
|
|
indexes[RIGHT] = (serverIndex + 1) % width != 0 ? (serverIndex + 1) : UNSET;
|
|
|
|
|
indexes[TOP] = (serverIndex - width) >= 0 ? (serverIndex - width) : UNSET;
|
|
|
|
|
indexes[BOTTOM] =
|
|
|
|
|
(serverIndex + width) < size ? (serverIndex + width) : UNSET;
|
|
|
|
|
indexes[TOP_LEFT] = (indexes[TOP] != UNSET && indexes[LEFT] != UNSET)
|
|
|
|
|
? indexes[TOP] - 1
|
|
|
|
|
: UNSET;
|
|
|
|
|
indexes[TOP_RIGHT] = (indexes[TOP] != UNSET && indexes[RIGHT] != UNSET)
|
|
|
|
|
? indexes[TOP] + 1
|
|
|
|
|
: UNSET;
|
|
|
|
|
indexes[BOTTOM_RIGHT] =
|
|
|
|
|
(indexes[BOTTOM] != UNSET && indexes[RIGHT] != UNSET)
|
|
|
|
|
? indexes[BOTTOM] + 1
|
|
|
|
|
: UNSET;
|
|
|
|
|
indexes[BOTTOM_LEFT] = (indexes[BOTTOM] != UNSET && indexes[LEFT] != UNSET)
|
|
|
|
|
? indexes[BOTTOM] - 1
|
|
|
|
|
: UNSET;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return indexes;
|
2021-05-18 10:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief getServerIndex finds server and returns it's index
|
|
|
|
|
* @param screens list to find server
|
|
|
|
|
* @return server index
|
|
|
|
|
*/
|
2024-07-02 19:07:06 +00:00
|
|
|
int getServerIndex(const ScreenList &screens) {
|
|
|
|
|
int serverIndex = -1;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < screens.size(); ++i) {
|
|
|
|
|
if (screens[i].isServer()) {
|
|
|
|
|
serverIndex = i;
|
|
|
|
|
break;
|
2021-05-18 10:05:59 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
2021-05-18 10:05:59 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
return serverIndex;
|
2021-05-18 10:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
} // namespace
|
2021-05-18 10:05:59 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
ScreenList::ScreenList(int width) : QList<Screen>(), m_width(width) {}
|
2021-05-18 10:05:59 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void ScreenList::addScreenByPriority(const Screen &newScreen) {
|
|
|
|
|
int serverIndex = getServerIndex(*this);
|
2024-08-08 17:41:59 +00:00
|
|
|
auto indexes =
|
|
|
|
|
getNeighborsIndexes(serverIndex, m_width, static_cast<int>(size()));
|
2021-05-18 10:05:59 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
bool isAdded = false;
|
|
|
|
|
for (const auto &index : indexes) {
|
|
|
|
|
if (index >= 0 && index < size()) {
|
|
|
|
|
auto &screen = operator[](index);
|
|
|
|
|
if (screen.isNull()) {
|
|
|
|
|
screen = newScreen;
|
|
|
|
|
isAdded = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-05-18 10:05:59 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
2021-05-18 10:05:59 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
if (!isAdded) {
|
|
|
|
|
addScreenToFirstEmpty(newScreen);
|
|
|
|
|
}
|
2021-05-18 10:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void ScreenList::addScreenToFirstEmpty(const Screen &newScreen) {
|
|
|
|
|
for (int i = 0; i < size(); ++i) {
|
|
|
|
|
auto &screen = operator[](i);
|
|
|
|
|
if (screen.isNull()) {
|
|
|
|
|
screen = newScreen;
|
|
|
|
|
break;
|
2021-05-18 10:05:59 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
2021-05-18 10:05:59 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
bool ScreenList::operator==(const ScreenList &sc) const {
|
|
|
|
|
return m_width == sc.m_width && QList::operator==(sc);
|
2021-08-20 12:22:07 +00:00
|
|
|
}
|