forked from linuxdeepin/dde-daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallpaper-slideshow.go
249 lines (212 loc) · 4.48 KB
/
wallpaper-slideshow.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
package appearance
import (
"encoding/json"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"strconv"
"sync"
"time"
"pkg.deepin.io/dde/daemon/appearance/background"
"pkg.deepin.io/lib/utils"
)
type changeBgFunc func(monitorSpace string, t time.Time)
// wallpaper slideshow scheduler
type WSScheduler struct {
mu sync.Mutex
lastSetBg time.Time //last set wallpaper time
interval time.Duration
quit chan chan struct{}
intervalChanged chan struct{}
running bool
fn changeBgFunc
}
func newWSScheduler(fun changeBgFunc) *WSScheduler {
s := &WSScheduler{
quit: make(chan chan struct{}),
intervalChanged: make(chan struct{}),
fn: fun,
}
return s
}
func (s *WSScheduler) remainDuration() time.Duration {
s.mu.Lock()
defer s.mu.Unlock()
elapsed := time.Since(s.lastSetBg)
if elapsed < 0 {
elapsed = 0
}
result := s.interval - elapsed
return result
}
func (s *WSScheduler) loopCheck(mointorSpace string) {
s.running = true
for {
select {
case <-s.intervalChanged:
continue
case t := <-time.After(s.remainDuration()):
if s.fn != nil {
go s.fn(mointorSpace, t)
}
s.lastSetBg = t
case ch := <-s.quit:
s.running = false
close(ch)
return
}
}
}
func (s *WSScheduler) updateInterval(monitorSpace string, v time.Duration) {
if v < time.Second {
v = time.Second
}
logger.Debug("update interval", v)
s.mu.Lock()
s.interval = v
if s.running {
s.mu.Unlock()
s.intervalChanged <- struct{}{}
return
}
s.mu.Unlock()
go s.loopCheck(monitorSpace)
}
func (s *WSScheduler) stop() {
s.mu.Lock()
defer s.mu.Unlock()
if !s.running {
return
}
logger.Debug("stop")
ch := make(chan struct{})
s.quit <- ch
// wait quit
<-ch
}
// wallpaper slideshow config
type WSConfig struct {
LastChange time.Time
Showed []string
}
func loadWSConfig(filename string) (mapMonitorWorkspaceWSConfig, error) {
var cfg mapMonitorWorkspaceWSConfig
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
err = json.Unmarshal(data, &cfg)
if err != nil {
return nil, err
}
return cfg, nil
}
func loadWSConfigSafe(filename string) mapMonitorWorkspaceWSConfig {
cfg, _ := loadWSConfig(filename)
return cfg
}
func (c mapMonitorWorkspaceWSConfig) save(filename string) error {
data, err := json.Marshal(c)
if err != nil {
return err
}
dir := filepath.Dir(filename)
err = os.MkdirAll(dir, 0755)
if err != nil {
return err
}
err = ioutil.WriteFile(filename, data, 0644)
return err
}
// wallpaper slideshow loop
type WSLoop struct {
mu sync.Mutex
rand *rand.Rand
showed map[string]struct{}
all []string
fsChanged bool
}
func newWSLoop() *WSLoop {
return &WSLoop{
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
showed: make(map[string]struct{}),
fsChanged: true,
}
}
func (wrl *WSLoop) GetShowed() []string {
wrl.mu.Lock()
result := make([]string, 0, len(wrl.showed))
for file := range wrl.showed {
result = append(result, file)
}
wrl.mu.Unlock()
return result
}
func (wrl *WSLoop) getNotShowed() []string {
if wrl.fsChanged {
bgs := background.ListBackground()
bgFiles := make([]string, 0, len(bgs))
for _, bg := range bgs {
bgFiles = append(bgFiles, utils.DecodeURI(bg.Id))
}
wrl.all = bgFiles
}
var result []string
for _, file := range wrl.all {
_, ok := wrl.showed[file]
if !ok {
result = append(result, file)
}
}
return result
}
func (wrl *WSLoop) getNext() string {
notShowed := wrl.getNotShowed()
if len(notShowed) == 0 {
return ""
}
idx := wrl.rand.Intn(len(notShowed))
next := notShowed[idx]
wrl.showed[next] = struct{}{}
return next
}
func (wrl *WSLoop) reset() {
logger.Debug("WSLoop.reset")
wrl.rand = rand.New(rand.NewSource(time.Now().UnixNano()))
// clear showed
for key := range wrl.showed {
delete(wrl.showed, key)
}
}
func (wrl *WSLoop) AddToShowed(file string) {
file = utils.DecodeURI(file)
wrl.mu.Lock()
wrl.showed[file] = struct{}{}
wrl.mu.Unlock()
}
func (wrl *WSLoop) GetNext() string {
wrl.mu.Lock()
defer wrl.mu.Unlock()
next := wrl.getNext()
if next != "" {
return next
}
if len(wrl.all) > 0 {
wrl.reset()
next = wrl.getNext()
}
return next
}
func (wrl *WSLoop) NotifyFsChanged() {
wrl.mu.Lock()
wrl.fsChanged = true
wrl.mu.Unlock()
}
func isValidWSPolicy(policy string) bool {
if policy == wsPolicyWakeup || policy == wsPolicyLogin || policy == "" {
return true
}
_, err := strconv.ParseUint(policy, 10, 32)
return err == nil
}