105b261ecSmrg/***********************************************************
205b261ecSmrg
305b261ecSmrgCopyright 1987, 1998  The Open Group
405b261ecSmrg
505b261ecSmrgPermission to use, copy, modify, distribute, and sell this software and its
605b261ecSmrgdocumentation for any purpose is hereby granted without fee, provided that
705b261ecSmrgthe above copyright notice appear in all copies and that both that
805b261ecSmrgcopyright notice and this permission notice appear in supporting
905b261ecSmrgdocumentation.
1005b261ecSmrg
1105b261ecSmrgThe above copyright notice and this permission notice shall be included in
1205b261ecSmrgall copies or substantial portions of the Software.
1305b261ecSmrg
1405b261ecSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1505b261ecSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1605b261ecSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
1705b261ecSmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
1805b261ecSmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1905b261ecSmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2005b261ecSmrg
2105b261ecSmrgExcept as contained in this notice, the name of The Open Group shall not be
2205b261ecSmrgused in advertising or otherwise to promote the sale, use or other dealings
2305b261ecSmrgin this Software without prior written authorization from The Open Group.
2405b261ecSmrg
2505b261ecSmrgCopyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
2605b261ecSmrg
2705b261ecSmrg                        All Rights Reserved
2805b261ecSmrg
29f7df2e56SmrgPermission to use, copy, modify, and distribute this software and its
30f7df2e56Smrgdocumentation for any purpose and without fee is hereby granted,
3105b261ecSmrgprovided that the above copyright notice appear in all copies and that
32f7df2e56Smrgboth that copyright notice and this permission notice appear in
3305b261ecSmrgsupporting documentation, and that the name of Digital not be
3405b261ecSmrgused in advertising or publicity pertaining to distribution of the
35f7df2e56Smrgsoftware without specific, written prior permission.
3605b261ecSmrg
3705b261ecSmrgDIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
3805b261ecSmrgALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
3905b261ecSmrgDIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
4005b261ecSmrgANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
4105b261ecSmrgWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
4205b261ecSmrgARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
4305b261ecSmrgSOFTWARE.
4405b261ecSmrg
4505b261ecSmrg******************************************************************/
4605b261ecSmrg
47f7df2e56Smrg/*
48f7df2e56Smrg * Copyright (c) 2000 by The XFree86 Project, Inc.
49f7df2e56Smrg *
50f7df2e56Smrg * Permission is hereby granted, free of charge, to any person obtaining a
51f7df2e56Smrg * copy of this software and associated documentation files (the "Software"),
52f7df2e56Smrg * to deal in the Software without restriction, including without limitation
53f7df2e56Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
54f7df2e56Smrg * and/or sell copies of the Software, and to permit persons to whom the
55f7df2e56Smrg * Software is furnished to do so, subject to the following conditions:
56f7df2e56Smrg *
57f7df2e56Smrg * The above copyright notice and this permission notice shall be included in
58f7df2e56Smrg * all copies or substantial portions of the Software.
59f7df2e56Smrg *
60f7df2e56Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
61f7df2e56Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
62f7df2e56Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
63f7df2e56Smrg * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
64f7df2e56Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
65f7df2e56Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
66f7df2e56Smrg * OTHER DEALINGS IN THE SOFTWARE.
67f7df2e56Smrg *
68f7df2e56Smrg * Except as contained in this notice, the name of the copyright holder(s)
69f7df2e56Smrg * and author(s) shall not be used in advertising or otherwise to promote
70f7df2e56Smrg * the sale, use or other dealings in this Software without prior written
71f7df2e56Smrg * authorization from the copyright holder(s) and author(s).
72f7df2e56Smrg */
73f7df2e56Smrg
7405b261ecSmrg#ifdef HAVE_DIX_CONFIG_H
7505b261ecSmrg#include <dix-config.h>
7605b261ecSmrg#endif
7705b261ecSmrg
7805b261ecSmrg#ifdef HAVE_XORG_CONFIG_H
7905b261ecSmrg#include <xorg-config.h>
80f7df2e56Smrg#include "xf86Extensions.h"
8105b261ecSmrg#endif
8205b261ecSmrg
8305b261ecSmrg#ifdef HAVE_DMX_CONFIG_H
8405b261ecSmrg#include <dmx-config.h>
854202a189Smrg#undef XV
864202a189Smrg#undef DBE
874202a189Smrg#undef SCREENSAVER
884202a189Smrg#undef RANDR
894202a189Smrg#undef DAMAGE
904202a189Smrg#undef COMPOSITE
914202a189Smrg#undef MITSHM
9205b261ecSmrg#endif
9305b261ecSmrg
9405b261ecSmrg#ifdef HAVE_XNEST_CONFIG_H
9505b261ecSmrg#include <xnest-config.h>
964642e01fSmrg#undef COMPOSITE
9705b261ecSmrg#undef DPMSExtension
9805b261ecSmrg#endif
9905b261ecSmrg
10005b261ecSmrg#include "misc.h"
10105b261ecSmrg#include "extension.h"
102f7df2e56Smrg#include "extinit.h"
103f7df2e56Smrg#include "micmap.h"
1047e31ba66Smrg#include "os.h"
105f7df2e56Smrg#include "globals.h"
10605b261ecSmrg
1075a112b11Smrg#include "miinitext.h"
1085a112b11Smrg
1097e31ba66Smrg/* List of built-in (statically linked) extensions */
1107e31ba66Smrgstatic const ExtensionModule staticExtensions[] = {
1117e31ba66Smrg    {GEExtensionInit, "Generic Event Extension", &noGEExtension},
1127e31ba66Smrg    {ShapeExtensionInit, "SHAPE", NULL},
1137e31ba66Smrg#ifdef MITSHM
1147e31ba66Smrg    {ShmExtensionInit, "MIT-SHM", &noMITShmExtension},
11505b261ecSmrg#endif
1167e31ba66Smrg    {XInputExtensionInit, "XInputExtension", NULL},
1177e31ba66Smrg#ifdef XTEST
1187e31ba66Smrg    {XTestExtensionInit, "XTEST", &noTestExtensions},
11905b261ecSmrg#endif
1207e31ba66Smrg    {BigReqExtensionInit, "BIG-REQUESTS", NULL},
1217e31ba66Smrg    {SyncExtensionInit, "SYNC", NULL},
1227e31ba66Smrg    {XkbExtensionInit, "XKEYBOARD", NULL},
1237e31ba66Smrg    {XCMiscExtensionInit, "XC-MISC", NULL},
1247e31ba66Smrg#ifdef XCSECURITY
1257e31ba66Smrg    {SecurityExtensionInit, "SECURITY", &noSecurityExtension},
12605b261ecSmrg#endif
1277e31ba66Smrg#ifdef PANORAMIX
1287e31ba66Smrg    {PanoramiXExtensionInit, "XINERAMA", &noPanoramiXExtension},
12905b261ecSmrg#endif
1307e31ba66Smrg    /* must be before Render to layer DisplayCursor correctly */
1317e31ba66Smrg    {XFixesExtensionInit, "XFIXES", &noXFixesExtension},
1327e31ba66Smrg#ifdef XF86BIGFONT
1337e31ba66Smrg    {XFree86BigfontExtensionInit, "XFree86-Bigfont", &noXFree86BigfontExtension},
13405b261ecSmrg#endif
1357e31ba66Smrg    {RenderExtensionInit, "RENDER", &noRenderExtension},
13605b261ecSmrg#ifdef RANDR
1377e31ba66Smrg    {RRExtensionInit, "RANDR", &noRRExtension},
13805b261ecSmrg#endif
1397e31ba66Smrg#ifdef COMPOSITE
1407e31ba66Smrg    {CompositeExtensionInit, "COMPOSITE", &noCompositeExtension},
14105b261ecSmrg#endif
1427e31ba66Smrg#ifdef DAMAGE
1437e31ba66Smrg    {DamageExtensionInit, "DAMAGE", &noDamageExtension},
14405b261ecSmrg#endif
1457e31ba66Smrg#ifdef SCREENSAVER
1467e31ba66Smrg    {ScreenSaverExtensionInit, "MIT-SCREEN-SAVER", &noScreenSaverExtension},
14705b261ecSmrg#endif
1487e31ba66Smrg#ifdef DBE
1497e31ba66Smrg    {DbeExtensionInit, "DOUBLE-BUFFER", &noDbeExtension},
15005b261ecSmrg#endif
1517e31ba66Smrg#ifdef XRECORD
1527e31ba66Smrg    {RecordExtensionInit, "RECORD", &noTestExtensions},
15305b261ecSmrg#endif
1547e31ba66Smrg#ifdef DPMSExtension
1557e31ba66Smrg    {DPMSExtensionInit, "DPMS", &noDPMSExtension},
15605b261ecSmrg#endif
1577e31ba66Smrg#ifdef PRESENT
1587e31ba66Smrg    {present_extension_init, "Present", NULL},
15905b261ecSmrg#endif
1607e31ba66Smrg#ifdef DRI3
1617e31ba66Smrg    {dri3_extension_init, "DRI3", NULL},
16205b261ecSmrg#endif
1637e31ba66Smrg#ifdef RES
1647e31ba66Smrg    {ResExtensionInit, "X-Resource", &noResExtension},
16505b261ecSmrg#endif
16605b261ecSmrg#ifdef XV
1677e31ba66Smrg    {XvExtensionInit, "XVideo", &noXvExtension},
1687e31ba66Smrg    {XvMCExtensionInit, "XVideo-MotionCompensation", &noXvExtension},
1697e31ba66Smrg#endif
1707e31ba66Smrg#ifdef XSELINUX
1717e31ba66Smrg    {SELinuxExtensionInit, "SELinux", &noSELinuxExtension},
1727e31ba66Smrg#endif
1737e31ba66Smrg#ifdef GLXEXT
1747e31ba66Smrg    {GlxExtensionInit, "GLX", &noGlxExtension},
17505b261ecSmrg#endif
17605b261ecSmrg};
17705b261ecSmrg
1785a112b11Smrgvoid
1795a112b11SmrgListStaticExtensions(void)
1805a112b11Smrg{
1815a112b11Smrg    const ExtensionModule *ext;
1825a112b11Smrg    int i;
1835a112b11Smrg
1845a112b11Smrg    ErrorF(" Only the following extensions can be run-time enabled/disabled:\n");
1855a112b11Smrg    for (i = 0; i < ARRAY_SIZE(staticExtensions); i++) {
1865a112b11Smrg        ext = &staticExtensions[i];
1875a112b11Smrg        if (ext->disablePtr != NULL) {
1885a112b11Smrg            ErrorF("\t%s\n", ext->name);
1895a112b11Smrg        }
1905a112b11Smrg    }
1915a112b11Smrg}
1925a112b11Smrg
193f7df2e56SmrgBool
194f7df2e56SmrgEnableDisableExtension(const char *name, Bool enable)
19505b261ecSmrg{
1967e31ba66Smrg    const ExtensionModule *ext;
197f7df2e56Smrg    int i;
198f7df2e56Smrg
1997e31ba66Smrg    for (i = 0; i < ARRAY_SIZE(staticExtensions); i++) {
2007e31ba66Smrg        ext = &staticExtensions[i];
201bf8e669cSmrg        if (strcasecmp(name, ext->name) == 0) {
202f7df2e56Smrg            if (ext->disablePtr != NULL) {
203f7df2e56Smrg                *ext->disablePtr = !enable;
204f7df2e56Smrg                return TRUE;
205f7df2e56Smrg            }
206f7df2e56Smrg            else {
207f7df2e56Smrg                /* Extension is always on, impossible to disable */
208f7df2e56Smrg                return enable;  /* okay if they wanted to enable,
209f7df2e56Smrg                                   fail if they tried to disable */
210f7df2e56Smrg            }
211f7df2e56Smrg        }
21205b261ecSmrg    }
21305b261ecSmrg
21405b261ecSmrg    return FALSE;
21505b261ecSmrg}
21605b261ecSmrg
217f7df2e56Smrgvoid
218f7df2e56SmrgEnableDisableExtensionError(const char *name, Bool enable)
21905b261ecSmrg{
2207e31ba66Smrg    const ExtensionModule *ext;
221f7df2e56Smrg    int i;
2224202a189Smrg    Bool found = FALSE;
22305b261ecSmrg
2247e31ba66Smrg    for (i = 0; i < ARRAY_SIZE(staticExtensions); i++) {
2257e31ba66Smrg        ext = &staticExtensions[i];
226f7df2e56Smrg        if ((strcmp(name, ext->name) == 0) && (ext->disablePtr == NULL)) {
227f7df2e56Smrg            ErrorF("[mi] Extension \"%s\" can not be disabled\n", name);
228f7df2e56Smrg            found = TRUE;
229f7df2e56Smrg            break;
230f7df2e56Smrg        }
2314202a189Smrg    }
2325a112b11Smrg    if (found == FALSE) {
233f7df2e56Smrg        ErrorF("[mi] Extension \"%s\" is not recognized\n", name);
2345a112b11Smrg        /* Disabling a non-existing extension is a no-op anyway */
2355a112b11Smrg        if (enable == FALSE)
2365a112b11Smrg            return;
2374202a189Smrg    }
2385a112b11Smrg    ListStaticExtensions();
23905b261ecSmrg}
24005b261ecSmrg
241f7df2e56Smrgstatic ExtensionModule *ExtensionModuleList = NULL;
242f7df2e56Smrgstatic int numExtensionModules = 0;
243f7df2e56Smrg
244f7df2e56Smrgstatic void
245f7df2e56SmrgAddStaticExtensions(void)
246f7df2e56Smrg{
247f7df2e56Smrg    static Bool listInitialised = FALSE;
248f7df2e56Smrg
249f7df2e56Smrg    if (listInitialised)
250f7df2e56Smrg        return;
251f7df2e56Smrg    listInitialised = TRUE;
252f7df2e56Smrg
253f7df2e56Smrg    /* Add built-in extensions to the list. */
254f7df2e56Smrg    LoadExtensionList(staticExtensions, ARRAY_SIZE(staticExtensions), TRUE);
255f7df2e56Smrg}
256f7df2e56Smrg
25705b261ecSmrgvoid
2584642e01fSmrgInitExtensions(int argc, char *argv[])
25905b261ecSmrg{
26005b261ecSmrg    int i;
26105b261ecSmrg    ExtensionModule *ext;
26205b261ecSmrg
263f7df2e56Smrg    AddStaticExtensions();
26405b261ecSmrg
265f7df2e56Smrg    for (i = 0; i < numExtensionModules; i++) {
266f7df2e56Smrg        ext = &ExtensionModuleList[i];
267f7df2e56Smrg        if (ext->initFunc != NULL &&
268f7df2e56Smrg            (ext->disablePtr == NULL || !*ext->disablePtr)) {
2697e31ba66Smrg            LogMessageVerb(X_INFO, 3, "Initializing extension %s\n",
2707e31ba66Smrg                           ext->name);
2717e31ba66Smrg
272f7df2e56Smrg            (ext->initFunc) ();
273f7df2e56Smrg        }
27405b261ecSmrg    }
275f7df2e56Smrg}
27605b261ecSmrg
277f7df2e56Smrgstatic ExtensionModule *
278f7df2e56SmrgNewExtensionModuleList(int size)
279f7df2e56Smrg{
280f7df2e56Smrg    ExtensionModule *save = ExtensionModuleList;
281f7df2e56Smrg    int n;
282f7df2e56Smrg
283f7df2e56Smrg    /* Sanity check */
284f7df2e56Smrg    if (!ExtensionModuleList)
285f7df2e56Smrg        numExtensionModules = 0;
286f7df2e56Smrg
287f7df2e56Smrg    n = numExtensionModules + size;
288f7df2e56Smrg    ExtensionModuleList = reallocarray(ExtensionModuleList, n,
289f7df2e56Smrg                                       sizeof(ExtensionModule));
290f7df2e56Smrg    if (ExtensionModuleList == NULL) {
291f7df2e56Smrg        ExtensionModuleList = save;
292f7df2e56Smrg        return NULL;
293f7df2e56Smrg    }
294f7df2e56Smrg    else {
295f7df2e56Smrg        numExtensionModules += size;
296f7df2e56Smrg        return ExtensionModuleList + (numExtensionModules - size);
29705b261ecSmrg    }
29805b261ecSmrg}
29905b261ecSmrg
300f7df2e56Smrgvoid
301f7df2e56SmrgLoadExtensionList(const ExtensionModule ext[], int size, Bool builtin)
302f7df2e56Smrg{
303f7df2e56Smrg    ExtensionModule *newext;
304f7df2e56Smrg    int i;
305f7df2e56Smrg
306f7df2e56Smrg    /* Make sure built-in extensions get added to the list before those
307f7df2e56Smrg     * in modules. */
308f7df2e56Smrg    AddStaticExtensions();
309f7df2e56Smrg
310f7df2e56Smrg    if (!(newext = NewExtensionModuleList(size)))
311f7df2e56Smrg        return;
312f7df2e56Smrg
313f7df2e56Smrg    for (i = 0; i < size; i++, newext++) {
314f7df2e56Smrg        newext->name = ext[i].name;
315f7df2e56Smrg        newext->initFunc = ext[i].initFunc;
316f7df2e56Smrg        newext->disablePtr = ext[i].disablePtr;
317f7df2e56Smrg    }
318f7df2e56Smrg}
319