aboutsummaryrefslogtreecommitdiffstats
path: root/src/local
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-05-30 00:19:48 +0200
committerbptato <nincsnevem662@gmail.com>2024-06-20 17:50:22 +0200
commit60dc37269cd2dc8cdf23d9f77680f6af9490032f (patch)
tree9a72ba24daffa546f92704e7e06cf84fded2d89d /src/local
parenta146a22b11cea39bc691417d9d9a1292b7177552 (diff)
downloadchawan-60dc37269cd2dc8cdf23d9f77680f6af9490032f.tar.gz
img, loader: separate out png codec into cgi, misc improvements
* multi-processed and sandboxed PNG decoding & encoding (through local CGI) * improved request body passing (including support for output id as response body) * simplified & faster blob()/text() - now every request starts suspended, and OngoingData.buf has been replaced with loader's buffering capability * image caching: we no longer pull bitmaps from the container after every single getLines call Next steps: replace our bespoke PNG decoder with something more usable, add other decoders, and make them stream.
Diffstat (limited to 'src/local')
-rw-r--r--src/local/client.nim4
-rw-r--r--src/local/container.nim8
-rw-r--r--src/local/pager.nim40
-rw-r--r--src/local/term.nim6
4 files changed, 46 insertions, 12 deletions
diff --git a/src/local/client.nim b/src/local/client.nim
index 8e83892c..ee469652 100644
--- a/src/local/client.nim
+++ b/src/local/client.nim
@@ -438,10 +438,10 @@ proc acceptBuffers(client: Client) =
let pid = container.process
if item.fdout == item.fdin:
loader.shareCachedItem(container.cacheId, pid)
- loader.resume(@[item.istreamOutputId])
+ loader.resume(item.istreamOutputId)
else:
outCacheId = loader.addCacheFile(item.ostreamOutputId, pid).outputId
- loader.resume(@[item.istreamOutputId, item.ostreamOutputId])
+ loader.resume([item.istreamOutputId, item.ostreamOutputId])
w.swrite(outCacheId)
if item.fdin != -1:
# pass down fdout
diff --git a/src/local/container.nim b/src/local/container.nim
index b64606a1..8717610f 100644
--- a/src/local/container.nim
+++ b/src/local/container.nim
@@ -7,6 +7,7 @@ import std/unicode
import config/config
import config/mimetypes
+import img/bitmap
import io/bufstream
import io/dynstream
import io/promise
@@ -157,6 +158,7 @@ type
mainConfig*: Config
flags*: set[ContainerFlag]
images*: seq[PosBitmap]
+ cachedImages*: seq[NetworkBitmap]
luctx: LUContext
jsDestructor(Highlight)
@@ -1741,6 +1743,12 @@ proc highlightMarks*(container: Container; display: var FixedGrid;
hlformat.bgcolor = hlcolor
display[y * display.width + x].format = hlformat
+func findCachedImage*(container: Container; id: int): NetworkBitmap =
+ for bmp in container.cachedImages:
+ if bmp.imageId == id:
+ return bmp
+ return nil
+
proc handleEvent*(container: Container) =
container.handleCommand()
if container.needslines:
diff --git a/src/local/pager.nim b/src/local/pager.nim
index 007ad6a9..8760da86 100644
--- a/src/local/pager.nim
+++ b/src/local/pager.nim
@@ -11,6 +11,7 @@ import std/unicode
import config/chapath
import config/config
import config/mailcap
+import img/bitmap
import io/bufreader
import io/dynstream
import io/posixstream
@@ -474,9 +475,34 @@ proc draw*(pager: Pager) =
else:
pager.term.writeGrid(pager.statusgrid, 0, pager.attrs.height - 1)
pager.term.outputGrid()
- if container != nil and pager.redraw:
+ if container != nil and pager.redraw and pager.term.imageMode != imNone:
pager.term.clearImages()
for image in container.images:
+ if image.bmp of NetworkBitmap:
+ # add cache file to pager, but source it from the container.
+ let bmp = NetworkBitmap(image.bmp)
+ let cached = container.findCachedImage(bmp.imageId)
+ if cached == nil:
+ let (cacheId, _) = pager.loader.addCacheFile(bmp.outputId,
+ pager.loader.clientPid, container.process)
+ let request = newRequest(newURL("cache:" & $cacheId).get)
+ # capture bmp for the closure
+ (proc(bmp: Bitmap) =
+ pager.loader.fetch(request).then(proc(res: JSResult[Response]):
+ EmptyPromise =
+ if res.isNone:
+ return nil
+ return res.get.saveToBitmap(bmp)
+ ).then(proc() =
+ pager.redraw = true
+ )
+ )(bmp)
+ pager.loader.resume(bmp.outputId) # get rid of dangling output
+ container.cachedImages.add(bmp)
+ continue
+ image.bmp = cached
+ if uint64(image.bmp.px.len) < image.bmp.width * image.bmp.height:
+ continue # loading
pager.term.outputImage(image.bmp, image.x - container.fromx,
image.y - container.fromy, pager.attrs.width, pager.attrs.height - 1)
if pager.askpromise != nil:
@@ -541,7 +567,6 @@ proc newContainer(pager: Pager; bufferConfig: BufferConfig;
redirectDepth = 0; flags = {cfCanReinterpret, cfUserRequested};
contentType = none(string); charsetStack: seq[Charset] = @[];
url = request.url; cacheId = -1; cacheFile = ""): Container =
- request.suspended = true
let stream = pager.loader.startRequest(request, loaderConfig)
pager.loader.registerFun(stream.fd)
let container = newContainer(
@@ -1021,6 +1046,9 @@ proc applySiteconf(pager: Pager; url: var URL; charsetOverride: Charset;
loaderConfig.insecureSSLNoVerify = sc.insecure_ssl_no_verify.get
if sc.autofocus.isSome:
res.autofocus = sc.autofocus.get
+ if res.images:
+ loaderConfig.filter.allowschemes
+ .add(pager.config.external.urimethodmap.imageProtos)
return res
# Load request in a new buffer.
@@ -1202,7 +1230,7 @@ proc updateReadLineISearch(pager: Pager; linemode: LineMode) =
proc saveTo(pager: Pager; data: LineDataDownload; path: string) =
if pager.loader.redirectToFile(data.outputId, path):
pager.alert("Saving file to " & path)
- pager.loader.resume(@[data.outputId])
+ pager.loader.resume(data.outputId)
data.stream.sclose()
pager.lineData = nil
else:
@@ -1544,7 +1572,7 @@ proc filterBuffer(pager: Pager; stream: SocketStream; cmd: string;
let url = parseURL("stream:" & $pid).get
pager.loader.passFd(url.pathname, FileHandle(fdout))
safeClose(fdout)
- let response = pager.loader.doRequest(newRequest(url, suspended = true))
+ let response = pager.loader.doRequest(newRequest(url))
return CheckMailcapResult(
connect: true,
fdout: response.body.fd,
@@ -1607,7 +1635,7 @@ proc checkMailcap(pager: Pager; container: Container; stream: SocketStream;
block needsConnect:
if entry.flags * {COPIOUSOUTPUT, HTMLOUTPUT, ANSIOUTPUT} == {}:
# No output. Resume here, so that blocking needsterminal filters work.
- pager.loader.resume(@[istreamOutputId])
+ pager.loader.resume(istreamOutputId)
if canpipe:
pager.runMailcapWritePipe(stream, needsterminal, cmd)
else:
@@ -1632,7 +1660,7 @@ proc checkMailcap(pager: Pager; container: Container; stream: SocketStream;
let url = parseURL("stream:" & $pid).get
pager.loader.passFd(url.pathname, FileHandle(fdout))
safeClose(cint(fdout))
- let response = pager.loader.doRequest(newRequest(url, suspended = true))
+ let response = pager.loader.doRequest(newRequest(url))
return CheckMailcapResult(
connect: true,
fdout: response.body.fd,
diff --git a/src/local/term.nim b/src/local/term.nim
index 7dd6d951..db479734 100644
--- a/src/local/term.nim
+++ b/src/local/term.nim
@@ -66,7 +66,7 @@ type
attrs*: WindowAttributes
colorMode: ColorMode
formatMode: FormatMode
- imageMode: ImageMode
+ imageMode*: ImageMode
smcup: bool
tc: Termcap
tname: string
@@ -717,8 +717,6 @@ proc outputKittyImage(term: Terminal; x, y, offx, offy, dispw, disph: int;
# x, y, maxw, maxh in cells
# x, y can be negative, then image starts outside the screen
proc outputImage*(term: Terminal; bmp: Bitmap; x, y, maxw, maxh: int) =
- if term.imageMode == imNone:
- return
var xpx = x * term.attrs.ppc
var ypx = y * term.attrs.ppl
# calculate offset inside image to start from
@@ -737,7 +735,7 @@ proc outputImage*(term: Terminal; bmp: Bitmap; x, y, maxw, maxh: int) =
let x = max(x, 0)
let y = max(y, 0)
case term.imageMode
- of imNone: discard
+ of imNone: assert false
of imSixel: term.outputSixelImage(x, y, offx, offy, dispw, disph, bmp)
of imKitty: term.outputKittyImage(x, y, offx, offy, dispw, disph, bmp)