hostx.c (53298B)
1 /* 2 * Xephyr - A kdrive X server that 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 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 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 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 153 hostx_set_display_name(char *name) 154 { 155 HostX.server_dpy_name = strdup(name); 156 } 157 158 void 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 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 204 hostx_want_host_cursor(void) 205 { 206 return !HostX.use_sw_cursor; 207 } 208 209 void 210 hostx_use_sw_cursor(void) 211 { 212 HostX.use_sw_cursor = TRUE; 213 } 214 215 xcb_cursor_t 216 hostx_get_empty_cursor(void) 217 { 218 return HostX.empty_cursor; 219 } 220 221 int 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 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 348 hostx_use_fullscreen(void) 349 { 350 HostX.use_fullscreen = TRUE; 351 } 352 353 int 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 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 392 hostx_toggle_damage_debug(void) 393 { 394 HostXWantDamageDebug ^= 1; 395 } 396 397 void 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 405 hostx_use_resname(char *name, int fromcmd) 406 { 407 ephyrResName = name; 408 ephyrResNameFromCmd = fromcmd; 409 } 410 411 void 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 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 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 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 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 * SCALE, 622 scrpriv->win_height * SCALE, 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((char*)"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 767 hostx_get_depth(void) 768 { 769 return HostX.depth; 770 } 771 772 int 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 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 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 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 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 * 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 * SCALE, 908 buffer_height * SCALE, 909 XCB_IMAGE_FORMAT_Z_PIXMAP, 910 HostX.depth, 911 NULL, 912 ~0, 913 NULL); 914 915 scrpriv->shmsize = scrpriv->ximg->stride * buffer_height * SCALE; 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 * SCALE, 935 buffer_height * SCALE, 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 * SCALE); 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 * SCALE, height * SCALE}; 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 * SCALE; 962 size_hints.max_height = size_hints.min_height = height * SCALE; 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 overridden 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 if (SCALE != 1) assert(scrpriv->ximg->bpp == 32); 995 #ifdef GLAMOR 996 if (ephyr_glamor) { 997 *bytes_per_line = 0; 998 ephyr_glamor_set_window_size(scrpriv->glamor, 999 scrpriv->win_width, scrpriv->win_height); 1000 return NULL; 1001 } else 1002 #endif 1003 if (SCALE == 1 && host_depth_matches_server(scrpriv)) { 1004 *bytes_per_line = scrpriv->ximg->stride; 1005 *bits_per_pixel = scrpriv->ximg->bpp; 1006 1007 EPHYR_DBG("Host matches server"); 1008 return scrpriv->ximg->data; 1009 } 1010 else { 1011 int server_depth = scrpriv->server_depth == 24 ? 32 : scrpriv->server_depth; 1012 int bytes_per_pixel = server_depth >> 3; 1013 int stride = (width * bytes_per_pixel + 0x3) & ~0x3; 1014 1015 *bytes_per_line = stride; 1016 *bits_per_pixel = server_depth; 1017 1018 EPHYR_DBG("server bpp %i", bytes_per_pixel); 1019 scrpriv->fb_data = xallocarray (stride, buffer_height); 1020 return scrpriv->fb_data; 1021 } 1022 } 1023 1024 static void hostx_paint_debug_rect(KdScreenInfo *screen, 1025 int x, int y, int width, int height); 1026 1027 void 1028 hostx_paint_rect(KdScreenInfo *screen, 1029 int sx, int sy, int dx, int dy, int width, int height) 1030 { 1031 EphyrScrPriv *scrpriv = screen->driver; 1032 1033 EPHYR_DBG("painting in screen %d\n", scrpriv->mynum); 1034 1035 #ifdef GLAMOR 1036 if (ephyr_glamor) { 1037 BoxRec box; 1038 RegionRec region; 1039 1040 box.x1 = dx; 1041 box.y1 = dy; 1042 box.x2 = dx + width; 1043 box.y2 = dy + height; 1044 1045 RegionInit(®ion, &box, 1); 1046 ephyr_glamor_damage_redisplay(scrpriv->glamor, ®ion); 1047 RegionUninit(®ion); 1048 return; 1049 } 1050 #endif 1051 1052 /* 1053 * Copy the image data updated by the shadow layer 1054 * on to the window 1055 */ 1056 1057 if (HostXWantDamageDebug) { 1058 hostx_paint_debug_rect(screen, dx, dy, width, height); 1059 } 1060 1061 /* 1062 * If the depth of the ephyr server is less than that of the host, 1063 * the kdrive fb does not point to the ximage data but to a buffer 1064 * ( fb_data ), we shift the various bits from this onto the XImage 1065 * so they match the host. 1066 * 1067 * Note, This code is pretty new ( and simple ) so may break on 1068 * endian issues, 32 bpp host etc. 1069 * Not sure if 8bpp case is right either. 1070 * ... and it will be slower than the matching depth case. 1071 */ 1072 1073 if (!host_depth_matches_server(scrpriv)) { 1074 int x, y, idx, bytes_per_pixel = (scrpriv->server_depth >> 3); 1075 int stride = (scrpriv->win_width * bytes_per_pixel + 0x3) & ~0x3; 1076 unsigned char r, g, b; 1077 unsigned long host_pixel; 1078 1079 EPHYR_DBG("Unmatched host depth scrpriv=%p\n", scrpriv); 1080 for (y = sy; y < sy + height; y++) 1081 for (x = sx; x < sx + width; x++) { 1082 idx = y * stride + x * bytes_per_pixel; 1083 1084 switch (scrpriv->server_depth) { 1085 case 16: 1086 { 1087 unsigned short pixel = 1088 *(unsigned short *) (scrpriv->fb_data + idx); 1089 1090 r = ((pixel & 0xf800) >> 8); 1091 g = ((pixel & 0x07e0) >> 3); 1092 b = ((pixel & 0x001f) << 3); 1093 1094 host_pixel = (r << 16) | (g << 8) | (b); 1095 1096 for (int yp = 0; yp < SCALE; ++yp) 1097 for (int xp = 0; xp < SCALE; ++xp) 1098 xcb_image_put_pixel(scrpriv->ximg, x*SCALE+xp, y*SCALE+yp, host_pixel); 1099 break; 1100 } 1101 case 8: 1102 { 1103 unsigned char pixel = 1104 *(unsigned char *) (scrpriv->fb_data + idx); 1105 host_pixel = scrpriv->cmap[pixel]; 1106 1107 for (int yp = 0; yp < SCALE; ++yp) 1108 for (int xp = 0; xp < SCALE; ++xp) 1109 xcb_image_put_pixel(scrpriv->ximg, x*SCALE+xp, y*SCALE+yp, host_pixel); 1110 break; 1111 } 1112 default: 1113 break; 1114 } 1115 } 1116 } else if (SCALE != 1) { 1117 uint32_t* dst = (uint32_t*) scrpriv->ximg->data; 1118 uint32_t* src = (uint32_t*) scrpriv->fb_data; 1119 size_t src_stride = scrpriv->win_width; 1120 size_t dst_stride = scrpriv->ximg->stride/4; 1121 for (int y = sy; y < sy + height; ++y) 1122 for (int x = sx; x < sx + width; ++x) { 1123 uint32_t val = src[y * src_stride + x]; 1124 for (int yp = 0; yp < SCALE; ++yp) 1125 for (int xp = 0; xp < SCALE; ++xp) 1126 dst[(y*SCALE+yp) * dst_stride + (x*SCALE+xp)] = val; 1127 } 1128 } 1129 1130 if (HostX.have_shm) { 1131 xcb_image_shm_put(HostX.conn, scrpriv->win, 1132 HostX.gc, scrpriv->ximg, 1133 scrpriv->shminfo, 1134 sx*SCALE, sy*SCALE, dx*SCALE, dy*SCALE, 1135 width*SCALE, height*SCALE, FALSE); 1136 } 1137 else { 1138 xcb_image_t *subimg = xcb_image_subimage(scrpriv->ximg, sx*SCALE, sy*SCALE, 1139 width*SCALE, height*SCALE, 0, 0, 0); 1140 xcb_image_t *img = xcb_image_native(HostX.conn, subimg, 1); 1141 xcb_image_put(HostX.conn, scrpriv->win, HostX.gc, img, dx*SCALE, dy*SCALE, 0); 1142 if (subimg != img) 1143 xcb_image_destroy(img); 1144 xcb_image_destroy(subimg); 1145 } 1146 1147 xcb_aux_sync(HostX.conn); 1148 } 1149 1150 static void 1151 hostx_paint_debug_rect(KdScreenInfo *screen, 1152 int x, int y, int width, int height) 1153 { 1154 EphyrScrPriv *scrpriv = screen->driver; 1155 struct timespec tspec; 1156 xcb_rectangle_t rect = { 1157 .x = x * SCALE, .y = y * SCALE, 1158 .width = width * SCALE, .height = height * SCALE 1159 }; 1160 xcb_void_cookie_t cookie; 1161 xcb_generic_error_t *e; 1162 1163 tspec.tv_sec = HostX.damage_debug_msec / (1000000); 1164 tspec.tv_nsec = (HostX.damage_debug_msec % 1000000) * 1000; 1165 1166 EPHYR_DBG("msec: %li tv_sec %li, tv_msec %li", 1167 HostX.damage_debug_msec, tspec.tv_sec, tspec.tv_nsec); 1168 1169 /* fprintf(stderr, "Xephyr updating: %i+%i %ix%i\n", x, y, width, height); */ 1170 1171 cookie = xcb_poly_fill_rectangle_checked(HostX.conn, scrpriv->win, 1172 HostX.gc, 1, &rect); 1173 e = xcb_request_check(HostX.conn, cookie); 1174 free(e); 1175 1176 /* nanosleep seems to work better than usleep for me... */ 1177 nanosleep(&tspec, NULL); 1178 } 1179 1180 Bool 1181 hostx_load_keymap(KeySymsPtr keySyms, CARD8 *modmap, XkbControlsPtr controls) 1182 { 1183 int min_keycode, max_keycode; 1184 int map_width; 1185 size_t i, j; 1186 int keymap_len; 1187 xcb_keysym_t *keymap; 1188 xcb_keycode_t *modifier_map; 1189 xcb_get_keyboard_mapping_cookie_t mapping_c; 1190 xcb_get_keyboard_mapping_reply_t *mapping_r; 1191 xcb_get_modifier_mapping_cookie_t modifier_c; 1192 xcb_get_modifier_mapping_reply_t *modifier_r; 1193 xcb_xkb_use_extension_cookie_t use_c; 1194 xcb_xkb_use_extension_reply_t *use_r; 1195 xcb_xkb_get_controls_cookie_t controls_c; 1196 xcb_xkb_get_controls_reply_t *controls_r; 1197 1198 /* First of all, collect host X server's 1199 * min_keycode and max_keycode, which are 1200 * independent from XKB support. */ 1201 min_keycode = xcb_get_setup(HostX.conn)->min_keycode; 1202 max_keycode = xcb_get_setup(HostX.conn)->max_keycode; 1203 1204 EPHYR_DBG("min: %d, max: %d", min_keycode, max_keycode); 1205 1206 keySyms->minKeyCode = min_keycode; 1207 keySyms->maxKeyCode = max_keycode; 1208 1209 /* Check for XKB availability in host X server */ 1210 if (!hostx_has_extension(&xcb_xkb_id)) { 1211 EPHYR_LOG_ERROR("XKB extension is not supported in host X server."); 1212 return FALSE; 1213 } 1214 1215 use_c = xcb_xkb_use_extension(HostX.conn, 1216 XCB_XKB_MAJOR_VERSION, 1217 XCB_XKB_MINOR_VERSION); 1218 use_r = xcb_xkb_use_extension_reply(HostX.conn, use_c, NULL); 1219 1220 if (!use_r) { 1221 EPHYR_LOG_ERROR("Couldn't use XKB extension."); 1222 return FALSE; 1223 } else if (!use_r->supported) { 1224 EPHYR_LOG_ERROR("XKB extension is not supported in host X server."); 1225 free(use_r); 1226 return FALSE; 1227 } 1228 1229 free(use_r); 1230 1231 /* Send all needed XCB requests at once, 1232 * and process the replies as needed. */ 1233 mapping_c = xcb_get_keyboard_mapping(HostX.conn, 1234 min_keycode, 1235 max_keycode - min_keycode + 1); 1236 modifier_c = xcb_get_modifier_mapping(HostX.conn); 1237 controls_c = xcb_xkb_get_controls(HostX.conn, 1238 XCB_XKB_ID_USE_CORE_KBD); 1239 1240 mapping_r = xcb_get_keyboard_mapping_reply(HostX.conn, 1241 mapping_c, 1242 NULL); 1243 1244 if (!mapping_r) { 1245 EPHYR_LOG_ERROR("xcb_get_keyboard_mapping_reply() failed."); 1246 return FALSE; 1247 } 1248 1249 map_width = mapping_r->keysyms_per_keycode; 1250 keymap = xcb_get_keyboard_mapping_keysyms(mapping_r); 1251 keymap_len = xcb_get_keyboard_mapping_keysyms_length(mapping_r); 1252 1253 keySyms->mapWidth = map_width; 1254 keySyms->map = calloc(keymap_len, sizeof(KeySym)); 1255 1256 if (!keySyms->map) { 1257 EPHYR_LOG_ERROR("Failed to allocate KeySym map."); 1258 free(mapping_r); 1259 return FALSE; 1260 } 1261 1262 for (i = 0; i < keymap_len; i++) { 1263 keySyms->map[i] = keymap[i]; 1264 } 1265 1266 free(mapping_r); 1267 1268 modifier_r = xcb_get_modifier_mapping_reply(HostX.conn, 1269 modifier_c, 1270 NULL); 1271 1272 if (!modifier_r) { 1273 EPHYR_LOG_ERROR("xcb_get_modifier_mapping_reply() failed."); 1274 return FALSE; 1275 } 1276 1277 modifier_map = xcb_get_modifier_mapping_keycodes(modifier_r); 1278 memset(modmap, 0, sizeof(CARD8) * MAP_LENGTH); 1279 1280 for (j = 0; j < 8; j++) { 1281 for (i = 0; i < modifier_r->keycodes_per_modifier; i++) { 1282 CARD8 keycode; 1283 1284 if ((keycode = modifier_map[j * modifier_r->keycodes_per_modifier + i])) { 1285 modmap[keycode] |= 1 << j; 1286 } 1287 } 1288 } 1289 1290 free(modifier_r); 1291 1292 controls_r = xcb_xkb_get_controls_reply(HostX.conn, 1293 controls_c, 1294 NULL); 1295 1296 if (!controls_r) { 1297 EPHYR_LOG_ERROR("xcb_xkb_get_controls_reply() failed."); 1298 return FALSE; 1299 } 1300 1301 controls->enabled_ctrls = controls_r->enabledControls; 1302 1303 for (i = 0; i < XkbPerKeyBitArraySize; i++) { 1304 controls->per_key_repeat[i] = controls_r->perKeyRepeat[i]; 1305 } 1306 1307 free(controls_r); 1308 1309 return TRUE; 1310 } 1311 1312 void 1313 hostx_size_set_from_configure(Bool ss) 1314 { 1315 HostX.size_set_from_configure = ss; 1316 } 1317 1318 xcb_connection_t * 1319 hostx_get_xcbconn(void) 1320 { 1321 return HostX.conn; 1322 } 1323 1324 xcb_generic_event_t * 1325 hostx_get_event(Bool queued_only) 1326 { 1327 xcb_generic_event_t *xev; 1328 1329 if (HostX.saved_event) { 1330 xev = HostX.saved_event; 1331 HostX.saved_event = NULL; 1332 } else { 1333 if (queued_only) 1334 xev = xcb_poll_for_queued_event(HostX.conn); 1335 else 1336 xev = xcb_poll_for_event(HostX.conn); 1337 } 1338 return xev; 1339 } 1340 1341 Bool 1342 hostx_has_queued_event(void) 1343 { 1344 if (!HostX.saved_event) 1345 HostX.saved_event = xcb_poll_for_queued_event(HostX.conn); 1346 return HostX.saved_event != NULL; 1347 } 1348 1349 int 1350 hostx_get_screen(void) 1351 { 1352 return HostX.screen; 1353 } 1354 1355 int 1356 hostx_get_fd(void) 1357 { 1358 return xcb_get_file_descriptor(HostX.conn); 1359 } 1360 1361 int 1362 hostx_get_window(int a_screen_number) 1363 { 1364 EphyrScrPriv *scrpriv; 1365 if (a_screen_number < 0 || a_screen_number >= HostX.n_screens) { 1366 EPHYR_LOG_ERROR("bad screen number:%d\n", a_screen_number); 1367 return 0; 1368 } 1369 scrpriv = HostX.screens[a_screen_number]->driver; 1370 return scrpriv->win; 1371 } 1372 1373 int 1374 hostx_get_window_attributes(int a_window, EphyrHostWindowAttributes * a_attrs) 1375 { 1376 xcb_get_geometry_cookie_t geom_cookie; 1377 xcb_get_window_attributes_cookie_t attr_cookie; 1378 xcb_get_geometry_reply_t *geom_reply; 1379 xcb_get_window_attributes_reply_t *attr_reply; 1380 1381 geom_cookie = xcb_get_geometry(HostX.conn, a_window); 1382 attr_cookie = xcb_get_window_attributes(HostX.conn, a_window); 1383 geom_reply = xcb_get_geometry_reply(HostX.conn, geom_cookie, NULL); 1384 attr_reply = xcb_get_window_attributes_reply(HostX.conn, attr_cookie, NULL); 1385 1386 a_attrs->x = geom_reply->x; 1387 a_attrs->y = geom_reply->y; 1388 a_attrs->width = geom_reply->width; 1389 a_attrs->height = geom_reply->height; 1390 a_attrs->visualid = attr_reply->visual; 1391 1392 free(geom_reply); 1393 free(attr_reply); 1394 return TRUE; 1395 } 1396 1397 int 1398 hostx_get_visuals_info(EphyrHostVisualInfo ** a_visuals, int *a_num_entries) 1399 { 1400 Bool is_ok = FALSE; 1401 EphyrHostVisualInfo *host_visuals = NULL; 1402 int nb_items = 0, i = 0, screen_num; 1403 xcb_screen_iterator_t screens; 1404 xcb_depth_iterator_t depths; 1405 1406 EPHYR_RETURN_VAL_IF_FAIL(a_visuals && a_num_entries, FALSE); 1407 EPHYR_LOG("enter\n"); 1408 1409 screens = xcb_setup_roots_iterator(xcb_get_setup(HostX.conn)); 1410 for (screen_num = 0; screens.rem; screen_num++, xcb_screen_next(&screens)) { 1411 depths = xcb_screen_allowed_depths_iterator(screens.data); 1412 for (; depths.rem; xcb_depth_next(&depths)) { 1413 xcb_visualtype_t *visuals = xcb_depth_visuals(depths.data); 1414 EphyrHostVisualInfo *tmp_visuals = 1415 reallocarray(host_visuals, 1416 nb_items + depths.data->visuals_len, 1417 sizeof(EphyrHostVisualInfo)); 1418 if (!tmp_visuals) { 1419 goto out; 1420 } 1421 host_visuals = tmp_visuals; 1422 for (i = 0; i < depths.data->visuals_len; i++) { 1423 host_visuals[nb_items + i].visualid = visuals[i].visual_id; 1424 host_visuals[nb_items + i].screen = screen_num; 1425 host_visuals[nb_items + i].depth = depths.data->depth; 1426 host_visuals[nb_items + i].class = visuals[i]._class; 1427 host_visuals[nb_items + i].red_mask = visuals[i].red_mask; 1428 host_visuals[nb_items + i].green_mask = visuals[i].green_mask; 1429 host_visuals[nb_items + i].blue_mask = visuals[i].blue_mask; 1430 host_visuals[nb_items + i].colormap_size = visuals[i].colormap_entries; 1431 host_visuals[nb_items + i].bits_per_rgb = visuals[i].bits_per_rgb_value; 1432 } 1433 nb_items += depths.data->visuals_len; 1434 } 1435 } 1436 1437 EPHYR_LOG("host advertises %d visuals\n", nb_items); 1438 *a_visuals = host_visuals; 1439 *a_num_entries = nb_items; 1440 host_visuals = NULL; 1441 1442 is_ok = TRUE; 1443 out: 1444 free(host_visuals); 1445 host_visuals = NULL; 1446 EPHYR_LOG("leave\n"); 1447 return is_ok; 1448 1449 } 1450 1451 int 1452 hostx_create_window(int a_screen_number, 1453 EphyrBox * a_geometry, 1454 int a_visual_id, int *a_host_peer /*out parameter */ ) 1455 { 1456 Bool is_ok = FALSE; 1457 xcb_window_t win; 1458 int winmask = 0; 1459 uint32_t attrs[2]; 1460 xcb_screen_t *screen = xcb_aux_get_screen(HostX.conn, hostx_get_screen()); 1461 xcb_visualtype_t *visual; 1462 int depth = 0; 1463 EphyrScrPriv *scrpriv = HostX.screens[a_screen_number]->driver; 1464 1465 EPHYR_RETURN_VAL_IF_FAIL(screen && a_geometry, FALSE); 1466 1467 EPHYR_LOG("enter\n"); 1468 1469 visual = xcb_aux_find_visual_by_id(screen, a_visual_id); 1470 if (!visual) { 1471 EPHYR_LOG_ERROR ("argh, could not find a remote visual with id:%d\n", 1472 a_visual_id); 1473 goto out; 1474 } 1475 depth = xcb_aux_get_depth_of_visual(screen, a_visual_id); 1476 1477 winmask = XCB_CW_EVENT_MASK | XCB_CW_COLORMAP; 1478 attrs[0] = XCB_EVENT_MASK_BUTTON_PRESS 1479 |XCB_EVENT_MASK_BUTTON_RELEASE 1480 |XCB_EVENT_MASK_POINTER_MOTION 1481 |XCB_EVENT_MASK_KEY_PRESS 1482 |XCB_EVENT_MASK_KEY_RELEASE 1483 |XCB_EVENT_MASK_EXPOSURE; 1484 attrs[1] = xcb_generate_id(HostX.conn); 1485 xcb_create_colormap(HostX.conn, 1486 XCB_COLORMAP_ALLOC_NONE, 1487 attrs[1], 1488 hostx_get_window(a_screen_number), 1489 a_visual_id); 1490 1491 win = xcb_generate_id(HostX.conn); 1492 xcb_create_window(HostX.conn, 1493 depth, 1494 win, 1495 hostx_get_window (a_screen_number), 1496 a_geometry->x, a_geometry->y, 1497 a_geometry->width * SCALE, a_geometry->height * SCALE, 0, 1498 XCB_WINDOW_CLASS_COPY_FROM_PARENT, 1499 a_visual_id, winmask, attrs); 1500 1501 if (scrpriv->peer_win == XCB_NONE) { 1502 scrpriv->peer_win = win; 1503 } 1504 else { 1505 EPHYR_LOG_ERROR("multiple peer windows created for same screen\n"); 1506 } 1507 xcb_flush(HostX.conn); 1508 xcb_map_window(HostX.conn, win); 1509 *a_host_peer = win; 1510 is_ok = TRUE; 1511 out: 1512 EPHYR_LOG("leave\n"); 1513 return is_ok; 1514 } 1515 1516 int 1517 hostx_destroy_window(int a_win) 1518 { 1519 xcb_destroy_window(HostX.conn, a_win); 1520 xcb_flush(HostX.conn); 1521 return TRUE; 1522 } 1523 1524 int 1525 hostx_set_window_geometry(int a_win, EphyrBox * a_geo) 1526 { 1527 uint32_t mask = XCB_CONFIG_WINDOW_X 1528 | XCB_CONFIG_WINDOW_Y 1529 | XCB_CONFIG_WINDOW_WIDTH 1530 | XCB_CONFIG_WINDOW_HEIGHT; 1531 uint32_t values[4]; 1532 1533 EPHYR_RETURN_VAL_IF_FAIL(a_geo, FALSE); 1534 1535 EPHYR_LOG("enter. x,y,w,h:(%d,%d,%d,%d)\n", 1536 a_geo->x, a_geo->y, a_geo->width, a_geo->height); 1537 1538 values[0] = a_geo->x; 1539 values[1] = a_geo->y; 1540 values[2] = a_geo->width * SCALE; 1541 values[3] = a_geo->height * SCALE; 1542 xcb_configure_window(HostX.conn, a_win, mask, values); 1543 1544 EPHYR_LOG("leave\n"); 1545 return TRUE; 1546 } 1547 1548 int 1549 hostx_set_window_bounding_rectangles(int a_window, 1550 EphyrRect * a_rects, int a_num_rects) 1551 { 1552 Bool is_ok = FALSE; 1553 int i = 0; 1554 xcb_rectangle_t *rects = NULL; 1555 1556 EPHYR_RETURN_VAL_IF_FAIL(a_rects, FALSE); 1557 1558 EPHYR_LOG("enter. num rects:%d\n", a_num_rects); 1559 1560 rects = calloc(a_num_rects, sizeof (xcb_rectangle_t)); 1561 if (!rects) 1562 goto out; 1563 for (i = 0; i < a_num_rects; i++) { 1564 rects[i].x = a_rects[i].x1; 1565 rects[i].y = a_rects[i].y1; 1566 rects[i].width = abs(a_rects[i].x2 - a_rects[i].x1); 1567 rects[i].height = abs(a_rects[i].y2 - a_rects[i].y1); 1568 EPHYR_LOG("borders clipped to rect[x:%d,y:%d,w:%d,h:%d]\n", 1569 rects[i].x, rects[i].y, rects[i].width, rects[i].height); 1570 } 1571 xcb_shape_rectangles(HostX.conn, 1572 XCB_SHAPE_SO_SET, 1573 XCB_SHAPE_SK_BOUNDING, 1574 XCB_CLIP_ORDERING_YX_BANDED, 1575 a_window, 1576 0, 0, 1577 a_num_rects, 1578 rects); 1579 is_ok = TRUE; 1580 1581 out: 1582 free(rects); 1583 rects = NULL; 1584 EPHYR_LOG("leave\n"); 1585 return is_ok; 1586 } 1587 1588 #ifdef GLAMOR 1589 Bool 1590 ephyr_glamor_init(ScreenPtr screen) 1591 { 1592 KdScreenPriv(screen); 1593 KdScreenInfo *kd_screen = pScreenPriv->screen; 1594 EphyrScrPriv *scrpriv = kd_screen->driver; 1595 1596 scrpriv->glamor = ephyr_glamor_glx_screen_init(scrpriv->win); 1597 ephyr_glamor_set_window_size(scrpriv->glamor, 1598 scrpriv->win_width, scrpriv->win_height); 1599 1600 if (!glamor_init(screen, 0)) { 1601 FatalError("Failed to initialize glamor\n"); 1602 return FALSE; 1603 } 1604 1605 return TRUE; 1606 } 1607 1608 static int 1609 ephyrSetPixmapVisitWindow(WindowPtr window, void *data) 1610 { 1611 ScreenPtr screen = window->drawable.pScreen; 1612 1613 if (screen->GetWindowPixmap(window) == data) { 1614 screen->SetWindowPixmap(window, screen->GetScreenPixmap(screen)); 1615 return WT_WALKCHILDREN; 1616 } 1617 return WT_DONTWALKCHILDREN; 1618 } 1619 1620 Bool 1621 ephyr_glamor_create_screen_resources(ScreenPtr pScreen) 1622 { 1623 KdScreenPriv(pScreen); 1624 KdScreenInfo *kd_screen = pScreenPriv->screen; 1625 EphyrScrPriv *scrpriv = kd_screen->driver; 1626 PixmapPtr old_screen_pixmap, screen_pixmap; 1627 uint32_t tex; 1628 1629 if (!ephyr_glamor) 1630 return TRUE; 1631 1632 /* kdrive's fbSetupScreen() told mi to have 1633 * miCreateScreenResources() (which is called before this) make a 1634 * scratch pixmap wrapping ephyr-glamor's NULL 1635 * KdScreenInfo->fb.framebuffer. 1636 * 1637 * We want a real (texture-based) screen pixmap at this point. 1638 * This is what glamor will render into, and we'll then texture 1639 * out of that into the host's window to present the results. 1640 * 1641 * Thus, delete the current screen pixmap, and put a fresh one in. 1642 */ 1643 old_screen_pixmap = pScreen->GetScreenPixmap(pScreen); 1644 pScreen->DestroyPixmap(old_screen_pixmap); 1645 1646 screen_pixmap = pScreen->CreatePixmap(pScreen, 1647 pScreen->width, 1648 pScreen->height, 1649 pScreen->rootDepth, 1650 GLAMOR_CREATE_NO_LARGE); 1651 if (!screen_pixmap) 1652 return FALSE; 1653 1654 pScreen->SetScreenPixmap(screen_pixmap); 1655 if (pScreen->root && pScreen->SetWindowPixmap) 1656 TraverseTree(pScreen->root, ephyrSetPixmapVisitWindow, old_screen_pixmap); 1657 1658 /* Tell the GLX code what to GL texture to read from. */ 1659 tex = glamor_get_pixmap_texture(screen_pixmap); 1660 if (!tex) 1661 return FALSE; 1662 1663 ephyr_glamor_set_texture(scrpriv->glamor, tex); 1664 1665 return TRUE; 1666 } 1667 1668 void 1669 ephyr_glamor_enable(ScreenPtr screen) 1670 { 1671 } 1672 1673 void 1674 ephyr_glamor_disable(ScreenPtr screen) 1675 { 1676 } 1677 1678 void 1679 ephyr_glamor_fini(ScreenPtr screen) 1680 { 1681 KdScreenPriv(screen); 1682 KdScreenInfo *kd_screen = pScreenPriv->screen; 1683 EphyrScrPriv *scrpriv = kd_screen->driver; 1684 1685 glamor_fini(screen); 1686 ephyr_glamor_glx_screen_fini(scrpriv->glamor); 1687 scrpriv->glamor = NULL; 1688 } 1689 #endif