Skip to content

Commit

Permalink
Merge pull request #2 from NiLuJe/master
Browse files Browse the repository at this point in the history
"Minor" build tweaks
  • Loading branch information
llandsmeer authored Mar 28, 2020
2 parents 9b7b2a3 + e6a8abe commit c629cc5
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 145 deletions.
2 changes: 1 addition & 1 deletion FBInk
39 changes: 26 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@ GITHASH='"'$(shell git log --format="%H" -n 1)'"'

CROSS_TC?=/home/llandsmeer/Build/gcc-linaro-7.5.0-2019.12-i686_arm-linux-gnueabihf/bin/arm-linux-gnueabihf

CPPFLAGS += -Wall -Ilibvterm-0.1.3/include -DGITHASH=$(GITHASH) -falign-labels=8 -std=gnu++17

ifeq ("$(DEBUG)","true")
CPPFLAGS += -g -pg
CFLAGS += -g -pg
CXXFLAGS += -g -pg
else
CPPFLAGS += -O2
CFLAGS ?= -O2
CXXFLAGS ?= -O2
endif

CPPFLAGS += -Ilibvterm-0.1.3/include -DGITHASH=$(GITHASH)
CFLAGS += -Wall -falign-labels=8
CXXFLAGS += -Wall -falign-labels=8 -std=gnu++17 -fpermissive

ifeq ("$(INPUT_EVDEV)","true")
ifdef INPUT_EVDEV
CPPFLAGS += -DINPUT_EVDEV
endif

ifdef INPUT_SERIAL
CPPFLAGS += -DINPUT_SERIAL
endif

LDFLAGS+=-lm -Lbuild -lutil

all: tracexec linux kobo
Expand All @@ -30,18 +37,18 @@ src/_kbsend.hpp: src/kbsend.html

linux: build/libfbink.a build/libvterm.a src/_kbsend.hpp
python3 keymap.py > src/_keymap.hpp
g++ src/main.cpp -lvterm -lfbink -o build/inkvt.host $(LDFLAGS) $(CPPFLAGS)
g++ $(CPPFLAGS) $(CXXFLAGS) src/main.cpp -lvterm -lfbink -o build/inkvt.host $(LDFLAGS)
ifneq ("$(DEBUG)","true")
strip -s build/inkvt.host
strip --strip-unneeded build/inkvt.host
endif
ifeq ($(INPUT_EVDEV),"true")
g++ src/evdev2serial.cpp -o build/evdev2serial.x86 $(LDFLAGS) $(CPPFLAGS)
g++ $(CPPFLAGS) $(CXXFLAGS) src/evdev2serial.cpp -o build/evdev2serial.x86 $(LDFLAGS)
endif

kobo: build/libfbink_kobo.a build/libvterm_kobo.a src/_kbsend.hpp
kobo: build/fbdepth build/libfbink_kobo.a build/libvterm_kobo.a src/_kbsend.hpp
python3 keymap.py > src/_keymap.hpp
$(CROSS_TC)-g++ -static -DTARGET_KOBO src/main.cpp -lvterm_kobo -lfbink_kobo -o build/inkvt.armhf $(LDFLAGS) $(CPPFLAGS)
$(CROSS_TC)-strip -s build/inkvt.armhf
$(CROSS_TC)-g++ -static -DTARGET_KOBO $(CPPFLAGS) $(CXXFLAGS) src/main.cpp -lvterm_kobo -lfbink_kobo -o build/inkvt.armhf $(LDFLAGS)
$(CROSS_TC)-strip --strip-unneeded build/inkvt.armhf
upx build/inkvt.armhf || echo "install UPX for smaller executables"

build/libvterm.a:
Expand All @@ -55,15 +62,21 @@ build/libvterm_kobo.a:
build/libfbink.a:
mkdir -p build
make -C FBInk clean
env -u CROSS_TC -u CPPFLAGS -u CFLAGS -u LDFLAGS -u AR -u RANLIB make -C FBInk LINUX=true static
env -u CROSS_TC -u CPPFLAGS -u CFLAGS -u CXXFLAGS -u LDFLAGS -u AR -u RANLIB make -C FBInk LINUX=true MINIMAL=true FONTS=true staticlib
cp FBInk/Release/libfbink.a build/libfbink.a

build/libfbink_kobo.a:
mkdir -p build
make -C FBInk clean
make -C FBInk CROSS_TC=$(CROSS_TC) KOBO=true static
make -C FBInk CROSS_TC=$(CROSS_TC) KOBO=true MINIMAL=true FONTS=true staticlib
cp FBInk/Release/libfbink.a build/libfbink_kobo.a

build/fbdepth:
mkdir -p build
make -C FBInk clean
make -C FBInk CROSS_TC=$(CROSS_TC) KOBO=true utils
cp FBInk/Release/fbdepth build/fbdepth

clean:
make -C FBInk clean
make -f Makevterm clean
Expand Down
2 changes: 1 addition & 1 deletion Makevterm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ifdef CROSS_TC
STRIP:=$(CROSS_TC)-strip
AR:=$(CROSS_TC)-gcc-ar
RANLIB:=$(CROSS_TC)-gcc-ranlib
CFLAGS:= -O2
CFLAGS?=-O2
endif

ifdef NATIVE_TC
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ Then update the `CROSS_TC` variable in the Makefile.
```
$ git clone 'https://github.com/llandsmeer/inkvt'
$ cd inkvt
make
make kobo
cp build/inkvt.armhf koboroot/.adds/inkvt/
cp build/fbdepth koboroot/.adds/inkvt/
```

Then copy the contents of the `koboroot/` directory to your kobo device:
Expand All @@ -50,8 +51,8 @@ emulator. The main problem is now input.
The kernel that comes with the device doesn't support usb host mode,
but it (OTG) is supported by the hardware. I use my laptop as host,
which sends keystrokes over serial to the Kobo device via the `g_serial`
loadable kernel module that comes with the device.
This option is enabled by uncommenting `inputs.add_serial()` in `src/main.cpp`.
loadable kernel module that *may* come with the device.
This option is enabled by passing `INPUT_SERIAL=true` to make.
The other option is sending keystrokes over wifi, which doesn't require
loading kernel modules and is more cross-platform (the default).

Expand Down Expand Up @@ -85,14 +86,14 @@ To send keyboard input, there are 3 options:
It requires that you're on the same (wifi) network, so for example, the builtin
phone hotspot or a common wifi router.
Also, android keyinput is extremely buggy...
- (disabled by default) `screen /dev/ttyACM0 9600` (this requires firmware version 7 and Kobo Libra H2O or similar hardware):
- (disabled by default) `screen /dev/ttyACM0 9600` (this requires a Mk. 7+ device, or a self-built g_serial module):

# Alternative input method (evdev & evdev over serial)

Before tty raw input, I build a keyboard input system around evdev.
That's a lot less portable than tty raw input, and will probably be obsoleted somewhere in
the future.
Nevertheless, if you need this `make INPUT_EVDEV=true` should generate 3 binaries:
Nevertheless, if you need this, passing `INPUT_EVDEV=true` to make should generate 3 binaries:

- `build/inkvt.armhf`: inkvt built for kobo.
Sets up serial over USB and listens to `/dev/input/event*` and `/dev/ttyGS0` for evdev
Expand Down
2 changes: 1 addition & 1 deletion keymap.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def esc(s):
#pragma once
#include <deque>
#include <linux/input-event-codes.h>
#include <linux/input.h>
class KeycodeTranslation {''')
for k in keymap['_track']:
Expand Down
Binary file removed koboroot/.adds/inkvt/dropbear
Binary file not shown.
Binary file removed koboroot/.adds/inkvt/fbdepth
Binary file not shown.
Binary file removed koboroot/.adds/inkvt/fbink
Binary file not shown.
110 changes: 7 additions & 103 deletions koboroot/.adds/inkvt/inkvt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export LC_ALL="en_US.UTF-8"
INKVT_DIR="${0%/*}"

cd "${INKVT_DIR}" || exit
export TESSDATA_PREFIX="data"

export FROM_NICKEL="false"
if pkill -0 nickel; then
Expand Down Expand Up @@ -67,120 +66,25 @@ fi
ORIG_FB_ROTA="$(cat /sys/class/graphics/fb0/rotate)"
echo "Original fb rotation is set @ ${ORIG_FB_ROTA}" >>crash.log 2>&1

ORIG_FB_BPP="$(./fbdepth -g)"
ORIG_FB_BPP="$(cat /sys/class/graphics/fb0/bits_per_pixel)"
echo "Original fb bitdepth is set @ ${ORIG_FB_BPP}bpp" >>crash.log 2>&1

case "${ORIG_FB_BPP}" in
8) ;;
16) ;;
32) ;;
*)
unset ORIG_FB_BPP
;;
esac

ko_do_fbdepth() {
if [ -n "${ORIG_FB_BPP}" ]; then
echo "Switching fb bitdepth to 8bpp & rotation to Portrait" >>crash.log 2>&1
./fbdepth -d 8 -r -1 >>crash.log 2>&1
fi
}

if awk '$4~/(^|,)ro($|,)/' /proc/mounts | grep ' /mnt/sd '; then
mount -o remount,rw /mnt/sd
fi

if [ -e crash.log ]; then
tail -c 500000 crash.log >crash.log.new
mv -f crash.log.new crash.log
fi

sh ./enable-wifi.sh

sleep 10

CRASH_COUNT=0
CRASH_TS=0
CRASH_PREV_TS=0
# Because we *want* an initial fbdepth pass ;).
RETURN_VALUE=85
while [ ${RETURN_VALUE} -ne 0 ]; do
# 85 is what we return when asking for a KOReader restart
if [ ${RETURN_VALUE} -eq 85 ]; then
# Do an update check now, so we can actually update KOReader via the "Restart KOReader" menu entry ;).
# Do or double-check the fb depth switch, or restore original bitdepth if requested
ko_do_fbdepth
fi

./inkvt.armhf >> crash.log 2>&1
RETURN_VALUE=$?

if [ ${RETURN_VALUE} -ne 0 ] && [ ${RETURN_VALUE} -ne 85 ]; then
CRASH_COUNT=$((CRASH_COUNT + 1))
CRASH_TS=$(date +'%s')
if [ $((CRASH_TS - CRASH_PREV_TS)) -ge 20 ]; then
CRASH_COUNT=1
fi
echo "Switching fb bitdepth to 8bpp & rotation to Portrait" >>crash.log 2>&1
./fbdepth -d 8 -r -1 >>crash.log 2>&1

if true; then
ALWAYS_ABORT="true"
CRASH_COUNT=1
else
ALWAYS_ABORT="false"
fi
./inkvt.armhf >> crash.log 2>&1
RETURN_VALUE=$?

viewWidth=600
viewHeight=800
FONTH=16
eval "$(./fbink -e | tr ';' '\n' | grep -e viewWidth -e viewHeight -e FONTH | tr '\n' ';')"
bombHeight=$((viewHeight / 2 + viewHeight / 15))
bombMargin=$((FONTH + FONTH / 2))
./fbink -q -b -c -B GRAY9 -m -y 1 "Don't Panic! (Crash n°${CRASH_COUNT} -> ${RETURN_VALUE})"
if [ ${CRASH_COUNT} -eq 1 ]; then
./fbink -q -b -O -m -y 2 "Tap the screen to continue."
fi
./fbink -q -b -O -m -t regular=./fonts/freefont/FreeSerif.ttf,px=${bombHeight},top=${bombMargin} $'\xf0\x9f\x92\xa3'
crashLog="$(tail -n 25 crash.log | sed -e 's/\t/ /g')"
./fbink -q -b -O -t regular=./fonts/droid/DroidSansMono.ttf,top=$((viewHeight / 2 + FONTH * 2 + FONTH / 2)),left=$((viewWidth / 60)),right=$((viewWidth / 60)),px=$((viewHeight / 64)) "${crashLog}"
./fbink -q -f -s
{
echo "!!!!"
echo "Uh oh, something went awry... (Crash n°${CRASH_COUNT}: $(date +'%x @ %X'))"
echo "Running FW $(cut -f3 -d',' /mnt/onboard/.kobo/version) on Linux $(uname -r) ($(uname -v))"
} >>crash.log 2>&1
if [ ${CRASH_COUNT} -lt 5 ] && [ "${ALWAYS_ABORT}" = "false" ]; then
echo "Attempting to restart KOReader . . ." >>crash.log 2>&1
echo "!!!!" >>crash.log 2>&1
fi

if [ ${CRASH_COUNT} -eq 1 ]; then
read -r -t 15 </dev/input/event1
fi
CRASH_PREV_TS=${CRASH_TS}

if [ ${CRASH_COUNT} -ge 5 ]; then
echo "Too many consecutive crashes, aborting . . ." >>crash.log 2>&1
echo "!!!! ! !!!!" >>crash.log 2>&1
break
fi

if [ "${ALWAYS_ABORT}" = "true" ]; then
echo "Aborting . . ." >>crash.log 2>&1
echo "!!!! ! !!!!" >>crash.log 2>&1
break
fi
else
CRASH_COUNT=0
fi
done

if [ -n "${ORIG_FB_BPP}" ]; then
echo "Restoring original fb bitdepth @ ${ORIG_FB_BPP}bpp & rotation @ ${ORIG_FB_ROTA}" >>crash.log 2>&1
./fbdepth -d "${ORIG_FB_BPP}" -r "${ORIG_FB_ROTA}" >>crash.log 2>&1
else
echo "Restoring original fb rotation @ ${ORIG_FB_ROTA}" >>crash.log 2>&1
./fbdepth -r "${ORIG_FB_ROTA}" >>crash.log 2>&1
fi
echo "Restoring original fb bitdepth @ ${ORIG_FB_BPP}bpp & rotation @ ${ORIG_FB_ROTA}" >>crash.log 2>&1
./fbdepth -d "${ORIG_FB_BPP}" -r "${ORIG_FB_ROTA}" >>crash.log 2>&1

if [ "${FROM_NICKEL}" = "true" ]; then
if [ "${FROM_KFMON}" != "true" ]; then
Expand Down
2 changes: 1 addition & 1 deletion koboroot/.adds/inkvt/nickel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/lib:"
export LD_LIBRARY_PATH="/usr/local/Kobo"

cd /
unset OLDPWD EXT_FONT_DIR TESSDATA_PREFIX FROM_NICKEL STARDICT_DATA_DIR LC_ALL KO_NO_CBB
unset OLDPWD FROM_NICKEL LC_ALL

(
if [ "${PLATFORM}" = "freescale" ] || [ "${PLATFORM}" = "mx50-ntx" ] || [ "${PLATFORM}" = "mx6sl-ntx" ]; then
Expand Down
2 changes: 1 addition & 1 deletion src/_keymap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#pragma once
#include <deque>
#include <linux/input-event-codes.h>
#include <linux/input.h>

class KeycodeTranslation {
int _is_leftmeta_down;
Expand Down
60 changes: 49 additions & 11 deletions src/input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,27 +239,65 @@ class Inputs {

void add_serial() {
#ifdef TARGET_KOBO
// NOTE: Obviously highly platform-specific ;).
// See http://trac.ak-team.com/trac/browser/niluje/Configs/trunk/Kindle/Kobo_Hacks/KoboStuff/src/usr/local/stuff/bin/usbnet-toggle.sh for a slightly more portable example.
// NOTE: Extra fun fact: I don't know when Kobo started shipping g_serial, but they didn't on Mk.5, so, here's one I just built to test on my H2O:
// NOTE: Do our best to make this mostly portable...
// See http://trac.ak-team.com/trac/browser/niluje/Configs/trunk/Kindle/Kobo_Hacks/KoboStuff/src/usr/local/stuff/bin/usbnet-toggle.sh for a similar example.
// NOTE: Fun fact: I don't know when Kobo started shipping g_serial, but they didn't on Mk.5, so, here's one I just built to test on my H2O:
// http://files.ak-team.com/niluje/mrpub/Other/USBSerial-Kobo-Mk5-H2O.tar.gz
system("insmod /drivers/mx6sll-ntx/usb/gadget/configfs.ko");
system("insmod /drivers/mx6sll-ntx/usb/gadget/libcomposite.ko");
system("insmod /drivers/mx6sll-ntx/usb/gadget/u_serial.ko");
system("insmod /drivers/mx6sll-ntx/usb/gadget/usb_f_acm.ko");
system("insmod /drivers/mx6sll-ntx/usb/gadget/g_serial.ko");
const char *platform = getenv("PLATFORM");

// Abort if we can't tell the platform from the env.
// NOTE: We could also compute that from FBInk's state via device_platform, although that's not a 1:1 mapping...
if (platform == NULL) {
puts("add_serial() is only supported on Kobo devices with a proper PLATFORM set in the env!");
return;
}

char module_path[PATH_MAX] = { 0 };

snprintf(module_path, sizeof(module_path), "/drivers/%s/usb/gadget/g_serial.ko", platform);
if (access(module_path, F_OK) != 0) {
puts("add_serial() is only supported on Kobo devices with a g_serial kernel module!");
return;
}

// Cheap Mk. 7+ detection...
snprintf(module_path, sizeof(module_path), "/drivers/%s/usb/gadget/configfs.ko", platform);
if (access(module_path, F_OK) == 0) {
// Mk. 7+
snprintf(module_path, sizeof(module_path), "insmod /drivers/%s/usb/gadget/configfs.ko", platform);
system(module_path);
snprintf(module_path, sizeof(module_path), "insmod /drivers/%s/usb/gadget/libcomposite.ko", platform);
system(module_path);
snprintf(module_path, sizeof(module_path), "insmod /drivers/%s/usb/gadget/u_serial.ko", platform);
system(module_path);
snprintf(module_path, sizeof(module_path), "insmod /drivers/%s/usb/gadget/usb_f_acm.ko", platform);
system(module_path);
snprintf(module_path, sizeof(module_path), "insmod /drivers/%s/usb/gadget/g_serial.ko", platform);
system(module_path);
} else {
// Older devices
snprintf(module_path, sizeof(module_path), "insmod /drivers/%s/usb/gadget/arcotg_udc.ko", platform);
system(module_path);
snprintf(module_path, sizeof(module_path), "insmod /drivers/%s/usb/gadget/g_serial.ko", platform);
system(module_path);
}

// Sleep a bit to leave the kernel time to breathe, because everything is terrible
const struct timespec zzz = { 0L, 500000000L };
nanosleep(&zzz, NULL);

int fd = open("/dev/ttyGS0", O_RDONLY | O_NONBLOCK);
if (fd != -1) {
fdtype[nfds] = FD_SERIAL;
fds[nfds].events = POLLIN;
fds[nfds++].fd = fd;
printf("opening /dev/ttyGS0");
puts("opening /dev/ttyGS0");
setup_serial(fd);
} else {
printf("couldn't open /dev/ttyGS0\n");
printf("couldn't open /dev/ttyGS0: %m\n");
}
#else
puts("add_serial() is only supported on kobo devices");
puts("add_serial() is only supported on Kobo devices");
#endif
}

Expand Down
Loading

0 comments on commit c629cc5

Please sign in to comment.