File: display_control.go

package info (click to toggle)
snapd 2.49-1%2Bdeb11u2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 36,432 kB
  • sloc: ansic: 12,125; sh: 8,453; python: 2,163; makefile: 1,284; exp: 173; xml: 22
file content (136 lines) | stat: -rw-r--r-- 3,898 bytes parent folder | download
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
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2019 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 *
 */

package builtin

import (
	"bytes"
	"fmt"
	"path"
	"path/filepath"

	"github.com/snapcore/snapd/interfaces"
	"github.com/snapcore/snapd/interfaces/apparmor"
)

const displayControlSummary = `allows configuring display parameters`

const displayControlBaseDeclarationSlots = `
  display-control:
    allow-installation:
      slot-snap-type:
        - core
    deny-auto-connection: true
`

// gnome-settings-daemon also provides an API via setting the Brightness
// property via a Set() method on the org.gnome.SettingsDaemon.Power.Screen
// interface, but we can't mediate member data. This could instead be supported
// via userd...
const displayControlConnectedPlugAppArmor = `
# Description: This interface allows getting information about a connected
# display and setting parameters like backlight brightness.

# keyboard backlight key
/sys/class/leds/ r,
/sys/devices/**/leds/**kbd_backlight/{,**} r,
/sys/devices/**/leds/**kbd_backlight/brightness w,

# upower
#include <abstractions/dbus-strict>
dbus (send)
    bus=system
    path=/org/freedesktop/UPower/KbdBacklight
    interface=org.freedesktop.DBus.Introspectable
    member=Introspect
    peer=(label=unconfined),
dbus (send)
    bus=system
    path=/org/freedesktop/UPower/KbdBacklight
    interface=org.freedesktop.UPower.KbdBacklight
    member={GetBrightness,GetMaxBrightness,SetBrightness}
    peer=(label=unconfined),

# gnome-settings-daemon
#include <abstractions/dbus-session-strict>
dbus (send)
    bus=session
    path=/org/gnome/SettingsDaemon/Power
    interface=org.freedesktop.DBus.Introspectable
    member=Introspect
    peer=(label=unconfined),
dbus (send)
    bus=session
    path=/org/gnome/SettingsDaemon/Power
    interface=org.gnome.SettingsDaemon.Power.Screen
    member=Step{Down,Up}
    peer=(label=unconfined),

/sys/class/backlight/ r,
`

type displayControlInterface struct {
	commonInterface
}

func (iface *displayControlInterface) dereferencedBacklightPaths() []string {
	var paths []string
	sysClass := "/sys/class/backlight"
	dirs, err := readDir(sysClass)
	if err != nil {
		return paths
	}

	for _, s := range dirs {
		p, err := evalSymlinks(filepath.Join(sysClass, s.Name()))
		if err != nil {
			continue
		}
		paths = append(paths, filepath.Clean(p))
	}
	return paths
}

func (iface *displayControlInterface) AppArmorConnectedPlug(spec *apparmor.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
	// add the static rules
	spec.AddSnippet(displayControlConnectedPlugAppArmor)

	// add the detected rules
	for _, p := range iface.dereferencedBacklightPaths() {
		var buf bytes.Buffer
		fmt.Fprintf(&buf, "# autodetected backlight: %s\n", path.Base(p))
		fmt.Fprintf(&buf, "%s/{,**} r,\n", p)
		fmt.Fprintf(&buf, "%s/bl_power w,\n", p)
		fmt.Fprintf(&buf, "%s/brightness w,\n", p)
		spec.AddSnippet(buf.String())
	}

	return nil
}

func init() {
	registerIface(&displayControlInterface{commonInterface{
		name:                  "display-control",
		summary:               displayControlSummary,
		implicitOnCore:        true,
		implicitOnClassic:     true,
		baseDeclarationSlots:  displayControlBaseDeclarationSlots,
		connectedPlugAppArmor: displayControlConnectedPlugAppArmor,
	}})
}