aboutsummaryrefslogtreecommitdiff
path: root/xorg-server/hw/dmx/dmxscrinit.c
blob: 37b3f3b5ae0b834d3dedbdaf0741619471d32798 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
 * Copyright 2001-2004 Red Hat Inc., Durham, North Carolina.
 *
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation on the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NON-INFRINGEMENT.  IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/*
 * Authors:
 *   Kevin E. Martin <kem@redhat.com>
 *   David H. Dawes <dawes@xfree86.org>
 *
 */

/** \file
 * This file provides support for screen initialization. */

#ifdef HAVE_DMX_CONFIG_H
#include <dmx-config.h>
#endif

#include "dmx.h"
#include "dmxsync.h"
#include "dmxshadow.h"
#include "dmxscrinit.h"
#include "dmxcursor.h"
#include "dmxgc.h"
#include "dmxgcops.h"
#include "dmxwindow.h"
#include "dmxpixmap.h"
#include "dmxfont.h"
#include "dmxcmap.h"
#include "dmxprop.h"
#include "dmxdpms.h"

#include "dmxpict.h"

#include "fb.h"
#include "mipointer.h"
#include "micmap.h"

extern Bool dmxCloseScreen(int idx, ScreenPtr pScreen);
static Bool dmxSaveScreen(ScreenPtr pScreen, int what);

static unsigned long dmxGeneration;
static unsigned long *dmxCursorGeneration;

DevPrivateKeyRec dmxGCPrivateKeyRec;
DevPrivateKeyRec dmxWinPrivateKeyRec;
DevPrivateKeyRec dmxPixPrivateKeyRec;
int dmxFontPrivateIndex;        /**< Private index for Fonts     */
DevPrivateKeyRec dmxScreenPrivateKeyRec;
DevPrivateKeyRec dmxColormapPrivateKeyRec;
DevPrivateKeyRec dmxPictPrivateKeyRec;
DevPrivateKeyRec dmxGlyphSetPrivateKeyRec;

/** Initialize the parts of screen \a idx that require access to the
 *  back-end server. */
void
dmxBEScreenInit(int idx, ScreenPtr pScreen)
{
    DMXScreenInfo *dmxScreen = &dmxScreens[idx];
    XSetWindowAttributes attribs;
    XGCValues gcvals;
    unsigned long mask;
    int i, j;

    /* FIXME: The dmxScreenInit() code currently assumes that it will
     * not be called if the Xdmx server is started with this screen
     * detached -- i.e., it assumes that dmxScreen->beDisplay is always
     * valid.  This is not necessarily a valid assumption when full
     * addition/removal of screens is implemented, but when this code is
     * broken out for screen reattachment, then we will reevaluate this
     * assumption.
     */

    pScreen->mmWidth = DisplayWidthMM(dmxScreen->beDisplay,
                                      DefaultScreen(dmxScreen->beDisplay));
    pScreen->mmHeight = DisplayHeightMM(dmxScreen->beDisplay,
                                        DefaultScreen(dmxScreen->beDisplay));

    pScreen->whitePixel = dmxScreen->beWhitePixel;
    pScreen->blackPixel = dmxScreen->beBlackPixel;

    /* Handle screen savers and DPMS on the backend */
    dmxDPMSInit(dmxScreen);

    /* Create root window for screen */
    mask = CWBackPixel | CWEventMask | CWColormap | CWOverrideRedirect;
    attribs.background_pixel = dmxScreen->beBlackPixel;
    attribs.event_mask = (KeyPressMask
                          | KeyReleaseMask
                          | ButtonPressMask
                          | ButtonReleaseMask
                          | EnterWindowMask
                          | LeaveWindowMask
                          | PointerMotionMask
                          | KeymapStateMask | FocusChangeMask);
    attribs.colormap = dmxScreen->beDefColormaps[dmxScreen->beDefVisualIndex];
    attribs.override_redirect = True;

    dmxScreen->scrnWin =
        XCreateWindow(dmxScreen->beDisplay,
                      DefaultRootWindow(dmxScreen->beDisplay),
                      dmxScreen->scrnX,
                      dmxScreen->scrnY,
                      dmxScreen->scrnWidth,
                      dmxScreen->scrnHeight,
                      0,
                      pScreen->rootDepth,
                      InputOutput,
                      dmxScreen->beVisuals[dmxScreen->beDefVisualIndex].visual,
                      mask, &attribs);
    dmxPropertyWindow(dmxScreen);

    /*
     * This turns off the cursor by defining a cursor with no visible
     * components.
     */
    {
        char noCursorData[] = { 0, 0, 0, 0,
            0, 0, 0, 0
        };
        Pixmap pixmap;
        XColor color, tmp;

        pixmap = XCreateBitmapFromData(dmxScreen->beDisplay, dmxScreen->scrnWin,
                                       noCursorData, 8, 8);
        XAllocNamedColor(dmxScreen->beDisplay, dmxScreen->beDefColormaps[0],
                         "black", &color, &tmp);
        dmxScreen->noCursor = XCreatePixmapCursor(dmxScreen->beDisplay,
                                                  pixmap, pixmap,
                                                  &color, &color, 0, 0);
        XDefineCursor(dmxScreen->beDisplay, dmxScreen->scrnWin,
                      dmxScreen->noCursor);

        XFreePixmap(dmxScreen->beDisplay, pixmap);
    }

    XMapWindow(dmxScreen->beDisplay, dmxScreen->scrnWin);

    if (dmxShadowFB) {
        mask = (GCFunction | GCPlaneMask | GCClipMask);
        gcvals.function = GXcopy;
        gcvals.plane_mask = AllPlanes;
        gcvals.clip_mask = None;

        dmxScreen->shadowGC = XCreateGC(dmxScreen->beDisplay,
                                        dmxScreen->scrnWin, mask, &gcvals);

        dmxScreen->shadowFBImage =
            XCreateImage(dmxScreen->beDisplay,
                         dmxScreen->beVisuals[dmxScreen->beDefVisualIndex].
                         visual, dmxScreen->beDepth, ZPixmap, 0,
                         (char *) dmxScreen->shadow, dmxScreen->scrnWidth,
                         dmxScreen->scrnHeight, dmxScreen->beBPP,
                         PixmapBytePad(dmxScreen->scrnWidth, dmxScreen->beBPP));
    }
    else {
        /* Create default drawables (used during GC creation) */
        for (i = 0; i < dmxScreen->beNumPixmapFormats; i++)
            for (j = 0; j < dmxScreen->beNumDepths; j++)
                if ((dmxScreen->bePixmapFormats[i].depth == 1) ||
                    (dmxScreen->bePixmapFormats[i].depth ==
                     dmxScreen->beDepths[j])) {
                    dmxScreen->scrnDefDrawables[i] = (Drawable)
                        XCreatePixmap(dmxScreen->beDisplay, dmxScreen->scrnWin,
                                      1, 1,
                                      dmxScreen->bePixmapFormats[i].depth);
                    break;
                }
    }
}

/** Initialize screen number \a idx. */
Bool
dmxScreenInit(int idx, ScreenPtr pScreen, int argc, char *argv[])
{
    DMXScreenInfo *dmxScreen = &dmxScreens[idx];
    int i, j;

    if (!dixRegisterPrivateKey(&dmxScreenPrivateKeyRec, PRIVATE_SCREEN, 0))
        return FALSE;
    if (!dixRegisterPrivateKey(&dmxColormapPrivateKeyRec, PRIVATE_COLORMAP, 0))
        return FALSE;
    if (!dixRegisterPrivateKey(&dmxGlyphSetPrivateKeyRec, PRIVATE_GLYPHSET, 0))
        return FALSE;

    if (dmxGeneration != serverGeneration) {
        /* Allocate font private index */
        dmxFontPrivateIndex = AllocateFontPrivateIndex();
        if (dmxFontPrivateIndex == -1)
            return FALSE;

        dmxGeneration = serverGeneration;
    }

    if (dmxShadowFB) {
        dmxScreen->shadow = shadowAlloc(dmxScreen->scrnWidth,
                                        dmxScreen->scrnHeight,
                                        dmxScreen->beBPP);
    }
    else {
        if (!dmxInitGC(pScreen))
            return FALSE;
        if (!dmxInitWindow(pScreen))
            return FALSE;
        if (!dmxInitPixmap(pScreen))
            return FALSE;
    }

    /*
     * Initalise the visual types.  miSetVisualTypesAndMasks() requires
     * that all of the types for each depth be collected together.  It's
     * intended for slightly different usage to what we would like here.
     * Maybe a miAddVisualTypeAndMask() function will be added to make
     * things easier here.
     */
    for (i = 0; i < dmxScreen->beNumDepths; i++) {
        int depth;
        int visuals = 0;
        int bitsPerRgb = 0;
        int preferredClass = -1;
        Pixel redMask = 0;
        Pixel greenMask = 0;
        Pixel blueMask = 0;

        depth = dmxScreen->beDepths[i];
        for (j = 0; j < dmxScreen->beNumVisuals; j++) {
            XVisualInfo *vi;

            vi = &dmxScreen->beVisuals[j];
            if (vi->depth == depth) {
                /* Assume the masks are all the same. */
                visuals |= (1 << vi->class);
                bitsPerRgb = vi->bits_per_rgb;
                redMask = vi->red_mask;
                greenMask = vi->green_mask;
                blueMask = vi->blue_mask;
                if (j == dmxScreen->beDefVisualIndex) {
                    preferredClass = vi->class;
                }
            }
        }
        miSetVisualTypesAndMasks(depth, visuals, bitsPerRgb, preferredClass,
                                 redMask, greenMask, blueMask);
    }

    fbScreenInit(pScreen,
                 dmxShadowFB ? dmxScreen->shadow : NULL,
                 dmxScreen->scrnWidth,
                 dmxScreen->scrnHeight,
                 dmxScreen->beXDPI,
                 dmxScreen->beXDPI, dmxScreen->scrnWidth, dmxScreen->beBPP);
    (void) dmxPictureInit(pScreen, 0, 0);

    /* Not yet... */
    pScreen->GetWindowPixmap = NULL;
    pScreen->SetWindowPixmap = NULL;

    if (dmxShadowFB && !shadowInit(pScreen, dmxShadowUpdateProc, NULL))
        return FALSE;

    if (dmxShadowFB) {
        miDCInitialize(pScreen, &dmxPointerCursorFuncs);
    }
    else {
        MAXSCREENSALLOC(dmxCursorGeneration);
        if (dmxCursorGeneration[idx] != serverGeneration) {
            if (!(miPointerInitialize(pScreen,
                                      &dmxPointerSpriteFuncs,
                                      &dmxPointerCursorFuncs, FALSE)))
                return FALSE;

            dmxCursorGeneration[idx] = serverGeneration;
        }
    }

    DMX_WRAP(CloseScreen, dmxCloseScreen, dmxScreen, pScreen);
    DMX_WRAP(SaveScreen, dmxSaveScreen, dmxScreen, pScreen);

    dmxBEScreenInit(idx, pScreen);

    if (!dmxShadowFB) {
        /* Wrap GC functions */
        DMX_WRAP(CreateGC, dmxCreateGC, dmxScreen, pScreen);

        /* Wrap Window functions */
        DMX_WRAP(CreateWindow, dmxCreateWindow, dmxScreen, pScreen);
        DMX_WRAP(DestroyWindow, dmxDestroyWindow, dmxScreen, pScreen);
        DMX_WRAP(PositionWindow, dmxPositionWindow, dmxScreen, pScreen);
        DMX_WRAP(ChangeWindowAttributes, dmxChangeWindowAttributes, dmxScreen,
                 pScreen);
        DMX_WRAP(RealizeWindow, dmxRealizeWindow, dmxScreen, pScreen);
        DMX_WRAP(UnrealizeWindow, dmxUnrealizeWindow, dmxScreen, pScreen);
        DMX_WRAP(RestackWindow, dmxRestackWindow, dmxScreen, pScreen);
        DMX_WRAP(WindowExposures, dmxWindowExposures, dmxScreen, pScreen);
        DMX_WRAP(CopyWindow, dmxCopyWindow, dmxScreen, pScreen);

        DMX_WRAP(ResizeWindow, dmxResizeWindow, dmxScreen, pScreen);
        DMX_WRAP(ReparentWindow, dmxReparentWindow, dmxScreen, pScreen);

        DMX_WRAP(ChangeBorderWidth, dmxChangeBorderWidth, dmxScreen, pScreen);

        /* Wrap Image functions */
        DMX_WRAP(GetImage, dmxGetImage, dmxScreen, pScreen);
        DMX_WRAP(GetSpans, dmxGetSpans, dmxScreen, pScreen);

        /* Wrap Pixmap functions */
        DMX_WRAP(CreatePixmap, dmxCreatePixmap, dmxScreen, pScreen);
        DMX_WRAP(DestroyPixmap, dmxDestroyPixmap, dmxScreen, pScreen);
        DMX_WRAP(BitmapToRegion, dmxBitmapToRegion, dmxScreen, pScreen);

        /* Wrap Font functions */
        DMX_WRAP(RealizeFont, dmxRealizeFont, dmxScreen, pScreen);
        DMX_WRAP(UnrealizeFont, dmxUnrealizeFont, dmxScreen, pScreen);

        /* Wrap Colormap functions */
        DMX_WRAP(CreateColormap, dmxCreateColormap, dmxScreen, pScreen);
        DMX_WRAP(DestroyColormap, dmxDestroyColormap, dmxScreen, pScreen);
        DMX_WRAP(InstallColormap, dmxInstallColormap, dmxScreen, pScreen);
        DMX_WRAP(StoreColors, dmxStoreColors, dmxScreen, pScreen);

        /* Wrap Shape functions */
        DMX_WRAP(SetShape, dmxSetShape, dmxScreen, pScreen);
    }

    if (!dmxCreateDefColormap(pScreen))
        return FALSE;

    return TRUE;
}

/** Close the \a pScreen resources on the back-end server. */
void
dmxBECloseScreen(ScreenPtr pScreen)
{
    DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
    int i;

    /* Restore the back-end screen-saver and DPMS state. */
    dmxDPMSTerm(dmxScreen);

    /* Free the screen resources */

    XFreeCursor(dmxScreen->beDisplay, dmxScreen->noCursor);
    dmxScreen->noCursor = (Cursor) 0;

    XUnmapWindow(dmxScreen->beDisplay, dmxScreen->scrnWin);
    XDestroyWindow(dmxScreen->beDisplay, dmxScreen->scrnWin);
    dmxScreen->scrnWin = (Window) 0;

    if (dmxShadowFB) {
        /* Free the shadow GC and image assocated with the back-end server */
        XFreeGC(dmxScreen->beDisplay, dmxScreen->shadowGC);
        dmxScreen->shadowGC = NULL;
        XFree(dmxScreen->shadowFBImage);
        dmxScreen->shadowFBImage = NULL;
    }
    else {
        /* Free the default drawables */
        for (i = 0; i < dmxScreen->beNumPixmapFormats; i++) {
            if (dmxScreen->scrnDefDrawables[i]) {
                XFreePixmap(dmxScreen->beDisplay,
                            dmxScreen->scrnDefDrawables[i]);
                dmxScreen->scrnDefDrawables[i] = (Drawable) 0;
            }
        }
    }

    /* Free resources allocated during initialization (in dmxinit.c) */
    for (i = 0; i < dmxScreen->beNumDefColormaps; i++)
        XFreeColormap(dmxScreen->beDisplay, dmxScreen->beDefColormaps[i]);
    free(dmxScreen->beDefColormaps);
    dmxScreen->beDefColormaps = NULL;

#if 0
    /* Do not free visuals, depths and pixmap formats here.  Free them
     * in dmxCloseScreen() instead -- see comment below. */
    XFree(dmxScreen->beVisuals);
    dmxScreen->beVisuals = NULL;

    XFree(dmxScreen->beDepths);
    dmxScreen->beDepths = NULL;

    XFree(dmxScreen->bePixmapFormats);
    dmxScreen->bePixmapFormats = NULL;
#endif

#ifdef GLXEXT
    if (dmxScreen->glxVisuals) {
        XFree(dmxScreen->glxVisuals);
        dmxScreen->glxVisuals = NULL;
        dmxScreen->numGlxVisuals = 0;
    }
#endif

    /* Close display */
    XCloseDisplay(dmxScreen->beDisplay);
    dmxScreen->beDisplay = NULL;
}

/** Close screen number \a idx. */
Bool
dmxCloseScreen(int idx, ScreenPtr pScreen)
{
    DMXScreenInfo *dmxScreen = &dmxScreens[idx];

    /* Reset the proc vectors */
    if (idx == 0) {
        dmxResetRender();
        dmxResetFonts();
    }

    if (dmxShadowFB) {
        /* Free the shadow framebuffer */
        free(dmxScreen->shadow);
    }
    else {

        /* Unwrap Shape functions */
        DMX_UNWRAP(SetShape, dmxScreen, pScreen);

        /* Unwrap the pScreen functions */
        DMX_UNWRAP(CreateGC, dmxScreen, pScreen);

        DMX_UNWRAP(CreateWindow, dmxScreen, pScreen);
        DMX_UNWRAP(DestroyWindow, dmxScreen, pScreen);
        DMX_UNWRAP(PositionWindow, dmxScreen, pScreen);
        DMX_UNWRAP(ChangeWindowAttributes, dmxScreen, pScreen);
        DMX_UNWRAP(RealizeWindow, dmxScreen, pScreen);
        DMX_UNWRAP(UnrealizeWindow, dmxScreen, pScreen);
        DMX_UNWRAP(RestackWindow, dmxScreen, pScreen);
        DMX_UNWRAP(WindowExposures, dmxScreen, pScreen);
        DMX_UNWRAP(CopyWindow, dmxScreen, pScreen);

        DMX_UNWRAP(ResizeWindow, dmxScreen, pScreen);
        DMX_UNWRAP(ReparentWindow, dmxScreen, pScreen);

        DMX_UNWRAP(ChangeBorderWidth, dmxScreen, pScreen);

        DMX_UNWRAP(GetImage, dmxScreen, pScreen);
        DMX_UNWRAP(GetSpans, dmxScreen, pScreen);

        DMX_UNWRAP(CreatePixmap, dmxScreen, pScreen);
        DMX_UNWRAP(DestroyPixmap, dmxScreen, pScreen);
        DMX_UNWRAP(BitmapToRegion, dmxScreen, pScreen);

        DMX_UNWRAP(RealizeFont, dmxScreen, pScreen);
        DMX_UNWRAP(UnrealizeFont, dmxScreen, pScreen);

        DMX_UNWRAP(CreateColormap, dmxScreen, pScreen);
        DMX_UNWRAP(DestroyColormap, dmxScreen, pScreen);
        DMX_UNWRAP(InstallColormap, dmxScreen, pScreen);
        DMX_UNWRAP(StoreColors, dmxScreen, pScreen);
    }

    DMX_UNWRAP(SaveScreen, dmxScreen, pScreen);

    if (dmxScreen->beDisplay) {
        dmxBECloseScreen(pScreen);

#if 1
        /* Free visuals, depths and pixmap formats here so that they
         * won't be freed when a screen is detached, thereby allowing
         * the screen to be reattached to be compared to the one
         * previously removed.
         */
        XFree(dmxScreen->beVisuals);
        dmxScreen->beVisuals = NULL;

        XFree(dmxScreen->beDepths);
        dmxScreen->beDepths = NULL;

        XFree(dmxScreen->bePixmapFormats);
        dmxScreen->bePixmapFormats = NULL;
#endif
    }

    DMX_UNWRAP(CloseScreen, dmxScreen, pScreen);
    return pScreen->CloseScreen(idx, pScreen);
}

static Bool
dmxSaveScreen(ScreenPtr pScreen, int what)
{
    DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];

    if (dmxScreen->beDisplay) {
        switch (what) {
        case SCREEN_SAVER_OFF:
        case SCREEN_SAVER_FORCER:
            XResetScreenSaver(dmxScreen->beDisplay);
            dmxSync(dmxScreen, FALSE);
            break;
        case SCREEN_SAVER_ON:
        case SCREEN_SAVER_CYCLE:
            XActivateScreenSaver(dmxScreen->beDisplay);
            dmxSync(dmxScreen, FALSE);
            break;
        }
    }

    return TRUE;
}