1 /*
2 * Xephyr - A kdrive X server thats runs in a host X window.
3 * Authored by Matthew Allum <mallum@o-hand.com>
4 *
5 * Copyright © 2004 Nokia
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation, and that the name of Nokia not be used in
12 * advertising or publicity pertaining to distribution of the software without
13 * specific, written prior permission. Nokia makes no
14 * representations about the suitability of this software for any purpose. It
15 * is provided "as is" without express or implied warranty.
16 *
17 * NOKIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19 * EVENT SHALL NOKIA BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
23 * PERFORMANCE OF THIS SOFTWARE.
24 */
25
26 #ifdef HAVE_DIX_CONFIG_H
27 #include <dix-config.h>
28 #endif
29
30 #include "hostx.h"
31 #include "input.h"
32
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <string.h> /* for memset */
37 #include <errno.h>
38 #include <time.h>
39 #include <err.h>
40
41 #include <sys/ipc.h>
42 #include <sys/shm.h>
43 #include <sys/time.h>
44 #include <sys/mman.h>
45
46 #include <X11/keysym.h>
47 #include <xcb/xcb.h>
48 #include <xcb/xproto.h>
49 #include <xcb/xcb_icccm.h>
50 #include <xcb/xcb_aux.h>
51 #include <xcb/shm.h>
52 #include <xcb/xcb_image.h>
53 #include <xcb/shape.h>
54 #include <xcb/xcb_keysyms.h>
55 #include <xcb/randr.h>
56 #include <xcb/xkb.h>
57 #ifdef GLAMOR
58 #include <epoxy/gl.h>
59 #include "glamor.h"
60 #include "ephyr_glamor_glx.h"
61 #endif
62 #include "ephyrlog.h"
63 #include "ephyr.h"
64
65 struct EphyrHostXVars {
66 char *server_dpy_name;
67 xcb_connection_t *conn;
68 int screen;
69 xcb_visualtype_t *visual;
70 Window winroot;
71 xcb_gcontext_t gc;
72 xcb_render_pictformat_t argb_format;
73 xcb_cursor_t empty_cursor;
74 xcb_generic_event_t *saved_event;
75 int depth;
76 Bool use_sw_cursor;
77 Bool use_fullscreen;
78 Bool have_shm;
79 Bool have_shm_fd_passing;
80
81 int n_screens;
82 KdScreenInfo **screens;
83
84 long damage_debug_msec;
85 Bool size_set_from_configure;
86 };
87
88 /* memset ( missing> ) instead of below */
89 /*static EphyrHostXVars HostX = { "?", 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};*/
90 static EphyrHostXVars HostX;
91
92 static int HostXWantDamageDebug = 0;
93
94 extern Bool EphyrWantResize;
95
96 char *ephyrResName = NULL;
97 int ephyrResNameFromCmd = 0;
98 char *ephyrTitle = NULL;
99 Bool ephyr_glamor = FALSE;
100 extern Bool ephyr_glamor_skip_present;
101
102 Bool
hostx_has_extension(xcb_extension_t * extension)103 hostx_has_extension(xcb_extension_t *extension)
104 {
105 const xcb_query_extension_reply_t *rep;
106
107 rep = xcb_get_extension_data(HostX.conn, extension);
108
109 return rep && rep->present;
110 }
111
112 static void
113 hostx_set_fullscreen_hint(void);
114
115 #define host_depth_matches_server(_vars) (HostX.depth == (_vars)->server_depth)
116
117 int
hostx_want_screen_geometry(KdScreenInfo * screen,int * width,int * height,int * x,int * y)118 hostx_want_screen_geometry(KdScreenInfo *screen, int *width, int *height, int *x, int *y)
119 {
120 EphyrScrPriv *scrpriv = screen->driver;
121
122 if (scrpriv && (scrpriv->win_pre_existing != None ||
123 scrpriv->output != NULL ||
124 HostX.use_fullscreen == TRUE)) {
125 *x = scrpriv->win_x;
126 *y = scrpriv->win_y;
127 *width = scrpriv->win_width;
128 *height = scrpriv->win_height;
129 return 1;
130 }
131
132 return 0;
133 }
134
135 void
hostx_add_screen(KdScreenInfo * screen,unsigned long win_id,int screen_num,Bool use_geometry,const char * output)136 hostx_add_screen(KdScreenInfo *screen, unsigned long win_id, int screen_num, Bool use_geometry, const char *output)
137 {
138 EphyrScrPriv *scrpriv = screen->driver;
139 int index = HostX.n_screens;
140
141 HostX.n_screens += 1;
142 HostX.screens = reallocarray(HostX.screens,
143 HostX.n_screens, sizeof(HostX.screens[0]));
144 HostX.screens[index] = screen;
145
146 scrpriv->screen = screen;
147 scrpriv->win_pre_existing = win_id;
148 scrpriv->win_explicit_position = use_geometry;
149 scrpriv->output = output;
150 }
151
152 void
hostx_set_display_name(char * name)153 hostx_set_display_name(char *name)
154 {
155 HostX.server_dpy_name = strdup(name);
156 }
157
158 void
hostx_set_screen_number(KdScreenInfo * screen,int number)159 hostx_set_screen_number(KdScreenInfo *screen, int number)
160 {
161 EphyrScrPriv *scrpriv = screen->driver;
162
163 if (scrpriv) {
164 scrpriv->mynum = number;
165 hostx_set_win_title(screen, "");
166 }
167 }
168
169 void
hostx_set_win_title(KdScreenInfo * screen,const char * extra_text)170 hostx_set_win_title(KdScreenInfo *screen, const char *extra_text)
171 {
172 EphyrScrPriv *scrpriv = screen->driver;
173
174 if (!scrpriv)
175 return;
176
177 if (ephyrTitle) {
178 xcb_icccm_set_wm_name(HostX.conn,
179 scrpriv->win,
180 XCB_ATOM_STRING,
181 8,
182 strlen(ephyrTitle),
183 ephyrTitle);
184 } else {
185 #define BUF_LEN 256
186 char buf[BUF_LEN + 1];
187
188 memset(buf, 0, BUF_LEN + 1);
189 snprintf(buf, BUF_LEN, "Xephyr on %s.%d %s",
190 HostX.server_dpy_name ? HostX.server_dpy_name : ":0",
191 scrpriv->mynum, (extra_text != NULL) ? extra_text : "");
192
193 xcb_icccm_set_wm_name(HostX.conn,
194 scrpriv->win,
195 XCB_ATOM_STRING,
196 8,
197 strlen(buf),
198 buf);
199 xcb_flush(HostX.conn);
200 }
201 }
202
203 int
hostx_want_host_cursor(void)204 hostx_want_host_cursor(void)
205 {
206 return !HostX.use_sw_cursor;
207 }
208
209 void
hostx_use_sw_cursor(void)210 hostx_use_sw_cursor(void)
211 {
212 HostX.use_sw_cursor = TRUE;
213 }
214
215 xcb_cursor_t
hostx_get_empty_cursor(void)216 hostx_get_empty_cursor(void)
217 {
218 return HostX.empty_cursor;
219 }
220
221 int
hostx_want_preexisting_window(KdScreenInfo * screen)222 hostx_want_preexisting_window(KdScreenInfo *screen)
223 {
224 EphyrScrPriv *scrpriv = screen->driver;
225
226 if (scrpriv && scrpriv->win_pre_existing) {
227 return 1;
228 }
229 else {
230 return 0;
231 }
232 }
233
234 void
hostx_get_output_geometry(const char * output,int * x,int * y,int * width,int * height)235 hostx_get_output_geometry(const char *output,
236 int *x, int *y,
237 int *width, int *height)
238 {
239 int i, name_len = 0, output_found = FALSE;
240 char *name = NULL;
241 xcb_generic_error_t *error;
242 xcb_randr_query_version_cookie_t version_c;
243 xcb_randr_query_version_reply_t *version_r;
244 xcb_randr_get_screen_resources_cookie_t screen_resources_c;
245 xcb_randr_get_screen_resources_reply_t *screen_resources_r;
246 xcb_randr_output_t *randr_outputs;
247 xcb_randr_get_output_info_cookie_t output_info_c;
248 xcb_randr_get_output_info_reply_t *output_info_r;
249 xcb_randr_get_crtc_info_cookie_t crtc_info_c;
250 xcb_randr_get_crtc_info_reply_t *crtc_info_r;
251
252 /* First of all, check for extension */
253 if (!hostx_has_extension(&xcb_randr_id))
254 {
255 fprintf(stderr, "\nHost X server does not support RANDR extension (or it's disabled).\n");
256 exit(1);
257 }
258
259 /* Check RandR version */
260 version_c = xcb_randr_query_version(HostX.conn, 1, 2);
261 version_r = xcb_randr_query_version_reply(HostX.conn,
262 version_c,
263 &error);
264
265 if (error != NULL || version_r == NULL)
266 {
267 fprintf(stderr, "\nFailed to get RandR version supported by host X server.\n");
268 exit(1);
269 }
270 else if (version_r->major_version < 1 || version_r->minor_version < 2)
271 {
272 free(version_r);
273 fprintf(stderr, "\nHost X server doesn't support RandR 1.2, needed for -output usage.\n");
274 exit(1);
275 }
276
277 free(version_r);
278
279 /* Get list of outputs from screen resources */
280 screen_resources_c = xcb_randr_get_screen_resources(HostX.conn,
281 HostX.winroot);
282 screen_resources_r = xcb_randr_get_screen_resources_reply(HostX.conn,
283 screen_resources_c,
284 NULL);
285 randr_outputs = xcb_randr_get_screen_resources_outputs(screen_resources_r);
286
287 for (i = 0; !output_found && i < screen_resources_r->num_outputs; i++)
288 {
289 /* Get info on the output */
290 output_info_c = xcb_randr_get_output_info(HostX.conn,
291 randr_outputs[i],
292 XCB_CURRENT_TIME);
293 output_info_r = xcb_randr_get_output_info_reply(HostX.conn,
294 output_info_c,
295 NULL);
296
297 /* Get output name */
298 name_len = xcb_randr_get_output_info_name_length(output_info_r);
299 name = malloc(name_len + 1);
300 strncpy(name, (char*)xcb_randr_get_output_info_name(output_info_r), name_len);
301 name[name_len] = '\0';
302
303 if (!strcmp(name, output))
304 {
305 output_found = TRUE;
306
307 /* Check if output is connected */
308 if (output_info_r->crtc == XCB_NONE)
309 {
310 free(name);
311 free(output_info_r);
312 free(screen_resources_r);
313 fprintf(stderr, "\nOutput %s is currently disabled (or not connected).\n", output);
314 exit(1);
315 }
316
317 /* Get CRTC from output info */
318 crtc_info_c = xcb_randr_get_crtc_info(HostX.conn,
319 output_info_r->crtc,
320 XCB_CURRENT_TIME);
321 crtc_info_r = xcb_randr_get_crtc_info_reply(HostX.conn,
322 crtc_info_c,
323 NULL);
324
325 /* Get CRTC geometry */
326 *x = crtc_info_r->x;
327 *y = crtc_info_r->y;
328 *width = crtc_info_r->width;
329 *height = crtc_info_r->height;
330
331 free(crtc_info_r);
332 }
333
334 free(name);
335 free(output_info_r);
336 }
337
338 free(screen_resources_r);
339
340 if (!output_found)
341 {
342 fprintf(stderr, "\nOutput %s not available in host X server.\n", output);
343 exit(1);
344 }
345 }
346
347 void
hostx_use_fullscreen(void)348 hostx_use_fullscreen(void)
349 {
350 HostX.use_fullscreen = TRUE;
351 }
352
353 int
hostx_want_fullscreen(void)354 hostx_want_fullscreen(void)
355 {
356 return HostX.use_fullscreen;
357 }
358
359 static xcb_intern_atom_cookie_t cookie_WINDOW_STATE,
360 cookie_WINDOW_STATE_FULLSCREEN;
361
362 static void
hostx_set_fullscreen_hint(void)363 hostx_set_fullscreen_hint(void)
364 {
365 xcb_atom_t atom_WINDOW_STATE, atom_WINDOW_STATE_FULLSCREEN;
366 int index;
367 xcb_intern_atom_reply_t *reply;
368
369 reply = xcb_intern_atom_reply(HostX.conn, cookie_WINDOW_STATE, NULL);
370 atom_WINDOW_STATE = reply->atom;
371 free(reply);
372
373 reply = xcb_intern_atom_reply(HostX.conn, cookie_WINDOW_STATE_FULLSCREEN,
374 NULL);
375 atom_WINDOW_STATE_FULLSCREEN = reply->atom;
376 free(reply);
377
378 for (index = 0; index < HostX.n_screens; index++) {
379 EphyrScrPriv *scrpriv = HostX.screens[index]->driver;
380 xcb_change_property(HostX.conn,
381 PropModeReplace,
382 scrpriv->win,
383 atom_WINDOW_STATE,
384 XCB_ATOM_ATOM,
385 32,
386 1,
387 &atom_WINDOW_STATE_FULLSCREEN);
388 }
389 }
390
391 static void
hostx_toggle_damage_debug(void)392 hostx_toggle_damage_debug(void)
393 {
394 HostXWantDamageDebug ^= 1;
395 }
396
397 void
hostx_handle_signal(int signum)398 hostx_handle_signal(int signum)
399 {
400 hostx_toggle_damage_debug();
401 EPHYR_DBG("Signal caught. Damage Debug:%i\n", HostXWantDamageDebug);
402 }
403
404 void
hostx_use_resname(char * name,int fromcmd)405 hostx_use_resname(char *name, int fromcmd)
406 {
407 ephyrResName = name;
408 ephyrResNameFromCmd = fromcmd;
409 }
410
411 void
hostx_set_title(char * title)412 hostx_set_title(char *title)
413 {
414 ephyrTitle = title;
415 }
416
417 #ifdef __SUNPRO_C
418 /* prevent "Function has no return statement" error for x_io_error_handler */
419 #pragma does_not_return(exit)
420 #endif
421
422 static void
hostx_init_shm(void)423 hostx_init_shm(void)
424 {
425 /* Try to get share memory ximages for a little bit more speed */
426 if (!hostx_has_extension(&xcb_shm_id) || getenv("XEPHYR_NO_SHM")) {
427 HostX.have_shm = FALSE;
428 } else {
429 xcb_generic_error_t *error = NULL;
430 xcb_shm_query_version_cookie_t cookie;
431 xcb_shm_query_version_reply_t *reply;
432
433 HostX.have_shm = TRUE;
434 HostX.have_shm_fd_passing = FALSE;
435 cookie = xcb_shm_query_version(HostX.conn);
436 reply = xcb_shm_query_version_reply(HostX.conn, cookie, &error);
437 if (reply) {
438 HostX.have_shm_fd_passing =
439 (reply->major_version == 1 && reply->minor_version >= 2) ||
440 reply->major_version > 1;
441 free(reply);
442 }
443 free(error);
444 }
445 }
446
447 static Bool
hostx_create_shm_segment(xcb_shm_segment_info_t * shminfo,size_t size)448 hostx_create_shm_segment(xcb_shm_segment_info_t *shminfo, size_t size)
449 {
450 shminfo->shmaddr = NULL;
451
452 if (HostX.have_shm_fd_passing) {
453 xcb_generic_error_t *error = NULL;
454 xcb_shm_create_segment_cookie_t cookie;
455 xcb_shm_create_segment_reply_t *reply;
456
457 shminfo->shmseg = xcb_generate_id(HostX.conn);
458 cookie = xcb_shm_create_segment(HostX.conn, shminfo->shmseg, size, TRUE);
459 reply = xcb_shm_create_segment_reply(HostX.conn, cookie, &error);
460 if (reply) {
461 int *fds = reply->nfd == 1 ?
462 xcb_shm_create_segment_reply_fds(HostX.conn, reply) : NULL;
463 if (fds) {
464 shminfo->shmaddr =
465 (uint8_t *)mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fds[0], 0);
466 close(fds[0]);
467 if (shminfo->shmaddr == MAP_FAILED)
468 shminfo->shmaddr = NULL;
469 }
470 if (!shminfo->shmaddr)
471 xcb_shm_detach(HostX.conn, shminfo->shmseg);
472
473 free(reply);
474 }
475 free(error);
476 } else {
477 shminfo->shmid = shmget(IPC_PRIVATE, size, IPC_CREAT|0666);
478 if (shminfo->shmid != -1) {
479 shminfo->shmaddr = shmat(shminfo->shmid, 0, 0);
480 if (shminfo->shmaddr == (void *)-1) {
481 shminfo->shmaddr = NULL;
482 } else {
483 xcb_generic_error_t *error = NULL;
484 xcb_void_cookie_t cookie;
485
486 shmctl(shminfo->shmid, IPC_RMID, 0);
487
488 shminfo->shmseg = xcb_generate_id(HostX.conn);
489 cookie = xcb_shm_attach_checked(HostX.conn, shminfo->shmseg, shminfo->shmid, TRUE);
490 error = xcb_request_check(HostX.conn, cookie);
491
492 if (error) {
493 free(error);
494 shmdt(shminfo->shmaddr);
495 shminfo->shmaddr = NULL;
496 }
497 }
498 }
499 }
500
501 return shminfo->shmaddr != NULL;
502 }
503
504 static void
hostx_destroy_shm_segment(xcb_shm_segment_info_t * shminfo,size_t size)505 hostx_destroy_shm_segment(xcb_shm_segment_info_t *shminfo, size_t size)
506 {
507 xcb_shm_detach(HostX.conn, shminfo->shmseg);
508
509 if (HostX.have_shm_fd_passing)
510 munmap(shminfo->shmaddr, size);
511 else
512 shmdt(shminfo->shmaddr);
513
514 shminfo->shmaddr = NULL;
515 }
516
517 int
hostx_init(void)518 hostx_init(void)
519 {
520 uint32_t attrs[2];
521 uint32_t attr_mask = 0;
522 xcb_pixmap_t cursor_pxm;
523 xcb_gcontext_t cursor_gc;
524 uint16_t red, green, blue;
525 uint32_t pixel;
526 int index;
527 char *tmpstr;
528 char *class_hint;
529 size_t class_len;
530 xcb_screen_t *xscreen;
531 xcb_rectangle_t rect = { 0, 0, 1, 1 };
532
533 attrs[0] =
534 XCB_EVENT_MASK_BUTTON_PRESS
535 | XCB_EVENT_MASK_BUTTON_RELEASE
536 | XCB_EVENT_MASK_POINTER_MOTION
537 | XCB_EVENT_MASK_KEY_PRESS
538 | XCB_EVENT_MASK_KEY_RELEASE
539 | XCB_EVENT_MASK_EXPOSURE
540 | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
541 attr_mask |= XCB_CW_EVENT_MASK;
542
543 EPHYR_DBG("mark");
544 #ifdef GLAMOR
545 if (ephyr_glamor)
546 HostX.conn = ephyr_glamor_connect();
547 else
548 #endif
549 HostX.conn = xcb_connect(NULL, &HostX.screen);
550 if (!HostX.conn || xcb_connection_has_error(HostX.conn)) {
551 fprintf(stderr, "\nXephyr cannot open host display. Is DISPLAY set?\n");
552 exit(1);
553 }
554
555 xscreen = xcb_aux_get_screen(HostX.conn, HostX.screen);
556 HostX.winroot = xscreen->root;
557 HostX.gc = xcb_generate_id(HostX.conn);
558 HostX.depth = xscreen->root_depth;
559 #ifdef GLAMOR
560 if (ephyr_glamor) {
561 HostX.visual = ephyr_glamor_get_visual();
562 if (HostX.visual->visual_id != xscreen->root_visual) {
563 attrs[1] = xcb_generate_id(HostX.conn);
564 attr_mask |= XCB_CW_COLORMAP;
565 xcb_create_colormap(HostX.conn,
566 XCB_COLORMAP_ALLOC_NONE,
567 attrs[1],
568 HostX.winroot,
569 HostX.visual->visual_id);
570 }
571 } else
572 #endif
573 HostX.visual = xcb_aux_find_visual_by_id(xscreen,xscreen->root_visual);
574
575 xcb_create_gc(HostX.conn, HostX.gc, HostX.winroot, 0, NULL);
576 cookie_WINDOW_STATE = xcb_intern_atom(HostX.conn, FALSE,
577 strlen("_NET_WM_STATE"),
578 "_NET_WM_STATE");
579 cookie_WINDOW_STATE_FULLSCREEN =
580 xcb_intern_atom(HostX.conn, FALSE,
581 strlen("_NET_WM_STATE_FULLSCREEN"),
582 "_NET_WM_STATE_FULLSCREEN");
583
584 for (index = 0; index < HostX.n_screens; index++) {
585 KdScreenInfo *screen = HostX.screens[index];
586 EphyrScrPriv *scrpriv = screen->driver;
587
588 scrpriv->win = xcb_generate_id(HostX.conn);
589 scrpriv->server_depth = HostX.depth;
590 scrpriv->ximg = NULL;
591 scrpriv->win_x = 0;
592 scrpriv->win_y = 0;
593
594 if (scrpriv->win_pre_existing != XCB_WINDOW_NONE) {
595 xcb_get_geometry_reply_t *prewin_geom;
596 xcb_get_geometry_cookie_t cookie;
597 xcb_generic_error_t *e = NULL;
598
599 /* Get screen size from existing window */
600 cookie = xcb_get_geometry(HostX.conn,
601 scrpriv->win_pre_existing);
602 prewin_geom = xcb_get_geometry_reply(HostX.conn, cookie, &e);
603
604 if (e) {
605 free(e);
606 free(prewin_geom);
607 fprintf (stderr, "\nXephyr -parent window' does not exist!\n");
608 exit (1);
609 }
610
611 scrpriv->win_width = prewin_geom->width;
612 scrpriv->win_height = prewin_geom->height;
613
614 free(prewin_geom);
615
616 xcb_create_window(HostX.conn,
617 XCB_COPY_FROM_PARENT,
618 scrpriv->win,
619 scrpriv->win_pre_existing,
620 0,0,
621 scrpriv->win_width,
622 scrpriv->win_height,
623 0,
624 XCB_WINDOW_CLASS_COPY_FROM_PARENT,
625 HostX.visual->visual_id,
626 attr_mask,
627 attrs);
628 }
629 else {
630 xcb_create_window(HostX.conn,
631 XCB_COPY_FROM_PARENT,
632 scrpriv->win,
633 HostX.winroot,
634 0,0,100,100, /* will resize */
635 0,
636 XCB_WINDOW_CLASS_COPY_FROM_PARENT,
637 HostX.visual->visual_id,
638 attr_mask,
639 attrs);
640
641 hostx_set_win_title(screen,
642 "(ctrl+shift grabs mouse and keyboard)");
643
644 if (HostX.use_fullscreen) {
645 scrpriv->win_width = xscreen->width_in_pixels;
646 scrpriv->win_height = xscreen->height_in_pixels;
647
648 hostx_set_fullscreen_hint();
649 }
650 else if (scrpriv->output) {
651 hostx_get_output_geometry(scrpriv->output,
652 &scrpriv->win_x,
653 &scrpriv->win_y,
654 &scrpriv->win_width,
655 &scrpriv->win_height);
656
657 HostX.use_fullscreen = TRUE;
658 hostx_set_fullscreen_hint();
659 }
660
661
662 tmpstr = getenv("RESOURCE_NAME");
663 if (tmpstr && (!ephyrResNameFromCmd))
664 ephyrResName = tmpstr;
665 class_len = strlen(ephyrResName) + 1 + strlen("Xephyr") + 1;
666 class_hint = malloc(class_len);
667 if (class_hint) {
668 strcpy(class_hint, ephyrResName);
669 strcpy(class_hint + strlen(ephyrResName) + 1, "Xephyr");
670 xcb_change_property(HostX.conn,
671 XCB_PROP_MODE_REPLACE,
672 scrpriv->win,
673 XCB_ATOM_WM_CLASS,
674 XCB_ATOM_STRING,
675 8,
676 class_len,
677 class_hint);
678 free(class_hint);
679 }
680 }
681 }
682
683 if (!xcb_aux_parse_color("red", &red, &green, &blue)) {
684 xcb_lookup_color_cookie_t c =
685 xcb_lookup_color(HostX.conn, xscreen->default_colormap, 3, "red");
686 xcb_lookup_color_reply_t *reply =
687 xcb_lookup_color_reply(HostX.conn, c, NULL);
688 red = reply->exact_red;
689 green = reply->exact_green;
690 blue = reply->exact_blue;
691 free(reply);
692 }
693
694 {
695 xcb_alloc_color_cookie_t c = xcb_alloc_color(HostX.conn,
696 xscreen->default_colormap,
697 red, green, blue);
698 xcb_alloc_color_reply_t *r = xcb_alloc_color_reply(HostX.conn, c, NULL);
699 red = r->red;
700 green = r->green;
701 blue = r->blue;
702 pixel = r->pixel;
703 free(r);
704 }
705
706 xcb_change_gc(HostX.conn, HostX.gc, XCB_GC_FOREGROUND, &pixel);
707
708 cursor_pxm = xcb_generate_id(HostX.conn);
709 xcb_create_pixmap(HostX.conn, 1, cursor_pxm, HostX.winroot, 1, 1);
710 cursor_gc = xcb_generate_id(HostX.conn);
711 pixel = 0;
712 xcb_create_gc(HostX.conn, cursor_gc, cursor_pxm,
713 XCB_GC_FOREGROUND, &pixel);
714 xcb_poly_fill_rectangle(HostX.conn, cursor_pxm, cursor_gc, 1, &rect);
715 xcb_free_gc(HostX.conn, cursor_gc);
716 HostX.empty_cursor = xcb_generate_id(HostX.conn);
717 xcb_create_cursor(HostX.conn,
718 HostX.empty_cursor,
719 cursor_pxm, cursor_pxm,
720 0,0,0,
721 0,0,0,
722 1,1);
723 xcb_free_pixmap(HostX.conn, cursor_pxm);
724 if (!hostx_want_host_cursor ()) {
725 CursorVisible = TRUE;
726 /* Ditch the cursor, we provide our 'own' */
727 for (index = 0; index < HostX.n_screens; index++) {
728 KdScreenInfo *screen = HostX.screens[index];
729 EphyrScrPriv *scrpriv = screen->driver;
730
731 xcb_change_window_attributes(HostX.conn,
732 scrpriv->win,
733 XCB_CW_CURSOR,
734 &HostX.empty_cursor);
735 }
736 }
737
738 hostx_init_shm();
739 if (HostX.have_shm) {
740 /* Really really check we have shm - better way ?*/
741 xcb_shm_segment_info_t shminfo;
742 if (!hostx_create_shm_segment(&shminfo, 1)) {
743 fprintf(stderr, "\nXephyr unable to use SHM XImages\n");
744 HostX.have_shm = FALSE;
745 } else {
746 hostx_destroy_shm_segment(&shminfo, 1);
747 }
748 } else {
749 fprintf(stderr, "\nXephyr unable to use SHM XImages\n");
750 }
751
752 xcb_flush(HostX.conn);
753
754 /* Setup the pause time between paints when debugging updates */
755
756 HostX.damage_debug_msec = 20000; /* 1/50 th of a second */
757
758 if (getenv("XEPHYR_PAUSE")) {
759 HostX.damage_debug_msec = strtol(getenv("XEPHYR_PAUSE"), NULL, 0);
760 EPHYR_DBG("pause is %li\n", HostX.damage_debug_msec);
761 }
762
763 return 1;
764 }
765
766 int
hostx_get_depth(void)767 hostx_get_depth(void)
768 {
769 return HostX.depth;
770 }
771
772 int
hostx_get_server_depth(KdScreenInfo * screen)773 hostx_get_server_depth(KdScreenInfo *screen)
774 {
775 EphyrScrPriv *scrpriv = screen->driver;
776
777 return scrpriv ? scrpriv->server_depth : 0;
778 }
779
780 int
hostx_get_bpp(KdScreenInfo * screen)781 hostx_get_bpp(KdScreenInfo *screen)
782 {
783 EphyrScrPriv *scrpriv = screen->driver;
784
785 if (!scrpriv)
786 return 0;
787
788 if (host_depth_matches_server(scrpriv))
789 return HostX.visual->bits_per_rgb_value;
790 else
791 return scrpriv->server_depth; /*XXX correct ?*/
792 }
793
794 void
hostx_get_visual_masks(KdScreenInfo * screen,CARD32 * rmsk,CARD32 * gmsk,CARD32 * bmsk)795 hostx_get_visual_masks(KdScreenInfo *screen,
796 CARD32 *rmsk, CARD32 *gmsk, CARD32 *bmsk)
797 {
798 EphyrScrPriv *scrpriv = screen->driver;
799
800 if (!scrpriv)
801 return;
802
803 if (host_depth_matches_server(scrpriv)) {
804 *rmsk = HostX.visual->red_mask;
805 *gmsk = HostX.visual->green_mask;
806 *bmsk = HostX.visual->blue_mask;
807 }
808 else if (scrpriv->server_depth == 16) {
809 /* Assume 16bpp 565 */
810 *rmsk = 0xf800;
811 *gmsk = 0x07e0;
812 *bmsk = 0x001f;
813 }
814 else {
815 *rmsk = 0x0;
816 *gmsk = 0x0;
817 *bmsk = 0x0;
818 }
819 }
820
821 static int
hostx_calculate_color_shift(unsigned long mask)822 hostx_calculate_color_shift(unsigned long mask)
823 {
824 int shift = 1;
825
826 /* count # of bits in mask */
827 while ((mask = (mask >> 1)))
828 shift++;
829 /* cmap entry is an unsigned char so adjust it by size of that */
830 shift = shift - sizeof(unsigned char) * 8;
831 if (shift < 0)
832 shift = 0;
833 return shift;
834 }
835
836 void
hostx_set_cmap_entry(ScreenPtr pScreen,unsigned char idx,unsigned char r,unsigned char g,unsigned char b)837 hostx_set_cmap_entry(ScreenPtr pScreen, unsigned char idx,
838 unsigned char r, unsigned char g, unsigned char b)
839 {
840 KdScreenPriv(pScreen);
841 KdScreenInfo *screen = pScreenPriv->screen;
842 EphyrScrPriv *scrpriv = screen->driver;
843 /* need to calculate the shifts for RGB because server could be BGR. */
844 /* XXX Not sure if this is correct for 8 on 16, but this works for 8 on 24.*/
845 static int rshift, bshift, gshift = 0;
846 static int first_time = 1;
847
848 if (first_time) {
849 first_time = 0;
850 rshift = hostx_calculate_color_shift(HostX.visual->red_mask);
851 gshift = hostx_calculate_color_shift(HostX.visual->green_mask);
852 bshift = hostx_calculate_color_shift(HostX.visual->blue_mask);
853 }
854 scrpriv->cmap[idx] = ((r << rshift) & HostX.visual->red_mask) |
855 ((g << gshift) & HostX.visual->green_mask) |
856 ((b << bshift) & HostX.visual->blue_mask);
857 }
858
859 /**
860 * hostx_screen_init creates the XImage that will contain the front buffer of
861 * the ephyr screen, and possibly offscreen memory.
862 *
863 * @param width width of the screen
864 * @param height height of the screen
865 * @param buffer_height height of the rectangle to be allocated.
866 *
867 * hostx_screen_init() creates an XImage, using MIT-SHM if it's available.
868 * buffer_height can be used to create a larger offscreen buffer, which is used
869 * by fakexa for storing offscreen pixmap data.
870 */
871 void *
hostx_screen_init(KdScreenInfo * screen,int x,int y,int width,int height,int buffer_height,int * bytes_per_line,int * bits_per_pixel)872 hostx_screen_init(KdScreenInfo *screen,
873 int x, int y,
874 int width, int height, int buffer_height,
875 int *bytes_per_line, int *bits_per_pixel)
876 {
877 EphyrScrPriv *scrpriv = screen->driver;
878 Bool shm_success = FALSE;
879
880 if (!scrpriv) {
881 fprintf(stderr, "%s: Error in accessing hostx data\n", __func__);
882 exit(1);
883 }
884
885 EPHYR_DBG("host_screen=%p x=%d, y=%d, wxh=%dx%d, buffer_height=%d",
886 screen, x, y, width, height, buffer_height);
887
888 if (scrpriv->ximg != NULL) {
889 /* Free up the image data if previously used
890 * i.ie called by server reset
891 */
892
893 if (HostX.have_shm) {
894 xcb_image_destroy(scrpriv->ximg);
895 hostx_destroy_shm_segment(&scrpriv->shminfo, scrpriv->shmsize);
896 }
897 else {
898 free(scrpriv->ximg->data);
899 scrpriv->ximg->data = NULL;
900
901 xcb_image_destroy(scrpriv->ximg);
902 }
903 }
904
905 if (!ephyr_glamor && HostX.have_shm) {
906 scrpriv->ximg = xcb_image_create_native(HostX.conn,
907 width,
908 buffer_height,
909 XCB_IMAGE_FORMAT_Z_PIXMAP,
910 HostX.depth,
911 NULL,
912 ~0,
913 NULL);
914
915 scrpriv->shmsize = scrpriv->ximg->stride * buffer_height;
916 if (!hostx_create_shm_segment(&scrpriv->shminfo,
917 scrpriv->shmsize)) {
918 EPHYR_DBG
919 ("Can't create SHM Segment, falling back to plain XImages");
920 HostX.have_shm = FALSE;
921 xcb_image_destroy(scrpriv->ximg);
922 }
923 else {
924 EPHYR_DBG("SHM segment created %p", scrpriv->shminfo.shmaddr);
925 scrpriv->ximg->data = scrpriv->shminfo.shmaddr;
926 shm_success = TRUE;
927 }
928 }
929
930 if (!ephyr_glamor && !shm_success) {
931 EPHYR_DBG("Creating image %dx%d for screen scrpriv=%p\n",
932 width, buffer_height, scrpriv);
933 scrpriv->ximg = xcb_image_create_native(HostX.conn,
934 width,
935 buffer_height,
936 XCB_IMAGE_FORMAT_Z_PIXMAP,
937 HostX.depth,
938 NULL,
939 ~0,
940 NULL);
941
942 /* Match server byte order so that the image can be converted to
943 * the native byte order by xcb_image_put() before drawing */
944 if (host_depth_matches_server(scrpriv))
945 scrpriv->ximg->byte_order = IMAGE_BYTE_ORDER;
946
947 scrpriv->ximg->data =
948 xallocarray(scrpriv->ximg->stride, buffer_height);
949 }
950
951 if (!HostX.size_set_from_configure)
952 {
953 uint32_t mask = XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT;
954 uint32_t values[2] = {width, height};
955 xcb_configure_window(HostX.conn, scrpriv->win, mask, values);
956 }
957
958 if (scrpriv->win_pre_existing == None && !EphyrWantResize) {
959 /* Ask the WM to keep our size static */
960 xcb_size_hints_t size_hints = {0};
961 size_hints.max_width = size_hints.min_width = width;
962 size_hints.max_height = size_hints.min_height = height;
963 size_hints.flags = (XCB_ICCCM_SIZE_HINT_P_MIN_SIZE |
964 XCB_ICCCM_SIZE_HINT_P_MAX_SIZE);
965 xcb_icccm_set_wm_normal_hints(HostX.conn, scrpriv->win,
966 &size_hints);
967 }
968
969 #ifdef GLAMOR
970 if (!ephyr_glamor_skip_present)
971 #endif
972 xcb_map_window(HostX.conn, scrpriv->win);
973
974 /* Set explicit window position if it was informed in
975 * -screen option (WxH+X or WxH+X+Y). Otherwise, accept the
976 * position set by WM.
977 * The trick here is putting this code after xcb_map_window() call,
978 * so these values won't be overriden by WM. */
979 if (scrpriv->win_explicit_position)
980 {
981 uint32_t mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y;
982 uint32_t values[2] = {x, y};
983 xcb_configure_window(HostX.conn, scrpriv->win, mask, values);
984 }
985
986
987 xcb_aux_sync(HostX.conn);
988
989 scrpriv->win_width = width;
990 scrpriv->win_height = height;
991 scrpriv->win_x = x;
992 scrpriv->win_y = y;
993
994 #ifdef GLAMOR
995 if (ephyr_glamor) {
996 *bytes_per_line = 0;
997 ephyr_glamor_set_window_size(scrpriv->glamor,
998 scrpriv->win_width, scrpriv->win_height);
999 return NULL;
1000 } else
1001 #endif
1002 if (host_depth_matches_server(scrpriv)) {
1003 *bytes_per_line = scrpriv->ximg->stride;
1004 *bits_per_pixel = scrpriv->ximg->bpp;
1005
1006 EPHYR_DBG("Host matches server");
1007 return scrpriv->ximg->data;
1008 }
1009 else {
1010 int bytes_per_pixel = scrpriv->server_depth >> 3;
1011 int stride = (width * bytes_per_pixel + 0x3) & ~0x3;
1012
1013 *bytes_per_line = stride;
1014 *bits_per_pixel = scrpriv->server_depth;
1015
1016 EPHYR_DBG("server bpp %i", bytes_per_pixel);
1017 scrpriv->fb_data = xallocarray (stride, buffer_height);
1018 return scrpriv->fb_data;
1019 }
1020 }
1021
1022 static void hostx_paint_debug_rect(KdScreenInfo *screen,
1023 int x, int y, int width, int height);
1024
1025 void
hostx_paint_rect(KdScreenInfo * screen,int sx,int sy,int dx,int dy,int width,int height)1026 hostx_paint_rect(KdScreenInfo *screen,
1027 int sx, int sy, int dx, int dy, int width, int height)
1028 {
1029 EphyrScrPriv *scrpriv = screen->driver;
1030
1031 EPHYR_DBG("painting in screen %d\n", scrpriv->mynum);
1032
1033 #ifdef GLAMOR
1034 if (ephyr_glamor) {
1035 BoxRec box;
1036 RegionRec region;
1037
1038 box.x1 = dx;
1039 box.y1 = dy;
1040 box.x2 = dx + width;
1041 box.y2 = dy + height;
1042
1043 RegionInit(®ion, &box, 1);
1044 ephyr_glamor_damage_redisplay(scrpriv->glamor, ®ion);
1045 RegionUninit(®ion);
1046 return;
1047 }
1048 #endif
1049
1050 /*
1051 * Copy the image data updated by the shadow layer
1052 * on to the window
1053 */
1054
1055 if (HostXWantDamageDebug) {
1056 hostx_paint_debug_rect(screen, dx, dy, width, height);
1057 }
1058
1059 /*
1060 * If the depth of the ephyr server is less than that of the host,
1061 * the kdrive fb does not point to the ximage data but to a buffer
1062 * ( fb_data ), we shift the various bits from this onto the XImage
1063 * so they match the host.
1064 *
1065 * Note, This code is pretty new ( and simple ) so may break on
1066 * endian issues, 32 bpp host etc.
1067 * Not sure if 8bpp case is right either.
1068 * ... and it will be slower than the matching depth case.
1069 */
1070
1071 if (!host_depth_matches_server(scrpriv)) {
1072 int x, y, idx, bytes_per_pixel = (scrpriv->server_depth >> 3);
1073 int stride = (scrpriv->win_width * bytes_per_pixel + 0x3) & ~0x3;
1074 unsigned char r, g, b;
1075 unsigned long host_pixel;
1076
1077 EPHYR_DBG("Unmatched host depth scrpriv=%p\n", scrpriv);
1078 for (y = sy; y < sy + height; y++)
1079 for (x = sx; x < sx + width; x++) {
1080 idx = y * stride + x * bytes_per_pixel;
1081
1082 switch (scrpriv->server_depth) {
1083 case 16:
1084 {
1085 unsigned short pixel =
1086 *(unsigned short *) (scrpriv->fb_data + idx);
1087
1088 r = ((pixel & 0xf800) >> 8);
1089 g = ((pixel & 0x07e0) >> 3);
1090 b = ((pixel & 0x001f) << 3);
1091
1092 host_pixel = (r << 16) | (g << 8) | (b);
1093
1094 xcb_image_put_pixel(scrpriv->ximg, x, y, host_pixel);
1095 break;
1096 }
1097 case 8:
1098 {
1099 unsigned char pixel =
1100 *(unsigned char *) (scrpriv->fb_data + idx);
1101 xcb_image_put_pixel(scrpriv->ximg, x, y,
1102 scrpriv->cmap[pixel]);
1103 break;
1104 }
1105 default:
1106 break;
1107 }
1108 }
1109 }
1110
1111 if (HostX.have_shm) {
1112 xcb_image_shm_put(HostX.conn, scrpriv->win,
1113 HostX.gc, scrpriv->ximg,
1114 scrpriv->shminfo,
1115 sx, sy, dx, dy, width, height, FALSE);
1116 }
1117 else {
1118 xcb_image_t *subimg = xcb_image_subimage(scrpriv->ximg, sx, sy,
1119 width, height, 0, 0, 0);
1120 xcb_image_t *img = xcb_image_native(HostX.conn, subimg, 1);
1121 xcb_image_put(HostX.conn, scrpriv->win, HostX.gc, img, dx, dy, 0);
1122 if (subimg != img)
1123 xcb_image_destroy(img);
1124 xcb_image_destroy(subimg);
1125 }
1126
1127 xcb_aux_sync(HostX.conn);
1128 }
1129
1130 static void
hostx_paint_debug_rect(KdScreenInfo * screen,int x,int y,int width,int height)1131 hostx_paint_debug_rect(KdScreenInfo *screen,
1132 int x, int y, int width, int height)
1133 {
1134 EphyrScrPriv *scrpriv = screen->driver;
1135 struct timespec tspec;
1136 xcb_rectangle_t rect = { .x = x, .y = y, .width = width, .height = height };
1137 xcb_void_cookie_t cookie;
1138 xcb_generic_error_t *e;
1139
1140 tspec.tv_sec = HostX.damage_debug_msec / (1000000);
1141 tspec.tv_nsec = (HostX.damage_debug_msec % 1000000) * 1000;
1142
1143 EPHYR_DBG("msec: %li tv_sec %li, tv_msec %li",
1144 HostX.damage_debug_msec, tspec.tv_sec, tspec.tv_nsec);
1145
1146 /* fprintf(stderr, "Xephyr updating: %i+%i %ix%i\n", x, y, width, height); */
1147
1148 cookie = xcb_poly_fill_rectangle_checked(HostX.conn, scrpriv->win,
1149 HostX.gc, 1, &rect);
1150 e = xcb_request_check(HostX.conn, cookie);
1151 free(e);
1152
1153 /* nanosleep seems to work better than usleep for me... */
1154 nanosleep(&tspec, NULL);
1155 }
1156
1157 Bool
hostx_load_keymap(KeySymsPtr keySyms,CARD8 * modmap,XkbControlsPtr controls)1158 hostx_load_keymap(KeySymsPtr keySyms, CARD8 *modmap, XkbControlsPtr controls)
1159 {
1160 int min_keycode, max_keycode;
1161 int map_width;
1162 size_t i, j;
1163 int keymap_len;
1164 xcb_keysym_t *keymap;
1165 xcb_keycode_t *modifier_map;
1166 xcb_get_keyboard_mapping_cookie_t mapping_c;
1167 xcb_get_keyboard_mapping_reply_t *mapping_r;
1168 xcb_get_modifier_mapping_cookie_t modifier_c;
1169 xcb_get_modifier_mapping_reply_t *modifier_r;
1170 xcb_xkb_use_extension_cookie_t use_c;
1171 xcb_xkb_use_extension_reply_t *use_r;
1172 xcb_xkb_get_controls_cookie_t controls_c;
1173 xcb_xkb_get_controls_reply_t *controls_r;
1174
1175 /* First of all, collect host X server's
1176 * min_keycode and max_keycode, which are
1177 * independent from XKB support. */
1178 min_keycode = xcb_get_setup(HostX.conn)->min_keycode;
1179 max_keycode = xcb_get_setup(HostX.conn)->max_keycode;
1180
1181 EPHYR_DBG("min: %d, max: %d", min_keycode, max_keycode);
1182
1183 keySyms->minKeyCode = min_keycode;
1184 keySyms->maxKeyCode = max_keycode;
1185
1186 /* Check for XKB availability in host X server */
1187 if (!hostx_has_extension(&xcb_xkb_id)) {
1188 EPHYR_LOG_ERROR("XKB extension is not supported in host X server.");
1189 return FALSE;
1190 }
1191
1192 use_c = xcb_xkb_use_extension(HostX.conn,
1193 XCB_XKB_MAJOR_VERSION,
1194 XCB_XKB_MINOR_VERSION);
1195 use_r = xcb_xkb_use_extension_reply(HostX.conn, use_c, NULL);
1196
1197 if (!use_r) {
1198 EPHYR_LOG_ERROR("Couldn't use XKB extension.");
1199 return FALSE;
1200 } else if (!use_r->supported) {
1201 EPHYR_LOG_ERROR("XKB extension is not supported in host X server.");
1202 free(use_r);
1203 return FALSE;
1204 }
1205
1206 free(use_r);
1207
1208 /* Send all needed XCB requests at once,
1209 * and process the replies as needed. */
1210 mapping_c = xcb_get_keyboard_mapping(HostX.conn,
1211 min_keycode,
1212 max_keycode - min_keycode + 1);
1213 modifier_c = xcb_get_modifier_mapping(HostX.conn);
1214 controls_c = xcb_xkb_get_controls(HostX.conn,
1215 XCB_XKB_ID_USE_CORE_KBD);
1216
1217 mapping_r = xcb_get_keyboard_mapping_reply(HostX.conn,
1218 mapping_c,
1219 NULL);
1220
1221 if (!mapping_r) {
1222 EPHYR_LOG_ERROR("xcb_get_keyboard_mapping_reply() failed.");
1223 return FALSE;
1224 }
1225
1226 map_width = mapping_r->keysyms_per_keycode;
1227 keymap = xcb_get_keyboard_mapping_keysyms(mapping_r);
1228 keymap_len = xcb_get_keyboard_mapping_keysyms_length(mapping_r);
1229
1230 keySyms->mapWidth = map_width;
1231 keySyms->map = calloc(keymap_len, sizeof(KeySym));
1232
1233 if (!keySyms->map) {
1234 EPHYR_LOG_ERROR("Failed to allocate KeySym map.");
1235 free(mapping_r);
1236 return FALSE;
1237 }
1238
1239 for (i = 0; i < keymap_len; i++) {
1240 keySyms->map[i] = keymap[i];
1241 }
1242
1243 free(mapping_r);
1244
1245 modifier_r = xcb_get_modifier_mapping_reply(HostX.conn,
1246 modifier_c,
1247 NULL);
1248
1249 if (!modifier_r) {
1250 EPHYR_LOG_ERROR("xcb_get_modifier_mapping_reply() failed.");
1251 return FALSE;
1252 }
1253
1254 modifier_map = xcb_get_modifier_mapping_keycodes(modifier_r);
1255 memset(modmap, 0, sizeof(CARD8) * MAP_LENGTH);
1256
1257 for (j = 0; j < 8; j++) {
1258 for (i = 0; i < modifier_r->keycodes_per_modifier; i++) {
1259 CARD8 keycode;
1260
1261 if ((keycode = modifier_map[j * modifier_r->keycodes_per_modifier + i])) {
1262 modmap[keycode] |= 1 << j;
1263 }
1264 }
1265 }
1266
1267 free(modifier_r);
1268
1269 controls_r = xcb_xkb_get_controls_reply(HostX.conn,
1270 controls_c,
1271 NULL);
1272
1273 if (!controls_r) {
1274 EPHYR_LOG_ERROR("xcb_xkb_get_controls_reply() failed.");
1275 return FALSE;
1276 }
1277
1278 controls->enabled_ctrls = controls_r->enabledControls;
1279
1280 for (i = 0; i < XkbPerKeyBitArraySize; i++) {
1281 controls->per_key_repeat[i] = controls_r->perKeyRepeat[i];
1282 }
1283
1284 free(controls_r);
1285
1286 return TRUE;
1287 }
1288
1289 void
hostx_size_set_from_configure(Bool ss)1290 hostx_size_set_from_configure(Bool ss)
1291 {
1292 HostX.size_set_from_configure = ss;
1293 }
1294
1295 xcb_connection_t *
hostx_get_xcbconn(void)1296 hostx_get_xcbconn(void)
1297 {
1298 return HostX.conn;
1299 }
1300
1301 xcb_generic_event_t *
hostx_get_event(Bool queued_only)1302 hostx_get_event(Bool queued_only)
1303 {
1304 xcb_generic_event_t *xev;
1305
1306 if (HostX.saved_event) {
1307 xev = HostX.saved_event;
1308 HostX.saved_event = NULL;
1309 } else {
1310 if (queued_only)
1311 xev = xcb_poll_for_queued_event(HostX.conn);
1312 else
1313 xev = xcb_poll_for_event(HostX.conn);
1314 }
1315 return xev;
1316 }
1317
1318 Bool
hostx_has_queued_event(void)1319 hostx_has_queued_event(void)
1320 {
1321 if (!HostX.saved_event)
1322 HostX.saved_event = xcb_poll_for_queued_event(HostX.conn);
1323 return HostX.saved_event != NULL;
1324 }
1325
1326 int
hostx_get_screen(void)1327 hostx_get_screen(void)
1328 {
1329 return HostX.screen;
1330 }
1331
1332 int
hostx_get_fd(void)1333 hostx_get_fd(void)
1334 {
1335 return xcb_get_file_descriptor(HostX.conn);
1336 }
1337
1338 int
hostx_get_window(int a_screen_number)1339 hostx_get_window(int a_screen_number)
1340 {
1341 EphyrScrPriv *scrpriv;
1342 if (a_screen_number < 0 || a_screen_number >= HostX.n_screens) {
1343 EPHYR_LOG_ERROR("bad screen number:%d\n", a_screen_number);
1344 return 0;
1345 }
1346 scrpriv = HostX.screens[a_screen_number]->driver;
1347 return scrpriv->win;
1348 }
1349
1350 int
hostx_get_window_attributes(int a_window,EphyrHostWindowAttributes * a_attrs)1351 hostx_get_window_attributes(int a_window, EphyrHostWindowAttributes * a_attrs)
1352 {
1353 xcb_get_geometry_cookie_t geom_cookie;
1354 xcb_get_window_attributes_cookie_t attr_cookie;
1355 xcb_get_geometry_reply_t *geom_reply;
1356 xcb_get_window_attributes_reply_t *attr_reply;
1357
1358 geom_cookie = xcb_get_geometry(HostX.conn, a_window);
1359 attr_cookie = xcb_get_window_attributes(HostX.conn, a_window);
1360 geom_reply = xcb_get_geometry_reply(HostX.conn, geom_cookie, NULL);
1361 attr_reply = xcb_get_window_attributes_reply(HostX.conn, attr_cookie, NULL);
1362
1363 a_attrs->x = geom_reply->x;
1364 a_attrs->y = geom_reply->y;
1365 a_attrs->width = geom_reply->width;
1366 a_attrs->height = geom_reply->height;
1367 a_attrs->visualid = attr_reply->visual;
1368
1369 free(geom_reply);
1370 free(attr_reply);
1371 return TRUE;
1372 }
1373
1374 int
hostx_get_visuals_info(EphyrHostVisualInfo ** a_visuals,int * a_num_entries)1375 hostx_get_visuals_info(EphyrHostVisualInfo ** a_visuals, int *a_num_entries)
1376 {
1377 Bool is_ok = FALSE;
1378 EphyrHostVisualInfo *host_visuals = NULL;
1379 int nb_items = 0, i = 0, screen_num;
1380 xcb_screen_iterator_t screens;
1381 xcb_depth_iterator_t depths;
1382
1383 EPHYR_RETURN_VAL_IF_FAIL(a_visuals && a_num_entries, FALSE);
1384 EPHYR_LOG("enter\n");
1385
1386 screens = xcb_setup_roots_iterator(xcb_get_setup(HostX.conn));
1387 for (screen_num = 0; screens.rem; screen_num++, xcb_screen_next(&screens)) {
1388 depths = xcb_screen_allowed_depths_iterator(screens.data);
1389 for (; depths.rem; xcb_depth_next(&depths)) {
1390 xcb_visualtype_t *visuals = xcb_depth_visuals(depths.data);
1391 EphyrHostVisualInfo *tmp_visuals =
1392 reallocarray(host_visuals,
1393 nb_items + depths.data->visuals_len,
1394 sizeof(EphyrHostVisualInfo));
1395 if (!tmp_visuals) {
1396 goto out;
1397 }
1398 host_visuals = tmp_visuals;
1399 for (i = 0; i < depths.data->visuals_len; i++) {
1400 host_visuals[nb_items + i].visualid = visuals[i].visual_id;
1401 host_visuals[nb_items + i].screen = screen_num;
1402 host_visuals[nb_items + i].depth = depths.data->depth;
1403 host_visuals[nb_items + i].class = visuals[i]._class;
1404 host_visuals[nb_items + i].red_mask = visuals[i].red_mask;
1405 host_visuals[nb_items + i].green_mask = visuals[i].green_mask;
1406 host_visuals[nb_items + i].blue_mask = visuals[i].blue_mask;
1407 host_visuals[nb_items + i].colormap_size = visuals[i].colormap_entries;
1408 host_visuals[nb_items + i].bits_per_rgb = visuals[i].bits_per_rgb_value;
1409 }
1410 nb_items += depths.data->visuals_len;
1411 }
1412 }
1413
1414 EPHYR_LOG("host advertises %d visuals\n", nb_items);
1415 *a_visuals = host_visuals;
1416 *a_num_entries = nb_items;
1417 host_visuals = NULL;
1418
1419 is_ok = TRUE;
1420 out:
1421 free(host_visuals);
1422 host_visuals = NULL;
1423 EPHYR_LOG("leave\n");
1424 return is_ok;
1425
1426 }
1427
1428 int
hostx_create_window(int a_screen_number,EphyrBox * a_geometry,int a_visual_id,int * a_host_peer)1429 hostx_create_window(int a_screen_number,
1430 EphyrBox * a_geometry,
1431 int a_visual_id, int *a_host_peer /*out parameter */ )
1432 {
1433 Bool is_ok = FALSE;
1434 xcb_window_t win;
1435 int winmask = 0;
1436 uint32_t attrs[2];
1437 xcb_screen_t *screen = xcb_aux_get_screen(HostX.conn, hostx_get_screen());
1438 xcb_visualtype_t *visual;
1439 int depth = 0;
1440 EphyrScrPriv *scrpriv = HostX.screens[a_screen_number]->driver;
1441
1442 EPHYR_RETURN_VAL_IF_FAIL(screen && a_geometry, FALSE);
1443
1444 EPHYR_LOG("enter\n");
1445
1446 visual = xcb_aux_find_visual_by_id(screen, a_visual_id);
1447 if (!visual) {
1448 EPHYR_LOG_ERROR ("argh, could not find a remote visual with id:%d\n",
1449 a_visual_id);
1450 goto out;
1451 }
1452 depth = xcb_aux_get_depth_of_visual(screen, a_visual_id);
1453
1454 winmask = XCB_CW_EVENT_MASK | XCB_CW_COLORMAP;
1455 attrs[0] = XCB_EVENT_MASK_BUTTON_PRESS
1456 |XCB_EVENT_MASK_BUTTON_RELEASE
1457 |XCB_EVENT_MASK_POINTER_MOTION
1458 |XCB_EVENT_MASK_KEY_PRESS
1459 |XCB_EVENT_MASK_KEY_RELEASE
1460 |XCB_EVENT_MASK_EXPOSURE;
1461 attrs[1] = xcb_generate_id(HostX.conn);
1462 xcb_create_colormap(HostX.conn,
1463 XCB_COLORMAP_ALLOC_NONE,
1464 attrs[1],
1465 hostx_get_window(a_screen_number),
1466 a_visual_id);
1467
1468 win = xcb_generate_id(HostX.conn);
1469 xcb_create_window(HostX.conn,
1470 depth,
1471 win,
1472 hostx_get_window (a_screen_number),
1473 a_geometry->x, a_geometry->y,
1474 a_geometry->width, a_geometry->height, 0,
1475 XCB_WINDOW_CLASS_COPY_FROM_PARENT,
1476 a_visual_id, winmask, attrs);
1477
1478 if (scrpriv->peer_win == XCB_NONE) {
1479 scrpriv->peer_win = win;
1480 }
1481 else {
1482 EPHYR_LOG_ERROR("multiple peer windows created for same screen\n");
1483 }
1484 xcb_flush(HostX.conn);
1485 xcb_map_window(HostX.conn, win);
1486 *a_host_peer = win;
1487 is_ok = TRUE;
1488 out:
1489 EPHYR_LOG("leave\n");
1490 return is_ok;
1491 }
1492
1493 int
hostx_destroy_window(int a_win)1494 hostx_destroy_window(int a_win)
1495 {
1496 xcb_destroy_window(HostX.conn, a_win);
1497 xcb_flush(HostX.conn);
1498 return TRUE;
1499 }
1500
1501 int
hostx_set_window_geometry(int a_win,EphyrBox * a_geo)1502 hostx_set_window_geometry(int a_win, EphyrBox * a_geo)
1503 {
1504 uint32_t mask = XCB_CONFIG_WINDOW_X
1505 | XCB_CONFIG_WINDOW_Y
1506 | XCB_CONFIG_WINDOW_WIDTH
1507 | XCB_CONFIG_WINDOW_HEIGHT;
1508 uint32_t values[4];
1509
1510 EPHYR_RETURN_VAL_IF_FAIL(a_geo, FALSE);
1511
1512 EPHYR_LOG("enter. x,y,w,h:(%d,%d,%d,%d)\n",
1513 a_geo->x, a_geo->y, a_geo->width, a_geo->height);
1514
1515 values[0] = a_geo->x;
1516 values[1] = a_geo->y;
1517 values[2] = a_geo->width;
1518 values[3] = a_geo->height;
1519 xcb_configure_window(HostX.conn, a_win, mask, values);
1520
1521 EPHYR_LOG("leave\n");
1522 return TRUE;
1523 }
1524
1525 int
hostx_set_window_bounding_rectangles(int a_window,EphyrRect * a_rects,int a_num_rects)1526 hostx_set_window_bounding_rectangles(int a_window,
1527 EphyrRect * a_rects, int a_num_rects)
1528 {
1529 Bool is_ok = FALSE;
1530 int i = 0;
1531 xcb_rectangle_t *rects = NULL;
1532
1533 EPHYR_RETURN_VAL_IF_FAIL(a_rects, FALSE);
1534
1535 EPHYR_LOG("enter. num rects:%d\n", a_num_rects);
1536
1537 rects = calloc(a_num_rects, sizeof (xcb_rectangle_t));
1538 if (!rects)
1539 goto out;
1540 for (i = 0; i < a_num_rects; i++) {
1541 rects[i].x = a_rects[i].x1;
1542 rects[i].y = a_rects[i].y1;
1543 rects[i].width = abs(a_rects[i].x2 - a_rects[i].x1);
1544 rects[i].height = abs(a_rects[i].y2 - a_rects[i].y1);
1545 EPHYR_LOG("borders clipped to rect[x:%d,y:%d,w:%d,h:%d]\n",
1546 rects[i].x, rects[i].y, rects[i].width, rects[i].height);
1547 }
1548 xcb_shape_rectangles(HostX.conn,
1549 XCB_SHAPE_SO_SET,
1550 XCB_SHAPE_SK_BOUNDING,
1551 XCB_CLIP_ORDERING_YX_BANDED,
1552 a_window,
1553 0, 0,
1554 a_num_rects,
1555 rects);
1556 is_ok = TRUE;
1557
1558 out:
1559 free(rects);
1560 rects = NULL;
1561 EPHYR_LOG("leave\n");
1562 return is_ok;
1563 }
1564
1565 #ifdef GLAMOR
1566 Bool
ephyr_glamor_init(ScreenPtr screen)1567 ephyr_glamor_init(ScreenPtr screen)
1568 {
1569 KdScreenPriv(screen);
1570 KdScreenInfo *kd_screen = pScreenPriv->screen;
1571 EphyrScrPriv *scrpriv = kd_screen->driver;
1572
1573 scrpriv->glamor = ephyr_glamor_glx_screen_init(scrpriv->win);
1574 ephyr_glamor_set_window_size(scrpriv->glamor,
1575 scrpriv->win_width, scrpriv->win_height);
1576
1577 if (!glamor_init(screen, 0)) {
1578 FatalError("Failed to initialize glamor\n");
1579 return FALSE;
1580 }
1581
1582 return TRUE;
1583 }
1584
1585 static int
ephyrSetPixmapVisitWindow(WindowPtr window,void * data)1586 ephyrSetPixmapVisitWindow(WindowPtr window, void *data)
1587 {
1588 ScreenPtr screen = window->drawable.pScreen;
1589
1590 if (screen->GetWindowPixmap(window) == data) {
1591 screen->SetWindowPixmap(window, screen->GetScreenPixmap(screen));
1592 return WT_WALKCHILDREN;
1593 }
1594 return WT_DONTWALKCHILDREN;
1595 }
1596
1597 Bool
ephyr_glamor_create_screen_resources(ScreenPtr pScreen)1598 ephyr_glamor_create_screen_resources(ScreenPtr pScreen)
1599 {
1600 KdScreenPriv(pScreen);
1601 KdScreenInfo *kd_screen = pScreenPriv->screen;
1602 EphyrScrPriv *scrpriv = kd_screen->driver;
1603 PixmapPtr old_screen_pixmap, screen_pixmap;
1604 uint32_t tex;
1605
1606 if (!ephyr_glamor)
1607 return TRUE;
1608
1609 /* kdrive's fbSetupScreen() told mi to have
1610 * miCreateScreenResources() (which is called before this) make a
1611 * scratch pixmap wrapping ephyr-glamor's NULL
1612 * KdScreenInfo->fb.framebuffer.
1613 *
1614 * We want a real (texture-based) screen pixmap at this point.
1615 * This is what glamor will render into, and we'll then texture
1616 * out of that into the host's window to present the results.
1617 *
1618 * Thus, delete the current screen pixmap, and put a fresh one in.
1619 */
1620 old_screen_pixmap = pScreen->GetScreenPixmap(pScreen);
1621 pScreen->DestroyPixmap(old_screen_pixmap);
1622
1623 screen_pixmap = pScreen->CreatePixmap(pScreen,
1624 pScreen->width,
1625 pScreen->height,
1626 pScreen->rootDepth,
1627 GLAMOR_CREATE_NO_LARGE);
1628 if (!screen_pixmap)
1629 return FALSE;
1630
1631 pScreen->SetScreenPixmap(screen_pixmap);
1632 if (pScreen->root && pScreen->SetWindowPixmap)
1633 TraverseTree(pScreen->root, ephyrSetPixmapVisitWindow, old_screen_pixmap);
1634
1635 /* Tell the GLX code what to GL texture to read from. */
1636 tex = glamor_get_pixmap_texture(screen_pixmap);
1637 if (!tex)
1638 return FALSE;
1639
1640 ephyr_glamor_set_texture(scrpriv->glamor, tex);
1641
1642 return TRUE;
1643 }
1644
1645 void
ephyr_glamor_enable(ScreenPtr screen)1646 ephyr_glamor_enable(ScreenPtr screen)
1647 {
1648 }
1649
1650 void
ephyr_glamor_disable(ScreenPtr screen)1651 ephyr_glamor_disable(ScreenPtr screen)
1652 {
1653 }
1654
1655 void
ephyr_glamor_fini(ScreenPtr screen)1656 ephyr_glamor_fini(ScreenPtr screen)
1657 {
1658 KdScreenPriv(screen);
1659 KdScreenInfo *kd_screen = pScreenPriv->screen;
1660 EphyrScrPriv *scrpriv = kd_screen->driver;
1661
1662 glamor_fini(screen);
1663 ephyr_glamor_glx_screen_fini(scrpriv->glamor);
1664 scrpriv->glamor = NULL;
1665 }
1666 #endif
1667