View | Details | Raw Unified | Return to bug 251290
Collapse All | Expand All

(-)sysutils/plasma5-powerdevil/files/patch-daemon_backends_upower_backlighthelper.cpp (+192 lines)
Line 0 Link Here
1
--- daemon/backends/upower/backlighthelper.cpp.orig	2020-11-21 13:29:06 UTC
2
+++ daemon/backends/upower/backlighthelper.cpp
3
@@ -32,11 +32,18 @@
4
 
5
 #ifdef Q_OS_FREEBSD
6
 #define USE_SYSCTL
7
+#if __FreeBSD_version >= 1300118
8
+#define USE_FBSDBACKLIGHT
9
 #endif
10
+#endif
11
 
12
 #ifdef USE_SYSCTL
13
 #include <sys/types.h>
14
 #include <sys/sysctl.h>
15
+#ifdef USE_FBSDBACKLIGHT
16
+#include <sys/ioctl.h>
17
+#include <sys/backlight.h>
18
+#endif
19
 
20
 #define HAS_SYSCTL(n) (sysctlbyname(n, nullptr, nullptr, nullptr, 0) == 0)
21
 #endif
22
@@ -44,6 +51,13 @@
23
 #define BACKLIGHT_SYSFS_PATH "/sys/class/backlight/"
24
 #define LED_SYSFS_PATH "/sys/class/leds/"
25
 
26
+#define BACKLIGHT_FREEBSD_PATH		"/dev/backlight/backlight0"
27
+#define BACKLIGHT_QUERY                 0x0001
28
+#define BACKLIGHT_SET_BRIGHTNESS        0x0002
29
+#define BACKLIGHT_INCR                  0x0004
30
+#define BACKLIGHT_DECR                  0x0008
31
+#define BACKLIGHT_INFO                  0x0010
32
+
33
 BacklightHelper::BacklightHelper(QObject *parent) : QObject(parent)
34
 {
35
     init();
36
@@ -53,6 +67,12 @@ void BacklightHelper::init()
37
 {
38
     initUsingBacklightType();
39
 
40
+    // try to use /dev/backlight/backlight0 on FreeBSD
41
+#ifdef USE_FBSDBACKLIGHT
42
+    initUsingFreeBSDBacklight();
43
+#endif
44
+
45
+    // fallback to sysctl
46
     if (m_dirname.isEmpty()) {
47
         initUsingSysctl();
48
 
49
@@ -130,6 +150,20 @@ void BacklightHelper::initUsingBacklightType()
50
 
51
 }
52
 
53
+void BacklightHelper::initUsingFreeBSDBacklight()
54
+{
55
+#ifdef USE_FBSDBACKLIGHT
56
+    QFile file(BACKLIGHT_FREEBSD_PATH);
57
+    if (file.open(QIODevice::ReadWrite)) {
58
+        m_dirname = BACKLIGHT_FREEBSD_PATH;
59
+        m_useFreeBSDBacklight = true;
60
+        file.close();
61
+    } else {
62
+        qCWarning(POWERDEVIL) << "BacklightHelper::initUsingFreeBSDBacklight no RW access on " << BACKLIGHT_FREEBSD_PATH;
63
+    }
64
+#endif
65
+}
66
+
67
 void BacklightHelper::initUsingSysctl()
68
 {
69
 #ifdef USE_SYSCTL
70
@@ -205,11 +239,31 @@ int BacklightHelper::readBrightness() const
71
     }
72
 
73
     int brightness;
74
-
75
-#ifdef USE_SYSCTL
76
-    size_t len = sizeof(int);
77
-    if (sysctlbyname(qPrintable(QStringLiteral("hw.acpi.video.%1.brightness").arg(m_sysctlDevice)), &brightness, &len, nullptr, 0) != 0) {
78
-        return -1;
79
+#ifdef Q_OS_FREEBSD
80
+    if (m_useFreeBSDBacklight == true) {
81
+#ifdef USE_FBSDBACKLIGHT
82
+        struct backlight_props props;
83
+     
84
+        QFile file(m_dirname);
85
+        if (!file.open(QIODevice::ReadWrite)) {
86
+            qCWarning(POWERDEVIL) << "BacklightHelper::readBrightness no RW access on " << BACKLIGHT_FREEBSD_PATH;
87
+            return -1;
88
+        }
89
+        if (ioctl(file.handle(), BACKLIGHTGETSTATUS, &props) == -1) {
90
+            qCWarning(POWERDEVIL) << "BacklightHelper::readBrightness Cannot query the backlight device";
91
+            file.close();
92
+            return -1;
93
+        }
94
+        file.close();
95
+     
96
+        brightness = props.brightness;
97
+        return brightness;
98
+#endif
99
+    } else {
100
+        size_t len = sizeof(int);
101
+        if (sysctlbyname(qPrintable(QStringLiteral("hw.acpi.video.%1.brightness").arg(m_sysctlDevice)), &brightness, &len, nullptr, 0) != 0) {
102
+            return -1;
103
+        }
104
     }
105
 #else
106
     QFile file(m_dirname + "/brightness");
107
@@ -252,27 +306,50 @@ ActionReply BacklightHelper::setbrightness(const QVari
108
 
109
 bool BacklightHelper::writeBrightness(int brightness) const
110
 {
111
-#ifdef USE_SYSCTL
112
-    int actual_level = -1;
113
-    int d1 = 101;
114
-    // Search for the nearest level.
115
-    for (int level : m_sysctlBrightnessLevels) {
116
-        int d2 = qAbs(level - brightness);
117
-        /*
118
-         * The list is sorted, so we break when it starts diverging. There may be repeated values,
119
-         * so we keep going on equal gap (e.g., value = 7.5, levels = 0 0 10 ...: we don't break at
120
-         * the second '0' so we can get to the '10'). This also means that the value will always
121
-         * round off to the bigger level when in the middle (e.g., value = 5, levels = 0 10 ...:
122
-         * value rounds off to 10).
123
-         */
124
-        if (d2 > d1) {
125
-            break;
126
+
127
+#ifdef Q_OS_FREEBSD
128
+    if (m_useFreeBSDBacklight == true) {
129
+#ifdef USE_FBSDBACKLIGHT
130
+        struct backlight_props props;
131
+    
132
+        props.brightness = brightness;
133
+    
134
+        QFile file(m_dirname);
135
+        if (!file.open(QIODevice::ReadWrite)) {
136
+            qCWarning(POWERDEVIL) << "BacklightHelper::writeBrightness no RW access on " << BACKLIGHT_FREEBSD_PATH;
137
+            return false;
138
         }
139
-        actual_level = level;
140
-        d1 = d2;
141
+        if (ioctl(file.handle(), BACKLIGHTUPDATESTATUS, &props) == -1) {
142
+            qCWarning(POWERDEVIL) << "BacklightHelper::writeBrightness Cannot query the backlight device";
143
+            file.close();
144
+            return false;
145
+        }
146
+        file.close();
147
+    
148
+        return true;
149
+#endif
150
+    } else {
151
+        int actual_level = -1;
152
+        int d1 = 101;
153
+        // Search for the nearest level.
154
+        for (int level : m_sysctlBrightnessLevels) {
155
+            int d2 = qAbs(level - brightness);
156
+            /*
157
+             * The list is sorted, so we break when it starts diverging. There may be repeated values,
158
+             * so we keep going on equal gap (e.g., value = 7.5, levels = 0 0 10 ...: we don't break at
159
+             * the second '0' so we can get to the '10'). This also means that the value will always
160
+             * round off to the bigger level when in the middle (e.g., value = 5, levels = 0 10 ...:
161
+             * value rounds off to 10).
162
+             */
163
+            if (d2 > d1) {
164
+                break;
165
+            }
166
+            actual_level = level;
167
+            d1 = d2;
168
+        }
169
+        size_t len = sizeof(int);
170
+        return sysctlbyname(qPrintable(QStringLiteral("hw.acpi.video.%1.brightness").arg(m_sysctlDevice)), nullptr, nullptr, &actual_level, len) == 0;
171
     }
172
-    size_t len = sizeof(int);
173
-    return sysctlbyname(qPrintable(QStringLiteral("hw.acpi.video.%1.brightness").arg(m_sysctlDevice)), nullptr, nullptr, &actual_level, len) == 0;
174
 #else
175
     QFile file(m_dirname + QLatin1String("/brightness"));
176
     if (!file.open(QIODevice::WriteOnly)) {
177
@@ -321,9 +398,12 @@ ActionReply BacklightHelper::brightnessmax(const QVari
178
 
179
     // maximum brightness
180
     int max_brightness;
181
-
182
-#ifdef USE_SYSCTL
183
-    max_brightness = m_sysctlBrightnessLevels.last();
184
+#ifdef Q_OS_FREEBSD
185
+    if (m_useFreeBSDBacklight == true) {
186
+        max_brightness = 100;
187
+    } else {
188
+        max_brightness = m_sysctlBrightnessLevels.last();
189
+    }
190
 #else
191
     QFile file(m_dirname + QLatin1String("/max_brightness"));
192
     if (!file.open(QIODevice::ReadOnly)) {
(-)sysutils/plasma5-powerdevil/files/patch-daemon_backends_upower_backlighthelper.h (+15 lines)
Line 0 Link Here
1
--- daemon/backends/upower/backlighthelper.h.orig	2020-11-21 13:29:19 UTC
2
+++ daemon/backends/upower/backlighthelper.h
3
@@ -55,8 +55,12 @@ public Q_SLOTS: (private)
4
     /**
5
      * FreeBSD (and other BSDs) can control backlight via acpi_video(4)
6
      */
7
+#if defined(__FreeBSD__)
8
+    void initUsingFreeBSDBacklight();
9
+#endif
10
     void initUsingSysctl();
11
 
12
+    bool m_useFreeBSDBacklight = false;
13
     bool m_isSupported = false;
14
     QString m_dirname;
15
     QString m_sysctlDevice;

Return to bug 251290