]> git.proxmox.com Git - spiceterm.git/blame - screen.c
drop SASL support allowing using Debians libspice-server
[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
5508c4fd
DC
58
59extern unsigned char color_table[];
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
c783f726
DM
97static void
98release_qxl_command_ext(QXLCommandExt *ext)
99{
100 g_assert(ext != NULL);
101
102 switch (ext->cmd.type) {
103 case QXL_CMD_DRAW:
104 spice_screen_destroy_update((void*)ext);
105 break;
106 case QXL_CMD_SURFACE:
107 free(ext);
108 break;
109 case QXL_CMD_CURSOR: {
110 QXLCursorCmd *cmd = (QXLCursorCmd *)(unsigned long)ext->cmd.data;
111 if (cmd->type == QXL_CURSOR_SET) {
112 free(cmd);
113 }
114 free(ext);
115 break;
116 }
117 default:
118 abort();
119 }
120}
121
122static void
123release_resource(QXLInstance *qin, struct QXLReleaseInfoExt release_info)
124{
125 QXLCommandExt *ext = (QXLCommandExt*)(unsigned long)release_info.info->id;
126
127 g_assert(release_info.group_id == MEM_SLOT_GROUP_ID);
128 release_qxl_command_ext(ext);
129}
130
e9a6b86c 131static int unique = 0x0ffff + 1;
cc04455b 132
64bc7a2f
DM
133static void
134set_cmd(QXLCommandExt *ext, uint32_t type, QXLPHYSICAL data)
cc04455b
DM
135{
136 ext->cmd.type = type;
137 ext->cmd.data = data;
138 ext->cmd.padding = 0;
139 ext->group_id = MEM_SLOT_GROUP_ID;
140 ext->flags = 0;
141}
142
64bc7a2f
DM
143static void
144simple_set_release_info(QXLReleaseInfo *info, intptr_t ptr)
cc04455b
DM
145{
146 info->id = ptr;
147 //info->group_id = MEM_SLOT_GROUP_ID;
148}
149
64bc7a2f 150/* Note: push_command/get_command are called from different threads */
7b4a7650 151
64bc7a2f
DM
152static void
153push_command(SpiceScreen *spice_screen, QXLCommandExt *ext)
7b4a7650 154{
30930d41
DM
155 int need_wakeup = 1;
156
b52b9534 157 g_mutex_lock(&spice_screen->command_mutex);
7b4a7650 158
64bc7a2f 159 while (spice_screen->commands_end - spice_screen->commands_start >= COMMANDS_SIZE) {
b52b9534 160 g_cond_wait(&spice_screen->command_cond, &spice_screen->command_mutex);
7b4a7650 161 }
7b4a7650 162
64bc7a2f
DM
163 g_assert(spice_screen->commands_end - spice_screen->commands_start < COMMANDS_SIZE);
164
30930d41
DM
165 if ((spice_screen->commands_end - spice_screen->commands_start) > 0) {
166 need_wakeup = 0;
167 }
168
64bc7a2f
DM
169 spice_screen->commands[spice_screen->commands_end % COMMANDS_SIZE] = ext;
170 spice_screen->commands_end++;
171
30930d41 172 if (need_wakeup) {
b52b9534
DM
173 spice_qxl_wakeup(&spice_screen->qxl_instance);
174 //spice_screen->qxl_worker->wakeup(spice_screen->qxl_worker);
30930d41
DM
175 }
176
b52b9534 177 g_mutex_unlock(&spice_screen->command_mutex);
64bc7a2f 178
7b4a7650
DM
179}
180
9f9c8d83 181/* bitmap are freed, so they must be allocated with g_malloc */
64bc7a2f 182static SimpleSpiceUpdate *
e9a6b86c 183spice_screen_update_from_bitmap_cmd(uint32_t surface_id, QXLRect bbox, uint8_t *bitmap, int cache_id)
cc04455b
DM
184{
185 SimpleSpiceUpdate *update;
186 QXLDrawable *drawable;
187 QXLImage *image;
188 uint32_t bw, bh;
189
190 bh = bbox.bottom - bbox.top;
191 bw = bbox.right - bbox.left;
192
9f9c8d83 193 update = g_new0(SimpleSpiceUpdate, 1);
cc04455b
DM
194 update->bitmap = bitmap;
195 drawable = &update->drawable;
196 image = &update->image;
197
198 drawable->surface_id = surface_id;
199
200 drawable->bbox = bbox;
4219b121 201 drawable->clip.type = SPICE_CLIP_TYPE_NONE;
cc04455b
DM
202 drawable->effect = QXL_EFFECT_OPAQUE;
203 simple_set_release_info(&drawable->release_info, (intptr_t)update);
204 drawable->type = QXL_DRAW_COPY;
205 drawable->surfaces_dest[0] = -1;
206 drawable->surfaces_dest[1] = -1;
207 drawable->surfaces_dest[2] = -1;
208
209 drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
210 drawable->u.copy.src_bitmap = (intptr_t)image;
211 drawable->u.copy.src_area.right = bw;
212 drawable->u.copy.src_area.bottom = bh;
213
e9a6b86c
DM
214 if (cache_id) {
215 QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, cache_id);
62e6a86b 216 image->descriptor.flags = SPICE_IMAGE_FLAGS_CACHE_ME;
e70e45a9 217 update->cache_id = cache_id;
e9a6b86c
DM
218 } else {
219 QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, ++unique);
220 }
cc04455b
DM
221 image->descriptor.type = SPICE_IMAGE_TYPE_BITMAP;
222 image->bitmap.flags = QXL_BITMAP_DIRECT | QXL_BITMAP_TOP_DOWN;
223 image->bitmap.stride = bw * 4;
224 image->descriptor.width = image->bitmap.x = bw;
225 image->descriptor.height = image->bitmap.y = bh;
226 image->bitmap.data = (intptr_t)bitmap;
227 image->bitmap.palette = 0;
228 image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
229
230 set_cmd(&update->ext, QXL_CMD_DRAW, (intptr_t)drawable);
231
232 return update;
233}
234
64bc7a2f
DM
235static SimpleSpiceUpdate *
236spice_screen_draw_char_cmd(SpiceScreen *spice_screen, int x, int y, int c,
b1f67c20 237 int fg, int bg, gboolean uline)
cc04455b
DM
238{
239 int top, left;
240 uint8_t *dst;
e70e45a9 241 uint8_t *bitmap = NULL;
cc04455b 242 int bw, bh;
9462ba15 243 int i, j;
cc04455b 244 QXLRect bbox;
e9a6b86c 245 int cache_id = 0;
e70e45a9 246 CachedImage *ce;
e9a6b86c 247
b1f67c20 248 if (!uline && c < 256) {
e9a6b86c 249 cache_id = ((fg << 12) | (bg << 8) | (c & 255)) & 0x0ffff;
e70e45a9
DM
250 if ((ce = (CachedImage *)g_hash_table_lookup(spice_screen->image_cache, &cache_id))) {
251 bitmap = ce->bitmap;
252 }
e9a6b86c 253 }
cc04455b 254
8a22eb4f
DM
255 left = x*8;
256 top = y*16;
8a22eb4f 257
26c64941
DM
258 bw = 8;
259 bh = 16;
cc04455b 260
e70e45a9
DM
261 if (!bitmap) {
262 bitmap = dst = g_malloc(bw * bh * 4);
263
264 unsigned char *data = vt_font_data + c*16;
265 unsigned char d = *data;
266
267 g_assert(fg >= 0 && fg < 16);
268 g_assert(bg >= 0 && bg < 16);
269
5508c4fd
DC
270 unsigned char fgc_red = default_red[color_table[fg]];
271 unsigned char fgc_blue = default_blu[color_table[fg]];
272 unsigned char fgc_green = default_grn[color_table[fg]];
273 unsigned char bgc_red = default_red[color_table[bg]];
274 unsigned char bgc_blue = default_blu[color_table[bg]];
275 unsigned char bgc_green = default_grn[color_table[bg]];
e70e45a9
DM
276
277 for (j = 0; j < 16; j++) {
278 gboolean ul = (j == 14) && uline;
279 for (i = 0; i < 8; i++) {
280 if (i == 0) {
281 d=*data;
282 data++;
283 }
284 if (ul || d&0x80) {
285 *(dst) = fgc_blue;
286 *(dst+1) = fgc_green;
287 *(dst+2) = fgc_red;
288 *(dst+3) = 0;
289 } else {
290 *(dst) = bgc_blue;
291 *(dst+1) = bgc_green;
292 *(dst+2) = bgc_red;
293 *(dst+3) = 0;
294 }
295 d<<=1;
296 dst += 4;
9462ba15 297 }
9462ba15 298 }
ffeedb04
DC
299
300 if (cache_id != 0) {
301 ce = g_new(CachedImage, 1);
302 ce->cache_id = cache_id;
303 ce->bitmap = bitmap;
304 g_hash_table_insert(spice_screen->image_cache, &ce->cache_id, ce);
305 }
cc04455b
DM
306 }
307
308 bbox.left = left; bbox.top = top;
309 bbox.right = left + bw; bbox.bottom = top + bh;
9462ba15 310
e9a6b86c 311 return spice_screen_update_from_bitmap_cmd(0, bbox, bitmap, cache_id);
cc04455b
DM
312}
313
64bc7a2f
DM
314void
315spice_screen_scroll(SpiceScreen *spice_screen, int x1, int y1,
316 int x2, int y2, int src_x, int src_y)
7b4a7650
DM
317{
318 SimpleSpiceUpdate *update;
319 QXLDrawable *drawable;
320 QXLRect bbox;
321
64bc7a2f 322 int surface_id = 0;
7b4a7650
DM
323
324 update = g_new0(SimpleSpiceUpdate, 1);
325 drawable = &update->drawable;
326
327 bbox.left = x1;
328 bbox.top = y1;
329 bbox.right = x2;
330 bbox.bottom = y2;
331
332 drawable->surface_id = surface_id;
333
334 drawable->bbox = bbox;
335 drawable->clip.type = SPICE_CLIP_TYPE_NONE;
336 drawable->effect = QXL_EFFECT_OPAQUE;
337 simple_set_release_info(&drawable->release_info, (intptr_t)update);
338 drawable->type = QXL_COPY_BITS;
339 drawable->surfaces_dest[0] = -1;
340 drawable->surfaces_dest[1] = -1;
341 drawable->surfaces_dest[2] = -1;
342
343 drawable->u.copy_bits.src_pos.x = src_x;
344 drawable->u.copy_bits.src_pos.y = src_y;
345
346 set_cmd(&update->ext, QXL_CMD_DRAW, (intptr_t)drawable);
347
64bc7a2f 348 push_command(spice_screen, &update->ext);
7b4a7650
DM
349}
350
64bc7a2f
DM
351void
352spice_screen_clear(SpiceScreen *spice_screen, int x1, int y1, int x2, int y2)
7b4a7650
DM
353{
354 SimpleSpiceUpdate *update;
355 QXLDrawable *drawable;
356 QXLRect bbox;
357
64bc7a2f 358 int surface_id = 0;
7b4a7650
DM
359
360 update = g_new0(SimpleSpiceUpdate, 1);
361 drawable = &update->drawable;
362
363 bbox.left = x1;
364 bbox.top = y1;
365 bbox.right = x2;
366 bbox.bottom = y2;
367
368 drawable->surface_id = surface_id;
369
370 drawable->bbox = bbox;
371 drawable->clip.type = SPICE_CLIP_TYPE_NONE;
372 drawable->effect = QXL_EFFECT_OPAQUE;
373 simple_set_release_info(&drawable->release_info, (intptr_t)update);
374 drawable->type = QXL_DRAW_BLACKNESS;
375 drawable->surfaces_dest[0] = -1;
376 drawable->surfaces_dest[1] = -1;
377 drawable->surfaces_dest[2] = -1;
378
379 set_cmd(&update->ext, QXL_CMD_DRAW, (intptr_t)drawable);
380
64bc7a2f 381 push_command(spice_screen, &update->ext);
7b4a7650
DM
382}
383
64bc7a2f
DM
384static void
385create_primary_surface(SpiceScreen *spice_screen, uint32_t width,
386 uint32_t height)
cc04455b 387{
cc04455b
DM
388 QXLDevSurfaceCreate surface = { 0, };
389
cc04455b
DM
390 g_assert(height > 0);
391 g_assert(width > 0);
392
ead260d7
DM
393 if (height > MAX_HEIGHT)
394 height = MAX_HEIGHT;
395
396 if (width > MAX_WIDTH)
397 width = MAX_WIDTH;
398
cc04455b 399 surface.format = SPICE_SURFACE_FMT_32_xRGB;
64bc7a2f
DM
400 surface.width = spice_screen->primary_width = width;
401 surface.height = spice_screen->primary_height = height;
cc04455b
DM
402 surface.stride = -width * 4; /* negative? */
403 surface.mouse_mode = TRUE; /* unused by red_worker */
404 surface.flags = 0;
405 surface.type = 0; /* unused by red_worker */
406 surface.position = 0; /* unused by red_worker */
64bc7a2f 407 surface.mem = (uint64_t)&spice_screen->primary_surface;
cc04455b
DM
408 surface.group_id = MEM_SLOT_GROUP_ID;
409
64bc7a2f
DM
410 spice_screen->width = width;
411 spice_screen->height = height;
cc04455b 412
6508981f
DM
413 spice_screen->cursor_set = 0;
414
b52b9534 415 spice_qxl_create_primary_surface(&spice_screen->qxl_instance, 0, &surface);
cc04455b
DM
416}
417
418QXLDevMemSlot slot = {
64bc7a2f
DM
419 .slot_group_id = MEM_SLOT_GROUP_ID,
420 .slot_id = 0,
421 .generation = 0,
422 .virt_start = 0,
423 .virt_end = ~0,
424 .addr_delta = 0,
425 .qxl_ram_size = ~0,
cc04455b
DM
426};
427
64bc7a2f
DM
428static void
429attache_worker(QXLInstance *qin, QXLWorker *_qxl_worker)
cc04455b 430{
64bc7a2f 431 SpiceScreen *spice_screen = SPICE_CONTAINEROF(qin, SpiceScreen, qxl_instance);
cc04455b 432
64bc7a2f
DM
433 if (spice_screen->qxl_worker) {
434 g_assert_not_reached();
cc04455b 435 }
64bc7a2f
DM
436
437 spice_screen->qxl_worker = _qxl_worker;
b52b9534 438 spice_qxl_add_memslot(&spice_screen->qxl_instance, &slot);
8f53ae81 439 create_primary_surface(spice_screen, spice_screen->width, spice_screen->height);
b52b9534 440 spice_server_vm_start(spice_screen->server);
cc04455b
DM
441}
442
64bc7a2f
DM
443static void
444set_compression_level(QXLInstance *qin, int level)
cc04455b 445{
64bc7a2f 446 /* not used */
cc04455b
DM
447}
448
64bc7a2f
DM
449static void
450set_mm_time(QXLInstance *qin, uint32_t mm_time)
cc04455b 451{
64bc7a2f 452 /* not used */
cc04455b 453}
64bc7a2f
DM
454
455static void
456get_init_info(QXLInstance *qin, QXLDevInitInfo *info)
cc04455b
DM
457{
458 memset(info, 0, sizeof(*info));
459 info->num_memslots = 1;
460 info->num_memslots_groups = 1;
461 info->memslot_id_bits = 1;
462 info->memslot_gen_bits = 1;
aae93060 463 info->n_surfaces = 1;
cc04455b
DM
464}
465
64bc7a2f
DM
466/* called from spice_server thread (i.e. red_worker thread) */
467static int
468get_command(QXLInstance *qin, struct QXLCommandExt *ext)
cc04455b 469{
64bc7a2f 470 SpiceScreen *spice_screen = SPICE_CONTAINEROF(qin, SpiceScreen, qxl_instance);
63a34f43
DM
471 int res = FALSE;
472
b52b9534 473 g_mutex_lock(&spice_screen->command_mutex);
63a34f43 474
64bc7a2f 475 if ((spice_screen->commands_end - spice_screen->commands_start) == 0) {
63a34f43
DM
476 res = FALSE;
477 goto ret;
cc04455b 478 }
63a34f43 479
64bc7a2f
DM
480 *ext = *spice_screen->commands[spice_screen->commands_start % COMMANDS_SIZE];
481 g_assert(spice_screen->commands_start < spice_screen->commands_end);
482 spice_screen->commands_start++;
b52b9534 483 g_cond_signal(&spice_screen->command_cond);
63a34f43
DM
484
485 res = TRUE;
486
487ret:
b52b9534 488 g_mutex_unlock(&spice_screen->command_mutex);
63a34f43 489 return res;
cc04455b
DM
490}
491
c783f726
DM
492void
493discard_pending_commands(SpiceScreen *spice_screen)
494{
495 int pos;
496
b52b9534 497 g_mutex_lock(&spice_screen->command_mutex);
c783f726
DM
498 for (pos = spice_screen->commands_start; pos < spice_screen->commands_end; pos++) {
499 release_qxl_command_ext(spice_screen->commands[pos % COMMANDS_SIZE]);
500 }
501 spice_screen->commands_start = spice_screen->commands_end;
b52b9534 502 g_mutex_unlock(&spice_screen->command_mutex);
c783f726
DM
503}
504
64bc7a2f
DM
505static int
506req_cmd_notification(QXLInstance *qin)
cc04455b 507{
64bc7a2f
DM
508 //SpiceScreen *spice_screen = SPICE_CONTAINEROF(qin, SpiceScreen, qxl_instance);
509 //spice_screen->core->timer_start(spice_screen->wakeup_timer, spice_screen->wakeup_ms);
abc13312 510
cc04455b
DM
511 return TRUE;
512}
f6cb554c 513
64bc7a2f
DM
514#define CURSOR_WIDTH 8
515#define CURSOR_HEIGHT 16
cc04455b
DM
516
517static struct {
518 QXLCursor cursor;
519 uint8_t data[CURSOR_WIDTH * CURSOR_HEIGHT * 4]; // 32bit per pixel
520} cursor;
521
64bc7a2f
DM
522static void
523cursor_init()
cc04455b
DM
524{
525 cursor.cursor.header.unique = 0;
526 cursor.cursor.header.type = SPICE_CURSOR_TYPE_COLOR32;
527 cursor.cursor.header.width = CURSOR_WIDTH;
528 cursor.cursor.header.height = CURSOR_HEIGHT;
529 cursor.cursor.header.hot_spot_x = 0;
530 cursor.cursor.header.hot_spot_y = 0;
531 cursor.cursor.data_size = CURSOR_WIDTH * CURSOR_HEIGHT * 4;
532
533 // X drivers addes it to the cursor size because it could be
534 // cursor data information or another cursor related stuffs.
535 // Otherwise, the code will break in client/cursor.cpp side,
536 // that expect the data_size plus cursor information.
537 // Blame cursor protocol for this. :-)
538 cursor.cursor.data_size += 128;
539 cursor.cursor.chunk.data_size = cursor.cursor.data_size;
540 cursor.cursor.chunk.prev_chunk = cursor.cursor.chunk.next_chunk = 0;
541}
542
64bc7a2f
DM
543static int
544get_cursor_command(QXLInstance *qin, struct QXLCommandExt *ext)
cc04455b 545{
6508981f 546 SpiceScreen *spice_screen = SPICE_CONTAINEROF(qin, SpiceScreen, qxl_instance);
622c777f 547
cc04455b
DM
548 QXLCursorCmd *cursor_cmd;
549 QXLCommandExt *cmd;
550
6508981f
DM
551 if (spice_screen->cursor_set)
552 return FALSE;
553
554 spice_screen->cursor_set = 1;
622c777f 555
cc04455b
DM
556 cmd = calloc(sizeof(QXLCommandExt), 1);
557 cursor_cmd = calloc(sizeof(QXLCursorCmd), 1);
558
559 cursor_cmd->release_info.id = (unsigned long)cmd;
560
622c777f
DM
561 cursor_cmd->type = QXL_CURSOR_SET;
562 cursor_cmd->u.set.position.x = 0;
563 cursor_cmd->u.set.position.y = 0;
564 cursor_cmd->u.set.visible = TRUE;
565 cursor_cmd->u.set.shape = (unsigned long)&cursor;
566 // white rect as cursor
567 memset(cursor.data, 255, sizeof(cursor.data));
cc04455b
DM
568
569 cmd->cmd.data = (unsigned long)cursor_cmd;
570 cmd->cmd.type = QXL_CMD_CURSOR;
571 cmd->group_id = MEM_SLOT_GROUP_ID;
622c777f 572 cmd->flags = 0;
cc04455b 573 *ext = *cmd;
64bc7a2f 574
cc04455b
DM
575 return TRUE;
576}
577
64bc7a2f
DM
578static int
579req_cursor_notification(QXLInstance *qin)
cc04455b 580{
64bc7a2f
DM
581 /* not used */
582
cc04455b
DM
583 return TRUE;
584}
585
64bc7a2f
DM
586static void
587notify_update(QXLInstance *qin, uint32_t update_id)
cc04455b 588{
64bc7a2f 589 /* not used */
cc04455b
DM
590}
591
64bc7a2f
DM
592static int
593flush_resources(QXLInstance *qin)
cc04455b 594{
64bc7a2f
DM
595 /* not used */
596
cc04455b
DM
597 return TRUE;
598}
599
64bc7a2f
DM
600static int
601client_monitors_config(QXLInstance *qin, VDAgentMonitorsConfig *monitors_config)
cc04455b 602{
64bc7a2f
DM
603 /* not used */
604
cc04455b
DM
605 return 0;
606}
607
64bc7a2f
DM
608static void
609set_client_capabilities(QXLInstance *qin, uint8_t client_present,
610 uint8_t caps[58])
cc04455b 611{
64bc7a2f 612 SpiceScreen *spice_screen = SPICE_CONTAINEROF(qin, SpiceScreen, qxl_instance);
cc04455b 613
a3fd8291 614 DPRINTF(1, "present %d caps %d", client_present, caps[0]);
64bc7a2f
DM
615
616 if (spice_screen->on_client_connected && client_present) {
617 spice_screen->on_client_connected(spice_screen);
cc04455b 618 }
64bc7a2f
DM
619 if (spice_screen->on_client_disconnected && !client_present) {
620 spice_screen->on_client_disconnected(spice_screen);
cc04455b
DM
621 }
622}
623
6faab5d5
DM
624static int client_count = 0;
625
64bc7a2f
DM
626static void
627client_connected(SpiceScreen *spice_screen)
6faab5d5 628{
6faab5d5 629 client_count++;
a0579497 630
a3fd8291 631 DPRINTF(1, "client_count = %d", client_count);
6faab5d5
DM
632}
633
64bc7a2f
DM
634static void
635client_disconnected(SpiceScreen *spice_screen)
6faab5d5 636{
6faab5d5
DM
637 if (client_count > 0) {
638 client_count--;
a3fd8291 639 DPRINTF(1, "client_count = %d", client_count);
6faab5d5
DM
640 exit(0); // fixme: cleanup?
641 }
642}
643
64bc7a2f
DM
644static void
645do_conn_timeout(void *opaque)
f6cb554c 646{
64bc7a2f 647 // SpiceScreen *spice_screen = opaque;
f6cb554c
DM
648
649 if (client_count <= 0) {
a0579497 650 printf("connection timeout - stopping server\n");
f6cb554c
DM
651 exit (0); // fixme: cleanup?
652 }
653}
654
c783f726 655
cc04455b
DM
656QXLInterface display_sif = {
657 .base = {
658 .type = SPICE_INTERFACE_QXL,
64bc7a2f 659 .description = "spiceterm display server",
cc04455b
DM
660 .major_version = SPICE_INTERFACE_QXL_MAJOR,
661 .minor_version = SPICE_INTERFACE_QXL_MINOR
662 },
663 .attache_worker = attache_worker,
664 .set_compression_level = set_compression_level,
665 .set_mm_time = set_mm_time,
666 .get_init_info = get_init_info,
667
668 /* the callbacks below are called from spice server thread context */
669 .get_command = get_command,
670 .req_cmd_notification = req_cmd_notification,
671 .release_resource = release_resource,
672 .get_cursor_command = get_cursor_command,
673 .req_cursor_notification = req_cursor_notification,
674 .notify_update = notify_update,
675 .flush_resources = flush_resources,
676 .client_monitors_config = client_monitors_config,
677 .set_client_capabilities = set_client_capabilities,
678};
679
cc04455b 680
64bc7a2f 681void
1d11aa12
DM
682spice_screen_draw_char(SpiceScreen *spice_screen, int x, int y, gunichar2 ch,
683 TextAttributes attrib)
22e5ba02
DM
684{
685 int fg, bg;
686
1d11aa12 687 int invers;
22e5ba02 688 if (attrib.invers) {
1d11aa12
DM
689 invers = attrib.selected ? 0 : 1;
690 } else {
691 invers = attrib.selected ? 1 : 0;
692 }
693
694 if (invers) {
22e5ba02
DM
695 bg = attrib.fgcol;
696 fg = attrib.bgcol;
697 } else {
698 bg = attrib.bgcol;
699 fg = attrib.fgcol;
700 }
701
702 if (attrib.bold) {
703 fg += 8;
704 }
705
706 // unsuported attributes = (attrib.blink || attrib.unvisible)
707
abc13312
DM
708 int c = vt_fontmap[ch];
709
22e5ba02 710 SimpleSpiceUpdate *update;
b1f67c20 711 update = spice_screen_draw_char_cmd(spice_screen, x, y, c, fg, bg, attrib.uline);
64bc7a2f 712 push_command(spice_screen, &update->ext);
22e5ba02
DM
713}
714
64bc7a2f 715SpiceScreen *
68c2b067
DM
716spice_screen_new(SpiceCoreInterface *core, uint32_t width, uint32_t height,
717 SpiceTermOptions *opts)
cc04455b 718{
64bc7a2f 719 SpiceScreen *spice_screen = g_new0(SpiceScreen, 1);
cc04455b 720 SpiceServer* server = spice_server_new();
1631c5a9
DM
721 char *x509_key_file = "/etc/pve/local/pve-ssl.key";
722 char *x509_cert_file = "/etc/pve/local/pve-ssl.pem";
723 char *x509_cacert_file = "/etc/pve/pve-root-ca.pem";
724 char *x509_key_password = NULL;
725 char *x509_dh_file = NULL;
9d53e332 726 char *tls_ciphers = "HIGH";
1631c5a9 727
8f53ae81
DM
728 spice_screen->width = width;
729 spice_screen->height = height;
730
b52b9534
DM
731 g_cond_init(&spice_screen->command_cond);
732 g_mutex_init(&spice_screen->command_mutex);
63a34f43 733
64bc7a2f
DM
734 spice_screen->on_client_connected = client_connected,
735 spice_screen->on_client_disconnected = client_disconnected,
6faab5d5 736
64bc7a2f
DM
737 spice_screen->qxl_instance.base.sif = &display_sif.base;
738 spice_screen->qxl_instance.id = 0;
cc04455b 739
64bc7a2f
DM
740 spice_screen->core = core;
741 spice_screen->server = server;
742
68c2b067
DM
743 if (opts->addr) {
744 printf("listening on '%s:%d' (TLS)\n", opts->addr, opts->port);
745 spice_server_set_addr(server, opts->addr, 0);
746 } else {
747 printf("listening on '*:%d' (TLS)\n", opts->port);
748 }
749
1631c5a9
DM
750 // spice_server_set_port(spice_server, port);
751 //spice_server_set_image_compression(server, SPICE_IMAGE_COMPRESS_OFF);
752
68c2b067 753 spice_server_set_tls(server, opts->port,
1631c5a9
DM
754 x509_cacert_file,
755 x509_cert_file,
756 x509_key_file,
757 x509_key_password,
758 x509_dh_file,
759 tls_ciphers);
760
68c2b067 761 if (opts->noauth) {
1631c5a9 762 spice_server_set_noauth(server);
68c2b067 763 } else {
16ca49da
TL
764 char *ticket = getenv("SPICE_TICKET");
765 if (ticket) {
766 spice_server_set_ticket(server, ticket, 300, 0, 0);
68c2b067 767 }
1631c5a9 768 }
64bc7a2f 769
1d7f2da4
DM
770 int res = spice_server_init(server, core);
771 if (res != 0) {
772 g_error("spice_server_init failed, res = %d\n", res);
773 }
cc04455b
DM
774
775 cursor_init();
f6cb554c 776
ecc34bb2
DM
777 if (opts->timeout > 0) {
778 spice_screen->conn_timeout_timer = core->timer_add(do_conn_timeout, spice_screen);
779 spice_screen->core->timer_start(spice_screen->conn_timeout_timer, opts->timeout*1000);
780 }
f6cb554c 781
38e3e912
DM
782 spice_server_add_interface(spice_screen->server, &spice_screen->qxl_instance.base);
783
64bc7a2f 784 return spice_screen;
cc04455b 785}
c783f726
DM
786
787void
788spice_screen_resize(SpiceScreen *spice_screen, uint32_t width,
789 uint32_t height)
790{
c783f726
DM
791 if (spice_screen->width == width && spice_screen->height == height) {
792 return;
793 }
794
795 discard_pending_commands(spice_screen);
796
b52b9534
DM
797 spice_qxl_destroy_primary_surface(&spice_screen->qxl_instance, 0);
798
c783f726
DM
799 create_primary_surface(spice_screen, width, height);
800
801 spice_screen_clear(spice_screen, 0, 0, width, height);
802}