MirrorBar+ part 3: local news flyout, system stats flyout with copy, feature switches
This commit is contained in:
+298
@@ -0,0 +1,298 @@
|
||||
package main
|
||||
|
||||
// System information for the stats panel: the kind of thing neofetch prints.
|
||||
// Everything is read from Windows itself (the registry and a few kernel calls);
|
||||
// nothing goes over the network.
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
var (
|
||||
pGlobalMemoryStatusEx = kern32t.NewProc("GlobalMemoryStatusEx")
|
||||
pGetTickCount64 = kern32t.NewProc("GetTickCount64")
|
||||
pGetLogicalDrives = kern32t.NewProc("GetLogicalDrives")
|
||||
pGetDriveType = kern32t.NewProc("GetDriveTypeW")
|
||||
pGetDiskFreeSpaceEx = kern32t.NewProc("GetDiskFreeSpaceExW")
|
||||
pGlobalAlloc = kern32t.NewProc("GlobalAlloc")
|
||||
pGlobalLock = kern32t.NewProc("GlobalLock")
|
||||
pGlobalUnlock = kern32t.NewProc("GlobalUnlock")
|
||||
pMoveMemory = kern32t.NewProc("RtlMoveMemory")
|
||||
pOpenClipboard = user32.NewProc("OpenClipboard")
|
||||
pEmptyClipboard = user32.NewProc("EmptyClipboard")
|
||||
pSetClipboardData = user32.NewProc("SetClipboardData")
|
||||
pCloseClipboard = user32.NewProc("CloseClipboard")
|
||||
)
|
||||
|
||||
type sysRow struct{ label, value string }
|
||||
|
||||
// ---------- the registry ----------
|
||||
|
||||
func regRead(path, name string) (buf []byte, ok bool) {
|
||||
var k syscall.Handle
|
||||
if syscall.RegOpenKeyEx(syscall.HKEY_LOCAL_MACHINE, u16(path), 0, syscall.KEY_READ, &k) != nil {
|
||||
return nil, false
|
||||
}
|
||||
defer syscall.RegCloseKey(k)
|
||||
var n, typ uint32
|
||||
if syscall.RegQueryValueEx(k, u16(name), nil, &typ, nil, &n) != nil || n == 0 {
|
||||
return nil, false
|
||||
}
|
||||
buf = make([]byte, n)
|
||||
if syscall.RegQueryValueEx(k, u16(name), nil, &typ, &buf[0], &n) != nil {
|
||||
return nil, false
|
||||
}
|
||||
return buf[:n], true
|
||||
}
|
||||
|
||||
func regString(path, name string) string {
|
||||
b, ok := regRead(path, name)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
w := make([]uint16, 0, len(b)/2)
|
||||
for i := 0; i+1 < len(b); i += 2 {
|
||||
c := binary.LittleEndian.Uint16(b[i:])
|
||||
if c == 0 {
|
||||
break
|
||||
}
|
||||
w = append(w, c)
|
||||
}
|
||||
return strings.TrimSpace(syscall.UTF16ToString(w))
|
||||
}
|
||||
|
||||
func regDword(path, name string) (uint32, bool) {
|
||||
b, ok := regRead(path, name)
|
||||
if !ok || len(b) < 4 {
|
||||
return 0, false
|
||||
}
|
||||
return binary.LittleEndian.Uint32(b), true
|
||||
}
|
||||
|
||||
// ---------- gathering ----------
|
||||
|
||||
type memStatusEx struct {
|
||||
Length uint32
|
||||
MemoryLoad uint32
|
||||
TotalPhys, AvailPhys uint64
|
||||
TotalPage, AvailPage uint64
|
||||
TotalVirt, AvailVirt uint64
|
||||
AvailExtendedVirtual uint64
|
||||
}
|
||||
|
||||
func gib(b uint64) string { return fmt.Sprintf("%.1f GiB", float64(b)/(1<<30)) }
|
||||
|
||||
func sysUptime() string {
|
||||
ms, _, _ := pGetTickCount64.Call()
|
||||
d := time.Duration(ms) * time.Millisecond
|
||||
days := int(d.Hours()) / 24
|
||||
hrs := int(d.Hours()) % 24
|
||||
mins := int(d.Minutes()) % 60
|
||||
var parts []string
|
||||
if days > 0 {
|
||||
parts = append(parts, fmt.Sprintf("%d day%s", days, plural(days)))
|
||||
}
|
||||
if days > 0 || hrs > 0 {
|
||||
parts = append(parts, fmt.Sprintf("%d hour%s", hrs, plural(hrs)))
|
||||
}
|
||||
parts = append(parts, fmt.Sprintf("%d min%s", mins, plural(mins)))
|
||||
return strings.Join(parts, ", ")
|
||||
}
|
||||
|
||||
func plural(n int) string {
|
||||
if n == 1 {
|
||||
return ""
|
||||
}
|
||||
return "s"
|
||||
}
|
||||
|
||||
func sysGather() []sysRow {
|
||||
var rows []sysRow
|
||||
add := func(l, v string) {
|
||||
if strings.TrimSpace(v) != "" {
|
||||
rows = append(rows, sysRow{l, v})
|
||||
}
|
||||
}
|
||||
|
||||
const cv = `SOFTWARE\Microsoft\Windows NT\CurrentVersion`
|
||||
build := regString(cv, "CurrentBuild")
|
||||
bn := 0
|
||||
fmt.Sscanf(build, "%d", &bn)
|
||||
name := regString(cv, "ProductName")
|
||||
if bn >= 22000 {
|
||||
name = strings.Replace(name, "Windows 10", "Windows 11", 1) // Windows 11 still says 10 here
|
||||
}
|
||||
ver := regString(cv, "DisplayVersion")
|
||||
arch := map[string]string{"amd64": "x86_64", "arm64": "arm64", "386": "x86"}[runtime.GOARCH]
|
||||
osText := name
|
||||
if ver != "" {
|
||||
osText += " " + ver
|
||||
}
|
||||
if arch != "" {
|
||||
osText += " " + arch
|
||||
}
|
||||
add("OS", osText)
|
||||
|
||||
const bios = `HARDWARE\DESCRIPTION\System\BIOS`
|
||||
host := strings.TrimSpace(regString(bios, "SystemManufacturer") + " " + regString(bios, "SystemProductName"))
|
||||
add("Host", host)
|
||||
|
||||
if build != "" {
|
||||
k := "NT 10.0." + build
|
||||
if ubr, ok := regDword(cv, "UBR"); ok {
|
||||
k += fmt.Sprintf(".%d", ubr)
|
||||
}
|
||||
add("Kernel", k)
|
||||
}
|
||||
add("Uptime", sysUptime())
|
||||
add("Shell", "MirrorBar")
|
||||
|
||||
sw, sh := screenSize()
|
||||
res := fmt.Sprintf("%dx%d", sw, sh)
|
||||
if s := int(scale*100 + 0.5); s != 100 {
|
||||
res += fmt.Sprintf(" (%d%% scaling)", s)
|
||||
}
|
||||
add("Resolution", res)
|
||||
|
||||
const cpuKey = `HARDWARE\DESCRIPTION\System\CentralProcessor\0`
|
||||
cpu := strings.Join(strings.Fields(regString(cpuKey, "ProcessorNameString")), " ")
|
||||
if cpu != "" {
|
||||
cpu += fmt.Sprintf(" (%d threads)", runtime.NumCPU())
|
||||
}
|
||||
add("CPU", cpu)
|
||||
|
||||
// Graphics adapters: the display class lists them as 0000, 0001, ...
|
||||
n := 0
|
||||
seen := map[string]bool{}
|
||||
for i := 0; i < 10; i++ {
|
||||
d := regString(fmt.Sprintf(`SYSTEM\CurrentControlSet\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}\%04d`, i), "DriverDesc")
|
||||
if d == "" || seen[d] || strings.Contains(d, "Basic Display") || strings.Contains(d, "Remote Display") {
|
||||
continue
|
||||
}
|
||||
seen[d] = true
|
||||
n++
|
||||
l := "GPU"
|
||||
if n > 1 {
|
||||
l = fmt.Sprintf("GPU %d", n)
|
||||
}
|
||||
add(l, d)
|
||||
}
|
||||
|
||||
var ms memStatusEx
|
||||
ms.Length = uint32(unsafe.Sizeof(ms))
|
||||
if r, _, _ := pGlobalMemoryStatusEx.Call(uintptr(unsafe.Pointer(&ms))); r != 0 && ms.TotalPhys > 0 {
|
||||
used := ms.TotalPhys - ms.AvailPhys
|
||||
add("Memory", fmt.Sprintf("%s / %s (%d%%)", gib(used), gib(ms.TotalPhys), used*100/ms.TotalPhys))
|
||||
}
|
||||
|
||||
// Fixed drives only; a sleeping network drive could make this wait.
|
||||
mask, _, _ := pGetLogicalDrives.Call()
|
||||
for i := 0; i < 26; i++ {
|
||||
if mask&(1<<uint(i)) == 0 {
|
||||
continue
|
||||
}
|
||||
root := string(rune('A'+i)) + `:\`
|
||||
if t, _, _ := pGetDriveType.Call(uintptr(unsafe.Pointer(u16(root)))); t != 3 {
|
||||
continue
|
||||
}
|
||||
var free, total, totalFree uint64
|
||||
r, _, _ := pGetDiskFreeSpaceEx.Call(uintptr(unsafe.Pointer(u16(root))),
|
||||
uintptr(unsafe.Pointer(&free)), uintptr(unsafe.Pointer(&total)), uintptr(unsafe.Pointer(&totalFree)))
|
||||
if r == 0 || total == 0 {
|
||||
continue
|
||||
}
|
||||
used := total - totalFree
|
||||
add("Disk ("+root[:2]+")", fmt.Sprintf("%s / %s (%d%%)", gib(used), gib(total), used*100/total))
|
||||
}
|
||||
|
||||
if battPct >= 0 {
|
||||
b := fmt.Sprintf("%d%%", battPct)
|
||||
switch {
|
||||
case battCharging:
|
||||
b += ", charging"
|
||||
case battAC:
|
||||
b += ", plugged in"
|
||||
}
|
||||
add("Battery", b)
|
||||
}
|
||||
add("Local IP", localIP())
|
||||
return rows
|
||||
}
|
||||
|
||||
// localIP is this PC's first private IPv4 address.
|
||||
func localIP() string {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
for _, a := range addrs {
|
||||
if ipn, ok := a.(*net.IPNet); ok && !ipn.IP.IsLoopback() {
|
||||
if v4 := ipn.IP.To4(); v4 != nil && v4.IsPrivate() {
|
||||
return v4.String()
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func sysUserHost() string {
|
||||
u := os.Getenv("USERNAME")
|
||||
h, _ := os.Hostname()
|
||||
if u == "" {
|
||||
return h
|
||||
}
|
||||
return u + "@" + h
|
||||
}
|
||||
|
||||
// sysText is everything as plain text, ready to paste.
|
||||
func sysText(rows []sysRow) string {
|
||||
head := sysUserHost()
|
||||
var sb strings.Builder
|
||||
sb.WriteString(head + "\r\n" + strings.Repeat("-", len([]rune(head))) + "\r\n")
|
||||
for _, r := range rows {
|
||||
sb.WriteString(r.label + ": " + r.value + "\r\n")
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// ---------- the clipboard ----------
|
||||
|
||||
// clipboardSet puts plain text on the clipboard. Returns false if it couldn't.
|
||||
func clipboardSet(text string) bool {
|
||||
w, err := syscall.UTF16FromString(text)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
for i := 0; i < 5; i++ { // another program may have it open for a moment
|
||||
if r, _, _ := pOpenClipboard.Call(hwndMain); r != 0 {
|
||||
break
|
||||
}
|
||||
if i == 4 {
|
||||
return false
|
||||
}
|
||||
time.Sleep(30 * time.Millisecond)
|
||||
}
|
||||
defer pCloseClipboard.Call()
|
||||
pEmptyClipboard.Call()
|
||||
const gmemMoveable, cfUnicodeText = 0x0002, 13
|
||||
h, _, _ := pGlobalAlloc.Call(gmemMoveable, uintptr(len(w)*2))
|
||||
if h == 0 {
|
||||
return false
|
||||
}
|
||||
p, _, _ := pGlobalLock.Call(h)
|
||||
if p == 0 {
|
||||
return false
|
||||
}
|
||||
pMoveMemory.Call(p, uintptr(unsafe.Pointer(&w[0])), uintptr(len(w)*2))
|
||||
pGlobalUnlock.Call(h)
|
||||
r, _, _ := pSetClipboardData.Call(cfUnicodeText, h)
|
||||
return r != 0 // the system owns the memory now
|
||||
}
|
||||
Reference in New Issue
Block a user