summaryrefslogtreecommitdiff
path: root/src/surface/kolibri.c
blob: f39fc44af88db7a716571432140f8051ddc1f2bf (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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
 * Copyright 2016 Nina Kalinina <ninacarrot@ya.ru>
 * Copyright 2016 Ashish Gupta <ashmew2@gmail.com>
 *
 * This file is a part of libnsfb, http://www.netsurf-browser.org/
 * Licenced under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 */

#include <stdbool.h>
#include <stdlib.h>

#include "libnsfb.h"
#include "libnsfb_event.h"
#include "libnsfb_plot.h"
#include "libnsfb_plot_util.h"

#include "nsfb.h"
#include "surface.h"
#include "palette.h"
#include "plot.h"
#include "cursor.h"

/* Define codes for KolibriOS' events  */
#define EVENT_REDRAW              (1 << 0)
#define EVENT_KEY                 (1 << 1)
#define EVENT_BUTTON              (1 << 2)
#define EVENT_END_REQUEST         (1 << 3)
#define EVENT_DESKTOP_BACK_DRAW   (1 << 4)
#define EVENT_MOUSE_CHANGE        (1 << 5)
#define EVENT_IPC                 (1 << 6)
#define EVENT_NETWORK             (1 << 7)
#define EVENT_INACTIVE_NO_CURSOR  (1 << 30)
#define EVENT_INACTIVE_NO_MOUSE   (1 << 31)

/* Pixel array which we need to pass around. */
unsigned char * pixels;
unsigned previous_mouse_position, previous_mouse_buttons;

int kolibri_get_button_id(void)
{
	uint16_t __ret;
	__asm__ __volatile__ (
		"int $0x40"
		:"=a"(__ret)
		:"0"(17));

	if ((__ret & 0xFF) == 0) {
		return (__ret >> 8) & 0xFF;
	}
	else {
		return -1;
	}
}

int kolibri_wait_for_event(void)
{
	uint32_t __ret;
	__asm__ __volatile__ ("int $0x40":"=a"(__ret):"0"(10));
	return __ret;
}

int kolibri_get_pressed_key(void)
{
	uint16_t __ret;
	__asm__ __volatile__ ("int $0x40":"=a"(__ret):"0"(2));

	if (!(__ret & 0xFF)) {
		return (__ret >> 8) & 0xFF;
	}
	else {
		return 0;
	}
}

void kolibri_define_window(uint16_t x1, uint16_t y1, uint16_t xsize,
		uint16_t ysize, uint32_t body_color, uint32_t grab_color,
		uint32_t frame_color)
{
	uint32_t a, b;
	a = (x1 << 16) | xsize;
	b = (y1 << 16) | ysize;

	__asm__ __volatile__ ("int $0x40"
		::"a"(0),
		"b"(a),
		"c"(b),
		"d"(body_color),
		"S"(grab_color),
		"D"(frame_color));
}

void kolibri_window_redraw(int status)
{
	__asm__ __volatile__ ("int $0x40"::"a"(12),"b"(status));
}

void kolibri_set_wanted_events(uint32_t ev)
{
	__asm__ __volatile__ ("int $0x40"::"a"(40),"b"(ev));
}

inline void f65_32bpp(unsigned x, unsigned y, unsigned w, unsigned h, char *d)
{
	__asm__ __volatile__ ("pusha");
	__asm__ __volatile__ ("nop"::"D"(0), "c"(w*65536 + h), "d"(x*65536 + y),
		"b"(d));
	__asm__ __volatile__ ("xor %eax, %eax");
	__asm__ __volatile__ ("movl %eax, %ebp");
	__asm__ __volatile__ ("pushl $32");
	__asm__ __volatile__ ("popl %esi");
	__asm__ __volatile__ ("int $0x40"::"a"(65));
	__asm__ __volatile__ ("popa");
}

unsigned kolibri_mouse_get_relative(void)
{
	unsigned error;
	__asm__ __volatile__ ("int $0x40":"=a"(error):"a"(37), "b"(1));
	return error;
}


unsigned kolibri_mouse_get_buttonpress(void)
{
	unsigned error;
	__asm__ __volatile__ ("int $0x40":"=a"(error):"a"(37), "b"(2));
	return error;
}

unsigned kolibri_mouse_get_scrolldata(void)
{
	unsigned error;
	__asm__ __volatile__ ("int $0x40":"=a"(error):"a"(37), "b"(7));
	return error;
}

/* timeout is in 1/100 seconds */
unsigned kolibri_wait_for_event_with_timeout(int timeout)
{
	unsigned event;
	__asm__ __volatile__ ("int $0x40":"=a"(event):"a"(23), "b"(timeout));
	return event;
}

unsigned kolibri_scancodes(void)
{
	unsigned error;
	__asm__ __volatile__ ("int $0x40":"=a"(error):"a"(66), "b"(1), "c"(1));
	return error;
}

void kolibri_redraw(nsfb_t *nsfb)
{
	f65_32bpp(0, 0, nsfb->width, nsfb->height, pixels);
}

unsigned kolibri_skin_get_height(void)
{
	unsigned error;
	__asm__ __volatile__ ("int $0x40":"=a"(error):"a"(48), "b"(4));
	return error;
}

unsigned kolibri_area(char *data)
{
	unsigned error;
	__asm__ __volatile__ ("int $0x40":"=a"(error):"a"(9), "b"(data),
		"c"(0xffffffff));
	return error;
}


void kolibri_fb_redraw(nsfb_t *nsfb)
{
	kolibri_window_redraw(1);
	kolibri_define_window(100, 100, nsfb->width + 9,
		nsfb->height + kolibri_skin_get_height(), 0x74000080,
		0x800000FF, "Netsurf for KolibriOS");

	debug_board_write_str("f65 is mighty with 32 bpp!\n");

	/* Here put image pixels! it's 32bpp */
	f65_32bpp(0, 0, nsfb->width, nsfb->height, pixels);
	kolibri_window_redraw(2);
}

static int kolibri_surface_set_geometry(nsfb_t *nsfb, int width, int height,
		enum nsfb_format_e format)
{

	/* fail if surface already initialised */
	if (nsfb->surface_priv != NULL)
		return -1;

	nsfb->width = width;
	nsfb->height = height;
	nsfb->format = format;

	/* We add one more byte to balance XBGR to BGRX for KolibriOS. */
	/* *4 because we only support 32bpp */

	pixels = (char *)malloc(width * height * 4 + 1);

	/* select default sw plotters for format */
	/* Fail if plotter selection fails */
	if (select_plotters(nsfb) == false) {
		return -1;
	}

	return 0;
}

static int kolibri_surface_initialise(nsfb_t *nsfb)
{
	enum nsfb_format_e fmt;

	kolibri_scancodes();

	previous_mouse_position = 0;
	previous_mouse_buttons = 0;

	debug_board_write_str("Kolibri Initialise in libnsfb.\n");

	if (nsfb->surface_priv != NULL) {
		debug_board_write_str("Surface already "
			"has private surface\n. Abort\n");

		return -1;
	}

	nsfb->surface_priv = pixels;
	nsfb->ptr = pixels;
	nsfb->linelen = (nsfb->width * nsfb->bpp) / 8;

	debug_board_write_str("Redraw\n");
	kolibri_redraw(nsfb);

	/* This is for setting flags for MCALL 40 for events read by a window */
	kolibri_set_wanted_events(EVENT_REDRAW |
		EVENT_KEY |
		EVENT_BUTTON |
		EVENT_MOUSE_CHANGE |
		EVENT_INACTIVE_NO_CURSOR |
		EVENT_INACTIVE_NO_MOUSE |
		EVENT_NETWORK);

	return 0;
}

static int kolibri_surface_finalise(nsfb_t *nsfb)
{
	nsfb = nsfb;
	exit(1);

	return 0;
}

int key_is_up(int scancode)
{
	return (scancode & 0x80) >> 7;
}

int scan2key(int scan)
{

	int keycode = scan & 0x0FF7F;

	/* MAIN KB - NUMS */
	if (keycode == 0x02) return NSFB_KEY_1;
	if (keycode == 0x03) return NSFB_KEY_2;
	if (keycode == 0x04) return NSFB_KEY_3;
	if (keycode == 0x05) return NSFB_KEY_4;
	if (keycode == 0x06) return NSFB_KEY_5;
	if (keycode == 0x07) return NSFB_KEY_6;
	if (keycode == 0x08) return NSFB_KEY_7;
	if (keycode == 0x09) return NSFB_KEY_8;
	if (keycode == 0x0A) return NSFB_KEY_9;
	if (keycode == 0x0B) return NSFB_KEY_0;

	if (keycode == 0x10) return NSFB_KEY_q;
	if (keycode == 0x11) return NSFB_KEY_w;
	if (keycode == 0x12) return NSFB_KEY_e;
	if (keycode == 0x13) return NSFB_KEY_r;
	if (keycode == 0x14) return NSFB_KEY_t;
	if (keycode == 0x15) return NSFB_KEY_y;
	if (keycode == 0x16) return NSFB_KEY_u;
	if (keycode == 0x17) return NSFB_KEY_i;
	if (keycode == 0x18) return NSFB_KEY_o;
	if (keycode == 0x19) return NSFB_KEY_p;
	if (keycode == 0x1A) return NSFB_KEY_LEFTBRACKET;
	if (keycode == 0x1B) return NSFB_KEY_RIGHTBRACKET;

	if (keycode == 0x1E) return NSFB_KEY_a;
	if (keycode == 0x1F) return NSFB_KEY_s;
	if (keycode == 0x20) return NSFB_KEY_d;
	if (keycode == 0x21) return NSFB_KEY_f;
	if (keycode == 0x22) return NSFB_KEY_g;
	if (keycode == 0x23) return NSFB_KEY_h;
	if (keycode == 0x24) return NSFB_KEY_j;
	if (keycode == 0x25) return NSFB_KEY_k;
	if (keycode == 0x26) return NSFB_KEY_l;

	if (keycode == 0x2C) return NSFB_KEY_z;
	if (keycode == 0x2D) return NSFB_KEY_x;
	if (keycode == 0x2E) return NSFB_KEY_c;
	if (keycode == 0x2F) return NSFB_KEY_v;
	if (keycode == 0x30) return NSFB_KEY_b;
	if (keycode == 0x31) return NSFB_KEY_n;
	if (keycode == 0x32) return NSFB_KEY_m;

	/* TODO: Add a TAB Key here to cycle through fields */
	if (keycode == 0x27) return NSFB_KEY_SEMICOLON;
	if (keycode == 0x28) return NSFB_KEY_QUOTEDBL;
	if (keycode == 0x2B) return NSFB_KEY_BACKSLASH;
	if (keycode == 0x33) return NSFB_KEY_COMMA;
	if (keycode == 0x34) return NSFB_KEY_PERIOD;
	if (keycode == 0x35) return NSFB_KEY_SLASH;
	if (keycode == 0x0C) return NSFB_KEY_MINUS;
	if (keycode == 0x0D) return NSFB_KEY_EQUALS;

	if (keycode == 0x0E) return NSFB_KEY_BACKSPACE;
	if (keycode == 0xE053) return NSFB_KEY_DELETE;
	if (keycode == 0x2A) return NSFB_KEY_LSHIFT;
	if (keycode == 0x36) return NSFB_KEY_RSHIFT;

	if (keycode == 0x1C) return NSFB_KEY_RETURN;

	if (keycode == 0xE04B) return NSFB_KEY_LEFT;
	if (keycode == 0xE04D) return NSFB_KEY_RIGHT;
	if (keycode == 0xE048) return NSFB_KEY_UP;
	if (keycode == 0xE050) return NSFB_KEY_DOWN;

	if (keycode == 0x3F) return NSFB_KEY_F5;

	if (keycode == 0x39) return NSFB_KEY_SPACE;
	if (keycode == 0x01) return NSFB_KEY_ESCAPE;

	if (keycode == 0x38) return NSFB_KEY_LALT;
	if (keycode == 0x1D) return NSFB_KEY_LCTRL;
	if (keycode == 0xE038) return NSFB_KEY_RALT;
	if (keycode == 0xE01D) return NSFB_KEY_RCTRL;


	if (keycode == 0xE047) return NSFB_KEY_HOME;
	if (keycode == 0xE04F) return NSFB_KEY_END;
	if (keycode == 0xE049) return NSFB_KEY_PAGEUP;
	if (keycode == 0xE051) return NSFB_KEY_PAGEDOWN;

	return NSFB_KEY_UNKNOWN;

}

/* TODO: Useful for future implementation */
int ispowerkey(int scancode)
{
	return (scancode & 0xE000) >> 15;
}

static bool kolibri_surface_input(nsfb_t *nsfb, nsfb_event_t *event,
		int timeout)
{
	int got_event;
	static int scanfull = 0;
	char event_num[20];

	nsfb = nsfb; /* unused */

	if (timeout >= 0) {
		got_event = kolibri_wait_for_event_with_timeout(timeout / 10);
	} else {
		got_event = kolibri_wait_for_event();
	}

	if (got_event == 0) {
		return false;
	}

	/* sprintf(event_num, "got_event = %d\n", got_event); */
	/* debug_board_write_str(event_num); */

	event->type = NSFB_EVENT_NONE;

	/* redraw event */
	if (got_event == 1) {
		kolibri_fb_redraw(nsfb);
	}
	/* keypress event */
	if (got_event == 2) {
		int scanz = kolibri_get_pressed_key();

		if (scanz == 0xE0) {
			scanfull = 0xE000;
			return true;
		} else {
			scanfull = scanfull + scanz;
	}

	if (key_is_up(scanfull) == 1) {
		event->type = NSFB_EVENT_KEY_UP;
	} else {
		event->type = NSFB_EVENT_KEY_DOWN;
	}

	event->value.keycode = scan2key(scanfull);

	scanfull = 0;

	return true;
	}

	/* button press event */
	if (got_event == 3) {
		if (kolibri_get_button_id() == 1)
			 kolibri_surface_finalise(nsfb);
		return true;
	}

	/* mouse event */
	if (got_event == 6) {
		unsigned z = kolibri_mouse_get_relative();
		unsigned b = kolibri_mouse_get_buttonpress();
		int s = kolibri_mouse_get_scrolldata();

		if (previous_mouse_position != z) {
			event->type = NSFB_EVENT_MOVE_ABSOLUTE;
			event->value.vector.x = (z & 0xffff0000) >> 16;
			event->value.vector.y = z & 0xffff;
			event->value.vector.z = 0;
			previous_mouse_position = z;
		}
		else if (previous_mouse_buttons != b) {
			unsigned diff = previous_mouse_buttons^b;
			/* All high bits in the XOR represent bits that
			   changed */

			if (diff & EVENT_REDRAW) {
				/* Left mouse button */
				event->value.keycode = NSFB_KEY_MOUSE_1;
				if (b & EVENT_REDRAW) {
					event->type = NSFB_EVENT_KEY_DOWN;
				} else {
					event->type = NSFB_EVENT_KEY_UP;
				}
			} else if (diff & EVENT_KEY) {
				 /* Right mouse button */
				event->value.keycode = NSFB_KEY_MOUSE_3;
				if (b & EVENT_KEY) {
					event->type = NSFB_EVENT_KEY_DOWN;
				} else {
					event->type = NSFB_EVENT_KEY_UP;
				}
			} else if (diff & EVENT_BUTTON) {
				/* Middle mouse button */
				event->value.keycode = NSFB_KEY_MOUSE_2;
				if (b & EVENT_BUTTON) {
					event->type = NSFB_EVENT_KEY_DOWN;
				} else {
					event->type = NSFB_EVENT_KEY_UP;
				}
			} else if (diff & EVENT_END_REQUEST) {
				/* 4th mouse button (forward) */
				event->value.keycode = NSFB_KEY_MOUSE_4;
				if (b & EVENT_END_REQUEST) {
					event->type = NSFB_EVENT_KEY_DOWN;
				} else {
					event->type = NSFB_EVENT_KEY_UP;
				}
			} else if (diff & EVENT_DESKTOP_BACK_DRAW) {
				/* 5th mouse button (back) */
				event->value.keycode = NSFB_KEY_MOUSE_5;
				if (b & EVENT_DESKTOP_BACK_DRAW) {
					event->type = NSFB_EVENT_KEY_DOWN;
				} else {
					event->type = NSFB_EVENT_KEY_UP;
				}
			} else {
				/*The Event 6 did not match any handled cases*/
				char diffstr[40];
				sprintf(diffstr, "Unhandled case."
					"Previous_mouse_buttons^b is :"
					"%u", diff);

				debug_board_write_str(diffstr);
			}
			previous_mouse_buttons = b;
		}
		else if (s != 0) {
			short int vert = s & 0xffff;
			short int hori = s >> 16;

			event->type = NSFB_EVENT_KEY_DOWN;

			if (vert != 0) {
				/*Handle vertical scroll*/
				if (vert > 0) /*SCROLL DOWN*/
					event->value.keycode = NSFB_KEY_MOUSE_5;
				else /*SCROLL UP*/
					event->value.keycode = NSFB_KEY_MOUSE_4;
			}
			else {
				/* TODO: Handle Horizontal scroll here */
			}
		}
		return true;
	}
}

static int kolibri_surface_claim(nsfb_t *nsfb, nsfb_bbox_t *box)
{
	/* TODO: Convert to full function from stub  */

	/*
	if ((cursor != NULL) &&
		(cursor->plotted == true) &&
		(nsfb_plot_bbox_intersect(box, &cursor->loc))) {
		nsfb_cursor_clear(nsfb, cursor);
	}
	*/

	return 0;
}

static int kolibri_surface_cursor(nsfb_t *nsfb, struct nsfb_cursor_s *cursor)
{
	/* Convert to full function from stub  */
	return true;
}

static int kolibri_surface_update(nsfb_t *nsfb, nsfb_bbox_t *box)
{
	/* Do the window redraw here */
	kolibri_redraw(nsfb);
	return 0;
}

const nsfb_surface_rtns_t kolibri_rtns = {
	.initialise = kolibri_surface_initialise,
	.finalise = kolibri_surface_finalise,
	.input = kolibri_surface_input,
	.claim = kolibri_surface_claim,
	.update = kolibri_surface_update,
	.cursor = kolibri_surface_cursor,
	.geometry = kolibri_surface_set_geometry,
};

NSFB_SURFACE_DEF(kolibri, NSFB_SURFACE_KOLIBRI, &kolibri_rtns)