nsGLPbufferGLX::~nsGLPbufferGLX()
{
    MakeContextCurrent();
#ifndef GL_FRAMEBUFFER
#define GL_FRAMEBUFFER 0x8D40
#endif
    // workaround for segfault on glXDestroyContext
    mGLWrap.fBindFramebuffer(GL_FRAMEBUFFER, 0);

    Destroy();

    if (mPbuffer)
        gGLXWrap.fDestroyPbuffer(mDisplay, mPbuffer);
    if (mPbufferContext)
        gGLXWrap.fDestroyContext(mDisplay, mPbufferContext);
#if !(defined(MOZ_WIDGET_GTK2) && defined(MOZ_X11))
    if (mDisplay)
        XCloseDisplay(mDisplay);
#endif

    gActiveBuffers--;
    fprintf (stderr, "nsGLPbufferGLX: gActiveBuffers: %d\n", gActiveBuffers);
    fflush (stderr);
}
nsresult nsSystemInfo::Init()
{
    struct pci_access *pacc;
    struct pci_dev *p;
    pciaddr_t ram = 0;
    char buf[128];
    int i, found, v, n;
    Display *dpy;
    char *display = NULL;
    Window root;
    XVisualInfo *info, templ;
    XWindowAttributes wts;
    XPixmapFormatValues *pf;
    XSetWindowAttributes attr;
    Window win;

    mWidth = 0;
    mHeight = 0;
    mDepth = 0;

    pacc = pci_alloc();
    pci_init(pacc);

    pci_scan_bus(pacc);
    for (p = pacc->devices; p; p=p->next) {
        pci_fill_info(p,
                      PCI_FILL_IDENT | PCI_FILL_CLASS | PCI_FILL_BASES | PCI_FILL_SIZES);
        if (p->device_class == PCI_CLASS_DISPLAY_VGA) {
            pci_lookup_name(pacc, buf, sizeof(buf),
                            PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
                            p->vendor_id, p->device_id);
            mDeviceName.AssignLiteral(buf);
            sprintf(buf, "0x%04X", p->vendor_id);
            mVendorID.AssignLiteral(buf);
            sprintf(buf, "0x%04X", p->device_id);
            mDeviceID.AssignLiteral(buf);
            for (i=0; i<6; i++) {
                pciaddr_t len = (p->known_fields & PCI_FILL_SIZES) ? p->size[i] : 0;
                if (len > ram) ram = len;
            }
            vram = ram / 1024 / 1024;
        }
        else {
            Log("Grafx Bot: No PCI VGA device found");
        }
    }

    pci_cleanup(pacc);

    if (NULL != (display = getenv("DISPLAY"))) {
        if (display[0] != ':') {
            display = strchr(display, ':');
            if (NULL == display) {
                Log("Grafx Bot: unable to find display");
                return NS_OK;
            }
        }
        if (NULL == (dpy = XOpenDisplay(display))) {
            Log("Grafx Bot: unable to find X display");
            return NS_OK;
        }
        root = DefaultRootWindow(dpy);
        XGetWindowAttributes(dpy, root, &wts);
        mWidth = wts.width;
        mHeight = wts.height;

        templ.screen = XDefaultScreen(dpy);
        info = XGetVisualInfo(dpy, VisualScreenMask, &templ, &found);
        v = -1;
        for (i = 0; v == -1 && i < found; i++) {
            if (info[i].depth >= 15)
                v = i;
        }
        for (i = 0; v == -1 && i < found; i++) {
            if (info[i].depth == 8)
                v = i;
        }
        if (-1 == v) {
            Log("Grafx Bot: can't find visual");
            return NS_OK;
        }

        pf = XListPixmapFormats(dpy, &n);
        for (i = 0; i < n; i++) {
            if (pf[i].depth == info[v].depth) {
                mDepth = pf[i].depth;
            }
        }

        if (gGLXWrap.OpenLibrary("libGL.so.1") && gGLXWrap.Init()) {
            attr.background_pixel = 0;
            attr.border_pixel = 0;
            attr.colormap  = XCreateColormap(dpy, root, info[v].visual, AllocNone);
            attr.event_mask = StructureNotifyMask | ExposureMask;
            win = XCreateWindow(dpy, root, 0, 0, 100, 100, 0, info[v].depth,
                                InputOutput, info[v].visual,
                                CWBackPixel | CWBorderPixel | CWColormap | CWEventMask, &attr);
            GLXContext ctx = gGLXWrap.fCreateContext(dpy, info, NULL, true);
            if (ctx) {
                if (gGLXWrap.fMakeCurrent(dpy, win, ctx)) {
                    mDriverVersion.AssignLiteral((char*)gGLXWrap.fGetString(LOCAL_GL_VERSION));
                    mDriver.AssignLiteral((char*)gGLXWrap.fGetString(LOCAL_GL_RENDERER));
                    mDriver.AppendLiteral(" (");
                    mDriver.AppendLiteral((char*)gGLXWrap.fGetString(LOCAL_GL_VENDOR));
                    mDriver.AppendLiteral(")");
                }
                else {
                    Log("Grafx Bot: unable to make current");
                }
                gGLXWrap.fDestroyContext(dpy, ctx);
            }
            else {
                Log("Grafx Bot: unable to create context");
            }
            XDestroyWindow(dpy, win);
        }
        else {
            Log("Grafx Bot: can't init libGL.so.1");
        }
    }

    return NS_OK;
}