3 Commits

Author SHA1 Message Date
4b39024091 Update randopix.nimble 2020-05-28 21:43:47 +02:00
9b8323349c Update build.yml 2020-05-23 19:02:11 +02:00
82232f0ccf Update build.yml 2020-05-23 18:57:21 +02:00
15 changed files with 190 additions and 476 deletions

30
.github/workflows/build.yml vendored Normal file
View File

@@ -0,0 +1,30 @@
name: Build randopix
# This workflow is triggered on pushes to the repository.
on:
push:
branches:
- build
jobs:
build:
name: Build 64 Bit Linux
# This job runs on Linux
runs-on: ubuntu-latest
steps:
- name: Install GTK packages
run: sudo apt install gir1.2-gtk-3.0 gir1.2-graphene-1.0 gir1.2-gtksource-3.0 gir1.2-vte-2.91 gir1.2-notify-0.7 gir1.2-gst-plugins-bad-1.0
- uses: actions/checkout@master
- name: Cache choosenim
id: cache-choosenim
uses: actions/cache@v1
with:
path: ~/.choosenim
key: ${{ runner.os }}-choosenim-stable
- name: Cache nimble
id: cache-nimble
uses: actions/cache@v1
with:
path: ~/.nimble
key: ${{ runner.os }}-nimble-stable
- uses: jiro4989/setup-nim-action@v1.0.2
- run: nimble build -Y

10
.gitignore vendored
View File

@@ -1,4 +1,12 @@
# Ignore all
*
# Unignore all with extensions
!*.*
# Unignore all dirs
!*/
### Above combination will ignore all files without extension ###
src/resources/pixctrl.js
bin/
.vscode/

View File

@@ -1,9 +0,0 @@
[Unit]
Description=randopix image display
[Service]
Environment=DISPLAY=:0
ExecStart=randopix -d /mnt/pix -m file
[Install]
WantedBy=default.target

View File

@@ -1,29 +1,18 @@
import strformat
# Package
version = "1.0.0"
version = "0.1.0"
author = "luxick"
description = "Play an image slide show from different sources"
license = "GPL-2.0"
srcDir = "src"
binDir = "bin"
bin = @["randopix", "pixctrl"]
# Dependencies
requires "nim >= 1.0.0", "gintro", "argparse", "jester", "ajax"
proc genJS =
echo "Generating JS Client"
exec("nim js -o:src/resources/pixctrl.js src/pixctrl.nim")
task genJS, "Generate the Javascript client":
genJS()
task buildAll, "Generate JS and run build":
genJS()
exec "nimble build"
requires "nim >= 1.0.0", "gintro <= 0.5.5", "argparse >=0.10.1"
task debug, "Compile debug version":
exec "nim c -d:debug --debugger:native -o:bin/randopix src/randopix.nim"
exec "nim c -d:debug --debugger:native --out:randopix src/randopix.nim"
before install:
genJS()
task release, "Compile release version":
exec fmt"nim c -d:release --out:randopix-{version} src/randopix.nim"

View File

@@ -2,8 +2,8 @@ window {
background: black;
font-size: 30px;
}
box {
background-color: rgba(255, 255, 255, .75);
label {
background-color: #fff;
border: 2px solid gray;
border-radius: 5px;
margin: 10px;

View File

@@ -1,5 +1,8 @@
import json
const
defaultPort* = 5555 ## Default port at which the control server will run
type
OpResult* = object of RootObj ## Result object for signalling failure state across proc calls
success*: bool ## Indicating if the opration was successfull
@@ -21,18 +24,18 @@ type
command*: Command ## Command that the application should execute
parameter*: string ## Optional parameter for the command
proc `$`(cMsg: CommandMessage): string =
$(%cMsg)
proc newOpResult*(): OpResult =
OpResult(success: true)
proc newOpResult*(msg: string): OpResult =
OpResult(success: false, errorMsg: msg)
proc newCommandMessage*(c: Command, p: string = ""): CommandMessage =
proc newCommand*(c: Command, p: string = ""): CommandMessage =
CommandMessage(command: c, parameter: p)
proc wrap*(msg: CommandMessage): string =
$(%msg) & "\r\L"
proc enumToStrings*(en: typedesc): seq[string] =
for x in en:
result.add $x

View File

@@ -1,94 +1,52 @@
import strutils, json
import strutils, net
import argparse
import common
when defined(js):
import ajax, jsconsole, dom
else:
import httpClient, strformat
import argparse
var randopixServer* {.exportc.}: string ## URL for the randopix server
const modeHelp = "Change the display mode. Possible values: [$1]" % Mode.enumToStrings().join(", ")
proc sendCommand(msg: CommandMessage) =
when defined(js):
console.log("Sending:", $msg, "to URL:", document.URL)
var req = newXMLHttpRequest()
var socket = newSocket()
proc processSend(e:Event) =
if req.readyState == rsDONE:
if req.status != 200:
console.log("There was a problem with the request.")
console.log($req.status, req.statusText)
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()
req.onreadystatechange = processSend
req.open("POST", document.URL)
req.send(cstring($(%*msg)))
else:
let client = newHttpClient()
let resp = client.post(randopixServer, $(%msg))
if not resp.status.contains("200"):
echo "Error while sending command: ", resp.status
proc sendCommand(cmd: Command) =
sendCommand(newCommandMessage(cmd))
proc switchMode*(mode: string) =
## Update the display mode
proc switchMode*(server, port: string, mode: string) =
try:
discard parseEnum[Mode](mode)
except ValueError:
echo "Invalid mode: ", mode
echo "Possible values: [$1]" % Mode.enumToStrings().join(", ")
return
sendCommand(newCommandMessage(cMode, mode))
let c = newCommand(cMode, mode)
sendCommand(server, port, c)
proc refresh*() {.exportc.} =
## Force refresh of the current image
sendCommand(cRefresh)
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)
proc setTimeout*(seconds: string) =
## Set the image timeout to this value
sendCommand(newCommandMessage(cTimeout, seconds))
when defined(js):
proc getModes(): seq[cstring] {.exportc.} =
for mode in enumToStrings(Mode):
result.add cstring(mode)
proc switchMode*(mode: cstring) {.exportc.} =
switchMode($mode)
proc setTimeout*(seconds: cstring) {.exportc.} =
setTimeout($seconds)
else:
when isMainModule:
const modeHelp = "Change the display mode. Possible values: [$1]" % Mode.enumToStrings().join(", ")
var p = newParser("pixctrl"):
help("Control utilitiy for randopix")
option("-s", "--server", help="Host running the randopix server", default="http://localhost:8080/")
command($cRefresh):
help("Force image refresh now")
run:
if opts.server.startsWith("http://"):
randopixServer = opts.server
else:
randopixServer = fmt"http://{opts.server}"
let c = newCommand(cRefresh)
sendCommand(opts.parentOpts.server, opts.parentOpts.port, c)
command($cRefresh):
## Force refresh command
help("Force image refresh now")
run:
refresh()
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($cTimeout):
## Timeout Command
help("Set timeout in seconds before a new image is displayed")
arg("seconds", default = "300")
run:
setTimeout(opts.seconds)
command($cMode):
## Mode switch command
help(modeHelp)
arg("mode")
run:
switchMode(opts.mode)
try:
p.run(commandLineParams())
except:
echo getCurrentExceptionMsg()
command($cMode):
help(modeHelp)
arg("mode")
run:
switchMode(opts.parentOpts.server, opts.parentOpts.port, opts.mode)
try:
p.run(commandLineParams())
except:
echo p.help

View File

@@ -1,4 +1,4 @@
import os, sets, random, httpClient, json, strutils, strformat, options, deques, times
import os, sets, random, httpClient, json, strformat, options
import gintro/[gdkpixbuf, gobject]
import common
@@ -6,6 +6,7 @@ const
supportedExts = @[".png", ".jpg", ".jpeg"]
foxesUrl = "https://randomfox.ca/floof/"
inspiroUrl = "http://inspirobot.me/api?generate=true"
tmpFile = "/tmp/randopix_tmp.png"
type
FileOpResult* = object of OpResult
@@ -17,18 +18,17 @@ type
mode* : Mode ## Selects the API that is used to get images
path*: Option[string] ## Path on the local file syetem that will be used in `file` mode
exts: HashSet[string] ## Allowed extensions that the `file` mode will display
files: seq[string] ## Currently loaded list of images in `file` mode
var
client = newHttpClient() ## For loading images from the web
tmpDir = getTempDir() / "randopix"
tmpFile = tmpDir / "tmp.png"
fileList = initDeque[string]()
########################
# Constructors
########################
proc newImageProvider(verbose: bool, mode: Mode, path: Option[string]): ImageProvider =
randomize()
ImageProvider(verbose: verbose, mode: mode, path: path, exts: supportedExts.toHashSet)
proc newImageProvider*(verbose: bool): ImageProvider =
@@ -53,9 +53,8 @@ proc newFileOpResult(file: string): FileOpResult =
# Utilities
########################
proc log(ip: ImageProvider, things: varargs[string, `$`]) =
if ip.verbose:
echo things.join()
proc log(ip: ImageProvider, msg: string) =
if ip.verbose: echo msg
########################
# Image Provider procs
@@ -95,24 +94,20 @@ proc getLocalFile(ip: var ImageProvider): FileOpResult =
# First, check if there are still images left to be loaded.
# If not reread all files from the path
if fileList.len == 0:
var tmp: seq[string]
var split: tuple[dir, name, ext: string]
if ip.files.len < 1:
if ip.path.isNone:
return newFileOpResultError("No path for image loading")
ip.log "Reloading file list..."
for file in walkDirRec(ip.path.get):
split = splitFile(file)
let split = splitFile(file)
if ip.exts.contains(split.ext):
tmp.add($file)
ip.files.add(file)
ip.log fmt"Loaded {ip.files.len} files"
shuffle(ip.files)
ip.log fmt"Loaded {tmp.len} files"
shuffle(tmp)
for file in tmp:
fileList.addLast(file)
if fileList.len == 0:
return newFileOpResultError("No files found")
let next = fileList.popFirst()
# Remove the current file after
result = newFileOpResult(next)
result = newFileOpResult(ip.files[0])
ip.files.delete(0)
proc getFileName(ip: var ImageProvider): FileOpResult =
## Get the temporary file name of the next file to display
@@ -148,10 +143,7 @@ proc next*(ip: var ImageProvider, width, height: int): FileOpResult =
else:
w = width
h = ((rawPixbuf.height * w) / rawPixbuf.width).toInt
let then = now()
var pixbuf = rawPixbuf.scaleSimple(w, h, InterpType.nearest)
let now = now()
ip.log "Image scaled. Time: ", (now - then).inMilliseconds, "ms"
var pixbuf = rawPixbuf.scaleSimple(w, h, InterpType.bilinear)
# The pixbuf is written to disk and loaded again once because
# directly setting the image from a pixbuf will leak memory
let saved = pixbuf.savev(tmpFile, "png", @[])
@@ -159,10 +151,7 @@ proc next*(ip: var ImageProvider, width, height: int): FileOpResult =
return newFileOpResultError("Error while saving temporary image")
# GTK pixbuf leaks memory when not manually decreasing reference count
pixbuf.unref()
rawPixbuf.unref()
pixbuf.genericGObjectUnref()
rawPixbuf.genericGObjectUnref()
newFileOpResult(tmpFile)
createDir(tmpDir)
randomize()
newFileOpResult(tmpFile)

View File

@@ -5,21 +5,14 @@ import argparse except run
import providers, server, common
const
css = slurp("resources/app.css")
version = slurp("version")
helpString = [
"ESC\tClose program",
"H\tShow/Hide this help",
"F\tToggle fullscreen",
"U\tForce refresh"
].join("\n")
css = slurp("app.css")
version = "0.1"
type
Args = ref object
fullscreen: bool ## Applicaion is show in fullscreen mode
verbose: bool ## More debug information in notification label
timeout: int ## Milliseconds between image refreshes
port: int ## Port to host the control server
var
imageProvider: ImageProvider ## Gets images from the chosen source
@@ -28,7 +21,6 @@ var
# Widgets
window: ApplicationWindow
label: Label
box: Box
# Server vor recieving commands from external tools
serverWorker: system.Thread[ServerArgs]
@@ -36,22 +28,21 @@ proc log(things: varargs[string, `$`]) =
if args.verbose:
echo things.join()
proc notify(label: Label, things: varargs[string, `$`]) =
proc notify(label: Label, message: string = "") =
## Shows the notification box in the lower left corner.
## If no message is passed, the box will be hidden
label.text = things.join()
if (label.text == ""):
box.hide
label.text = message
if (message == ""):
label.hide
else:
box.show
label.show
proc newArgs(): Option[Args] =
let p = newParser("randopix"):
help(fmt"Version {version} - Display random images from different sources")
option("-m", "--mode", help="The image source mode.", choices=enumToStrings(Mode))
option("-d", "--directoy", help="Path to a directory with images for the 'file' mode")
option("-p", "--path", help="Path to a directory with images for the 'file' mode")
option("-t", "--timeout", help="Seconds before the image is refreshed", default="300")
option("-p", "--port", help="Port over which the control server should be accessible", default="8080")
flag("-w", "--windowed", help="Do not start in fullscreen mode")
flag("-v", "--verbose", help="Show more information")
@@ -70,8 +61,8 @@ proc newArgs(): Option[Args] =
startMode = Mode.None
# Create the image provider
if opts.directoy != "":
imageProvider = newImageProvider(opts.verbose, startMode, opts.directoy)
if opts.path != "":
imageProvider = newImageProvider(opts.verbose, startMode, opts.path)
else:
imageProvider = newImageProvider(opts.verbose, startMode)
@@ -85,8 +76,7 @@ proc newArgs(): Option[Args] =
return some(Args(
fullscreen: not opts.windowed,
verbose: opts.verbose,
timeout: timeout,
port: opts.port.parseInt))
timeout: timeout))
except:
echo p.help
@@ -116,19 +106,17 @@ proc updateImage(image: Image): bool =
e = getCurrentException()
msg = getCurrentExceptionMsg()
log "Got exception ", repr(e), " with message ", msg
label.notify "Error while refreshing, retrying..."
label.notify "Error while refreshing image, retrying..."
return false
proc timedUpdate(image: Image): bool =
discard updateImage(image)
# Force garbage collection now. Otherwise the RAM will fill up until the GC is triggered.
GC_fullCollect()
discard updateImage(image);
updateTimeout = int(timeoutAdd(uint32(args.timeout), timedUpdate, image))
return false
proc forceUpdate(action: SimpleAction; parameter: Variant; image: Image): void =
log "Refreshing..."
label.notify "Refreshing..."
log "Refreshing image..."
label.notify "Refreshing image..."
if updateTimeout > 0:
discard updateTimeout.remove
updateTimeout = int(timeoutAdd(500, timedUpdate, image))
@@ -149,17 +137,14 @@ proc checkServerChannel(image: Image): bool =
let val = msg.parameter.parseInt * 1000
log "Setting timeout to ", val
args.timeout = val
if updateTimeout > 0:
discard updateTimeout.remove
discard updateTimeout.remove
updateTimeout = int(timeoutAdd(uint32(args.timeout), timedUpdate, image))
of cMode:
try:
let mode = parseEnum[Mode](msg.parameter)
imageProvider.mode = mode
forceUpdate(nil, nil, image)
log "Switching mode: ", mode
label.notify fmt"Switch Mode: {msg.parameter.capitalizeAscii()}"
imageProvider.mode = mode
except ValueError:
log "Invalid mode: ", msg.parameter
@@ -177,28 +162,19 @@ proc toggleFullscreen(action: SimpleAction; parameter: Variant; window: Applicat
window.fullscreen
args.fullscreen = not args.fullscreen
proc toggleHelp(action: SimpleAction; parameter: Variant; box: Box) =
if box.visible:
box.hide
else:
box.show
proc cleanUp(w: ApplicationWindow, app: Application) =
## Stop the control server and exit the GTK application
log "Stopping control server..."
closeServer()
serverWorker.joinThread()
chan.close()
log "Server channel closed."
log "Server stopped."
app.quit()
proc quit(action: SimpleAction; parameter: Variant; app: Application) =
## Application quit event
cleanUp(window, app)
proc hidePointer(window: ApplicationWindow): void =
## Hides the mouse pointer for the application.
let cur = window.getDisplay().newCursorForDisplay(CursorType.blankCursor)
let win = window.getWindow()
win.setCursor(cur)
proc appActivate(app: Application) =
# Parse arguments from the command line
let parsed = newArgs()
@@ -218,26 +194,12 @@ proc appActivate(app: Application) =
addProviderForScreen(getDefaultScreen(), provider, STYLE_PROVIDER_PRIORITY_USER)
# Create all windgets we are gonna use
label = newLabel(fmt"Starting ('H' for help)...")
let spinner = newSpinner()
spinner.start()
box = newBox(Orientation.horizontal, 2)
box.halign = Align.`end`
box.valign = Align.`end`
box.packStart(spinner, true, true, 10)
box.packStart(label, true, true, 0)
let helpText = newLabel(helpString)
let helpBox = newBox(Orientation.vertical, 0)
helpBox.packStart(helpText, true, true, 0)
helpBox.halign = Align.start
helpBox.valign = Align.start
label = newLabel("Starting...")
label.halign = Align.`end`
label.valign = Align.`end`
let container = newOverlay()
container.addOverlay(box)
container.addOverlay(helpBox)
container.addOverlay(label)
window.add(container)
let image = newImage()
@@ -247,34 +209,24 @@ proc appActivate(app: Application) =
window.fullscreen
## Connect the GTK signals to the procs
var action: SimpleAction
action = newSimpleAction("fullscreen")
discard action.connect("activate", toggleFullscreen, window)
let fullscreenAction = newSimpleAction("fullscreen")
discard fullscreenAction.connect("activate", toggleFullscreen, window)
app.setAccelsForAction("win.fullscreen", "F")
window.actionMap.addAction(action)
window.actionMap.addAction(fullscreenAction)
action = newSimpleAction("quit")
discard action.connect("activate", quit, app)
let quitAction = newSimpleAction("quit")
discard quitAction.connect("activate", quit, app)
app.setAccelsForAction("win.quit", "Escape")
window.actionMap.addAction(action)
window.actionMap.addAction(quitAction)
action = newSimpleAction("update")
discard action.connect("activate", forceUpdate, image)
let updateImageAction = newSimpleAction("update")
discard updateImageAction.connect("activate", forceUpdate, image)
app.setAccelsForAction("win.update", "U")
window.actionMap.addAction(action)
action = newSimpleAction("help")
discard action.connect("activate", toggleHelp, helpBox)
app.setAccelsForAction("win.help", "H")
window.actionMap.addAction(action)
window.actionMap.addAction(updateImageAction)
window.connect("destroy", cleanUp, app)
window.connect("realize", hidePointer)
window.showAll
# Help is only shown on demand
helpBox.hide
# Setting the inital image
# Fix 1 second timeout to make sure all other initialization has finished
@@ -284,7 +236,7 @@ proc appActivate(app: Application) =
chan.open()
## Start the server for handling incoming commands
let serverArgs = ServerArgs(verbose: args.verbose, port: args.port)
let serverArgs = newServerArgs(args.verbose)
createThread(serverWorker, runServer, serverArgs)
discard idleAdd(checkServerChannel, image)

View File

@@ -1,3 +1,3 @@
threads:on
d:ssl
# gc:arc
gc:arc

View File

@@ -1,45 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="x-ua-compatible" content="ie=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>randopix</title>
<link rel="stylesheet" type="text/css" href="/site.css">
<script src="/pixctrl.js" type="text/javascript"></script>
<script src="/script.js" type="text/javascript"></script>
</head>
<body class="star-bg">
<main>
<h1 class="center">Randopix Remote</h1>
<div class="control">
<div class="control-info">Load the next image</div>
<div class="control-body">
<button type="button" class="btn" onclick="refresh()">Refresh Image Now!</button>
</div>
</div>
<div class="control">
<div class="control-info">Switch the image mode</div>
<div class="control-body">
<select id="modeselect"></select>
<button type="button" class="btn" onclick="js_setMode()">Set Mode</button>
</div>
</div>
<div class="control">
<div class="control-info">Adjust timespan between images</div>
<div class="control-body">
<input id="timeout" type="number">
<button type="button" class="btn" onclick="js_setTimeout()">Set Timeout</button>
</div>
</div>
</main>
</body>
</html>

View File

@@ -1,30 +0,0 @@
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
window.onload = () => {
const modes = getModes();
const modeselect = document.getElementById("modeselect");
modes.forEach(mode => {
let opt = document.createElement('option');
opt.value = mode;
opt.innerHTML = capitalize(mode)
modeselect.appendChild(opt);
});
}
function js_setTimeout() {
const elem = document.getElementById("timeout");
const timeout = parseInt(elem.value);
if (timeout < 1) {
console.error("timeout must be positive");
return;
}
setTimeout(timeout.toString());
elem.value = null;
}
function js_setMode() {
const modeselect = document.getElementById("modeselect");
switchMode(modeselect.value)
}

File diff suppressed because one or more lines are too long

View File

@@ -1,55 +1,60 @@
import asyncdispatch, strutils, json, logging
import jester
import net, json, strutils
import common
const
index = slurp("resources/index.html")
style = slurp("resources/site.css")
pixctrlJs = slurp("resources/pixctrl.js")
script = slurp("resources/script.js")
type
ServerArgs* = object of RootObj
verbose*: bool
port*: int
verbose: bool
var
chan*: Channel[CommandMessage]
verbose: bool
proc newServerArgs*(verbose: bool): ServerArgs =
ServerArgs(verbose: verbose)
proc log(things: varargs[string, `$`]) =
if verbose:
echo things.join()
router randopixRouter:
get "/":
resp index
get "/site.css":
resp(style, contentType="text/css")
get "/pixctrl.js":
resp(pixctrlJs, contentType="text/javascript")
get "/script.js":
resp(script, contentType="text/javascript")
post "/":
try:
log "Command from ", request.ip
let json = request.body.parseJson
let msg = json.to(CommandMessage)
log "Got message: ", $msg
# Pass command from client to main applicaiton
chan.send(msg)
resp Http200
except:
log "Error: ", getCurrentExceptionMsg()
proc closeServer*() =
## Sends a "Close" command to the server
var socket = newSocket()
socket.connect("127.0.0.1", Port(defaultPort))
let c = newCommand(cClose)
socket.send(c.wrap)
socket.close()
proc runServer*[ServerArgs](arg: ServerArgs) {.thread, nimcall.} =
verbose = arg.verbose
logging.setLogFilter(lvlInfo)
let port = Port(arg.port)
let settings = newSettings(port=port)
var server = initJester(randopixRouter, settings=settings)
server.serve()
var server = net.newSocket()
server.bindAddr(Port(defaultPort))
server.listen()
log "Control server is listening"
while true:
# Process client requests
var client = net.newSocket()
server.accept(client)
log "Client connected"
try:
var line = client.recvLine()
if line == "":
log "No data from client"
continue
var jsonData = parseJson(line)
let msg = jsonData.to(CommandMessage)
case msg.command
of cClose:
log "Server recieved termination command. Exiting."
break
else:
# Pass command from client to main applicaiton
chan.send(msg)
except OSError:
log "Server error: ", getCurrentExceptionMsg()
except:
log "Invalid command from client: ", getCurrentExceptionMsg()
log repr(getCurrentException())
server.close()

View File

@@ -1 +0,0 @@
1.0.0