-
Notifications
You must be signed in to change notification settings - Fork 35
/
gsd-backlight-linux.c
123 lines (105 loc) · 3.3 KB
/
gsd-backlight-linux.c
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2010-2011 Richard Hughes <richard@hughsie.com>
*
* Licensed under the GNU General Public License Version 2
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdlib.h>
#include "config.h"
#include "gsd-backlight-linux.h"
#include <stdlib.h>
#ifdef HAVE_GUDEV
#include <gudev/gudev.h>
static gchar *
gsd_backlight_helper_get_type (GList *devices, const gchar *type)
{
const gchar *type_tmp;
GList *d;
for (d = devices; d != NULL; d = d->next) {
type_tmp = g_udev_device_get_sysfs_attr (d->data, "type");
if (g_strcmp0 (type_tmp, type) == 0)
return g_strdup (g_udev_device_get_sysfs_path (d->data));
}
return NULL;
}
/*
* Search for a raw backlight interface, raw backlight interfaces registered
* by the drm driver will have the drm-connector as their parent, check the
* drm-connector's enabled sysfs attribute so that we pick the right LCD-panel
* connector on laptops with hybrid-gfx. Fall back to just picking the first
* raw backlight interface if no enabled interface is found.
*/
static gchar *
gsd_backlight_helper_get_raw (GList *devices)
{
GUdevDevice *parent;
const gchar *attr;
GList *d;
for (d = devices; d != NULL; d = d->next) {
attr = g_udev_device_get_sysfs_attr (d->data, "type");
if (g_strcmp0 (attr, "raw") != 0)
continue;
parent = g_udev_device_get_parent (d->data);
if (!parent)
continue;
attr = g_udev_device_get_sysfs_attr (parent, "enabled");
if (!attr || g_strcmp0 (attr, "enabled") != 0)
continue;
return g_strdup (g_udev_device_get_sysfs_path (d->data));
}
return gsd_backlight_helper_get_type (devices, "raw");
}
#endif /* HAVE_GUDEV */
char *
gsd_backlight_helper_get_best_backlight (GsdBacklightType *type)
{
#ifdef HAVE_GUDEV
gchar *path = NULL;
GList *devices;
GUdevClient *client;
client = g_udev_client_new (NULL);
devices = g_udev_client_query_by_subsystem (client, "backlight");
if (devices == NULL)
goto out;
/* search the backlight devices and prefer the types:
* firmware -> platform -> raw */
path = gsd_backlight_helper_get_type (devices, "firmware");
if (path != NULL) {
if (type)
*type = GSD_BACKLIGHT_TYPE_FIRMWARE;
goto out;
}
path = gsd_backlight_helper_get_type (devices, "platform");
if (path != NULL) {
if (type)
*type = GSD_BACKLIGHT_TYPE_PLATFORM;
goto out;
}
path = gsd_backlight_helper_get_raw (devices);
if (path != NULL) {
if (type)
*type = GSD_BACKLIGHT_TYPE_RAW;
goto out;
}
out:
g_object_unref (client);
g_list_foreach (devices, (GFunc) g_object_unref, NULL);
g_list_free (devices);
return path;
#endif /* HAVE_GUDEV */
return NULL;
}