3 Copyright (C) 2013 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
58 /* these colours are from linux kernel drivers/char/vt.c */
59 /* the default colour table, for VGA+ colour systems */
60 int default_red
[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
61 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
62 int default_grn
[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
63 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
64 int default_blu
[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
65 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
67 /* Parts cribbed from spice-display.h/.c/qxl.c */
69 typedef struct SimpleSpiceUpdate
{
70 QXLCommandExt ext
; // needs to be first member
74 int cache_id
; // do not free bitmap if cache_id != 0
78 spice_screen_destroy_update(SimpleSpiceUpdate
*update
)
83 if (update
->drawable
.clip
.type
!= SPICE_CLIP_TYPE_NONE
) {
84 uint8_t *ptr
= (uint8_t*)update
->drawable
.clip
.data
;
87 if (update
->bitmap
&& !update
->cache_id
) {
88 g_free(update
->bitmap
);
94 static int unique
= 0x0ffff + 1;
97 set_cmd(QXLCommandExt
*ext
, uint32_t type
, QXLPHYSICAL data
)
100 ext
->cmd
.data
= data
;
101 ext
->cmd
.padding
= 0;
102 ext
->group_id
= MEM_SLOT_GROUP_ID
;
107 simple_set_release_info(QXLReleaseInfo
*info
, intptr_t ptr
)
110 //info->group_id = MEM_SLOT_GROUP_ID;
113 /* Note: push_command/get_command are called from different threads */
116 push_command(SpiceScreen
*spice_screen
, QXLCommandExt
*ext
)
120 g_mutex_lock(spice_screen
->command_mutex
);
122 while (spice_screen
->commands_end
- spice_screen
->commands_start
>= COMMANDS_SIZE
) {
123 g_cond_wait(spice_screen
->command_cond
, spice_screen
->command_mutex
);
126 g_assert(spice_screen
->commands_end
- spice_screen
->commands_start
< COMMANDS_SIZE
);
128 if ((spice_screen
->commands_end
- spice_screen
->commands_start
) > 0) {
132 spice_screen
->commands
[spice_screen
->commands_end
% COMMANDS_SIZE
] = ext
;
133 spice_screen
->commands_end
++;
136 spice_screen
->qxl_worker
->wakeup(spice_screen
->qxl_worker
);
139 g_mutex_unlock(spice_screen
->command_mutex
);
143 /* bitmap are freed, so they must be allocated with g_malloc */
144 static SimpleSpiceUpdate
*
145 spice_screen_update_from_bitmap_cmd(uint32_t surface_id
, QXLRect bbox
, uint8_t *bitmap
, int cache_id
)
147 SimpleSpiceUpdate
*update
;
148 QXLDrawable
*drawable
;
152 bh
= bbox
.bottom
- bbox
.top
;
153 bw
= bbox
.right
- bbox
.left
;
155 update
= g_new0(SimpleSpiceUpdate
, 1);
156 update
->bitmap
= bitmap
;
157 drawable
= &update
->drawable
;
158 image
= &update
->image
;
160 drawable
->surface_id
= surface_id
;
162 drawable
->bbox
= bbox
;
163 drawable
->clip
.type
= SPICE_CLIP_TYPE_NONE
;
164 drawable
->effect
= QXL_EFFECT_OPAQUE
;
165 simple_set_release_info(&drawable
->release_info
, (intptr_t)update
);
166 drawable
->type
= QXL_DRAW_COPY
;
167 drawable
->surfaces_dest
[0] = -1;
168 drawable
->surfaces_dest
[1] = -1;
169 drawable
->surfaces_dest
[2] = -1;
171 drawable
->u
.copy
.rop_descriptor
= SPICE_ROPD_OP_PUT
;
172 drawable
->u
.copy
.src_bitmap
= (intptr_t)image
;
173 drawable
->u
.copy
.src_area
.right
= bw
;
174 drawable
->u
.copy
.src_area
.bottom
= bh
;
177 QXL_SET_IMAGE_ID(image
, QXL_IMAGE_GROUP_DEVICE
, cache_id
);
178 image
->descriptor
.flags
= SPICE_IMAGE_FLAGS_CACHE_ME
;
179 update
->cache_id
= cache_id
;
181 QXL_SET_IMAGE_ID(image
, QXL_IMAGE_GROUP_DEVICE
, ++unique
);
183 image
->descriptor
.type
= SPICE_IMAGE_TYPE_BITMAP
;
184 image
->bitmap
.flags
= QXL_BITMAP_DIRECT
| QXL_BITMAP_TOP_DOWN
;
185 image
->bitmap
.stride
= bw
* 4;
186 image
->descriptor
.width
= image
->bitmap
.x
= bw
;
187 image
->descriptor
.height
= image
->bitmap
.y
= bh
;
188 image
->bitmap
.data
= (intptr_t)bitmap
;
189 image
->bitmap
.palette
= 0;
190 image
->bitmap
.format
= SPICE_BITMAP_FMT_32BIT
;
192 set_cmd(&update
->ext
, QXL_CMD_DRAW
, (intptr_t)drawable
);
197 static SimpleSpiceUpdate
*
198 spice_screen_draw_char_cmd(SpiceScreen
*spice_screen
, int x
, int y
, int c
,
199 int fg
, int bg
, gboolean uline
)
203 uint8_t *bitmap
= NULL
;
210 if (!uline
&& c
< 256) {
211 cache_id
= ((fg
<< 12) | (bg
<< 8) | (c
& 255)) & 0x0ffff;
212 if ((ce
= (CachedImage
*)g_hash_table_lookup(spice_screen
->image_cache
, &cache_id
))) {
224 bitmap
= dst
= g_malloc(bw
* bh
* 4);
226 unsigned char *data
= vt_font_data
+ c
*16;
227 unsigned char d
= *data
;
229 g_assert(fg
>= 0 && fg
< 16);
230 g_assert(bg
>= 0 && bg
< 16);
232 unsigned char fgc_red
= default_red
[fg
];
233 unsigned char fgc_blue
= default_blu
[fg
];
234 unsigned char fgc_green
= default_grn
[fg
];
235 unsigned char bgc_red
= default_red
[bg
];
236 unsigned char bgc_blue
= default_blu
[bg
];
237 unsigned char bgc_green
= default_grn
[bg
];
239 for (j
= 0; j
< 16; j
++) {
240 gboolean ul
= (j
== 14) && uline
;
241 for (i
= 0; i
< 8; i
++) {
248 *(dst
+1) = fgc_green
;
253 *(dst
+1) = bgc_green
;
261 ce
= g_new(CachedImage
, 1);
262 ce
->cache_id
= cache_id
;
264 g_hash_table_insert(spice_screen
->image_cache
, &ce
->cache_id
, ce
);
267 bbox
.left
= left
; bbox
.top
= top
;
268 bbox
.right
= left
+ bw
; bbox
.bottom
= top
+ bh
;
270 return spice_screen_update_from_bitmap_cmd(0, bbox
, bitmap
, cache_id
);
274 spice_screen_scroll(SpiceScreen
*spice_screen
, int x1
, int y1
,
275 int x2
, int y2
, int src_x
, int src_y
)
277 SimpleSpiceUpdate
*update
;
278 QXLDrawable
*drawable
;
283 update
= g_new0(SimpleSpiceUpdate
, 1);
284 drawable
= &update
->drawable
;
291 drawable
->surface_id
= surface_id
;
293 drawable
->bbox
= bbox
;
294 drawable
->clip
.type
= SPICE_CLIP_TYPE_NONE
;
295 drawable
->effect
= QXL_EFFECT_OPAQUE
;
296 simple_set_release_info(&drawable
->release_info
, (intptr_t)update
);
297 drawable
->type
= QXL_COPY_BITS
;
298 drawable
->surfaces_dest
[0] = -1;
299 drawable
->surfaces_dest
[1] = -1;
300 drawable
->surfaces_dest
[2] = -1;
302 drawable
->u
.copy_bits
.src_pos
.x
= src_x
;
303 drawable
->u
.copy_bits
.src_pos
.y
= src_y
;
305 set_cmd(&update
->ext
, QXL_CMD_DRAW
, (intptr_t)drawable
);
307 push_command(spice_screen
, &update
->ext
);
311 spice_screen_clear(SpiceScreen
*spice_screen
, int x1
, int y1
, int x2
, int y2
)
313 SimpleSpiceUpdate
*update
;
314 QXLDrawable
*drawable
;
319 update
= g_new0(SimpleSpiceUpdate
, 1);
320 drawable
= &update
->drawable
;
327 drawable
->surface_id
= surface_id
;
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_DRAW_BLACKNESS
;
334 drawable
->surfaces_dest
[0] = -1;
335 drawable
->surfaces_dest
[1] = -1;
336 drawable
->surfaces_dest
[2] = -1;
338 set_cmd(&update
->ext
, QXL_CMD_DRAW
, (intptr_t)drawable
);
340 push_command(spice_screen
, &update
->ext
);
344 create_primary_surface(SpiceScreen
*spice_screen
, uint32_t width
,
347 QXLWorker
*qxl_worker
= spice_screen
->qxl_worker
;
348 QXLDevSurfaceCreate surface
= { 0, };
350 g_assert(height
> 0);
353 if (height
> MAX_HEIGHT
)
356 if (width
> MAX_WIDTH
)
359 surface
.format
= SPICE_SURFACE_FMT_32_xRGB
;
360 surface
.width
= spice_screen
->primary_width
= width
;
361 surface
.height
= spice_screen
->primary_height
= height
;
362 surface
.stride
= -width
* 4; /* negative? */
363 surface
.mouse_mode
= TRUE
; /* unused by red_worker */
365 surface
.type
= 0; /* unused by red_worker */
366 surface
.position
= 0; /* unused by red_worker */
367 surface
.mem
= (uint64_t)&spice_screen
->primary_surface
;
368 surface
.group_id
= MEM_SLOT_GROUP_ID
;
370 spice_screen
->width
= width
;
371 spice_screen
->height
= height
;
373 qxl_worker
->create_primary_surface(qxl_worker
, 0, &surface
);
377 spice_screen_resize(SpiceScreen
*spice_screen
, uint32_t width
,
380 QXLWorker
*qxl_worker
= spice_screen
->qxl_worker
;
382 if (spice_screen
->width
== width
&& spice_screen
->height
== height
) {
386 qxl_worker
->destroy_primary_surface(qxl_worker
, 0);
388 create_primary_surface(spice_screen
, width
, height
);
390 spice_screen_clear(spice_screen
, 0, 0, width
, height
);
394 QXLDevMemSlot slot
= {
395 .slot_group_id
= MEM_SLOT_GROUP_ID
,
405 attache_worker(QXLInstance
*qin
, QXLWorker
*_qxl_worker
)
407 SpiceScreen
*spice_screen
= SPICE_CONTAINEROF(qin
, SpiceScreen
, qxl_instance
);
409 if (spice_screen
->qxl_worker
) {
410 g_assert_not_reached();
413 spice_screen
->qxl_worker
= _qxl_worker
;
414 spice_screen
->qxl_worker
->add_memslot(spice_screen
->qxl_worker
, &slot
);
415 create_primary_surface(spice_screen
, spice_screen
->width
, spice_screen
->height
);
416 spice_screen
->qxl_worker
->start(spice_screen
->qxl_worker
);
420 set_compression_level(QXLInstance
*qin
, int level
)
426 set_mm_time(QXLInstance
*qin
, uint32_t mm_time
)
432 get_init_info(QXLInstance
*qin
, QXLDevInitInfo
*info
)
434 memset(info
, 0, sizeof(*info
));
435 info
->num_memslots
= 1;
436 info
->num_memslots_groups
= 1;
437 info
->memslot_id_bits
= 1;
438 info
->memslot_gen_bits
= 1;
439 info
->n_surfaces
= 1;
442 /* called from spice_server thread (i.e. red_worker thread) */
444 get_command(QXLInstance
*qin
, struct QXLCommandExt
*ext
)
446 SpiceScreen
*spice_screen
= SPICE_CONTAINEROF(qin
, SpiceScreen
, qxl_instance
);
449 g_mutex_lock(spice_screen
->command_mutex
);
451 if ((spice_screen
->commands_end
- spice_screen
->commands_start
) == 0) {
456 *ext
= *spice_screen
->commands
[spice_screen
->commands_start
% COMMANDS_SIZE
];
457 g_assert(spice_screen
->commands_start
< spice_screen
->commands_end
);
458 spice_screen
->commands_start
++;
459 g_cond_signal(spice_screen
->command_cond
);
464 g_mutex_unlock(spice_screen
->command_mutex
);
469 req_cmd_notification(QXLInstance
*qin
)
471 //SpiceScreen *spice_screen = SPICE_CONTAINEROF(qin, SpiceScreen, qxl_instance);
472 //spice_screen->core->timer_start(spice_screen->wakeup_timer, spice_screen->wakeup_ms);
478 release_resource(QXLInstance
*qin
, struct QXLReleaseInfoExt release_info
)
480 QXLCommandExt
*ext
= (QXLCommandExt
*)(unsigned long)release_info
.info
->id
;
482 g_assert(release_info
.group_id
== MEM_SLOT_GROUP_ID
);
483 switch (ext
->cmd
.type
) {
485 spice_screen_destroy_update((void*)ext
);
487 case QXL_CMD_SURFACE
:
490 case QXL_CMD_CURSOR
: {
491 QXLCursorCmd
*cmd
= (QXLCursorCmd
*)(unsigned long)ext
->cmd
.data
;
492 if (cmd
->type
== QXL_CURSOR_SET
) {
503 #define CURSOR_WIDTH 8
504 #define CURSOR_HEIGHT 16
508 uint8_t data
[CURSOR_WIDTH
* CURSOR_HEIGHT
* 4]; // 32bit per pixel
514 cursor
.cursor
.header
.unique
= 0;
515 cursor
.cursor
.header
.type
= SPICE_CURSOR_TYPE_COLOR32
;
516 cursor
.cursor
.header
.width
= CURSOR_WIDTH
;
517 cursor
.cursor
.header
.height
= CURSOR_HEIGHT
;
518 cursor
.cursor
.header
.hot_spot_x
= 0;
519 cursor
.cursor
.header
.hot_spot_y
= 0;
520 cursor
.cursor
.data_size
= CURSOR_WIDTH
* CURSOR_HEIGHT
* 4;
522 // X drivers addes it to the cursor size because it could be
523 // cursor data information or another cursor related stuffs.
524 // Otherwise, the code will break in client/cursor.cpp side,
525 // that expect the data_size plus cursor information.
526 // Blame cursor protocol for this. :-)
527 cursor
.cursor
.data_size
+= 128;
528 cursor
.cursor
.chunk
.data_size
= cursor
.cursor
.data_size
;
529 cursor
.cursor
.chunk
.prev_chunk
= cursor
.cursor
.chunk
.next_chunk
= 0;
533 get_cursor_command(QXLInstance
*qin
, struct QXLCommandExt
*ext
)
535 //SpiceScreen *spice_screen = SPICE_CONTAINEROF(qin, SpiceScreen, qxl_instance);
538 QXLCursorCmd
*cursor_cmd
;
541 if (!set
) return FALSE
;
545 cmd
= calloc(sizeof(QXLCommandExt
), 1);
546 cursor_cmd
= calloc(sizeof(QXLCursorCmd
), 1);
548 cursor_cmd
->release_info
.id
= (unsigned long)cmd
;
550 cursor_cmd
->type
= QXL_CURSOR_SET
;
551 cursor_cmd
->u
.set
.position
.x
= 0;
552 cursor_cmd
->u
.set
.position
.y
= 0;
553 cursor_cmd
->u
.set
.visible
= TRUE
;
554 cursor_cmd
->u
.set
.shape
= (unsigned long)&cursor
;
555 // white rect as cursor
556 memset(cursor
.data
, 255, sizeof(cursor
.data
));
558 cmd
->cmd
.data
= (unsigned long)cursor_cmd
;
559 cmd
->cmd
.type
= QXL_CMD_CURSOR
;
560 cmd
->group_id
= MEM_SLOT_GROUP_ID
;
568 req_cursor_notification(QXLInstance
*qin
)
576 notify_update(QXLInstance
*qin
, uint32_t update_id
)
582 flush_resources(QXLInstance
*qin
)
590 client_monitors_config(QXLInstance
*qin
, VDAgentMonitorsConfig
*monitors_config
)
598 set_client_capabilities(QXLInstance
*qin
, uint8_t client_present
,
601 SpiceScreen
*spice_screen
= SPICE_CONTAINEROF(qin
, SpiceScreen
, qxl_instance
);
603 DPRINTF(1, "present %d caps %d", client_present
, caps
[0]);
605 if (spice_screen
->on_client_connected
&& client_present
) {
606 spice_screen
->on_client_connected(spice_screen
);
608 if (spice_screen
->on_client_disconnected
&& !client_present
) {
609 spice_screen
->on_client_disconnected(spice_screen
);
613 static int client_count
= 0;
616 client_connected(SpiceScreen
*spice_screen
)
620 DPRINTF(1, "client_count = %d", client_count
);
624 client_disconnected(SpiceScreen
*spice_screen
)
626 if (client_count
> 0) {
628 DPRINTF(1, "client_count = %d", client_count
);
629 exit(0); // fixme: cleanup?
634 do_conn_timeout(void *opaque
)
636 // SpiceScreen *spice_screen = opaque;
638 if (client_count
<= 0) {
639 printf("connection timeout - stopping server\n");
640 exit (0); // fixme: cleanup?
644 QXLInterface display_sif
= {
646 .type
= SPICE_INTERFACE_QXL
,
647 .description
= "spiceterm display server",
648 .major_version
= SPICE_INTERFACE_QXL_MAJOR
,
649 .minor_version
= SPICE_INTERFACE_QXL_MINOR
651 .attache_worker
= attache_worker
,
652 .set_compression_level
= set_compression_level
,
653 .set_mm_time
= set_mm_time
,
654 .get_init_info
= get_init_info
,
656 /* the callbacks below are called from spice server thread context */
657 .get_command
= get_command
,
658 .req_cmd_notification
= req_cmd_notification
,
659 .release_resource
= release_resource
,
660 .get_cursor_command
= get_cursor_command
,
661 .req_cursor_notification
= req_cursor_notification
,
662 .notify_update
= notify_update
,
663 .flush_resources
= flush_resources
,
664 .client_monitors_config
= client_monitors_config
,
665 .set_client_capabilities
= set_client_capabilities
,
670 spice_screen_draw_char(SpiceScreen
*spice_screen
, int x
, int y
, gunichar2 ch
,
671 TextAttributes attrib
)
677 invers
= attrib
.selected
? 0 : 1;
679 invers
= attrib
.selected
? 1 : 0;
694 // unsuported attributes = (attrib.blink || attrib.unvisible)
696 int c
= vt_fontmap
[ch
];
698 SimpleSpiceUpdate
*update
;
699 update
= spice_screen_draw_char_cmd(spice_screen
, x
, y
, c
, fg
, bg
, attrib
.uline
);
700 push_command(spice_screen
, &update
->ext
);
704 spice_screen_new(SpiceCoreInterface
*core
, uint32_t width
, uint32_t height
, guint timeout
)
707 SpiceScreen
*spice_screen
= g_new0(SpiceScreen
, 1);
708 SpiceServer
* server
= spice_server_new();
710 spice_screen
->width
= width
;
711 spice_screen
->height
= height
;
713 spice_screen
->command_cond
= g_cond_new();
714 spice_screen
->command_mutex
= g_mutex_new();
716 spice_screen
->on_client_connected
= client_connected
,
717 spice_screen
->on_client_disconnected
= client_disconnected
,
719 spice_screen
->qxl_instance
.base
.sif
= &display_sif
.base
;
720 spice_screen
->qxl_instance
.id
= 0;
722 spice_screen
->core
= core
;
723 spice_screen
->server
= server
;
725 printf("listening on port %d (unsecure)\n", port
);
727 spice_server_set_port(server
, port
);
728 spice_server_set_noauth(server
);
730 int res
= spice_server_init(server
, core
);
732 g_error("spice_server_init failed, res = %d\n", res
);
737 spice_screen
->conn_timeout_timer
= core
->timer_add(do_conn_timeout
, spice_screen
);
738 spice_screen
->core
->timer_start(spice_screen
->conn_timeout_timer
, timeout
*1000);
740 spice_server_add_interface(spice_screen
->server
, &spice_screen
->qxl_instance
.base
);