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 /* these colours are from linux kernel drivers/char/vt.c */
26 /* the default colour table, for VGA+ colour systems */
27 int default_red
[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
28 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
29 int default_grn
[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
30 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
31 int default_blu
[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
32 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
34 /* Parts cribbed from spice-display.h/.c/qxl.c */
36 typedef struct SimpleSpiceUpdate
{
37 QXLCommandExt ext
; // first
43 static void test_spice_destroy_update(SimpleSpiceUpdate
*update
)
48 if (update
->drawable
.clip
.type
!= SPICE_CLIP_TYPE_NONE
) {
49 uint8_t *ptr
= (uint8_t*)update
->drawable
.clip
.data
;
52 g_free(update
->bitmap
);
56 #define DEFAULT_WIDTH 640
57 #define DEFAULT_HEIGHT 320
60 static const int angle_parts
= 64 / SINGLE_PART
;
61 static int unique
= 1;
62 static int color
= -1;
65 __attribute__((noreturn
))
66 static void sigchld_handler(int signal_num
) // wait for the child process and exit
73 static void set_cmd(QXLCommandExt
*ext
, uint32_t type
, QXLPHYSICAL data
)
78 ext
->group_id
= MEM_SLOT_GROUP_ID
;
82 static void simple_set_release_info(QXLReleaseInfo
*info
, intptr_t ptr
)
85 //info->group_id = MEM_SLOT_GROUP_ID;
88 /* bitmap are freed, so they must be allocated with g_malloc */
89 SimpleSpiceUpdate
*test_spice_create_update_from_bitmap(uint32_t surface_id
,
93 SimpleSpiceUpdate
*update
;
94 QXLDrawable
*drawable
;
98 bh
= bbox
.bottom
- bbox
.top
;
99 bw
= bbox
.right
- bbox
.left
;
101 update
= g_new0(SimpleSpiceUpdate
, 1);
102 update
->bitmap
= bitmap
;
103 drawable
= &update
->drawable
;
104 image
= &update
->image
;
106 drawable
->surface_id
= surface_id
;
108 drawable
->bbox
= bbox
;
109 drawable
->clip
.type
= SPICE_CLIP_TYPE_NONE
;
110 drawable
->effect
= QXL_EFFECT_OPAQUE
;
111 simple_set_release_info(&drawable
->release_info
, (intptr_t)update
);
112 drawable
->type
= QXL_DRAW_COPY
;
113 drawable
->surfaces_dest
[0] = -1;
114 drawable
->surfaces_dest
[1] = -1;
115 drawable
->surfaces_dest
[2] = -1;
117 drawable
->u
.copy
.rop_descriptor
= SPICE_ROPD_OP_PUT
;
118 drawable
->u
.copy
.src_bitmap
= (intptr_t)image
;
119 drawable
->u
.copy
.src_area
.right
= bw
;
120 drawable
->u
.copy
.src_area
.bottom
= bh
;
122 QXL_SET_IMAGE_ID(image
, QXL_IMAGE_GROUP_DEVICE
, unique
);
123 image
->descriptor
.type
= SPICE_IMAGE_TYPE_BITMAP
;
124 image
->bitmap
.flags
= QXL_BITMAP_DIRECT
| QXL_BITMAP_TOP_DOWN
;
125 image
->bitmap
.stride
= bw
* 4;
126 image
->descriptor
.width
= image
->bitmap
.x
= bw
;
127 image
->descriptor
.height
= image
->bitmap
.y
= bh
;
128 image
->bitmap
.data
= (intptr_t)bitmap
;
129 image
->bitmap
.palette
= 0;
130 image
->bitmap
.format
= SPICE_BITMAP_FMT_32BIT
;
132 set_cmd(&update
->ext
, QXL_CMD_DRAW
, (intptr_t)drawable
);
137 static SimpleSpiceUpdate
*test_draw_char(Test
*test
, int x
, int y
, int c
, int fg
, int bg
)
149 // printf("DRAWCHAR %d %d %d\n", left, top, c);
156 bitmap
= dst
= g_malloc(bw
* bh
* 4);
158 unsigned char *data
= vt_font_data
+ c
*16;
159 unsigned char d
= *data
;
161 g_assert(fg
>= 0 && fg
< 16);
162 g_assert(bg
>= 0 && bg
< 16);
164 unsigned char fgc_red
= default_red
[fg
];
165 unsigned char fgc_blue
= default_blu
[fg
];
166 unsigned char fgc_green
= default_grn
[fg
];
167 unsigned char bgc_red
= default_red
[bg
];
168 unsigned char bgc_blue
= default_blu
[bg
];
169 unsigned char bgc_green
= default_grn
[bg
];
171 for (j
= 0; j
< 16; j
++) {
172 for (i
= 0; i
< 8; i
++) {
179 *(dst
+1) = fgc_green
;
184 *(dst
+1) = bgc_green
;
193 bbox
.left
= left
; bbox
.top
= top
;
194 bbox
.right
= left
+ bw
; bbox
.bottom
= top
+ bh
;
196 return test_spice_create_update_from_bitmap(0, bbox
, bitmap
);
199 static void create_primary_surface(Test
*test
, uint32_t width
,
202 QXLWorker
*qxl_worker
= test
->qxl_worker
;
203 QXLDevSurfaceCreate surface
= { 0, };
205 g_assert(height
<= MAX_HEIGHT
);
206 g_assert(width
<= MAX_WIDTH
);
207 g_assert(height
> 0);
210 surface
.format
= SPICE_SURFACE_FMT_32_xRGB
;
211 surface
.width
= test
->primary_width
= width
;
212 surface
.height
= test
->primary_height
= height
;
213 surface
.stride
= -width
* 4; /* negative? */
214 surface
.mouse_mode
= TRUE
; /* unused by red_worker */
216 surface
.type
= 0; /* unused by red_worker */
217 surface
.position
= 0; /* unused by red_worker */
218 surface
.mem
= (uint64_t)&test
->primary_surface
;
219 surface
.group_id
= MEM_SLOT_GROUP_ID
;
222 test
->height
= height
;
224 qxl_worker
->create_primary_surface(qxl_worker
, 0, &surface
);
227 QXLDevMemSlot slot
= {
228 .slot_group_id
= MEM_SLOT_GROUP_ID
,
237 static void attache_worker(QXLInstance
*qin
, QXLWorker
*_qxl_worker
)
239 Test
*test
= SPICE_CONTAINEROF(qin
, Test
, qxl_instance
);
241 if (test
->qxl_worker
) {
242 if (test
->qxl_worker
!= _qxl_worker
)
243 printf("%s ignored, %p is set, ignoring new %p\n", __func__
,
244 test
->qxl_worker
, _qxl_worker
);
246 printf("%s ignored, redundant\n", __func__
);
249 printf("%s\n", __func__
);
250 test
->qxl_worker
= _qxl_worker
;
251 test
->qxl_worker
->add_memslot(test
->qxl_worker
, &slot
);
252 create_primary_surface(test
, DEFAULT_WIDTH
, DEFAULT_HEIGHT
);
253 test
->qxl_worker
->start(test
->qxl_worker
);
256 static void set_compression_level(QXLInstance
*qin
, int level
)
258 printf("%s\n", __func__
);
261 static void set_mm_time(QXLInstance
*qin
, uint32_t mm_time
)
264 static void get_init_info(QXLInstance
*qin
, QXLDevInitInfo
*info
)
266 memset(info
, 0, sizeof(*info
));
267 info
->num_memslots
= 1;
268 info
->num_memslots_groups
= 1;
269 info
->memslot_id_bits
= 1;
270 info
->memslot_gen_bits
= 1;
271 info
->n_surfaces
= 1;
275 // We shall now have a ring of commands, so that we can update
276 // it from a separate thread - since get_command is called from
277 // the worker thread, and we need to sometimes do an update_area,
278 // which cannot be done from red_worker context (not via dispatcher,
279 // since you get a deadlock, and it isn't designed to be done
280 // any other way, so no point testing that).
283 static void push_command(Test
*test
, QXLCommandExt
*ext
)
285 g_mutex_lock(test
->command_mutex
);
287 while (test
->commands_end
- test
->commands_start
>= COMMANDS_SIZE
) {
288 g_cond_wait(test
->command_cond
, test
->command_mutex
);
290 g_assert(test
->commands_end
- test
->commands_start
< COMMANDS_SIZE
);
291 test
->commands
[test
->commands_end
% COMMANDS_SIZE
] = ext
;
292 test
->commands_end
++;
293 g_mutex_unlock(test
->command_mutex
);
295 test
->qxl_worker
->wakeup(test
->qxl_worker
);
298 // called from spice_server thread (i.e. red_worker thread)
299 static int get_command(QXLInstance
*qin
, struct QXLCommandExt
*ext
)
301 Test
*test
= SPICE_CONTAINEROF(qin
, Test
, qxl_instance
);
304 g_mutex_lock(test
->command_mutex
);
306 if ((test
->commands_end
- test
->commands_start
) == 0) {
311 *ext
= *test
->commands
[test
->commands_start
% COMMANDS_SIZE
];
312 g_assert(test
->commands_start
< test
->commands_end
);
313 test
->commands_start
++;
314 g_cond_signal(test
->command_cond
);
319 g_mutex_unlock(test
->command_mutex
);
323 static int req_cmd_notification(QXLInstance
*qin
)
325 Test
*test
= SPICE_CONTAINEROF(qin
, Test
, qxl_instance
);
327 //test->core->timer_start(test->wakeup_timer, test->wakeup_ms);
331 static void release_resource(QXLInstance
*qin
, struct QXLReleaseInfoExt release_info
)
333 QXLCommandExt
*ext
= (QXLCommandExt
*)(unsigned long)release_info
.info
->id
;
334 //printf("%s\n", __func__);
335 g_assert(release_info
.group_id
== MEM_SLOT_GROUP_ID
);
336 switch (ext
->cmd
.type
) {
338 test_spice_destroy_update((void*)ext
);
340 case QXL_CMD_SURFACE
:
343 case QXL_CMD_CURSOR
: {
344 QXLCursorCmd
*cmd
= (QXLCursorCmd
*)(unsigned long)ext
->cmd
.data
;
345 if (cmd
->type
== QXL_CURSOR_SET
) {
356 #define CURSOR_WIDTH 32
357 #define CURSOR_HEIGHT 32
361 uint8_t data
[CURSOR_WIDTH
* CURSOR_HEIGHT
* 4]; // 32bit per pixel
364 static void cursor_init()
366 cursor
.cursor
.header
.unique
= 0;
367 cursor
.cursor
.header
.type
= SPICE_CURSOR_TYPE_COLOR32
;
368 cursor
.cursor
.header
.width
= CURSOR_WIDTH
;
369 cursor
.cursor
.header
.height
= CURSOR_HEIGHT
;
370 cursor
.cursor
.header
.hot_spot_x
= 0;
371 cursor
.cursor
.header
.hot_spot_y
= 0;
372 cursor
.cursor
.data_size
= CURSOR_WIDTH
* CURSOR_HEIGHT
* 4;
374 // X drivers addes it to the cursor size because it could be
375 // cursor data information or another cursor related stuffs.
376 // Otherwise, the code will break in client/cursor.cpp side,
377 // that expect the data_size plus cursor information.
378 // Blame cursor protocol for this. :-)
379 cursor
.cursor
.data_size
+= 128;
380 cursor
.cursor
.chunk
.data_size
= cursor
.cursor
.data_size
;
381 cursor
.cursor
.chunk
.prev_chunk
= cursor
.cursor
.chunk
.next_chunk
= 0;
384 static int get_cursor_command(QXLInstance
*qin
, struct QXLCommandExt
*ext
)
386 Test
*test
= SPICE_CONTAINEROF(qin
, Test
, qxl_instance
);
387 static int color
= 0;
389 static int x
= 0, y
= 0;
390 QXLCursorCmd
*cursor_cmd
;
393 if (!test
->cursor_notify
) {
397 test
->cursor_notify
--;
398 cmd
= calloc(sizeof(QXLCommandExt
), 1);
399 cursor_cmd
= calloc(sizeof(QXLCursorCmd
), 1);
401 cursor_cmd
->release_info
.id
= (unsigned long)cmd
;
404 cursor_cmd
->type
= QXL_CURSOR_SET
;
405 cursor_cmd
->u
.set
.position
.x
= 0;
406 cursor_cmd
->u
.set
.position
.y
= 0;
407 cursor_cmd
->u
.set
.visible
= TRUE
;
408 cursor_cmd
->u
.set
.shape
= (unsigned long)&cursor
;
409 // Only a white rect (32x32) as cursor
410 memset(cursor
.data
, 255, sizeof(cursor
.data
));
413 cursor_cmd
->type
= QXL_CURSOR_MOVE
;
414 cursor_cmd
->u
.position
.x
= x
++ % test
->primary_width
;
415 cursor_cmd
->u
.position
.y
= y
++ % test
->primary_height
;
418 cmd
->cmd
.data
= (unsigned long)cursor_cmd
;
419 cmd
->cmd
.type
= QXL_CMD_CURSOR
;
420 cmd
->group_id
= MEM_SLOT_GROUP_ID
;
423 //printf("%s\n", __func__);
427 static int req_cursor_notification(QXLInstance
*qin
)
429 printf("%s\n", __func__
);
433 static void notify_update(QXLInstance
*qin
, uint32_t update_id
)
435 printf("%s\n", __func__
);
438 static int flush_resources(QXLInstance
*qin
)
440 printf("%s\n", __func__
);
444 static int client_monitors_config(QXLInstance
*qin
,
445 VDAgentMonitorsConfig
*monitors_config
)
447 if (!monitors_config
) {
448 printf("%s: NULL monitors_config\n", __func__
);
450 printf("%s: %d\n", __func__
, monitors_config
->num_of_monitors
);
455 static void set_client_capabilities(QXLInstance
*qin
,
456 uint8_t client_present
,
459 Test
*test
= SPICE_CONTAINEROF(qin
, Test
, qxl_instance
);
461 printf("%s: present %d caps %d\n", __func__
, client_present
, caps
[0]);
462 if (test
->on_client_connected
&& client_present
) {
463 test
->on_client_connected(test
);
465 if (test
->on_client_disconnected
&& !client_present
) {
466 test
->on_client_disconnected(test
);
470 static int client_count
= 0;
472 static void client_connected(Test
*test
)
474 printf("Client connected\n");
478 static void client_disconnected(Test
*test
)
481 if (client_count
> 0) {
483 printf("Client disconnected\n");
484 exit(0); // fixme: cleanup?
488 static void do_conn_timeout(void *opaque
)
492 if (client_count
<= 0) {
493 printf("do_conn_timeout\n");
494 exit (0); // fixme: cleanup?
499 QXLInterface display_sif
= {
501 .type
= SPICE_INTERFACE_QXL
,
502 .description
= "test",
503 .major_version
= SPICE_INTERFACE_QXL_MAJOR
,
504 .minor_version
= SPICE_INTERFACE_QXL_MINOR
506 .attache_worker
= attache_worker
,
507 .set_compression_level
= set_compression_level
,
508 .set_mm_time
= set_mm_time
,
509 .get_init_info
= get_init_info
,
511 /* the callbacks below are called from spice server thread context */
512 .get_command
= get_command
,
513 .req_cmd_notification
= req_cmd_notification
,
514 .release_resource
= release_resource
,
515 .get_cursor_command
= get_cursor_command
,
516 .req_cursor_notification
= req_cursor_notification
,
517 .notify_update
= notify_update
,
518 .flush_resources
= flush_resources
,
519 .client_monitors_config
= client_monitors_config
,
520 .set_client_capabilities
= set_client_capabilities
,
523 /* interface for tests */
524 void test_add_display_interface(Test
* test
)
526 spice_server_add_interface(test
->server
, &test
->qxl_instance
.base
);
529 static int vmc_write(SpiceCharDeviceInstance
*sin
, const uint8_t *buf
, int len
)
531 // printf("%s: %d\n", __func__, len);
535 static int vmc_read(SpiceCharDeviceInstance
*sin
, uint8_t *buf
, int len
)
537 // printf("%s: %d\n", __func__, len);
541 static void vmc_state(SpiceCharDeviceInstance
*sin
, int connected
)
543 // printf("%s: %d\n", __func__, connected);
546 static SpiceCharDeviceInterface vdagent_sif
= {
547 .base
.type
= SPICE_INTERFACE_CHAR_DEVICE
,
548 .base
.description
= "test spice virtual channel char device",
549 .base
.major_version
= SPICE_INTERFACE_CHAR_DEVICE_MAJOR
,
550 .base
.minor_version
= SPICE_INTERFACE_CHAR_DEVICE_MINOR
,
556 SpiceCharDeviceInstance vdagent_sin
= {
558 .sif
= &vdagent_sif
.base
,
560 .subtype
= "vdagent",
563 void test_add_agent_interface(SpiceServer
*server
)
565 spice_server_add_interface(server
, &vdagent_sin
.base
);
568 static int my_charcode
= 65;
569 static int my_posx
= 0;
570 static void kbd_push_key(SpiceKbdInstance
*sin
, uint8_t frag
)
572 Test
*test
= SPICE_CONTAINEROF(sin
, Test
, keyboard_sin
);
574 printf("KEYCODE %u %p\n", frag
, test
);
578 void test_draw_update_char(Test
*test
, int x
, int y
, int c
, TextAttributes attrib
)
594 // unsuported attributes = (attrib.blink || attrib.unvisible)
596 //if (attrib.uline) {
597 //rfbDrawLine (vt->screen, rx, ry + 14, rxe, ry + 14, fg);
600 SimpleSpiceUpdate
*update
;
601 update
= test_draw_char(test
, x
, y
, c
, fg
, bg
);
602 push_command(test
, &update
->ext
);
605 static uint8_t kbd_get_leds(SpiceKbdInstance
*sin
)
610 static SpiceKbdInterface keyboard_sif
= {
611 .base
.type
= SPICE_INTERFACE_KEYBOARD
,
612 .base
.description
= "spiceterm keyboard device",
613 .base
.major_version
= SPICE_INTERFACE_KEYBOARD_MAJOR
,
614 .base
.minor_version
= SPICE_INTERFACE_KEYBOARD_MINOR
,
615 .push_scan_freg
= kbd_push_key
,
616 .get_leds
= kbd_get_leds
,
619 void test_add_keyboard_interface(Test
* test
)
621 spice_server_add_interface(test
->server
, &test
->keyboard_sin
.base
);
624 Test
*test_new(SpiceCoreInterface
*core
)
627 Test
*test
= g_new0(Test
, 1);
628 SpiceServer
* server
= spice_server_new();
630 test
->command_cond
= g_cond_new();
631 test
->command_mutex
= g_mutex_new();
633 test
->on_client_connected
= client_connected
,
634 test
->on_client_disconnected
= client_disconnected
,
636 test
->qxl_instance
.base
.sif
= &display_sif
.base
;
637 test
->qxl_instance
.id
= 0;
639 test
->keyboard_sin
.base
.sif
= &keyboard_sif
.base
;
642 test
->server
= server
;
644 test
->cursor_notify
= NOTIFY_CURSOR_BATCH
;
645 // some common initialization for all display tests
646 printf("TESTER: listening on port %d (unsecure)\n", port
);
647 spice_server_set_port(server
, port
);
648 spice_server_set_noauth(server
);
649 int res
= spice_server_init(server
, core
);
651 g_error("spice_server_init failed, res = %d\n", res
);
655 test
->has_secondary
= 0;
657 int timeout
= 10; // max time to wait for client connection
658 test
->conn_timeout_timer
= core
->timer_add(do_conn_timeout
, test
);
659 test
->core
->timer_start(test
->conn_timeout_timer
, timeout
*1000);
665 __attribute__((noreturn
))
666 void usage(const char *argv0
, const int exitcode
)
669 printf("usage: %s\n", argv0
);
673 void spice_test_config_parse_args(int argc
, char **argv
)
675 struct option options
[] = {
676 // {"automated-tests", no_argument, &has_automated_tests, 1},
682 while ((val
= getopt_long(argc
, argv
, "", options
, &option_index
)) != -1) {
685 printf("unrecognized option '%s'\n", argv
[optind
- 1]);
686 usage(argv
[0], EXIT_FAILURE
);
693 printf("unknown argument '%s'\n", argv
[optind
]);
694 usage(argv
[0], EXIT_FAILURE
);