aboutsummaryrefslogtreecommitdiff
path: root/moustest.c
blob: 97e554bc33ad63d5b16d5fbe424f18721f4b28b0 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

#include <dos.h>
#include <conio.h>
#include <graph.h>

#include "int33.h"
#include "utils.h"
#include "dlog.h"

#define DPREFIX "moustest: "

bool exiting = false;

uint16_t driver_caps;
uint16_t num_buttons;
uint16_t num_wheels;

struct videoconfig vidconf;

short ctexttop, ctextbot, ctextleft, ctextright;
struct rccoord ctextpos;
const short ctextcolor = 7;
const long ctextbg = _BLACK;

void gui_printf(const char *format, ...)
{
    va_list arglist;
    char buffer[120];

    va_start(arglist, format);
    vsprintf(buffer, format, arglist);
    va_end(arglist);

    _outtext(buffer);
}

void gui_draw_textwindow(int top, int left, int bottom, int right, grcolor fg, long bg)
{
    _settextwindow(top, left, bottom, right);
    _setbkcolor(bg);
    _settextcolor(fg);
    _clearscreen(_GWINDOW);
}

void gui_draw_title()
{
    const int cols = vidconf.numtextcols;

    gui_draw_textwindow(1, 1, 1, cols, 0, _WHITE);
    _outtext("MOUSTEST   ESC to exit, 'r' reset, 's' show, 'h' hide");
}

void gui_draw_status()
{
    const int statusrow = vidconf.numtextrows;
    const int cols = vidconf.numtextcols;

    gui_draw_textwindow(statusrow, 1, statusrow, cols, 0, _WHITE);
}

void gui_draw_console()
{
    const int rows = vidconf.numtextrows, cols = vidconf.numtextcols;
    ctexttop = 3;
    ctextbot = rows - 2;
    ctextleft = 1;
    ctextright = cols;
    gui_draw_textwindow(ctexttop, ctextleft, ctextbot, ctextright,
                        ctextcolor, ctextbg);
}

void gui_init()
{
    _setbkcolor(_BLACK);
    _clearscreen(_GCLEARSCREEN);

    gui_draw_title();

    gui_draw_status();

    gui_draw_console();
}

void console_enter()
{
    _settextwindow(ctexttop, ctextleft, ctextbot, ctextright);
    _settextposition(ctextpos.row, ctextpos.col);
    _setbkcolor(ctextbg);
    _settextcolor(ctextcolor);
}

void console_leave()
{
    ctextpos = _gettextposition();
}

void console_print(char __far *str)
{
    _disable();
    int33_hide_cursor();
    console_enter();
    _outtext(str);
    console_leave();
    int33_show_cursor();
    _enable();
}

void console_printf(const char *format, ...)
{
    va_list arglist;
    char buffer[120];

    va_start(arglist, format);
    vsprintf(buffer, format, arglist);
    va_end(arglist);

    console_print(buffer);
}

/** Called by the int33 mouse driver. */
void __far mouse_callback(uint16_t events, uint16_t buttons, int16_t x, int16_t y, int16_t delta_x, int16_t delta_y)
#pragma aux (INT33_CB) mouse_callback
{
	int8_t z = buttons >> 8;

#if 1
	dprintf(DPREFIX "mouse_callback events=0x%x buttons=0x%x x=%d y=%d dx=%d dy=%d\n",
	        events, buttons, x, y, delta_x, delta_y);
#endif

	// Update the status bar
	_disable();
    gui_draw_status();

    gui_printf(" %3s %4u , %4u |",
               events & INT33_EVENT_MASK_ABSOLUTE ? "ABS" : "REL", x, y);

    if (num_buttons <= 2) {
        gui_printf(" [%5s] [%5s] ",
                   buttons & INT33_BUTTON_MASK_LEFT ? "LEFT" : "",
                   buttons & INT33_BUTTON_MASK_RIGHT ? "RIGHT" : "");
    } else {
        gui_printf(" [%5s] [%5s] [%5s] ",
                   buttons & INT33_BUTTON_MASK_LEFT ? "LEFT" : "",
                   buttons & INT33_BUTTON_MASK_CENTER ? "MID " : "",
                   buttons & INT33_BUTTON_MASK_RIGHT ? "RIGHT" : "");
        if (num_buttons > 3) {
            gui_printf(" [%3s] [%3s] ",
                       buttons & INT33_BUTTON_MASK_4TH ? "4TH" : "",
                       buttons & INT33_BUTTON_MASK_5TH ? "5TH" : "");
        }
    }

    if (num_wheels > 0) {
        char c = ' ';
        if (events & INT33_EVENT_MASK_WHEEL_MOVEMENT) {
            c = z > 0 ? 0x19 : 0x18;
        } else if (events & INT33_EVENT_MASK_HORIZ_WHEEL_MOVEMENT) {
            c = z > 0 ? 0x1A : 0x1B;
        }
        gui_printf("| %c", c);
    }

    _settextposition(1, 1);
    _enable();

    // Log the events
	if (events & INT33_EVENT_MASK_LEFT_BUTTON_PRESSED) {
		console_printf("Left button pressed\n");
	}
	if (events & INT33_EVENT_MASK_LEFT_BUTTON_RELEASED) {
		console_printf("Left button released\n");
	}
	if (events & INT33_EVENT_MASK_CENTER_BUTTON_PRESSED) {
		console_printf("Middle button pressed\n");
	}
	if (events & INT33_EVENT_MASK_CENTER_BUTTON_RELEASED) {
		console_printf("Middle button released\n");
	}
	if (events & INT33_EVENT_MASK_RIGHT_BUTTON_PRESSED) {
		console_printf("Right button pressed\n");
	}
	if (events & INT33_EVENT_MASK_RIGHT_BUTTON_RELEASED) {
		console_printf("Right button released\n");
	}
	if (events & INT33_EVENT_MASK_4TH_BUTTON_PRESSED) {
		console_printf("4th button pressed\n");
	}
	if (events & INT33_EVENT_MASK_4TH_BUTTON_RELEASED) {
		console_printf("4th button released\n");
	}
	if (events & INT33_EVENT_MASK_5TH_BUTTON_PRESSED) {
		console_printf("5th button pressed\n");
	}
	if (events & INT33_EVENT_MASK_5TH_BUTTON_RELEASED) {
		console_printf("5th button released\n");
	}

	if (events & INT33_EVENT_MASK_WHEEL_MOVEMENT) {
		console_printf("Wheel %s %d\n", z > 0 ? "down" : "up", z);
	}
	if (events & INT33_EVENT_MASK_HORIZ_WHEEL_MOVEMENT) {
		console_printf("Wheel %s %d\n", z > 0 ? "right" : "left", z);
	}
}

bool mouse_reset()
{
    if (!int33_reset_get_buttons(&num_buttons)) {
        puts("Mouse not installed");
        return false;
    }

    driver_caps = int33_get_capabilities();
    num_wheels = 0;
    if (driver_caps & INT33_CAPABILITY_WHEEL_API) {
        num_wheels = 1;
    }
    if (driver_caps & INT33_CAPABILITY_WHEEL2_API) {
        num_wheels = 2;
    }

    int33_set_event_handler(INT33_EVENT_MASK_ALL, mouse_callback);

    return true;
}

void mouse_report()
{
    const char __far *fstr;

    if (driver_caps != 0) {
        console_printf("Driver capabilities bits: 0x%x %s %s\n", driver_caps,
                       driver_caps & INT33_CAPABILITY_WHEEL_API ? "<wheel API>" : "",
                       driver_caps & INT33_CAPABILITY_WHEEL2_API ? "<wheel API v2>" : "");
    }

    fstr = int33_get_version_string();
    if (fstr) {
        console_printf("Driver version string: %Fs\n", fstr);
    }

    console_printf("Number of buttons: %u\n", num_buttons);
    console_printf("Number of wheels: %u\n", num_wheels);

}

void mouse_quit()
{
    int33_reset();
}

void mouse_debug()
{
    // TODO ?
    union REGS in, out;
    in.x.ax = INT33_GET_MOUSE_POSITION;
    in.x.bx = 0x4567;
    in.x.cx = 0x7845;
    in.x.dx = 0x9AAC;
    in.x.si = 0xAAAA;
    in.x.di = 0xBBBB;

    int86(0x33, &in, &out);

    console_printf("IN  ax 0x%04x bx 0x%04x cx 0x%04x dx 0x%04x si 0x%04x di 0x%04x\n",
                   in.x.ax, in.x.bx, in.x.cx, in.x.dx, in.x.si, in.x.di);
    console_printf("OUT ax 0x%04x bx 0x%04x cx 0x%04x dx 0x%04x si 0x%04x di 0x%04x\n",
                   out.x.ax, out.x.bx, out.x.cx, out.x.dx, out.x.si, out.x.di);
}

int main(int argc, const char *argv[])
{
    int c;

    (void)argc;
    (void)argv;

    if (!mouse_reset()) {
        return EXIT_FAILURE;
    }

    _setvideomode(_TEXTC80);
    _getvideoconfig(&vidconf);

    gui_init();

    mouse_report();

    int33_show_cursor();

    while (!exiting) {
        int c;

        c = getch();

        dprintf(DPREFIX "getch returns %d\n", c);

        if (c == 0) {
            // Extended key
            c = getch();
            console_printf("Keyboard extended key %d\n", c);
        } else {
            console_printf("Keyboard key %d '%c'\n", c, c);
            switch (c) {
            case 27: // Escape
                exiting = true;
                break;
            case 'r':
                console_printf("Reset mouse driver\n");
                mouse_reset();
                mouse_report();
                break;
            case 's':
                int33_show_cursor();
                break;
            case 'h':
                int33_hide_cursor();
                break;
            case 'd':
                mouse_debug();
                break;
            }
        }
    }

    mouse_quit();
    _setvideomode(_DEFAULTMODE);

    return EXIT_SUCCESS;
}