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