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