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
);
95 release_qxl_command_ext(QXLCommandExt
*ext
)
97 g_assert(ext
!= NULL
);
99 switch (ext
->cmd
.type
) {
101 spice_screen_destroy_update((void*)ext
);
103 case QXL_CMD_SURFACE
:
106 case QXL_CMD_CURSOR
: {
107 QXLCursorCmd
*cmd
= (QXLCursorCmd
*)(unsigned long)ext
->cmd
.data
;
108 if (cmd
->type
== QXL_CURSOR_SET
) {
120 release_resource(QXLInstance
*qin
, struct QXLReleaseInfoExt release_info
)
122 QXLCommandExt
*ext
= (QXLCommandExt
*)(unsigned long)release_info
.info
->id
;
124 g_assert(release_info
.group_id
== MEM_SLOT_GROUP_ID
);
125 release_qxl_command_ext(ext
);
128 static int unique
= 0x0ffff + 1;
131 set_cmd(QXLCommandExt
*ext
, uint32_t type
, QXLPHYSICAL data
)
133 ext
->cmd
.type
= type
;
134 ext
->cmd
.data
= data
;
135 ext
->cmd
.padding
= 0;
136 ext
->group_id
= MEM_SLOT_GROUP_ID
;
141 simple_set_release_info(QXLReleaseInfo
*info
, intptr_t ptr
)
144 //info->group_id = MEM_SLOT_GROUP_ID;
147 /* Note: push_command/get_command are called from different threads */
150 push_command(SpiceScreen
*spice_screen
, QXLCommandExt
*ext
)
154 g_mutex_lock(spice_screen
->command_mutex
);
156 while (spice_screen
->commands_end
- spice_screen
->commands_start
>= COMMANDS_SIZE
) {
157 g_cond_wait(spice_screen
->command_cond
, spice_screen
->command_mutex
);
160 g_assert(spice_screen
->commands_end
- spice_screen
->commands_start
< COMMANDS_SIZE
);
162 if ((spice_screen
->commands_end
- spice_screen
->commands_start
) > 0) {
166 spice_screen
->commands
[spice_screen
->commands_end
% COMMANDS_SIZE
] = ext
;
167 spice_screen
->commands_end
++;
170 spice_screen
->qxl_worker
->wakeup(spice_screen
->qxl_worker
);
173 g_mutex_unlock(spice_screen
->command_mutex
);
177 /* bitmap are freed, so they must be allocated with g_malloc */
178 static SimpleSpiceUpdate
*
179 spice_screen_update_from_bitmap_cmd(uint32_t surface_id
, QXLRect bbox
, uint8_t *bitmap
, int cache_id
)
181 SimpleSpiceUpdate
*update
;
182 QXLDrawable
*drawable
;
186 bh
= bbox
.bottom
- bbox
.top
;
187 bw
= bbox
.right
- bbox
.left
;
189 update
= g_new0(SimpleSpiceUpdate
, 1);
190 update
->bitmap
= bitmap
;
191 drawable
= &update
->drawable
;
192 image
= &update
->image
;
194 drawable
->surface_id
= surface_id
;
196 drawable
->bbox
= bbox
;
197 drawable
->clip
.type
= SPICE_CLIP_TYPE_NONE
;
198 drawable
->effect
= QXL_EFFECT_OPAQUE
;
199 simple_set_release_info(&drawable
->release_info
, (intptr_t)update
);
200 drawable
->type
= QXL_DRAW_COPY
;
201 drawable
->surfaces_dest
[0] = -1;
202 drawable
->surfaces_dest
[1] = -1;
203 drawable
->surfaces_dest
[2] = -1;
205 drawable
->u
.copy
.rop_descriptor
= SPICE_ROPD_OP_PUT
;
206 drawable
->u
.copy
.src_bitmap
= (intptr_t)image
;
207 drawable
->u
.copy
.src_area
.right
= bw
;
208 drawable
->u
.copy
.src_area
.bottom
= bh
;
211 QXL_SET_IMAGE_ID(image
, QXL_IMAGE_GROUP_DEVICE
, cache_id
);
212 image
->descriptor
.flags
= SPICE_IMAGE_FLAGS_CACHE_ME
;
213 update
->cache_id
= cache_id
;
215 QXL_SET_IMAGE_ID(image
, QXL_IMAGE_GROUP_DEVICE
, ++unique
);
217 image
->descriptor
.type
= SPICE_IMAGE_TYPE_BITMAP
;
218 image
->bitmap
.flags
= QXL_BITMAP_DIRECT
| QXL_BITMAP_TOP_DOWN
;
219 image
->bitmap
.stride
= bw
* 4;
220 image
->descriptor
.width
= image
->bitmap
.x
= bw
;
221 image
->descriptor
.height
= image
->bitmap
.y
= bh
;
222 image
->bitmap
.data
= (intptr_t)bitmap
;
223 image
->bitmap
.palette
= 0;
224 image
->bitmap
.format
= SPICE_BITMAP_FMT_32BIT
;
226 set_cmd(&update
->ext
, QXL_CMD_DRAW
, (intptr_t)drawable
);
231 static SimpleSpiceUpdate
*
232 spice_screen_draw_char_cmd(SpiceScreen
*spice_screen
, int x
, int y
, int c
,
233 int fg
, int bg
, gboolean uline
)
237 uint8_t *bitmap
= NULL
;
244 if (!uline
&& c
< 256) {
245 cache_id
= ((fg
<< 12) | (bg
<< 8) | (c
& 255)) & 0x0ffff;
246 if ((ce
= (CachedImage
*)g_hash_table_lookup(spice_screen
->image_cache
, &cache_id
))) {
258 bitmap
= dst
= g_malloc(bw
* bh
* 4);
260 unsigned char *data
= vt_font_data
+ c
*16;
261 unsigned char d
= *data
;
263 g_assert(fg
>= 0 && fg
< 16);
264 g_assert(bg
>= 0 && bg
< 16);
266 unsigned char fgc_red
= default_red
[fg
];
267 unsigned char fgc_blue
= default_blu
[fg
];
268 unsigned char fgc_green
= default_grn
[fg
];
269 unsigned char bgc_red
= default_red
[bg
];
270 unsigned char bgc_blue
= default_blu
[bg
];
271 unsigned char bgc_green
= default_grn
[bg
];
273 for (j
= 0; j
< 16; j
++) {
274 gboolean ul
= (j
== 14) && uline
;
275 for (i
= 0; i
< 8; i
++) {
282 *(dst
+1) = fgc_green
;
287 *(dst
+1) = bgc_green
;
295 ce
= g_new(CachedImage
, 1);
296 ce
->cache_id
= cache_id
;
298 g_hash_table_insert(spice_screen
->image_cache
, &ce
->cache_id
, ce
);
301 bbox
.left
= left
; bbox
.top
= top
;
302 bbox
.right
= left
+ bw
; bbox
.bottom
= top
+ bh
;
304 return spice_screen_update_from_bitmap_cmd(0, bbox
, bitmap
, cache_id
);
308 spice_screen_scroll(SpiceScreen
*spice_screen
, int x1
, int y1
,
309 int x2
, int y2
, int src_x
, int src_y
)
311 SimpleSpiceUpdate
*update
;
312 QXLDrawable
*drawable
;
317 update
= g_new0(SimpleSpiceUpdate
, 1);
318 drawable
= &update
->drawable
;
325 drawable
->surface_id
= surface_id
;
327 drawable
->bbox
= bbox
;
328 drawable
->clip
.type
= SPICE_CLIP_TYPE_NONE
;
329 drawable
->effect
= QXL_EFFECT_OPAQUE
;
330 simple_set_release_info(&drawable
->release_info
, (intptr_t)update
);
331 drawable
->type
= QXL_COPY_BITS
;
332 drawable
->surfaces_dest
[0] = -1;
333 drawable
->surfaces_dest
[1] = -1;
334 drawable
->surfaces_dest
[2] = -1;
336 drawable
->u
.copy_bits
.src_pos
.x
= src_x
;
337 drawable
->u
.copy_bits
.src_pos
.y
= src_y
;
339 set_cmd(&update
->ext
, QXL_CMD_DRAW
, (intptr_t)drawable
);
341 push_command(spice_screen
, &update
->ext
);
345 spice_screen_clear(SpiceScreen
*spice_screen
, int x1
, int y1
, int x2
, int y2
)
347 SimpleSpiceUpdate
*update
;
348 QXLDrawable
*drawable
;
353 update
= g_new0(SimpleSpiceUpdate
, 1);
354 drawable
= &update
->drawable
;
361 drawable
->surface_id
= surface_id
;
363 drawable
->bbox
= bbox
;
364 drawable
->clip
.type
= SPICE_CLIP_TYPE_NONE
;
365 drawable
->effect
= QXL_EFFECT_OPAQUE
;
366 simple_set_release_info(&drawable
->release_info
, (intptr_t)update
);
367 drawable
->type
= QXL_DRAW_BLACKNESS
;
368 drawable
->surfaces_dest
[0] = -1;
369 drawable
->surfaces_dest
[1] = -1;
370 drawable
->surfaces_dest
[2] = -1;
372 set_cmd(&update
->ext
, QXL_CMD_DRAW
, (intptr_t)drawable
);
374 push_command(spice_screen
, &update
->ext
);
378 create_primary_surface(SpiceScreen
*spice_screen
, uint32_t width
,
381 QXLWorker
*qxl_worker
= spice_screen
->qxl_worker
;
382 QXLDevSurfaceCreate surface
= { 0, };
384 g_assert(height
> 0);
387 if (height
> MAX_HEIGHT
)
390 if (width
> MAX_WIDTH
)
393 surface
.format
= SPICE_SURFACE_FMT_32_xRGB
;
394 surface
.width
= spice_screen
->primary_width
= width
;
395 surface
.height
= spice_screen
->primary_height
= height
;
396 surface
.stride
= -width
* 4; /* negative? */
397 surface
.mouse_mode
= TRUE
; /* unused by red_worker */
399 surface
.type
= 0; /* unused by red_worker */
400 surface
.position
= 0; /* unused by red_worker */
401 surface
.mem
= (uint64_t)&spice_screen
->primary_surface
;
402 surface
.group_id
= MEM_SLOT_GROUP_ID
;
404 spice_screen
->width
= width
;
405 spice_screen
->height
= height
;
407 spice_screen
->cursor_set
= 0;
409 qxl_worker
->create_primary_surface(qxl_worker
, 0, &surface
);
412 QXLDevMemSlot slot
= {
413 .slot_group_id
= MEM_SLOT_GROUP_ID
,
423 attache_worker(QXLInstance
*qin
, QXLWorker
*_qxl_worker
)
425 SpiceScreen
*spice_screen
= SPICE_CONTAINEROF(qin
, SpiceScreen
, qxl_instance
);
427 if (spice_screen
->qxl_worker
) {
428 g_assert_not_reached();
431 spice_screen
->qxl_worker
= _qxl_worker
;
432 spice_screen
->qxl_worker
->add_memslot(spice_screen
->qxl_worker
, &slot
);
433 create_primary_surface(spice_screen
, spice_screen
->width
, spice_screen
->height
);
434 spice_screen
->qxl_worker
->start(spice_screen
->qxl_worker
);
438 set_compression_level(QXLInstance
*qin
, int level
)
444 set_mm_time(QXLInstance
*qin
, uint32_t mm_time
)
450 get_init_info(QXLInstance
*qin
, QXLDevInitInfo
*info
)
452 memset(info
, 0, sizeof(*info
));
453 info
->num_memslots
= 1;
454 info
->num_memslots_groups
= 1;
455 info
->memslot_id_bits
= 1;
456 info
->memslot_gen_bits
= 1;
457 info
->n_surfaces
= 1;
460 /* called from spice_server thread (i.e. red_worker thread) */
462 get_command(QXLInstance
*qin
, struct QXLCommandExt
*ext
)
464 SpiceScreen
*spice_screen
= SPICE_CONTAINEROF(qin
, SpiceScreen
, qxl_instance
);
467 g_mutex_lock(spice_screen
->command_mutex
);
469 if ((spice_screen
->commands_end
- spice_screen
->commands_start
) == 0) {
474 *ext
= *spice_screen
->commands
[spice_screen
->commands_start
% COMMANDS_SIZE
];
475 g_assert(spice_screen
->commands_start
< spice_screen
->commands_end
);
476 spice_screen
->commands_start
++;
477 g_cond_signal(spice_screen
->command_cond
);
482 g_mutex_unlock(spice_screen
->command_mutex
);
487 discard_pending_commands(SpiceScreen
*spice_screen
)
491 g_mutex_lock(spice_screen
->command_mutex
);
492 for (pos
= spice_screen
->commands_start
; pos
< spice_screen
->commands_end
; pos
++) {
493 release_qxl_command_ext(spice_screen
->commands
[pos
% COMMANDS_SIZE
]);
495 spice_screen
->commands_start
= spice_screen
->commands_end
;
496 g_mutex_unlock(spice_screen
->command_mutex
);
500 req_cmd_notification(QXLInstance
*qin
)
502 //SpiceScreen *spice_screen = SPICE_CONTAINEROF(qin, SpiceScreen, qxl_instance);
503 //spice_screen->core->timer_start(spice_screen->wakeup_timer, spice_screen->wakeup_ms);
508 #define CURSOR_WIDTH 8
509 #define CURSOR_HEIGHT 16
513 uint8_t data
[CURSOR_WIDTH
* CURSOR_HEIGHT
* 4]; // 32bit per pixel
519 cursor
.cursor
.header
.unique
= 0;
520 cursor
.cursor
.header
.type
= SPICE_CURSOR_TYPE_COLOR32
;
521 cursor
.cursor
.header
.width
= CURSOR_WIDTH
;
522 cursor
.cursor
.header
.height
= CURSOR_HEIGHT
;
523 cursor
.cursor
.header
.hot_spot_x
= 0;
524 cursor
.cursor
.header
.hot_spot_y
= 0;
525 cursor
.cursor
.data_size
= CURSOR_WIDTH
* CURSOR_HEIGHT
* 4;
527 // X drivers addes it to the cursor size because it could be
528 // cursor data information or another cursor related stuffs.
529 // Otherwise, the code will break in client/cursor.cpp side,
530 // that expect the data_size plus cursor information.
531 // Blame cursor protocol for this. :-)
532 cursor
.cursor
.data_size
+= 128;
533 cursor
.cursor
.chunk
.data_size
= cursor
.cursor
.data_size
;
534 cursor
.cursor
.chunk
.prev_chunk
= cursor
.cursor
.chunk
.next_chunk
= 0;
538 get_cursor_command(QXLInstance
*qin
, struct QXLCommandExt
*ext
)
540 SpiceScreen
*spice_screen
= SPICE_CONTAINEROF(qin
, SpiceScreen
, qxl_instance
);
542 QXLCursorCmd
*cursor_cmd
;
545 if (spice_screen
->cursor_set
)
548 spice_screen
->cursor_set
= 1;
550 cmd
= calloc(sizeof(QXLCommandExt
), 1);
551 cursor_cmd
= calloc(sizeof(QXLCursorCmd
), 1);
553 cursor_cmd
->release_info
.id
= (unsigned long)cmd
;
555 cursor_cmd
->type
= QXL_CURSOR_SET
;
556 cursor_cmd
->u
.set
.position
.x
= 0;
557 cursor_cmd
->u
.set
.position
.y
= 0;
558 cursor_cmd
->u
.set
.visible
= TRUE
;
559 cursor_cmd
->u
.set
.shape
= (unsigned long)&cursor
;
560 // white rect as cursor
561 memset(cursor
.data
, 255, sizeof(cursor
.data
));
563 cmd
->cmd
.data
= (unsigned long)cursor_cmd
;
564 cmd
->cmd
.type
= QXL_CMD_CURSOR
;
565 cmd
->group_id
= MEM_SLOT_GROUP_ID
;
573 req_cursor_notification(QXLInstance
*qin
)
581 notify_update(QXLInstance
*qin
, uint32_t update_id
)
587 flush_resources(QXLInstance
*qin
)
595 client_monitors_config(QXLInstance
*qin
, VDAgentMonitorsConfig
*monitors_config
)
603 set_client_capabilities(QXLInstance
*qin
, uint8_t client_present
,
606 SpiceScreen
*spice_screen
= SPICE_CONTAINEROF(qin
, SpiceScreen
, qxl_instance
);
608 DPRINTF(1, "present %d caps %d", client_present
, caps
[0]);
610 if (spice_screen
->on_client_connected
&& client_present
) {
611 spice_screen
->on_client_connected(spice_screen
);
613 if (spice_screen
->on_client_disconnected
&& !client_present
) {
614 spice_screen
->on_client_disconnected(spice_screen
);
618 static int client_count
= 0;
621 client_connected(SpiceScreen
*spice_screen
)
625 DPRINTF(1, "client_count = %d", client_count
);
629 client_disconnected(SpiceScreen
*spice_screen
)
631 if (client_count
> 0) {
633 DPRINTF(1, "client_count = %d", client_count
);
634 exit(0); // fixme: cleanup?
639 do_conn_timeout(void *opaque
)
641 // SpiceScreen *spice_screen = opaque;
643 if (client_count
<= 0) {
644 printf("connection timeout - stopping server\n");
645 exit (0); // fixme: cleanup?
650 QXLInterface display_sif
= {
652 .type
= SPICE_INTERFACE_QXL
,
653 .description
= "spiceterm display server",
654 .major_version
= SPICE_INTERFACE_QXL_MAJOR
,
655 .minor_version
= SPICE_INTERFACE_QXL_MINOR
657 .attache_worker
= attache_worker
,
658 .set_compression_level
= set_compression_level
,
659 .set_mm_time
= set_mm_time
,
660 .get_init_info
= get_init_info
,
662 /* the callbacks below are called from spice server thread context */
663 .get_command
= get_command
,
664 .req_cmd_notification
= req_cmd_notification
,
665 .release_resource
= release_resource
,
666 .get_cursor_command
= get_cursor_command
,
667 .req_cursor_notification
= req_cursor_notification
,
668 .notify_update
= notify_update
,
669 .flush_resources
= flush_resources
,
670 .client_monitors_config
= client_monitors_config
,
671 .set_client_capabilities
= set_client_capabilities
,
676 spice_screen_draw_char(SpiceScreen
*spice_screen
, int x
, int y
, gunichar2 ch
,
677 TextAttributes attrib
)
683 invers
= attrib
.selected
? 0 : 1;
685 invers
= attrib
.selected
? 1 : 0;
700 // unsuported attributes = (attrib.blink || attrib.unvisible)
702 int c
= vt_fontmap
[ch
];
704 SimpleSpiceUpdate
*update
;
705 update
= spice_screen_draw_char_cmd(spice_screen
, x
, y
, c
, fg
, bg
, attrib
.uline
);
706 push_command(spice_screen
, &update
->ext
);
710 spice_screen_new(SpiceCoreInterface
*core
, uint32_t width
, uint32_t height
, guint timeout
)
713 SpiceScreen
*spice_screen
= g_new0(SpiceScreen
, 1);
714 SpiceServer
* server
= spice_server_new();
716 spice_screen
->width
= width
;
717 spice_screen
->height
= height
;
719 spice_screen
->command_cond
= g_cond_new();
720 spice_screen
->command_mutex
= g_mutex_new();
722 spice_screen
->on_client_connected
= client_connected
,
723 spice_screen
->on_client_disconnected
= client_disconnected
,
725 spice_screen
->qxl_instance
.base
.sif
= &display_sif
.base
;
726 spice_screen
->qxl_instance
.id
= 0;
728 spice_screen
->core
= core
;
729 spice_screen
->server
= server
;
731 printf("listening on port %d (unsecure)\n", port
);
733 spice_server_set_port(server
, port
);
734 spice_server_set_noauth(server
);
736 int res
= spice_server_init(server
, core
);
738 g_error("spice_server_init failed, res = %d\n", res
);
743 spice_screen
->conn_timeout_timer
= core
->timer_add(do_conn_timeout
, spice_screen
);
744 spice_screen
->core
->timer_start(spice_screen
->conn_timeout_timer
, timeout
*1000);
746 spice_server_add_interface(spice_screen
->server
, &spice_screen
->qxl_instance
.base
);
752 spice_screen_resize(SpiceScreen
*spice_screen
, uint32_t width
,
755 QXLWorker
*qxl_worker
= spice_screen
->qxl_worker
;
757 if (spice_screen
->width
== width
&& spice_screen
->height
== height
) {
761 discard_pending_commands(spice_screen
);
763 qxl_worker
->destroy_primary_surface(qxl_worker
, 0);
765 create_primary_surface(spice_screen
, width
, height
);
767 spice_screen_clear(spice_screen
, 0, 0, width
, height
);