Merge remote-tracking branch 'remotes/kraxel/tags/pull-console-20140905-2' into staging
[sdk/emulator/qemu.git] / ui / spice-display.c
1 /*
2  * Copyright (C) 2010 Red Hat, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 or
7  * (at your option) version 3 of the License.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "qemu-common.h"
19 #include "ui/qemu-spice.h"
20 #include "qemu/timer.h"
21 #include "qemu/queue.h"
22 #include "monitor/monitor.h"
23 #include "ui/console.h"
24 #include "sysemu/sysemu.h"
25 #include "trace.h"
26
27 #include "ui/spice-display.h"
28
29 static int debug = 0;
30
31 static void GCC_FMT_ATTR(2, 3) dprint(int level, const char *fmt, ...)
32 {
33     va_list args;
34
35     if (level <= debug) {
36         va_start(args, fmt);
37         vfprintf(stderr, fmt, args);
38         va_end(args);
39     }
40 }
41
42 int qemu_spice_rect_is_empty(const QXLRect* r)
43 {
44     return r->top == r->bottom || r->left == r->right;
45 }
46
47 void qemu_spice_rect_union(QXLRect *dest, const QXLRect *r)
48 {
49     if (qemu_spice_rect_is_empty(r)) {
50         return;
51     }
52
53     if (qemu_spice_rect_is_empty(dest)) {
54         *dest = *r;
55         return;
56     }
57
58     dest->top = MIN(dest->top, r->top);
59     dest->left = MIN(dest->left, r->left);
60     dest->bottom = MAX(dest->bottom, r->bottom);
61     dest->right = MAX(dest->right, r->right);
62 }
63
64 QXLCookie *qxl_cookie_new(int type, uint64_t io)
65 {
66     QXLCookie *cookie;
67
68     cookie = g_malloc0(sizeof(*cookie));
69     cookie->type = type;
70     cookie->io = io;
71     return cookie;
72 }
73
74 void qemu_spice_add_memslot(SimpleSpiceDisplay *ssd, QXLDevMemSlot *memslot,
75                             qxl_async_io async)
76 {
77     trace_qemu_spice_add_memslot(ssd->qxl.id, memslot->slot_id,
78                                 memslot->virt_start, memslot->virt_end,
79                                 async);
80
81     if (async != QXL_SYNC) {
82         spice_qxl_add_memslot_async(&ssd->qxl, memslot,
83                 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
84                                           QXL_IO_MEMSLOT_ADD_ASYNC));
85     } else {
86         spice_qxl_add_memslot(&ssd->qxl, memslot);
87     }
88 }
89
90 void qemu_spice_del_memslot(SimpleSpiceDisplay *ssd, uint32_t gid, uint32_t sid)
91 {
92     trace_qemu_spice_del_memslot(ssd->qxl.id, gid, sid);
93     spice_qxl_del_memslot(&ssd->qxl, gid, sid);
94 }
95
96 void qemu_spice_create_primary_surface(SimpleSpiceDisplay *ssd, uint32_t id,
97                                        QXLDevSurfaceCreate *surface,
98                                        qxl_async_io async)
99 {
100     trace_qemu_spice_create_primary_surface(ssd->qxl.id, id, surface, async);
101     if (async != QXL_SYNC) {
102         spice_qxl_create_primary_surface_async(&ssd->qxl, id, surface,
103                 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
104                                           QXL_IO_CREATE_PRIMARY_ASYNC));
105     } else {
106         spice_qxl_create_primary_surface(&ssd->qxl, id, surface);
107     }
108 }
109
110 void qemu_spice_destroy_primary_surface(SimpleSpiceDisplay *ssd,
111                                         uint32_t id, qxl_async_io async)
112 {
113     trace_qemu_spice_destroy_primary_surface(ssd->qxl.id, id, async);
114     if (async != QXL_SYNC) {
115         spice_qxl_destroy_primary_surface_async(&ssd->qxl, id,
116                 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
117                                           QXL_IO_DESTROY_PRIMARY_ASYNC));
118     } else {
119         spice_qxl_destroy_primary_surface(&ssd->qxl, id);
120     }
121 }
122
123 void qemu_spice_wakeup(SimpleSpiceDisplay *ssd)
124 {
125     trace_qemu_spice_wakeup(ssd->qxl.id);
126     spice_qxl_wakeup(&ssd->qxl);
127 }
128
129 static void qemu_spice_create_one_update(SimpleSpiceDisplay *ssd,
130                                          QXLRect *rect)
131 {
132     SimpleSpiceUpdate *update;
133     QXLDrawable *drawable;
134     QXLImage *image;
135     QXLCommand *cmd;
136     int bw, bh;
137     struct timespec time_space;
138     pixman_image_t *dest;
139
140     trace_qemu_spice_create_update(
141            rect->left, rect->right,
142            rect->top, rect->bottom);
143
144     update   = g_malloc0(sizeof(*update));
145     drawable = &update->drawable;
146     image    = &update->image;
147     cmd      = &update->ext.cmd;
148
149     bw       = rect->right - rect->left;
150     bh       = rect->bottom - rect->top;
151     update->bitmap = g_malloc(bw * bh * 4);
152
153     drawable->bbox            = *rect;
154     drawable->clip.type       = SPICE_CLIP_TYPE_NONE;
155     drawable->effect          = QXL_EFFECT_OPAQUE;
156     drawable->release_info.id = (uintptr_t)(&update->ext);
157     drawable->type            = QXL_DRAW_COPY;
158     drawable->surfaces_dest[0] = -1;
159     drawable->surfaces_dest[1] = -1;
160     drawable->surfaces_dest[2] = -1;
161     clock_gettime(CLOCK_MONOTONIC, &time_space);
162     /* time in milliseconds from epoch. */
163     drawable->mm_time = time_space.tv_sec * 1000
164                       + time_space.tv_nsec / 1000 / 1000;
165
166     drawable->u.copy.rop_descriptor  = SPICE_ROPD_OP_PUT;
167     drawable->u.copy.src_bitmap      = (uintptr_t)image;
168     drawable->u.copy.src_area.right  = bw;
169     drawable->u.copy.src_area.bottom = bh;
170
171     QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, ssd->unique++);
172     image->descriptor.type   = SPICE_IMAGE_TYPE_BITMAP;
173     image->bitmap.flags      = QXL_BITMAP_DIRECT | QXL_BITMAP_TOP_DOWN;
174     image->bitmap.stride     = bw * 4;
175     image->descriptor.width  = image->bitmap.x = bw;
176     image->descriptor.height = image->bitmap.y = bh;
177     image->bitmap.data = (uintptr_t)(update->bitmap);
178     image->bitmap.palette = 0;
179     image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
180
181     dest = pixman_image_create_bits(PIXMAN_x8r8g8b8, bw, bh,
182                                     (void *)update->bitmap, bw * 4);
183     pixman_image_composite(PIXMAN_OP_SRC, ssd->surface, NULL, ssd->mirror,
184                            rect->left, rect->top, 0, 0,
185                            rect->left, rect->top, bw, bh);
186     pixman_image_composite(PIXMAN_OP_SRC, ssd->mirror, NULL, dest,
187                            rect->left, rect->top, 0, 0,
188                            0, 0, bw, bh);
189     pixman_image_unref(dest);
190
191     cmd->type = QXL_CMD_DRAW;
192     cmd->data = (uintptr_t)drawable;
193
194     QTAILQ_INSERT_TAIL(&ssd->updates, update, next);
195 }
196
197 static void qemu_spice_create_update(SimpleSpiceDisplay *ssd)
198 {
199     static const int blksize = 32;
200     int blocks = (surface_width(ssd->ds) + blksize - 1) / blksize;
201     int dirty_top[blocks];
202     int y, yoff, x, xoff, blk, bw;
203     int bpp = surface_bytes_per_pixel(ssd->ds);
204     uint8_t *guest, *mirror;
205
206     if (qemu_spice_rect_is_empty(&ssd->dirty)) {
207         return;
208     };
209
210     if (ssd->surface == NULL) {
211         ssd->surface = pixman_image_ref(ssd->ds->image);
212         ssd->mirror  = qemu_pixman_mirror_create(ssd->ds->format,
213                                                  ssd->ds->image);
214     }
215
216     for (blk = 0; blk < blocks; blk++) {
217         dirty_top[blk] = -1;
218     }
219
220     guest = surface_data(ssd->ds);
221     mirror = (void *)pixman_image_get_data(ssd->mirror);
222     for (y = ssd->dirty.top; y < ssd->dirty.bottom; y++) {
223         yoff = y * surface_stride(ssd->ds);
224         for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
225             xoff = x * bpp;
226             blk = x / blksize;
227             bw = MIN(blksize, ssd->dirty.right - x);
228             if (memcmp(guest + yoff + xoff,
229                        mirror + yoff + xoff,
230                        bw * bpp) == 0) {
231                 if (dirty_top[blk] != -1) {
232                     QXLRect update = {
233                         .top    = dirty_top[blk],
234                         .bottom = y,
235                         .left   = x,
236                         .right  = x + bw,
237                     };
238                     qemu_spice_create_one_update(ssd, &update);
239                     dirty_top[blk] = -1;
240                 }
241             } else {
242                 if (dirty_top[blk] == -1) {
243                     dirty_top[blk] = y;
244                 }
245             }
246         }
247     }
248
249     for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
250         blk = x / blksize;
251         bw = MIN(blksize, ssd->dirty.right - x);
252         if (dirty_top[blk] != -1) {
253             QXLRect update = {
254                 .top    = dirty_top[blk],
255                 .bottom = ssd->dirty.bottom,
256                 .left   = x,
257                 .right  = x + bw,
258             };
259             qemu_spice_create_one_update(ssd, &update);
260             dirty_top[blk] = -1;
261         }
262     }
263
264     memset(&ssd->dirty, 0, sizeof(ssd->dirty));
265 }
266
267 static SimpleSpiceCursor*
268 qemu_spice_create_cursor_update(SimpleSpiceDisplay *ssd,
269                                 QEMUCursor *c)
270 {
271     size_t size = c ? c->width * c->height * 4 : 0;
272     SimpleSpiceCursor *update;
273     QXLCursorCmd *ccmd;
274     QXLCursor *cursor;
275     QXLCommand *cmd;
276
277     update   = g_malloc0(sizeof(*update) + size);
278     ccmd     = &update->cmd;
279     cursor   = &update->cursor;
280     cmd      = &update->ext.cmd;
281
282     if (c) {
283         ccmd->type = QXL_CURSOR_SET;
284         ccmd->u.set.position.x = ssd->ptr_x;
285         ccmd->u.set.position.y = ssd->ptr_y;
286         ccmd->u.set.visible    = true;
287         ccmd->u.set.shape      = (uintptr_t)cursor;
288         cursor->header.unique     = ssd->unique++;
289         cursor->header.type       = SPICE_CURSOR_TYPE_ALPHA;
290         cursor->header.width      = c->width;
291         cursor->header.height     = c->height;
292         cursor->header.hot_spot_x = c->hot_x;
293         cursor->header.hot_spot_y = c->hot_y;
294         cursor->data_size         = size;
295         cursor->chunk.data_size   = size;
296         memcpy(cursor->chunk.data, c->data, size);
297     } else {
298         ccmd->type = QXL_CURSOR_MOVE;
299         ccmd->u.position.x = ssd->ptr_x;
300         ccmd->u.position.y = ssd->ptr_y;
301     }
302     ccmd->release_info.id = (uintptr_t)(&update->ext);
303
304     cmd->type = QXL_CMD_CURSOR;
305     cmd->data = (uintptr_t)ccmd;
306
307     return update;
308 }
309
310 /*
311  * Called from spice server thread context (via interface_release_resource)
312  * We do *not* hold the global qemu mutex here, so extra care is needed
313  * when calling qemu functions.  QEMU interfaces used:
314  *    - g_free (underlying glibc free is re-entrant).
315  */
316 void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
317 {
318     g_free(update->bitmap);
319     g_free(update);
320 }
321
322 void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
323 {
324     QXLDevMemSlot memslot;
325
326     dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
327
328     memset(&memslot, 0, sizeof(memslot));
329     memslot.slot_group_id = MEMSLOT_GROUP_HOST;
330     memslot.virt_end = ~0;
331     qemu_spice_add_memslot(ssd, &memslot, QXL_SYNC);
332 }
333
334 void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
335 {
336     QXLDevSurfaceCreate surface;
337     uint64_t surface_size;
338
339     memset(&surface, 0, sizeof(surface));
340
341     surface_size = (uint64_t) surface_width(ssd->ds) *
342         surface_height(ssd->ds) * 4;
343     assert(surface_size > 0);
344     assert(surface_size < INT_MAX);
345     if (ssd->bufsize < surface_size) {
346         ssd->bufsize = surface_size;
347         g_free(ssd->buf);
348         ssd->buf = g_malloc(ssd->bufsize);
349     }
350
351     dprint(1, "%s/%d: %ux%u (size %" PRIu64 "/%d)\n", __func__, ssd->qxl.id,
352            surface_width(ssd->ds), surface_height(ssd->ds),
353            surface_size, ssd->bufsize);
354
355     surface.format     = SPICE_SURFACE_FMT_32_xRGB;
356     surface.width      = surface_width(ssd->ds);
357     surface.height     = surface_height(ssd->ds);
358     surface.stride     = -surface.width * 4;
359     surface.mouse_mode = true;
360     surface.flags      = 0;
361     surface.type       = 0;
362     surface.mem        = (uintptr_t)ssd->buf;
363     surface.group_id   = MEMSLOT_GROUP_HOST;
364
365     qemu_spice_create_primary_surface(ssd, 0, &surface, QXL_SYNC);
366 }
367
368 void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
369 {
370     dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
371
372     qemu_spice_destroy_primary_surface(ssd, 0, QXL_SYNC);
373 }
374
375 void qemu_spice_display_init_common(SimpleSpiceDisplay *ssd)
376 {
377     qemu_mutex_init(&ssd->lock);
378     QTAILQ_INIT(&ssd->updates);
379     ssd->mouse_x = -1;
380     ssd->mouse_y = -1;
381     if (ssd->num_surfaces == 0) {
382         ssd->num_surfaces = 1024;
383     }
384 }
385
386 /* display listener callbacks */
387
388 void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
389                                int x, int y, int w, int h)
390 {
391     QXLRect update_area;
392
393     dprint(2, "%s/%d: x %d y %d w %d h %d\n", __func__,
394            ssd->qxl.id, x, y, w, h);
395     update_area.left = x,
396     update_area.right = x + w;
397     update_area.top = y;
398     update_area.bottom = y + h;
399
400     if (qemu_spice_rect_is_empty(&ssd->dirty)) {
401         ssd->notify++;
402     }
403     qemu_spice_rect_union(&ssd->dirty, &update_area);
404 }
405
406 void qemu_spice_display_switch(SimpleSpiceDisplay *ssd,
407                                DisplaySurface *surface)
408 {
409     SimpleSpiceUpdate *update;
410     bool need_destroy;
411
412     dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
413
414     memset(&ssd->dirty, 0, sizeof(ssd->dirty));
415     if (ssd->surface) {
416         pixman_image_unref(ssd->surface);
417         ssd->surface = NULL;
418         pixman_image_unref(ssd->mirror);
419         ssd->mirror = NULL;
420     }
421
422     qemu_mutex_lock(&ssd->lock);
423     need_destroy = (ssd->ds != NULL);
424     ssd->ds = surface;
425     while ((update = QTAILQ_FIRST(&ssd->updates)) != NULL) {
426         QTAILQ_REMOVE(&ssd->updates, update, next);
427         qemu_spice_destroy_update(ssd, update);
428     }
429     qemu_mutex_unlock(&ssd->lock);
430     if (need_destroy) {
431         qemu_spice_destroy_host_primary(ssd);
432     }
433     if (ssd->ds) {
434         qemu_spice_create_host_primary(ssd);
435     }
436
437     memset(&ssd->dirty, 0, sizeof(ssd->dirty));
438     ssd->notify++;
439 }
440
441 void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay *ssd)
442 {
443     if (ssd->cursor) {
444         assert(ssd->dcl.con);
445         dpy_cursor_define(ssd->dcl.con, ssd->cursor);
446         cursor_put(ssd->cursor);
447         ssd->cursor = NULL;
448     }
449     if (ssd->mouse_x != -1 && ssd->mouse_y != -1) {
450         assert(ssd->dcl.con);
451         dpy_mouse_set(ssd->dcl.con, ssd->mouse_x, ssd->mouse_y, 1);
452         ssd->mouse_x = -1;
453         ssd->mouse_y = -1;
454     }
455 }
456
457 void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
458 {
459     dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
460     graphic_hw_update(ssd->dcl.con);
461
462     qemu_mutex_lock(&ssd->lock);
463     if (QTAILQ_EMPTY(&ssd->updates) && ssd->ds) {
464         qemu_spice_create_update(ssd);
465         ssd->notify++;
466     }
467     qemu_spice_cursor_refresh_unlocked(ssd);
468     qemu_mutex_unlock(&ssd->lock);
469
470     if (ssd->notify) {
471         ssd->notify = 0;
472         qemu_spice_wakeup(ssd);
473         dprint(2, "%s/%d: notify\n", __func__, ssd->qxl.id);
474     }
475 }
476
477 /* spice display interface callbacks */
478
479 static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
480 {
481     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
482
483     dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
484     ssd->worker = qxl_worker;
485 }
486
487 static void interface_set_compression_level(QXLInstance *sin, int level)
488 {
489     dprint(1, "%s/%d:\n", __func__, sin->id);
490     /* nothing to do */
491 }
492
493 static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
494 {
495     dprint(3, "%s/%d:\n", __func__, sin->id);
496     /* nothing to do */
497 }
498
499 static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
500 {
501     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
502
503     info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
504     info->memslot_id_bits  = MEMSLOT_SLOT_BITS;
505     info->num_memslots = NUM_MEMSLOTS;
506     info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
507     info->internal_groupslot_id = 0;
508     info->qxl_ram_size = 16 * 1024 * 1024;
509     info->n_surfaces = ssd->num_surfaces;
510 }
511
512 static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
513 {
514     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
515     SimpleSpiceUpdate *update;
516     int ret = false;
517
518     dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
519
520     qemu_mutex_lock(&ssd->lock);
521     update = QTAILQ_FIRST(&ssd->updates);
522     if (update != NULL) {
523         QTAILQ_REMOVE(&ssd->updates, update, next);
524         *ext = update->ext;
525         ret = true;
526     }
527     qemu_mutex_unlock(&ssd->lock);
528
529     return ret;
530 }
531
532 static int interface_req_cmd_notification(QXLInstance *sin)
533 {
534     dprint(1, "%s/%d:\n", __func__, sin->id);
535     return 1;
536 }
537
538 static void interface_release_resource(QXLInstance *sin,
539                                        struct QXLReleaseInfoExt rext)
540 {
541     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
542     SimpleSpiceUpdate *update;
543     SimpleSpiceCursor *cursor;
544     QXLCommandExt *ext;
545
546     dprint(2, "%s/%d:\n", __func__, ssd->qxl.id);
547     ext = (void *)(intptr_t)(rext.info->id);
548     switch (ext->cmd.type) {
549     case QXL_CMD_DRAW:
550         update = container_of(ext, SimpleSpiceUpdate, ext);
551         qemu_spice_destroy_update(ssd, update);
552         break;
553     case QXL_CMD_CURSOR:
554         cursor = container_of(ext, SimpleSpiceCursor, ext);
555         g_free(cursor);
556         break;
557     default:
558         g_assert_not_reached();
559     }
560 }
561
562 static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext)
563 {
564     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
565     int ret;
566
567     dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
568
569     qemu_mutex_lock(&ssd->lock);
570     if (ssd->ptr_define) {
571         *ext = ssd->ptr_define->ext;
572         ssd->ptr_define = NULL;
573         ret = true;
574     } else if (ssd->ptr_move) {
575         *ext = ssd->ptr_move->ext;
576         ssd->ptr_move = NULL;
577         ret = true;
578     } else {
579         ret = false;
580     }
581     qemu_mutex_unlock(&ssd->lock);
582     return ret;
583 }
584
585 static int interface_req_cursor_notification(QXLInstance *sin)
586 {
587     dprint(1, "%s:\n", __FUNCTION__);
588     return 1;
589 }
590
591 static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
592 {
593     fprintf(stderr, "%s: abort()\n", __FUNCTION__);
594     abort();
595 }
596
597 static int interface_flush_resources(QXLInstance *sin)
598 {
599     fprintf(stderr, "%s: abort()\n", __FUNCTION__);
600     abort();
601     return 0;
602 }
603
604 static void interface_update_area_complete(QXLInstance *sin,
605         uint32_t surface_id,
606         QXLRect *dirty, uint32_t num_updated_rects)
607 {
608     /* should never be called, used in qxl native mode only */
609     fprintf(stderr, "%s: abort()\n", __func__);
610     abort();
611 }
612
613 /* called from spice server thread context only */
614 static void interface_async_complete(QXLInstance *sin, uint64_t cookie_token)
615 {
616     /* should never be called, used in qxl native mode only */
617     fprintf(stderr, "%s: abort()\n", __func__);
618     abort();
619 }
620
621 static void interface_set_client_capabilities(QXLInstance *sin,
622                                               uint8_t client_present,
623                                               uint8_t caps[58])
624 {
625     dprint(3, "%s:\n", __func__);
626 }
627
628 static int interface_client_monitors_config(QXLInstance *sin,
629                                             VDAgentMonitorsConfig *mc)
630 {
631     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
632     QemuUIInfo info;
633     int rc;
634
635     if (!mc) {
636         return 1;
637     }
638
639     /*
640      * FIXME: multihead is tricky due to the way
641      * spice has multihead implemented.
642      */
643     memset(&info, 0, sizeof(info));
644     if (mc->num_of_monitors > 0) {
645         info.width  = mc->monitors[0].width;
646         info.height = mc->monitors[0].height;
647     }
648     rc = dpy_set_ui_info(ssd->dcl.con, &info);
649     dprint(1, "%s/%d: size %dx%d, rc %d   <---   ==========================\n",
650            __func__, ssd->qxl.id, info.width, info.height, rc);
651     if (rc != 0) {
652         return 0; /* == not supported by guest */
653     } else {
654         return 1;
655     }
656 }
657
658 static const QXLInterface dpy_interface = {
659     .base.type               = SPICE_INTERFACE_QXL,
660     .base.description        = "qemu simple display",
661     .base.major_version      = SPICE_INTERFACE_QXL_MAJOR,
662     .base.minor_version      = SPICE_INTERFACE_QXL_MINOR,
663
664     .attache_worker          = interface_attach_worker,
665     .set_compression_level   = interface_set_compression_level,
666     .set_mm_time             = interface_set_mm_time,
667     .get_init_info           = interface_get_init_info,
668
669     /* the callbacks below are called from spice server thread context */
670     .get_command             = interface_get_command,
671     .req_cmd_notification    = interface_req_cmd_notification,
672     .release_resource        = interface_release_resource,
673     .get_cursor_command      = interface_get_cursor_command,
674     .req_cursor_notification = interface_req_cursor_notification,
675     .notify_update           = interface_notify_update,
676     .flush_resources         = interface_flush_resources,
677     .async_complete          = interface_async_complete,
678     .update_area_complete    = interface_update_area_complete,
679     .set_client_capabilities = interface_set_client_capabilities,
680     .client_monitors_config  = interface_client_monitors_config,
681 };
682
683 static void display_update(DisplayChangeListener *dcl,
684                            int x, int y, int w, int h)
685 {
686     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
687     qemu_spice_display_update(ssd, x, y, w, h);
688 }
689
690 static void display_switch(DisplayChangeListener *dcl,
691                            struct DisplaySurface *surface)
692 {
693     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
694     qemu_spice_display_switch(ssd, surface);
695 }
696
697 static void display_refresh(DisplayChangeListener *dcl)
698 {
699     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
700     qemu_spice_display_refresh(ssd);
701 }
702
703 static void display_mouse_set(DisplayChangeListener *dcl,
704                               int x, int y, int on)
705 {
706     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
707
708     qemu_mutex_lock(&ssd->lock);
709     ssd->ptr_x = x;
710     ssd->ptr_y = x;
711     if (ssd->ptr_move) {
712         g_free(ssd->ptr_move);
713     }
714     ssd->ptr_move = qemu_spice_create_cursor_update(ssd, NULL);
715     qemu_mutex_unlock(&ssd->lock);
716 }
717
718 static void display_mouse_define(DisplayChangeListener *dcl,
719                                  QEMUCursor *c)
720 {
721     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
722
723     qemu_mutex_lock(&ssd->lock);
724     if (ssd->ptr_move) {
725         g_free(ssd->ptr_move);
726         ssd->ptr_move = NULL;
727     }
728     if (ssd->ptr_define) {
729         g_free(ssd->ptr_define);
730     }
731     ssd->ptr_define = qemu_spice_create_cursor_update(ssd, c);
732     qemu_mutex_unlock(&ssd->lock);
733 }
734
735 static const DisplayChangeListenerOps display_listener_ops = {
736     .dpy_name          = "spice",
737     .dpy_gfx_update    = display_update,
738     .dpy_gfx_switch    = display_switch,
739     .dpy_refresh       = display_refresh,
740     .dpy_mouse_set     = display_mouse_set,
741     .dpy_cursor_define = display_mouse_define,
742 };
743
744 static void qemu_spice_display_init_one(QemuConsole *con)
745 {
746     SimpleSpiceDisplay *ssd = g_new0(SimpleSpiceDisplay, 1);
747
748     qemu_spice_display_init_common(ssd);
749
750     ssd->qxl.base.sif = &dpy_interface.base;
751     qemu_spice_add_display_interface(&ssd->qxl, con);
752     assert(ssd->worker);
753
754     qemu_spice_create_host_memslot(ssd);
755
756     ssd->dcl.ops = &display_listener_ops;
757     ssd->dcl.con = con;
758     register_displaychangelistener(&ssd->dcl);
759 }
760
761 void qemu_spice_display_init(void)
762 {
763     QemuConsole *con;
764     int i;
765
766     for (i = 0;; i++) {
767         con = qemu_console_lookup_by_index(i);
768         if (!con || !qemu_console_is_graphic(con)) {
769             break;
770         }
771         if (qemu_spice_have_display_interface(con)) {
772             continue;
773         }
774         qemu_spice_display_init_one(con);
775     }
776 }