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