Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Import Suckless Terminal #1341

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .format-exclude
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ bin/tetris/
bin/echo/
bin/ln/
bin/chmod/
bin/st/
lib/libutil/
lib/libterminfo/
lib/libc/
Expand Down
2 changes: 1 addition & 1 deletion bin/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
TOPDIR = $(realpath ..)

SUBDIR = ksh mandelbrot ps \
sandbox setwinsize stty test_rtc tetris utest
sandbox setwinsize st stty test_rtc tetris utest

all: build

Expand Down
34 changes: 34 additions & 0 deletions bin/st/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
MIT/X Consortium License

© 2014-2020 Hiltjo Posthuma <hiltjo at codemadness dot org>
© 2018 Devin J. Pohly <djpohly at gmail dot com>
© 2014-2017 Quentin Rameau <quinq at fifth dot space>
© 2009-2012 Aurélien APTEL <aurelien dot aptel at gmail dot com>
© 2008-2017 Anselm R Garbe <garbeam at gmail dot com>
© 2012-2017 Roberto E. Vargas Caballero <k0ga at shike2 dot com>
© 2012-2016 Christoph Lohmann <20h at r-36 dot net>
© 2013 Eon S. Jeon <esjeon at hyunmu dot am>
© 2013 Alexander Sedov <alex0player at gmail dot com>
© 2013 Mark Edgar <medgar123 at gmail dot com>
© 2013-2014 Eric Pruitt <eric.pruitt at gmail dot com>
© 2013 Michael Forney <mforney at mforney dot org>
© 2013-2014 Markus Teich <markus dot teich at stusta dot mhn dot de>
© 2014-2015 Laslo Hunhold <dev at frign dot de>

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
13 changes: 13 additions & 0 deletions bin/st/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
TOPDIR = $(realpath ../..)

PROGRAM = st
SOURCES = win.c font.c input.c st.c

LDLIBS = -lutil

# TODO: Why those are needed with gcc on MIPS ?
WFLAGS = -Wno-error=strict-prototypes -Wno-error=unused-variable \
-Wno-error=type-limits -Wno-error=implicit-fallthrough \
-Wno-error=maybe-uninitialized

include $(TOPDIR)/build/build.prog.mk
140 changes: 140 additions & 0 deletions bin/st/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/* See LICENSE file for copyright and license details. */

static const char *fb_dev = "/dev/fb0";
static const char *ev_dev = "/dev/input/event0";
static const char *font_file = "/etc/spleen-8x16.fnt";

/*
* appearance
*
* font: see http://freedesktop.org/software/fontconfig/fontconfig-user.html
*/
static char *font = "Liberation Mono:pixelsize=12:antialias=true:autohint=true";
static int borderpx = 2;

/*
* What program is execed by st depends of these precedence rules:
* 1: program passed with -e
* 2: scroll and/or utmp
* 3: SHELL environment variable
* 4: value of shell in /etc/passwd
* 5: value of shell in config.h
*/
static char *shell = "/bin/sh";
char *utmp = NULL;
/* scroll program: to enable use a string like "scroll" */
char *scroll = NULL;
char *stty_args = "stty raw pass8 nl -echo -iexten -cstopb 38400";

/* identification sequence returned in DA and DECID */
char *vtiden = "\033[?6c";

/* Kerning / character bounding-box multipliers */
static float cwscale = 1.0;
static float chscale = 1.0;

/*
* word delimiter string
*
* More advanced example: L" `'\"()[]{}"
*/
wchar_t *worddelimiters = L" ";

/* selection timeouts (in milliseconds) */
static unsigned int doubleclicktimeout = 300;
static unsigned int tripleclicktimeout = 600;

/* alt screens */
int allowaltscreen = 1;

/* allow certain non-interactive (insecure) window operations such as:
setting the clipboard text */
int allowwindowops = 0;

/*
* draw latency range in ms - from new content/keypress/etc until drawing.
* within this range, st draws when content stops arriving (idle). mostly it's
* near minlatency, but it waits longer for slow updates to avoid partial draw.
* low minlatency will tear/flicker more, as it can "detect" idle too early.
*/
static double minlatency = 8;
static double maxlatency = 33;

/*
* blinking timeout (set to 0 to disable blinking) for the terminal blinking
* attribute.
*/
static unsigned int blinktimeout = 800;

/*
* thickness of underline and bar cursors
*/
static unsigned int cursorthickness = 2;

/* default TERM value */
char *termname = "st-256color";

/*
* spaces per tab
*
* When you are changing this value, don't forget to adapt the »it« value in
* the st.info and appropriately install the st.info in the environment where
* you use this st version.
*
* it#$tabspaces,
*
* Secondly make sure your kernel is not expanding tabs. When running `stty
* -a` »tab0« should appear. You can tell the terminal to not expand tabs by
* running following command:
*
* stty tabs
*/
unsigned int tabspaces = 8;

static const unsigned colors[] = {
[0x0] = 0x000000,
[0x1] = 0xaa0000,
[0x2] = 0x00aa00,
[0x3] = 0xaa5500,
[0x4] = 0x0000aa,
[0x5] = 0xaa00aa,
[0x6] = 0x00aaaa,
[0x7] = 0xaaaaaa,
[0x8] = 0x555555,
[0x9] = 0xff5555,
[0xA] = 0x55ff55,
[0xB] = 0xffff55,
[0xC] = 0x5555ff,
[0xD] = 0xff55ff,
[0xE] = 0x55ffff,
[0xF] = 0xffffff
};

/*
* Default colors (colorname index)
* foreground, background, cursor, reverse cursor
*/
unsigned int defaultfg = 268;
unsigned int defaultbg = 259;
unsigned int defaultcs = 256;
static unsigned int defaultrcs = 257;

/*
* Default shape of cursor
* 2: Block ("█")
* 4: Underline ("_")
* 6: Bar ("|")
* 7: Snowman ("☃")
*/
static unsigned int cursorshape = 2;


/*
* Default colour and shape of the mouse cursor
*/

/*
* Color used to display font attributes when fontconfig selected a font which
* doesn't match the ones requested.
*/
static unsigned int defaultattr = 11;
86 changes: 86 additions & 0 deletions bin/st/font.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include "font.h"

#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include <endian.h>

#define DEFWIDTH 8
#define DEFHEIGHT 16

font_t *font_load(const char *path) {
int ffd, res;
int len;
struct stat st;

if ((ffd = open(path, O_RDONLY, 0)) < 0)
return NULL;

font_t *font = malloc(sizeof(font_t));

font->fontwidth = DEFWIDTH;
font->fontheight = DEFHEIGHT;
font->firstchar = 0;
font->numchars = 256;
font->stride = (DEFWIDTH + 7) / 8;
font->name = strdup(path);

len = font->numchars * font->fontheight * font->stride;
if (fstat(ffd, &st) == 0) {
if (len != st.st_size) {
uint32_t foo = 0;
char b[65];
len = st.st_size;
/* read header */
res = read(ffd, b, 4);
if (strncmp(b, "WSFT", 4) != 0)
goto err;

res = read(ffd, b, 64);
b[64] = 0;
free(font->name);
font->name = strdup(b);

res = read(ffd, &foo, 4);
font->firstchar = le32toh(foo);
res = read(ffd, &foo, 4);
font->numchars = le32toh(foo);
res = read(ffd, &foo, 4);
font->encoding = le32toh(foo);
res = read(ffd, &foo, 4);
font->fontwidth = le32toh(foo);
res = read(ffd, &foo, 4);
font->fontheight = le32toh(foo);
res = read(ffd, &foo, 4);
font->stride = le32toh(foo);
res = read(ffd, &foo, 4);
font->bitorder = le32toh(foo);
res = read(ffd, &foo, 4);
font->byteorder = le32toh(foo);
len = font->numchars * font->fontheight * font->stride;
}
}

font->data = malloc(len);
res = read(ffd, font->data, len);
if (res != len) {
free(font->data);
goto err;
}

close(ffd);
return font;

err:
free(font);
close(ffd);
return NULL;
}

char *font_glyph(font_t *font, int c) {
return (char *)font->data +
(c - font->firstchar) * font->fontheight * font->stride;
}
15 changes: 15 additions & 0 deletions bin/st/font.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef FONT_H
#define FONT_H

typedef struct {
char *name;
int firstchar, numchars;
int fontwidth, fontheight, stride;
int encoding, bitorder, byteorder;
void *data;
} font_t;

font_t *font_load(const char *path);
char *font_glyph(font_t *font, int c);

#endif
49 changes: 49 additions & 0 deletions bin/st/input.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "input.h"

#include <sys/types.h>
#include <sys/input.h>
#include <wchar.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

#include "kbio.h"
#include "kdbtable.h"

#include "st.h"

struct {
int fd;
} input;

static void handle_event(struct input_event *ie) {
char buf[64];

if (ie->type != EV_KEY)
return;

return;
if (ie->value != 0) {
buf[0] = key_map.key[ie->code].map[0];
buf[1] = 0;
ttywrite(buf, 1, 1);
}
}

int input_init(const char *ev_dev) {
if ((input.fd = open(ev_dev, O_RDONLY)) < 0)
return 0;
return 1;
}

void input_read(void) {
struct input_event ie;
int ret;

if ((ret = read(input.fd, &ie, sizeof(struct input_event))) > 0)
handle_event(&ie);
}

int input_getfd(void) {
return input.fd;
}
8 changes: 8 additions & 0 deletions bin/st/input.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef INPUT_H
#define INPUT_H

int input_init(const char *ev_dev);
void input_read(void);
int input_getfd(void);

#endif
Loading
Loading