]> git.proxmox.com Git - spiceterm.git/blob - test_display_base.c
implement connection timeout
[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 free(update->bitmap);
44 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 and rects are freed, so they must be allocated with 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 = calloc(sizeof(*update), 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_spice_create_update_draw(Test *test, uint32_t surface_id)
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 = 100;
138 top = 100;
139
140 unique++;
141
142 bw = 8;
143 bh = 16;
144
145 bitmap = dst = malloc(bw * bh * 4);
146 //printf("allocated %p\n", dst);
147
148 int c = 65;
149 unsigned char *data = vt_font_data + c*16;
150 unsigned char d = *data;
151
152 for (j = 0; j < 16; j++) {
153 for (i = 0; i < 8; i++) {
154 if ((i&7) == 0) {
155 d=*data;
156 data++;
157 }
158 if (d&0x80) {
159 *(dst) = 255;
160 *(dst+1) = 255;
161 *(dst+2) = 255;
162 *(dst+3) = 0;
163 }
164 d<<=1;
165 dst += 4;
166 }
167 }
168
169 bbox.left = left; bbox.top = top;
170 bbox.right = left + bw; bbox.bottom = top + bh;
171
172 return test_spice_create_update_from_bitmap(surface_id, bbox, bitmap);
173 }
174
175
176 static void create_primary_surface(Test *test, uint32_t width,
177 uint32_t height)
178 {
179 QXLWorker *qxl_worker = test->qxl_worker;
180 QXLDevSurfaceCreate surface = { 0, };
181
182 g_assert(height <= MAX_HEIGHT);
183 g_assert(width <= MAX_WIDTH);
184 g_assert(height > 0);
185 g_assert(width > 0);
186
187 surface.format = SPICE_SURFACE_FMT_32_xRGB;
188 surface.width = test->primary_width = width;
189 surface.height = test->primary_height = height;
190 surface.stride = -width * 4; /* negative? */
191 surface.mouse_mode = TRUE; /* unused by red_worker */
192 surface.flags = 0;
193 surface.type = 0; /* unused by red_worker */
194 surface.position = 0; /* unused by red_worker */
195 surface.mem = (uint64_t)&test->primary_surface;
196 surface.group_id = MEM_SLOT_GROUP_ID;
197
198 test->width = width;
199 test->height = height;
200
201 qxl_worker->create_primary_surface(qxl_worker, 0, &surface);
202 }
203
204 QXLDevMemSlot slot = {
205 .slot_group_id = MEM_SLOT_GROUP_ID,
206 .slot_id = 0,
207 .generation = 0,
208 .virt_start = 0,
209 .virt_end = ~0,
210 .addr_delta = 0,
211 .qxl_ram_size = ~0,
212 };
213
214 static void attache_worker(QXLInstance *qin, QXLWorker *_qxl_worker)
215 {
216 Test *test = SPICE_CONTAINEROF(qin, Test, qxl_instance);
217
218 if (test->qxl_worker) {
219 if (test->qxl_worker != _qxl_worker)
220 printf("%s ignored, %p is set, ignoring new %p\n", __func__,
221 test->qxl_worker, _qxl_worker);
222 else
223 printf("%s ignored, redundant\n", __func__);
224 return;
225 }
226 printf("%s\n", __func__);
227 test->qxl_worker = _qxl_worker;
228 test->qxl_worker->add_memslot(test->qxl_worker, &slot);
229 create_primary_surface(test, DEFAULT_WIDTH, DEFAULT_HEIGHT);
230 test->qxl_worker->start(test->qxl_worker);
231 }
232
233 static void set_compression_level(QXLInstance *qin, int level)
234 {
235 printf("%s\n", __func__);
236 }
237
238 static void set_mm_time(QXLInstance *qin, uint32_t mm_time)
239 {
240 }
241 static void get_init_info(QXLInstance *qin, QXLDevInitInfo *info)
242 {
243 memset(info, 0, sizeof(*info));
244 info->num_memslots = 1;
245 info->num_memslots_groups = 1;
246 info->memslot_id_bits = 1;
247 info->memslot_gen_bits = 1;
248 info->n_surfaces = 1;
249 }
250
251
252 // We shall now have a ring of commands, so that we can update
253 // it from a separate thread - since get_command is called from
254 // the worker thread, and we need to sometimes do an update_area,
255 // which cannot be done from red_worker context (not via dispatcher,
256 // since you get a deadlock, and it isn't designed to be done
257 // any other way, so no point testing that).
258 int commands_end = 0;
259 int commands_start = 0;
260 struct QXLCommandExt* commands[1024];
261
262 #define COMMANDS_SIZE COUNT(commands)
263
264 static void push_command(QXLCommandExt *ext)
265 {
266 g_assert(commands_end - commands_start < COMMANDS_SIZE);
267 commands[commands_end % COMMANDS_SIZE] = ext;
268 commands_end++;
269 }
270
271 static struct QXLCommandExt *get_simple_command(void)
272 {
273 struct QXLCommandExt *ret = commands[commands_start % COMMANDS_SIZE];
274 g_assert(commands_start < commands_end);
275 commands_start++;
276 return ret;
277 }
278
279 static int get_num_commands(void)
280 {
281 return commands_end - commands_start;
282 }
283
284 // called from spice_server thread (i.e. red_worker thread)
285 static int get_command(QXLInstance *qin, struct QXLCommandExt *ext)
286 {
287 if (get_num_commands() == 0) {
288 return FALSE;
289 }
290 *ext = *get_simple_command();
291 return TRUE;
292 }
293
294 static void produce_command(Test *test)
295 {
296 Command *command;
297 QXLWorker *qxl_worker = test->qxl_worker;
298
299 g_assert(qxl_worker);
300
301 if (!test->num_commands) {
302 usleep(1000);
303 return;
304 }
305
306 command = &test->commands[test->cmd_index];
307 if (command->cb) {
308 command->cb(test, command);
309 }
310 switch (command->command) {
311 /* Drawing commands, they all push a command to the command ring */
312 case SIMPLE_DRAW: {
313 SimpleSpiceUpdate *update;
314 update = test_spice_create_update_draw(test, 0);
315 push_command(&update->ext);
316 break;
317 }
318 }
319 test->cmd_index = (test->cmd_index + 1) % test->num_commands;
320 }
321
322 static int req_cmd_notification(QXLInstance *qin)
323 {
324 Test *test = SPICE_CONTAINEROF(qin, Test, qxl_instance);
325
326 test->core->timer_start(test->wakeup_timer, test->wakeup_ms);
327 return TRUE;
328 }
329
330 static void do_wakeup(void *opaque)
331 {
332 Test *test = opaque;
333 int notify;
334
335 test->cursor_notify = NOTIFY_CURSOR_BATCH;
336 for (notify = NOTIFY_DISPLAY_BATCH; notify > 0;--notify) {
337 produce_command(test);
338 }
339
340 test->core->timer_start(test->wakeup_timer, test->wakeup_ms);
341 test->qxl_worker->wakeup(test->qxl_worker);
342 }
343
344 static void release_resource(QXLInstance *qin, struct QXLReleaseInfoExt release_info)
345 {
346 QXLCommandExt *ext = (QXLCommandExt*)(unsigned long)release_info.info->id;
347 //printf("%s\n", __func__);
348 g_assert(release_info.group_id == MEM_SLOT_GROUP_ID);
349 switch (ext->cmd.type) {
350 case QXL_CMD_DRAW:
351 test_spice_destroy_update((void*)ext);
352 break;
353 case QXL_CMD_SURFACE:
354 free(ext);
355 break;
356 case QXL_CMD_CURSOR: {
357 QXLCursorCmd *cmd = (QXLCursorCmd *)(unsigned long)ext->cmd.data;
358 if (cmd->type == QXL_CURSOR_SET) {
359 free(cmd);
360 }
361 free(ext);
362 break;
363 }
364 default:
365 abort();
366 }
367 }
368
369 #define CURSOR_WIDTH 32
370 #define CURSOR_HEIGHT 32
371
372 static struct {
373 QXLCursor cursor;
374 uint8_t data[CURSOR_WIDTH * CURSOR_HEIGHT * 4]; // 32bit per pixel
375 } cursor;
376
377 static void cursor_init()
378 {
379 cursor.cursor.header.unique = 0;
380 cursor.cursor.header.type = SPICE_CURSOR_TYPE_COLOR32;
381 cursor.cursor.header.width = CURSOR_WIDTH;
382 cursor.cursor.header.height = CURSOR_HEIGHT;
383 cursor.cursor.header.hot_spot_x = 0;
384 cursor.cursor.header.hot_spot_y = 0;
385 cursor.cursor.data_size = CURSOR_WIDTH * CURSOR_HEIGHT * 4;
386
387 // X drivers addes it to the cursor size because it could be
388 // cursor data information or another cursor related stuffs.
389 // Otherwise, the code will break in client/cursor.cpp side,
390 // that expect the data_size plus cursor information.
391 // Blame cursor protocol for this. :-)
392 cursor.cursor.data_size += 128;
393 cursor.cursor.chunk.data_size = cursor.cursor.data_size;
394 cursor.cursor.chunk.prev_chunk = cursor.cursor.chunk.next_chunk = 0;
395 }
396
397 static int get_cursor_command(QXLInstance *qin, struct QXLCommandExt *ext)
398 {
399 Test *test = SPICE_CONTAINEROF(qin, Test, qxl_instance);
400 static int color = 0;
401 static int set = 1;
402 static int x = 0, y = 0;
403 QXLCursorCmd *cursor_cmd;
404 QXLCommandExt *cmd;
405
406 if (!test->cursor_notify) {
407 return FALSE;
408 }
409
410 test->cursor_notify--;
411 cmd = calloc(sizeof(QXLCommandExt), 1);
412 cursor_cmd = calloc(sizeof(QXLCursorCmd), 1);
413
414 cursor_cmd->release_info.id = (unsigned long)cmd;
415
416 if (set) {
417 cursor_cmd->type = QXL_CURSOR_SET;
418 cursor_cmd->u.set.position.x = 0;
419 cursor_cmd->u.set.position.y = 0;
420 cursor_cmd->u.set.visible = TRUE;
421 cursor_cmd->u.set.shape = (unsigned long)&cursor;
422 // Only a white rect (32x32) as cursor
423 memset(cursor.data, 255, sizeof(cursor.data));
424 set = 0;
425 } else {
426 cursor_cmd->type = QXL_CURSOR_MOVE;
427 cursor_cmd->u.position.x = x++ % test->primary_width;
428 cursor_cmd->u.position.y = y++ % test->primary_height;
429 }
430
431 cmd->cmd.data = (unsigned long)cursor_cmd;
432 cmd->cmd.type = QXL_CMD_CURSOR;
433 cmd->group_id = MEM_SLOT_GROUP_ID;
434 cmd->flags = 0;
435 *ext = *cmd;
436 //printf("%s\n", __func__);
437 return TRUE;
438 }
439
440 static int req_cursor_notification(QXLInstance *qin)
441 {
442 printf("%s\n", __func__);
443 return TRUE;
444 }
445
446 static void notify_update(QXLInstance *qin, uint32_t update_id)
447 {
448 printf("%s\n", __func__);
449 }
450
451 static int flush_resources(QXLInstance *qin)
452 {
453 printf("%s\n", __func__);
454 return TRUE;
455 }
456
457 static int client_monitors_config(QXLInstance *qin,
458 VDAgentMonitorsConfig *monitors_config)
459 {
460 if (!monitors_config) {
461 printf("%s: NULL monitors_config\n", __func__);
462 } else {
463 printf("%s: %d\n", __func__, monitors_config->num_of_monitors);
464 }
465 return 0;
466 }
467
468 static void set_client_capabilities(QXLInstance *qin,
469 uint8_t client_present,
470 uint8_t caps[58])
471 {
472 Test *test = SPICE_CONTAINEROF(qin, Test, qxl_instance);
473
474 printf("%s: present %d caps %d\n", __func__, client_present, caps[0]);
475 if (test->on_client_connected && client_present) {
476 test->on_client_connected(test);
477 }
478 if (test->on_client_disconnected && !client_present) {
479 test->on_client_disconnected(test);
480 }
481 }
482
483 static int client_count = 0;
484
485 static void client_connected(Test *test)
486 {
487 printf("Client connected\n");
488 client_count++;
489 }
490
491 static void client_disconnected(Test *test)
492 {
493
494 if (client_count > 0) {
495 client_count--;
496 printf("Client disconnected\n");
497 exit(0); // fixme: cleanup?
498 }
499 }
500
501 static void do_conn_timeout(void *opaque)
502 {
503 Test *test = opaque;
504
505 if (client_count <= 0) {
506 printf("do_conn_timeout\n");
507 exit (0); // fixme: cleanup?
508 }
509 }
510
511
512 QXLInterface display_sif = {
513 .base = {
514 .type = SPICE_INTERFACE_QXL,
515 .description = "test",
516 .major_version = SPICE_INTERFACE_QXL_MAJOR,
517 .minor_version = SPICE_INTERFACE_QXL_MINOR
518 },
519 .attache_worker = attache_worker,
520 .set_compression_level = set_compression_level,
521 .set_mm_time = set_mm_time,
522 .get_init_info = get_init_info,
523
524 /* the callbacks below are called from spice server thread context */
525 .get_command = get_command,
526 .req_cmd_notification = req_cmd_notification,
527 .release_resource = release_resource,
528 .get_cursor_command = get_cursor_command,
529 .req_cursor_notification = req_cursor_notification,
530 .notify_update = notify_update,
531 .flush_resources = flush_resources,
532 .client_monitors_config = client_monitors_config,
533 .set_client_capabilities = set_client_capabilities,
534 };
535
536 /* interface for tests */
537 void test_add_display_interface(Test* test)
538 {
539 spice_server_add_interface(test->server, &test->qxl_instance.base);
540 }
541
542 static int vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
543 {
544 // printf("%s: %d\n", __func__, len);
545 return len;
546 }
547
548 static int vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len)
549 {
550 // printf("%s: %d\n", __func__, len);
551 return 0;
552 }
553
554 static void vmc_state(SpiceCharDeviceInstance *sin, int connected)
555 {
556 // printf("%s: %d\n", __func__, connected);
557 }
558
559 static SpiceCharDeviceInterface vdagent_sif = {
560 .base.type = SPICE_INTERFACE_CHAR_DEVICE,
561 .base.description = "test spice virtual channel char device",
562 .base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR,
563 .base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR,
564 .state = vmc_state,
565 .write = vmc_write,
566 .read = vmc_read,
567 };
568
569 SpiceCharDeviceInstance vdagent_sin = {
570 .base = {
571 .sif = &vdagent_sif.base,
572 },
573 .subtype = "vdagent",
574 };
575
576 void test_add_agent_interface(SpiceServer *server)
577 {
578 spice_server_add_interface(server, &vdagent_sin.base);
579 }
580
581 static void kbd_push_key(SpiceKbdInstance *sin, uint8_t frag)
582 {
583 printf("KEYCODE %u\n", frag);
584 }
585
586 static uint8_t kbd_get_leds(SpiceKbdInstance *sin)
587 {
588 return 0;
589 }
590
591 static SpiceKbdInterface keyboard_sif = {
592 .base.type = SPICE_INTERFACE_KEYBOARD ,
593 .base.description = "spiceterm keyboard device",
594 .base.major_version = SPICE_INTERFACE_KEYBOARD_MAJOR,
595 .base.minor_version = SPICE_INTERFACE_KEYBOARD_MINOR,
596 .push_scan_freg = kbd_push_key,
597 .get_leds = kbd_get_leds,
598 };
599
600 SpiceKbdInstance keyboard_sin = {
601 .base = {
602 .sif = &keyboard_sif.base,
603 },
604 };
605
606 void test_add_keyboard_interface(SpiceServer *server)
607 {
608 spice_server_add_interface(server, &keyboard_sin.base);
609 }
610
611 void test_set_simple_command_list(Test *test, int *simple_commands, int num_commands)
612 {
613 int i;
614
615 /* FIXME: leaks */
616 test->commands = malloc(sizeof(*test->commands) * num_commands);
617 memset(test->commands, 0, sizeof(*test->commands) * num_commands);
618 test->num_commands = num_commands;
619 for (i = 0 ; i < num_commands; ++i) {
620 test->commands[i].command = simple_commands[i];
621 }
622 }
623
624 void test_set_command_list(Test *test, Command *commands, int num_commands)
625 {
626 test->commands = commands;
627 test->num_commands = num_commands;
628 }
629
630
631 Test *test_new(SpiceCoreInterface *core)
632 {
633 int port = 5912;
634 Test *test = g_new0(Test, 1);
635 SpiceServer* server = spice_server_new();
636
637 test->on_client_connected = client_connected,
638 test->on_client_disconnected = client_disconnected,
639
640 test->qxl_instance.base.sif = &display_sif.base;
641 test->qxl_instance.id = 0;
642
643 test->core = core;
644 test->server = server;
645 test->wakeup_ms = 50;
646 test->cursor_notify = NOTIFY_CURSOR_BATCH;
647 // some common initialization for all display tests
648 printf("TESTER: listening on port %d (unsecure)\n", port);
649 spice_server_set_port(server, port);
650 spice_server_set_noauth(server);
651 int res = spice_server_init(server, core);
652 if (res != 0) {
653 g_error("spice_server_init failed, res = %d\n", res);
654 }
655
656 cursor_init();
657 test->has_secondary = 0;
658 test->wakeup_timer = core->timer_add(do_wakeup, test);
659
660 int timeout = 10; // max time to wait for client connection
661 test->conn_timeout_timer = core->timer_add(do_conn_timeout, test);
662 test->core->timer_start(test->conn_timeout_timer, timeout*1000);
663
664 return test;
665 }
666
667
668 __attribute__((noreturn))
669 void usage(const char *argv0, const int exitcode)
670 {
671
672 printf("usage: %s\n", argv0);
673 exit(exitcode);
674 }
675
676 void spice_test_config_parse_args(int argc, char **argv)
677 {
678 struct option options[] = {
679 // {"automated-tests", no_argument, &has_automated_tests, 1},
680 {NULL, 0, NULL, 0},
681 };
682 int option_index;
683 int val;
684
685 while ((val = getopt_long(argc, argv, "", options, &option_index)) != -1) {
686 switch (val) {
687 case '?':
688 printf("unrecognized option '%s'\n", argv[optind - 1]);
689 usage(argv[0], EXIT_FAILURE);
690 case 0:
691 break;
692 }
693 }
694
695 if (argc > optind) {
696 printf("unknown argument '%s'\n", argv[optind]);
697 usage(argv[0], EXIT_FAILURE);
698 }
699 return;
700 }