]> git.proxmox.com Git - spiceterm.git/blame - test_display_base.c
remove wakup timer
[spiceterm.git] / test_display_base.c
CommitLineData
cc04455b
DM
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
4219b121 12#include "glyphs.h"
cc04455b 13#include <spice.h>
474762d8 14#include <spice/enums.h>
cc04455b
DM
15#include <spice/macros.h>
16#include <spice/qxl_dev.h>
17
18#include "test_display_base.h"
cc04455b
DM
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
27typedef struct SimpleSpiceUpdate {
28 QXLCommandExt ext; // first
29 QXLDrawable drawable;
30 QXLImage image;
31 uint8_t *bitmap;
32} SimpleSpiceUpdate;
33
cc04455b
DM
34static 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
51static const int angle_parts = 64 / SINGLE_PART;
52static int unique = 1;
53static int color = -1;
54static int c_i = 0;
55
cc04455b
DM
56__attribute__((noreturn))
57static 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
cc04455b
DM
64static 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
73static 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
cc04455b
DM
79/* bitmap and rects are freed, so they must be allocated with malloc */
80SimpleSpiceUpdate *test_spice_create_update_from_bitmap(uint32_t surface_id,
81 QXLRect bbox,
4219b121 82 uint8_t *bitmap)
cc04455b
DM
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;
4219b121 100 drawable->clip.type = SPICE_CLIP_TYPE_NONE;
cc04455b
DM
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
8a22eb4f 128static SimpleSpiceUpdate *test_draw_char(Test *test, int x, int y, int c)
cc04455b
DM
129{
130 int top, left;
131 uint8_t *dst;
132 uint8_t *bitmap;
133 int bw, bh;
9462ba15 134 int i, j;
cc04455b
DM
135 QXLRect bbox;
136
8a22eb4f
DM
137 left = x*8;
138 top = y*16;
26c64941 139
8a22eb4f
DM
140 printf("DRAW %d %d %d\n", left, top, c);
141
cc04455b
DM
142 unique++;
143
26c64941
DM
144 bw = 8;
145 bh = 16;
cc04455b
DM
146
147 bitmap = dst = malloc(bw * bh * 4);
cc04455b 148
9462ba15
DM
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 }
cc04455b
DM
167 }
168
169 bbox.left = left; bbox.top = top;
170 bbox.right = left + bw; bbox.bottom = top + bh;
9462ba15 171
8a22eb4f 172 return test_spice_create_update_from_bitmap(0, bbox, bitmap);
cc04455b
DM
173}
174
cc04455b 175
cc04455b
DM
176static 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
204QXLDevMemSlot 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
214static 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
233static void set_compression_level(QXLInstance *qin, int level)
234{
235 printf("%s\n", __func__);
236}
237
238static void set_mm_time(QXLInstance *qin, uint32_t mm_time)
239{
240}
cc04455b
DM
241static 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;
aae93060 248 info->n_surfaces = 1;
cc04455b
DM
249}
250
aae93060 251
cc04455b
DM
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).
258int commands_end = 0;
259int commands_start = 0;
260struct QXLCommandExt* commands[1024];
261
262#define COMMANDS_SIZE COUNT(commands)
263
264static 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
271static 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
279static 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)
285static 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
cc04455b
DM
294static int req_cmd_notification(QXLInstance *qin)
295{
296 Test *test = SPICE_CONTAINEROF(qin, Test, qxl_instance);
297
8a22eb4f 298 //test->core->timer_start(test->wakeup_timer, test->wakeup_ms);
cc04455b
DM
299 return TRUE;
300}
f6cb554c 301
cc04455b
DM
302static void release_resource(QXLInstance *qin, struct QXLReleaseInfoExt release_info)
303{
304 QXLCommandExt *ext = (QXLCommandExt*)(unsigned long)release_info.info->id;
305 //printf("%s\n", __func__);
306 g_assert(release_info.group_id == MEM_SLOT_GROUP_ID);
307 switch (ext->cmd.type) {
308 case QXL_CMD_DRAW:
309 test_spice_destroy_update((void*)ext);
310 break;
311 case QXL_CMD_SURFACE:
312 free(ext);
313 break;
314 case QXL_CMD_CURSOR: {
315 QXLCursorCmd *cmd = (QXLCursorCmd *)(unsigned long)ext->cmd.data;
316 if (cmd->type == QXL_CURSOR_SET) {
317 free(cmd);
318 }
319 free(ext);
320 break;
321 }
322 default:
323 abort();
324 }
325}
326
327#define CURSOR_WIDTH 32
328#define CURSOR_HEIGHT 32
329
330static struct {
331 QXLCursor cursor;
332 uint8_t data[CURSOR_WIDTH * CURSOR_HEIGHT * 4]; // 32bit per pixel
333} cursor;
334
335static void cursor_init()
336{
337 cursor.cursor.header.unique = 0;
338 cursor.cursor.header.type = SPICE_CURSOR_TYPE_COLOR32;
339 cursor.cursor.header.width = CURSOR_WIDTH;
340 cursor.cursor.header.height = CURSOR_HEIGHT;
341 cursor.cursor.header.hot_spot_x = 0;
342 cursor.cursor.header.hot_spot_y = 0;
343 cursor.cursor.data_size = CURSOR_WIDTH * CURSOR_HEIGHT * 4;
344
345 // X drivers addes it to the cursor size because it could be
346 // cursor data information or another cursor related stuffs.
347 // Otherwise, the code will break in client/cursor.cpp side,
348 // that expect the data_size plus cursor information.
349 // Blame cursor protocol for this. :-)
350 cursor.cursor.data_size += 128;
351 cursor.cursor.chunk.data_size = cursor.cursor.data_size;
352 cursor.cursor.chunk.prev_chunk = cursor.cursor.chunk.next_chunk = 0;
353}
354
355static int get_cursor_command(QXLInstance *qin, struct QXLCommandExt *ext)
356{
357 Test *test = SPICE_CONTAINEROF(qin, Test, qxl_instance);
358 static int color = 0;
359 static int set = 1;
360 static int x = 0, y = 0;
361 QXLCursorCmd *cursor_cmd;
362 QXLCommandExt *cmd;
363
364 if (!test->cursor_notify) {
365 return FALSE;
366 }
367
368 test->cursor_notify--;
369 cmd = calloc(sizeof(QXLCommandExt), 1);
370 cursor_cmd = calloc(sizeof(QXLCursorCmd), 1);
371
372 cursor_cmd->release_info.id = (unsigned long)cmd;
373
374 if (set) {
375 cursor_cmd->type = QXL_CURSOR_SET;
376 cursor_cmd->u.set.position.x = 0;
377 cursor_cmd->u.set.position.y = 0;
378 cursor_cmd->u.set.visible = TRUE;
379 cursor_cmd->u.set.shape = (unsigned long)&cursor;
380 // Only a white rect (32x32) as cursor
381 memset(cursor.data, 255, sizeof(cursor.data));
382 set = 0;
383 } else {
384 cursor_cmd->type = QXL_CURSOR_MOVE;
385 cursor_cmd->u.position.x = x++ % test->primary_width;
386 cursor_cmd->u.position.y = y++ % test->primary_height;
387 }
388
389 cmd->cmd.data = (unsigned long)cursor_cmd;
390 cmd->cmd.type = QXL_CMD_CURSOR;
391 cmd->group_id = MEM_SLOT_GROUP_ID;
392 cmd->flags = 0;
393 *ext = *cmd;
394 //printf("%s\n", __func__);
395 return TRUE;
396}
397
398static int req_cursor_notification(QXLInstance *qin)
399{
400 printf("%s\n", __func__);
401 return TRUE;
402}
403
404static void notify_update(QXLInstance *qin, uint32_t update_id)
405{
406 printf("%s\n", __func__);
407}
408
409static int flush_resources(QXLInstance *qin)
410{
411 printf("%s\n", __func__);
412 return TRUE;
413}
414
415static int client_monitors_config(QXLInstance *qin,
416 VDAgentMonitorsConfig *monitors_config)
417{
418 if (!monitors_config) {
419 printf("%s: NULL monitors_config\n", __func__);
420 } else {
421 printf("%s: %d\n", __func__, monitors_config->num_of_monitors);
422 }
423 return 0;
424}
425
426static void set_client_capabilities(QXLInstance *qin,
427 uint8_t client_present,
428 uint8_t caps[58])
429{
430 Test *test = SPICE_CONTAINEROF(qin, Test, qxl_instance);
431
432 printf("%s: present %d caps %d\n", __func__, client_present, caps[0]);
433 if (test->on_client_connected && client_present) {
434 test->on_client_connected(test);
435 }
436 if (test->on_client_disconnected && !client_present) {
437 test->on_client_disconnected(test);
438 }
439}
440
6faab5d5
DM
441static int client_count = 0;
442
443static void client_connected(Test *test)
444{
445 printf("Client connected\n");
446 client_count++;
447}
448
449static void client_disconnected(Test *test)
450{
451
452 if (client_count > 0) {
453 client_count--;
454 printf("Client disconnected\n");
455 exit(0); // fixme: cleanup?
456 }
457}
458
f6cb554c
DM
459static void do_conn_timeout(void *opaque)
460{
461 Test *test = opaque;
462
463 if (client_count <= 0) {
464 printf("do_conn_timeout\n");
465 exit (0); // fixme: cleanup?
466 }
467}
468
469
cc04455b
DM
470QXLInterface display_sif = {
471 .base = {
472 .type = SPICE_INTERFACE_QXL,
473 .description = "test",
474 .major_version = SPICE_INTERFACE_QXL_MAJOR,
475 .minor_version = SPICE_INTERFACE_QXL_MINOR
476 },
477 .attache_worker = attache_worker,
478 .set_compression_level = set_compression_level,
479 .set_mm_time = set_mm_time,
480 .get_init_info = get_init_info,
481
482 /* the callbacks below are called from spice server thread context */
483 .get_command = get_command,
484 .req_cmd_notification = req_cmd_notification,
485 .release_resource = release_resource,
486 .get_cursor_command = get_cursor_command,
487 .req_cursor_notification = req_cursor_notification,
488 .notify_update = notify_update,
489 .flush_resources = flush_resources,
490 .client_monitors_config = client_monitors_config,
491 .set_client_capabilities = set_client_capabilities,
492};
493
494/* interface for tests */
495void test_add_display_interface(Test* test)
496{
497 spice_server_add_interface(test->server, &test->qxl_instance.base);
498}
499
500static int vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len)
501{
6faab5d5 502// printf("%s: %d\n", __func__, len);
cc04455b
DM
503 return len;
504}
505
506static int vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len)
507{
6faab5d5 508// printf("%s: %d\n", __func__, len);
cc04455b
DM
509 return 0;
510}
511
512static void vmc_state(SpiceCharDeviceInstance *sin, int connected)
513{
6faab5d5 514// printf("%s: %d\n", __func__, connected);
cc04455b
DM
515}
516
517static SpiceCharDeviceInterface vdagent_sif = {
518 .base.type = SPICE_INTERFACE_CHAR_DEVICE,
519 .base.description = "test spice virtual channel char device",
520 .base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR,
521 .base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR,
522 .state = vmc_state,
523 .write = vmc_write,
524 .read = vmc_read,
525};
526
527SpiceCharDeviceInstance vdagent_sin = {
528 .base = {
529 .sif = &vdagent_sif.base,
530 },
531 .subtype = "vdagent",
532};
533
534void test_add_agent_interface(SpiceServer *server)
535{
536 spice_server_add_interface(server, &vdagent_sin.base);
537}
538
8a22eb4f
DM
539static int my_charcode = 65;
540static int my_posx = 0;
474762d8
DM
541static void kbd_push_key(SpiceKbdInstance *sin, uint8_t frag)
542{
8a22eb4f
DM
543 Test *test = SPICE_CONTAINEROF(sin, Test, keyboard_sin);
544
545 printf("KEYCODE %u %p\n", frag, test);
546
547 SimpleSpiceUpdate *update;
548 update = test_draw_char(test, my_posx, 10, my_charcode);
549 my_posx++;
550 my_charcode++;
551 push_command(&update->ext);
552
553 test->qxl_worker->wakeup(test->qxl_worker);
474762d8
DM
554}
555
556static uint8_t kbd_get_leds(SpiceKbdInstance *sin)
557{
558 return 0;
559}
560
561static SpiceKbdInterface keyboard_sif = {
562 .base.type = SPICE_INTERFACE_KEYBOARD ,
563 .base.description = "spiceterm keyboard device",
564 .base.major_version = SPICE_INTERFACE_KEYBOARD_MAJOR,
565 .base.minor_version = SPICE_INTERFACE_KEYBOARD_MINOR,
566 .push_scan_freg = kbd_push_key,
567 .get_leds = kbd_get_leds,
568};
569
8a22eb4f 570void test_add_keyboard_interface(Test* test)
474762d8 571{
8a22eb4f 572 spice_server_add_interface(test->server, &test->keyboard_sin.base);
474762d8
DM
573}
574
cc04455b
DM
575void test_set_simple_command_list(Test *test, int *simple_commands, int num_commands)
576{
577 int i;
578
579 /* FIXME: leaks */
580 test->commands = malloc(sizeof(*test->commands) * num_commands);
581 memset(test->commands, 0, sizeof(*test->commands) * num_commands);
582 test->num_commands = num_commands;
583 for (i = 0 ; i < num_commands; ++i) {
584 test->commands[i].command = simple_commands[i];
585 }
586}
587
588void test_set_command_list(Test *test, Command *commands, int num_commands)
589{
590 test->commands = commands;
591 test->num_commands = num_commands;
592}
593
594
595Test *test_new(SpiceCoreInterface *core)
596{
597 int port = 5912;
598 Test *test = g_new0(Test, 1);
599 SpiceServer* server = spice_server_new();
600
6faab5d5
DM
601 test->on_client_connected = client_connected,
602 test->on_client_disconnected = client_disconnected,
603
cc04455b
DM
604 test->qxl_instance.base.sif = &display_sif.base;
605 test->qxl_instance.id = 0;
606
8a22eb4f
DM
607 test->keyboard_sin.base.sif = &keyboard_sif.base;
608
cc04455b
DM
609 test->core = core;
610 test->server = server;
8a22eb4f 611
cc04455b
DM
612 test->cursor_notify = NOTIFY_CURSOR_BATCH;
613 // some common initialization for all display tests
614 printf("TESTER: listening on port %d (unsecure)\n", port);
615 spice_server_set_port(server, port);
616 spice_server_set_noauth(server);
1d7f2da4
DM
617 int res = spice_server_init(server, core);
618 if (res != 0) {
619 g_error("spice_server_init failed, res = %d\n", res);
620 }
cc04455b
DM
621
622 cursor_init();
cc04455b 623 test->has_secondary = 0;
f6cb554c
DM
624
625 int timeout = 10; // max time to wait for client connection
626 test->conn_timeout_timer = core->timer_add(do_conn_timeout, test);
627 test->core->timer_start(test->conn_timeout_timer, timeout*1000);
628
cc04455b
DM
629 return test;
630}
631
cc04455b
DM
632
633__attribute__((noreturn))
634void usage(const char *argv0, const int exitcode)
635{
cc04455b 636
7f30e760 637 printf("usage: %s\n", argv0);
cc04455b
DM
638 exit(exitcode);
639}
640
641void spice_test_config_parse_args(int argc, char **argv)
642{
643 struct option options[] = {
7f30e760 644// {"automated-tests", no_argument, &has_automated_tests, 1},
cc04455b
DM
645 {NULL, 0, NULL, 0},
646 };
647 int option_index;
648 int val;
649
650 while ((val = getopt_long(argc, argv, "", options, &option_index)) != -1) {
651 switch (val) {
652 case '?':
653 printf("unrecognized option '%s'\n", argv[optind - 1]);
654 usage(argv[0], EXIT_FAILURE);
655 case 0:
656 break;
657 }
658 }
659
660 if (argc > optind) {
661 printf("unknown argument '%s'\n", argv[optind]);
662 usage(argv[0], EXIT_FAILURE);
663 }
cc04455b
DM
664 return;
665}