Upload files to "/"
This commit is contained in:
+133
@@ -0,0 +1,133 @@
|
||||
package main
|
||||
|
||||
// Start with Windows: a per-user entry in the Run list, toggled from the
|
||||
// right-click menu. No administrator rights needed.
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
var (
|
||||
advapi32 = syscall.NewLazyDLL("advapi32.dll")
|
||||
pRegOpenKeyEx = advapi32.NewProc("RegOpenKeyExW")
|
||||
pRegSetValueEx = advapi32.NewProc("RegSetValueExW")
|
||||
pRegQueryValueEx = advapi32.NewProc("RegQueryValueExW")
|
||||
pRegDeleteValue = advapi32.NewProc("RegDeleteValueW")
|
||||
pRegCloseKey = advapi32.NewProc("RegCloseKey")
|
||||
pGetAsyncKeyState = syscall.NewLazyDLL("user32.dll").NewProc("GetAsyncKeyState")
|
||||
)
|
||||
|
||||
const (
|
||||
hkeyCurrentUser = 0x80000001
|
||||
keyQuerySet = 0x0003 // KEY_QUERY_VALUE | KEY_SET_VALUE
|
||||
regSZ = 1
|
||||
vkShift = 0x10
|
||||
|
||||
menuAutostartID = 31
|
||||
runKeyPath = `Software\Microsoft\Windows\CurrentVersion\Run`
|
||||
runValueName = "MirrorBar"
|
||||
)
|
||||
|
||||
func openRunKey() (uintptr, bool) {
|
||||
var key uintptr
|
||||
path := u16(runKeyPath)
|
||||
r, _, _ := pRegOpenKeyEx.Call(hkeyCurrentUser, uintptr(unsafe.Pointer(path)), 0, keyQuerySet,
|
||||
uintptr(unsafe.Pointer(&key)))
|
||||
return key, r == 0
|
||||
}
|
||||
|
||||
// autostartCommand is what Windows runs at sign-in: this program, with the
|
||||
// self-closing safety timer turned off.
|
||||
func autostartCommand() string {
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return `"` + exe + `" --forever`
|
||||
}
|
||||
|
||||
func autostartValue() (string, bool) {
|
||||
key, ok := openRunKey()
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
defer pRegCloseKey.Call(key)
|
||||
|
||||
name := u16(runValueName)
|
||||
var typ uint32
|
||||
var buf [1024]uint16
|
||||
size := uint32(len(buf) * 2)
|
||||
r, _, _ := pRegQueryValueEx.Call(key, uintptr(unsafe.Pointer(name)), 0,
|
||||
uintptr(unsafe.Pointer(&typ)), uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&size)))
|
||||
if r != 0 || typ != regSZ || size > uint32(len(buf)*2) {
|
||||
return "", false
|
||||
}
|
||||
return syscall.UTF16ToString(buf[:size/2]), true
|
||||
}
|
||||
|
||||
func autostartEnabled() bool {
|
||||
_, ok := autostartValue()
|
||||
return ok
|
||||
}
|
||||
|
||||
func setAutostart(on bool) {
|
||||
key, ok := openRunKey()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
defer pRegCloseKey.Call(key)
|
||||
|
||||
name := u16(runValueName)
|
||||
if !on {
|
||||
pRegDeleteValue.Call(key, uintptr(unsafe.Pointer(name)))
|
||||
return
|
||||
}
|
||||
cmd := autostartCommand()
|
||||
if cmd == "" {
|
||||
return
|
||||
}
|
||||
data, err := syscall.UTF16FromString(cmd)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
pRegSetValueEx.Call(key, uintptr(unsafe.Pointer(name)), 0, regSZ,
|
||||
uintptr(unsafe.Pointer(&data[0])), uintptr(len(data)*2))
|
||||
}
|
||||
|
||||
// refreshAutostartPath keeps the sign-in entry pointing at the copy of the
|
||||
// program that was run most recently, so a new version in a new folder takes
|
||||
// over. It does nothing if start with Windows is off.
|
||||
func refreshAutostartPath() {
|
||||
cur, ok := autostartValue()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if want := autostartCommand(); want != "" && !strings.EqualFold(cur, want) {
|
||||
setAutostart(true)
|
||||
}
|
||||
}
|
||||
|
||||
// shiftHeld is the escape hatch: hold Shift while the program starts and it
|
||||
// exits at once without touching the taskbar.
|
||||
func shiftHeld() bool {
|
||||
r, _, _ := pGetAsyncKeyState.Call(vkShift)
|
||||
return r&0x8000 != 0
|
||||
}
|
||||
|
||||
// waitForTaskbar gives Explorer time to create the real taskbar at sign-in.
|
||||
func waitForTaskbar(max time.Duration) uintptr {
|
||||
deadline := time.Now().Add(max)
|
||||
for {
|
||||
if tb := findTaskbar(); tb != 0 {
|
||||
return tb
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
return 0
|
||||
}
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
package main
|
||||
|
||||
// Moving the pill: the whole background of the bar is a drag area, the dragged
|
||||
// spot is remembered, and "Reset position" in the menu goes back to the default
|
||||
// (bottom center, a few pixels above the screen edge).
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const (
|
||||
menuResetPosID = 32
|
||||
menuOrientID = 33
|
||||
abeRight = 2
|
||||
|
||||
wmNcHitTest = 0x0084
|
||||
wmNcLButtonDblClk = 0x00A3
|
||||
wmNcRButtonUp = 0x00A5
|
||||
wmEnterSizeMove = 0x0231
|
||||
wmExitSizeMove = 0x0232
|
||||
htClient = 1
|
||||
htCaption = 2
|
||||
)
|
||||
|
||||
var (
|
||||
customPos bool // false means the default spot
|
||||
customCX int32 // center of the pill when moved
|
||||
customY int32 // vertical center of the pill when moved
|
||||
vertical bool // true: the bar stands up and down at the right edge
|
||||
dragging bool
|
||||
appbarOn = true
|
||||
)
|
||||
|
||||
func posFile() string { return filepath.Join(dataDir(), "pos.txt") }
|
||||
|
||||
func loadPos() {
|
||||
b, err := os.ReadFile(posFile())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
parts := strings.Split(strings.TrimSpace(string(b)), ",")
|
||||
if len(parts) != 2 {
|
||||
return
|
||||
}
|
||||
cx, e1 := strconv.Atoi(parts[0])
|
||||
y, e2 := strconv.Atoi(parts[1])
|
||||
if e1 != nil || e2 != nil {
|
||||
return
|
||||
}
|
||||
customPos, customCX, customY = true, int32(cx), int32(y)
|
||||
}
|
||||
|
||||
func savePos() {
|
||||
if !customPos {
|
||||
_ = os.Remove(posFile())
|
||||
return
|
||||
}
|
||||
_ = os.MkdirAll(dataDir(), 0o755)
|
||||
_ = os.WriteFile(posFile(), []byte(strconv.Itoa(int(customCX))+","+strconv.Itoa(int(customY))), 0o644)
|
||||
}
|
||||
|
||||
func orientFile() string { return filepath.Join(dataDir(), "orient.txt") }
|
||||
|
||||
func loadOrient() {
|
||||
b, err := os.ReadFile(orientFile())
|
||||
vertical = err == nil && strings.TrimSpace(string(b)) == "vertical"
|
||||
}
|
||||
|
||||
func saveOrient() {
|
||||
_ = os.MkdirAll(dataDir(), 0o755)
|
||||
v := "horizontal"
|
||||
if vertical {
|
||||
v = "vertical"
|
||||
}
|
||||
_ = os.WriteFile(orientFile(), []byte(v), 0o644)
|
||||
}
|
||||
|
||||
// Everything is laid out as if the bar were horizontal (a "length" along the
|
||||
// bar and a "thickness" across it). Vertical mode turns the picture a quarter
|
||||
// turn, so mouse positions are converted back to a position along the bar.
|
||||
func alongClient(lparam uintptr) int32 {
|
||||
x := int32(int16(lparam & 0xFFFF))
|
||||
y := int32(int16(lparam >> 16 & 0xFFFF))
|
||||
if vertical {
|
||||
return y
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
func alongScreen(lparam uintptr, wr rect) int32 {
|
||||
x := int32(int16(lparam&0xFFFF)) - wr.Left
|
||||
y := int32(int16(lparam>>16&0xFFFF)) - wr.Top
|
||||
if vertical {
|
||||
return y
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
// pillSize is the real window size for the current orientation.
|
||||
func pillSize() (int32, int32) {
|
||||
l := pillWidth()
|
||||
if vertical {
|
||||
return barHeight, l
|
||||
}
|
||||
return l, barHeight
|
||||
}
|
||||
|
||||
// mapRect turns a rectangle from the horizontal layout into the same spot on
|
||||
// the turned bar (a clockwise quarter turn; t is the bar's thickness).
|
||||
func mapRect(r rect, t int32) rect {
|
||||
return rect{t - r.Bottom, r.Left, t - r.Top, r.Right}
|
||||
}
|
||||
|
||||
func clampI(v, lo, hi int32) int32 {
|
||||
if hi < lo {
|
||||
hi = lo
|
||||
}
|
||||
return max(lo, min(hi, v))
|
||||
}
|
||||
|
||||
// pillPos is where the top-left of a pill of width nw belongs.
|
||||
func pillPos(pw, ph int32) (int32, int32) {
|
||||
if !customPos {
|
||||
return defaultPos(pw, ph)
|
||||
}
|
||||
sw, sh := screenSize()
|
||||
x := clampI(customCX-pw/2, 0, sw-pw)
|
||||
y := clampI(customY-ph/2, 0, sh-ph)
|
||||
return x, y
|
||||
}
|
||||
|
||||
func defaultPos(pw, ph int32) (int32, int32) {
|
||||
sw, sh := screenSize()
|
||||
if vertical {
|
||||
return sw - pw - barGap(), (sh - ph) / 2
|
||||
}
|
||||
return (sw - pw) / 2, sh - ph - barGap()
|
||||
}
|
||||
|
||||
// finishDrag runs when a drag ends: remember the new spot, or snap back to the
|
||||
// default if it was dropped right on it.
|
||||
func finishDrag(hwnd uintptr) {
|
||||
dragging = false
|
||||
var wr rect
|
||||
pGetWindowRect.Call(hwnd, uintptr(unsafe.Pointer(&wr)))
|
||||
dx, dy := defaultPos(wr.Right-wr.Left, wr.Bottom-wr.Top)
|
||||
if abs32(wr.Left-dx) <= px(8) && abs32(wr.Top-dy) <= px(8) {
|
||||
resetPos(hwnd)
|
||||
return
|
||||
}
|
||||
customPos, customCX, customY = true, (wr.Left+wr.Right)/2, (wr.Top+wr.Bottom)/2
|
||||
savePos()
|
||||
if appbarOn {
|
||||
// A floating bar no longer reserves screen space.
|
||||
abd := appBarData{Hwnd: hwnd}
|
||||
appBar(abmRemove, &abd)
|
||||
appbarOn = false
|
||||
}
|
||||
redraw(hwnd)
|
||||
}
|
||||
|
||||
func abs32(v int32) int32 {
|
||||
if v < 0 {
|
||||
return -v
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func resetPos(hwnd uintptr) {
|
||||
customPos = false
|
||||
savePos()
|
||||
if !appbarOn {
|
||||
abd := appBarData{Hwnd: hwnd, CallbackMsg: callbackMsg}
|
||||
appBar(abmNew, &abd)
|
||||
appbarOn = true
|
||||
}
|
||||
positionBar(hwnd)
|
||||
}
|
||||
@@ -0,0 +1,643 @@
|
||||
package main
|
||||
|
||||
// The right side of the bar, in mirror order from the center line outward:
|
||||
// clock, battery, volume, network, hidden icons arrow.
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const (
|
||||
trayClock = iota + 1
|
||||
trayBattery
|
||||
trayVolume
|
||||
trayWifi
|
||||
trayChevron
|
||||
)
|
||||
|
||||
const (
|
||||
netNone = iota
|
||||
netWired
|
||||
netWifi
|
||||
)
|
||||
|
||||
const (
|
||||
wmMouseWheel = 0x020A
|
||||
|
||||
vkReturn = 0x0D
|
||||
vkVolumeMute = 0xAD
|
||||
vkVolumeDown = 0xAE
|
||||
vkVolumeUp = 0xAF
|
||||
|
||||
menuDateID = 21
|
||||
menuPowerID = 22
|
||||
menuSoundID = 23
|
||||
menuNetID = 24
|
||||
menuMuteID = 25
|
||||
)
|
||||
|
||||
var (
|
||||
gdi32t = syscall.NewLazyDLL("gdi32.dll")
|
||||
ole32 = syscall.NewLazyDLL("ole32.dll")
|
||||
wlanapi = syscall.NewLazyDLL("wlanapi.dll")
|
||||
kern32t = syscall.NewLazyDLL("kernel32.dll")
|
||||
|
||||
pMoveToEx = gdi32t.NewProc("MoveToEx")
|
||||
pLineTo = gdi32t.NewProc("LineTo")
|
||||
pCreatePen = gdi32t.NewProc("CreatePen")
|
||||
pPolygon = gdi32t.NewProc("Polygon")
|
||||
pGetStockObject = gdi32t.NewProc("GetStockObject")
|
||||
pCoInitializeEx = ole32.NewProc("CoInitializeEx")
|
||||
pCoCreateInstance = ole32.NewProc("CoCreateInstance")
|
||||
pWlanOpenHandle = wlanapi.NewProc("WlanOpenHandle")
|
||||
pWlanCloseHandle = wlanapi.NewProc("WlanCloseHandle")
|
||||
pWlanEnumInterfaces = wlanapi.NewProc("WlanEnumInterfaces")
|
||||
pWlanQueryInterface = wlanapi.NewProc("WlanQueryInterface")
|
||||
pWlanFreeMemory = wlanapi.NewProc("WlanFreeMemory")
|
||||
pGetSystemPowerStatus = kern32t.NewProc("GetSystemPowerStatus")
|
||||
)
|
||||
|
||||
// Current values shown on the right side.
|
||||
var (
|
||||
battPct = -1 // -1 means this PC has no battery, so no battery icon
|
||||
battCharging bool
|
||||
battAC bool
|
||||
volLevel float32
|
||||
volMuted bool
|
||||
volOK bool
|
||||
netKind = netNone
|
||||
netQuality int
|
||||
hoverTray int
|
||||
)
|
||||
|
||||
// ---------- layout ----------
|
||||
|
||||
type trayItem struct {
|
||||
kind int
|
||||
r rect
|
||||
}
|
||||
|
||||
func trayItems(clientW, clientH int32) []trayItem {
|
||||
x := clientW / 2
|
||||
var out []trayItem
|
||||
add := func(kind int, w int32) {
|
||||
out = append(out, trayItem{kind, rect{x, 0, x + w, clientH}})
|
||||
x += w
|
||||
}
|
||||
add(trayClock, clockWidth())
|
||||
if battPct >= 0 {
|
||||
add(trayBattery, px(36))
|
||||
}
|
||||
add(trayVolume, px(36))
|
||||
add(trayWifi, px(36))
|
||||
add(trayChevron, px(28))
|
||||
return out
|
||||
}
|
||||
|
||||
func trayAt(hwnd uintptr, x int32) int {
|
||||
w, h := clientSize(hwnd)
|
||||
for _, it := range trayItems(w, h) {
|
||||
if x >= it.r.Left && x < it.r.Right {
|
||||
return it.kind
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// ---------- drawing ----------
|
||||
|
||||
func polyline(hdc uintptr, pts []point, width int32, color uintptr) {
|
||||
if len(pts) < 2 {
|
||||
return
|
||||
}
|
||||
pen, _, _ := pCreatePen.Call(0, uintptr(width), color)
|
||||
old, _, _ := pSelectObject.Call(hdc, pen)
|
||||
pMoveToEx.Call(hdc, uintptr(pts[0].X), uintptr(pts[0].Y), 0)
|
||||
for _, p := range pts[1:] {
|
||||
pLineTo.Call(hdc, uintptr(p.X), uintptr(p.Y))
|
||||
}
|
||||
pSelectObject.Call(hdc, old)
|
||||
pDeleteObject.Call(pen)
|
||||
}
|
||||
|
||||
func polygon(hdc uintptr, pts []point, color uintptr) {
|
||||
brush, _, _ := pCreateSolidBrush.Call(color)
|
||||
nullPen, _, _ := pGetStockObject.Call(8) // NULL_PEN
|
||||
oldBrush, _, _ := pSelectObject.Call(hdc, brush)
|
||||
oldPen, _, _ := pSelectObject.Call(hdc, nullPen)
|
||||
pPolygon.Call(hdc, uintptr(unsafe.Pointer(&pts[0])), uintptr(len(pts)))
|
||||
pSelectObject.Call(hdc, oldPen)
|
||||
pSelectObject.Call(hdc, oldBrush)
|
||||
pDeleteObject.Call(brush)
|
||||
}
|
||||
|
||||
func center(r rect) (int32, int32) { return (r.Left + r.Right) / 2, (r.Top + r.Bottom) / 2 }
|
||||
|
||||
func drawBattery(hdc uintptr, r rect) {
|
||||
cx, cy := center(r)
|
||||
bw, bh := px(18), px(9)
|
||||
nubW, nubH := px(2), px(4)
|
||||
t := px(1)
|
||||
left := cx - (bw+nubW)/2
|
||||
top := cy - bh/2
|
||||
white := rgb(245, 245, 245)
|
||||
|
||||
fill(hdc, rect{left, top, left + bw, top + t}, white)
|
||||
fill(hdc, rect{left, top + bh - t, left + bw, top + bh}, white)
|
||||
fill(hdc, rect{left, top, left + t, top + bh}, white)
|
||||
fill(hdc, rect{left + bw - t, top, left + bw, top + bh}, white)
|
||||
fill(hdc, rect{left + bw, cy - nubH/2, left + bw + nubW, cy + nubH/2}, white)
|
||||
|
||||
col := white
|
||||
switch {
|
||||
case battCharging || battAC:
|
||||
col = rgb(90, 205, 120)
|
||||
case battPct <= 15:
|
||||
col = rgb(235, 80, 80)
|
||||
}
|
||||
gap := t + px(1)
|
||||
inner := bw - 2*gap
|
||||
fw := inner * int32(battPct) / 100
|
||||
if battPct > 0 && fw < px(1) {
|
||||
fw = px(1)
|
||||
}
|
||||
fill(hdc, rect{left + gap, top + gap, left + gap + fw, top + bh - gap}, col)
|
||||
}
|
||||
|
||||
func drawVolume(hdc uintptr, r rect) {
|
||||
cx, cy := center(r)
|
||||
white, dim := rgb(245, 245, 245), rgb(110, 110, 120)
|
||||
x0 := cx - px(9)
|
||||
|
||||
fill(hdc, rect{x0, cy - px(3), x0 + px(3), cy + px(3)}, white)
|
||||
polygon(hdc, []point{
|
||||
{x0 + px(3), cy - px(3)},
|
||||
{x0 + px(7), cy - px(6)},
|
||||
{x0 + px(7), cy + px(6)},
|
||||
{x0 + px(3), cy + px(3)},
|
||||
}, white)
|
||||
|
||||
active := 0
|
||||
if volOK && !volMuted {
|
||||
switch {
|
||||
case volLevel > 0.66:
|
||||
active = 3
|
||||
case volLevel > 0.33:
|
||||
active = 2
|
||||
case volLevel > 0:
|
||||
active = 1
|
||||
}
|
||||
}
|
||||
heights := []int32{px(5), px(9), px(13)}
|
||||
for i := 0; i < 3; i++ {
|
||||
bx := x0 + px(10) + int32(i)*px(3)
|
||||
col := dim
|
||||
if i < active {
|
||||
col = white
|
||||
}
|
||||
fill(hdc, rect{bx, cy - heights[i]/2, bx + px(2), cy + heights[i]/2}, col)
|
||||
}
|
||||
if volMuted {
|
||||
polyline(hdc, []point{{x0 + px(10), cy - px(4)}, {x0 + px(17), cy + px(4)}}, px(1), white)
|
||||
polyline(hdc, []point{{x0 + px(10), cy + px(4)}, {x0 + px(17), cy - px(4)}}, px(1), white)
|
||||
}
|
||||
}
|
||||
|
||||
func drawNetwork(hdc uintptr, r rect) {
|
||||
cx, cy := center(r)
|
||||
white, dim := rgb(245, 245, 245), rgb(110, 110, 120)
|
||||
|
||||
if netKind == netWired {
|
||||
t := px(1)
|
||||
l, tp := cx-px(7), cy-px(6)
|
||||
w, h := px(14), px(9)
|
||||
fill(hdc, rect{l, tp, l + w, tp + t}, white)
|
||||
fill(hdc, rect{l, tp + h - t, l + w, tp + h}, white)
|
||||
fill(hdc, rect{l, tp, l + t, tp + h}, white)
|
||||
fill(hdc, rect{l + w - t, tp, l + w, tp + h}, white)
|
||||
fill(hdc, rect{cx - px(1), tp + h, cx + px(1), tp + h + px(3)}, white)
|
||||
fill(hdc, rect{cx - px(4), tp + h + px(3), cx + px(4), tp + h + px(4)}, white)
|
||||
return
|
||||
}
|
||||
|
||||
bars := 0
|
||||
if netKind == netWifi {
|
||||
bars = (netQuality + 24) / 25
|
||||
if bars < 1 {
|
||||
bars = 1
|
||||
}
|
||||
if bars > 4 {
|
||||
bars = 4
|
||||
}
|
||||
}
|
||||
heights := []int32{px(3), px(5), px(8), px(11)}
|
||||
baseY := cy + px(5)
|
||||
x := cx - px(7)
|
||||
for i := 0; i < 4; i++ {
|
||||
col := dim
|
||||
if i < bars {
|
||||
col = white
|
||||
}
|
||||
fill(hdc, rect{x, baseY - heights[i], x + px(2), baseY}, col)
|
||||
x += px(4)
|
||||
}
|
||||
if netKind == netNone {
|
||||
polyline(hdc, []point{{cx + px(1), cy - px(6)}, {cx + px(8), cy + px(1)}}, px(1), white)
|
||||
polyline(hdc, []point{{cx + px(1), cy + px(1)}, {cx + px(8), cy - px(6)}}, px(1), white)
|
||||
}
|
||||
}
|
||||
|
||||
func drawChevron(hdc uintptr, r rect) {
|
||||
cx, cy := center(r)
|
||||
polyline(hdc, []point{{cx - px(5), cy + px(2)}, {cx, cy - px(3)}, {cx + px(5), cy + px(2)}},
|
||||
px(1.5), rgb(245, 245, 245))
|
||||
}
|
||||
|
||||
func drawTray(hdc uintptr, w, h int32) {
|
||||
for _, it := range trayItems(w, h) {
|
||||
if it.kind == hoverTray {
|
||||
fill(hdc, rect{it.r.Left + px(1), it.r.Top + px(3), it.r.Right - px(1), it.r.Bottom - px(3)}, rgb(40, 40, 48))
|
||||
}
|
||||
if vertical && it.kind != trayClock {
|
||||
continue // a vertical bar draws these upright, after turning
|
||||
}
|
||||
switch it.kind {
|
||||
case trayClock:
|
||||
drawClock(hdc, it.r)
|
||||
case trayBattery:
|
||||
drawBattery(hdc, it.r)
|
||||
case trayVolume:
|
||||
drawVolume(hdc, it.r)
|
||||
case trayWifi:
|
||||
drawNetwork(hdc, it.r)
|
||||
case trayChevron:
|
||||
drawChevron(hdc, it.r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- battery ----------
|
||||
|
||||
type systemPowerStatus struct {
|
||||
ACLineStatus byte
|
||||
BatteryFlag byte
|
||||
BatteryLifePercent byte
|
||||
SystemStatusFlag byte
|
||||
BatteryLifeTime uint32
|
||||
BatteryFullLifeTime uint32
|
||||
}
|
||||
|
||||
func readBattery() (pct int, charging, ac bool) {
|
||||
var s systemPowerStatus
|
||||
r, _, _ := pGetSystemPowerStatus.Call(uintptr(unsafe.Pointer(&s)))
|
||||
if r == 0 || s.BatteryFlag == 255 || s.BatteryFlag&128 != 0 || s.BatteryLifePercent > 100 {
|
||||
return -1, false, false
|
||||
}
|
||||
return int(s.BatteryLifePercent), s.BatteryFlag&8 != 0, s.ACLineStatus == 1
|
||||
}
|
||||
|
||||
// ---------- volume (Windows Core Audio, called by hand) ----------
|
||||
|
||||
type guid struct {
|
||||
D1 uint32
|
||||
D2 uint16
|
||||
D3 uint16
|
||||
D4 [8]byte
|
||||
}
|
||||
|
||||
var (
|
||||
clsidMMDeviceEnumerator = guid{0xBCDE0395, 0xE52F, 0x467C, [8]byte{0x8E, 0x3D, 0xC4, 0x57, 0x92, 0x91, 0x69, 0x2E}}
|
||||
iidIMMDeviceEnumerator = guid{0xA95664D2, 0x9614, 0x4F35, [8]byte{0xA7, 0x46, 0xDE, 0x8D, 0xB6, 0x36, 0x17, 0xE6}}
|
||||
iidIAudioEndpointVolume = guid{0x5CDF2C82, 0x841E, 0x4546, [8]byte{0x97, 0x22, 0x0C, 0xF7, 0x40, 0x78, 0x22, 0x9A}}
|
||||
)
|
||||
|
||||
// vcall calls method number idx of a COM object.
|
||||
func vcall(obj unsafe.Pointer, idx uintptr, args ...uintptr) uintptr {
|
||||
vtbl := *(*unsafe.Pointer)(obj)
|
||||
fn := *(*uintptr)(unsafe.Add(vtbl, idx*unsafe.Sizeof(uintptr(0))))
|
||||
all := append([]uintptr{uintptr(obj)}, args...)
|
||||
r, _, _ := syscall.SyscallN(fn, all...)
|
||||
return r
|
||||
}
|
||||
|
||||
func failed(hr uintptr) bool { return int32(hr) < 0 }
|
||||
|
||||
// withEndpointVolume hands f the speakers' volume control, then releases everything.
|
||||
func withEndpointVolume(f func(vol unsafe.Pointer)) bool {
|
||||
var enum unsafe.Pointer
|
||||
hr, _, _ := pCoCreateInstance.Call(
|
||||
uintptr(unsafe.Pointer(&clsidMMDeviceEnumerator)), 0, 23,
|
||||
uintptr(unsafe.Pointer(&iidIMMDeviceEnumerator)), uintptr(unsafe.Pointer(&enum)))
|
||||
if failed(hr) || enum == nil {
|
||||
return false
|
||||
}
|
||||
defer vcall(enum, 2) // Release
|
||||
|
||||
var dev unsafe.Pointer
|
||||
// GetDefaultAudioEndpoint(eRender, eConsole)
|
||||
if failed(vcall(enum, 4, 0, 0, uintptr(unsafe.Pointer(&dev)))) || dev == nil {
|
||||
return false
|
||||
}
|
||||
defer vcall(dev, 2)
|
||||
|
||||
var vol unsafe.Pointer
|
||||
// Activate(IID_IAudioEndpointVolume, CLSCTX_ALL, NULL, &vol)
|
||||
if failed(vcall(dev, 3, uintptr(unsafe.Pointer(&iidIAudioEndpointVolume)), 23, 0, uintptr(unsafe.Pointer(&vol)))) || vol == nil {
|
||||
return false
|
||||
}
|
||||
defer vcall(vol, 2)
|
||||
|
||||
f(vol)
|
||||
return true
|
||||
}
|
||||
|
||||
func readVolume() (level float32, muted bool, ok bool) {
|
||||
ok = withEndpointVolume(func(vol unsafe.Pointer) {
|
||||
var l float32
|
||||
var m int32
|
||||
vcall(vol, 9, uintptr(unsafe.Pointer(&l))) // GetMasterVolumeLevelScalar
|
||||
vcall(vol, 15, uintptr(unsafe.Pointer(&m))) // GetMute
|
||||
level, muted = l, m != 0
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// ---------- network ----------
|
||||
|
||||
type wlanIfaceHeader struct {
|
||||
Count uint32
|
||||
Index uint32
|
||||
}
|
||||
|
||||
type wlanIfaceInfo struct {
|
||||
Guid guid
|
||||
Desc [256]uint16
|
||||
State uint32
|
||||
}
|
||||
|
||||
type wlanAssoc struct {
|
||||
SSIDLen uint32
|
||||
SSID [32]byte
|
||||
BssType uint32
|
||||
Bssid [6]byte
|
||||
PhyType uint32
|
||||
PhyIndex uint32
|
||||
Quality uint32
|
||||
RxRate uint32
|
||||
TxRate uint32
|
||||
}
|
||||
|
||||
type wlanConn struct {
|
||||
State uint32
|
||||
Mode uint32
|
||||
Profile [256]uint16
|
||||
Assoc wlanAssoc
|
||||
}
|
||||
|
||||
// wifiState reports whether a Wi-Fi adapter is connected and its signal (0 to 100).
|
||||
func wifiState() (connected bool, quality int) {
|
||||
var negotiated uint32
|
||||
var client uintptr
|
||||
if r, _, _ := pWlanOpenHandle.Call(2, 0, uintptr(unsafe.Pointer(&negotiated)), uintptr(unsafe.Pointer(&client))); r != 0 {
|
||||
return false, 0
|
||||
}
|
||||
defer pWlanCloseHandle.Call(client, 0)
|
||||
|
||||
var list unsafe.Pointer
|
||||
if r, _, _ := pWlanEnumInterfaces.Call(client, 0, uintptr(unsafe.Pointer(&list))); r != 0 || list == nil {
|
||||
return false, 0
|
||||
}
|
||||
defer pWlanFreeMemory.Call(uintptr(list))
|
||||
|
||||
n := int((*wlanIfaceHeader)(list).Count)
|
||||
first := unsafe.Add(list, unsafe.Sizeof(wlanIfaceHeader{}))
|
||||
for i := 0; i < n; i++ {
|
||||
info := (*wlanIfaceInfo)(unsafe.Add(first, uintptr(i)*unsafe.Sizeof(wlanIfaceInfo{})))
|
||||
if info.State != 1 { // wlan_interface_state_connected
|
||||
continue
|
||||
}
|
||||
var dataSize uint32
|
||||
var data unsafe.Pointer
|
||||
var valueType uint32
|
||||
r, _, _ := pWlanQueryInterface.Call(client, uintptr(unsafe.Pointer(&info.Guid)),
|
||||
7, // wlan_intf_opcode_current_connection
|
||||
0, uintptr(unsafe.Pointer(&dataSize)), uintptr(unsafe.Pointer(&data)), uintptr(unsafe.Pointer(&valueType)))
|
||||
if r != 0 || data == nil {
|
||||
continue
|
||||
}
|
||||
q := int((*wlanConn)(data).Assoc.Quality)
|
||||
pWlanFreeMemory.Call(uintptr(data))
|
||||
return true, q
|
||||
}
|
||||
return false, 0
|
||||
}
|
||||
|
||||
// wiredUp reports whether a real wired network adapter has a usable address.
|
||||
func wiredUp() bool {
|
||||
ifs, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
skip := []string{"virtual", "vethernet", "vmware", "vbox", "bluetooth", "wsl", "hyper-v",
|
||||
"loopback", "wi-fi", "wireless", "wlan", "pseudo", "tailscale", "vpn"}
|
||||
for _, i := range ifs {
|
||||
if i.Flags&net.FlagUp == 0 || i.Flags&net.FlagLoopback != 0 || len(i.HardwareAddr) == 0 {
|
||||
continue
|
||||
}
|
||||
name := strings.ToLower(i.Name)
|
||||
bad := false
|
||||
for _, s := range skip {
|
||||
if strings.Contains(name, s) {
|
||||
bad = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if bad {
|
||||
continue
|
||||
}
|
||||
addrs, _ := i.Addrs()
|
||||
for _, a := range addrs {
|
||||
ipn, ok := a.(*net.IPNet)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if ip4 := ipn.IP.To4(); ip4 != nil && !(ip4[0] == 169 && ip4[1] == 254) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func readNetwork() (kind int, quality int) {
|
||||
if ok, q := wifiState(); ok {
|
||||
return netWifi, q
|
||||
}
|
||||
if wiredUp() {
|
||||
return netWired, 0
|
||||
}
|
||||
return netNone, 0
|
||||
}
|
||||
|
||||
// ---------- refresh ----------
|
||||
|
||||
// refreshStatus reads battery and volume every call, and the network when full
|
||||
// is set. Returns true if anything shown on the bar changed.
|
||||
func refreshStatus(full bool) bool {
|
||||
changed := false
|
||||
|
||||
pct, ch, ac := readBattery()
|
||||
if pct != battPct || ch != battCharging || ac != battAC {
|
||||
battPct, battCharging, battAC = pct, ch, ac
|
||||
changed = true
|
||||
}
|
||||
|
||||
lvl, mu, ok := readVolume()
|
||||
diff := lvl - volLevel
|
||||
if diff < 0 {
|
||||
diff = -diff
|
||||
}
|
||||
if ok != volOK || mu != volMuted || (ok && diff > 0.004) {
|
||||
volLevel, volMuted, volOK = lvl, mu, ok
|
||||
changed = true
|
||||
}
|
||||
|
||||
if full {
|
||||
k, q := readNetwork()
|
||||
if k != netKind || (q+24)/25 != (netQuality+24)/25 {
|
||||
netKind, netQuality = k, q
|
||||
changed = true
|
||||
} else {
|
||||
netQuality = q
|
||||
}
|
||||
}
|
||||
return changed
|
||||
}
|
||||
|
||||
// ---------- clicks and menus ----------
|
||||
|
||||
func tapKey(k uintptr) {
|
||||
pKeybdEvent.Call(k, 0, 0, 0)
|
||||
pKeybdEvent.Call(k, 0, keyUp, 0)
|
||||
}
|
||||
|
||||
func tapChord(mod, k uintptr) {
|
||||
pKeybdEvent.Call(mod, 0, 0, 0)
|
||||
pKeybdEvent.Call(k, 0, 0, 0)
|
||||
pKeybdEvent.Call(k, 0, keyUp, 0)
|
||||
pKeybdEvent.Call(mod, 0, keyUp, 0)
|
||||
}
|
||||
|
||||
// trayClick opens the matching Windows panel with its keyboard shortcut.
|
||||
func trayClick(kind int) {
|
||||
switch kind {
|
||||
case trayClock:
|
||||
tapChord(vkLWin, 'N') // notification center and calendar
|
||||
case trayBattery, trayVolume, trayWifi:
|
||||
tapChord(vkLWin, 'A') // quick settings
|
||||
case trayChevron:
|
||||
// Win+B moves focus to the notification area; Enter opens the hidden icons.
|
||||
tapChord(vkLWin, 'B')
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
tapKey(vkReturn)
|
||||
}
|
||||
}
|
||||
|
||||
// nudgeVolume presses the volume keys, one press per wheel notch.
|
||||
func nudgeVolume(steps int) {
|
||||
key := uintptr(vkVolumeUp)
|
||||
if steps < 0 {
|
||||
key = vkVolumeDown
|
||||
steps = -steps
|
||||
}
|
||||
for i := 0; i < steps; i++ {
|
||||
tapKey(key)
|
||||
}
|
||||
time.Sleep(60 * time.Millisecond)
|
||||
refreshStatus(false)
|
||||
}
|
||||
|
||||
func batteryText() string {
|
||||
s := "Battery: " + strconv.Itoa(battPct) + "%"
|
||||
switch {
|
||||
case battCharging:
|
||||
s += ", charging"
|
||||
case battAC:
|
||||
s += ", plugged in"
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func volumeText() string {
|
||||
switch {
|
||||
case !volOK:
|
||||
return "Volume: unavailable"
|
||||
case volMuted:
|
||||
return "Volume: muted"
|
||||
}
|
||||
return "Volume: " + strconv.Itoa(int(volLevel*100+0.5)) + "%"
|
||||
}
|
||||
|
||||
func networkText() string {
|
||||
switch netKind {
|
||||
case netWifi:
|
||||
return "Wi-Fi: connected, signal " + strconv.Itoa(netQuality) + "%"
|
||||
case netWired:
|
||||
return "Ethernet: connected"
|
||||
}
|
||||
return "No network connection"
|
||||
}
|
||||
|
||||
// addTrayMenu adds the entries for the right-side item that was right-clicked.
|
||||
func addTrayMenu(add func(flags uintptr, id uintptr, text string), sep func(), kind int) {
|
||||
switch kind {
|
||||
case trayClock:
|
||||
add(0, menuDateID, "Adjust date and time")
|
||||
sep()
|
||||
case trayBattery:
|
||||
add(mfGrayed, 0, batteryText())
|
||||
add(0, menuPowerID, "Power && battery settings")
|
||||
sep()
|
||||
case trayVolume:
|
||||
add(mfGrayed, 0, volumeText())
|
||||
if volMuted {
|
||||
add(0, menuMuteID, "Unmute")
|
||||
} else {
|
||||
add(0, menuMuteID, "Mute")
|
||||
}
|
||||
add(0, menuSoundID, "Sound settings")
|
||||
sep()
|
||||
case trayWifi:
|
||||
add(mfGrayed, 0, networkText())
|
||||
add(0, menuNetID, "Network && internet settings")
|
||||
sep()
|
||||
}
|
||||
}
|
||||
|
||||
// handleTrayCommand runs a menu choice from addTrayMenu. Returns true if it was one.
|
||||
func handleTrayCommand(cmd uintptr) bool {
|
||||
switch cmd {
|
||||
case menuDateID:
|
||||
shellOpen("ms-settings:dateandtime", "")
|
||||
case menuPowerID:
|
||||
shellOpen("ms-settings:powersleep", "")
|
||||
case menuSoundID:
|
||||
shellOpen("ms-settings:sound", "")
|
||||
case menuNetID:
|
||||
shellOpen("ms-settings:network", "")
|
||||
case menuAutostartID:
|
||||
setAutostart(!autostartEnabled())
|
||||
case menuMuteID:
|
||||
tapKey(vkVolumeMute)
|
||||
time.Sleep(60 * time.Millisecond)
|
||||
refreshStatus(false)
|
||||
default:
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user