shithub: femtolisp

Download patch

ref: 2d4baeba73c5f08383d21b4183d07a64992be56e
parent: 46828171bff7566180e8d0ae1b30debe1aa47fb9
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Mon Jan 6 15:45:23 EST 2025

remove sixel and terminal functionality

--- a/flisp.c
+++ b/flisp.c
@@ -16,7 +16,6 @@
 #include "hashing.h"
 #include "table.h"
 #include "iostream.h"
-#include "fsixel.h"
 #include "compress.h"
 
 value_t FL_builtins_table_sym, FL_quote, FL_lambda, FL_function, FL_comma, FL_commaat;
@@ -1383,7 +1382,6 @@
 
 	table_init();
 	iostream_init();
-	fsixel_init();
 	compress_init();
 }
 
--- a/fsixel.h
+++ /dev/null
@@ -1,1 +1,0 @@
-void fsixel_init(void);
--- a/meson.build
+++ b/meson.build
@@ -66,7 +66,6 @@
 	'string.c',
 	'sys_posix.c',
 	'table.c',
-	'terminal_posix.c',
 	'types.c',
 	'utf8.c',
 ]
@@ -89,20 +88,6 @@
 	)
 endif
 
-sixel = get_option('sixel')
-libsixel_dep = []
-if sixel.disabled()
-	src += ['sixel_disabled.c']
-else
-	libsixel = dependency('libsixel', required: sixel)
-	if libsixel.found()
-		src += ['sixel.c']
-		libsixel_dep = [libsixel]
-	else
-		src += ['sixel_disabled.c']
-	endif
-endif
-
 math = cc.find_library('m', required: false)
 
 boot = custom_target(
@@ -209,7 +194,7 @@
 	],
 	dependencies: [
 		math,
-	] + libsixel_dep,
+	],
 	include_directories: include_directories(
 		'3rd',
 		'3rd/brieflz',
--- a/meson.options
+++ b/meson.options
@@ -1,1 +1,0 @@
-option('sixel', type : 'feature', value : 'auto')
--- a/mkfile
+++ b/mkfile
@@ -39,7 +39,6 @@
 	ptrhash.$O\
 	random.$O\
 	read.$O\
-	sixel_disabled.$O\
 	string.$O\
 	sys_plan9.$O\
 	table.$O\
--- a/sixel.c
+++ /dev/null
@@ -1,258 +1,0 @@
-#include <sixel.h>
-#include "flisp.h"
-#include "cvalues.h"
-#include "types.h"
-#include "iostream.h"
-#include "print.h"
-#include "operators.h"
-#include "fsixel.h"
-
-typedef struct fso_t fso_t;
-
-struct fso_t {
-	sixel_output_t *out;
-	sixel_dither_t *dither;
-	ios_t *io;
-	int numcolors;
-	int scalex;
-	int scaley;
-	uint8_t *buf;
-	int bufsz;
-};
-
-value_t FL_fsosym;
-static sixel_allocator_t *salloc;
-
-static int
-issixeloutput(value_t v)
-{
-	return iscvalue(v) && cv_class(ptr(v)) == FL(fsotype);
-}
-
-BUILTIN("sixel-output?", fsixel_outputp)
-{
-	argcount(nargs, 1);
-	return issixeloutput(args[0]) ? FL_t : FL_f;
-}
-
-static int
-fso_write(char *data, int size, void *priv)
-{
-	fso_t *f = priv;
-	return ios_write(f->io, data, size);
-}
-
-// :: num-colors -> ...
-BUILTIN("sixel-output", fsixel_output)
-{
-	int numcolors = 256;
-	if(nargs > 0){
-		argcount(nargs, 1);
-		numcolors = tosize(args[0]);
-	}
-	if(numcolors < 1 || numcolors > 256)
-		lerrorf(FL_ArgError, "invalid number of colors: %d", numcolors);
-	value_t v = cvalue(FL(fsotype), sizeof(fso_t));
-	fso_t *f = value2c(fso_t*, v);
-	if(salloc == nil)
-		sixel_allocator_new(&salloc, malloc, calloc, realloc, free);
-	SIXELSTATUS r = sixel_output_new(&f->out, fso_write, f, salloc);
-	if(SIXEL_FAILED(r))
-		lerrorf(FL_IOError, "could not create sixel output");
-	r = sixel_dither_new(&f->dither, numcolors, salloc);
-	if(SIXEL_FAILED(r))
-		lerrorf(FL_IOError, "could not create sixel dither");
-	sixel_output_set_palette_type(f->out, SIXEL_PALETTETYPE_RGB);
-	sixel_dither_set_pixelformat(f->dither, SIXEL_PIXELFORMAT_PAL8);
-	sixel_dither_set_transparent(f->dither, 0xff);
-	f->numcolors = numcolors;
-	f->scalex = f->scaley = 1;
-	f->buf = nil;
-	f->bufsz = 0;
-	return v;
-}
-
-BUILTIN("sixel-set-scale", fsixel_set_scale)
-{
-	if(nargs < 2 || nargs > 3)
-		argcount(nargs, 2);
-	if(!issixeloutput(args[0]))
-		type_error("sixel-output", args[0]);
-	fso_t *f = value2c(fso_t*, args[0]);
-	int scalex = tosize(args[1]);
-	int scaley = scalex;
-	if(nargs > 2)
-		scaley = tosize(args[2]);
-	if(scalex < 1 || scalex > 32 || scaley < 1 || scaley > 32)
-		lerrorf(FL_ArgError, "invalid scale factor: %dx%d", scalex, scaley);
-	f->scalex = scalex;
-	f->scaley = scaley;
-	return FL_void;
-}
-
-// :: sixel-output -> palette -> [paltype ->] ...
-BUILTIN("sixel-set-palette", fsixel_set_palette)
-{
-	if(nargs < 2 || nargs > 3)
-		argcount(nargs, 2);
-	if(!issixeloutput(args[0]))
-		type_error("sixel-output", args[0]);
-	fso_t *ftype = value2c(fso_t*, args[0]);
-	bool isrgb = true;
-	size_t len;
-	if(nargs >= 3){
-		if(!fl_isstring(args[2]))
-			type_error("string", args[2]);
-		len = cv_len((cvalue_t*)ptr(args[2]));
-		char *s = cvalue_data(args[2]);
-		if(len == 3 && strncmp(s, "rgb", 3) == 0)
-			isrgb = true;
-		else if(len == 3 && strncmp(s, "hls", 3) == 0)
-			isrgb = false;
-		else
-			lerrorf(FL_ArgError, "invalid palette type (must be either \"rgb\" or \"hls\")");
-	}
-
-	if(!isarray(args[1]))
-		type_error("array", args[1]);
-	len = cvalue_arraylen(args[1]);
-	if(ftype->numcolors*3 != (int)len)
-		lerrorf(FL_ArgError, "invalid palette: expected %d colors, got %d", ftype->numcolors, (int)len);
-
-	fltype_t *type = cv_class(ptr(args[1]));
-	size_t elsize = type->elsz;
-	fltype_t *eltype = type->eltype;
-	numerictype_t nt = eltype->numtype;
-
-	uint8_t out[256*3] = {0};
-	if(isrgb){
-		if(eltype->type == FL_uint8sym || eltype->type == FL_bytesym)
-			memcpy(out, cptr(args[1]), ftype->numcolors*3);
-		else
-			lerrorf(FL_ArgError, "invalid palette type: expected bytes");
-	}else{
-		uint8_t *pal = cptr(args[1]);
-		for(int i = 0; i < ftype->numcolors; i++){
-			float s = (float)conv_to_int32(pal+(i*3+2)*elsize, nt) / 100.0;
-			if(s == 0)
-				out[i*3+0] = out[i*3+1] = out[i*3+2] = 255*(int)conv_to_int32(pal+(i*3+1)*elsize, nt)/100;
-			else{
-				float h = (float)conv_to_int32(pal+(i*3+0)*elsize, nt) / 360.0;
-				float l = (float)conv_to_int32(pal+(i*3+1)*elsize, nt) / 100.0;
-				int k = h*6;
-				float f = h*6 - k;
-				float p = l*(1-s);
-				float q = l*(1-f*s);
-				float t = l*(1-(1-f)*s);
-				float r, g, b;
-				switch(k % 6){
-				case 0:  r=l; g=t; b=p; break;
-				case 1:  r=q; g=l; b=p; break;
-				case 2:  r=p; g=l; b=t; break;
-				case 3:  r=p; g=q; b=l; break;
-				case 4:  r=t; g=p; b=l; break;
-				default: r=l; g=p; b=q; break;
-				}
-				out[i*3+0] = 255*r;
-				out[i*3+1] = 255*g;
-				out[i*3+2] = 255*b;
-			}
-		}
-	}
-	sixel_dither_set_palette(ftype->dither, out);
-
-	return FL_void;
-}
-
-// :: sixel-output -> iostream -> pixels -> width -> height -> ...
-BUILTIN("sixel-encode", fsixel_encode)
-{
-	argcount(nargs, 5);
-	if(!issixeloutput(args[0]))
-		type_error("sixel-output", args[0]);
-	fso_t *f = value2c(fso_t*, args[0]);
-	f->io = toiostream(args[1]);
-	uint8_t *pix;
-	size_t sz;
-	to_sized_ptr(args[2], &pix, &sz);
-	size_t w = tosize(args[3]);
-	size_t h = tosize(args[4]);
-	if(w <= 0 || w > sz)
-		bounds_error(args[2], args[3]);
-	if(h <= 0 || w*h > sz)
-		bounds_error(args[2], args[4]);
-	SIXELSTATUS r;
-	if(f->scalex > 1 || f->scaley > 1){
-		int ow = w * f->scalex, oh = h * f->scaley, osz = ow*oh;
-		if(ow < 1 || oh < 1 || osz < ow || osz < oh)
-			lerrorf(FL_ArgError, "scaling out of range");
-		if(f->bufsz < osz){
-			f->buf = MEM_REALLOC(f->buf, osz);
-			f->bufsz = osz;
-		}
-		r = sixel_helper_scale_image(
-			f->buf,
-			pix,
-			w, h,
-			SIXEL_PIXELFORMAT_PAL8,
-			ow, oh,
-			SIXEL_RES_NEAREST,
-			salloc
-		);
-		if(SIXEL_FAILED(r))
-			lerrorf(FL_IOError, "could not scale image");
-		w = ow;
-		h = oh;
-		pix = f->buf;
-	}else if(f->buf != nil){
-		MEM_FREE(f->buf);
-		f->buf = nil;
-		f->bufsz = 0;
-	}
-	r = sixel_encode(pix, w, h, 0, f->dither, f->out);
-	if(SIXEL_FAILED(r))
-		lerrorf(FL_IOError, "could not encode image");
-	return FL_void;
-}
-
-static void
-print_sixeloutput(value_t v, ios_t *s)
-{
-	USED(v);
-	fl_print_str(s, "#<sixel output>");
-}
-
-static void
-relocate_sixeloutput(value_t oldv, value_t newv)
-{
-	fso_t *oldf = cvalue_data(oldv);
-	fso_t *f = cvalue_data(newv);
-	sixel_output_destroy(oldf->out);
-	SIXELSTATUS r = sixel_output_new(&f->out, fso_write, f, salloc);
-	if(SIXEL_FAILED(r))
-		lerrorf(FL_IOError, "could not recreate sixel output");
-	sixel_output_set_palette_type(f->out, SIXEL_PALETTETYPE_RGB);
-}
-
-static void
-free_sixeloutput(value_t self)
-{
-	fso_t *f = value2c(fso_t*, self);
-	sixel_dither_destroy(f->dither);
-	sixel_output_destroy(f->out);
-	MEM_FREE(f->buf);
-}
-
-static cvtable_t fso_vtable = {
-	print_sixeloutput,
-	relocate_sixeloutput,
-	free_sixeloutput,
-	nil
-};
-
-void
-fsixel_init(void)
-{
-	FL_fsosym = symbol("sixel-output", false);
-	FL(fsotype) = define_opaque_type(FL_fsosym, sizeof(fso_t), &fso_vtable, nil);
-}
--- a/sixel_disabled.c
+++ /dev/null
@@ -1,6 +1,0 @@
-#include "fsixel.h"
-
-void
-fsixel_init(void)
-{
-}
--- a/terminal_posix.c
+++ /dev/null
@@ -1,131 +1,0 @@
-#include "flisp.h"
-#include <sys/ioctl.h>
-#include <termios.h>
-
-static bool inraw = false;
-static bool cursorvisible = true;
-static bool atexitset = false;
-static struct termios tios;
-
-static void termreset(void);
-
-static int
-termsetraw(bool raw, bool showcursor)
-{
-	if(!atexitset){
-		atexit(termreset);
-		atexitset = true;
-	}
-	if(showcursor && !cursorvisible){
-		write(STDOUT_FILENO, "\x1b[?25h", 6);
-		cursorvisible = true;
-	}else if(!showcursor && cursorvisible){
-		write(STDOUT_FILENO, "\x1b[?25l", 6);
-		cursorvisible = false;
-	}
-
-	if(raw && !inraw){
-		if(tcgetattr(STDIN_FILENO, &tios) < 0)
-			return -1;
-		struct termios t;
-		memcpy(&t, &tios, sizeof(t));
-		t.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
-		t.c_lflag &= ~(ISIG | ICANON | ECHO | IEXTEN);
-		t.c_oflag &= ~(OPOST);
-		t.c_cc[VMIN] = 0;
-		if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t) < 0)
-			return -1;
-		inraw = true;
-	}else if(!raw && inraw){
-		if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &tios) < 0)
-			return -1;
-		inraw = false;
-	}
-	return 0;
-}
-
-static void
-termreset(void)
-{
-	termsetraw(false, true);
-}
-
-BUILTIN("terminal-enter-raw-mode", terminal_enter_raw_mode)
-{
-	USED(args);
-	argcount(nargs, 0);
-	return termsetraw(true, cursorvisible) == 0 ? FL_t : FL_f;
-}
-
-BUILTIN("terminal-leave-raw-mode", terminal_leave_raw_mode)
-{
-	USED(args);
-	argcount(nargs, 0);
-	return termsetraw(false, cursorvisible) == 0 ? FL_t : FL_f;
-}
-
-BUILTIN("terminal-show-cursor", terminal_show_cursor)
-{
-	USED(args);
-	argcount(nargs, 0);
-	return termsetraw(inraw, true) == 0 ? FL_t : FL_f;
-}
-
-BUILTIN("terminal-hide-cursor", terminal_hide_cursor)
-{
-	USED(args);
-	argcount(nargs, 0);
-	return termsetraw(inraw, false) == 0 ? FL_t : FL_f;
-}
-
-BUILTIN("terminal-get-size", terminal_get_size)
-{
-	USED(args);
-	argcount(nargs, 0);
-	struct winsize ws;
-	if(ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) < 0)
-		return FL_f;
-	value_t v = mk_cons(), tex, pix;
-	car_(v) = tex = mk_cons();
-	car_(tex) = fixnum(ws.ws_col);
-	cdr_(tex) = mk_cons(); car_(cdr_(tex)) = fixnum(ws.ws_row); cdr_(cdr_(tex)) = FL_nil;
-	int x = ws.ws_xpixel, y = ws.ws_ypixel;
-	bool wasraw = inraw;
-	if((x == 0 || y == 0) && isatty(STDOUT_FILENO) && termsetraw(true, cursorvisible) == 0){
-		// FIXME(sigrid): read everything out of stdin first
-		write(STDOUT_FILENO, "\x1b[14t", 5);
-		/* make sure not to hang if nothing is returned */
-		struct timeval t;
-		t.tv_sec = 0;
-		t.tv_usec = 100;
-		fd_set f;
-		FD_ZERO(&f);
-		FD_SET(STDIN_FILENO, &f);
-		int r;
-		while((r = select(STDIN_FILENO+1, &f, nil, nil, &t)) == EINTR);
-		if(r > 0 && FD_ISSET(STDIN_FILENO, &f)){
-			char buf[64];
-			if((r = read(STDIN_FILENO, buf, sizeof(buf)-1)) >= 8){
-				buf[r] = 0;
-				if(buf[0] == 0x1b && buf[1] == '[' && buf[2] == '4' && buf[3] == ';'){ /* CSI 4 */
-					x = atoi(buf+5);
-					char *s = strchr(buf+5, ';');
-					if(s != nil)
-						y = atoi(s+1);
-					while(strchr(buf, 't') == nil){
-						if((r = read(STDIN_FILENO, buf, sizeof(buf)-1)) <= 0)
-							break;
-						buf[r] = 0;
-					}
-				}
-			}
-		}
-		if(!wasraw)
-			termsetraw(false, cursorvisible);
-	}
-	cdr_(v) = pix = mk_cons(); cdr_(pix) = FL_nil;
-	car_(pix) = mk_cons(); pix = car_(pix);
-	car_(pix) = fixnum(x);
-	cdr_(pix) = mk_cons(); car_(cdr_(pix)) = fixnum(y); cdr_(cdr_(pix)) = FL_nil;
-	return v;
-}