~ubuntu-branches/ubuntu/wily/kwin/wily-proposed

« back to all changes in this revision

Viewing changes to backends/fbdev/fb_backend.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2015-08-10 23:16:37 UTC
  • mfrom: (1.1.10)
  • Revision ID: package-import@ubuntu.com-20150810231637-5zb2tstjkez93hml
Tags: 4:5.3.95-0ubuntu1
new upstream beta release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/********************************************************************
 
2
 KWin - the KDE window manager
 
3
 This file is part of the KDE project.
 
4
 
 
5
Copyright (C) 2015 Martin Gräßlin <mgraesslin@kde.org>
 
6
 
 
7
This program is free software; you can redistribute it and/or modify
 
8
it under the terms of the GNU General Public License as published by
 
9
the Free Software Foundation; either version 2 of the License, or
 
10
(at your option) any later version.
 
11
 
 
12
This program is distributed in the hope that it will be useful,
 
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
GNU General Public License for more details.
 
16
 
 
17
You should have received a copy of the GNU General Public License
 
18
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
*********************************************************************/
 
20
#include "fb_backend.h"
 
21
#include "composite.h"
 
22
#include "logging.h"
 
23
#include "logind.h"
 
24
#include "scene_qpainter_fb_backend.h"
 
25
#include "screens_fb.h"
 
26
#include "virtual_terminal.h"
 
27
// system
 
28
#include <fcntl.h>
 
29
#include <unistd.h>
 
30
#include <sys/ioctl.h>
 
31
#include <sys/mman.h>
 
32
// Linux
 
33
#include <linux/fb.h>
 
34
 
 
35
namespace KWin
 
36
{
 
37
 
 
38
FramebufferBackend::FramebufferBackend(QObject *parent)
 
39
    : AbstractBackend(parent)
 
40
{
 
41
    setSoftWareCursor(true);
 
42
}
 
43
 
 
44
FramebufferBackend::~FramebufferBackend()
 
45
{
 
46
    unmap();
 
47
    if (m_fd >= 0) {
 
48
        close(m_fd);
 
49
    }
 
50
}
 
51
 
 
52
Screens *FramebufferBackend::createScreens(QObject *parent)
 
53
{
 
54
    return new FramebufferScreens(this, parent);
 
55
}
 
56
 
 
57
QPainterBackend *FramebufferBackend::createQPainterBackend()
 
58
{
 
59
    return new FramebufferQPainterBackend(this);
 
60
}
 
61
 
 
62
void FramebufferBackend::init()
 
63
{
 
64
    LogindIntegration *logind = LogindIntegration::self();
 
65
    auto takeControl = [logind, this]() {
 
66
        if (logind->hasSessionControl()) {
 
67
            openFrameBuffer();
 
68
        } else {
 
69
            logind->takeControl();
 
70
            connect(logind, &LogindIntegration::hasSessionControlChanged, this, &FramebufferBackend::openFrameBuffer);
 
71
        }
 
72
    };
 
73
    if (logind->isConnected()) {
 
74
        takeControl();
 
75
    } else {
 
76
        connect(logind, &LogindIntegration::connectedChanged, this, takeControl);
 
77
    }
 
78
    VirtualTerminal::create(this);
 
79
}
 
80
 
 
81
void FramebufferBackend::openFrameBuffer()
 
82
{
 
83
    VirtualTerminal::self()->init();
 
84
    int fd = LogindIntegration::self()->takeDevice(deviceIdentifier().constData());
 
85
    if (fd < 0) {
 
86
        qCWarning(KWIN_FB) << "Failed to open frame buffer device through logind, trying without";
 
87
    }
 
88
    fd = open(deviceIdentifier().constData(), O_RDWR | O_CLOEXEC);
 
89
    if (fd < 0) {
 
90
        qCWarning(KWIN_FB) << "failed to open frame buffer device";
 
91
        return;
 
92
    }
 
93
    m_fd = fd;
 
94
    queryScreenInfo();
 
95
    setReady(true);
 
96
    emit screensQueried();
 
97
}
 
98
 
 
99
bool FramebufferBackend::queryScreenInfo()
 
100
{
 
101
    if (m_fd < 0) {
 
102
        return false;
 
103
    }
 
104
 
 
105
    fb_var_screeninfo varinfo;
 
106
    fb_fix_screeninfo fixinfo;
 
107
 
 
108
    // Probe the device for screen information.
 
109
    if (ioctl(m_fd, FBIOGET_FSCREENINFO, &fixinfo) < 0 || ioctl(m_fd, FBIOGET_VSCREENINFO, &varinfo) < 0) {
 
110
        return false;
 
111
    }
 
112
    m_resolution = QSize(varinfo.xres, varinfo.yres);
 
113
    m_physicalSize = QSize(varinfo.width, varinfo.height);
 
114
    m_id = QByteArray(fixinfo.id);
 
115
    m_red = {varinfo.red.offset, varinfo.red.length};
 
116
    m_green = {varinfo.green.offset, varinfo.green.length};
 
117
    m_blue = {varinfo.blue.offset, varinfo.blue.length};
 
118
    m_alpha = {varinfo.transp.offset, varinfo.transp.length};
 
119
    m_bitsPerPixel = varinfo.bits_per_pixel;
 
120
    m_bufferLength = fixinfo.smem_len;
 
121
    m_bytesPerLine = fixinfo.line_length;
 
122
 
 
123
    return true;
 
124
}
 
125
 
 
126
void FramebufferBackend::map()
 
127
{
 
128
    if (m_memory) {
 
129
        // already mapped;
 
130
        return;
 
131
    }
 
132
    if (m_fd < 0) {
 
133
        // not valid
 
134
        return;
 
135
    }
 
136
    void *mem = mmap(nullptr, m_bufferLength, PROT_WRITE, MAP_SHARED, m_fd, 0);
 
137
    if (mem == MAP_FAILED) {
 
138
        qCWarning(KWIN_FB) << "Failed to mmap frame buffer";
 
139
        return;
 
140
    }
 
141
    m_memory = mem;
 
142
}
 
143
 
 
144
void FramebufferBackend::unmap()
 
145
{
 
146
    if (!m_memory) {
 
147
        return;
 
148
    }
 
149
    if (munmap(m_memory, m_bufferLength) < 0) {
 
150
        qCWarning(KWIN_FB) << "Failed to munmap frame buffer";
 
151
    }
 
152
    m_memory = nullptr;
 
153
}
 
154
 
 
155
QImage::Format FramebufferBackend::imageFormat() const
 
156
{
 
157
    if (m_fd < 0) {
 
158
        return QImage::Format_Invalid;
 
159
    }
 
160
    if (m_alpha.length == 0 &&
 
161
            m_red.length == 8 &&
 
162
            m_green.length == 8 &&
 
163
            m_blue.length == 8 &&
 
164
            m_blue.offset == 0 &&
 
165
            m_green.offset == 8 &&
 
166
            m_red.offset == 16) {
 
167
        return QImage::Format_RGB32;
 
168
    }
 
169
    return QImage::Format_Invalid;
 
170
}
 
171
 
 
172
}