]> git.proxmox.com Git - spiceterm.git/blame - screen.c
remove unneeded assertion
[spiceterm.git] / screen.c
CommitLineData
4ef70811
DM
1/*
2
3 Copyright (C) 2013 Proxmox Server Solutions GmbH
4
5 Copyright: spiceterm is under GNU GPL, the GNU General Public License.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; version 2 dated June, 1991.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20
21 Author: Dietmar Maurer <dietmar@proxmox.com>
22
23 Note: qlx drawing code is copied from spice-server test code.
24
25*/
26
cc04455b
DM
27#include <stdlib.h>
28#include <math.h>
29#include <string.h>
30#include <stdio.h>
31#include <unistd.h>
32#include <signal.h>
33#include <wait.h>
34#include <sys/select.h>
35#include <sys/types.h>
36#include <getopt.h>
37
38#include <spice.h>
474762d8 39#include <spice/enums.h>
cc04455b
DM
40#include <spice/macros.h>
41#include <spice/qxl_dev.h>
60b9ef63 42#include <spice/vd_agent.h>
cc04455b 43
e3759a34
DM
44#include "glyphs.h"
45
46#include "spiceterm.h"
cc04455b 47
a0579497
DM
48static int debug = 0;
49
50#define DPRINTF(x, format, ...) { \
51 if (x <= debug) { \
52 printf("%s: " format "\n" , __FUNCTION__, ## __VA_ARGS__); \
53 } \
54}
55
cc04455b
DM
56#define MEM_SLOT_GROUP_ID 0
57
58#define NOTIFY_DISPLAY_BATCH (SINGLE_PART/2)
59#define NOTIFY_CURSOR_BATCH 10
60
65007123
DM
61/* these colours are from linux kernel drivers/char/vt.c */
62/* the default colour table, for VGA+ colour systems */
63int default_red[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
64 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
65int default_grn[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
66 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
67int default_blu[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
68 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
69
cc04455b
DM
70/* Parts cribbed from spice-display.h/.c/qxl.c */
71
72typedef struct SimpleSpiceUpdate {
e70e45a9 73 QXLCommandExt ext; // needs to be first member
cc04455b
DM
74 QXLDrawable drawable;
75 QXLImage image;
76 uint8_t *bitmap;
e70e45a9 77 int cache_id; // do not free bitmap if cache_id != 0
cc04455b
DM
78} SimpleSpiceUpdate;
79
64bc7a2f
DM
80static void
81spice_screen_destroy_update(SimpleSpiceUpdate *update)
cc04455b
DM
82{
83 if (!update) {
84 return;
85 }
86 if (update->drawable.clip.type != SPICE_CLIP_TYPE_NONE) {
87 uint8_t *ptr = (uint8_t*)update->drawable.clip.data;
88 free(ptr);
89 }
e70e45a9
DM
90 if (update->bitmap && !update->cache_id) {
91 g_free(update->bitmap);
92 }
93
9f9c8d83 94 g_free(update);
cc04455b
DM
95}
96
e9a6b86c 97static int unique = 0x0ffff + 1;
cc04455b 98
64bc7a2f
DM
99static void
100set_cmd(QXLCommandExt *ext, uint32_t type, QXLPHYSICAL data)
cc04455b
DM
101{
102 ext->cmd.type = type;
103 ext->cmd.data = data;
104 ext->cmd.padding = 0;
105 ext->group_id = MEM_SLOT_GROUP_ID;
106 ext->flags = 0;
107}
108
64bc7a2f
DM
109static void
110simple_set_release_info(QXLReleaseInfo *info, intptr_t ptr)
cc04455b
DM
111{
112 info->id = ptr;
113 //info->group_id = MEM_SLOT_GROUP_ID;
114}
115
64bc7a2f 116/* Note: push_command/get_command are called from different threads */
7b4a7650 117
64bc7a2f
DM
118static void
119push_command(SpiceScreen *spice_screen, QXLCommandExt *ext)
7b4a7650 120{
30930d41
DM
121 int need_wakeup = 1;
122
64bc7a2f 123 g_mutex_lock(spice_screen->command_mutex);
7b4a7650 124
64bc7a2f
DM
125 while (spice_screen->commands_end - spice_screen->commands_start >= COMMANDS_SIZE) {
126 g_cond_wait(spice_screen->command_cond, spice_screen->command_mutex);
7b4a7650 127 }
7b4a7650 128
64bc7a2f
DM
129 g_assert(spice_screen->commands_end - spice_screen->commands_start < COMMANDS_SIZE);
130
30930d41
DM
131 if ((spice_screen->commands_end - spice_screen->commands_start) > 0) {
132 need_wakeup = 0;
133 }
134
64bc7a2f
DM
135 spice_screen->commands[spice_screen->commands_end % COMMANDS_SIZE] = ext;
136 spice_screen->commands_end++;
137
30930d41
DM
138 if (need_wakeup) {
139 spice_screen->qxl_worker->wakeup(spice_screen->qxl_worker);
140 }
141
64bc7a2f
DM
142 g_mutex_unlock(spice_screen->command_mutex);
143
7b4a7650
DM
144}
145
9f9c8d83 146/* bitmap are freed, so they must be allocated with g_malloc */
64bc7a2f 147static SimpleSpiceUpdate *
e9a6b86c 148spice_screen_update_from_bitmap_cmd(uint32_t surface_id, QXLRect bbox, uint8_t *bitmap, int cache_id)
cc04455b
DM
149{
150 SimpleSpiceUpdate *update;
151 QXLDrawable *drawable;
152 QXLImage *image;
153 uint32_t bw, bh;
154
155 bh = bbox.bottom - bbox.top;
156 bw = bbox.right - bbox.left;
157
9f9c8d83 158 update = g_new0(SimpleSpiceUpdate, 1);
cc04455b
DM
159 update->bitmap = bitmap;
160 drawable = &update->drawable;
161 image = &update->image;
162
163 drawable->surface_id = surface_id;
164
165 drawable->bbox = bbox;
4219b121 166 drawable->clip.type = SPICE_CLIP_TYPE_NONE;
cc04455b
DM
167 drawable->effect = QXL_EFFECT_OPAQUE;
168 simple_set_release_info(&drawable->release_info, (intptr_t)update);
169 drawable->type = QXL_DRAW_COPY;
170 drawable->surfaces_dest[0] = -1;
171 drawable->surfaces_dest[1] = -1;
172 drawable->surfaces_dest[2] = -1;
173
174 drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
175 drawable->u.copy.src_bitmap = (intptr_t)image;
176 drawable->u.copy.src_area.right = bw;
177 drawable->u.copy.src_area.bottom = bh;
178
e9a6b86c
DM
179 if (cache_id) {
180 QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, cache_id);
62e6a86b 181 image->descriptor.flags = SPICE_IMAGE_FLAGS_CACHE_ME;
e70e45a9 182 update->cache_id = cache_id;
e9a6b86c
DM
183 } else {
184 QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, ++unique);
185 }
cc04455b
DM
186 image->descriptor.type = SPICE_IMAGE_TYPE_BITMAP;
187 image->bitmap.flags = QXL_BITMAP_DIRECT | QXL_BITMAP_TOP_DOWN;
188 image->bitmap.stride = bw * 4;
189 image->descriptor.width = image->bitmap.x = bw;
190 image->descriptor.height = image->bitmap.y = bh;
191 image->bitmap.data = (intptr_t)bitmap;
192 image->bitmap.palette = 0;
193 image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
194
195 set_cmd(&update->ext, QXL_CMD_DRAW, (intptr_t)drawable);
196
197 return update;
198}
199
64bc7a2f
DM
200static SimpleSpiceUpdate *
201spice_screen_draw_char_cmd(SpiceScreen *spice_screen, int x, int y, int c,
b1f67c20 202 int fg, int bg, gboolean uline)
cc04455b
DM
203{
204 int top, left;
205 uint8_t *dst;
e70e45a9 206 uint8_t *bitmap = NULL;
cc04455b 207 int bw, bh;
9462ba15 208 int i, j;
cc04455b 209 QXLRect bbox;
e9a6b86c 210 int cache_id = 0;
e70e45a9 211 CachedImage *ce;
e9a6b86c 212
b1f67c20 213 if (!uline && c < 256) {
e9a6b86c 214 cache_id = ((fg << 12) | (bg << 8) | (c & 255)) & 0x0ffff;
e70e45a9
DM
215 if ((ce = (CachedImage *)g_hash_table_lookup(spice_screen->image_cache, &cache_id))) {
216 bitmap = ce->bitmap;
217 }
e9a6b86c 218 }
cc04455b 219
8a22eb4f
DM
220 left = x*8;
221 top = y*16;
8a22eb4f 222
26c64941
DM
223 bw = 8;
224 bh = 16;
cc04455b 225
e70e45a9
DM
226 if (!bitmap) {
227 bitmap = dst = g_malloc(bw * bh * 4);
228
229 unsigned char *data = vt_font_data + c*16;
230 unsigned char d = *data;
231
232 g_assert(fg >= 0 && fg < 16);
233 g_assert(bg >= 0 && bg < 16);
234
235 unsigned char fgc_red = default_red[fg];
236 unsigned char fgc_blue = default_blu[fg];
237 unsigned char fgc_green = default_grn[fg];
238 unsigned char bgc_red = default_red[bg];
239 unsigned char bgc_blue = default_blu[bg];
240 unsigned char bgc_green = default_grn[bg];
241
242 for (j = 0; j < 16; j++) {
243 gboolean ul = (j == 14) && uline;
244 for (i = 0; i < 8; i++) {
245 if (i == 0) {
246 d=*data;
247 data++;
248 }
249 if (ul || d&0x80) {
250 *(dst) = fgc_blue;
251 *(dst+1) = fgc_green;
252 *(dst+2) = fgc_red;
253 *(dst+3) = 0;
254 } else {
255 *(dst) = bgc_blue;
256 *(dst+1) = bgc_green;
257 *(dst+2) = bgc_red;
258 *(dst+3) = 0;
259 }
260 d<<=1;
261 dst += 4;
9462ba15 262 }
9462ba15 263 }
e70e45a9
DM
264 ce = g_new(CachedImage, 1);
265 ce->cache_id = cache_id;
266 ce->bitmap = bitmap;
267 g_hash_table_insert(spice_screen->image_cache, &ce->cache_id, ce);
cc04455b
DM
268 }
269
270 bbox.left = left; bbox.top = top;
271 bbox.right = left + bw; bbox.bottom = top + bh;
9462ba15 272
e9a6b86c 273 return spice_screen_update_from_bitmap_cmd(0, bbox, bitmap, cache_id);
cc04455b
DM
274}
275
64bc7a2f
DM
276void
277spice_screen_scroll(SpiceScreen *spice_screen, int x1, int y1,
278 int x2, int y2, int src_x, int src_y)
7b4a7650
DM
279{
280 SimpleSpiceUpdate *update;
281 QXLDrawable *drawable;
282 QXLRect bbox;
283
64bc7a2f 284 int surface_id = 0;
7b4a7650
DM
285
286 update = g_new0(SimpleSpiceUpdate, 1);
287 drawable = &update->drawable;
288
289 bbox.left = x1;
290 bbox.top = y1;
291 bbox.right = x2;
292 bbox.bottom = y2;
293
294 drawable->surface_id = surface_id;
295
296 drawable->bbox = bbox;
297 drawable->clip.type = SPICE_CLIP_TYPE_NONE;
298 drawable->effect = QXL_EFFECT_OPAQUE;
299 simple_set_release_info(&drawable->release_info, (intptr_t)update);
300 drawable->type = QXL_COPY_BITS;
301 drawable->surfaces_dest[0] = -1;
302 drawable->surfaces_dest[1] = -1;
303 drawable->surfaces_dest[2] = -1;
304
305 drawable->u.copy_bits.src_pos.x = src_x;
306 drawable->u.copy_bits.src_pos.y = src_y;
307
308 set_cmd(&update->ext, QXL_CMD_DRAW, (intptr_t)drawable);
309
64bc7a2f 310 push_command(spice_screen, &update->ext);
7b4a7650
DM
311}
312
64bc7a2f
DM
313void
314spice_screen_clear(SpiceScreen *spice_screen, int x1, int y1, int x2, int y2)
7b4a7650
DM
315{
316 SimpleSpiceUpdate *update;
317 QXLDrawable *drawable;
318 QXLRect bbox;
319
64bc7a2f 320 int surface_id = 0;
7b4a7650
DM
321
322 update = g_new0(SimpleSpiceUpdate, 1);
323 drawable = &update->drawable;
324
325 bbox.left = x1;
326 bbox.top = y1;
327 bbox.right = x2;
328 bbox.bottom = y2;
329
330 drawable->surface_id = surface_id;
331
332 drawable->bbox = bbox;
333 drawable->clip.type = SPICE_CLIP_TYPE_NONE;
334 drawable->effect = QXL_EFFECT_OPAQUE;
335 simple_set_release_info(&drawable->release_info, (intptr_t)update);
336 drawable->type = QXL_DRAW_BLACKNESS;
337 drawable->surfaces_dest[0] = -1;
338 drawable->surfaces_dest[1] = -1;
339 drawable->surfaces_dest[2] = -1;
340
341 set_cmd(&update->ext, QXL_CMD_DRAW, (intptr_t)drawable);
342
64bc7a2f 343 push_command(spice_screen, &update->ext);
7b4a7650
DM
344}
345
64bc7a2f
DM
346static void
347create_primary_surface(SpiceScreen *spice_screen, uint32_t width,
348 uint32_t height)
cc04455b 349{
64bc7a2f 350 QXLWorker *qxl_worker = spice_screen->qxl_worker;
cc04455b
DM
351 QXLDevSurfaceCreate surface = { 0, };
352
cc04455b
DM
353 g_assert(height > 0);
354 g_assert(width > 0);
355
ead260d7
DM
356 if (height > MAX_HEIGHT)
357 height = MAX_HEIGHT;
358
359 if (width > MAX_WIDTH)
360 width = MAX_WIDTH;
361
362
cc04455b 363 surface.format = SPICE_SURFACE_FMT_32_xRGB;
64bc7a2f
DM
364 surface.width = spice_screen->primary_width = width;
365 surface.height = spice_screen->primary_height = height;
cc04455b
DM
366 surface.stride = -width * 4; /* negative? */
367 surface.mouse_mode = TRUE; /* unused by red_worker */
368 surface.flags = 0;
369 surface.type = 0; /* unused by red_worker */
370 surface.position = 0; /* unused by red_worker */
64bc7a2f 371 surface.mem = (uint64_t)&spice_screen->primary_surface;
cc04455b
DM
372 surface.group_id = MEM_SLOT_GROUP_ID;
373
64bc7a2f
DM
374 spice_screen->width = width;
375 spice_screen->height = height;
cc04455b
DM
376
377 qxl_worker->create_primary_surface(qxl_worker, 0, &surface);
378}
379
8f53ae81
DM
380void
381spice_screen_resize(SpiceScreen *spice_screen, uint32_t width,
382 uint32_t height)
383{
384 QXLWorker *qxl_worker = spice_screen->qxl_worker;
385
386 if (spice_screen->width == width && spice_screen->height == height) {
387 return;
388 }
389
390 qxl_worker->destroy_primary_surface(qxl_worker, 0);
391
392 create_primary_surface(spice_screen, width, height);
393}
394
395
cc04455b 396QXLDevMemSlot slot = {
64bc7a2f
DM
397 .slot_group_id = MEM_SLOT_GROUP_ID,
398 .slot_id = 0,
399 .generation = 0,
400 .virt_start = 0,
401 .virt_end = ~0,
402 .addr_delta = 0,
403 .qxl_ram_size = ~0,
cc04455b
DM
404};
405
64bc7a2f
DM
406static void
407attache_worker(QXLInstance *qin, QXLWorker *_qxl_worker)
cc04455b 408{
64bc7a2f 409 SpiceScreen *spice_screen = SPICE_CONTAINEROF(qin, SpiceScreen, qxl_instance);
cc04455b 410
64bc7a2f
DM
411 if (spice_screen->qxl_worker) {
412 g_assert_not_reached();
cc04455b 413 }
64bc7a2f
DM
414
415 spice_screen->qxl_worker = _qxl_worker;
416 spice_screen->qxl_worker->add_memslot(spice_screen->qxl_worker, &slot);
8f53ae81 417 create_primary_surface(spice_screen, spice_screen->width, spice_screen->height);
64bc7a2f 418 spice_screen->qxl_worker->start(spice_screen->qxl_worker);
cc04455b
DM
419}
420
64bc7a2f
DM
421static void
422set_compression_level(QXLInstance *qin, int level)
cc04455b 423{
64bc7a2f 424 /* not used */
cc04455b
DM
425}
426
64bc7a2f
DM
427static void
428set_mm_time(QXLInstance *qin, uint32_t mm_time)
cc04455b 429{
64bc7a2f 430 /* not used */
cc04455b 431}
64bc7a2f
DM
432
433static void
434get_init_info(QXLInstance *qin, QXLDevInitInfo *info)
cc04455b
DM
435{
436 memset(info, 0, sizeof(*info));
437 info->num_memslots = 1;
438 info->num_memslots_groups = 1;
439 info->memslot_id_bits = 1;
440 info->memslot_gen_bits = 1;
aae93060 441 info->n_surfaces = 1;
cc04455b
DM
442}
443
64bc7a2f
DM
444/* called from spice_server thread (i.e. red_worker thread) */
445static int
446get_command(QXLInstance *qin, struct QXLCommandExt *ext)
cc04455b 447{
64bc7a2f 448 SpiceScreen *spice_screen = SPICE_CONTAINEROF(qin, SpiceScreen, qxl_instance);
63a34f43
DM
449 int res = FALSE;
450
64bc7a2f 451 g_mutex_lock(spice_screen->command_mutex);
63a34f43 452
64bc7a2f 453 if ((spice_screen->commands_end - spice_screen->commands_start) == 0) {
63a34f43
DM
454 res = FALSE;
455 goto ret;
cc04455b 456 }
63a34f43 457
64bc7a2f
DM
458 *ext = *spice_screen->commands[spice_screen->commands_start % COMMANDS_SIZE];
459 g_assert(spice_screen->commands_start < spice_screen->commands_end);
460 spice_screen->commands_start++;
461 g_cond_signal(spice_screen->command_cond);
63a34f43
DM
462
463 res = TRUE;
464
465ret:
64bc7a2f 466 g_mutex_unlock(spice_screen->command_mutex);
63a34f43 467 return res;
cc04455b
DM
468}
469
64bc7a2f
DM
470static int
471req_cmd_notification(QXLInstance *qin)
cc04455b 472{
64bc7a2f
DM
473 //SpiceScreen *spice_screen = SPICE_CONTAINEROF(qin, SpiceScreen, qxl_instance);
474 //spice_screen->core->timer_start(spice_screen->wakeup_timer, spice_screen->wakeup_ms);
abc13312 475
cc04455b
DM
476 return TRUE;
477}
f6cb554c 478
64bc7a2f
DM
479static void
480release_resource(QXLInstance *qin, struct QXLReleaseInfoExt release_info)
cc04455b
DM
481{
482 QXLCommandExt *ext = (QXLCommandExt*)(unsigned long)release_info.info->id;
64bc7a2f 483
cc04455b
DM
484 g_assert(release_info.group_id == MEM_SLOT_GROUP_ID);
485 switch (ext->cmd.type) {
486 case QXL_CMD_DRAW:
64bc7a2f 487 spice_screen_destroy_update((void*)ext);
cc04455b
DM
488 break;
489 case QXL_CMD_SURFACE:
490 free(ext);
491 break;
492 case QXL_CMD_CURSOR: {
493 QXLCursorCmd *cmd = (QXLCursorCmd *)(unsigned long)ext->cmd.data;
494 if (cmd->type == QXL_CURSOR_SET) {
495 free(cmd);
496 }
497 free(ext);
498 break;
499 }
500 default:
501 abort();
502 }
503}
504
64bc7a2f
DM
505#define CURSOR_WIDTH 8
506#define CURSOR_HEIGHT 16
cc04455b
DM
507
508static struct {
509 QXLCursor cursor;
510 uint8_t data[CURSOR_WIDTH * CURSOR_HEIGHT * 4]; // 32bit per pixel
511} cursor;
512
64bc7a2f
DM
513static void
514cursor_init()
cc04455b
DM
515{
516 cursor.cursor.header.unique = 0;
517 cursor.cursor.header.type = SPICE_CURSOR_TYPE_COLOR32;
518 cursor.cursor.header.width = CURSOR_WIDTH;
519 cursor.cursor.header.height = CURSOR_HEIGHT;
520 cursor.cursor.header.hot_spot_x = 0;
521 cursor.cursor.header.hot_spot_y = 0;
522 cursor.cursor.data_size = CURSOR_WIDTH * CURSOR_HEIGHT * 4;
523
524 // X drivers addes it to the cursor size because it could be
525 // cursor data information or another cursor related stuffs.
526 // Otherwise, the code will break in client/cursor.cpp side,
527 // that expect the data_size plus cursor information.
528 // Blame cursor protocol for this. :-)
529 cursor.cursor.data_size += 128;
530 cursor.cursor.chunk.data_size = cursor.cursor.data_size;
531 cursor.cursor.chunk.prev_chunk = cursor.cursor.chunk.next_chunk = 0;
532}
533
64bc7a2f
DM
534static int
535get_cursor_command(QXLInstance *qin, struct QXLCommandExt *ext)
cc04455b 536{
64bc7a2f 537 SpiceScreen *spice_screen = SPICE_CONTAINEROF(qin, SpiceScreen, qxl_instance);
cc04455b
DM
538 static int set = 1;
539 static int x = 0, y = 0;
540 QXLCursorCmd *cursor_cmd;
541 QXLCommandExt *cmd;
542
64bc7a2f 543 if (!spice_screen->cursor_notify) {
cc04455b
DM
544 return FALSE;
545 }
546
64bc7a2f 547 spice_screen->cursor_notify--;
cc04455b
DM
548 cmd = calloc(sizeof(QXLCommandExt), 1);
549 cursor_cmd = calloc(sizeof(QXLCursorCmd), 1);
550
551 cursor_cmd->release_info.id = (unsigned long)cmd;
552
553 if (set) {
554 cursor_cmd->type = QXL_CURSOR_SET;
555 cursor_cmd->u.set.position.x = 0;
556 cursor_cmd->u.set.position.y = 0;
557 cursor_cmd->u.set.visible = TRUE;
558 cursor_cmd->u.set.shape = (unsigned long)&cursor;
64bc7a2f 559 // white rect as cursor
cc04455b
DM
560 memset(cursor.data, 255, sizeof(cursor.data));
561 set = 0;
562 } else {
563 cursor_cmd->type = QXL_CURSOR_MOVE;
64bc7a2f
DM
564 cursor_cmd->u.position.x = x++ % spice_screen->primary_width;
565 cursor_cmd->u.position.y = y++ % spice_screen->primary_height;
cc04455b
DM
566 }
567
568 cmd->cmd.data = (unsigned long)cursor_cmd;
569 cmd->cmd.type = QXL_CMD_CURSOR;
570 cmd->group_id = MEM_SLOT_GROUP_ID;
571 cmd->flags = 0;
572 *ext = *cmd;
64bc7a2f 573
cc04455b
DM
574 return TRUE;
575}
576
64bc7a2f
DM
577static int
578req_cursor_notification(QXLInstance *qin)
cc04455b 579{
64bc7a2f
DM
580 /* not used */
581
cc04455b
DM
582 return TRUE;
583}
584
64bc7a2f
DM
585static void
586notify_update(QXLInstance *qin, uint32_t update_id)
cc04455b 587{
64bc7a2f 588 /* not used */
cc04455b
DM
589}
590
64bc7a2f
DM
591static int
592flush_resources(QXLInstance *qin)
cc04455b 593{
64bc7a2f
DM
594 /* not used */
595
cc04455b
DM
596 return TRUE;
597}
598
64bc7a2f
DM
599static int
600client_monitors_config(QXLInstance *qin, VDAgentMonitorsConfig *monitors_config)
cc04455b 601{
64bc7a2f
DM
602 /* not used */
603
cc04455b
DM
604 return 0;
605}
606
64bc7a2f
DM
607static void
608set_client_capabilities(QXLInstance *qin, uint8_t client_present,
609 uint8_t caps[58])
cc04455b 610{
64bc7a2f 611 SpiceScreen *spice_screen = SPICE_CONTAINEROF(qin, SpiceScreen, qxl_instance);
cc04455b 612
a3fd8291 613 DPRINTF(1, "present %d caps %d", client_present, caps[0]);
64bc7a2f
DM
614
615 if (spice_screen->on_client_connected && client_present) {
616 spice_screen->on_client_connected(spice_screen);
cc04455b 617 }
64bc7a2f
DM
618 if (spice_screen->on_client_disconnected && !client_present) {
619 spice_screen->on_client_disconnected(spice_screen);
cc04455b
DM
620 }
621}
622
6faab5d5
DM
623static int client_count = 0;
624
64bc7a2f
DM
625static void
626client_connected(SpiceScreen *spice_screen)
6faab5d5 627{
6faab5d5 628 client_count++;
a0579497 629
a3fd8291 630 DPRINTF(1, "client_count = %d", client_count);
6faab5d5
DM
631}
632
64bc7a2f
DM
633static void
634client_disconnected(SpiceScreen *spice_screen)
6faab5d5 635{
6faab5d5
DM
636 if (client_count > 0) {
637 client_count--;
a3fd8291 638 DPRINTF(1, "client_count = %d", client_count);
6faab5d5
DM
639 exit(0); // fixme: cleanup?
640 }
641}
642
64bc7a2f
DM
643static void
644do_conn_timeout(void *opaque)
f6cb554c 645{
64bc7a2f 646 // SpiceScreen *spice_screen = opaque;
f6cb554c
DM
647
648 if (client_count <= 0) {
a0579497 649 printf("connection timeout - stopping server\n");
f6cb554c
DM
650 exit (0); // fixme: cleanup?
651 }
652}
653
cc04455b
DM
654QXLInterface display_sif = {
655 .base = {
656 .type = SPICE_INTERFACE_QXL,
64bc7a2f 657 .description = "spiceterm display server",
cc04455b
DM
658 .major_version = SPICE_INTERFACE_QXL_MAJOR,
659 .minor_version = SPICE_INTERFACE_QXL_MINOR
660 },
661 .attache_worker = attache_worker,
662 .set_compression_level = set_compression_level,
663 .set_mm_time = set_mm_time,
664 .get_init_info = get_init_info,
665
666 /* the callbacks below are called from spice server thread context */
667 .get_command = get_command,
668 .req_cmd_notification = req_cmd_notification,
669 .release_resource = release_resource,
670 .get_cursor_command = get_cursor_command,
671 .req_cursor_notification = req_cursor_notification,
672 .notify_update = notify_update,
673 .flush_resources = flush_resources,
674 .client_monitors_config = client_monitors_config,
675 .set_client_capabilities = set_client_capabilities,
676};
677
cc04455b 678
64bc7a2f 679void
1d11aa12
DM
680spice_screen_draw_char(SpiceScreen *spice_screen, int x, int y, gunichar2 ch,
681 TextAttributes attrib)
22e5ba02
DM
682{
683 int fg, bg;
684
1d11aa12 685 int invers;
22e5ba02 686 if (attrib.invers) {
1d11aa12
DM
687 invers = attrib.selected ? 0 : 1;
688 } else {
689 invers = attrib.selected ? 1 : 0;
690 }
691
692 if (invers) {
22e5ba02
DM
693 bg = attrib.fgcol;
694 fg = attrib.bgcol;
695 } else {
696 bg = attrib.bgcol;
697 fg = attrib.fgcol;
698 }
699
700 if (attrib.bold) {
701 fg += 8;
702 }
703
704 // unsuported attributes = (attrib.blink || attrib.unvisible)
705
abc13312
DM
706 int c = vt_fontmap[ch];
707
22e5ba02 708 SimpleSpiceUpdate *update;
b1f67c20 709 update = spice_screen_draw_char_cmd(spice_screen, x, y, c, fg, bg, attrib.uline);
64bc7a2f 710 push_command(spice_screen, &update->ext);
22e5ba02
DM
711}
712
64bc7a2f 713SpiceScreen *
8f53ae81 714spice_screen_new(SpiceCoreInterface *core, uint32_t width, uint32_t height, guint timeout)
cc04455b
DM
715{
716 int port = 5912;
64bc7a2f 717 SpiceScreen *spice_screen = g_new0(SpiceScreen, 1);
cc04455b
DM
718 SpiceServer* server = spice_server_new();
719
8f53ae81
DM
720 spice_screen->width = width;
721 spice_screen->height = height;
722
64bc7a2f
DM
723 spice_screen->command_cond = g_cond_new();
724 spice_screen->command_mutex = g_mutex_new();
63a34f43 725
64bc7a2f
DM
726 spice_screen->on_client_connected = client_connected,
727 spice_screen->on_client_disconnected = client_disconnected,
6faab5d5 728
64bc7a2f
DM
729 spice_screen->qxl_instance.base.sif = &display_sif.base;
730 spice_screen->qxl_instance.id = 0;
cc04455b 731
64bc7a2f
DM
732 spice_screen->core = core;
733 spice_screen->server = server;
734
735 spice_screen->cursor_notify = NOTIFY_CURSOR_BATCH;
736
737 printf("listening on port %d (unsecure)\n", port);
8a22eb4f 738
cc04455b
DM
739 spice_server_set_port(server, port);
740 spice_server_set_noauth(server);
64bc7a2f 741
1d7f2da4
DM
742 int res = spice_server_init(server, core);
743 if (res != 0) {
744 g_error("spice_server_init failed, res = %d\n", res);
745 }
cc04455b
DM
746
747 cursor_init();
f6cb554c 748
64bc7a2f
DM
749 spice_screen->conn_timeout_timer = core->timer_add(do_conn_timeout, spice_screen);
750 spice_screen->core->timer_start(spice_screen->conn_timeout_timer, timeout*1000);
f6cb554c 751
38e3e912
DM
752 spice_server_add_interface(spice_screen->server, &spice_screen->qxl_instance.base);
753
64bc7a2f 754 return spice_screen;
cc04455b 755}