3 Copyright (C) 2013 - 2021 Proxmox Server Solutions GmbH
5 Copyright: spiceterm is under GNU GPL, the GNU General Public License.
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.
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.
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
21 Author: Dietmar Maurer <dietmar@proxmox.com>
23 Note: qlx drawing code is copied from spice-server test code.
34 #include <sys/select.h>
35 #include <sys/types.h>
39 #include <spice/enums.h>
40 #include <spice/macros.h>
41 #include <spice/qxl_dev.h>
42 #include <spice/vd_agent.h>
46 #include "spiceterm.h"
50 #define DPRINTF(x, format, ...) { \
52 printf("%s: " format "\n" , __FUNCTION__, ## __VA_ARGS__); \
56 #define MEM_SLOT_GROUP_ID 0
59 extern unsigned char color_table
[];
61 /* these colours are from linux kernel drivers/char/vt.c */
62 /* the default colour table, for VGA+ colour systems */
63 int default_red
[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
64 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
65 int default_grn
[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
66 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
67 int default_blu
[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
68 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
70 /* Parts cribbed from spice-display.h/.c/qxl.c */
72 typedef struct __attribute__ ((__packed__
)) SimpleSpiceUpdate
{
73 QXLCommandExt ext
; // needs to be first member
77 int cache_id
; // do not free bitmap if cache_id != 0
81 spice_screen_destroy_update(SimpleSpiceUpdate
*update
)
86 if (update
->drawable
.clip
.type
!= SPICE_CLIP_TYPE_NONE
) {
87 uint8_t *ptr
= (uint8_t*)update
->drawable
.clip
.data
;
90 if (update
->bitmap
&& !update
->cache_id
) {
91 g_free(update
->bitmap
);
98 release_qxl_command_ext(QXLCommandExt
*ext
)
100 g_assert(ext
!= NULL
);
102 switch (ext
->cmd
.type
) {
104 spice_screen_destroy_update((void*)ext
);
106 case QXL_CMD_SURFACE
:
109 case QXL_CMD_CURSOR
: {
110 QXLCursorCmd
*cmd
= (QXLCursorCmd
*)(unsigned long)ext
->cmd
.data
;
111 if (cmd
->type
== QXL_CURSOR_SET
) {
123 release_resource(QXLInstance
*qin
, struct QXLReleaseInfoExt release_info
)
125 QXLCommandExt
*ext
= (QXLCommandExt
*)(unsigned long)release_info
.info
->id
;
127 g_assert(release_info
.group_id
== MEM_SLOT_GROUP_ID
);
128 release_qxl_command_ext(ext
);
131 static int unique
= 0x0ffff + 1;
134 set_cmd(QXLCommandExt
*ext
, uint32_t type
, QXLPHYSICAL data
)
136 ext
->cmd
.type
= type
;
137 ext
->cmd
.data
= data
;
138 ext
->cmd
.padding
= 0;
139 ext
->group_id
= MEM_SLOT_GROUP_ID
;
144 simple_set_release_info(QXLReleaseInfo
*info
, intptr_t ptr
)
147 //info->group_id = MEM_SLOT_GROUP_ID;
150 /* Note: push_command/get_command are called from different threads */
153 push_command(SpiceScreen
*spice_screen
, QXLCommandExt
*ext
)
157 g_mutex_lock(&spice_screen
->command_mutex
);
159 while (spice_screen
->commands_end
- spice_screen
->commands_start
>= COMMANDS_SIZE
) {
160 g_cond_wait(&spice_screen
->command_cond
, &spice_screen
->command_mutex
);
163 g_assert(spice_screen
->commands_end
- spice_screen
->commands_start
< COMMANDS_SIZE
);
165 if ((spice_screen
->commands_end
- spice_screen
->commands_start
) > 0) {
169 spice_screen
->commands
[spice_screen
->commands_end
% COMMANDS_SIZE
] = ext
;
170 spice_screen
->commands_end
++;
173 spice_qxl_wakeup(&spice_screen
->qxl_instance
);
174 //spice_screen->qxl_worker->wakeup(spice_screen->qxl_worker);
177 g_mutex_unlock(&spice_screen
->command_mutex
);
181 /* bitmap are freed, so they must be allocated with g_malloc */
182 static SimpleSpiceUpdate
*
183 spice_screen_update_from_bitmap_cmd(
189 SimpleSpiceUpdate
*update
;
190 QXLDrawable
*drawable
;
194 bh
= bbox
.bottom
- bbox
.top
;
195 bw
= bbox
.right
- bbox
.left
;
197 update
= g_new0(SimpleSpiceUpdate
, 1);
198 update
->bitmap
= bitmap
;
199 drawable
= &update
->drawable
;
200 image
= &update
->image
;
202 drawable
->surface_id
= surface_id
;
204 drawable
->bbox
= bbox
;
205 drawable
->clip
.type
= SPICE_CLIP_TYPE_NONE
;
206 drawable
->effect
= QXL_EFFECT_OPAQUE
;
207 simple_set_release_info(&drawable
->release_info
, (intptr_t)update
);
208 drawable
->type
= QXL_DRAW_COPY
;
209 drawable
->surfaces_dest
[0] = -1;
210 drawable
->surfaces_dest
[1] = -1;
211 drawable
->surfaces_dest
[2] = -1;
213 drawable
->u
.copy
.rop_descriptor
= SPICE_ROPD_OP_PUT
;
214 drawable
->u
.copy
.src_bitmap
= (intptr_t)image
;
215 drawable
->u
.copy
.src_area
.right
= bw
;
216 drawable
->u
.copy
.src_area
.bottom
= bh
;
219 QXL_SET_IMAGE_ID(image
, QXL_IMAGE_GROUP_DEVICE
, cache_id
);
220 image
->descriptor
.flags
= SPICE_IMAGE_FLAGS_CACHE_ME
;
221 update
->cache_id
= cache_id
;
223 QXL_SET_IMAGE_ID(image
, QXL_IMAGE_GROUP_DEVICE
, ++unique
);
225 image
->descriptor
.type
= SPICE_IMAGE_TYPE_BITMAP
;
226 image
->bitmap
.flags
= QXL_BITMAP_DIRECT
| QXL_BITMAP_TOP_DOWN
;
227 image
->bitmap
.stride
= bw
* 4;
228 image
->descriptor
.width
= image
->bitmap
.x
= bw
;
229 image
->descriptor
.height
= image
->bitmap
.y
= bh
;
230 image
->bitmap
.data
= (intptr_t)bitmap
;
231 image
->bitmap
.palette
= 0;
232 image
->bitmap
.format
= SPICE_BITMAP_FMT_32BIT
;
234 set_cmd(&update
->ext
, QXL_CMD_DRAW
, (intptr_t)drawable
);
239 static SimpleSpiceUpdate
*
240 spice_screen_draw_char_cmd(
241 SpiceScreen
*spice_screen
,
249 uint8_t *bitmap
= NULL
;
254 if (!uline
&& c
< 256) {
255 cache_id
= ((fg
<< 12) | (bg
<< 8) | (c
& 255)) & 0x0ffff;
256 if ((ce
= (CachedImage
*)g_hash_table_lookup(spice_screen
->image_cache
, &cache_id
))) {
262 int left
= x
* bw
, top
= y
* bh
;
265 uint8_t *dst
= bitmap
= g_malloc(bw
* bh
* 4);
267 unsigned char *data
= vt_font_data
+ c
*16;
268 unsigned char d
= *data
;
270 g_assert(fg
>= 0 && fg
< 16);
271 g_assert(bg
>= 0 && bg
< 16);
273 unsigned char fgc_red
= default_red
[color_table
[fg
]];
274 unsigned char fgc_blue
= default_blu
[color_table
[fg
]];
275 unsigned char fgc_green
= default_grn
[color_table
[fg
]];
276 unsigned char bgc_red
= default_red
[color_table
[bg
]];
277 unsigned char bgc_blue
= default_blu
[color_table
[bg
]];
278 unsigned char bgc_green
= default_grn
[color_table
[bg
]];
280 for (int j
= 0; j
< 16; j
++) {
281 gboolean ul
= (j
== 14) && uline
;
282 for (int i
= 0; i
< 8; i
++) {
289 *(dst
+1) = fgc_green
;
294 *(dst
+1) = bgc_green
;
304 ce
= g_new(CachedImage
, 1);
305 ce
->cache_id
= cache_id
;
307 g_hash_table_insert(spice_screen
->image_cache
, &ce
->cache_id
, ce
);
311 bbox
.left
= left
; bbox
.top
= top
;
312 bbox
.right
= left
+ bw
; bbox
.bottom
= top
+ bh
;
314 return spice_screen_update_from_bitmap_cmd(0, bbox
, bitmap
, cache_id
);
319 SpiceScreen
*spice_screen
,
327 SimpleSpiceUpdate
*update
;
328 QXLDrawable
*drawable
;
333 update
= g_new0(SimpleSpiceUpdate
, 1);
334 drawable
= &update
->drawable
;
341 drawable
->surface_id
= surface_id
;
343 drawable
->bbox
= bbox
;
344 drawable
->clip
.type
= SPICE_CLIP_TYPE_NONE
;
345 drawable
->effect
= QXL_EFFECT_OPAQUE
;
346 simple_set_release_info(&drawable
->release_info
, (intptr_t)update
);
347 drawable
->type
= QXL_COPY_BITS
;
348 drawable
->surfaces_dest
[0] = -1;
349 drawable
->surfaces_dest
[1] = -1;
350 drawable
->surfaces_dest
[2] = -1;
352 drawable
->u
.copy_bits
.src_pos
.x
= src_x
;
353 drawable
->u
.copy_bits
.src_pos
.y
= src_y
;
355 set_cmd(&update
->ext
, QXL_CMD_DRAW
, (intptr_t)drawable
);
357 push_command(spice_screen
, &update
->ext
);
361 spice_screen_clear(SpiceScreen
*spice_screen
, int x1
, int y1
, int x2
, int y2
)
363 SimpleSpiceUpdate
*update
;
364 QXLDrawable
*drawable
;
369 update
= g_new0(SimpleSpiceUpdate
, 1);
370 drawable
= &update
->drawable
;
377 drawable
->surface_id
= surface_id
;
379 drawable
->bbox
= bbox
;
380 drawable
->clip
.type
= SPICE_CLIP_TYPE_NONE
;
381 drawable
->effect
= QXL_EFFECT_OPAQUE
;
382 simple_set_release_info(&drawable
->release_info
, (intptr_t)update
);
383 drawable
->type
= QXL_DRAW_BLACKNESS
;
384 drawable
->surfaces_dest
[0] = -1;
385 drawable
->surfaces_dest
[1] = -1;
386 drawable
->surfaces_dest
[2] = -1;
388 set_cmd(&update
->ext
, QXL_CMD_DRAW
, (intptr_t)drawable
);
390 push_command(spice_screen
, &update
->ext
);
394 create_primary_surface(
395 SpiceScreen
*spice_screen
,
399 QXLDevSurfaceCreate surface
= { 0, };
401 g_assert(height
> 0);
404 if (height
> MAX_HEIGHT
)
407 if (width
> MAX_WIDTH
)
410 surface
.format
= SPICE_SURFACE_FMT_32_xRGB
;
411 surface
.width
= spice_screen
->primary_width
= width
;
412 surface
.height
= spice_screen
->primary_height
= height
;
413 surface
.stride
= -width
* 4; /* negative? */
414 surface
.mouse_mode
= TRUE
; /* unused by red_worker */
416 surface
.type
= 0; /* unused by red_worker */
417 surface
.position
= 0; /* unused by red_worker */
418 surface
.mem
= (uint64_t)&spice_screen
->primary_surface
;
419 surface
.group_id
= MEM_SLOT_GROUP_ID
;
421 spice_screen
->width
= width
;
422 spice_screen
->height
= height
;
424 spice_screen
->cursor_set
= 0;
426 spice_qxl_create_primary_surface(&spice_screen
->qxl_instance
, 0, &surface
);
429 QXLDevMemSlot slot
= {
430 .slot_group_id
= MEM_SLOT_GROUP_ID
,
440 attache_worker(QXLInstance
*qin
, QXLWorker
*_qxl_worker
)
442 SpiceScreen
*spice_screen
= SPICE_CONTAINEROF(qin
, SpiceScreen
, qxl_instance
);
444 if (spice_screen
->qxl_worker
) {
445 g_assert_not_reached();
448 spice_screen
->qxl_worker
= _qxl_worker
;
449 spice_qxl_add_memslot(&spice_screen
->qxl_instance
, &slot
);
450 create_primary_surface(spice_screen
, spice_screen
->width
, spice_screen
->height
);
451 spice_server_vm_start(spice_screen
->server
);
455 set_compression_level(QXLInstance
*qin
, int level
)
461 set_mm_time(QXLInstance
*qin
, uint32_t mm_time
)
467 get_init_info(QXLInstance
*qin
, QXLDevInitInfo
*info
)
469 memset(info
, 0, sizeof(*info
));
470 info
->num_memslots
= 1;
471 info
->num_memslots_groups
= 1;
472 info
->memslot_id_bits
= 1;
473 info
->memslot_gen_bits
= 1;
474 info
->n_surfaces
= 1;
477 /* called from spice_server thread (i.e. red_worker thread) */
479 get_command(QXLInstance
*qin
, struct QXLCommandExt
*ext
)
481 SpiceScreen
*spice_screen
= SPICE_CONTAINEROF(qin
, SpiceScreen
, qxl_instance
);
484 g_mutex_lock(&spice_screen
->command_mutex
);
486 if ((spice_screen
->commands_end
- spice_screen
->commands_start
) == 0) {
491 *ext
= *spice_screen
->commands
[spice_screen
->commands_start
% COMMANDS_SIZE
];
492 g_assert(spice_screen
->commands_start
< spice_screen
->commands_end
);
493 spice_screen
->commands_start
++;
494 g_cond_signal(&spice_screen
->command_cond
);
499 g_mutex_unlock(&spice_screen
->command_mutex
);
504 discard_pending_commands(SpiceScreen
*spice_screen
)
508 g_mutex_lock(&spice_screen
->command_mutex
);
509 for (pos
= spice_screen
->commands_start
; pos
< spice_screen
->commands_end
; pos
++) {
510 release_qxl_command_ext(spice_screen
->commands
[pos
% COMMANDS_SIZE
]);
512 spice_screen
->commands_start
= spice_screen
->commands_end
;
513 g_mutex_unlock(&spice_screen
->command_mutex
);
517 req_cmd_notification(QXLInstance
*qin
)
519 //SpiceScreen *spice_screen = SPICE_CONTAINEROF(qin, SpiceScreen, qxl_instance);
520 //spice_screen->core->timer_start(spice_screen->wakeup_timer, spice_screen->wakeup_ms);
525 #define CURSOR_WIDTH 8
526 #define CURSOR_HEIGHT 16
530 uint8_t data
[CURSOR_WIDTH
* CURSOR_HEIGHT
* 4]; // 32bit per pixel
536 cursor
.cursor
.header
.unique
= 0;
537 cursor
.cursor
.header
.type
= SPICE_CURSOR_TYPE_COLOR32
;
538 cursor
.cursor
.header
.width
= CURSOR_WIDTH
;
539 cursor
.cursor
.header
.height
= CURSOR_HEIGHT
;
540 cursor
.cursor
.header
.hot_spot_x
= 0;
541 cursor
.cursor
.header
.hot_spot_y
= 0;
542 cursor
.cursor
.data_size
= CURSOR_WIDTH
* CURSOR_HEIGHT
* 4;
544 // X drivers addes it to the cursor size because it could be
545 // cursor data information or another cursor related stuffs.
546 // Otherwise, the code will break in client/cursor.cpp side,
547 // that expect the data_size plus cursor information.
548 // Blame cursor protocol for this. :-)
549 cursor
.cursor
.data_size
+= 128;
550 cursor
.cursor
.chunk
.data_size
= cursor
.cursor
.data_size
;
551 cursor
.cursor
.chunk
.prev_chunk
= cursor
.cursor
.chunk
.next_chunk
= 0;
555 get_cursor_command(QXLInstance
*qin
, struct QXLCommandExt
*ext
)
557 SpiceScreen
*spice_screen
= SPICE_CONTAINEROF(qin
, SpiceScreen
, qxl_instance
);
559 QXLCursorCmd
*cursor_cmd
;
562 if (spice_screen
->cursor_set
)
565 spice_screen
->cursor_set
= 1;
567 cmd
= calloc(sizeof(QXLCommandExt
), 1);
568 cursor_cmd
= calloc(sizeof(QXLCursorCmd
), 1);
570 cursor_cmd
->release_info
.id
= (unsigned long)cmd
;
572 cursor_cmd
->type
= QXL_CURSOR_SET
;
573 cursor_cmd
->u
.set
.position
.x
= 0;
574 cursor_cmd
->u
.set
.position
.y
= 0;
575 cursor_cmd
->u
.set
.visible
= TRUE
;
576 cursor_cmd
->u
.set
.shape
= (unsigned long)&cursor
;
577 // white rect as cursor
578 memset(cursor
.data
, 255, sizeof(cursor
.data
));
580 cmd
->cmd
.data
= (unsigned long)cursor_cmd
;
581 cmd
->cmd
.type
= QXL_CMD_CURSOR
;
582 cmd
->group_id
= MEM_SLOT_GROUP_ID
;
590 req_cursor_notification(QXLInstance
*qin
)
598 notify_update(QXLInstance
*qin
, uint32_t update_id
)
604 flush_resources(QXLInstance
*qin
)
612 client_monitors_config(QXLInstance
*qin
, VDAgentMonitorsConfig
*monitors_config
)
620 set_client_capabilities(
622 uint8_t client_present
,
625 SpiceScreen
*spice_screen
= SPICE_CONTAINEROF(qin
, SpiceScreen
, qxl_instance
);
627 DPRINTF(1, "present %d caps %d", client_present
, caps
[0]);
629 if (spice_screen
->on_client_connected
&& client_present
) {
630 spice_screen
->on_client_connected(spice_screen
);
632 if (spice_screen
->on_client_disconnected
&& !client_present
) {
633 spice_screen
->on_client_disconnected(spice_screen
);
637 static int client_count
= 0;
640 client_connected(SpiceScreen
*spice_screen
)
644 DPRINTF(1, "client_count = %d", client_count
);
648 client_disconnected(SpiceScreen
*spice_screen
)
650 if (client_count
> 0) {
652 DPRINTF(1, "client_count = %d", client_count
);
653 exit(0); // fixme: cleanup?
658 do_conn_timeout(void *opaque
)
660 // SpiceScreen *spice_screen = opaque;
662 if (client_count
<= 0) {
663 printf("connection timeout - stopping server\n");
664 exit (0); // fixme: cleanup?
669 QXLInterface display_sif
= {
671 .type
= SPICE_INTERFACE_QXL
,
672 .description
= "spiceterm display server",
673 .major_version
= SPICE_INTERFACE_QXL_MAJOR
,
674 .minor_version
= SPICE_INTERFACE_QXL_MINOR
676 .attache_worker
= attache_worker
,
677 .set_compression_level
= set_compression_level
,
678 .set_mm_time
= set_mm_time
,
679 .get_init_info
= get_init_info
,
681 /* the callbacks below are called from spice server thread context */
682 .get_command
= get_command
,
683 .req_cmd_notification
= req_cmd_notification
,
684 .release_resource
= release_resource
,
685 .get_cursor_command
= get_cursor_command
,
686 .req_cursor_notification
= req_cursor_notification
,
687 .notify_update
= notify_update
,
688 .flush_resources
= flush_resources
,
689 .client_monitors_config
= client_monitors_config
,
690 .set_client_capabilities
= set_client_capabilities
,
695 spice_screen_draw_char(
696 SpiceScreen
*spice_screen
,
700 TextAttributes attrib
706 invers
= attrib
.selected
? 0 : 1;
708 invers
= attrib
.selected
? 1 : 0;
723 // unsuported attributes = (attrib.blink || attrib.unvisible)
725 int c
= vt_fontmap
[ch
];
727 SimpleSpiceUpdate
*update
;
728 update
= spice_screen_draw_char_cmd(spice_screen
, x
, y
, c
, fg
, bg
, attrib
.uline
);
729 push_command(spice_screen
, &update
->ext
);
734 SpiceCoreInterface
*core
,
737 SpiceTermOptions
*opts
739 SpiceScreen
*spice_screen
= g_new0(SpiceScreen
, 1);
740 SpiceServer
* server
= spice_server_new();
741 char *x509_key_file
= "/etc/pve/local/pve-ssl.key";
742 char *x509_cert_file
= "/etc/pve/local/pve-ssl.pem";
743 char *x509_cacert_file
= "/etc/pve/pve-root-ca.pem";
744 char *x509_key_password
= NULL
;
745 char *x509_dh_file
= NULL
;
746 char *tls_ciphers
= "HIGH";
748 spice_screen
->width
= width
;
749 spice_screen
->height
= height
;
751 g_cond_init(&spice_screen
->command_cond
);
752 g_mutex_init(&spice_screen
->command_mutex
);
754 spice_screen
->on_client_connected
= client_connected
,
755 spice_screen
->on_client_disconnected
= client_disconnected
,
757 spice_screen
->qxl_instance
.base
.sif
= &display_sif
.base
;
758 spice_screen
->qxl_instance
.id
= 0;
760 spice_screen
->core
= core
;
761 spice_screen
->server
= server
;
764 printf("listening on '%s:%d' (TLS)\n", opts
->addr
, opts
->port
);
765 spice_server_set_addr(server
, opts
->addr
, 0);
767 printf("listening on '*:%d' (TLS)\n", opts
->port
);
770 // spice_server_set_port(spice_server, port);
771 //spice_server_set_image_compression(server, SPICE_IMAGE_COMPRESS_OFF);
773 spice_server_set_tls(
784 spice_server_set_noauth(server
);
786 char *ticket
= getenv("SPICE_TICKET");
788 spice_server_set_ticket(server
, ticket
, 300, 0, 0);
792 int res
= spice_server_init(server
, core
);
794 g_error("spice_server_init failed, res = %d\n", res
);
799 if (opts
->timeout
> 0) {
800 spice_screen
->conn_timeout_timer
= core
->timer_add(do_conn_timeout
, spice_screen
);
801 spice_screen
->core
->timer_start(spice_screen
->conn_timeout_timer
, opts
->timeout
*1000);
804 spice_server_add_interface(spice_screen
->server
, &spice_screen
->qxl_instance
.base
);
811 SpiceScreen
*spice_screen
,
815 if (spice_screen
->width
== width
&& spice_screen
->height
== height
) {
819 discard_pending_commands(spice_screen
);
821 spice_qxl_destroy_primary_surface(&spice_screen
->qxl_instance
, 0);
823 create_primary_surface(spice_screen
, width
, height
);
825 spice_screen_clear(spice_screen
, 0, 0, width
, height
);