From 87bfce1a4666db9138769124f472626cdef72423 Mon Sep 17 00:00:00 2001 From: luxick Date: Sat, 23 May 2020 13:55:16 +0200 Subject: [PATCH] Add mode switching. --- src/app.css | 2 +- src/{commands.nim => common.nim} | 13 ++++++- src/pixctrl.nim | 61 +++++++++++++++++++++----------- src/providers.nim | 8 +---- src/randopix.nim | 22 ++++++++---- src/server.nim | 8 ++--- 6 files changed, 75 insertions(+), 39 deletions(-) rename src/{commands.nim => common.nim} (71%) diff --git a/src/app.css b/src/app.css index 3e204cc..0fb2e93 100644 --- a/src/app.css +++ b/src/app.css @@ -1,4 +1,4 @@ -window { +window { background: black; font-size: 30px; } diff --git a/src/commands.nim b/src/common.nim similarity index 71% rename from src/commands.nim rename to src/common.nim index 97d5cf0..478700d 100644 --- a/src/commands.nim +++ b/src/common.nim @@ -8,10 +8,17 @@ type success*: bool ## Indicating if the opration was successfull errorMsg*: string ## Error meassge in case the operation failed + Mode* {.pure.} = enum ## Options for the display mode + None = "none" ## No images will be displayed + Foxes = "foxes" ## Some nice foxes + Inspiro = "inspiro" ## Inspiring nonsense + File = "file" ## Images from a local path + Command* = enum cClose = "close" ## Closes the control server and exists the applicaiton cRefresh = "refresh" ## Force refresh of the image now cTimeout = "timeout" ## Set image timeout to a new value + cMode = "mode" ## Change the servers display mode CommandMessage* = object of RootObj command*: Command ## Command that the application should execute @@ -27,4 +34,8 @@ proc newCommand*(c: Command, p: string = ""): CommandMessage = CommandMessage(command: c, parameter: p) proc wrap*(msg: CommandMessage): string = - $(%msg) & "\r\L" \ No newline at end of file + $(%msg) & "\r\L" + +proc enumToStrings*(en: typedesc): seq[string] = + for x in en: + result.add $x \ No newline at end of file diff --git a/src/pixctrl.nim b/src/pixctrl.nim index 60a4159..edd8457 100644 --- a/src/pixctrl.nim +++ b/src/pixctrl.nim @@ -1,29 +1,50 @@ -import net +import strutils, net import argparse -import commands +import common var socket = newSocket() -proc sendCommand(server, port: string, msg: CommandMessage) = +proc sendCommand*(server, port: string, msg: CommandMessage) = socket.connect(server, Port(port.parseInt)) if not socket.trySend(msg.wrap): echo "Cannot send command: ", msg socket.close() -var p = newParser("pixctrl"): - help("Control utilitiy for randopix") - option("-s", "--server", help="Host running the randopix server", default="127.0.0.1") - option("-p", "--port", help="Port to connect to the randopix server", default = $defaultPort) - command("refresh"): - help("Force image refresh now") - run: - let c = newCommand(cRefresh) - sendCommand(opts.parentOpts.server, opts.parentOpts.port, c) - command("timeout"): - help("Set timeout in seconds before a new image is displayed") - arg("seconds", default = "300") - run: - let c = newCommand(cTimeout, opts.seconds) - sendCommand(opts.parentOpts.server, opts.parentOpts.port, c) - -p.run(commandLineParams()) \ No newline at end of file +proc switchMode*(server, port: string, mode: string) = + try: + discard parseEnum[Mode](mode) + except ValueError: + echo "Invalid mode: ", mode + echo "Accepted modes: ", enumToStrings(Mode).join(", ") + return + let c = newCommand(cMode, mode) + sendCommand(server, port, c) + +when isMainModule: + var p = newParser("pixctrl"): + help("Control utilitiy for randopix") + option("-s", "--server", help="Host running the randopix server", default="127.0.0.1") + option("-p", "--port", help="Port to connect to the randopix server", default = $defaultPort) + + command($cRefresh): + help("Force image refresh now") + run: + let c = newCommand(cRefresh) + sendCommand(opts.parentOpts.server, opts.parentOpts.port, c) + + command($cTimeout): + help("Set timeout in seconds before a new image is displayed") + arg("seconds", default = "300") + run: + let c = newCommand(cTimeout, opts.seconds) + sendCommand(opts.parentOpts.server, opts.parentOpts.port, c) + + command($cMode): + help("Change the display mode of the server") + arg("mode") + run: + switchMode(opts.parentOpts.server, opts.parentOpts.port, opts.mode) + try: + p.run(commandLineParams()) + except: + echo p.help \ No newline at end of file diff --git a/src/providers.nim b/src/providers.nim index dacde94..97c0575 100644 --- a/src/providers.nim +++ b/src/providers.nim @@ -1,6 +1,6 @@ import os, sets, random, httpClient, json, strformat, options import gintro/[gdkpixbuf, gobject] -import commands +import common const supportedExts = @[".png", ".jpg", ".jpeg"] @@ -12,12 +12,6 @@ type FileOpResult* = object of OpResult file*: string - Mode* {.pure.} = enum ## Options for the display mode - None = "none" ## No images will be displayed - Foxes = "foxes" ## Some nice foxes - Inspiro = "inspiro" ## Inspiring nonsense - File = "file" ## Images from a local path - ImageProvider* = ref object of RootObj ## Manages images that should be displayed verbose: bool ## Additional logging for the image provider diff --git a/src/randopix.nim b/src/randopix.nim index a6335a1..060bb4a 100644 --- a/src/randopix.nim +++ b/src/randopix.nim @@ -2,7 +2,7 @@ import os, options, strformat import gintro/[glib, gobject, gtk, gio] import gintro/gdk except Window import argparse except run -import providers, server, commands +import providers, server, common const css = slurp("app.css") @@ -24,10 +24,6 @@ var # Server vor recieving commands from external tools serverWorker: system.Thread[ServerArgs] -proc enumToStrings(en: typedesc): seq[string] = - for x in en: - result.add $x - proc log(things: varargs[string, `$`]) = if args.verbose: echo things.join() @@ -53,6 +49,10 @@ proc newArgs(): Option[Args] = try: let opts = p.parse(commandLineParams()) + # Catch the help option. Do nothing more + if opts.help: + return + # Parse the starting mode var startMode: Mode try: @@ -127,17 +127,27 @@ proc checkServerChannel(image: Image): bool = if tried.dataAvailable: let msg: CommandMessage = tried.msg - log "Main app got message: ", msg.command + log "Recieved command: ", msg.command case msg.command of cRefresh: forceUpdate(nil, nil, image) + of cTimeout: let val = msg.parameter.parseInt * 1000 log "Setting timeout to ", val args.timeout = val discard updateTimeout.remove updateTimeout = int(timeoutAdd(uint32(args.timeout), timedUpdate, image)) + + of cMode: + try: + let mode = parseEnum[Mode](msg.parameter) + log "Switching mode: ", mode + imageProvider.mode = mode + except ValueError: + log "Invalid mode: ", msg.parameter + else: log "Command ignored: ", msg.command diff --git a/src/server.nim b/src/server.nim index 7758540..ea21fe8 100644 --- a/src/server.nim +++ b/src/server.nim @@ -1,11 +1,11 @@ import net, json, strutils -import commands +import common type ServerArgs* = object of RootObj verbose: bool -var +var chan*: Channel[CommandMessage] verbose: bool @@ -31,7 +31,7 @@ proc runServer*[ServerArgs](arg: ServerArgs) {.thread, nimcall.} = server.listen() log "Control server is listening" - while true: + while true: # Process client requests var client = net.newSocket() server.accept(client) @@ -41,7 +41,7 @@ proc runServer*[ServerArgs](arg: ServerArgs) {.thread, nimcall.} = if line == "": log "No data from client" continue - + var jsonData = parseJson(line) let msg = jsonData.to(CommandMessage) case msg.command