]> git.proxmox.com Git - spiceterm.git/blob - test_display_base.c
use new unicode spice input extension
[spiceterm.git] / test_display_base.c
1 #include <stdlib.h>
2 #include <math.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include <unistd.h>
6 #include <signal.h>
7 #include <wait.h>
8 #include <sys/select.h>
9 #include <sys/types.h>
10 #include <getopt.h>
11
12 #include "glyphs.h"
13 #include <spice.h>
14 #include <spice/enums.h>
15 #include <spice/macros.h>
16 #include <spice/qxl_dev.h>
17
18 #include "test_display_base.h"
19
20 #define MEM_SLOT_GROUP_ID 0
21
22 #define NOTIFY_DISPLAY_BATCH (SINGLE_PART/2)
23 #define NOTIFY_CURSOR_BATCH 10
24
25 /* Parts cribbed from spice-display.h/.c/qxl.c */
26
27 typedef struct SimpleSpiceUpdate {
28 QXLCommandExt ext; // first
29 QXLDrawable drawable;
30 QXLImage image;
31 uint8_t *bitmap;
32 } SimpleSpiceUpdate;
33
34 static void test_spice_destroy_update(SimpleSpiceUpdate *update)
35 {
36 if (!update) {
37 return;
38 }
39 if (update->drawable.clip.type != SPICE_CLIP_TYPE_NONE) {
40 uint8_t *ptr = (uint8_t*)update->drawable.clip.data;
41 free(ptr);
42 }
43 g_free(update->bitmap);
44 g_free(update);
45 }
46
47 #define DEFAULT_WIDTH 640
48 #define DEFAULT_HEIGHT 320
49
50 #define SINGLE_PART 4
51 static const int angle_parts = 64 / SINGLE_PART;
52 static int unique = 1;
53 static int color = -1;
54 static int c_i = 0;
55
56 __attribute__((noreturn))
57 static void sigchld_handler(int signal_num) // wait for the child process and exit
58 {
59 int status;
60 wait(&status);
61 exit(0);
62 }
63
64 static void set_cmd(QXLCommandExt *ext, uint32_t type, QXLPHYSICAL data)
65 {
66 ext->cmd.type = type;
67 ext->cmd.data = data;
68 ext->cmd.padding = 0;
69 ext->group_id = MEM_SLOT_GROUP_ID;
70 ext->flags = 0;
71 }
72
73 static void simple_set_release_info(QXLReleaseInfo *info, intptr_t ptr)
74 {
75 info->id = ptr;
76 //info->group_id = MEM_SLOT_GROUP_ID;
77 }
78
79 /* bitmap are freed, so they must be allocated with g_malloc */
80 SimpleSpiceUpdate *test_spice_create_update_from_bitmap(uint32_t surface_id,
81 QXLRect bbox,
82 uint8_t *bitmap)
83 {
84 SimpleSpiceUpdate *update;
85 QXLDrawable *drawable;
86 QXLImage *image;
87 uint32_t bw, bh;
88
89 bh = bbox.bottom - bbox.top;
90 bw = bbox.right - bbox.left;
91
92 update = g_new0(SimpleSpiceUpdate, 1);
93 update->bitmap = bitmap;
94 drawable = &update->drawable;
95 image = &update->image;
96
97 drawable->surface_id = surface_id;
98
99 drawable->bbox = bbox;
100 drawable->clip.type = SPICE_CLIP_TYPE_NONE;
101 drawable->effect = QXL_EFFECT_OPAQUE;
102 simple_set_release_info(&drawable->release_info, (intptr_t)update);
103 drawable->type = QXL_DRAW_COPY;
104 drawable->surfaces_dest[0] = -1;
105 drawable->surfaces_dest[1] = -1;
106 drawable->surfaces_dest[2] = -1;
107
108 drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
109 drawable->u.copy.src_bitmap = (intptr_t)image;
110 drawable->u.copy.src_area.right = bw;
111 drawable->u.copy.src_area.bottom = bh;
112
113 QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, unique);
114 image->descriptor.type = SPICE_IMAGE_TYPE_BITMAP;
115 image->bitmap.flags = QXL_BITMAP_DIRECT | QXL_BITMAP_TOP_DOWN;
116 image->bitmap.stride = bw * 4;
117 image->descriptor.width = image->bitmap.x = bw;
118 image->descriptor.height = image->bitmap.y = bh;
119 image->bitmap.data = (intptr_t)bitmap;
120 image->bitmap.palette = 0;
121 image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
122
123 set_cmd(&update->ext, QXL_CMD_DRAW, (intptr_t)drawable);
124
125 return update;
126 }
127
128 static SimpleSpiceUpdate *test_draw_char(Test *test, int x, int y, int c)
129 {
130 int top, left;
131 uint8_t *dst;
132 uint8_t *bitmap;
133 int bw, bh;
134 int i, j;
135 QXLRect bbox;
136
137 left = x*8;
138 top = y*16;
139
140 // printf("DRAWCHAR %d %d %d\n", left, top, c);
141
142 unique++;
143
144 bw = 8;
145 bh = 16;
146
147 bitmap = dst = g_malloc(bw * bh * 4);
148
149 unsigned char *data = vt_font_data + c*16;
150 unsigned char d = *data;
151
152 unsigned char fgc_red = 255;
153 unsigned char fgc_blue = 255;
154 unsigned char fgc_green = 255;
155 unsigned char bgc_red = 0;
156 unsigned char bgc_blue = 0;
157 unsigned char bgc_green = 0;
158
159 for (j = 0; j < 16; j++) {
160 for (i = 0; i < 8; i++) {
161 if ((i&7) == 0) {
162 d=*data;
163 data++;
164 }
165 if (d&0x80) {
166 *(dst) = fgc_blue;
167 *(dst+1) = fgc_green;
168 *(dst+2) = fgc_red;
169 *(dst+3) = 0;
170 } else {
171 *(dst) = bgc_blue;
172 *(dst+1) = bgc_green;
173 *(dst+2) = bgc_red;
174 *(dst+3) = 0;
175 }
176 d<<=1;
177 dst += 4;
178 }
179 }
180
181 bbox.left = left; bbox.top = top;
182 bbox.right = left + bw; bbox.bottom = top + bh;
183
184 return test_spice_create_update_from_bitmap(0, bbox, bitmap);
185 }
186
187 static void create_primary_surface(Test *test, uint32_t width,
188 uint32_t height)
189 {
190 QXLWorker *qxl_worker = test->qxl_worker;
191 QXLDevSurfaceCreate surface = { 0, };
192
193 g_assert(height <= MAX_HEIGHT);
194 g_assert(width <= MAX_WIDTH);
195 g_assert(height > 0);
196 g_assert(width > 0);
197
198 surface.format = SPICE_SURFACE_FMT_32_xRGB;
199 surface.width = test->primary_width = width;
200 surface.height = test->primary_height = height;
201 surface.stride = -width * 4; /* negative? */
202 surface.mouse_mode = TRUE; /* unused by red_worker */
203 surface.flags = 0;
204 surface.type = 0; /* unused by red_worker */
205 surface.position = 0; /* unused by red_worker */
206 surface.mem = (uint64_t)&test->primary_surface;
207 surface.group_id = MEM_SLOT_GROUP_ID;
208
209 test->width = width;
210 test->height = height;
211
212 qxl_worker->create_primary_surface(qxl_worker, 0, &surface);
213 }
214
215 QXLDevMemSlot slot = {
216 .slot_group_id = MEM_SLOT_GROUP_ID,
217 .slot_id = 0,
218 .generation = 0,
219 .virt_start = 0,
220 .virt_end = ~0,
221 .addr_delta = 0,
222 .qxl_ram_size = ~0,
223 };
224
225 static void attache_worker(QXLInstance *qin, QXLWorker *_qxl_worker)
226 {
227 Test *test = SPICE_CONTAINEROF(qin, Test, qxl_instance);
228
229 if (test->qxl_worker) {
230 if (test->qxl_worker != _qxl_worker)
231 printf("%s ignored, %p is set, ignoring new %p\n", __func__,
232 test->qxl_worker, _qxl_worker);
233 else
234 printf("%s ignored, redundant\n", __func__);
235 return;
236 }
237 printf("%s\n", __func__);
238 test->qxl_worker = _qxl_worker;
239 test->qxl_worker->add_memslot(test->qxl_worker, &slot);
240 create_primary_surface(test, DEFAULT_WIDTH, DEFAULT_HEIGHT);
241 test->qxl_worker->start(test->qxl_worker);
242 }
243
244 static void set_compression_level(QXLInstance *qin, int level)
245 {
246 printf("%s\n", __func__);
247 }
248
249 static void set_mm_time(QXLInstance *qin, uint32_t mm_time)
250 {
251 }
252 static void get_init_info(QXLInstance *qin, QXLDevInitInfo *info)
253 {
254 memset(info, 0, sizeof(*info));
255 info->num_memslots = 1;
256 info->num_memslots_groups = 1;
257 info->memslot_id_bits = 1;
258 info->memslot_gen_bits = 1;
259 info->n_surfaces = 1;
260 }
261
262
263 // We shall now have a ring of commands, so that we can update
264 // it from a separate thread - since get_command is called from
265 // the worker thread, and we need to sometimes do an update_area,
266 // which cannot be done from red_worker context (not via dispatcher,
267 // since you get a deadlock, and it isn't designed to be done
268 // any other way, so no point testing that).
269 int commands_end = 0;
270 int commands_start = 0;
271 struct QXLCommandExt* commands[1024];
272
273 #define COMMANDS_SIZE COUNT(commands)
274
275 static void push_command(QXLCommandExt *ext)
276 {
277 g_assert(commands_end - commands_start < COMMANDS_SIZE);
278 commands[commands_end % COMMANDS_SIZE] = ext;
279 commands_end++;
280 }
281
282 static struct QXLCommandExt *get_simple_command(void)
283 {
284 struct QXLCommandExt *ret = commands[commands_start % COMMANDS_SIZE];
285 g_assert(commands_start < commands_end);
286 commands_start++;
287 return ret;
288 }
289
290 static int get_num_commands(void)
291 {
292 return commands_end - commands_start;
293 }
294
295 // called from spice_server thread (i.e. red_worker thread)
296 static int get_command(QXLInstance *qin, struct QXLCommandExt *ext)
297 {
298 if (get_num_commands() == 0) {
299 return FALSE;
300 }
301 *ext = *get_simple_command();
302 return TRUE;
303 }
304
305 static int req_cmd_notification(QXLInstance *qin)
306 {
307 Test *test = SPICE_CONTAINEROF(qin, Test, qxl_instance);
308
309 //test->core->timer_start(test->wakeup_timer, test->wakeup_ms);
310 return TRUE;
311 }
312
313 static void release_resource(QXLInstance *qin, struct QXLReleaseInfoExt release_info)
314 {
315 QXLCommandExt *ext = (QXLCommandExt*)(unsigned long)release_info.info->id;
316 //printf("%s\n", __func__);
317 g_assert(release_info.group_id == MEM_SLOT_GROUP_ID);
318 switch (ext->cmd.type) {
319 case QXL_CMD_DRAW:
320 test_spice_destroy_update((void*)ext);
321 break;
322 case QXL_CMD_SURFACE:
323 free(ext);
324 break;
325 case QXL_CMD_CURSOR: {
326 QXLCursorCmd *cmd = (QXLCursorCmd *)(unsigned long)ext->cmd.data;
327 if (cmd->type == QXL_CURSOR_SET) {
328 free(cmd);
329 }
330 free(ext);
331 break;
332 }
333 default:
334 abort();
335 }
336 }
337
338 #define CURSOR_WIDTH 32
339 #define CURSOR_HEIGHT 32
340
341 static struct {
342 QXLCursor cursor;
343 uint8_t data[CURSOR_WIDTH * CURSOR_HEIGHT * 4]; // 32bit per pixel
344 } cursor;
345
346 static void cursor_init()
347 {
348 cursor.cursor.header.unique = 0;
349 cursor.cursor.header.type = SPICE_CURSOR_TYPE_COLOR32;
350 cursor.cursor.header.width = CURSOR_WIDTH;
351 cursor.cursor.header.height = CURSOR_HEIGHT;
352 cursor.cursor.header.hot_spot_x = 0;
353 cursor.cursor.header.hot_spot_y = 0;
354 cursor.cursor.data_size = CURSOR_WIDTH * CURSOR_HEIGHT * 4;
355
356 // X drivers addes it to the cursor size because it could be
357 // cursor data information or another cursor related stuffs.
358 // Otherwise, the code will break in client/cursor.cpp side,
359 // that expect the data_size plus cursor information.
360 // Blame cursor protocol for this. :-)
361 cursor.cursor.data_size += 128;
362 cursor.cursor.chunk.data_size = cursor.cursor.data_size;
363 cursor.cursor.chunk.prev_chunk = cursor.cursor.chunk.next_chunk = 0;
364 }
365
366 static int get_cursor_command(QXLInstance *qin, struct QXLCommandExt *ext)
367 {
368 Test *test = SPICE_CONTAINEROF(qin, Test, qxl_instance);
369 static int color = 0;
370 static int set = 1;
371 static int x = 0, y = 0;
372 QXLCursorCmd *cursor_cmd;
373 QXLCommandExt *cmd;
374
375 if (!test->cursor_notify) {
376 return FALSE;
377 }
378
379 test->cursor_notify--;
380 cmd = calloc(sizeof(QXLCommandExt), 1);
381 cursor_cmd = calloc(sizeof(QXLCursorCmd), 1);
382
383 cursor_cmd->release_info.id = (unsigned long)cmd;
384
385 if (set) {
386 cursor_cmd->type = QXL_CURSOR_SET;
387 cursor_cmd->u.set.position.x = 0;
388 cursor_cmd->u.set.position.y = 0;
389 cursor_cmd->u.set.visible = TRUE;
390 cursor_cmd->u.set.shape = (unsigned long)&cursor;
391 // Only a white rect (32x32) as cursor
392 memset(cursor.data, 255, sizeof(cursor.data));
393 set = 0;
394 } else {
395 cursor_cmd->type = QXL_CURSOR_MOVE;
396 cursor_cmd->u.position.x = x++ % test->primary_width;
397 cursor_cmd->u.position.y = y++ % test->primary_height;
398 }
399
400 cmd->cmd.data = (unsigned long)cursor_cmd;
401 cmd->cmd.type = QXL_CMD_CURSOR;
402 cmd->group_id = MEM_SLOT_GROUP_ID;
403 cmd->flags = 0;
404 *ext = *cmd;
405 //printf("%s\n", __func__);
406 return TRUE;
407 }
408
409 static int req_cursor_notification(QXLInstance *qin)
410 {
411 printf("%s\n", __func__);
412 return TRUE;
413 }
414
415 static void notify_update(QXLInstance *qin, uint32_t update_id)
416 {
417 printf("%s\n", __func__);
418 }
419
420 static int flush_resources(QXLInstance *qin)
421 {
422 printf("%s\n", __func__);
423 return TRUE;
424 }
425
426 static int client_monitors_config(QXLInstance *qin,
427 VDAgentMonitorsConfig *monitors_config)
428 {
429 if (!monitors_config) {
430 printf("%s: NULL monitors_config\n", __func__);
431 } else {
432 printf("%s: %d\n", __func__, monitors_config->num_of_monitors);
433 }
434 return 0;
435 }
436
437 static void set_client_capabilities(QXLInstance *qin,
438 uint8_t client_present,
439 uint8_t caps[58])
440 {
441 Test *test = SPICE_CONTAINEROF(qin, Test, qxl_instance);
442
443 printf("%s: present %d caps %d\n", __func__, client_present, caps[0]);
444 if (test->on_client_connected && client_present) {
445 test->on_client_connected(test);
446 }
447 if (test->on_client_disconnected && !client_present) {
448 test->on_client_disconnected(test);
449 }
450 }
451
452 static int client_count = 0;
453
454 static void client_connected(Test *test)
455 {
456 printf("Client connected\n");
457 client_count++;
458 }
459
460 static void client_disconnected(Test *test)
461 {
462
463 if (client_count > 0) {
464 client_count--;
465 printf("Client disconnected\n");
466 exit(0); // fixme: cleanup?
467 }
468 }
469
470 static void do_conn_timeout(void *opaque)
471 {
472 Test *test = opaque;
473
474 if (client_count <= 0) {
475 printf("do_conn_timeout\n");
476 exit (0); // fixme: cleanup?
477 }
478 }
479
480
481 QXLInterface display_sif = {
482 .base = {
483 .type = SPICE_INTERFACE_QXL,
484 .description = "test",
485 .major_version = SPICE_INTERFACE_QXL_MAJOR,
486 .minor_version = SPICE_INTERFACE_QXL_MINOR
487 },
488 .attache_worker = attache_worker,
489 .set_compression_level = set_compression_level,
490 .set_mm_time = set_mm_time,
491 .get_init_info = get_init_info,
492
493 /* the callbacks below are called from spice server thread context */
494 .get_command = get_command,
495 .req_cmd_notification = req_cmd_notification,
496 .release_resource = release_resource,
497 .get_cursor_command = get_cursor_command,
498 .req_cursor_notification = req_cursor_notification,
499 .notify_update = notify_update,
500 .flush_resources = flush_resources,
501 .client_monitors_config = client_monitors_config,
502 .set_client_capabilities = set_client_capabilities,
503 };
504
505 /* interface for tests */
506 void test_add_display_interface(Test* test)
507 {
508 spice_server_add_interface(test->server, &test->qxl_instance.base);
509 }
510
511 static int vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
512 {
513 // printf("%s: %d\n", __func__, len);
514 return len;
515 }
516
517 static int vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len)
518 {
519 // printf("%s: %d\n", __func__, len);
520 return 0;
521 }
522
523 static void vmc_state(SpiceCharDeviceInstance *sin, int connected)
524 {
525 // printf("%s: %d\n", __func__, connected);
526 }
527
528 static SpiceCharDeviceInterface vdagent_sif = {
529 .base.type = SPICE_INTERFACE_CHAR_DEVICE,
530 .base.description = "test spice virtual channel char device",
531 .base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR,
532 .base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR,
533 .state = vmc_state,
534 .write = vmc_write,
535 .read = vmc_read,
536 };
537
538 SpiceCharDeviceInstance vdagent_sin = {
539 .base = {
540 .sif = &vdagent_sif.base,
541 },
542 .subtype = "vdagent",
543 };
544
545 void test_add_agent_interface(SpiceServer *server)
546 {
547 spice_server_add_interface(server, &vdagent_sin.base);
548 }
549
550 static int my_charcode = 65;
551 static int my_posx = 0;
552 static void kbd_push_key(SpiceKbdInstance *sin, uint8_t frag)
553 {
554 Test *test = SPICE_CONTAINEROF(sin, Test, keyboard_sin);
555
556 printf("KEYCODE %u %p\n", frag, test);
557
558 SimpleSpiceUpdate *update;
559 update = test_draw_char(test, my_posx, 10, my_charcode);
560 my_posx++;
561 my_charcode++;
562 push_command(&update->ext);
563
564 test->qxl_worker->wakeup(test->qxl_worker);
565 }
566
567 void test_draw_update_char(Test *test, int x, int y, int c, TextAttributes attrib)
568 {
569 int fg, bg;
570
571 if (attrib.invers) {
572 bg = attrib.fgcol;
573 fg = attrib.bgcol;
574 } else {
575 bg = attrib.bgcol;
576 fg = attrib.fgcol;
577 }
578
579 if (attrib.bold) {
580 fg += 8;
581 }
582
583 // unsuported attributes = (attrib.blink || attrib.unvisible)
584
585 //if (attrib.uline) {
586 //rfbDrawLine (vt->screen, rx, ry + 14, rxe, ry + 14, fg);
587 //}
588
589 SimpleSpiceUpdate *update;
590 update = test_draw_char(test, x, y, c);
591 push_command(&update->ext);
592
593 test->qxl_worker->wakeup(test->qxl_worker);
594 }
595
596 static uint8_t kbd_get_leds(SpiceKbdInstance *sin)
597 {
598 return 0;
599 }
600
601 static SpiceKbdInterface keyboard_sif = {
602 .base.type = SPICE_INTERFACE_KEYBOARD ,
603 .base.description = "spiceterm keyboard device",
604 .base.major_version = SPICE_INTERFACE_KEYBOARD_MAJOR,
605 .base.minor_version = SPICE_INTERFACE_KEYBOARD_MINOR,
606 .push_scan_freg = kbd_push_key,
607 .get_leds = kbd_get_leds,
608 };
609
610 void test_add_keyboard_interface(Test* test)
611 {
612 spice_server_add_interface(test->server, &test->keyboard_sin.base);
613 }
614
615 Test *test_new(SpiceCoreInterface *core)
616 {
617 int port = 5912;
618 Test *test = g_new0(Test, 1);
619 SpiceServer* server = spice_server_new();
620
621 test->on_client_connected = client_connected,
622 test->on_client_disconnected = client_disconnected,
623
624 test->qxl_instance.base.sif = &display_sif.base;
625 test->qxl_instance.id = 0;
626
627 test->keyboard_sin.base.sif = &keyboard_sif.base;
628
629 test->core = core;
630 test->server = server;
631
632 test->cursor_notify = NOTIFY_CURSOR_BATCH;
633 // some common initialization for all display tests
634 printf("TESTER: listening on port %d (unsecure)\n", port);
635 spice_server_set_port(server, port);
636 spice_server_set_noauth(server);
637 int res = spice_server_init(server, core);
638 if (res != 0) {
639 g_error("spice_server_init failed, res = %d\n", res);
640 }
641
642 cursor_init();
643 test->has_secondary = 0;
644
645 int timeout = 10; // max time to wait for client connection
646 test->conn_timeout_timer = core->timer_add(do_conn_timeout, test);
647 test->core->timer_start(test->conn_timeout_timer, timeout*1000);
648
649 return test;
650 }
651
652
653 __attribute__((noreturn))
654 void usage(const char *argv0, const int exitcode)
655 {
656
657 printf("usage: %s\n", argv0);
658 exit(exitcode);
659 }
660
661 void spice_test_config_parse_args(int argc, char **argv)
662 {
663 struct option options[] = {
664 // {"automated-tests", no_argument, &has_automated_tests, 1},
665 {NULL, 0, NULL, 0},
666 };
667 int option_index;
668 int val;
669
670 while ((val = getopt_long(argc, argv, "", options, &option_index)) != -1) {
671 switch (val) {
672 case '?':
673 printf("unrecognized option '%s'\n", argv[optind - 1]);
674 usage(argv[0], EXIT_FAILURE);
675 case 0:
676 break;
677 }
678 }
679
680 if (argc > optind) {
681 printf("unknown argument '%s'\n", argv[optind]);
682 usage(argv[0], EXIT_FAILURE);
683 }
684 return;
685 }