Skip to content

Commit

Permalink
remove support for X11 shared memory.
Browse files Browse the repository at this point in the history
This simplifies the code and reduces dependencies.
We need to rewrite the video driver anyway, so simplifying it first is good.

Still compiles and runs as standalone C and wrapped from rust.
  • Loading branch information
diekmann committed Mar 4, 2021
1 parent 0d6534e commit 8ac4030
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 213 deletions.
1 change: 0 additions & 1 deletion doom/build.rs
Expand Up @@ -5,7 +5,6 @@ fn main() {


// libraries which should be removed before going to wasm
println!("cargo:rustc-link-lib=Xext");
println!("cargo:rustc-link-lib=X11");
println!("cargo:rustc-link-lib=nsl");
}
2 changes: 1 addition & 1 deletion doom/linuxdoom-1.10/Makefile
Expand Up @@ -13,7 +13,7 @@ AR=llvm-ar-10
# apt install gcc-multilib libx11-dev:i386 libxext-dev:i386
CFLAGS=-m32 -O2 -Wall -Wno-unused-const-variable -DNORMALUNIX -DLINUX
LDFLAGS=-L/usr/X11R6/lib
LIBS=-lXext -lX11 -lnsl
LIBS=-lX11 -lnsl

# subdirectory for objects
O=linux
Expand Down
211 changes: 0 additions & 211 deletions doom/linuxdoom-1.10/i_video.c
Expand Up @@ -27,19 +27,11 @@ rcsid[] = "$Id: i_x.c,v 1.6 1997/02/03 22:45:10 b1 Exp $";
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>

#include <X11/extensions/XShm.h>
// Had to dig up XShm.c for this one.
// It is in the libXext, but not in the XFree86 headers.
#ifdef LINUX
int XShmGetEventBase( Display* dpy ); // problems with g++?
#endif

#include <stdarg.h>
#include <sys/time.h>
#include <sys/types.h>
Expand Down Expand Up @@ -71,12 +63,6 @@ XImage* image;
int X_width;
int X_height;

// MIT SHared Memory extension.
boolean doShm;

XShmSegmentInfo X_shminfo;
int X_shmeventtype;

// Fake mouse handling.
// This cannot work properly w/o DGA.
// Needs an invisible mouse cursor at least.
Expand Down Expand Up @@ -163,14 +149,6 @@ int xlatekey(void)

void I_ShutdownGraphics(void)
{
// Detach from X server
if (!XShmDetach(X_display, &X_shminfo))
I_Error("XShmDetach() failed in I_ShutdownGraphics()");

// Release shared memory.
shmdt(X_shminfo.shmaddr);
shmctl(X_shminfo.shmid, IPC_RMID, 0);

// Paranoia.
if (image){
image->data = NULL;
Expand All @@ -191,7 +169,6 @@ void I_StartFrame (void)
static int lastmousex = 0;
static int lastmousey = 0;
boolean mousemoved = false;
boolean shmFinished;

void I_GetEvent(void)
{
Expand Down Expand Up @@ -274,7 +251,6 @@ void I_GetEvent(void)
break;

default:
if (doShm && X_event.type == X_shmeventtype) shmFinished = true;
break;
}

Expand Down Expand Up @@ -482,28 +458,6 @@ void I_FinishUpdate (void)
Expand4 ((unsigned *)(screens[0]), (double *) (image->data));
}

if (doShm)
{

if (!XShmPutImage( X_display,
X_mainWindow,
X_gc,
image,
0, 0,
0, 0,
X_width, X_height,
True ))
I_Error("XShmPutImage() failed\n");

// wait for it to finish and processes all input events
shmFinished = false;
do
{
I_GetEvent();
} while (!shmFinished);

}
else
{

// draw the image
Expand Down Expand Up @@ -587,110 +541,6 @@ void I_SetPalette (byte* palette)
}


//
// This function is probably redundant,
// if XShmDetach works properly.
// ddt never detached the XShm memory,
// thus there might have been stale
// handles accumulating.
//
void grabsharedmemory(int size)
{

int key = ('d'<<24) | ('o'<<16) | ('o'<<8) | 'm';
struct shmid_ds shminfo;
int minsize = 320*200;
int id;
int rc;
// UNUSED int done=0;
int pollution=5;

// try to use what was here before
do
{
id = shmget((key_t) key, minsize, 0777); // just get the id
if (id != -1)
{
rc=shmctl(id, IPC_STAT, &shminfo); // get stats on it
if (!rc)
{
if (shminfo.shm_nattch)
{
fprintf(stderr, "User %d appears to be running "
"DOOM. Is that wise?\n", shminfo.shm_cpid);
key++;
}
else
{
if (getuid() == shminfo.shm_perm.cuid)
{
rc = shmctl(id, IPC_RMID, 0);
if (!rc)
fprintf(stderr,
"Was able to kill my old shared memory\n");
else
I_Error("Was NOT able to kill my old shared memory");

id = shmget((key_t)key, size, IPC_CREAT|0777);
if (id==-1)
I_Error("Could not get shared memory");

rc=shmctl(id, IPC_STAT, &shminfo);

break;

}
if (size >= shminfo.shm_segsz)
{
fprintf(stderr,
"will use %d's stale shared memory\n",
shminfo.shm_cpid);
break;
}
else
{
fprintf(stderr,
"warning: can't use stale "
"shared memory belonging to id %d, "
"key=0x%x\n",
shminfo.shm_cpid, key);
key++;
}
}
}
else
{
I_Error("could not get stats on key=%d", key);
}
}
else
{
id = shmget((key_t)key, size, IPC_CREAT|0777);
if (id==-1)
{
extern int errno;
fprintf(stderr, "errno=%d\n", errno);
I_Error("Could not get any shared memory");
}
break;
}
} while (--pollution);

if (!pollution)
{
I_Error("Sorry, system too polluted with stale "
"shared memory segments.\n");
}

X_shminfo.shmid = id;

// attach to the shared memory segment
image->data = X_shminfo.shmaddr = shmat(id, 0, 0);

fprintf(stderr, "shared memory id=%d, addr=0x%x\n", id,
(int) (image->data));
}

void I_InitGraphics(void)
{

Expand Down Expand Up @@ -774,23 +624,6 @@ void I_InitGraphics(void)
I_Error("xdoom currently only supports 256-color PseudoColor screens");
X_visual = X_visualinfo.visual;

// check for the MITSHM extension
doShm = XShmQueryExtension(X_display);

// even if it's available, make sure it's a local connection
if (doShm)
{
if (!displayname) displayname = (char *) getenv("DISPLAY");
if (displayname)
{
d = displayname;
while (*d && (*d != ':')) d++;
if (*d) *d = 0;
if (!(!strcasecmp(displayname, "unix") || !*displayname)) doShm = false;
}
}

fprintf(stderr, "Using MITSHM extension\n");

// create the colormap
X_cmap = XCreateColormap(X_display, RootWindow(X_display,
Expand Down Expand Up @@ -853,50 +686,6 @@ void I_InitGraphics(void)
GrabModeAsync, GrabModeAsync,
X_mainWindow, None, CurrentTime);

if (doShm)
{

X_shmeventtype = XShmGetEventBase(X_display) + ShmCompletion;

// create the image
image = XShmCreateImage( X_display,
X_visual,
8,
ZPixmap,
0,
&X_shminfo,
X_width,
X_height );

grabsharedmemory(image->bytes_per_line * image->height);


// UNUSED
// create the shared memory segment
// X_shminfo.shmid = shmget (IPC_PRIVATE,
// image->bytes_per_line * image->height, IPC_CREAT | 0777);
// if (X_shminfo.shmid < 0)
// {
// perror("");
// I_Error("shmget() failed in InitGraphics()");
// }
// fprintf(stderr, "shared memory id=%d\n", X_shminfo.shmid);
// attach to the shared memory segment
// image->data = X_shminfo.shmaddr = shmat(X_shminfo.shmid, 0, 0);


if (!image->data)
{
perror("");
I_Error("shmat() failed in InitGraphics()");
}

// get the X server to attach to it
if (!XShmAttach(X_display, &X_shminfo))
I_Error("XShmAttach() failed in InitGraphics()");

}
else
{
image = XCreateImage( X_display,
X_visual,
Expand Down

0 comments on commit 8ac4030

Please sign in to comment.