KWin
Loading...
Searching...
No Matches
context.cpp
Go to the documentation of this file.
1/*
2 KWin - the KDE window manager
3 This file is part of the KDE project.
4
5 SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
6
7 SPDX-License-Identifier: GPL-2.0-or-later
8*/
9#include "context.h"
10#include "events.h"
11#include "libinput_logging.h"
12
13#include "core/session.h"
14#include "utils/udev.h"
15
16#include <fcntl.h>
17#include <unistd.h>
18
19#include <cerrno>
20
21namespace KWin
22{
23namespace LibInput
24{
25
26static void libinputLogHandler(libinput *libinput, libinput_log_priority priority, const char *format, va_list args)
27{
28 char buf[1024];
29 if (std::vsnprintf(buf, 1023, format, args) <= 0) {
30 return;
31 }
32 switch (priority) {
33 case LIBINPUT_LOG_PRIORITY_DEBUG:
34 qCDebug(KWIN_LIBINPUT) << "Libinput:" << buf;
35 break;
36 case LIBINPUT_LOG_PRIORITY_INFO:
37 qCInfo(KWIN_LIBINPUT) << "Libinput:" << buf;
38 break;
39 case LIBINPUT_LOG_PRIORITY_ERROR:
40 default:
41 qCCritical(KWIN_LIBINPUT) << "Libinput:" << buf;
42 break;
43 }
44}
45
46Context::Context(Session *session, std::unique_ptr<Udev> &&udev)
47 : m_session(session)
48 , m_libinput(libinput_udev_create_context(&Context::s_interface, this, *udev.get()))
49 , m_suspended(false)
50 , m_udev(std::move(udev))
51{
52 libinput_log_set_priority(m_libinput, LIBINPUT_LOG_PRIORITY_DEBUG);
53 libinput_log_set_handler(m_libinput, &libinputLogHandler);
54}
55
57{
58 if (m_libinput) {
59 libinput_unref(m_libinput);
60 }
61}
62
64{
65 if (!isValid()) {
66 return false;
67 }
68 return libinput_udev_assign_seat(m_libinput, m_session->seat().toUtf8().constData()) == 0;
69}
70
72{
73 return m_session;
74}
75
77{
78 if (!isValid()) {
79 return -1;
80 }
81 return libinput_get_fd(m_libinput);
82}
83
85{
86 libinput_dispatch(m_libinput);
87}
88
89const struct libinput_interface Context::s_interface = {
90 Context::openRestrictedCallback,
91 Context::closeRestrictedCallBack,
92};
93
94int Context::openRestrictedCallback(const char *path, int flags, void *user_data)
95{
96 return ((Context *)user_data)->openRestricted(path, flags);
97}
98
99void Context::closeRestrictedCallBack(int fd, void *user_data)
100{
101 ((Context *)user_data)->closeRestricted(fd);
102}
103
104int Context::openRestricted(const char *path, int flags)
105{
106 int fd = m_session->openRestricted(path);
107 if (fd < 0) {
108 // failed
109 return fd;
110 }
111 // adjust flags - based on Weston (logind-util.c)
112 int fl = fcntl(fd, F_GETFL);
113 auto errorHandling = [fd, this]() {
114 close(fd);
115 closeRestricted(fd);
116 };
117 if (fl < 0) {
118 errorHandling();
119 return -1;
120 }
121
122 if (flags & O_NONBLOCK) {
123 fl |= O_NONBLOCK;
124 }
125
126 if (fcntl(fd, F_SETFL, fl) < 0) {
127 errorHandling();
128 return -1;
129 }
130
131 fl = fcntl(fd, F_GETFD);
132 if (fl < 0) {
133 errorHandling();
134 return -1;
135 }
136
137 if (!(flags & O_CLOEXEC)) {
138 fl &= ~FD_CLOEXEC;
139 }
140
141 if (fcntl(fd, F_SETFD, fl) < 0) {
142 errorHandling();
143 return -1;
144 }
145 return fd;
146}
147
148void Context::closeRestricted(int fd)
149{
150 m_session->closeRestricted(fd);
151}
152
153std::unique_ptr<Event> Context::event()
154{
155 return Event::create(libinput_get_event(m_libinput));
156}
157
159{
160 if (m_suspended) {
161 return;
162 }
163 libinput_suspend(m_libinput);
164 m_suspended = true;
165}
166
168{
169 if (!m_suspended) {
170 return;
171 }
172 libinput_resume(m_libinput);
173 m_suspended = false;
174}
175
176}
177}
static std::unique_ptr< Event > create(libinput_event *event)
Definition events.cpp:19
virtual int openRestricted(const QString &fileName)=0
virtual QString seat() const =0
virtual void closeRestricted(int fileDescriptor)=0
int libinput_dispatch(struct libinput *libinput)
void libinput_log_set_handler(struct libinput *libinput, libinput_log_handler log_handler)
int libinput_resume(struct libinput *libinput)
struct libinput_event * libinput_get_event(struct libinput *libinput)
void libinput_suspend(struct libinput *libinput)
struct libinput * libinput_udev_create_context(const struct libinput_interface *interface, void *user_data, struct udev *udev)
int libinput_udev_assign_seat(struct libinput *libinput, const char *seat_id)
void libinput_log_set_priority(struct libinput *libinput, enum libinput_log_priority priority)
int libinput_get_fd(struct libinput *libinput)
struct libinput * libinput_unref(struct libinput *libinput)
GLenum format
Definition gltexture.cpp:49
Context(Session *session, std::unique_ptr< Udev > &&udev)
Definition context.cpp:46
static int openRestrictedCallback(const char *path, int flags, void *user_data)
Definition context.cpp:94
Session * session() const
Definition context.cpp:71
bool isValid() const
Definition context.h:31
std::unique_ptr< Event > event()
Definition context.cpp:153
static void closeRestrictedCallBack(int fd, void *user_data)
Definition context.cpp:99