package lcdproc import ( "fmt" "net" "net/textproto" "strconv" "strings" "git.maze.io/go/bragi/config" "git.maze.io/go/bragi/display" "github.com/sirupsen/logrus" ) const ( DefaultPort = "13666" DefaultAddr = "localhost:" + DefaultPort screenName = "bragi" ) type Client struct { Version string ProtocolVersion string addr string conn *textproto.Conn width, height int cellWidth, cellHeight int text [][]byte } func New(config *config.Display) (display.Text, error) { var ( addr, host, port string err error ) if host = config.Host; host == "" { host = "localhost" } if port = config.Port; port == "" { port = DefaultPort } addr = net.JoinHostPort(host, port) log := logrus.WithFields(logrus.Fields{ "driver": "lcdproc", "addr": addr, }) log.Debug("connecting to server") conn, err := textproto.Dial("tcp", addr) if err != nil { return nil, err } c := &Client{ addr: addr, conn: conn, } if err = c.hello(log); err != nil { _ = conn.Close() return nil, err } if err = c.setup(log); err != nil { _ = conn.Close() return nil, err } c.text = make([][]byte, c.height) for i := 0; i < c.height; i++ { c.text[i] = make([]byte, c.width) } return c, nil } func (c *Client) hello(log *logrus.Entry) (err error) { log.Trace("sending hello") var id uint if id, err = c.conn.Cmd("hello"); err != nil { return } c.conn.StartResponse(id) defer c.conn.EndResponse(id) var line string if line, err = c.conn.ReadLine(); err != nil { return } log.WithFields(logrus.Fields{ "data": line, "id": id, }).Trace("recieved response") var parts = strings.Split(line, " ") for i := 0; i < len(parts); i++ { switch part := parts[i]; part { case "connect": i++ // ignore "LCDproc" c.Version = parts[i] i++ log.Debugf("received version %s", c.Version) case "protocol": i++ c.ProtocolVersion = parts[i] log.Debugf("received protocol version %s", c.ProtocolVersion) case "lcd": // ignore case "wid": i++ c.width, _ = strconv.Atoi(parts[i]) case "hgt": i++ c.height, _ = strconv.Atoi(parts[i]) case "cellwid": i++ c.cellWidth, _ = strconv.Atoi(parts[i]) case "cellhgt": i++ c.cellHeight, _ = strconv.Atoi(parts[i]) } } log.Debugf("%dx%d display", c.width*c.cellWidth, c.height*c.cellHeight) return } func (c *Client) setup(log *logrus.Entry) (err error) { if err = c.sendCommand("client_set -name Bragi"); err != nil { return } if err = c.fireCommand("screen_del %s", screenName); err != nil { return } if err = c.fireCommand("screen_add %s", screenName); err != nil { return } if err = c.sendCommand("screen_set %s -cursor off -heartbeat off -priority foreground -duration -wid %d -hgt %d", screenName, c.width, c.height); err != nil { return } for y := 0; y < c.height; y++ { if err = c.sendCommand("widget_add %s row%d string", screenName, y); err != nil { return } } return } func (c *Client) log() *logrus.Entry { return logrus.WithFields(logrus.Fields{ "driver": "lcdproc", "addr": c.addr, }) } func (c *Client) sendCommand(format string, args ...interface{}) (err error) { log := c.log().WithField("command", fmt.Sprintf(format, args...)) log.Debug("sending command") var id uint if id, err = c.conn.Cmd(format, args...); err != nil { return } c.conn.StartResponse(id) defer c.conn.EndResponse(id) log = log.WithField("id", id) log.Trace("reading response") for { var line string if line, err = c.conn.ReadLine(); err != nil { log.WithError(err).Error("error reading response") c.conn.EndResponse(id) return } log.WithField("data", line).Trace("response") switch { case line == "success": return case strings.HasPrefix(line, "huh? "): return textproto.ProtocolError(line[5:]) case strings.HasPrefix(line, "listen "): continue case strings.HasPrefix(line, "ignore "): continue default: return textproto.ProtocolError("unexpected response: " + line) } } } func (c *Client) fireCommand(format string, args ...interface{}) (err error) { log := c.log().WithField("command", fmt.Sprintf(format, args...)) log.Debug("sending command") var id uint if id, err = c.conn.Cmd(format, args...); err != nil { return } c.conn.StartResponse(id) defer c.conn.EndResponse(id) log = log.WithField("id", id) log.Trace("reading response") for { var line string if line, err = c.conn.ReadLine(); err != nil { log.WithError(err).Error("error reading response") c.conn.EndResponse(id) return } log.WithField("data", line).Trace("response") switch { case line == "success": return case strings.HasPrefix(line, "huh? "): return case strings.HasPrefix(line, "listen "): continue case strings.HasPrefix(line, "ignore "): continue } } } func (c *Client) String() string { return fmt.Sprintf("lcdproc %s at %s", c.Version, c.addr) } func (c *Client) Columns() int { return c.width } func (c *Client) Rows() int { return c.height } func (c *Client) Halt() error { return c.conn.Close() } func (Client) Refresh() error { return nil } func (c *Client) Size() (w, h int) { return c.width * c.cellWidth, c.height * c.cellHeight } func (c *Client) Text(x, y int, message string) { w := len(message) if x+w > c.width { message = message[:c.width-x] } _ = c.fireCommand("widget_set %s main %d %d %q", screenName, x+1, y+1, message) } func init() { display.Register("lcdproc", func(config *config.Display) (display.Display, error) { return New(config) }) }