2017-07-11 01:17:27 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2017-07-12 16:21:51 +00:00
|
|
|
"github.com/BurntSushi/xgbutil"
|
2017-07-11 01:17:27 +00:00
|
|
|
"github.com/BurntSushi/xgbutil/ewmh"
|
2017-07-12 16:21:51 +00:00
|
|
|
"github.com/BurntSushi/xgbutil/icccm"
|
|
|
|
"github.com/BurntSushi/xgbutil/xevent"
|
2017-07-11 01:17:27 +00:00
|
|
|
"github.com/BurntSushi/xgbutil/xprop"
|
|
|
|
"github.com/fhs/gompd/mpd"
|
|
|
|
"github.com/fsnotify/fsnotify"
|
|
|
|
)
|
|
|
|
|
2017-07-11 15:25:19 +00:00
|
|
|
func (bar *Bar) clockFun() {
|
2018-12-23 17:03:56 +00:00
|
|
|
block := bar.initBlock("clock", "?", 800, 'a', 0, "#445967", "#CCCCCC")
|
2017-07-11 01:17:27 +00:00
|
|
|
|
|
|
|
init := true
|
|
|
|
for {
|
|
|
|
if !init {
|
|
|
|
time.Sleep(20 * time.Second)
|
|
|
|
}
|
|
|
|
init = false
|
|
|
|
|
2017-07-13 18:51:08 +00:00
|
|
|
txt := time.Now().Format("Monday, January 2th 03:04 PM")
|
|
|
|
if block.txt == txt {
|
|
|
|
continue
|
|
|
|
}
|
2017-07-11 01:17:27 +00:00
|
|
|
|
2017-07-13 18:51:08 +00:00
|
|
|
block.txt = txt
|
|
|
|
bar.redraw <- block
|
2017-07-11 01:17:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-24 19:16:42 +00:00
|
|
|
func (bar *Bar) musicFun() {
|
2018-12-23 17:03:56 +00:00
|
|
|
block := bar.initBlock("music", "?", 660, 'r', -13, "#3C4F5B", "#CCCCCC")
|
|
|
|
|
2018-12-24 19:16:42 +00:00
|
|
|
/*block.actions["button1"] = func() {
|
2018-12-23 17:03:56 +00:00
|
|
|
if block.popup == nil {
|
|
|
|
var err error
|
|
|
|
block.popup, err = bar.initPopup(1920-304-29, 29, 304, 148,
|
|
|
|
"#3C4F5B", "#CCCCCC")
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2017-07-13 18:51:08 +00:00
|
|
|
|
2018-12-23 17:03:56 +00:00
|
|
|
//popup.draw()
|
|
|
|
} else {
|
|
|
|
block.popup = block.popup.destroy()
|
|
|
|
}
|
2018-12-24 19:16:42 +00:00
|
|
|
}*/
|
2017-07-13 18:51:08 +00:00
|
|
|
block.actions["button3"] = func() {
|
|
|
|
conn, err := mpd.Dial("tcp", ":6600")
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2018-12-23 17:03:56 +00:00
|
|
|
defer conn.Close()
|
2017-07-13 18:51:08 +00:00
|
|
|
|
|
|
|
status, err := conn.Status()
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2018-12-23 17:03:56 +00:00
|
|
|
|
|
|
|
if err := conn.Pause(status["state"] != "pause"); err != nil {
|
|
|
|
log.Print(err)
|
2017-07-13 18:51:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
block.actions["button4"] = func() {
|
|
|
|
conn, err := mpd.Dial("tcp", ":6600")
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2018-12-23 17:03:56 +00:00
|
|
|
defer conn.Close()
|
2017-07-13 18:51:08 +00:00
|
|
|
|
2018-12-23 17:03:56 +00:00
|
|
|
if err := conn.Previous(); err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2017-07-13 18:51:08 +00:00
|
|
|
}
|
|
|
|
block.actions["button5"] = func() {
|
|
|
|
conn, err := mpd.Dial("tcp", ":6600")
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2018-12-23 17:03:56 +00:00
|
|
|
defer conn.Close()
|
2017-07-13 18:51:08 +00:00
|
|
|
|
2018-12-23 17:03:56 +00:00
|
|
|
if err := conn.Next(); err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2017-07-13 18:51:08 +00:00
|
|
|
}
|
2017-07-11 01:17:27 +00:00
|
|
|
|
|
|
|
watcher, err := mpd.NewWatcher("tcp", ":6600", "", "player")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
var conn *mpd.Client
|
|
|
|
init := true
|
|
|
|
for {
|
|
|
|
if !init {
|
|
|
|
conn.Close()
|
|
|
|
<-watcher.Event
|
|
|
|
}
|
|
|
|
init = false
|
|
|
|
|
2018-12-23 17:03:56 +00:00
|
|
|
// TODO: Is it maybe possible to not create a new connection each loop?
|
2017-07-11 01:17:27 +00:00
|
|
|
conn, err = mpd.Dial("tcp", ":6600")
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
cur, err := conn.CurrentSong()
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-07-13 18:51:08 +00:00
|
|
|
status, err := conn.Status()
|
2017-07-11 01:17:27 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
continue
|
|
|
|
}
|
2018-12-23 17:03:56 +00:00
|
|
|
|
2017-07-11 01:17:27 +00:00
|
|
|
var state string
|
2017-07-13 18:51:08 +00:00
|
|
|
if status["state"] == "pause" {
|
2017-07-11 01:17:27 +00:00
|
|
|
state = "[paused] "
|
|
|
|
}
|
|
|
|
|
2017-07-13 18:51:08 +00:00
|
|
|
txt := state + cur["Artist"] + " - " + cur["Title"]
|
|
|
|
if block.txt == txt {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
block.txt = txt
|
|
|
|
bar.redraw <- block
|
2017-07-11 01:17:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-11 15:25:19 +00:00
|
|
|
func (bar *Bar) todoFun() {
|
2018-12-23 17:03:56 +00:00
|
|
|
block := bar.initBlock("todo", "?", 29, 'c', 0, "#5394C9", "#FFFFFF")
|
2017-07-11 01:17:27 +00:00
|
|
|
|
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-12-23 17:03:56 +00:00
|
|
|
if err := watcher.Add("/home/onodera/.todo"); err != nil {
|
2017-07-11 01:17:27 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-12-23 17:03:56 +00:00
|
|
|
file, err := os.Open("/home/onodera/.todo")
|
2017-07-11 01:17:27 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
init := true
|
|
|
|
for {
|
|
|
|
if !init {
|
|
|
|
ev := <-watcher.Events
|
|
|
|
if ev.Op&fsnotify.Write != fsnotify.Write {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
init = false
|
|
|
|
|
|
|
|
s := bufio.NewScanner(file)
|
|
|
|
s.Split(bufio.ScanLines)
|
|
|
|
var c int
|
|
|
|
for s.Scan() {
|
|
|
|
c++
|
|
|
|
}
|
|
|
|
if _, err := file.Seek(0, 0); err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-07-13 18:51:08 +00:00
|
|
|
txt := strconv.Itoa(c)
|
|
|
|
if block.txt == txt {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
block.txt = txt
|
|
|
|
bar.redraw <- block
|
2017-07-11 01:17:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-13 18:51:08 +00:00
|
|
|
/*
|
2017-07-11 15:25:19 +00:00
|
|
|
func (bar *Bar) weatherFun() {
|
2017-07-13 18:51:08 +00:00
|
|
|
block := bar.initBlock("weather", "?", 29, 'r', 0, "#5394C9",
|
|
|
|
"#FFFFFF")
|
2017-07-11 01:17:27 +00:00
|
|
|
|
|
|
|
w, err := owm.NewCurrent("C", "en")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
init := true
|
|
|
|
for {
|
|
|
|
if !init {
|
|
|
|
time.Sleep(200 * time.Second)
|
|
|
|
}
|
|
|
|
init = false
|
|
|
|
|
|
|
|
if err := w.CurrentByID(2758106); err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-07-13 18:51:08 +00:00
|
|
|
var state uint
|
|
|
|
switch w.Weather[0].Icon[0:2] {
|
|
|
|
case "01":
|
|
|
|
state = 0
|
|
|
|
case "02":
|
|
|
|
state = 1
|
|
|
|
case "03":
|
|
|
|
state = 2
|
|
|
|
case "04":
|
|
|
|
state = 3
|
|
|
|
case "09":
|
|
|
|
state = 4
|
|
|
|
case "10":
|
|
|
|
state = 5
|
|
|
|
case "11":
|
|
|
|
state = 6
|
|
|
|
case "13":
|
|
|
|
state = 7
|
|
|
|
case "50":
|
|
|
|
state = 8
|
|
|
|
}
|
2017-07-11 01:17:27 +00:00
|
|
|
|
2017-07-13 18:51:08 +00:00
|
|
|
block.txt = strconv.FormatFloat(w.Main.Temp, 'f', 0, 64) +
|
|
|
|
" °C"
|
|
|
|
bar.redraw <- block
|
2017-07-11 01:17:27 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-13 18:51:08 +00:00
|
|
|
*/
|
2017-07-11 01:17:27 +00:00
|
|
|
|
2017-07-11 15:25:19 +00:00
|
|
|
func (bar *Bar) windowFun() {
|
2018-12-23 17:03:56 +00:00
|
|
|
block := bar.initBlock("window", "?", 220, 'c', 0, "#37BF8D", "#FFFFFF")
|
2017-07-11 01:17:27 +00:00
|
|
|
|
2018-12-23 17:03:56 +00:00
|
|
|
// TODO: I'm not sure how I can use init (to prevent a black bar) here?
|
|
|
|
xevent.PropertyNotifyFun(func(_ *xgbutil.XUtil, ev xevent.
|
|
|
|
PropertyNotifyEvent) {
|
2017-07-12 16:21:51 +00:00
|
|
|
atom, err := xprop.Atm(bar.xu, "_NET_ACTIVE_WINDOW")
|
|
|
|
if ev.Atom != atom {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return
|
2017-07-11 01:17:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
id, err := ewmh.ActiveWindowGet(bar.xu)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
2017-07-12 16:21:51 +00:00
|
|
|
return
|
2017-07-11 01:17:27 +00:00
|
|
|
}
|
2017-07-13 18:51:08 +00:00
|
|
|
|
|
|
|
txt, err := ewmh.WmNameGet(bar.xu, id)
|
|
|
|
if err != nil || len(txt) == 0 {
|
|
|
|
txt, err = icccm.WmNameGet(bar.xu, id)
|
|
|
|
if err != nil || len(txt) == 0 {
|
|
|
|
txt = "?"
|
2017-07-12 16:21:51 +00:00
|
|
|
}
|
2017-07-11 01:17:27 +00:00
|
|
|
}
|
2018-12-24 19:16:42 +00:00
|
|
|
|
|
|
|
if len(txt) > 30 {
|
|
|
|
txt = txt[0:30] + "..."
|
|
|
|
}
|
|
|
|
|
2017-07-13 18:51:08 +00:00
|
|
|
if block.txt == txt {
|
|
|
|
return
|
|
|
|
}
|
2017-07-11 01:17:27 +00:00
|
|
|
|
2017-07-13 18:51:08 +00:00
|
|
|
block.txt = txt
|
|
|
|
bar.redraw <- block
|
2017-07-12 16:21:51 +00:00
|
|
|
}).Connect(bar.xu, bar.xu.RootWin())
|
2017-07-11 01:17:27 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 15:25:19 +00:00
|
|
|
func (bar *Bar) workspaceFun() {
|
2018-12-23 17:03:56 +00:00
|
|
|
blockWWW := bar.initBlock("www", "www", 74, 'c', 0, "#5394C9", "#FFFFFF")
|
|
|
|
blockWWW.actions["button1"] = func() {
|
2017-07-13 18:57:42 +00:00
|
|
|
if err := ewmh.CurrentDesktopReq(bar.xu, 0); err != nil {
|
2017-07-13 18:51:08 +00:00
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-23 17:03:56 +00:00
|
|
|
blockIRC := bar.initBlock("irc", "irc", 67, 'c', 0, "#5394C9", "#FFFFFF")
|
|
|
|
blockIRC.actions["button1"] = func() {
|
2017-07-13 18:51:08 +00:00
|
|
|
if err := ewmh.CurrentDesktopReq(bar.xu, 1); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-23 17:03:56 +00:00
|
|
|
blockSRC := bar.initBlock("src", "src", 70, 'c', 0, "#5394C9", "#FFFFFF")
|
|
|
|
blockSRC.actions["button1"] = func() {
|
2017-07-13 18:57:42 +00:00
|
|
|
if err := ewmh.CurrentDesktopReq(bar.xu, 2); err != nil {
|
2017-07-13 18:51:08 +00:00
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
2017-07-11 01:17:27 +00:00
|
|
|
|
2018-12-23 17:03:56 +00:00
|
|
|
// TODO: I'm not sure how I can use init (to prevent a black bar) here?
|
2017-07-13 18:51:08 +00:00
|
|
|
var owsp uint
|
2018-12-23 17:03:56 +00:00
|
|
|
var pwsp, nwsp int
|
|
|
|
xevent.PropertyNotifyFun(func(_ *xgbutil.XUtil, ev xevent.
|
|
|
|
PropertyNotifyEvent) {
|
2017-07-12 16:21:51 +00:00
|
|
|
atom, err := xprop.Atm(bar.xu, "_NET_CURRENT_DESKTOP")
|
|
|
|
if ev.Atom != atom {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return
|
2017-07-11 01:17:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wsp, err := ewmh.CurrentDesktopGet(bar.xu)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
2017-07-12 16:21:51 +00:00
|
|
|
return
|
2017-07-11 01:17:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch wsp {
|
|
|
|
case 0:
|
2018-12-23 17:03:56 +00:00
|
|
|
blockWWW.bg = "#72A7D3"
|
|
|
|
blockIRC.bg = "#5394C9"
|
|
|
|
blockSRC.bg = "#5394C9"
|
|
|
|
|
|
|
|
pwsp = 2
|
|
|
|
nwsp = 1
|
2017-07-11 01:17:27 +00:00
|
|
|
case 1:
|
2018-12-23 17:03:56 +00:00
|
|
|
blockWWW.bg = "#5394C9"
|
|
|
|
blockIRC.bg = "#72A7D3"
|
|
|
|
blockSRC.bg = "#5394C9"
|
|
|
|
|
|
|
|
pwsp = 0
|
|
|
|
nwsp = 2
|
2017-07-11 01:17:27 +00:00
|
|
|
case 2:
|
2018-12-23 17:03:56 +00:00
|
|
|
blockWWW.bg = "#5394C9"
|
|
|
|
blockIRC.bg = "#5394C9"
|
|
|
|
blockSRC.bg = "#72A7D3"
|
|
|
|
|
|
|
|
pwsp = 1
|
|
|
|
nwsp = 0
|
2017-07-11 01:17:27 +00:00
|
|
|
}
|
2018-12-23 17:03:56 +00:00
|
|
|
|
|
|
|
if owsp == wsp {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
owsp = wsp
|
|
|
|
|
|
|
|
bar.redraw <- blockWWW
|
|
|
|
bar.redraw <- blockIRC
|
|
|
|
bar.redraw <- blockSRC
|
2017-07-12 16:21:51 +00:00
|
|
|
}).Connect(bar.xu, bar.xu.RootWin())
|
2018-12-23 17:03:56 +00:00
|
|
|
|
|
|
|
prevFun := func() {
|
|
|
|
if err := ewmh.CurrentDesktopReq(bar.xu, pwsp); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nextFun := func() {
|
|
|
|
if err := ewmh.CurrentDesktopReq(bar.xu, nwsp); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
blockWWW.actions["button4"] = prevFun
|
|
|
|
blockWWW.actions["button5"] = nextFun
|
|
|
|
blockIRC.actions["button4"] = prevFun
|
|
|
|
blockIRC.actions["button5"] = nextFun
|
|
|
|
blockSRC.actions["button4"] = prevFun
|
|
|
|
blockSRC.actions["button5"] = nextFun
|
2017-07-11 01:17:27 +00:00
|
|
|
}
|