r2918 - branches/ng-jtimer/jtimer-server/src/main/go
Author: echatellier Date: 2013-08-16 17:43:41 +0200 (Fri, 16 Aug 2013) New Revision: 2918 Url: http://chorem.org/projects/jtimer/repository/revisions/2918 Log: Add go client with systray support, systray menu and chromium launch Added: branches/ng-jtimer/jtimer-server/src/main/go/jtimer-40-green.png branches/ng-jtimer/jtimer-server/src/main/go/jtimer-40-orange.png Modified: branches/ng-jtimer/jtimer-server/src/main/go/conn.go branches/ng-jtimer/jtimer-server/src/main/go/hub.go branches/ng-jtimer/jtimer-server/src/main/go/jtimer.go branches/ng-jtimer/jtimer-server/src/main/go/systray.go branches/ng-jtimer/jtimer-server/src/main/go/ui.go Modified: branches/ng-jtimer/jtimer-server/src/main/go/conn.go =================================================================== --- branches/ng-jtimer/jtimer-server/src/main/go/conn.go 2013-07-26 21:09:49 UTC (rev 2917) +++ branches/ng-jtimer/jtimer-server/src/main/go/conn.go 2013-08-16 15:43:41 UTC (rev 2918) @@ -6,7 +6,7 @@ type connection struct { // The websocket connection. - ws *websocket.Conn + ws *websocket.Conn // Buffered channel of outbound messages. send chan string @@ -40,4 +40,4 @@ defer func() { h.unregister <- c }() go c.writer() c.reader() -} \ No newline at end of file +} Modified: branches/ng-jtimer/jtimer-server/src/main/go/hub.go =================================================================== --- branches/ng-jtimer/jtimer-server/src/main/go/hub.go 2013-07-26 21:09:49 UTC (rev 2917) +++ branches/ng-jtimer/jtimer-server/src/main/go/hub.go 2013-08-16 15:43:41 UTC (rev 2918) @@ -1,44 +1,44 @@ package main type hub struct { - // Registered connections. - connections map[*connection]bool + // Registered connections. + connections map[*connection]bool - // Inbound messages from the connections. - broadcast chan string + // Inbound messages from the connections. + broadcast chan string - // Register requests from the connections. - register chan *connection + // Register requests from the connections. + register chan *connection - // Unregister requests from connections. - unregister chan *connection + // Unregister requests from connections. + unregister chan *connection } -var h = hub{ - broadcast: make(chan string), - register: make(chan *connection), - unregister: make(chan *connection), - connections: make(map[*connection]bool), +var h = hub { + broadcast: make(chan string), + register: make(chan *connection), + unregister: make(chan *connection), + connections: make(map[*connection]bool), } func (h *hub) run() { - for { - select { - case c := <-h.register: - h.connections[c] = true - case c := <-h.unregister: - delete(h.connections, c) - close(c.send) - case m := <-h.broadcast: - for c := range h.connections { - select { - case c.send <- m: - default: - delete(h.connections, c) - close(c.send) - go c.ws.Close() - } - } - } - } + for { + select { + case c := <-h.register: + h.connections[c] = true + case c := <-h.unregister: + delete(h.connections, c) + close(c.send) + case m := <-h.broadcast: + for c := range h.connections { + select { + case c.send <- m: + default: + delete(h.connections, c) + close(c.send) + go c.ws.Close() + } + } + } + } } Added: branches/ng-jtimer/jtimer-server/src/main/go/jtimer-40-green.png =================================================================== (Binary files differ) Property changes on: branches/ng-jtimer/jtimer-server/src/main/go/jtimer-40-green.png ___________________________________________________________________ Added: svn:mime-type + image/png Added: branches/ng-jtimer/jtimer-server/src/main/go/jtimer-40-orange.png =================================================================== (Binary files differ) Property changes on: branches/ng-jtimer/jtimer-server/src/main/go/jtimer-40-orange.png ___________________________________________________________________ Added: svn:mime-type + image/png Modified: branches/ng-jtimer/jtimer-server/src/main/go/jtimer.go =================================================================== --- branches/ng-jtimer/jtimer-server/src/main/go/jtimer.go 2013-07-26 21:09:49 UTC (rev 2917) +++ branches/ng-jtimer/jtimer-server/src/main/go/jtimer.go 2013-08-16 15:43:41 UTC (rev 2918) @@ -1,17 +1,17 @@ /* * Copyright (C) 2013 CodeLutin, Chatellier Eric - * + * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as - * published by the Free Software Foundation, either version 3 of the + * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public + * + * You should have received a copy of the GNU General Public * License along with this program. If not, see * <http://www.gnu.org/licenses/gpl-3.0.html>. */ @@ -26,43 +26,40 @@ */ import ( - "fmt" - "log" - "time" - "os" - "net/http" - "github.com/BurntSushi/xgb" - "github.com/BurntSushi/xgb/xproto" - "github.com/BurntSushi/xgb/screensaver" - "code.google.com/p/go.net/websocket" + "code.google.com/p/go.net/websocket" + "fmt" + "github.com/BurntSushi/xgb" + "github.com/BurntSushi/xgb/screensaver" + "github.com/BurntSushi/xgb/xproto" + "log" + "net/http" + "os" + "time" ) func RunIdleReporting() { - X, err := xgb.NewConn() - if err != nil { - fmt.Println(err) - return - } + X, err := xgb.NewConn() + if err != nil { + fmt.Println(err) + return + } - screen := xproto.Setup(X).DefaultScreen(X) - //wid, err := xproto.NewWindowId(X) - draw := xproto.Drawable(screen.Root) - screensaver.Init(X) + screen := xproto.Setup(X).DefaultScreen(X) + //wid, err := xproto.NewWindowId(X) + draw := xproto.Drawable(screen.Root) + screensaver.Init(X) - // connect to webscocket server - origin := "http://localhost:8080/" - url := "ws://localhost:12345/jtimer" - - for { - time.Sleep(1 * time.Second) - cookie := screensaver.QueryInfo(X, draw) - reply, _ := cookie.Reply() - fmt.Println("Idle since", reply.MsSinceUserInput); - - ws, _ := websocket.Dial(url, "", origin) - msSinceUserInputString := fmt.Sprintf("{idle:%d}", reply.MsSinceUserInput) - websocket.Message.Send(ws, msSinceUserInputString) - } + //lastIdleTime := -1 + for { + time.Sleep(1 * time.Second) + cookie := screensaver.QueryInfo(X, draw) + reply, _ := cookie.Reply() + currentIdleTime := reply.MsSinceUserInput + fmt.Println("Idle since", currentIdleTime) + + msSinceUserInputString := fmt.Sprintf("{idle:%d}", currentIdleTime) + h.broadcast <- msSinceUserInputString + } } func StartWebsocketServer() { @@ -76,8 +73,13 @@ func main() { go StartWebsocketServer() go RunIdleReporting() - cmd := RunClient() - cmd.Start() + cmd := RunBrowser() + err := cmd.Start() + if err != nil { + log.Fatal(err) + } + time.Sleep(5 * time.Second) + ControlWindow() InstallSystray(&os.Args) cmd.Process.Kill() } Modified: branches/ng-jtimer/jtimer-server/src/main/go/systray.go =================================================================== --- branches/ng-jtimer/jtimer-server/src/main/go/systray.go 2013-07-26 21:09:49 UTC (rev 2917) +++ branches/ng-jtimer/jtimer-server/src/main/go/systray.go 2013-08-16 15:43:41 UTC (rev 2918) @@ -1,8 +1,8 @@ package main import ( - "github.com/mattn/go-gtk/gtk" "github.com/mattn/go-gtk/glib" + "github.com/mattn/go-gtk/gtk" ) func InstallSystray(args *[]string) { @@ -16,18 +16,18 @@ // Hide menu item miHide := gtk.NewMenuItemWithLabel("Cacher") miHide.Connect("activate", func() { - + }) popupMenu.Append(miHide) - + // Stop all task popupMenu.Append(gtk.NewSeparatorMenuItem()) miStop := gtk.NewMenuItemWithLabel("Arrêter toutes les tâches") miStop.Connect("activate", func() { - + }) popupMenu.Append(miStop) - + // Quit miQuit := gtk.NewMenuItemWithLabel("Quitter") miQuit.Connect("activate", func() { @@ -45,4 +45,4 @@ }) gtk.Main() -} \ No newline at end of file +} Modified: branches/ng-jtimer/jtimer-server/src/main/go/ui.go =================================================================== --- branches/ng-jtimer/jtimer-server/src/main/go/ui.go 2013-07-26 21:09:49 UTC (rev 2917) +++ branches/ng-jtimer/jtimer-server/src/main/go/ui.go 2013-08-16 15:43:41 UTC (rev 2918) @@ -2,10 +2,70 @@ import ( "os/exec" + "os/user" + "log" + "fmt" + "strings" + + "github.com/BurntSushi/xgb/xproto" + "github.com/BurntSushi/xgbutil" + "github.com/BurntSushi/xgbutil/ewmh" + //"github.com/BurntSushi/xgbutil/icccm" + //"github.com/BurntSushi/xgbutil/xwindow" ) -func RunClient() *exec.Cmd { +func RunBrowser() *exec.Cmd { + + usr, err := user.Current() + if err != nil { + log.Fatal( err ) + } + binary, _ := exec.LookPath("chromium") return exec.Command(binary, "--app=http://localhost:8080", - "--user-data-dir=/tmp/jtimer") + "--user-data-dir=" + usr.HomeDir + "/.jtimer/chromium") } + +func ControlWindow() { + // Connect to the X server using the DISPLAY environment variable. + X, err := xgbutil.NewConn() + if err != nil { + log.Fatal(err) + } + + // Get a list of all client ids. + clientids, err := ewmh.ClientListGet(X) + if err != nil { + log.Fatal(err) + } + + // Iterate through each client, find its name and find its size. + for _, clientid := range clientids { + name, _ := ewmh.WmNameGet(X, clientid) + + if strings.Contains(name, "jTimer") { + fmt.Println("Found jtimer (0x%x)", clientid) + + //win := xwindow.New(X, clientid) + + XConn := X.Conn() + xproto.MapWindow(XConn, clientid) + for { + ev, xerr := XConn.WaitForEvent() + if ev == nil && xerr == nil { + fmt.Println("Both event and error are nil. Exiting...") + return + } + + if ev != nil { + fmt.Printf("Event: %s\n", ev) + } + if xerr != nil { + fmt.Printf("Error: %s\n", xerr) + } + } + } + + //fmt.Printf("%s (0x%x)\n", name, clientid) + } +}
participants (1)
-
echatellier@users.chorem.org