forked from linuxdeepin/dde-daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
278 lines (245 loc) · 6.32 KB
/
manager.go
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
/*
* Copyright (C) 2014 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: jouyouyun <jouyouwen717@gmail.com>
*
* 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 3 of the License, or
* 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, see <http://www.gnu.org/licenses/>.
*/
package sessionwatcher
import (
"sync"
"github.com/godbus/dbus"
libdisplay "github.com/linuxdeepin/go-dbus-factory/com.deepin.daemon.display"
login1 "github.com/linuxdeepin/go-dbus-factory/org.freedesktop.login1"
"pkg.deepin.io/lib/dbusutil"
"pkg.deepin.io/lib/dbusutil/proxy"
)
//go:generate dbusutil-gen em -type Manager
const (
dbusServiceName = "com.deepin.daemon.SessionWatcher"
dbusPath = "/com/deepin/daemon/SessionWatcher"
dbusInterface = dbusServiceName
)
type Manager struct {
service *dbusutil.Service
display *libdisplay.Display
loginManager *login1.Manager
systemSigLoop *dbusutil.SignalLoop
mu sync.Mutex
sessions map[string]*login1.Session
activeSessionType string
PropsMu sync.RWMutex
IsActive bool
}
var (
_validSessionList = []string{
"x11",
"wayland",
}
)
func newManager(service *dbusutil.Service) (*Manager, error) {
manager := &Manager{
service: service,
sessions: make(map[string]*login1.Session),
}
systemConn, err := dbus.SystemBus()
if err != nil {
return nil, err
}
sessionConn := service.Conn()
manager.loginManager = login1.NewManager(systemConn)
manager.display = libdisplay.NewDisplay(sessionConn)
manager.systemSigLoop = dbusutil.NewSignalLoop(systemConn, 10)
manager.systemSigLoop.Start()
manager.loginManager.InitSignalExt(manager.systemSigLoop, true)
// default as active
manager.IsActive = true
return manager, nil
}
func (m *Manager) destroy() {
m.mu.Lock()
for _, session := range m.sessions {
session.RemoveHandler(proxy.RemoveAllHandlers)
}
m.mu.Unlock()
m.loginManager.RemoveHandler(proxy.RemoveAllHandlers)
m.systemSigLoop.Stop()
}
func (*Manager) GetInterfaceName() string {
return dbusInterface
}
func (m *Manager) initUserSessions() {
sessions, err := m.loginManager.ListSessions(0)
if err != nil {
logger.Warning("List sessions failed:", err)
return
}
for _, session := range sessions {
m.addSession(session.SessionId, session.Path)
}
m.handleSessionChanged()
_, err = m.loginManager.ConnectSessionNew(func(id string, path dbus.ObjectPath) {
logger.Debug("Session added:", id, path)
m.addSession(id, path)
m.handleSessionChanged()
})
if err != nil {
logger.Warning("ConnectSessionNew error:", err)
}
_, err = m.loginManager.ConnectSessionRemoved(func(id string, path dbus.ObjectPath) {
logger.Debug("Session removed:", id, path)
m.deleteSession(id, path)
m.handleSessionChanged()
})
if err != nil {
logger.Warning("ConnectSessionRemoved error:", err)
}
}
func (m *Manager) addSession(id string, path dbus.ObjectPath) {
systemConn := m.systemSigLoop.Conn()
session, err := login1.NewSession(systemConn, path)
if err != nil {
logger.Warning(err)
return
}
userInfo, err := session.User().Get(0)
if err != nil {
logger.Warning(err)
return
}
uid := userInfo.UID
logger.Debug("Add session:", id, path, uid)
if !isCurrentUser(uid) {
logger.Debug("Not the current user session:", id, path, uid)
return
}
remote, err := session.Remote().Get(0)
if err != nil {
logger.Warning(err)
return
}
if remote {
logger.Debugf("session %v is remote", id)
return
}
m.mu.Lock()
m.sessions[id] = session
m.mu.Unlock()
session.InitSignalExt(m.systemSigLoop, true)
err = session.Active().ConnectChanged(func(hasValue bool, value bool) {
m.handleSessionChanged()
})
if err != nil {
logger.Warning("ConnectChanged error:", err)
}
}
func (m *Manager) deleteSession(id string, path dbus.ObjectPath) {
m.mu.Lock()
session, ok := m.sessions[id]
if !ok {
m.mu.Unlock()
return
}
session.RemoveHandler(proxy.RemoveAllHandlers)
logger.Debug("Delete session:", id, path)
delete(m.sessions, id)
m.mu.Unlock()
}
func (m *Manager) handleSessionChanged() {
m.mu.Lock()
defer m.mu.Unlock()
if len(m.sessions) == 0 {
return
}
session := m.getActiveSession()
var isActive bool
var sessionType string
if session != nil {
isActive = true
var err error
sessionType, err = session.Type().Get(0)
if err != nil {
logger.Warning(err)
}
}
m.activeSessionType = sessionType
m.PropsMu.Lock()
changed := m.setIsActive(isActive)
m.PropsMu.Unlock()
if !changed {
return
}
if isActive {
logger.Debug("[handleSessionChanged] Resume pulse")
// fixed block when unused pulse-audio
go suspendPulseSinks(0)
go suspendPulseSources(0)
logger.Debug("[handleSessionChanged] Refresh Brightness")
go func() {
_ = m.display.RefreshBrightness(0)
}()
} else {
logger.Debug("[handleSessionChanged] Suspend pulse")
go suspendPulseSinks(1)
go suspendPulseSources(1)
}
}
// return is changed?
func (m *Manager) setIsActive(val bool) bool {
if m.IsActive != val {
m.IsActive = val
logger.Debug("[setIsActive] IsActive changed:", val)
err := m.service.EmitPropertyChanged(m, "IsActive", val)
if err != nil {
logger.Warning("EmitPropertyChanged error:", err)
}
return true
}
return false
}
func (m *Manager) getActiveSession() *login1.Session {
for _, session := range m.sessions {
active, err := session.Active().Get(0)
if err != nil {
logger.Warning(err)
continue
}
if active {
return session
}
}
return nil
}
func (m *Manager) IsX11SessionActive() (active bool, busErr *dbus.Error) {
m.mu.Lock()
ty := m.activeSessionType
m.mu.Unlock()
for _, session := range _validSessionList {
if session == ty {
return true, nil
}
}
return false, nil
}
func (m *Manager) GetSessions() (sessions []dbus.ObjectPath, err *dbus.Error) {
m.mu.Lock()
sessions = make([]dbus.ObjectPath, len(m.sessions))
i := 0
for _, session := range m.sessions {
sessions[i] = session.Path_()
i++
}
m.mu.Unlock()
return
}