summaryrefslogtreecommitdiff
path: root/src/waylandentries.cpp
blob: 42808bb7780053637e2dea7c33488e6c6f9271e1 (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
#include "waylandentries.h"
#include <QWaylandXdgSurface>
#include <QWaylandSurface>
#include <QMutexLocker>
#include <qdebug.h>

WaylandEntries *m_instance = nullptr;

WaylandEntries::WaylandEntries(QObject *parent)
    : QAbstractListModel(parent)
{
    if(m_instance != nullptr)
    {
        qWarning() << "Many instance of WaylandEntries";
    }
    m_instance = this;
}

int WaylandEntries::rowCount(const QModelIndex &parent) const
{
    QMutexLocker locker(&m_lock);
    return m_surfaces.count();
}

QVariant WaylandEntries::data(const QModelIndex &index, int role) const
{
    QMutexLocker locker(&m_lock);
    if(index.row() > m_surfaces.count())
        return QVariant();

    QWaylandXdgSurface *surface = m_surfaces[index.row()];
    if(surface == nullptr)
        return QVariant();

    switch(role) {
        case Qt::DisplayRole:
        case TitleRole:
            return surface->toplevel()->title();
        case AppIdRole:
            return surface->toplevel()->appId();
        case SurfaceRole:
            return QVariant::fromValue<QWaylandXdgSurface*>(surface);
        case PidRole:
            return surface->surface()->client()->processId();
    }

    return QVariant();
}

QHash<int, QByteArray> WaylandEntries::roleNames() const
{
    QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
    roles[SurfaceRole] = "wl_surface";
    roles[TitleRole] = "title";
    roles[PidRole] = "pid";
    roles[AppIdRole] = "appId";
    return roles;
}

// Need only for application manager interface for cli
WaylandEntries *WaylandEntries::getInstance()
{
    return m_instance;
}

void WaylandEntries::addSurface(QObject *obj)
{
    QMutexLocker locker(&m_lock);
    QWaylandXdgSurface *s = qobject_cast<QWaylandXdgSurface*>(obj);
    if(s)
    {
        QObject::connect(s, &QWaylandXdgSurface::destroyed, this, &WaylandEntries::surfaceDestroyed);
        if(m_hidden.contains(s->surface()->client()->processId()))
        {
            m_surfaces_hidden.append(s);
        }
        else
        {
            int id = m_surfaces.count();
            beginInsertRows(QModelIndex(), m_surfaces.count(), m_surfaces.count());
            QObject::connect(s->toplevel(), &QWaylandXdgToplevel::titleChanged, this, [this, id](){
                const QVector<int> roles = {TitleRole};
                emit dataChanged(index(id), index(id), roles);
            });
            QObject::connect(s->toplevel(), &QWaylandXdgToplevel::appIdChanged, this, [this, id, s](){
                const QVector<int> roles = {AppIdRole};

                emit topWindowRegistered(s, s->toplevel()->appId());
                emit dataChanged(index(id), index(id), roles);
            });
            m_surfaces.append(s);
            endInsertRows();
            emit nbAppsChanged();
        }
    }
}

void WaylandEntries::addHidePid(qint64 pid)
{
    QMutexLocker locker(&m_lock);
    m_hidden.append(pid);
    QWaylandXdgSurface *surfaceToHide = nullptr;

    for(QWaylandXdgSurface *surface: m_surfaces)
    {
        if(surface->surface()->client()->processId() == pid)
        {
            surfaceToHide = surface;
            break;
        }
    }
    if(surfaceToHide)
    {
        beginRemoveRows(QModelIndex(), m_surfaces.indexOf(surfaceToHide), m_surfaces.indexOf(surfaceToHide));
        m_surfaces.removeAll(surfaceToHide);
        endRemoveRows();
        m_surfaces_hidden.append(surfaceToHide);
        emit nbAppsChanged();
    }
}

void WaylandEntries::surfaceDestroyed()
{
    QObject *obj = QObject::sender();
    QWaylandXdgSurface *s = static_cast<QWaylandXdgSurface*>(obj);
    emit topWindowDestroyed(s, s->toplevel()->appId());
    QMutexLocker locker(&m_lock);
    beginRemoveRows(QModelIndex(), m_surfaces.indexOf(s), m_surfaces.indexOf(s));
    m_surfaces.removeAll(s);
    endRemoveRows();
    emit nbAppsChanged();
}

void WaylandEntries::stop(int pid)
{
    kill(pid, SIGTERM);
}

QObject *WaylandEntries::getSurface(int pid)
{
    QMutexLocker locker(&m_lock);
    for(QWaylandXdgSurface *surface: m_surfaces)
    {
        if(surface->surface()->client()->processId() == pid)
            return surface;
    }
    for(QWaylandXdgSurface *surface: m_surfaces_hidden)
    {
        if(surface->surface()->client()->processId() == pid)
            return surface;
    }
    return nullptr;
}

int WaylandEntries::nbApps() const
{
    QMutexLocker locker(&m_lock);
    return m_surfaces.count();
}