File: xrandrbrightness.cpp

package info (click to toggle)
lxqt-config 0.14.1-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 3,996 kB
  • sloc: cpp: 11,275; makefile: 13; sh: 4
file content (410 lines) | stat: -rw-r--r-- 14,070 bytes parent folder | download | duplicates (2)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*  This file is part of the KDE project
 *    Copyright (C) 2010 Lukas Tinkl <ltinkl@redhat.com>
 *    Copyright (C) 2015 Kai Uwe Broulik <kde@privat.broulik.de>
 * 
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Library General Public
 *    License version 2 as published by the Free Software Foundation.
 * 
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Library General Public License for more details.
 * 
 *    You should have received a copy of the GNU Library General Public License
 *    along with this library; see the file COPYING.LIB.  If not, write to
 *    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *    Boston, MA 02110-1301, USA.
 * 
 */


#include <QX11Info>
#include <QDebug>

#include "xrandrbrightness.h"

XRandrBrightness::XRandrBrightness()
{
    if (!QX11Info::isPlatformX11()) {
        return;
    }
    ScopedCPointer<xcb_randr_query_version_reply_t> versionReply(xcb_randr_query_version_reply(QX11Info::connection(),
        xcb_randr_query_version(QX11Info::connection(), 1, 2),
    nullptr));

    if (!versionReply) {
        qDebug() << "RandR Query version returned null";
        return;
    }

    if (versionReply->major_version < 1 || (versionReply->major_version == 1 && versionReply->minor_version < 2)) {
        qDebug() << "RandR version" << versionReply->major_version << "." << versionReply->minor_version << " too old";
        return;
    }
    ScopedCPointer<xcb_intern_atom_reply_t> backlightReply(xcb_intern_atom_reply(QX11Info::connection(),
        xcb_intern_atom (QX11Info::connection(), 1, strlen("Backlight"), "Backlight"),
    nullptr));

    if (!backlightReply) {
        qDebug() << "Intern Atom for Backlight returned null";
        return;
    }

    m_backlight = backlightReply->atom;

    if (m_backlight == XCB_NONE) {
        qDebug() << "No outputs have backlight property";
        //return;
    }
    xcb_screen_iterator_t iter = xcb_setup_roots_iterator(xcb_get_setup(QX11Info::connection()));
    if (!iter.rem) {
        qDebug() << "XCB Screen Roots Iterator rem was null";
        return;
    }

    xcb_screen_t *screen = iter.data;
    xcb_window_t root = screen->root;

    m_resources.reset(xcb_randr_get_screen_resources_current_reply(QX11Info::connection(),
        xcb_randr_get_screen_resources_current(QX11Info::connection(), root)
    , nullptr));

    if (!m_resources) {
        qDebug() << "RANDR Get Screen Resources returned null";
        return;
    }
}



bool XRandrBrightness::backlight_get_with_range(xcb_randr_output_t output, long &value, long &min, long &max) const {
    long cur = backlight_get(output);
    if (cur == -1) {
       return false;
    }

    ScopedCPointer<xcb_randr_query_output_property_reply_t> propertyReply(xcb_randr_query_output_property_reply(QX11Info::connection(),
        xcb_randr_query_output_property(QX11Info::connection(), output, m_backlight)
    , nullptr));

    if (!propertyReply) {
        return false;
    }

    if (propertyReply->range && xcb_randr_query_output_property_valid_values_length(propertyReply.data()) == 2) {
        int32_t *values = xcb_randr_query_output_property_valid_values(propertyReply.data());
        value = cur;
        min = values[0];
        max = values[1];
        return true;
    }

    return false;
}

long XRandrBrightness::backlight_get(xcb_randr_output_t output) const
{
    ScopedCPointer<xcb_randr_get_output_property_reply_t> propertyReply;
    long value;

    if (m_backlight != XCB_ATOM_NONE) {
        propertyReply.reset(xcb_randr_get_output_property_reply(QX11Info::connection(),
            xcb_randr_get_output_property(QX11Info::connection(), output, m_backlight, XCB_ATOM_NONE, 0, 4, 0, 0)
        , nullptr));

        if (!propertyReply) {
            return -1;
        }
    }

    if (!propertyReply || propertyReply->type != XCB_ATOM_INTEGER || propertyReply->num_items != 1 || propertyReply->format != 32) {
        value = -1;
    } else {
        value = *(reinterpret_cast<long *>(xcb_randr_get_output_property_data(propertyReply.data())));
    }
    return value;
}

void XRandrBrightness::backlight_set(xcb_randr_output_t output, long value)
{
    xcb_randr_change_output_property(QX11Info::connection(), output, m_backlight, XCB_ATOM_INTEGER,
                                     32, XCB_PROP_MODE_REPLACE,
                                     1, reinterpret_cast<unsigned char *>(&value));
}


float XRandrBrightness::gamma_brightness_get(xcb_randr_output_t output)
{
    xcb_generic_error_t  *error;

    xcb_randr_get_output_info_cookie_t output_info_cookie = xcb_randr_get_output_info (QX11Info::connection(), output, 0);
    xcb_randr_get_output_info_reply_t * output_info = xcb_randr_get_output_info_reply (QX11Info::connection(), output_info_cookie, &error);
    if(error != NULL)
    {
        qDebug() << "Error getting output_info";
        return -1;
    }
    if(output_info == NULL)
    {
        qDebug() << "Error: output_info is null";
        return -1;
    }
    // xcb_randr_get_output_info_reply_t tiene como elemento crtc
    xcb_randr_get_crtc_gamma_cookie_t gamma_cookie = xcb_randr_get_crtc_gamma_unchecked (QX11Info::connection(), output_info->crtc);
    xcb_randr_get_crtc_gamma_reply_t *gamma_reply =  xcb_randr_get_crtc_gamma_reply (QX11Info::connection(), gamma_cookie, &error);
    if(error != NULL)
    {
        qDebug() << "Error getting gamma_reply";
        return -1;
    }
    if(gamma_reply == NULL)
    {
        qDebug() << "Error: gamma_reply is null";
        return -1;
    }
    uint16_t *red = xcb_randr_get_crtc_gamma_red (gamma_reply);
    if(red == NULL)
    {
        qDebug() << "Error: red is null";
        return -1;
    }
    int red_length = xcb_randr_get_crtc_gamma_red_length(gamma_reply);
    
    // uint16_t *green = xcb_randr_get_crtc_gamma_green (gamma_reply);
    // if(green == NULL)
    // {
    //     qDebug() << "Error: green is null";
    //     return -1;
    // }
    // uint16_t *blue = xcb_randr_get_crtc_gamma_blue (gamma_reply);
    // if(blue == NULL)
    // {
    //     qDebug() << "Error: blue is null";
    //     return -1;
    // }

    float brightness = (float)red[red_length-1]/65535.0;
    return brightness;
}

void XRandrBrightness::gamma_brightness_set(xcb_randr_output_t output, float percent)
{
    xcb_generic_error_t  *error;

    xcb_randr_get_output_info_cookie_t output_info_cookie = xcb_randr_get_output_info (QX11Info::connection(), output, 0);
    xcb_randr_get_output_info_reply_t * output_info = xcb_randr_get_output_info_reply (QX11Info::connection(), output_info_cookie, &error);
    if(error != NULL)
    {
        qDebug() << "Error getting output_info";
        return;
    }
    if(output_info == NULL)
    {
        qDebug() << "Error: output_info is null";
        return;
    }
    // xcb_randr_get_output_info_reply_t tiene como elemento crtc
    xcb_randr_get_crtc_gamma_cookie_t gamma_cookie = xcb_randr_get_crtc_gamma_unchecked (QX11Info::connection(), output_info->crtc);
    xcb_randr_get_crtc_gamma_reply_t *gamma_reply =  xcb_randr_get_crtc_gamma_reply (QX11Info::connection(), gamma_cookie, &error);
    if(error != NULL)
    {
        qDebug() << "Error getting gamma_reply";
        return;
    }
    if(gamma_reply == NULL)
    {
        qDebug() << "Error: gamma_reply is null";
        return;
    }
    uint16_t *red = xcb_randr_get_crtc_gamma_red (gamma_reply);
    if(red == NULL)
    {
        qDebug() << "Error: red is null";
        return;
    }
    int red_length = xcb_randr_get_crtc_gamma_red_length(gamma_reply);
    uint16_t *green = xcb_randr_get_crtc_gamma_green (gamma_reply);
    if(green == NULL)
    {
        qDebug() << "Error: green is null";
        return;
    }
    uint16_t *blue = xcb_randr_get_crtc_gamma_blue (gamma_reply);
    if(blue == NULL)
    {
        qDebug() << "Error: blue is null";
        return;
    }

    float max_gamma = 65535*percent;
    for(int i=0;i<red_length;i++)
    {
        int value = qMin((int)(((float)i/(float)(red_length-1))*max_gamma),65535);
        green[i] = blue[i] = red[i] = value;
    }
    xcb_randr_set_crtc_gamma (QX11Info::connection(), output_info->crtc, red_length, red, green, blue);
}


QList<MonitorInfo> XRandrBrightness::getMonitorsInfo()
{
    QList<MonitorInfo> monitors;

    if (!m_resources) {
        return monitors;
    }

    auto *outputs = xcb_randr_get_screen_resources_current_outputs(m_resources.data());
    for (int i = 0; i < m_resources->num_outputs; ++i) {
        xcb_randr_output_t output = outputs[i];

        xcb_generic_error_t  *error;

        xcb_randr_get_output_info_cookie_t output_info_cookie = xcb_randr_get_output_info (QX11Info::connection(), output, 0);
        xcb_randr_get_output_info_reply_t * output_info = xcb_randr_get_output_info_reply (QX11Info::connection(), output_info_cookie, &error);
        if(error != NULL)
        {
            qDebug() << "Error getting output_info";
            continue;
        }
        if(output_info == NULL)
        {
            qDebug() << "Error: output_info is null";
            continue;
        }

        QString name = QString::fromUtf8((const char *) xcb_randr_get_output_info_name(output_info), output_info->name_len);

    
        qDebug() << "Found output:" << name;


        // Is connected?
        if ( (xcb_randr_connection_t)(output_info->connection) != XCB_RANDR_CONNECTION_CONNECTED )
        {
            qDebug() << "Output is not connected";
            continue; // This output is not connected. Check other
        }

        // Is enabled?
        if( output_info->crtc == 0)
        {
            qDebug() << "Crtc is not null. Output not enabled.";
            continue;
        }
        xcb_randr_get_crtc_info_cookie_t crtc_info_cookie = xcb_randr_get_crtc_info_unchecked (QX11Info::connection(), output_info->crtc, 0);
        xcb_randr_get_crtc_info_reply_t *crtc_info =xcb_randr_get_crtc_info_reply (QX11Info::connection(), crtc_info_cookie, &error);
        if(error != NULL)
        {
            qDebug() << "Error getting output_info";
            continue;
        }
        if(crtc_info == NULL)
        {
            qDebug() << "Error: output_info is null";
            continue;
        }
        if( crtc_info->mode == XCB_NONE )
        {
            qDebug() << "No modes. Output not enabled.";
            continue;
        }

        // Output is connected and enabled. Get data: 
        bool backlightIsSuported = false;
        long cur, min, max, backlight_max = -1;
        if (backlight_get(output) != -1)
        {
            if (backlight_get_with_range(output, cur, min, max))
            {
                backlightIsSuported = true;
                backlight_max = max - min; 
            }
        }
        
        MonitorInfo monitor((int)output, name, backlight_max);

        if(backlightIsSuported)
            monitor.setBacklight(cur-min);
        
        monitor.setBrightness(gamma_brightness_get(output));
       
        qDebug() << "Output:" << name << "added";
        monitors.append(monitor); 
        
    }

    return monitors;
}

void XRandrBrightness::setMonitorsSettings(QList<MonitorInfo> monitors)
{
    if (!m_resources) {
        return;
    }

    auto *outputs = xcb_randr_get_screen_resources_current_outputs(m_resources.data());
    for (int i = 0; i < m_resources->num_outputs; ++i) {
        xcb_randr_output_t output = outputs[i];

        xcb_generic_error_t  *error;

        xcb_randr_get_output_info_cookie_t output_info_cookie = xcb_randr_get_output_info (QX11Info::connection(), output, 0);
        xcb_randr_get_output_info_reply_t * output_info = xcb_randr_get_output_info_reply (QX11Info::connection(), output_info_cookie, &error);
        if(error != NULL)
        {
            qDebug() << "Error getting output_info";
            continue;
        }
        if(output_info == NULL)
        {
            qDebug() << "Error: output_info is null";
            continue;
        }

        // Is connected?
        if ( (xcb_randr_connection_t)(output_info->connection) != XCB_RANDR_CONNECTION_CONNECTED )
            continue; // This output is not connected. Check other

        // Is enabled?
        if( output_info->crtc == 0)
            continue;
        xcb_randr_get_crtc_info_cookie_t crtc_info_cookie = xcb_randr_get_crtc_info_unchecked (QX11Info::connection(), output_info->crtc, 0);
        xcb_randr_get_crtc_info_reply_t *crtc_info =xcb_randr_get_crtc_info_reply (QX11Info::connection(), crtc_info_cookie, &error);
        if(error != NULL)
            continue;
        if(crtc_info == NULL && crtc_info->mode == XCB_NONE )
            continue;

        QString name = QString::fromUtf8((const char *) xcb_randr_get_output_info_name(output_info), output_info->name_len);

        // Output is connected and enabled. Get data: 
        bool backlightIsSuported = false;
        long cur, min, max, backlight_value = 0;
        if (backlight_get(output) != -1)
        {
            if (backlight_get_with_range(output, cur, min, max))
            {
                backlightIsSuported = true;
                backlight_value = cur - min;
            }
        }
        float brightness_value = gamma_brightness_get(output);

        // Compare output info with settings and set it.
        for(const MonitorInfo &monitor: monitors)
        {
            if(monitor.id() == (int)output && monitor.name() == name)
            {
                // Set settings
                if(backlightIsSuported && monitor.backlight() != backlight_value)
                    backlight_set(output, min+monitor.backlight());
                if(monitor.brightness() != brightness_value)
                    gamma_brightness_set(output, monitor.brightness());
                break;
            }
        }
    }
}