KWin
Loading...
Searching...
No Matches
filedescriptor.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:: 2022 Xaver Hugl <xaver.hugl@gmail.com>
6
7 SPDX-License-Identifier: GPL-2.0-or-later
8*/
9#include "filedescriptor.h"
10
11#include <fcntl.h>
12#include <sys/poll.h>
13#include <unistd.h>
14#include <utility>
15
16namespace KWin
17{
18
20 : m_fd(fd)
21{
22}
23
25 : m_fd(std::exchange(other.m_fd, -1))
26{
27}
28
30{
31 if (m_fd != -1) {
32 ::close(m_fd);
33 }
34 m_fd = std::exchange(other.m_fd, -1);
35 return *this;
36}
37
39{
40 if (m_fd != -1) {
41 ::close(m_fd);
42 }
43}
44
46{
47 return m_fd != -1;
48}
49
51{
52 return m_fd;
53}
54
56{
57 return std::exchange(m_fd, -1);
58}
59
61{
62 if (m_fd != -1) {
63 ::close(m_fd);
64 m_fd = -1;
65 }
66}
67
69{
70 if (m_fd != -1) {
71 return FileDescriptor{fcntl(m_fd, F_DUPFD_CLOEXEC, 0)};
72 } else {
73 return {};
74 }
75}
76
78{
79 return isClosed(m_fd);
80}
81
83{
84 return isReadable(m_fd);
85}
86
88{
89 pollfd pfd = {
90 .fd = fd,
91 .events = POLLIN,
92 .revents = 0,
93 };
94 if (poll(&pfd, 1, 0) < 0) {
95 return true;
96 }
97 return pfd.revents & (POLLHUP | POLLERR);
98}
99
101{
102 pollfd pfd = {
103 .fd = fd,
104 .events = POLLIN,
105 .revents = 0,
106 };
107 return poll(&pfd, 1, 0) && (pfd.revents & (POLLIN | POLLNVAL)) != 0;
108}
109}
FileDescriptor & operator=(FileDescriptor &&)
FileDescriptor()=default
FileDescriptor duplicate() const