Skip to content

Commit

Permalink
Fix for Deepin
Browse files Browse the repository at this point in the history
+ fix for transitioning from before midnight to after midnight
  • Loading branch information
xyproto committed Feb 15, 2019
1 parent 8b47e09 commit 80c23b9
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 19 deletions.
9 changes: 8 additions & 1 deletion simpletimed.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ type Transition struct {
}

func (t *Transition) Duration() time.Duration {
return t.UpTo.Sub(t.From)
d := t.UpTo.Sub(t.From)
for d < 0 {
d += h24
}
for d > h24 {
d -= h24
}
return d
}

func (t *Transition) String(format string) string {
Expand Down
83 changes: 66 additions & 17 deletions timed.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ func (stw *SimpleTimedWallpaper) SetInitialWallpaper(verbose bool) error {
//elapsed := time.Now().Sub(s.At)
elapsed := event.ToToday(time.Now()).Sub(event.ToToday(s.At))
window := stw.UntilNext(s.At) - elapsed // duration until next event start, minus time elapsed
for window < 0 {
window += h24
}
for window > h24 {
window -= h24
}
cooldown := window

imageFilename := s.Filename
Expand Down Expand Up @@ -66,6 +72,12 @@ func (stw *SimpleTimedWallpaper) SetInitialWallpaper(verbose bool) error {
now := time.Now()
window := t.Duration()
progress := window - event.ToToday(t.UpTo).Sub(event.ToToday(now))
for progress > h24 {
progress -= h24
}
for progress < 0 {
progress += h24
}
ratio := float64(progress) / float64(window)
from := t.From
steps := 10
Expand All @@ -75,6 +87,8 @@ func (stw *SimpleTimedWallpaper) SetInitialWallpaper(verbose bool) error {
tFromFilename := t.FromFilename
tToFilename := t.ToFilename
loopWait := stw.LoopWait
tempDir := ""
var err error

if verbose {
fmt.Printf("Initial transition event at %s (%d%% complete)\n", c(from), int(ratio*100))
Expand All @@ -88,7 +102,12 @@ func (stw *SimpleTimedWallpaper) SetInitialWallpaper(verbose bool) error {
fmt.Println("To filename", tToFilename)
}

tempDir, err := ioutil.TempDir("", "crossfade")
if exists(tempDir) {
// Clean up
os.RemoveAll(tempDir)
}

tempDir, err = ioutil.TempDir("", "crossfade")
if err != nil {
return fmt.Errorf("Could not create temporary directory: %v\n", err)
}
Expand Down Expand Up @@ -127,9 +146,6 @@ func (stw *SimpleTimedWallpaper) SetInitialWallpaper(verbose bool) error {
return fmt.Errorf("Could not set wallpaper: %v\n", err)
}

// Clean up
os.RemoveAll(tempDir)

// Just sleep for half the cooldown, to have some time to register events too
fmt.Println("Sleeping for", cooldown/2)
time.Sleep(cooldown / 2)
Expand Down Expand Up @@ -158,6 +174,12 @@ func (stw *SimpleTimedWallpaper) EventLoop(verbose bool) error {
// Place values into variables, before enclosing it in the function below.
from := s.At
window := stw.UntilNext(s.At) // duration until next event start
for window < 0 {
window += h24
}
for window > h24 {
window -= h24
}
cooldown := window
imageFilename := s.Filename

Expand Down Expand Up @@ -209,11 +231,19 @@ func (stw *SimpleTimedWallpaper) EventLoop(verbose bool) error {
tFromFilename := t.FromFilename
tToFilename := t.ToFilename
loopWait := stw.LoopWait
tempDir := ""
var err error

// Register a transition event
//eventloop.Add(event.New(from, window, cooldown, event.ProgressWrapperInterval(from, upTo, loopWait, func(ratio float64) {
eventloop.Add(event.New(from, window, cooldown, func() {
progress := window - event.ToToday(upTo).Sub(event.ToToday(time.Now()))
for progress > h24 {
progress -= h24
}
for progress < 0 {
progress += h24
}
ratio := float64(progress) / float64(window)

if verbose {
Expand All @@ -228,7 +258,12 @@ func (stw *SimpleTimedWallpaper) EventLoop(verbose bool) error {
fmt.Println("To filename", tToFilename)
}

tempDir, err := ioutil.TempDir("", "crossfade")
if exists(tempDir) {
// Clean up
os.RemoveAll(tempDir)
}

tempDir, err = ioutil.TempDir("", "crossfade")
if err != nil {
fmt.Fprintf(os.Stderr, "Could not create temporary directory: %v\n", err)
return // return from anon func
Expand Down Expand Up @@ -260,9 +295,6 @@ func (stw *SimpleTimedWallpaper) EventLoop(verbose bool) error {
fmt.Fprintf(os.Stderr, "Could not set wallpaper: %v\n", err)
return // return from anon func
}

// The removal of the temporary directory should not be deferred, since this is an endless loop
os.RemoveAll(tempDir) // clean up
}))
}

Expand Down Expand Up @@ -371,10 +403,18 @@ func (gw *GnomeTimedWallpaper) EventLoop(verbose bool) error {
tFromFilename := t.FromFilename
tToFilename := t.ToFilename
loopWait := gw.LoopWait
tempDir := ""
var err error

// Register a transition event
eventloop.Add(event.New(from, window, cooldown, func() {
progress := window - event.ToToday(upTo).Sub(event.ToToday(time.Now()))
for progress > h24 {
progress -= h24
}
for progress < 0 {
progress += h24
}
ratio := float64(progress) / float64(window)
if verbose {
fmt.Printf("Triggered transition event at %s (%d%% complete)\n", c(from), int(ratio*100))
Expand All @@ -388,7 +428,12 @@ func (gw *GnomeTimedWallpaper) EventLoop(verbose bool) error {
fmt.Println("To filename", tToFilename)
}

tempDir, err := ioutil.TempDir("", "crossfade")
if exists(tempDir) {
// Clean up
os.RemoveAll(tempDir)
}

tempDir, err = ioutil.TempDir("", "crossfade")
if err != nil {
fmt.Fprintf(os.Stderr, "Could not create temporary directory: %v\n", err)
return // return from anon func
Expand Down Expand Up @@ -421,8 +466,6 @@ func (gw *GnomeTimedWallpaper) EventLoop(verbose bool) error {
return // return from anon func
}

// The removal of the temporary directory should not be deferred, since this is an endless loop
os.RemoveAll(tempDir) // clean up
}))

// Increase the variable that keeps track of the time
Expand All @@ -448,15 +491,17 @@ func (stw *SimpleTimedWallpaper) UntilNext(et time.Time) time.Duration {
for _, s := range stw.Statics {
startTimes = append(startTimes, s.At)
}
h24 := 24 * time.Hour
mindiff := h24
// OK, have all start times, now to find the ones that are both positive and smallest
for _, st := range startTimes {
//diff := st.Sub(et)
diff := event.ToToday(et).Sub(event.ToToday(st))
if diff < 0 {
for diff < 0 {
diff += h24
}
for diff > h24 {
diff -= h24
}
if diff > 0 && diff < mindiff {
mindiff = diff
}
Expand All @@ -479,17 +524,19 @@ func (stw *SimpleTimedWallpaper) NextEvent(now time.Time) (interface{}, error) {
return nil, errors.New("can not find next event: got no events")
}
// Go though all the event time stamps, and find the one that has the smallest (now time - event time)
h24 := 24 * time.Hour
minDiff := h24
var minEvent interface{}
for t, e := range events {
//fmt.Printf("now is: %v (%T)\n", now, now)
//fmt.Printf("t is: %v (%T)\n", t, t)
diff := event.ToToday(t).Sub(event.ToToday(now))
//diff := t.Sub(now)
if diff < 0 {
for diff < 0 {
diff += h24
}
for diff > h24 {
diff -= h24
}
//fmt.Println("Diff for", c(t), ":", diff)
if diff > 0 && diff < minDiff {
minDiff = diff
Expand All @@ -515,17 +562,19 @@ func (stw *SimpleTimedWallpaper) PrevEvent(now time.Time) (interface{}, error) {
return nil, errors.New("can not find previous event: got no events")
}
// Go though all the event time stamps, and find the one that has the smallest (now time - event time)
h24 := 24 * time.Hour
minDiff := h24
var minEvent interface{}
for t, e := range events {
//fmt.Printf("now is: %v (%T)\n", now, now)
//fmt.Printf("t is: %v (%T)\n", t, t)
diff := event.ToToday(now).Sub(event.ToToday(t))
//diff := now.Sub(t)
if diff < 0 {
for diff < 0 {
diff += h24
}
for diff > h24 {
diff -= h24
}
//fmt.Println("Diff for", c(t), ":", diff)
if diff > 0 && diff < minDiff {
minDiff = diff
Expand Down
9 changes: 8 additions & 1 deletion timedxml.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ type StaticMap map[int]int

// Duration returns how long a static wallpaper should last
func (s *GStatic) Duration() time.Duration {
return time.Duration(s.Seconds) * time.Second
d := time.Duration(s.Seconds) * time.Second
for d < 0 {
d += h24
}
for d > h24 {
d -= h24
}
return d
}

// Duration returns how long a transition should last
Expand Down
2 changes: 2 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"time"
)

var h24 = time.Hour * 24

// c formats a timestamp as HH:MM
func c(t time.Time) string {
return fmt.Sprintf("%.2d:%.2d", t.Hour(), t.Minute())
Expand Down

0 comments on commit 80c23b9

Please sign in to comment.