summaryrefslogtreecommitdiff
path: root/bar/bar.go
blob: 6ab7f3587e47864b02ec66de424436c5410e1ce6 (plain)
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
package bar

import (
	"fmt"
	"container/list"

	"hhvn.uk/hbspbar/common"
	"hhvn.uk/hbspbar/bspc"

	"github.com/jezek/xgb"
	"github.com/jezek/xgb/xproto"

	"github.com/jezek/xgbutil"
	"github.com/jezek/xgbutil/icccm"
	"github.com/jezek/xgbutil/ewmh"
)

var conf = struct {
	H   uint
	Bg  uint32
	Fg  uint32
	Sel uint32
	Urg uint32
} { 	16,
	0xff050a10,
	0xffeeeeee,
	0xff1b364b,
	0xff90222b }

var bars *list.List

type bar struct {
	X   *xgb.Conn
	Xu  *xgbutil.XUtil
	Scr *xproto.ScreenInfo
	Mon *bspc.Monitor
	w   xproto.Window
}

func (b *bar) Init() error {
	var err error

	b.w, err = xproto.NewWindowId(b.X)
	if err != nil {
		return common.Perror("xproto.NewWindowId", err)
	}

	geom := b.Mon.Rectangle
	xproto.CreateWindow(b.X, b.Scr.RootDepth, b.w, b.Scr.Root,
		int16(geom.X), int16(geom.Y), uint16(geom.Width), uint16(conf.H), 0,
		xproto.WindowClassInputOutput, b.Scr.RootVisual, 
		xproto.CwBackPixel|xproto.CwEventMask, []uint32{
			conf.Bg,
			xproto.EventMaskStructureNotify })

	class := icccm.WmClass{"hbspbar", "hbspbar"}
	icccm.WmClassSet(b.Xu, b.w, &class)

	ewmh.WmWindowTypeSet(b.Xu, b.w, []string{"_NET_WM_WINDOW_TYPE_DOCK"})
	ewmh.WmStateSet(b.Xu, b.w, []string{"_NET_WM_STATE_STICKY", "_NET_WM_STATE_ABOVE"})

	err = xproto.MapWindowChecked(b.X, b.w).Check()
	if err != nil {
		return common.Perror("xproto.MapWindow", err)
	}

	go b.Mon.TopPadding(conf.H)

	return nil
}

func (b *bar) Destroy() {
	b.Mon.TopPadding(0)
}

type eventwrap struct {
	Ev  xgb.Event
	Err error
}

// External interface
type Handle struct {
	Create   chan int
	Destroy  chan int
	NewState chan *bspc.State
	Err      chan error
}

func Init(state *bspc.State) (*Handle, error) {
	bars = list.New()

	x, err := xgb.NewConn()
	if err != nil { return nil, err }

	xu, err := xgbutil.NewConnXgb(x)
	if err != nil { return nil, err }

	setup  := xproto.Setup(x)
	screen := setup.DefaultScreen(x)

	var h Handle
	h.Create   = make(chan int)
	h.Destroy  = make(chan int)
	h.NewState = make(chan *bspc.State)
	h.Err      = make(chan error)

	events := make(chan *eventwrap)

	go func() { for {
		ev, err := x.WaitForEvent()
		w := eventwrap{ev, err}
		events <- &w

		select {
		case <- events: return
		default:
		}
	}}()

	go func() {
		defer func() { events <- nil }()

		for {
			select {
			case e := <- events:
				if e.Ev == nil && e.Err == nil {
					h.Err <- fmt.Errorf("X connection clossed")
					return
				}

				if e.Err != nil {
					common.Perror("x.WaitForEvents", err)
					continue
				}

				switch e.Ev.(type) {
				case xproto.DestroyNotifyEvent:
					h.Err <- fmt.Errorf("Window destroyed")
					return
				}
			case id := <- h.Destroy:
				destroy(id)
			case id := <- h.Create:
				if err := create(x, xu, screen, state.GetMon(id)); err != nil {
					h.Err <- fmt.Errorf("Couldn't create window: %s\n", err)
					return
				}
			case state := <- h.NewState:
				for e := bars.Front(); e != nil; e = e.Next() {
					v := e.Value.(*bar)
					v.Mon = state.GetMon(v.Mon.ID)
				}
			default:
			}
		}
	}()
	
	return &h, nil
}

func create(x *xgb.Conn, xu *xgbutil.XUtil,
		scr *xproto.ScreenInfo, m *bspc.Monitor) (error) {
	var b bar

	b.X   = x
	b.Xu  = xu
	b.Scr = scr
	b.Mon = m

	if err := b.Init(); err != nil {
		return err
	}

	bars.PushBack(&b)
	return nil
}

func destroy(monid int) (bool) {
	var next *list.Element

	for e := bars.Front(); e != nil; e = next {
		next = e.Next()
		v   := e.Value.(*bar)

		/* A monid of -1 means that we're doing
		   the bar.Cleanup schermoodle today */
		if monid == -1 || monid > 0 && v.Mon.ID == int(monid) {
			v.Destroy()
			bars.Remove(e)
			if monid != -1 { return true }
		}
	}

	if monid == -1 { return true }
	return false
}

func Cleanup() {
	destroy(-1)
}