diff --git a/common/context_x11.c b/common/context_x11.c index 1be48ac..ad2fc44 100644 --- a/common/context_x11.c +++ b/common/context_x11.c @@ -45,12 +45,17 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include #include +#include +#include static void (*event_handlers[LASTEvent])(XEvent *); qboolean oktodraw = false; int x_shmeventtype; +Display *x_disp = NULL; +static int x_disp_ref_count = 0; -qboolean x11_add_event(int event, void (*event_handler)(XEvent *)) +qboolean +x11_add_event(int event, void (*event_handler)(XEvent *)) { if (event >= LASTEvent) { printf("event: %d, LASTEvent: %d\n", event, LASTEvent); @@ -63,7 +68,8 @@ qboolean x11_add_event(int event, void (*event_handler)(XEvent *)) return true; } -qboolean x11_del_event(int event, void (*event_handler)(XEvent *)) +qboolean +x11_del_event(int event, void (*event_handler)(XEvent *)) { if (event >= LASTEvent) return false; @@ -74,7 +80,8 @@ qboolean x11_del_event(int event, void (*event_handler)(XEvent *)) return true; } -void x11_process_event(void) +void +x11_process_event(void) { XEvent x_event; @@ -89,10 +96,55 @@ void x11_process_event(void) event_handlers[x_event.type](&x_event); } -void x11_process_events(void) +void +x11_process_events(void) { /* Get events from X server. */ while (XPending(x_disp)) { x11_process_event(); } } + +// ======================================================================== +// Tragic death handler +// ======================================================================== + +static void TragicDeath(int signal_num) +{ + //XCloseDisplay(x_disp); + VID_Shutdown(); + Sys_Error("This death brought to you by the number %d\n", signal_num); +} + +void +x11_open_display(void) +{ + struct sigaction sa; + + if (!x_disp) { + x_disp = XOpenDisplay(0); + if (!x_disp) { + Sys_Error("VID: Could not open display [%s]\n", XDisplayName(0)); + } + + // catch signals + sigaction(SIGINT, 0, &sa); + sa.sa_handler = TragicDeath; + sigaction(SIGINT, &sa, 0); + sigaction(SIGTERM, &sa, 0); + + // for debugging only + XSynchronize(x_disp, True); + } else { + x_disp_ref_count++; + } +} + +void +x11_close_display(void) +{ + if (!--x_disp_ref_count) { + XCloseDisplay(x_disp); + x_disp = NULL; + } +} diff --git a/common/context_x11.h b/common/context_x11.h index b7d44df..7d698b6 100644 --- a/common/context_x11.h +++ b/common/context_x11.h @@ -35,9 +35,11 @@ extern qboolean doShm; extern int x_shmeventtype; extern qboolean oktodraw; -extern qboolean x11_add_event(int event, void (*event_handler)(XEvent *)); -extern qboolean x11_del_event(int event, void (*event_handler)(XEvent *)); -extern void x11_process_event(void); -extern void x11_process_events(void); +qboolean x11_add_event(int event, void (*event_handler)(XEvent *)); +qboolean x11_del_event(int event, void (*event_handler)(XEvent *)); +void x11_process_event(void); +void x11_process_events(void); +void x11_open_display(void); +void x11_close_display(void); #endif // __CONTEXT_X11_H__ diff --git a/common/gl_vidglx.c b/common/gl_vidglx.c index 21ec9fe..4fc00e2 100644 --- a/common/gl_vidglx.c +++ b/common/gl_vidglx.c @@ -31,6 +31,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include #include #include +#include #ifndef _EXPERIMENTAL_ # undef HAS_DGA @@ -70,7 +71,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define WARP_WIDTH 320 #define WARP_HEIGHT 200 -Display *x_disp = NULL; static int screen; Window x_win; static GLXContext ctx = NULL; @@ -145,10 +145,7 @@ VID_Shutdown(void) { Con_Printf("VID_Shutdown\n"); - if (x_disp && ctx) { - glXDestroyContext(x_disp, ctx); - ctx = NULL; - } + glXDestroyContext(x_disp, ctx); #ifdef HAS_DGA if (hasvidmode) { int i; @@ -165,11 +162,7 @@ VID_Shutdown(void) dlhand = NULL; } #endif - - if (x_disp) { - XCloseDisplay(x_disp); - x_disp = NULL; - } + x11_close_display(); } @@ -450,11 +443,7 @@ void VID_Init(unsigned char *palette) if (vid.conheight < 200) vid.conheight = 200; - x_disp = XOpenDisplay(NULL); - if ( !x_disp ) { - fprintf(stderr, "Error couldn't open the X display\n"); - exit(1); - } + x11_open_display(); screen = DefaultScreen(x_disp); root = RootWindow(x_disp, screen); diff --git a/common/in_x11.c b/common/in_x11.c index 22cc917..bf737a8 100644 --- a/common/in_x11.c +++ b/common/in_x11.c @@ -94,11 +94,8 @@ void IN_Shutdown(void) { Con_Printf("IN_Shutdown\n"); mouse_avail = 0; - if (x_disp) { - XAutoRepeatOn(x_disp); - XCloseDisplay(x_disp); - x_disp = 0; - } + XAutoRepeatOn(x_disp); + x11_close_display(); } @@ -378,6 +375,7 @@ int IN_Init () if (!x_win) Sys_Error("IN: No window!!\n"); + x11_open_display(); // call to increment the reference counter { int attribmask = CWEventMask; XWindowAttributes attribs_1; diff --git a/common/vid_x11.c b/common/vid_x11.c index a7f1802..6c007be 100644 --- a/common/vid_x11.c +++ b/common/vid_x11.c @@ -65,7 +65,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. viddef_t vid; // global video state unsigned short d_8to16table[256]; -Display *x_disp = NULL; Window x_win; static Colormap x_cmap; static GC x_gc; @@ -287,16 +286,6 @@ void VID_Gamma_f (void) } } -// ======================================================================== -// Tragic death handler -// ======================================================================== - -static void TragicDeath(int signal_num) -{ - XCloseDisplay(x_disp); - Sys_Error("This death brought to you by the number %d\n", signal_num); -} - // ======================================================================== // makes a null cursor // ======================================================================== @@ -503,27 +492,7 @@ void VID_Init (unsigned char *palette) verbose=COM_CheckParm("-verbose"); // open the display - x_disp = XOpenDisplay(0); - if (!x_disp) - { - if (getenv("DISPLAY")) - Sys_Error("VID: Could not open display [%s]\n", - getenv("DISPLAY")); - else - Sys_Error("VID: Could not open local display\n"); - } - -// catch signals - { - struct sigaction sa; - sigaction(SIGINT, 0, &sa); - sa.sa_handler = TragicDeath; - sigaction(SIGINT, &sa, 0); - sigaction(SIGTERM, &sa, 0); - } - -// for debugging only - XSynchronize(x_disp, True); + x11_open_display(); // check for command-line window size if ((pnum=COM_CheckParm("-winsize"))) @@ -746,7 +715,7 @@ VID_Shutdown(void) Sys_Printf("VID_Shutdown\n"); if (x_disp) { XAutoRepeatOn(x_disp); - XCloseDisplay(x_disp); + x11_close_display(); x_disp = 0; } }