1/*
2 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Copyright © 2003 Keith Packard
24 *
25 * Permission to use, copy, modify, distribute, and sell this software and its
26 * documentation for any purpose is hereby granted without fee, provided that
27 * the above copyright notice appear in all copies and that both that
28 * copyright notice and this permission notice appear in supporting
29 * documentation, and that the name of Keith Packard not be used in
30 * advertising or publicity pertaining to distribution of the software without
31 * specific, written prior permission.  Keith Packard makes no
32 * representations about the suitability of this software for any purpose.  It
33 * is provided "as is" without express or implied warranty.
34 *
35 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
36 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
37 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
38 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
39 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
40 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
41 * PERFORMANCE OF THIS SOFTWARE.
42 */
43
44#ifdef HAVE_DIX_CONFIG_H
45#include <dix-config.h>
46#endif
47
48#include "compint.h"
49#include "xace.h"
50
51#ifdef PANORAMIX
52#include "panoramiXsrv.h"
53#endif
54
55/*
56 * Delete the given overlay client list element from its screen list.
57 */
58void
59compFreeOverlayClient(CompOverlayClientPtr pOcToDel)
60{
61    ScreenPtr pScreen = pOcToDel->pScreen;
62    CompScreenPtr cs = GetCompScreen(pScreen);
63    CompOverlayClientPtr *pPrev, pOc;
64
65    for (pPrev = &cs->pOverlayClients; (pOc = *pPrev); pPrev = &pOc->pNext) {
66        if (pOc == pOcToDel) {
67            *pPrev = pOc->pNext;
68            free(pOc);
69            break;
70        }
71    }
72
73    /* Destroy overlay window when there are no more clients using it */
74    if (cs->pOverlayClients == NULL)
75        compDestroyOverlayWindow(pScreen);
76}
77
78/*
79 * Return the client's first overlay client rec from the given screen
80 */
81CompOverlayClientPtr
82compFindOverlayClient(ScreenPtr pScreen, ClientPtr pClient)
83{
84    CompScreenPtr cs = GetCompScreen(pScreen);
85    CompOverlayClientPtr pOc;
86
87    for (pOc = cs->pOverlayClients; pOc != NULL; pOc = pOc->pNext)
88        if (pOc->pClient == pClient)
89            return pOc;
90
91    return NULL;
92}
93
94/*
95 * Create an overlay client object for the given client
96 */
97CompOverlayClientPtr
98compCreateOverlayClient(ScreenPtr pScreen, ClientPtr pClient)
99{
100    CompScreenPtr cs = GetCompScreen(pScreen);
101    CompOverlayClientPtr pOc;
102
103    pOc = (CompOverlayClientPtr) malloc(sizeof(CompOverlayClientRec));
104    if (pOc == NULL)
105        return NULL;
106
107    pOc->pClient = pClient;
108    pOc->pScreen = pScreen;
109    pOc->resource = FakeClientID(pClient->index);
110    pOc->pNext = cs->pOverlayClients;
111    cs->pOverlayClients = pOc;
112
113    /*
114     * Create a resource for this element so it can be deleted
115     * when the client goes away.
116     */
117    if (!AddResource(pOc->resource, CompositeClientOverlayType, (void *) pOc))
118        return NULL;
119
120    return pOc;
121}
122
123/*
124 * Create the overlay window and map it
125 */
126Bool
127compCreateOverlayWindow(ScreenPtr pScreen)
128{
129    CompScreenPtr cs = GetCompScreen(pScreen);
130    WindowPtr pRoot = pScreen->root;
131    WindowPtr pWin;
132    XID attrs[] = { None, TRUE };       /* backPixmap, overrideRedirect */
133    int result;
134    int w = pScreen->width;
135    int h = pScreen->height;
136    int x = 0, y = 0;
137
138#ifdef PANORAMIX
139    if (!noPanoramiXExtension) {
140        x = -pScreen->x;
141        y = -pScreen->y;
142        w = PanoramiXPixWidth;
143        h = PanoramiXPixHeight;
144    }
145#endif
146
147    pWin = cs->pOverlayWin =
148        CreateWindow(cs->overlayWid, pRoot, x, y, w, h, 0,
149                     InputOutput, CWBackPixmap | CWOverrideRedirect, &attrs[0],
150                     pRoot->drawable.depth,
151                     serverClient, pScreen->rootVisual, &result);
152    if (pWin == NULL)
153        return FALSE;
154
155    if (!AddResource(pWin->drawable.id, RT_WINDOW, (void *) pWin))
156        return FALSE;
157
158    MapWindow(pWin, serverClient);
159
160    return TRUE;
161}
162
163/*
164 * Destroy the overlay window
165 */
166void
167compDestroyOverlayWindow(ScreenPtr pScreen)
168{
169    CompScreenPtr cs = GetCompScreen(pScreen);
170
171    cs->pOverlayWin = NullWindow;
172    FreeResource(cs->overlayWid, RT_NONE);
173}
174