From 43b06b2e1a6931113c4b0856a1980da2d2152782 Mon Sep 17 00:00:00 2001 From: Ruben Pollan Date: Fri, 21 Dec 2018 18:23:11 +0100 Subject: [bug] launch the systray ASAP so notifications work The linux version of the notifications library we are using doesn't handle the gtk.main loop. It requires the systray to be running to be able to display a notification. Spliting the start of the systray and the loop we can start the systray pretty early and later on launch the main loop once we have bitmask and other stuff ready. - Related: #88 --- main.go | 10 ++++- systray.go | 136 +++++++++++++++++++++++++++++++------------------------------ 2 files changed, 78 insertions(+), 68 deletions(-) diff --git a/main.go b/main.go index 6222cde..a9dc9e9 100644 --- a/main.go +++ b/main.go @@ -60,11 +60,17 @@ func main() { os.Exit(0) } + bt := bmTray{conf: conf} + go initialize(conf, &bt) + bt.start() +} + +func initialize(conf *systrayConfig, bt *bmTray) { if _, err := os.Stat(bitmask.ConfigPath); os.IsNotExist(err) { os.MkdirAll(bitmask.ConfigPath, os.ModePerm) } - err = acquirePID() + err := acquirePID() if err != nil { log.Fatal(err) } @@ -85,7 +91,7 @@ func main() { if err != nil { log.Printf("Error enabling autostart: %v", err) } - run(b, conf, notify, as) + bt.loop(b, notify, as) } func checkAndStartBitmask(b bitmask.Bitmask, notify *notificator, conf *systrayConfig) { diff --git a/systray.go b/systray.go index a37c0e8..c6c75e4 100644 --- a/systray.go +++ b/systray.go @@ -36,7 +36,10 @@ type bmTray struct { mStatus *systray.MenuItem mTurnOn *systray.MenuItem mTurnOff *systray.MenuItem + mHelp *systray.MenuItem mDonate *systray.MenuItem + mAbout *systray.MenuItem + mQuit *systray.MenuItem activeGateway *gatewayTray autostart autostart } @@ -46,26 +49,18 @@ type gatewayTray struct { name string } -func run(bm bitmask.Bitmask, conf *systrayConfig, notify *notificator, as autostart) { +func (bt *bmTray) start() { // XXX this removes the snap error message, but produces an invisible icon. // https://0xacab.org/leap/riseup_vpn/issues/44 // os.Setenv("TMPDIR", "/var/tmp") - bt := bmTray{bm: bm, conf: conf, notify: notify, autostart: as} systray.Run(bt.onReady, bt.onExit) } -func (bt bmTray) onExit() { - status, _ := bt.bm.GetStatus() - if status != "off" { - bt.bm.StopVPN() - } +func (bt *bmTray) onExit() { log.Println("Closing systray") } func (bt *bmTray) onReady() { - signalCh := make(chan os.Signal, 1) - signal.Notify(signalCh, os.Interrupt) - systray.SetIcon(icon.Off) bt.mStatus = systray.AddMenuItem(printer.Sprintf("Checking status..."), "") @@ -80,71 +75,80 @@ func (bt *bmTray) onReady() { bt.addGateways() } - mHelp := systray.AddMenuItem(printer.Sprintf("Help..."), "") + bt.mHelp = systray.AddMenuItem(printer.Sprintf("Help..."), "") bt.mDonate = systray.AddMenuItem(printer.Sprintf("Donate..."), "") - mAbout := systray.AddMenuItem(printer.Sprintf("About..."), "") + bt.mAbout = systray.AddMenuItem(printer.Sprintf("About..."), "") systray.AddSeparator() - mQuit := systray.AddMenuItem(printer.Sprintf("Quit"), "") + bt.mQuit = systray.AddMenuItem(printer.Sprintf("Quit"), "") +} - go func() { - ch := bt.bm.GetStatusCh() - if status, err := bt.bm.GetStatus(); err != nil { - log.Printf("Error getting status: %v", err) - } else { +func (bt *bmTray) loop(bm bitmask.Bitmask, notify *notificator, as autostart) { + bt.bm = bm + bt.notify = notify + bt.autostart = as + + signalCh := make(chan os.Signal, 1) + signal.Notify(signalCh, os.Interrupt) + + ch := bt.bm.GetStatusCh() + if status, err := bt.bm.GetStatus(); err != nil { + log.Printf("Error getting status: %v", err) + } else { + bt.changeStatus(status) + } + + for { + select { + case status := <-ch: + log.Println("status: " + status) bt.changeStatus(status) - } - for { - select { - case status := <-ch: - log.Println("status: " + status) - bt.changeStatus(status) + case <-bt.mTurnOn.ClickedCh: + log.Println("on") + bt.changeStatus("starting") + bt.bm.StartVPN(provider) + bt.conf.setUserStoppedVPN(false) + case <-bt.mTurnOff.ClickedCh: + log.Println("off") + bt.changeStatus("stopping") + bt.bm.StopVPN() + bt.conf.setUserStoppedVPN(true) + + case <-bt.mHelp.ClickedCh: + open.Run("https://riseup.net/vpn/support") + case <-bt.mDonate.ClickedCh: + bt.conf.setDonated() + open.Run("https://riseup.net/vpn/donate") + case <-bt.mAbout.ClickedCh: + bitmaskVersion, err := bt.bm.Version() + versionStr := version + if err != nil { + log.Printf("Error getting version: %v", err) + } else if bitmaskVersion != "" { + versionStr = fmt.Sprintf("%s (bitmaskd %s)", version, bitmaskVersion) + } + bt.notify.about(versionStr) - case <-bt.mTurnOn.ClickedCh: - log.Println("on") - bt.changeStatus("starting") - bt.bm.StartVPN(provider) - bt.conf.setUserStoppedVPN(false) - case <-bt.mTurnOff.ClickedCh: - log.Println("off") - bt.changeStatus("stopping") - bt.bm.StopVPN() - bt.conf.setUserStoppedVPN(true) - - case <-mHelp.ClickedCh: - open.Run("https://riseup.net/vpn/support") - case <-bt.mDonate.ClickedCh: - bt.conf.setDonated() - open.Run("https://riseup.net/vpn/donate") - case <-mAbout.ClickedCh: - bitmaskVersion, err := bt.bm.Version() - versionStr := version - if err != nil { - log.Printf("Error getting version: %v", err) - } else if bitmaskVersion != "" { - versionStr = fmt.Sprintf("%s (bitmaskd %s)", version, bitmaskVersion) - } - bt.notify.about(versionStr) - - case <-mQuit.ClickedCh: - err := bt.autostart.Disable() - if err != nil { - log.Printf("Error disabling autostart: %v", err) - } - systray.Quit() - case <-signalCh: - systray.Quit() - - case <-time.After(5 * time.Second): - if status, err := bt.bm.GetStatus(); err != nil { - log.Printf("Error getting status: %v", err) - } else { - bt.changeStatus(status) - } + case <-bt.mQuit.ClickedCh: + err := bt.autostart.Disable() + if err != nil { + log.Printf("Error disabling autostart: %v", err) + } + systray.Quit() + return + case <-signalCh: + systray.Quit() + return + + case <-time.After(5 * time.Second): + if status, err := bt.bm.GetStatus(); err != nil { + log.Printf("Error getting status: %v", err) + } else { + bt.changeStatus(status) } } - }() + } } func (bt *bmTray) addGateways() { -- cgit v1.2.3