]>
Commit | Line | Data |
---|---|---|
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 | ||
65007123 DM |
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}; | |
33 | ||
cc04455b DM |
34 | /* Parts cribbed from spice-display.h/.c/qxl.c */ |
35 | ||
36 | typedef struct SimpleSpiceUpdate { | |
37 | QXLCommandExt ext; // first | |
38 | QXLDrawable drawable; | |
39 | QXLImage image; | |
40 | uint8_t *bitmap; | |
41 | } SimpleSpiceUpdate; | |
42 | ||
cc04455b DM |
43 | static void test_spice_destroy_update(SimpleSpiceUpdate *update) |
44 | { | |
45 | if (!update) { | |
46 | return; | |
47 | } | |
48 | if (update->drawable.clip.type != SPICE_CLIP_TYPE_NONE) { | |
49 | uint8_t *ptr = (uint8_t*)update->drawable.clip.data; | |
50 | free(ptr); | |
51 | } | |
9f9c8d83 DM |
52 | g_free(update->bitmap); |
53 | g_free(update); | |
cc04455b DM |
54 | } |
55 | ||
56 | #define DEFAULT_WIDTH 640 | |
57 | #define DEFAULT_HEIGHT 320 | |
58 | ||
abc13312 DM |
59 | //#define SINGLE_PART 4 |
60 | //static const int angle_parts = 64 / SINGLE_PART; | |
cc04455b | 61 | static int unique = 1; |
abc13312 DM |
62 | //static int color = -1; |
63 | //static int c_i = 0; | |
cc04455b | 64 | |
cc04455b DM |
65 | __attribute__((noreturn)) |
66 | static void sigchld_handler(int signal_num) // wait for the child process and exit | |
67 | { | |
68 | int status; | |
69 | wait(&status); | |
70 | exit(0); | |
71 | } | |
72 | ||
cc04455b DM |
73 | static void set_cmd(QXLCommandExt *ext, uint32_t type, QXLPHYSICAL data) |
74 | { | |
75 | ext->cmd.type = type; | |
76 | ext->cmd.data = data; | |
77 | ext->cmd.padding = 0; | |
78 | ext->group_id = MEM_SLOT_GROUP_ID; | |
79 | ext->flags = 0; | |
80 | } | |
81 | ||
82 | static void simple_set_release_info(QXLReleaseInfo *info, intptr_t ptr) | |
83 | { | |
84 | info->id = ptr; | |
85 | //info->group_id = MEM_SLOT_GROUP_ID; | |
86 | } | |
87 | ||
7b4a7650 DM |
88 | // We shall now have a ring of commands, so that we can update |
89 | // it from a separate thread - since get_command is called from | |
90 | // the worker thread, and we need to sometimes do an update_area, | |
91 | // which cannot be done from red_worker context (not via dispatcher, | |
92 | // since you get a deadlock, and it isn't designed to be done | |
93 | // any other way, so no point testing that). | |
94 | ||
95 | ||
96 | static void push_command(Test *test, QXLCommandExt *ext) | |
97 | { | |
98 | g_mutex_lock(test->command_mutex); | |
99 | ||
100 | while (test->commands_end - test->commands_start >= COMMANDS_SIZE) { | |
101 | g_cond_wait(test->command_cond, test->command_mutex); | |
102 | } | |
103 | g_assert(test->commands_end - test->commands_start < COMMANDS_SIZE); | |
104 | test->commands[test->commands_end % COMMANDS_SIZE] = ext; | |
105 | test->commands_end++; | |
106 | g_mutex_unlock(test->command_mutex); | |
107 | ||
108 | test->qxl_worker->wakeup(test->qxl_worker); | |
109 | } | |
110 | ||
9f9c8d83 | 111 | /* bitmap are freed, so they must be allocated with g_malloc */ |
cc04455b DM |
112 | SimpleSpiceUpdate *test_spice_create_update_from_bitmap(uint32_t surface_id, |
113 | QXLRect bbox, | |
4219b121 | 114 | uint8_t *bitmap) |
cc04455b DM |
115 | { |
116 | SimpleSpiceUpdate *update; | |
117 | QXLDrawable *drawable; | |
118 | QXLImage *image; | |
119 | uint32_t bw, bh; | |
120 | ||
121 | bh = bbox.bottom - bbox.top; | |
122 | bw = bbox.right - bbox.left; | |
123 | ||
9f9c8d83 | 124 | update = g_new0(SimpleSpiceUpdate, 1); |
cc04455b DM |
125 | update->bitmap = bitmap; |
126 | drawable = &update->drawable; | |
127 | image = &update->image; | |
128 | ||
129 | drawable->surface_id = surface_id; | |
130 | ||
131 | drawable->bbox = bbox; | |
4219b121 | 132 | drawable->clip.type = SPICE_CLIP_TYPE_NONE; |
cc04455b DM |
133 | drawable->effect = QXL_EFFECT_OPAQUE; |
134 | simple_set_release_info(&drawable->release_info, (intptr_t)update); | |
135 | drawable->type = QXL_DRAW_COPY; | |
136 | drawable->surfaces_dest[0] = -1; | |
137 | drawable->surfaces_dest[1] = -1; | |
138 | drawable->surfaces_dest[2] = -1; | |
139 | ||
140 | drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT; | |
141 | drawable->u.copy.src_bitmap = (intptr_t)image; | |
142 | drawable->u.copy.src_area.right = bw; | |
143 | drawable->u.copy.src_area.bottom = bh; | |
144 | ||
145 | QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, unique); | |
146 | image->descriptor.type = SPICE_IMAGE_TYPE_BITMAP; | |
147 | image->bitmap.flags = QXL_BITMAP_DIRECT | QXL_BITMAP_TOP_DOWN; | |
148 | image->bitmap.stride = bw * 4; | |
149 | image->descriptor.width = image->bitmap.x = bw; | |
150 | image->descriptor.height = image->bitmap.y = bh; | |
151 | image->bitmap.data = (intptr_t)bitmap; | |
152 | image->bitmap.palette = 0; | |
153 | image->bitmap.format = SPICE_BITMAP_FMT_32BIT; | |
154 | ||
155 | set_cmd(&update->ext, QXL_CMD_DRAW, (intptr_t)drawable); | |
156 | ||
157 | return update; | |
158 | } | |
159 | ||
65007123 | 160 | static SimpleSpiceUpdate *test_draw_char(Test *test, int x, int y, int c, int fg, int bg) |
cc04455b DM |
161 | { |
162 | int top, left; | |
163 | uint8_t *dst; | |
164 | uint8_t *bitmap; | |
165 | int bw, bh; | |
9462ba15 | 166 | int i, j; |
cc04455b DM |
167 | QXLRect bbox; |
168 | ||
8a22eb4f DM |
169 | left = x*8; |
170 | top = y*16; | |
26c64941 | 171 | |
77133eee | 172 | // printf("DRAWCHAR %d %d %d\n", left, top, c); |
8a22eb4f | 173 | |
cc04455b DM |
174 | unique++; |
175 | ||
26c64941 DM |
176 | bw = 8; |
177 | bh = 16; | |
cc04455b | 178 | |
9f9c8d83 | 179 | bitmap = dst = g_malloc(bw * bh * 4); |
cc04455b | 180 | |
9462ba15 DM |
181 | unsigned char *data = vt_font_data + c*16; |
182 | unsigned char d = *data; | |
183 | ||
65007123 DM |
184 | g_assert(fg >= 0 && fg < 16); |
185 | g_assert(bg >= 0 && bg < 16); | |
186 | ||
187 | unsigned char fgc_red = default_red[fg]; | |
188 | unsigned char fgc_blue = default_blu[fg]; | |
189 | unsigned char fgc_green = default_grn[fg]; | |
190 | unsigned char bgc_red = default_red[bg]; | |
191 | unsigned char bgc_blue = default_blu[bg]; | |
192 | unsigned char bgc_green = default_grn[bg]; | |
9f9c8d83 | 193 | |
9462ba15 DM |
194 | for (j = 0; j < 16; j++) { |
195 | for (i = 0; i < 8; i++) { | |
196 | if ((i&7) == 0) { | |
197 | d=*data; | |
198 | data++; | |
199 | } | |
200 | if (d&0x80) { | |
9f9c8d83 DM |
201 | *(dst) = fgc_blue; |
202 | *(dst+1) = fgc_green; | |
203 | *(dst+2) = fgc_red; | |
204 | *(dst+3) = 0; | |
205 | } else { | |
206 | *(dst) = bgc_blue; | |
207 | *(dst+1) = bgc_green; | |
208 | *(dst+2) = bgc_red; | |
9462ba15 DM |
209 | *(dst+3) = 0; |
210 | } | |
211 | d<<=1; | |
212 | dst += 4; | |
213 | } | |
cc04455b DM |
214 | } |
215 | ||
216 | bbox.left = left; bbox.top = top; | |
217 | bbox.right = left + bw; bbox.bottom = top + bh; | |
9462ba15 | 218 | |
8a22eb4f | 219 | return test_spice_create_update_from_bitmap(0, bbox, bitmap); |
cc04455b DM |
220 | } |
221 | ||
7b4a7650 DM |
222 | void test_spice_scroll(Test *test, int x1, int y1, int x2, int y2, int src_x, int src_y) |
223 | { | |
224 | SimpleSpiceUpdate *update; | |
225 | QXLDrawable *drawable; | |
226 | QXLRect bbox; | |
227 | ||
228 | int surface_id = 0; // fixme | |
229 | ||
230 | update = g_new0(SimpleSpiceUpdate, 1); | |
231 | drawable = &update->drawable; | |
232 | ||
233 | bbox.left = x1; | |
234 | bbox.top = y1; | |
235 | bbox.right = x2; | |
236 | bbox.bottom = y2; | |
237 | ||
238 | drawable->surface_id = surface_id; | |
239 | ||
240 | drawable->bbox = bbox; | |
241 | drawable->clip.type = SPICE_CLIP_TYPE_NONE; | |
242 | drawable->effect = QXL_EFFECT_OPAQUE; | |
243 | simple_set_release_info(&drawable->release_info, (intptr_t)update); | |
244 | drawable->type = QXL_COPY_BITS; | |
245 | drawable->surfaces_dest[0] = -1; | |
246 | drawable->surfaces_dest[1] = -1; | |
247 | drawable->surfaces_dest[2] = -1; | |
248 | ||
249 | drawable->u.copy_bits.src_pos.x = src_x; | |
250 | drawable->u.copy_bits.src_pos.y = src_y; | |
251 | ||
252 | set_cmd(&update->ext, QXL_CMD_DRAW, (intptr_t)drawable); | |
253 | ||
254 | push_command(test, &update->ext); | |
255 | } | |
256 | ||
257 | void test_spice_clear(Test *test, int x1, int y1, int x2, int y2) | |
258 | { | |
259 | SimpleSpiceUpdate *update; | |
260 | QXLDrawable *drawable; | |
261 | QXLRect bbox; | |
262 | ||
263 | int surface_id = 0; // fixme | |
264 | ||
265 | update = g_new0(SimpleSpiceUpdate, 1); | |
266 | drawable = &update->drawable; | |
267 | ||
268 | bbox.left = x1; | |
269 | bbox.top = y1; | |
270 | bbox.right = x2; | |
271 | bbox.bottom = y2; | |
272 | ||
273 | drawable->surface_id = surface_id; | |
274 | ||
275 | drawable->bbox = bbox; | |
276 | drawable->clip.type = SPICE_CLIP_TYPE_NONE; | |
277 | drawable->effect = QXL_EFFECT_OPAQUE; | |
278 | simple_set_release_info(&drawable->release_info, (intptr_t)update); | |
279 | drawable->type = QXL_DRAW_BLACKNESS; | |
280 | drawable->surfaces_dest[0] = -1; | |
281 | drawable->surfaces_dest[1] = -1; | |
282 | drawable->surfaces_dest[2] = -1; | |
283 | ||
284 | set_cmd(&update->ext, QXL_CMD_DRAW, (intptr_t)drawable); | |
285 | ||
286 | push_command(test, &update->ext); | |
287 | } | |
288 | ||
cc04455b DM |
289 | static void create_primary_surface(Test *test, uint32_t width, |
290 | uint32_t height) | |
291 | { | |
292 | QXLWorker *qxl_worker = test->qxl_worker; | |
293 | QXLDevSurfaceCreate surface = { 0, }; | |
294 | ||
295 | g_assert(height <= MAX_HEIGHT); | |
296 | g_assert(width <= MAX_WIDTH); | |
297 | g_assert(height > 0); | |
298 | g_assert(width > 0); | |
299 | ||
300 | surface.format = SPICE_SURFACE_FMT_32_xRGB; | |
301 | surface.width = test->primary_width = width; | |
302 | surface.height = test->primary_height = height; | |
303 | surface.stride = -width * 4; /* negative? */ | |
304 | surface.mouse_mode = TRUE; /* unused by red_worker */ | |
305 | surface.flags = 0; | |
306 | surface.type = 0; /* unused by red_worker */ | |
307 | surface.position = 0; /* unused by red_worker */ | |
308 | surface.mem = (uint64_t)&test->primary_surface; | |
309 | surface.group_id = MEM_SLOT_GROUP_ID; | |
310 | ||
311 | test->width = width; | |
312 | test->height = height; | |
313 | ||
314 | qxl_worker->create_primary_surface(qxl_worker, 0, &surface); | |
315 | } | |
316 | ||
317 | QXLDevMemSlot slot = { | |
318 | .slot_group_id = MEM_SLOT_GROUP_ID, | |
319 | .slot_id = 0, | |
320 | .generation = 0, | |
321 | .virt_start = 0, | |
322 | .virt_end = ~0, | |
323 | .addr_delta = 0, | |
324 | .qxl_ram_size = ~0, | |
325 | }; | |
326 | ||
327 | static void attache_worker(QXLInstance *qin, QXLWorker *_qxl_worker) | |
328 | { | |
329 | Test *test = SPICE_CONTAINEROF(qin, Test, qxl_instance); | |
330 | ||
331 | if (test->qxl_worker) { | |
332 | if (test->qxl_worker != _qxl_worker) | |
333 | printf("%s ignored, %p is set, ignoring new %p\n", __func__, | |
334 | test->qxl_worker, _qxl_worker); | |
335 | else | |
336 | printf("%s ignored, redundant\n", __func__); | |
337 | return; | |
338 | } | |
339 | printf("%s\n", __func__); | |
340 | test->qxl_worker = _qxl_worker; | |
341 | test->qxl_worker->add_memslot(test->qxl_worker, &slot); | |
342 | create_primary_surface(test, DEFAULT_WIDTH, DEFAULT_HEIGHT); | |
343 | test->qxl_worker->start(test->qxl_worker); | |
344 | } | |
345 | ||
346 | static void set_compression_level(QXLInstance *qin, int level) | |
347 | { | |
348 | printf("%s\n", __func__); | |
349 | } | |
350 | ||
351 | static void set_mm_time(QXLInstance *qin, uint32_t mm_time) | |
352 | { | |
353 | } | |
cc04455b DM |
354 | static void get_init_info(QXLInstance *qin, QXLDevInitInfo *info) |
355 | { | |
356 | memset(info, 0, sizeof(*info)); | |
357 | info->num_memslots = 1; | |
358 | info->num_memslots_groups = 1; | |
359 | info->memslot_id_bits = 1; | |
360 | info->memslot_gen_bits = 1; | |
aae93060 | 361 | info->n_surfaces = 1; |
cc04455b DM |
362 | } |
363 | ||
cc04455b DM |
364 | // called from spice_server thread (i.e. red_worker thread) |
365 | static int get_command(QXLInstance *qin, struct QXLCommandExt *ext) | |
366 | { | |
63a34f43 DM |
367 | Test *test = SPICE_CONTAINEROF(qin, Test, qxl_instance); |
368 | int res = FALSE; | |
369 | ||
370 | g_mutex_lock(test->command_mutex); | |
371 | ||
372 | if ((test->commands_end - test->commands_start) == 0) { | |
373 | res = FALSE; | |
374 | goto ret; | |
cc04455b | 375 | } |
63a34f43 DM |
376 | |
377 | *ext = *test->commands[test->commands_start % COMMANDS_SIZE]; | |
378 | g_assert(test->commands_start < test->commands_end); | |
379 | test->commands_start++; | |
380 | g_cond_signal(test->command_cond); | |
381 | ||
382 | res = TRUE; | |
383 | ||
384 | ret: | |
385 | g_mutex_unlock(test->command_mutex); | |
386 | return res; | |
cc04455b DM |
387 | } |
388 | ||
cc04455b DM |
389 | static int req_cmd_notification(QXLInstance *qin) |
390 | { | |
abc13312 | 391 | //Test *test = SPICE_CONTAINEROF(qin, Test, qxl_instance); |
8a22eb4f | 392 | //test->core->timer_start(test->wakeup_timer, test->wakeup_ms); |
abc13312 | 393 | |
cc04455b DM |
394 | return TRUE; |
395 | } | |
f6cb554c | 396 | |
cc04455b DM |
397 | static void release_resource(QXLInstance *qin, struct QXLReleaseInfoExt release_info) |
398 | { | |
399 | QXLCommandExt *ext = (QXLCommandExt*)(unsigned long)release_info.info->id; | |
400 | //printf("%s\n", __func__); | |
401 | g_assert(release_info.group_id == MEM_SLOT_GROUP_ID); | |
402 | switch (ext->cmd.type) { | |
403 | case QXL_CMD_DRAW: | |
404 | test_spice_destroy_update((void*)ext); | |
405 | break; | |
406 | case QXL_CMD_SURFACE: | |
407 | free(ext); | |
408 | break; | |
409 | case QXL_CMD_CURSOR: { | |
410 | QXLCursorCmd *cmd = (QXLCursorCmd *)(unsigned long)ext->cmd.data; | |
411 | if (cmd->type == QXL_CURSOR_SET) { | |
412 | free(cmd); | |
413 | } | |
414 | free(ext); | |
415 | break; | |
416 | } | |
417 | default: | |
418 | abort(); | |
419 | } | |
420 | } | |
421 | ||
422 | #define CURSOR_WIDTH 32 | |
423 | #define CURSOR_HEIGHT 32 | |
424 | ||
425 | static struct { | |
426 | QXLCursor cursor; | |
427 | uint8_t data[CURSOR_WIDTH * CURSOR_HEIGHT * 4]; // 32bit per pixel | |
428 | } cursor; | |
429 | ||
430 | static void cursor_init() | |
431 | { | |
432 | cursor.cursor.header.unique = 0; | |
433 | cursor.cursor.header.type = SPICE_CURSOR_TYPE_COLOR32; | |
434 | cursor.cursor.header.width = CURSOR_WIDTH; | |
435 | cursor.cursor.header.height = CURSOR_HEIGHT; | |
436 | cursor.cursor.header.hot_spot_x = 0; | |
437 | cursor.cursor.header.hot_spot_y = 0; | |
438 | cursor.cursor.data_size = CURSOR_WIDTH * CURSOR_HEIGHT * 4; | |
439 | ||
440 | // X drivers addes it to the cursor size because it could be | |
441 | // cursor data information or another cursor related stuffs. | |
442 | // Otherwise, the code will break in client/cursor.cpp side, | |
443 | // that expect the data_size plus cursor information. | |
444 | // Blame cursor protocol for this. :-) | |
445 | cursor.cursor.data_size += 128; | |
446 | cursor.cursor.chunk.data_size = cursor.cursor.data_size; | |
447 | cursor.cursor.chunk.prev_chunk = cursor.cursor.chunk.next_chunk = 0; | |
448 | } | |
449 | ||
450 | static int get_cursor_command(QXLInstance *qin, struct QXLCommandExt *ext) | |
451 | { | |
452 | Test *test = SPICE_CONTAINEROF(qin, Test, qxl_instance); | |
cc04455b DM |
453 | static int set = 1; |
454 | static int x = 0, y = 0; | |
455 | QXLCursorCmd *cursor_cmd; | |
456 | QXLCommandExt *cmd; | |
457 | ||
458 | if (!test->cursor_notify) { | |
459 | return FALSE; | |
460 | } | |
461 | ||
462 | test->cursor_notify--; | |
463 | cmd = calloc(sizeof(QXLCommandExt), 1); | |
464 | cursor_cmd = calloc(sizeof(QXLCursorCmd), 1); | |
465 | ||
466 | cursor_cmd->release_info.id = (unsigned long)cmd; | |
467 | ||
468 | if (set) { | |
469 | cursor_cmd->type = QXL_CURSOR_SET; | |
470 | cursor_cmd->u.set.position.x = 0; | |
471 | cursor_cmd->u.set.position.y = 0; | |
472 | cursor_cmd->u.set.visible = TRUE; | |
473 | cursor_cmd->u.set.shape = (unsigned long)&cursor; | |
474 | // Only a white rect (32x32) as cursor | |
475 | memset(cursor.data, 255, sizeof(cursor.data)); | |
476 | set = 0; | |
477 | } else { | |
478 | cursor_cmd->type = QXL_CURSOR_MOVE; | |
479 | cursor_cmd->u.position.x = x++ % test->primary_width; | |
480 | cursor_cmd->u.position.y = y++ % test->primary_height; | |
481 | } | |
482 | ||
483 | cmd->cmd.data = (unsigned long)cursor_cmd; | |
484 | cmd->cmd.type = QXL_CMD_CURSOR; | |
485 | cmd->group_id = MEM_SLOT_GROUP_ID; | |
486 | cmd->flags = 0; | |
487 | *ext = *cmd; | |
488 | //printf("%s\n", __func__); | |
489 | return TRUE; | |
490 | } | |
491 | ||
492 | static int req_cursor_notification(QXLInstance *qin) | |
493 | { | |
494 | printf("%s\n", __func__); | |
495 | return TRUE; | |
496 | } | |
497 | ||
498 | static void notify_update(QXLInstance *qin, uint32_t update_id) | |
499 | { | |
500 | printf("%s\n", __func__); | |
501 | } | |
502 | ||
503 | static int flush_resources(QXLInstance *qin) | |
504 | { | |
505 | printf("%s\n", __func__); | |
506 | return TRUE; | |
507 | } | |
508 | ||
509 | static int client_monitors_config(QXLInstance *qin, | |
510 | VDAgentMonitorsConfig *monitors_config) | |
511 | { | |
512 | if (!monitors_config) { | |
513 | printf("%s: NULL monitors_config\n", __func__); | |
514 | } else { | |
515 | printf("%s: %d\n", __func__, monitors_config->num_of_monitors); | |
516 | } | |
517 | return 0; | |
518 | } | |
519 | ||
520 | static void set_client_capabilities(QXLInstance *qin, | |
521 | uint8_t client_present, | |
522 | uint8_t caps[58]) | |
523 | { | |
524 | Test *test = SPICE_CONTAINEROF(qin, Test, qxl_instance); | |
525 | ||
526 | printf("%s: present %d caps %d\n", __func__, client_present, caps[0]); | |
527 | if (test->on_client_connected && client_present) { | |
528 | test->on_client_connected(test); | |
529 | } | |
530 | if (test->on_client_disconnected && !client_present) { | |
531 | test->on_client_disconnected(test); | |
532 | } | |
533 | } | |
534 | ||
6faab5d5 DM |
535 | static int client_count = 0; |
536 | ||
537 | static void client_connected(Test *test) | |
538 | { | |
539 | printf("Client connected\n"); | |
540 | client_count++; | |
541 | } | |
542 | ||
543 | static void client_disconnected(Test *test) | |
544 | { | |
545 | ||
546 | if (client_count > 0) { | |
547 | client_count--; | |
548 | printf("Client disconnected\n"); | |
549 | exit(0); // fixme: cleanup? | |
550 | } | |
551 | } | |
552 | ||
f6cb554c DM |
553 | static void do_conn_timeout(void *opaque) |
554 | { | |
abc13312 | 555 | // Test *test = opaque; |
f6cb554c DM |
556 | |
557 | if (client_count <= 0) { | |
558 | printf("do_conn_timeout\n"); | |
559 | exit (0); // fixme: cleanup? | |
560 | } | |
561 | } | |
562 | ||
563 | ||
cc04455b DM |
564 | QXLInterface display_sif = { |
565 | .base = { | |
566 | .type = SPICE_INTERFACE_QXL, | |
567 | .description = "test", | |
568 | .major_version = SPICE_INTERFACE_QXL_MAJOR, | |
569 | .minor_version = SPICE_INTERFACE_QXL_MINOR | |
570 | }, | |
571 | .attache_worker = attache_worker, | |
572 | .set_compression_level = set_compression_level, | |
573 | .set_mm_time = set_mm_time, | |
574 | .get_init_info = get_init_info, | |
575 | ||
576 | /* the callbacks below are called from spice server thread context */ | |
577 | .get_command = get_command, | |
578 | .req_cmd_notification = req_cmd_notification, | |
579 | .release_resource = release_resource, | |
580 | .get_cursor_command = get_cursor_command, | |
581 | .req_cursor_notification = req_cursor_notification, | |
582 | .notify_update = notify_update, | |
583 | .flush_resources = flush_resources, | |
584 | .client_monitors_config = client_monitors_config, | |
585 | .set_client_capabilities = set_client_capabilities, | |
586 | }; | |
587 | ||
588 | /* interface for tests */ | |
589 | void test_add_display_interface(Test* test) | |
590 | { | |
591 | spice_server_add_interface(test->server, &test->qxl_instance.base); | |
592 | } | |
593 | ||
594 | static int vmc_write(SpiceCharDeviceInstance *sin, const uint8_t *buf, int len) | |
595 | { | |
6faab5d5 | 596 | // printf("%s: %d\n", __func__, len); |
cc04455b DM |
597 | return len; |
598 | } | |
599 | ||
600 | static int vmc_read(SpiceCharDeviceInstance *sin, uint8_t *buf, int len) | |
601 | { | |
6faab5d5 | 602 | // printf("%s: %d\n", __func__, len); |
cc04455b DM |
603 | return 0; |
604 | } | |
605 | ||
606 | static void vmc_state(SpiceCharDeviceInstance *sin, int connected) | |
607 | { | |
6faab5d5 | 608 | // printf("%s: %d\n", __func__, connected); |
cc04455b DM |
609 | } |
610 | ||
611 | static SpiceCharDeviceInterface vdagent_sif = { | |
612 | .base.type = SPICE_INTERFACE_CHAR_DEVICE, | |
613 | .base.description = "test spice virtual channel char device", | |
614 | .base.major_version = SPICE_INTERFACE_CHAR_DEVICE_MAJOR, | |
615 | .base.minor_version = SPICE_INTERFACE_CHAR_DEVICE_MINOR, | |
616 | .state = vmc_state, | |
617 | .write = vmc_write, | |
618 | .read = vmc_read, | |
619 | }; | |
620 | ||
621 | SpiceCharDeviceInstance vdagent_sin = { | |
622 | .base = { | |
623 | .sif = &vdagent_sif.base, | |
624 | }, | |
625 | .subtype = "vdagent", | |
626 | }; | |
627 | ||
628 | void test_add_agent_interface(SpiceServer *server) | |
629 | { | |
630 | spice_server_add_interface(server, &vdagent_sin.base); | |
631 | } | |
632 | ||
474762d8 DM |
633 | static void kbd_push_key(SpiceKbdInstance *sin, uint8_t frag) |
634 | { | |
8a22eb4f DM |
635 | Test *test = SPICE_CONTAINEROF(sin, Test, keyboard_sin); |
636 | ||
637 | printf("KEYCODE %u %p\n", frag, test); | |
638 | ||
474762d8 DM |
639 | } |
640 | ||
abc13312 | 641 | void test_draw_update_char(Test *test, int x, int y, gunichar ch, TextAttributes attrib) |
22e5ba02 DM |
642 | { |
643 | int fg, bg; | |
644 | ||
645 | if (attrib.invers) { | |
646 | bg = attrib.fgcol; | |
647 | fg = attrib.bgcol; | |
648 | } else { | |
649 | bg = attrib.bgcol; | |
650 | fg = attrib.fgcol; | |
651 | } | |
652 | ||
653 | if (attrib.bold) { | |
654 | fg += 8; | |
655 | } | |
656 | ||
657 | // unsuported attributes = (attrib.blink || attrib.unvisible) | |
658 | ||
659 | //if (attrib.uline) { | |
660 | //rfbDrawLine (vt->screen, rx, ry + 14, rxe, ry + 14, fg); | |
661 | //} | |
662 | ||
abc13312 DM |
663 | int c = vt_fontmap[ch]; |
664 | ||
22e5ba02 | 665 | SimpleSpiceUpdate *update; |
65007123 | 666 | update = test_draw_char(test, x, y, c, fg, bg); |
63a34f43 | 667 | push_command(test, &update->ext); |
22e5ba02 DM |
668 | } |
669 | ||
474762d8 DM |
670 | static uint8_t kbd_get_leds(SpiceKbdInstance *sin) |
671 | { | |
672 | return 0; | |
673 | } | |
674 | ||
675 | static SpiceKbdInterface keyboard_sif = { | |
676 | .base.type = SPICE_INTERFACE_KEYBOARD , | |
677 | .base.description = "spiceterm keyboard device", | |
678 | .base.major_version = SPICE_INTERFACE_KEYBOARD_MAJOR, | |
679 | .base.minor_version = SPICE_INTERFACE_KEYBOARD_MINOR, | |
680 | .push_scan_freg = kbd_push_key, | |
681 | .get_leds = kbd_get_leds, | |
682 | }; | |
683 | ||
8a22eb4f | 684 | void test_add_keyboard_interface(Test* test) |
474762d8 | 685 | { |
8a22eb4f | 686 | spice_server_add_interface(test->server, &test->keyboard_sin.base); |
474762d8 DM |
687 | } |
688 | ||
cc04455b DM |
689 | Test *test_new(SpiceCoreInterface *core) |
690 | { | |
691 | int port = 5912; | |
692 | Test *test = g_new0(Test, 1); | |
693 | SpiceServer* server = spice_server_new(); | |
694 | ||
63a34f43 DM |
695 | test->command_cond = g_cond_new(); |
696 | test->command_mutex = g_mutex_new(); | |
697 | ||
6faab5d5 DM |
698 | test->on_client_connected = client_connected, |
699 | test->on_client_disconnected = client_disconnected, | |
700 | ||
cc04455b DM |
701 | test->qxl_instance.base.sif = &display_sif.base; |
702 | test->qxl_instance.id = 0; | |
703 | ||
8a22eb4f DM |
704 | test->keyboard_sin.base.sif = &keyboard_sif.base; |
705 | ||
cc04455b DM |
706 | test->core = core; |
707 | test->server = server; | |
8a22eb4f | 708 | |
cc04455b DM |
709 | test->cursor_notify = NOTIFY_CURSOR_BATCH; |
710 | // some common initialization for all display tests | |
711 | printf("TESTER: listening on port %d (unsecure)\n", port); | |
712 | spice_server_set_port(server, port); | |
713 | spice_server_set_noauth(server); | |
1d7f2da4 DM |
714 | int res = spice_server_init(server, core); |
715 | if (res != 0) { | |
716 | g_error("spice_server_init failed, res = %d\n", res); | |
717 | } | |
cc04455b DM |
718 | |
719 | cursor_init(); | |
f6cb554c DM |
720 | |
721 | int timeout = 10; // max time to wait for client connection | |
722 | test->conn_timeout_timer = core->timer_add(do_conn_timeout, test); | |
723 | test->core->timer_start(test->conn_timeout_timer, timeout*1000); | |
724 | ||
cc04455b DM |
725 | return test; |
726 | } | |
727 | ||
cc04455b DM |
728 | |
729 | __attribute__((noreturn)) | |
730 | void usage(const char *argv0, const int exitcode) | |
731 | { | |
cc04455b | 732 | |
7f30e760 | 733 | printf("usage: %s\n", argv0); |
cc04455b DM |
734 | exit(exitcode); |
735 | } | |
736 | ||
737 | void spice_test_config_parse_args(int argc, char **argv) | |
738 | { | |
739 | struct option options[] = { | |
7f30e760 | 740 | // {"automated-tests", no_argument, &has_automated_tests, 1}, |
cc04455b DM |
741 | {NULL, 0, NULL, 0}, |
742 | }; | |
743 | int option_index; | |
744 | int val; | |
745 | ||
746 | while ((val = getopt_long(argc, argv, "", options, &option_index)) != -1) { | |
747 | switch (val) { | |
748 | case '?': | |
749 | printf("unrecognized option '%s'\n", argv[optind - 1]); | |
750 | usage(argv[0], EXIT_FAILURE); | |
751 | case 0: | |
752 | break; | |
753 | } | |
754 | } | |
755 | ||
756 | if (argc > optind) { | |
757 | printf("unknown argument '%s'\n", argv[optind]); | |
758 | usage(argv[0], EXIT_FAILURE); | |
759 | } | |
cc04455b DM |
760 | return; |
761 | } |