gitee.com/mysnapcore/mysnapd@v0.1.0/interfaces/builtin/display_control.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2019 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package builtin 21 22 import ( 23 "bytes" 24 "fmt" 25 "path" 26 "path/filepath" 27 28 "gitee.com/mysnapcore/mysnapd/interfaces" 29 "gitee.com/mysnapcore/mysnapd/interfaces/apparmor" 30 ) 31 32 const displayControlSummary = `allows configuring display parameters` 33 34 const displayControlBaseDeclarationSlots = ` 35 display-control: 36 allow-installation: 37 slot-snap-type: 38 - core 39 deny-auto-connection: true 40 ` 41 42 // gnome-settings-daemon also provides an API via setting the Brightness 43 // property via a Set() method on the org.gnome.SettingsDaemon.Power.Screen 44 // interface, but we can't mediate member data. This could instead be supported 45 // via userd... 46 const displayControlConnectedPlugAppArmor = ` 47 # Description: This interface allows getting information about a connected 48 # display and setting parameters like backlight brightness. 49 50 # keyboard backlight key 51 /sys/class/leds/ r, 52 /sys/devices/**/leds/**kbd_backlight/{,**} r, 53 /sys/devices/**/leds/**kbd_backlight/brightness w, 54 55 # upower 56 #include <abstractions/dbus-strict> 57 dbus (send) 58 bus=system 59 path=/org/freedesktop/UPower/KbdBacklight 60 interface=org.freedesktop.DBus.Introspectable 61 member=Introspect 62 peer=(label=unconfined), 63 dbus (send) 64 bus=system 65 path=/org/freedesktop/UPower/KbdBacklight 66 interface=org.freedesktop.UPower.KbdBacklight 67 member={GetBrightness,GetMaxBrightness,SetBrightness} 68 peer=(label=unconfined), 69 70 # gnome-settings-daemon 71 #include <abstractions/dbus-session-strict> 72 dbus (send) 73 bus=session 74 path=/org/gnome/SettingsDaemon/Power 75 interface=org.freedesktop.DBus.Introspectable 76 member=Introspect 77 peer=(label=unconfined), 78 dbus (send) 79 bus=session 80 path=/org/gnome/SettingsDaemon/Power 81 interface=org.gnome.SettingsDaemon.Power.Screen 82 member=Step{Down,Up} 83 peer=(label=unconfined), 84 85 /sys/class/backlight/ r, 86 87 # Allow changing backlight 88 /sys/devices/**/**/drm/card[0-9]/card[0-9]*/*_backlight/brightness w, 89 ` 90 91 type displayControlInterface struct { 92 commonInterface 93 } 94 95 func (iface *displayControlInterface) dereferencedBacklightPaths() []string { 96 var paths []string 97 sysClass := "/sys/class/backlight" 98 dirs, err := readDir(sysClass) 99 if err != nil { 100 return paths 101 } 102 103 for _, s := range dirs { 104 p, err := evalSymlinks(filepath.Join(sysClass, s.Name())) 105 if err != nil { 106 continue 107 } 108 paths = append(paths, filepath.Clean(p)) 109 } 110 return paths 111 } 112 113 func (iface *displayControlInterface) AppArmorConnectedPlug(spec *apparmor.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error { 114 // add the static rules 115 spec.AddSnippet(displayControlConnectedPlugAppArmor) 116 117 // add the detected rules 118 for _, p := range iface.dereferencedBacklightPaths() { 119 var buf bytes.Buffer 120 fmt.Fprintf(&buf, "# autodetected backlight: %s\n", path.Base(p)) 121 fmt.Fprintf(&buf, "%s/{,**} r,\n", p) 122 fmt.Fprintf(&buf, "%s/bl_power w,\n", p) 123 fmt.Fprintf(&buf, "%s/brightness w,\n", p) 124 spec.AddSnippet(buf.String()) 125 } 126 127 return nil 128 } 129 130 func init() { 131 registerIface(&displayControlInterface{commonInterface{ 132 name: "display-control", 133 summary: displayControlSummary, 134 implicitOnCore: true, 135 implicitOnClassic: true, 136 baseDeclarationSlots: displayControlBaseDeclarationSlots, 137 connectedPlugAppArmor: displayControlConnectedPlugAppArmor, 138 }}) 139 }