2017-07-11 01:17:27 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-07-12 16:21:51 +00:00
|
|
|
"fmt"
|
2017-07-11 01:17:27 +00:00
|
|
|
"image"
|
2018-12-27 23:00:30 +00:00
|
|
|
"log"
|
2017-07-13 18:51:08 +00:00
|
|
|
"strconv"
|
2017-07-11 01:17:27 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/BurntSushi/xgb/xproto"
|
|
|
|
"github.com/BurntSushi/xgbutil"
|
|
|
|
"github.com/BurntSushi/xgbutil/ewmh"
|
2017-07-12 16:21:51 +00:00
|
|
|
"github.com/BurntSushi/xgbutil/xevent"
|
2017-07-11 01:17:27 +00:00
|
|
|
"github.com/BurntSushi/xgbutil/xgraphics"
|
|
|
|
"github.com/BurntSushi/xgbutil/xwindow"
|
2018-12-24 19:16:42 +00:00
|
|
|
"golang.org/x/image/font"
|
|
|
|
"golang.org/x/image/math/fixed"
|
2017-07-11 01:17:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Bar is a struct with information about the bar.
|
|
|
|
type Bar struct {
|
2018-12-27 23:00:30 +00:00
|
|
|
// Bar window, and bar image.
|
2017-07-11 01:17:27 +00:00
|
|
|
win *xwindow.Window
|
|
|
|
img *xgraphics.Image
|
|
|
|
|
2017-07-11 15:25:19 +00:00
|
|
|
// The width and height of the bar.
|
|
|
|
w, h int
|
|
|
|
|
2018-12-23 17:03:56 +00:00
|
|
|
// This is a sum of all of the block widths, used to draw a block to the
|
|
|
|
// right of the last block.
|
2017-07-11 15:25:19 +00:00
|
|
|
xsum int
|
|
|
|
|
2017-07-13 19:22:19 +00:00
|
|
|
// A map with information about the block, see the `Block` type.
|
2018-12-27 23:00:30 +00:00
|
|
|
blocks *sync.Map
|
2017-07-11 01:17:27 +00:00
|
|
|
|
2018-12-23 17:03:56 +00:00
|
|
|
// A channel where the block should be send to to once its ready to be
|
|
|
|
// redrawn.
|
2017-07-13 18:51:08 +00:00
|
|
|
redraw chan *Block
|
2018-12-26 20:24:04 +00:00
|
|
|
|
|
|
|
// A channel where a boolean should be send once a block has initizalized,
|
|
|
|
// notifying that the next block can intialize.
|
|
|
|
ready chan bool
|
2017-07-11 01:17:27 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 23:00:30 +00:00
|
|
|
func initBar(x, y, w, h int) (*Bar, error) {
|
2017-07-11 01:17:27 +00:00
|
|
|
bar := new(Bar)
|
|
|
|
var err error
|
|
|
|
|
2018-12-23 17:03:56 +00:00
|
|
|
// Create a window for the bar. This window listens to button press events
|
|
|
|
// in order to respond to them.
|
2018-12-27 23:00:30 +00:00
|
|
|
bar.win, err = xwindow.Generate(X)
|
2017-07-11 01:17:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-27 23:00:30 +00:00
|
|
|
bar.win.Create(X.RootWin(), x, y, w, h, xproto.CwBackPixel|xproto.
|
2018-12-23 17:03:56 +00:00
|
|
|
CwEventMask, 0x000000, xproto.EventMaskButtonPress)
|
2017-07-11 19:58:23 +00:00
|
|
|
|
2018-12-23 17:03:56 +00:00
|
|
|
// EWMH stuff to make the window behave like an actual bar.
|
|
|
|
// XXX: `WmStateSet` and `WmDesktopSet` are basically here to keep OpenBox
|
|
|
|
// happy, can I somehow remove them and just use `_NET_WM_WINDOW_TYPE_DOCK`
|
|
|
|
// like I can with WindowChef?
|
2018-12-27 23:00:30 +00:00
|
|
|
if err := ewmh.WmWindowTypeSet(X, bar.win.Id, []string{
|
2017-07-11 01:17:27 +00:00
|
|
|
"_NET_WM_WINDOW_TYPE_DOCK"}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-27 23:00:30 +00:00
|
|
|
if err := ewmh.WmStateSet(X, bar.win.Id, []string{
|
2017-07-12 16:21:51 +00:00
|
|
|
"_NET_WM_STATE_STICKY"}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-27 23:00:30 +00:00
|
|
|
if err := ewmh.WmDesktopSet(X, bar.win.Id, ^uint(0)); err != nil {
|
2017-07-12 16:21:51 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-27 23:00:30 +00:00
|
|
|
if err := ewmh.WmNameSet(X, bar.win.Id, "melonbar"); err != nil {
|
2017-07-12 16:21:51 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-11 19:58:23 +00:00
|
|
|
|
|
|
|
// Map window.
|
2017-07-11 01:17:27 +00:00
|
|
|
bar.win.Map()
|
2017-07-13 19:22:19 +00:00
|
|
|
|
2018-12-23 17:03:56 +00:00
|
|
|
// XXX: Moving the window is again a hack to keep OpenBox happy.
|
2017-07-12 16:21:51 +00:00
|
|
|
bar.win.Move(x, y)
|
2017-07-11 01:17:27 +00:00
|
|
|
|
2017-07-13 19:22:19 +00:00
|
|
|
// Create the bar image.
|
2018-12-27 23:00:30 +00:00
|
|
|
bar.img = xgraphics.New(X, image.Rect(0, 0, w, h))
|
2018-12-23 17:03:56 +00:00
|
|
|
if err := bar.img.XSurfaceSet(bar.win.Id); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-11 19:58:23 +00:00
|
|
|
bar.img.XDraw()
|
2017-07-11 01:17:27 +00:00
|
|
|
|
2018-12-23 17:03:56 +00:00
|
|
|
bar.w = w
|
|
|
|
bar.h = h
|
|
|
|
|
2018-12-27 23:00:30 +00:00
|
|
|
bar.blocks = new(sync.Map)
|
2017-07-13 18:51:08 +00:00
|
|
|
bar.redraw = make(chan *Block)
|
|
|
|
|
2017-07-13 21:06:10 +00:00
|
|
|
// Listen to mouse events and execute the required function.
|
2018-12-23 17:03:56 +00:00
|
|
|
xevent.ButtonPressFun(func(_ *xgbutil.XUtil, ev xevent.ButtonPressEvent) {
|
2017-07-13 18:51:08 +00:00
|
|
|
// Determine what block the cursor is in.
|
|
|
|
var block *Block
|
2018-12-27 23:00:30 +00:00
|
|
|
bar.blocks.Range(func(name, i interface{}) bool {
|
2017-07-13 18:51:08 +00:00
|
|
|
block = i.(*Block)
|
2018-12-27 23:00:30 +00:00
|
|
|
|
2017-07-13 21:06:10 +00:00
|
|
|
// XXX: Hack for music block.
|
|
|
|
if name == "music" {
|
2018-12-27 23:00:30 +00:00
|
|
|
tw := font.MeasureString(face, block.txt).Ceil()
|
|
|
|
if ev.EventX >= int16(block.x+(block.w-tw+(block.xoff*2))) &&
|
|
|
|
ev.EventX < int16(block.x+block.w) {
|
2017-07-13 21:06:10 +00:00
|
|
|
return false
|
|
|
|
}
|
2018-12-27 23:00:30 +00:00
|
|
|
block = nil
|
2017-07-13 21:06:10 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-12-27 23:00:30 +00:00
|
|
|
if ev.EventX >= int16(block.x) && ev.EventX < int16(block.x+block.
|
2018-12-23 17:03:56 +00:00
|
|
|
w) {
|
2017-07-13 18:51:08 +00:00
|
|
|
return false
|
|
|
|
}
|
2018-12-27 23:00:30 +00:00
|
|
|
block = nil
|
2017-07-13 18:51:08 +00:00
|
|
|
return true
|
|
|
|
})
|
2017-07-11 01:17:27 +00:00
|
|
|
|
2017-07-13 18:51:08 +00:00
|
|
|
// Execute the function as specified.
|
2018-12-27 23:00:30 +00:00
|
|
|
if block != nil {
|
|
|
|
if err := block.actions["button"+strconv.Itoa(int(ev.
|
|
|
|
Detail))](); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).Connect(X, bar.win.Id)
|
2017-07-11 19:58:23 +00:00
|
|
|
|
2017-07-13 18:51:08 +00:00
|
|
|
return bar, nil
|
2017-07-11 01:17:27 +00:00
|
|
|
}
|
|
|
|
|
2017-07-13 18:57:42 +00:00
|
|
|
func (bar *Bar) draw(block *Block) error {
|
2018-12-24 19:16:42 +00:00
|
|
|
d := &font.Drawer{
|
|
|
|
Dst: block.img,
|
|
|
|
Src: image.NewUniform(hexToBGRA(block.fg)),
|
2018-12-27 23:00:30 +00:00
|
|
|
Face: face,
|
2018-12-24 19:16:42 +00:00
|
|
|
}
|
|
|
|
|
2018-12-23 17:03:56 +00:00
|
|
|
// Calculate the required x coordinate for the different aligments.
|
2017-07-11 01:17:27 +00:00
|
|
|
var x int
|
2018-12-24 19:16:42 +00:00
|
|
|
tw := d.MeasureString(block.txt).Ceil()
|
2017-07-11 01:17:27 +00:00
|
|
|
switch block.align {
|
|
|
|
case 'l':
|
2017-07-13 21:06:10 +00:00
|
|
|
x = block.x
|
2017-07-11 01:17:27 +00:00
|
|
|
case 'c':
|
2017-07-13 21:06:10 +00:00
|
|
|
x = block.x + ((block.w / 2) - (tw / 2))
|
2017-07-11 01:17:27 +00:00
|
|
|
case 'r':
|
2017-07-13 21:06:10 +00:00
|
|
|
x = (block.x + block.w) - tw
|
|
|
|
case 'a':
|
|
|
|
x = (bar.w / 2) - (tw / 2)
|
2017-07-12 16:21:51 +00:00
|
|
|
default:
|
2018-12-23 17:03:56 +00:00
|
|
|
return fmt.Errorf("draw %#U: Not a valid aligment rune", block.align)
|
2017-07-11 01:17:27 +00:00
|
|
|
}
|
2017-07-13 21:06:10 +00:00
|
|
|
x += block.xoff
|
2017-07-11 01:17:27 +00:00
|
|
|
|
2017-07-13 21:06:10 +00:00
|
|
|
// Color the background.
|
2017-07-12 16:21:51 +00:00
|
|
|
block.img.For(func(cx, cy int) xgraphics.BGRA {
|
2017-07-13 21:06:10 +00:00
|
|
|
// XXX: Hack for music block.
|
2017-07-13 18:51:08 +00:00
|
|
|
if block.w == 660 {
|
2017-07-12 16:21:51 +00:00
|
|
|
if cx < x+block.xoff {
|
|
|
|
return hexToBGRA("#445967")
|
|
|
|
}
|
2017-07-13 18:51:08 +00:00
|
|
|
return hexToBGRA(block.bg)
|
2017-07-12 16:21:51 +00:00
|
|
|
}
|
|
|
|
|
2017-07-13 18:51:08 +00:00
|
|
|
return hexToBGRA(block.bg)
|
2017-07-12 16:21:51 +00:00
|
|
|
})
|
|
|
|
|
2017-07-11 01:17:27 +00:00
|
|
|
// Draw the text.
|
2018-12-26 14:35:02 +00:00
|
|
|
d.Dot = fixed.P(x, 18)
|
2018-12-24 19:16:42 +00:00
|
|
|
d.DrawString(block.txt)
|
2017-07-11 01:17:27 +00:00
|
|
|
|
2018-12-24 19:16:42 +00:00
|
|
|
// Redraw the bar.
|
2017-07-11 01:17:27 +00:00
|
|
|
block.img.XDraw()
|
|
|
|
bar.img.XPaint(bar.win.Id)
|
2018-12-23 17:03:56 +00:00
|
|
|
|
2017-07-13 18:51:08 +00:00
|
|
|
return nil
|
2017-07-11 01:17:27 +00:00
|
|
|
}
|
2018-12-26 20:24:04 +00:00
|
|
|
|
|
|
|
func (bar *Bar) initBlocks(blocks []func()) {
|
|
|
|
bar.ready = make(chan bool)
|
|
|
|
|
|
|
|
for _, f := range blocks {
|
|
|
|
go f()
|
|
|
|
<-bar.ready
|
|
|
|
}
|
|
|
|
|
|
|
|
close(bar.ready)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bar *Bar) listen() {
|
|
|
|
for {
|
|
|
|
if err := bar.draw(<-bar.redraw); err != nil {
|
2018-12-27 23:00:30 +00:00
|
|
|
log.Fatalln(err)
|
2018-12-26 20:24:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|