| 1 | /* |
| 2 | * Copyright © 2004 Keith Packard |
| 3 | * |
| 4 | * Permission to use, copy, modify, distribute, and sell this software and its |
| 5 | * documentation for any purpose is hereby granted without fee, provided that |
| 6 | * the above copyright notice appear in all copies and that both that |
| 7 | * copyright notice and this permission notice appear in supporting |
| 8 | * documentation, and that the name of Keith Packard not be used in |
| 9 | * advertising or publicity pertaining to distribution of the software without |
| 10 | * specific, written prior permission. Keith Packard makes no |
| 11 | * representations about the suitability of this software for any purpose. It |
| 12 | * is provided "as is" without express or implied warranty. |
| 13 | * |
| 14 | * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 15 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 16 | * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 17 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 18 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 19 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 20 | * PERFORMANCE OF THIS SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <kdrive-config.h> |
| 25 | #endif |
| 26 | #include "fake.h" |
| 27 | |
| 28 | extern int KdTsPhyScreen; |
| 29 | |
| 30 | Bool |
| 31 | fakeInitialize(KdCardInfo * card, FakePriv * priv) |
| 32 | { |
| 33 | priv->base = 0; |
| 34 | priv->bytes_per_line = 0; |
| 35 | return TRUE; |
| 36 | } |
| 37 | |
| 38 | Bool |
| 39 | fakeCardInit(KdCardInfo * card) |
| 40 | { |
| 41 | FakePriv *priv; |
| 42 | |
| 43 | priv = (FakePriv *) malloc(sizeof(FakePriv)); |
| 44 | if (!priv) |
| 45 | return FALSE; |
| 46 | |
| 47 | if (!fakeInitialize(card, priv)) { |
| 48 | free(priv); |
| 49 | return FALSE; |
| 50 | } |
| 51 | card->driver = priv; |
| 52 | |
| 53 | return TRUE; |
| 54 | } |
| 55 | |
| 56 | Bool |
| 57 | fakeScreenInitialize(KdScreenInfo * screen, FakeScrPriv * scrpriv) |
| 58 | { |
| 59 | if (!screen->width || !screen->height) { |
| 60 | screen->width = 1024; |
| 61 | screen->height = 768; |
| 62 | screen->rate = 72; |
| 63 | } |
| 64 | |
| 65 | if (screen->width <= 0) |
| 66 | screen->width = 1; |
| 67 | if (screen->height <= 0) |
| 68 | screen->height = 1; |
| 69 | |
| 70 | if (!screen->fb.depth) |
| 71 | screen->fb.depth = 16; |
| 72 | |
| 73 | if (screen->fb.depth <= 8) { |
| 74 | screen->fb.visuals = ((1 << StaticGray) | |
| 75 | (1 << GrayScale) | |
| 76 | (1 << StaticColor) | |
| 77 | (1 << PseudoColor) | |
| 78 | (1 << TrueColor) | (1 << DirectColor)); |
| 79 | } |
| 80 | else { |
| 81 | screen->fb.visuals = (1 << TrueColor); |
| 82 | #define Mask(o,l) (((1 << l) - 1) << o) |
| 83 | if (screen->fb.depth <= 15) { |
| 84 | screen->fb.depth = 15; |
| 85 | screen->fb.bitsPerPixel = 16; |
| 86 | screen->fb.redMask = Mask (10, 5); |
| 87 | screen->fb.greenMask = Mask (5, 5); |
| 88 | screen->fb.blueMask = Mask (0, 5); |
| 89 | } |
| 90 | else if (screen->fb.depth <= 16) { |
| 91 | screen->fb.depth = 16; |
| 92 | screen->fb.bitsPerPixel = 16; |
| 93 | screen->fb.redMask = Mask (11, 5); |
| 94 | screen->fb.greenMask = Mask (5, 6); |
| 95 | screen->fb.blueMask = Mask (0, 5); |
| 96 | } |
| 97 | else { |
| 98 | screen->fb.depth = 24; |
| 99 | screen->fb.bitsPerPixel = 32; |
| 100 | screen->fb.redMask = Mask (16, 8); |
| 101 | screen->fb.greenMask = Mask (8, 8); |
| 102 | screen->fb.blueMask = Mask (0, 8); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | scrpriv->randr = screen->randr; |
| 107 | |
| 108 | return fakeMapFramebuffer(screen); |
| 109 | } |
| 110 | |
| 111 | Bool |
| 112 | fakeScreenInit(KdScreenInfo * screen) |
| 113 | { |
| 114 | FakeScrPriv *scrpriv; |
| 115 | |
| 116 | scrpriv = calloc(1, sizeof(FakeScrPriv)); |
| 117 | if (!scrpriv) |
| 118 | return FALSE; |
| 119 | screen->driver = scrpriv; |
| 120 | if (!fakeScreenInitialize(screen, scrpriv)) { |
| 121 | screen->driver = 0; |
| 122 | free(scrpriv); |
| 123 | return FALSE; |
| 124 | } |
| 125 | return TRUE; |
| 126 | } |
| 127 | |
| 128 | void * |
| 129 | fakeWindowLinear(ScreenPtr pScreen, |
| 130 | CARD32 row, |
| 131 | CARD32 offset, int mode, CARD32 *size, void *closure) |
| 132 | { |
| 133 | KdScreenPriv(pScreen); |
| 134 | FakePriv *priv = pScreenPriv->card->driver; |
| 135 | |
| 136 | if (!pScreenPriv->enabled) |
| 137 | return 0; |
| 138 | *size = priv->bytes_per_line; |
| 139 | return priv->base + row * priv->bytes_per_line; |
| 140 | } |
| 141 | |
| 142 | Bool |
| 143 | fakeMapFramebuffer(KdScreenInfo * screen) |
| 144 | { |
| 145 | FakeScrPriv *scrpriv = screen->driver; |
| 146 | KdPointerMatrix m; |
| 147 | FakePriv *priv = screen->card->driver; |
| 148 | |
| 149 | if (scrpriv->randr != RR_Rotate_0) |
| 150 | scrpriv->shadow = TRUE; |
| 151 | else |
| 152 | scrpriv->shadow = FALSE; |
| 153 | |
| 154 | KdComputePointerMatrix(&m, scrpriv->randr, screen->width, screen->height); |
| 155 | |
| 156 | KdSetPointerMatrix(&m); |
| 157 | |
| 158 | priv->bytes_per_line = |
| 159 | ((screen->width * screen->fb.bitsPerPixel + 31) >> 5) << 2; |
| 160 | free(priv->base); |
| 161 | priv->base = malloc(priv->bytes_per_line * screen->height); |
| 162 | |
| 163 | if (scrpriv->shadow) { |
| 164 | if (!KdShadowFbAlloc |
| 165 | (screen, scrpriv->randr & (RR_Rotate_90 | RR_Rotate_270))) |
| 166 | return FALSE; |
| 167 | } |
| 168 | else { |
| 169 | screen->fb.byteStride = priv->bytes_per_line; |
| 170 | screen->fb.pixelStride = (priv->bytes_per_line * 8 / |
| 171 | screen->fb.bitsPerPixel); |
| 172 | screen->fb.frameBuffer = (CARD8 *) (priv->base); |
| 173 | } |
| 174 | |
| 175 | return TRUE; |
| 176 | } |
| 177 | |
| 178 | void |
| 179 | fakeSetScreenSizes(ScreenPtr pScreen) |
| 180 | { |
| 181 | KdScreenPriv(pScreen); |
| 182 | KdScreenInfo *screen = pScreenPriv->screen; |
| 183 | FakeScrPriv *scrpriv = screen->driver; |
| 184 | |
| 185 | if (scrpriv->randr & (RR_Rotate_0 | RR_Rotate_180)) { |
| 186 | pScreen->width = screen->width; |
| 187 | pScreen->height = screen->height; |
| 188 | pScreen->mmWidth = screen->width_mm; |
| 189 | pScreen->mmHeight = screen->height_mm; |
| 190 | } |
| 191 | else { |
| 192 | pScreen->width = screen->width; |
| 193 | pScreen->height = screen->height; |
| 194 | pScreen->mmWidth = screen->height_mm; |
| 195 | pScreen->mmHeight = screen->width_mm; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | Bool |
| 200 | fakeUnmapFramebuffer(KdScreenInfo * screen) |
| 201 | { |
| 202 | FakePriv *priv = screen->card->driver; |
| 203 | |
| 204 | KdShadowFbFree(screen); |
| 205 | free(priv->base); |
| 206 | priv->base = NULL; |
| 207 | return TRUE; |
| 208 | } |
| 209 | |
| 210 | Bool |
| 211 | fakeSetShadow(ScreenPtr pScreen) |
| 212 | { |
| 213 | KdScreenPriv(pScreen); |
| 214 | KdScreenInfo *screen = pScreenPriv->screen; |
| 215 | FakeScrPriv *scrpriv = screen->driver; |
| 216 | ShadowUpdateProc update; |
| 217 | ShadowWindowProc window; |
| 218 | |
| 219 | window = fakeWindowLinear; |
| 220 | update = 0; |
| 221 | if (scrpriv->randr) |
| 222 | update = shadowUpdateRotatePacked; |
| 223 | else |
| 224 | update = shadowUpdatePacked; |
| 225 | return KdShadowSet(pScreen, scrpriv->randr, update, window); |
| 226 | } |
| 227 | |
| 228 | #ifdef RANDR |
| 229 | Bool |
| 230 | fakeRandRGetInfo(ScreenPtr pScreen, Rotation * rotations) |
| 231 | { |
| 232 | KdScreenPriv(pScreen); |
| 233 | KdScreenInfo *screen = pScreenPriv->screen; |
| 234 | FakeScrPriv *scrpriv = screen->driver; |
| 235 | RRScreenSizePtr pSize; |
| 236 | Rotation randr; |
| 237 | int n; |
| 238 | |
| 239 | *rotations = RR_Rotate_All | RR_Reflect_All; |
| 240 | |
| 241 | for (n = 0; n < pScreen->numDepths; n++) |
| 242 | if (pScreen->allowedDepths[n].numVids) |
| 243 | break; |
| 244 | if (n == pScreen->numDepths) |
| 245 | return FALSE; |
| 246 | |
| 247 | pSize = RRRegisterSize(pScreen, |
| 248 | screen->width, |
| 249 | screen->height, screen->width_mm, screen->height_mm); |
| 250 | |
| 251 | randr = KdSubRotation(scrpriv->randr, screen->randr); |
| 252 | |
| 253 | RRSetCurrentConfig(pScreen, randr, 0, pSize); |
| 254 | |
| 255 | return TRUE; |
| 256 | } |
| 257 | |
| 258 | Bool |
| 259 | fakeRandRSetConfig(ScreenPtr pScreen, |
| 260 | Rotation randr, int rate, RRScreenSizePtr pSize) |
| 261 | { |
| 262 | KdScreenPriv(pScreen); |
| 263 | KdScreenInfo *screen = pScreenPriv->screen; |
| 264 | FakeScrPriv *scrpriv = screen->driver; |
| 265 | Bool wasEnabled = pScreenPriv->enabled; |
| 266 | FakeScrPriv oldscr; |
| 267 | int oldwidth; |
| 268 | int oldheight; |
| 269 | int oldmmwidth; |
| 270 | int oldmmheight; |
| 271 | int newwidth, newheight; |
| 272 | |
| 273 | if (screen->randr & (RR_Rotate_0 | RR_Rotate_180)) { |
| 274 | newwidth = pSize->width; |
| 275 | newheight = pSize->height; |
| 276 | } |
| 277 | else { |
| 278 | newwidth = pSize->height; |
| 279 | newheight = pSize->width; |
| 280 | } |
| 281 | |
| 282 | if (wasEnabled) |
| 283 | KdDisableScreen(pScreen); |
| 284 | |
| 285 | oldscr = *scrpriv; |
| 286 | |
| 287 | oldwidth = screen->width; |
| 288 | oldheight = screen->height; |
| 289 | oldmmwidth = pScreen->mmWidth; |
| 290 | oldmmheight = pScreen->mmHeight; |
| 291 | |
| 292 | /* |
| 293 | * Set new configuration |
| 294 | */ |
| 295 | |
| 296 | scrpriv->randr = KdAddRotation(screen->randr, randr); |
| 297 | |
| 298 | fakeUnmapFramebuffer(screen); |
| 299 | |
| 300 | if (!fakeMapFramebuffer(screen)) |
| 301 | goto bail4; |
| 302 | |
| 303 | KdShadowUnset(screen->pScreen); |
| 304 | |
| 305 | if (!fakeSetShadow(screen->pScreen)) |
| 306 | goto bail4; |
| 307 | |
| 308 | fakeSetScreenSizes(screen->pScreen); |
| 309 | |
| 310 | /* |
| 311 | * Set frame buffer mapping |
| 312 | */ |
| 313 | (*pScreen->ModifyPixmapHeader) (fbGetScreenPixmap(pScreen), |
| 314 | pScreen->width, |
| 315 | pScreen->height, |
| 316 | screen->fb.depth, |
| 317 | screen->fb.bitsPerPixel, |
| 318 | screen->fb.byteStride, |
| 319 | screen->fb.frameBuffer); |
| 320 | |
| 321 | /* set the subpixel order */ |
| 322 | |
| 323 | KdSetSubpixelOrder(pScreen, scrpriv->randr); |
| 324 | if (wasEnabled) |
| 325 | KdEnableScreen(pScreen); |
| 326 | |
| 327 | return TRUE; |
| 328 | |
| 329 | bail4: |
| 330 | fakeUnmapFramebuffer(screen); |
| 331 | *scrpriv = oldscr; |
| 332 | (void) fakeMapFramebuffer(screen); |
| 333 | pScreen->width = oldwidth; |
| 334 | pScreen->height = oldheight; |
| 335 | pScreen->mmWidth = oldmmwidth; |
| 336 | pScreen->mmHeight = oldmmheight; |
| 337 | |
| 338 | if (wasEnabled) |
| 339 | KdEnableScreen(pScreen); |
| 340 | return FALSE; |
| 341 | } |
| 342 | |
| 343 | Bool |
| 344 | fakeRandRInit(ScreenPtr pScreen) |
| 345 | { |
| 346 | rrScrPrivPtr pScrPriv; |
| 347 | |
| 348 | if (!RRScreenInit(pScreen)) |
| 349 | return FALSE; |
| 350 | |
| 351 | pScrPriv = rrGetScrPriv(pScreen); |
| 352 | pScrPriv->rrGetInfo = fakeRandRGetInfo; |
| 353 | pScrPriv->rrSetConfig = fakeRandRSetConfig; |
| 354 | return TRUE; |
| 355 | } |
| 356 | #endif |
| 357 | |
| 358 | Bool |
| 359 | fakeCreateColormap(ColormapPtr pmap) |
| 360 | { |
| 361 | return fbInitializeColormap(pmap); |
| 362 | } |
| 363 | |
| 364 | Bool |
| 365 | fakeInitScreen(ScreenPtr pScreen) |
| 366 | { |
| 367 | #ifdef TOUCHSCREEN |
| 368 | KdTsPhyScreen = pScreen->myNum; |
| 369 | #endif |
| 370 | |
| 371 | pScreen->CreateColormap = fakeCreateColormap; |
| 372 | return TRUE; |
| 373 | } |
| 374 | |
| 375 | Bool |
| 376 | fakeFinishInitScreen(ScreenPtr pScreen) |
| 377 | { |
| 378 | if (!shadowSetup(pScreen)) |
| 379 | return FALSE; |
| 380 | |
| 381 | #ifdef RANDR |
| 382 | if (!fakeRandRInit(pScreen)) |
| 383 | return FALSE; |
| 384 | #endif |
| 385 | |
| 386 | return TRUE; |
| 387 | } |
| 388 | |
| 389 | Bool |
| 390 | fakeCreateResources(ScreenPtr pScreen) |
| 391 | { |
| 392 | return fakeSetShadow(pScreen); |
| 393 | } |
| 394 | |
| 395 | void |
| 396 | fakePreserve(KdCardInfo * card) |
| 397 | { |
| 398 | } |
| 399 | |
| 400 | Bool |
| 401 | fakeEnable(ScreenPtr pScreen) |
| 402 | { |
| 403 | return TRUE; |
| 404 | } |
| 405 | |
| 406 | Bool |
| 407 | fakeDPMS(ScreenPtr pScreen, int mode) |
| 408 | { |
| 409 | return TRUE; |
| 410 | } |
| 411 | |
| 412 | void |
| 413 | fakeDisable(ScreenPtr pScreen) |
| 414 | { |
| 415 | } |
| 416 | |
| 417 | void |
| 418 | fakeRestore(KdCardInfo * card) |
| 419 | { |
| 420 | } |
| 421 | |
| 422 | void |
| 423 | fakeScreenFini(KdScreenInfo * screen) |
| 424 | { |
| 425 | } |
| 426 | |
| 427 | void |
| 428 | fakeCardFini(KdCardInfo * card) |
| 429 | { |
| 430 | FakePriv *priv = card->driver; |
| 431 | |
| 432 | free(priv->base); |
| 433 | free(priv); |
| 434 | } |
| 435 | |
| 436 | void |
| 437 | fakeGetColors(ScreenPtr pScreen, int n, xColorItem * pdefs) |
| 438 | { |
| 439 | while (n--) { |
| 440 | pdefs->red = 0; |
| 441 | pdefs->green = 0; |
| 442 | pdefs->blue = 0; |
| 443 | pdefs++; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | void |
| 448 | fakePutColors(ScreenPtr pScreen, int n, xColorItem * pdefs) |
| 449 | { |
| 450 | } |