Simplify. Only have a single window creation function.

This commit is contained in:
Steve Checkoway
2006-12-07 10:51:17 +00:00
parent 4dca42c26b
commit a6016a9c03
2 changed files with 28 additions and 78 deletions
+24 -69
View File
@@ -1,17 +1,10 @@
#include "global.h" #include "global.h"
#include "X11Helper.h" #include "X11Helper.h"
#include <X11/Xlib.h>
#include "RageLog.h" #include "RageLog.h"
#include "RageDisplay.h"
#include "RageThreads.h"
// Currently open masks:
static vector<long> g_aiMasks;
// Number of subsystems using the X connection: // Number of subsystems using the X connection:
static int g_iRefCount = 0; static int g_iRefCount = 0;
static unsigned long g_iMask = 0L;
Display *X11Helper::Dpy = NULL; Display *X11Helper::Dpy = NULL;
Window X11Helper::Win = None; Window X11Helper::Win = None;
@@ -40,106 +33,68 @@ void X11Helper::Stop()
if( g_iRefCount == 0 ) if( g_iRefCount == 0 )
{ {
XCloseDisplay( Dpy ); // The window should have been shut down
DEBUG_ASSERT( Win == None );
Dpy = NULL; // For sanity's sake Dpy = NULL; // For sanity's sake
g_aiMasks.clear();
} }
} }
static void ApplyMasks(); void X11Helper::OpenMask( unsigned long mask )
void X11Helper::OpenMask( long mask )
{ {
g_aiMasks.push_back(mask); g_iMask |= mask;
ApplyMasks(); if( Dpy && Win )
XSelectInput( Dpy, Win, g_iMask );
} }
void X11Helper::CloseMask( long mask ) void X11Helper::CloseMask( unsigned long mask )
{ {
vector<long>::iterator i = find( g_aiMasks.begin(), g_aiMasks.end(), mask ); g_iMask &= ~mask;
if( i == g_aiMasks.end() ) if( Dpy && Win )
return; XSelectInput( Dpy, Win, g_iMask );
g_aiMasks.erase( i );
ApplyMasks();
} }
static void ApplyMasks() bool X11Helper::MakeWindow( Window &win, int screenNum, int depth, Visual *visual, int width, int height, bool overrideRedirect )
{
if( X11Helper::Dpy == NULL || X11Helper::Win == None )
return;
LOG->Trace( "X11Helper: Reapplying event masks." );
long iMask = 0;
for( unsigned i = 0; i < g_aiMasks.size(); ++i )
iMask |= g_aiMasks[i];
XSelectInput( X11Helper::Dpy, X11Helper::Win, iMask );
}
bool X11Helper::MakeWindow( int screenNum, int depth, Visual *visual, int width, int height, bool overrideRedirect )
{ {
if( g_iRefCount == 0 ) if( g_iRefCount == 0 )
return false; return false;
if( Win ) if( win )
{ XDestroyWindow( Dpy, win );
XDestroyWindow( Dpy, Win );
Win = None;
}
Win = CreateWindow( screenNum, depth, visual, width, height, overrideRedirect );
return true;
}
Window X11Helper::CreateWindow( int screenNum, int depth, Visual *visual, int width, int height, bool overrideRedirect )
{
vector<long>::iterator i;
XSetWindowAttributes winAttribs; XSetWindowAttributes winAttribs;
winAttribs.border_pixel = 0; winAttribs.border_pixel = 0;
winAttribs.event_mask = 0; winAttribs.event_mask = g_iMask;
i = g_aiMasks.begin();
while( i != g_aiMasks.end() )
{
winAttribs.event_mask |= *i;
i++;
}
// XXX: Error catching/handling? // XXX: Error catching/handling?
winAttribs.colormap = XCreateColormap( Dpy, RootWindow(Dpy, screenNum), visual, AllocNone ); winAttribs.colormap = XCreateColormap( Dpy, RootWindow(Dpy, screenNum), visual, AllocNone );
unsigned long mask = CWBorderPixel | CWColormap | CWEventMask;
Window Win;
if( overrideRedirect ) if( overrideRedirect )
{ {
winAttribs.override_redirect = True; winAttribs.override_redirect = True;
Win = XCreateWindow( Dpy, RootWindow(Dpy, screenNum), 0, 0, width, mask |= CWOverrideRedirect;
height, 0, depth, InputOutput, visual,
CWBorderPixel | CWColormap | CWEventMask | CWOverrideRedirect, &winAttribs );
}
else
{
Win = XCreateWindow( Dpy, RootWindow(Dpy, screenNum), 0, 0, width,
height, 0, depth, InputOutput, visual,
CWBorderPixel | CWColormap | CWEventMask, &winAttribs );
} }
win = XCreateWindow( Dpy, RootWindow(Dpy, screenNum), 0, 0, width, height, 0,
depth, InputOutput, visual, mask, &winAttribs );
if( win == None )
return false;
/* Hide the mouse cursor. */ /* Hide the mouse cursor. */
{ {
const char pBlank[] = { 0,0,0,0,0,0,0,0 }; const char pBlank[] = { 0,0,0,0,0,0,0,0 };
Pixmap BlankBitmap = XCreateBitmapFromData( Dpy, Win, pBlank, 8, 8 ); Pixmap BlankBitmap = XCreateBitmapFromData( Dpy, win, pBlank, 8, 8 );
XColor black = { 0, 0, 0, 0, 0, 0 }; XColor black = { 0, 0, 0, 0, 0, 0 };
Cursor pBlankPointer = XCreatePixmapCursor( Dpy, BlankBitmap, BlankBitmap, &black, &black, 0, 0 ); Cursor pBlankPointer = XCreatePixmapCursor( Dpy, BlankBitmap, BlankBitmap, &black, &black, 0, 0 );
XFreePixmap( Dpy, BlankBitmap ); XFreePixmap( Dpy, BlankBitmap );
XDefineCursor( Dpy, Win, pBlankPointer ); XDefineCursor( Dpy, win, pBlankPointer );
XFreeCursor( Dpy, pBlankPointer ); XFreeCursor( Dpy, pBlankPointer );
} }
return Win; return true;
} }
int ErrorCallback( Display *d, XErrorEvent *err ) int ErrorCallback( Display *d, XErrorEvent *err )
+4 -9
View File
@@ -3,9 +3,6 @@
#define X11_HELPER_H #define X11_HELPER_H
#include <X11/Xlib.h> // Window #include <X11/Xlib.h> // Window
#include "RageDisplay.h" // RageDisplay
namespace X11Helper namespace X11Helper
{ {
// All functions in here that return a bool return true on success, and // All functions in here that return a bool return true on success, and
@@ -31,18 +28,16 @@ namespace X11Helper
// this visual type, this width (optional -- you can resize the window // this visual type, this width (optional -- you can resize the window
// in your callback later), and this height (optional). // in your callback later), and this height (optional).
// Also, whether to enable override redirect on the window. // Also, whether to enable override redirect on the window.
bool MakeWindow(int screenNum, int depth, Visual *visual, bool MakeWindow( Window &win, int screenNum, int depth, Visual *visual,
int width=32, int height=32, bool overrideRedirect=false); int width, int height, bool overrideRedirect );
Window CreateWindow( int screenNum, int depth, Visual *visual, int width, int height, bool overrideRedirect );
// Unmask one X event type mask thingy (XSelectInput() arg 3) on the // Unmask one X event type mask thingy (XSelectInput() arg 3) on the
// current window. Masked/unmasked events will carry between windows. // current window. Masked/unmasked events will carry between windows.
void OpenMask( long mask ); void OpenMask( unsigned long mask );
// (Re)mask one X event type mask thingy (XSelectInput() arg 3) on the // (Re)mask one X event type mask thingy (XSelectInput() arg 3) on the
// current window. Masked/unmasked events will carry between windows. // current window. Masked/unmasked events will carry between windows.
void CloseMask( long mask ); void CloseMask( unsigned long mask );
}; };
#endif #endif