8 #include <sys/select.h>
14 #include <spice/enums.h>
15 #include <spice/macros.h>
16 #include <spice/qxl_dev.h>
18 #include "test_display_base.h"
20 #define MEM_SLOT_GROUP_ID 0
22 #define NOTIFY_DISPLAY_BATCH (SINGLE_PART/2)
23 #define NOTIFY_CURSOR_BATCH 10
25 /* Parts cribbed from spice-display.h/.c/qxl.c */
27 typedef struct SimpleSpiceUpdate
{
28 QXLCommandExt ext
; // first
34 static void test_spice_destroy_update(SimpleSpiceUpdate
*update
)
39 if (update
->drawable
.clip
.type
!= SPICE_CLIP_TYPE_NONE
) {
40 uint8_t *ptr
= (uint8_t*)update
->drawable
.clip
.data
;
43 g_free(update
->bitmap
);
47 #define DEFAULT_WIDTH 640
48 #define DEFAULT_HEIGHT 320
51 static const int angle_parts
= 64 / SINGLE_PART
;
52 static int unique
= 1;
53 static int color
= -1;
56 __attribute__((noreturn
))
57 static void sigchld_handler(int signal_num
) // wait for the child process and exit
64 static void set_cmd(QXLCommandExt
*ext
, uint32_t type
, QXLPHYSICAL data
)
69 ext
->group_id
= MEM_SLOT_GROUP_ID
;
73 static void simple_set_release_info(QXLReleaseInfo
*info
, intptr_t ptr
)
76 //info->group_id = MEM_SLOT_GROUP_ID;
79 /* bitmap are freed, so they must be allocated with g_malloc */
80 SimpleSpiceUpdate
*test_spice_create_update_from_bitmap(uint32_t surface_id
,
84 SimpleSpiceUpdate
*update
;
85 QXLDrawable
*drawable
;
89 bh
= bbox
.bottom
- bbox
.top
;
90 bw
= bbox
.right
- bbox
.left
;
92 update
= g_new0(SimpleSpiceUpdate
, 1);
93 update
->bitmap
= bitmap
;
94 drawable
= &update
->drawable
;
95 image
= &update
->image
;
97 drawable
->surface_id
= surface_id
;
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;
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
;
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
;
123 set_cmd(&update
->ext
, QXL_CMD_DRAW
, (intptr_t)drawable
);
128 static SimpleSpiceUpdate
*test_draw_char(Test
*test
, int x
, int y
, int c
)
140 // printf("DRAWCHAR %d %d %d\n", left, top, c);
147 bitmap
= dst
= g_malloc(bw
* bh
* 4);
149 unsigned char *data
= vt_font_data
+ c
*16;
150 unsigned char d
= *data
;
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;
159 for (j
= 0; j
< 16; j
++) {
160 for (i
= 0; i
< 8; i
++) {
167 *(dst
+1) = fgc_green
;
172 *(dst
+1) = bgc_green
;
181 bbox
.left
= left
; bbox
.top
= top
;
182 bbox
.right
= left
+ bw
; bbox
.bottom
= top
+ bh
;
184 return test_spice_create_update_from_bitmap(0, bbox
, bitmap
);
187 static void create_primary_surface(Test
*test
, uint32_t width
,
190 QXLWorker
*qxl_worker
= test
->qxl_worker
;
191 QXLDevSurfaceCreate surface
= { 0, };
193 g_assert(height
<= MAX_HEIGHT
);
194 g_assert(width
<= MAX_WIDTH
);
195 g_assert(height
> 0);
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 */
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
;
210 test
->height
= height
;
212 qxl_worker
->create_primary_surface(qxl_worker
, 0, &surface
);
215 QXLDevMemSlot slot
= {
216 .slot_group_id
= MEM_SLOT_GROUP_ID
,
225 static void attache_worker(QXLInstance
*qin
, QXLWorker
*_qxl_worker
)
227 Test
*test
= SPICE_CONTAINEROF(qin
, Test
, qxl_instance
);
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
);
234 printf("%s ignored, redundant\n", __func__
);
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
);
244 static void set_compression_level(QXLInstance
*qin
, int level
)
246 printf("%s\n", __func__
);
249 static void set_mm_time(QXLInstance
*qin
, uint32_t mm_time
)
252 static void get_init_info(QXLInstance
*qin
, QXLDevInitInfo
*info
)
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;
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];
273 #define COMMANDS_SIZE COUNT(commands)
275 static void push_command(QXLCommandExt
*ext
)
277 g_assert(commands_end
- commands_start
< COMMANDS_SIZE
);
278 commands
[commands_end
% COMMANDS_SIZE
] = ext
;
282 static struct QXLCommandExt
*get_simple_command(void)
284 struct QXLCommandExt
*ret
= commands
[commands_start
% COMMANDS_SIZE
];
285 g_assert(commands_start
< commands_end
);
290 static int get_num_commands(void)
292 return commands_end
- commands_start
;
295 // called from spice_server thread (i.e. red_worker thread)
296 static int get_command(QXLInstance
*qin
, struct QXLCommandExt
*ext
)
298 if (get_num_commands() == 0) {
301 *ext
= *get_simple_command();
305 static int req_cmd_notification(QXLInstance
*qin
)
307 Test
*test
= SPICE_CONTAINEROF(qin
, Test
, qxl_instance
);
309 //test->core->timer_start(test->wakeup_timer, test->wakeup_ms);
313 static void release_resource(QXLInstance
*qin
, struct QXLReleaseInfoExt release_info
)
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
) {
320 test_spice_destroy_update((void*)ext
);
322 case QXL_CMD_SURFACE
:
325 case QXL_CMD_CURSOR
: {
326 QXLCursorCmd
*cmd
= (QXLCursorCmd
*)(unsigned long)ext
->cmd
.data
;
327 if (cmd
->type
== QXL_CURSOR_SET
) {
338 #define CURSOR_WIDTH 32
339 #define CURSOR_HEIGHT 32
343 uint8_t data
[CURSOR_WIDTH
* CURSOR_HEIGHT
* 4]; // 32bit per pixel
346 static void cursor_init()
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;
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;
366 static int get_cursor_command(QXLInstance
*qin
, struct QXLCommandExt
*ext
)
368 Test
*test
= SPICE_CONTAINEROF(qin
, Test
, qxl_instance
);
369 static int color
= 0;
371 static int x
= 0, y
= 0;
372 QXLCursorCmd
*cursor_cmd
;
375 if (!test
->cursor_notify
) {
379 test
->cursor_notify
--;
380 cmd
= calloc(sizeof(QXLCommandExt
), 1);
381 cursor_cmd
= calloc(sizeof(QXLCursorCmd
), 1);
383 cursor_cmd
->release_info
.id
= (unsigned long)cmd
;
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
));
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
;
400 cmd
->cmd
.data
= (unsigned long)cursor_cmd
;
401 cmd
->cmd
.type
= QXL_CMD_CURSOR
;
402 cmd
->group_id
= MEM_SLOT_GROUP_ID
;
405 //printf("%s\n", __func__);
409 static int req_cursor_notification(QXLInstance
*qin
)
411 printf("%s\n", __func__
);
415 static void notify_update(QXLInstance
*qin
, uint32_t update_id
)
417 printf("%s\n", __func__
);
420 static int flush_resources(QXLInstance
*qin
)
422 printf("%s\n", __func__
);
426 static int client_monitors_config(QXLInstance
*qin
,
427 VDAgentMonitorsConfig
*monitors_config
)
429 if (!monitors_config
) {
430 printf("%s: NULL monitors_config\n", __func__
);
432 printf("%s: %d\n", __func__
, monitors_config
->num_of_monitors
);
437 static void set_client_capabilities(QXLInstance
*qin
,
438 uint8_t client_present
,
441 Test
*test
= SPICE_CONTAINEROF(qin
, Test
, qxl_instance
);
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
);
447 if (test
->on_client_disconnected
&& !client_present
) {
448 test
->on_client_disconnected(test
);
452 static int client_count
= 0;
454 static void client_connected(Test
*test
)
456 printf("Client connected\n");
460 static void client_disconnected(Test
*test
)
463 if (client_count
> 0) {
465 printf("Client disconnected\n");
466 exit(0); // fixme: cleanup?
470 static void do_conn_timeout(void *opaque
)
474 if (client_count
<= 0) {
475 printf("do_conn_timeout\n");
476 exit (0); // fixme: cleanup?
481 QXLInterface display_sif
= {
483 .type
= SPICE_INTERFACE_QXL
,
484 .description
= "test",
485 .major_version
= SPICE_INTERFACE_QXL_MAJOR
,
486 .minor_version
= SPICE_INTERFACE_QXL_MINOR
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
,
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
,
505 /* interface for tests */
506 void test_add_display_interface(Test
* test
)
508 spice_server_add_interface(test
->server
, &test
->qxl_instance
.base
);
511 static int vmc_write(SpiceCharDeviceInstance
*sin
, const uint8_t *buf
, int len
)
513 // printf("%s: %d\n", __func__, len);
517 static int vmc_read(SpiceCharDeviceInstance
*sin
, uint8_t *buf
, int len
)
519 // printf("%s: %d\n", __func__, len);
523 static void vmc_state(SpiceCharDeviceInstance
*sin
, int connected
)
525 // printf("%s: %d\n", __func__, connected);
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
,
538 SpiceCharDeviceInstance vdagent_sin
= {
540 .sif
= &vdagent_sif
.base
,
542 .subtype
= "vdagent",
545 void test_add_agent_interface(SpiceServer
*server
)
547 spice_server_add_interface(server
, &vdagent_sin
.base
);
550 static int my_charcode
= 65;
551 static int my_posx
= 0;
552 static void kbd_push_key(SpiceKbdInstance
*sin
, uint8_t frag
)
554 Test
*test
= SPICE_CONTAINEROF(sin
, Test
, keyboard_sin
);
556 printf("KEYCODE %u %p\n", frag
, test
);
558 SimpleSpiceUpdate
*update
;
559 update
= test_draw_char(test
, my_posx
, 10, my_charcode
);
562 push_command(&update
->ext
);
564 test
->qxl_worker
->wakeup(test
->qxl_worker
);
567 void test_draw_update_char(Test
*test
, int x
, int y
, int c
, TextAttributes attrib
)
583 // unsuported attributes = (attrib.blink || attrib.unvisible)
585 //if (attrib.uline) {
586 //rfbDrawLine (vt->screen, rx, ry + 14, rxe, ry + 14, fg);
589 SimpleSpiceUpdate
*update
;
590 update
= test_draw_char(test
, x
, y
, c
);
591 push_command(&update
->ext
);
593 test
->qxl_worker
->wakeup(test
->qxl_worker
);
596 static uint8_t kbd_get_leds(SpiceKbdInstance
*sin
)
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
,
610 void test_add_keyboard_interface(Test
* test
)
612 spice_server_add_interface(test
->server
, &test
->keyboard_sin
.base
);
615 Test
*test_new(SpiceCoreInterface
*core
)
618 Test
*test
= g_new0(Test
, 1);
619 SpiceServer
* server
= spice_server_new();
621 test
->on_client_connected
= client_connected
,
622 test
->on_client_disconnected
= client_disconnected
,
624 test
->qxl_instance
.base
.sif
= &display_sif
.base
;
625 test
->qxl_instance
.id
= 0;
627 test
->keyboard_sin
.base
.sif
= &keyboard_sif
.base
;
630 test
->server
= server
;
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
);
639 g_error("spice_server_init failed, res = %d\n", res
);
643 test
->has_secondary
= 0;
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);
653 __attribute__((noreturn
))
654 void usage(const char *argv0
, const int exitcode
)
657 printf("usage: %s\n", argv0
);
661 void spice_test_config_parse_args(int argc
, char **argv
)
663 struct option options
[] = {
664 // {"automated-tests", no_argument, &has_automated_tests, 1},
670 while ((val
= getopt_long(argc
, argv
, "", options
, &option_index
)) != -1) {
673 printf("unrecognized option '%s'\n", argv
[optind
- 1]);
674 usage(argv
[0], EXIT_FAILURE
);
681 printf("unknown argument '%s'\n", argv
[optind
]);
682 usage(argv
[0], EXIT_FAILURE
);