forked from taviso/wpunix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
patch.c
95 lines (80 loc) · 2.58 KB
/
patch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#define _GNU_SOURCE
#include <sys/ioctl.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include "wptypes.h"
#include "wpfuncs.h"
#include "wpvars.h"
#include "hook.h"
// WordPerfect expects 4 decimal characters on mouse press, XXYY. I don't know
// what terminal generates this sequence, certainly no mouse tracking
// mode that XTerm supports looks like that.
//
// https://www.xfree86.org/current/ctlseqs.html#Mouse%20Tracking
//
// The closest is DEC Locator mode, which sends CSI M C b C x C y, but those are
// not decimal coordinates -- they're binary. It's also 3 bytes of parameters, not
// 4. The solution is to patch the callback table and decode the parameters
// ourselves.
static void xy_xterm_mouse()
{
// The button is currently ignored, but we could examine it.
uint16_t button = msqot() - 0x20;
*mouse_x = msqot() - 0x21;
*mouse_y = msqot() - 0x21;
}
// The wp implementation of dodelay() busy waits, which means macros waiting
// for responses or using {WAIT} keep a whole core busy. Let's use sleep
// instead.
static void delay_millis(int delay)
{
struct timespec req = {
.tv_sec = delay / 100,
.tv_nsec = (delay % 100) * 10000000,
};
do {
if (nanosleep(&req, &req) == 0)
return;
} while (errno == EINTR);
}
// Fake a resize event by setting up a SIGWINCH handler.
static void win_resize_event(int signum)
{
struct winsize w = {0};
// Fetch the new size.
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != 0) {
return;
}
// Make sure this looks sane.
if (w.ws_col < 24)
w.ws_col = 24;
if (w.ws_row < 80)
w.ws_row = 80;
// Okay, send this to wordperfect.
win_resize(w.ws_col, w.ws_row);
// Rewrite screen.
frewrt();
}
static struct sigaction winchact = {
.sa_handler = win_resize_event,
};
uint16_t zero_ext_mapkey(uint16_t code);
void _init()
{
// This code is called on startup, we can hook or replace wp internals.
// Patch the mouse event callback to use an XTerm compatible version.
ms_tbl[0].nbytes = 3;
ms_tbl[0].mscallback = xy_xterm_mouse;
// Insert hooks and redirects.
insert_function_redirect(dodelay, delay_millis, HOOK_REPLACE_FUNCTION);
insert_function_redirect(mapkey, zero_ext_mapkey, HOOK_REPLACE_FUNCTION);
// WordPerfect expects the terminal to send an escape sequence on resize.
// This isn't how things work on Linux, but we can fake it by setting up a
// SIGWINCH handler.
sigaction(SIGWINCH, &winchact, NULL);
}