Annotation of doom/i_video.c, revision 1.1.1.3

1.1       root        1: // Emacs style mode select   -*- C++ -*- 
                      2: //-----------------------------------------------------------------------------
                      3: //
                      4: // $Id:$
                      5: //
                      6: // Copyright (C) 1993-1996 by id Software, Inc.
                      7: //
1.1.1.3 ! root        8: // This source is available for distribution and/or modification
        !             9: // only under the terms of the DOOM Source Code License as
        !            10: // published by id Software. All rights reserved.
1.1       root       11: //
1.1.1.3 ! root       12: // The source is distributed in the hope that it will be useful,
1.1       root       13: // but WITHOUT ANY WARRANTY; without even the implied warranty of
1.1.1.3 ! root       14: // FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License
        !            15: // for more details.
1.1       root       16: //
                     17: // $Log:$
                     18: //
                     19: // DESCRIPTION:
                     20: //     DOOM graphics stuff for X11, UNIX.
                     21: //
                     22: //-----------------------------------------------------------------------------
                     23: 
                     24: static const char
                     25: rcsid[] = "$Id: i_x.c,v 1.6 1997/02/03 22:45:10 b1 Exp $";
                     26: 
                     27: #include <stdlib.h>
1.1.1.3 ! root       28: //#include <unistd.h>
        !            29: //#include <sys/ipc.h>
        !            30: //#include <sys/shm.h>
        !            31: 
        !            32: //#include <X11/Xlib.h>
        !            33: //#include <X11/Xutil.h>
        !            34: //#include <X11/keysym.h>
        !            35: 
        !            36: //#include <X11/extensions/XShm.h>
        !            37: // Had to dig up XShm.c for this one.
        !            38: // It is in the libXext, but not in the XFree86 headers.
        !            39: #ifdef LINUX
        !            40: int XShmGetEventBase( Display* dpy ); // problems with g++?
        !            41: #endif
        !            42: 
1.1       root       43: #include <stdarg.h>
1.1.1.3 ! root       44: #include <time.h>
1.1       root       45: #include <sys/types.h>
1.1.1.3 ! root       46: //#include <sys/socket.h>
1.1       root       47: 
1.1.1.3 ! root       48: //#include <netinet/in.h>
        !            49: //#include <errnos.h>
1.1       root       50: #include <signal.h>
1.1.1.3 ! root       51: #include <ddraw.h>
1.1.1.2   root       52: 
1.1       root       53: #include "doomstat.h"
                     54: #include "i_system.h"
                     55: #include "v_video.h"
                     56: #include "m_argv.h"
                     57: #include "d_main.h"
                     58: 
                     59: #include "doomdef.h"
                     60: 
1.1.1.3 ! root       61: #define POINTER_WARP_COUNTDOWN 1
1.1.1.2   root       62: 
1.1.1.3 ! root       63: //FIXME
        !            64: void RenderScene(void);
1.1       root       65: 
1.1.1.3 ! root       66: /*
        !            67: Display*       X_display=0;
        !            68: Window         X_mainWindow;
        !            69: Colormap       X_cmap;
        !            70: Visual*                X_visual;
        !            71: GC             X_gc;
        !            72: XEvent         X_event;
        !            73: int            X_screen;
        !            74: XVisualInfo    X_visualinfo;
        !            75: XImage*                image;
        !            76: int            X_width;
        !            77: int            X_height;
        !            78: // MIT SHared Memory extension.
        !            79: boolean                doShm;
        !            80: 
        !            81: 
        !            82: XShmSegmentInfo        X_shminfo;
        !            83: int            X_shmeventtype;
        !            84: 
        !            85: int            doPointerWarp = POINTER_WARP_COUNTDOWN;
        !            86: */
        !            87: // Fake mouse handling.
        !            88: // This cannot work properly w/o DGA.
        !            89: // Needs an invisible mouse cursor at least.
        !            90: boolean                grabMouse;
        !            91: // Blocky mode,
        !            92: // replace each 320x200 pixel with multiply*multiply pixels.
        !            93: // According to Dave Taylor, it still is a bonehead thing
        !            94: // to use ....
        !            95: static int     multiply=1;
1.1       root       96: 
                     97: 
1.1.1.3 ! root       98: //
        !            99: //  Translates the key currently in X_event
        !           100: //
1.1       root      101: 
1.1.1.3 ! root      102: int xlatekey(void)
1.1       root      103: {
1.1.1.3 ! root      104: /*
        !           105:     int rc;
1.1       root      106: 
1.1.1.3 ! root      107:     switch(rc = XKeycodeToKeysym(X_display, X_event.xkey.keycode, 0))
        !           108:     {
        !           109:       case XK_Left:    rc = KEY_LEFTARROW;     break;
        !           110:       case XK_Right:   rc = KEY_RIGHTARROW;    break;
        !           111:       case XK_Down:    rc = KEY_DOWNARROW;     break;
        !           112:       case XK_Up:      rc = KEY_UPARROW;       break;
        !           113:       case XK_Escape:  rc = KEY_ESCAPE;        break;
        !           114:       case XK_Return:  rc = KEY_ENTER;         break;
        !           115:       case XK_Tab:     rc = KEY_TAB;           break;
        !           116:       case XK_F1:      rc = KEY_F1;            break;
        !           117:       case XK_F2:      rc = KEY_F2;            break;
        !           118:       case XK_F3:      rc = KEY_F3;            break;
        !           119:       case XK_F4:      rc = KEY_F4;            break;
        !           120:       case XK_F5:      rc = KEY_F5;            break;
        !           121:       case XK_F6:      rc = KEY_F6;            break;
        !           122:       case XK_F7:      rc = KEY_F7;            break;
        !           123:       case XK_F8:      rc = KEY_F8;            break;
        !           124:       case XK_F9:      rc = KEY_F9;            break;
        !           125:       case XK_F10:     rc = KEY_F10;           break;
        !           126:       case XK_F11:     rc = KEY_F11;           break;
        !           127:       case XK_F12:     rc = KEY_F12;           break;
        !           128:        
        !           129:       case XK_BackSpace:
        !           130:       case XK_Delete:  rc = KEY_BACKSPACE;     break;
        !           131: 
        !           132:       case XK_Pause:   rc = KEY_PAUSE;         break;
        !           133: 
        !           134:       case XK_KP_Equal:
        !           135:       case XK_equal:   rc = KEY_EQUALS;        break;
        !           136: 
        !           137:       case XK_KP_Subtract:
        !           138:       case XK_minus:   rc = KEY_MINUS;         break;
        !           139: 
        !           140:       case XK_Shift_L:
        !           141:       case XK_Shift_R:
        !           142:        rc = KEY_RSHIFT;
        !           143:        break;
        !           144:        
        !           145:       case XK_Control_L:
        !           146:       case XK_Control_R:
        !           147:        rc = KEY_RCTRL;
        !           148:        break;
        !           149:        
        !           150:       case XK_Alt_L:
        !           151:       case XK_Meta_L:
        !           152:       case XK_Alt_R:
        !           153:       case XK_Meta_R:
        !           154:        rc = KEY_RALT;
        !           155:        break;
        !           156:        
        !           157:       default:
        !           158:        if (rc >= XK_space && rc <= XK_asciitilde)
        !           159:            rc = rc - XK_space + ' ';
        !           160:        if (rc >= 'A' && rc <= 'Z')
        !           161:            rc = rc - 'A' + 'a';
        !           162:        break;
        !           163:     }
        !           164:     return rc;
        !           165: */
        !           166:   return 0;
1.1       root      167: 
1.1.1.3 ! root      168: }
1.1       root      169: 
                    170: void I_ShutdownGraphics(void)
                    171: {
1.1.1.3 ! root      172: /*
        !           173:   // Detach from X server
        !           174:   if (!XShmDetach(X_display, &X_shminfo))
        !           175:            I_Error("XShmDetach() failed in I_ShutdownGraphics()");
        !           176: 
        !           177:   // Release shared memory.
        !           178:   shmdt(X_shminfo.shmaddr);
        !           179:   shmctl(X_shminfo.shmid, IPC_RMID, 0);
        !           180: 
        !           181:   // Paranoia.
        !           182:   image->data = NULL;
        !           183: */
1.1       root      184: }
                    185: 
                    186: 
                    187: 
                    188: //
                    189: // I_StartFrame
                    190: //
                    191: void I_StartFrame (void)
                    192: {
                    193:     // er?
                    194: 
                    195: }
                    196: 
1.1.1.3 ! root      197: static int     lastmousex = 0;
        !           198: static int     lastmousey = 0;
        !           199: boolean                mousemoved = false;
        !           200: boolean                shmFinished;
1.1       root      201: 
1.1.1.3 ! root      202: void I_GetEvent(void)
        !           203: {
        !           204: /*
        !           205:     event_t event;
        !           206: 
        !           207:     // put event-grabbing stuff in here
        !           208:     XNextEvent(X_display, &X_event);
        !           209:     switch (X_event.type)
1.1.1.2   root      210:     {
1.1.1.3 ! root      211:       case KeyPress:
        !           212:        event.type = ev_keydown;
        !           213:        event.data1 = xlatekey();
        !           214:        D_PostEvent(&event);
        !           215:        // fprintf(stderr, "k");
        !           216:        break;
        !           217:       case KeyRelease:
        !           218:        event.type = ev_keyup;
        !           219:        event.data1 = xlatekey();
        !           220:        D_PostEvent(&event);
        !           221:        // fprintf(stderr, "ku");
        !           222:        break;
        !           223:       case ButtonPress:
        !           224:        event.type = ev_mouse;
        !           225:        event.data1 =
        !           226:            (X_event.xbutton.state & Button1Mask)
        !           227:            | (X_event.xbutton.state & Button2Mask ? 2 : 0)
        !           228:            | (X_event.xbutton.state & Button3Mask ? 4 : 0)
        !           229:            | (X_event.xbutton.button == Button1)
        !           230:            | (X_event.xbutton.button == Button2 ? 2 : 0)
        !           231:            | (X_event.xbutton.button == Button3 ? 4 : 0);
        !           232:        event.data2 = event.data3 = 0;
        !           233:        D_PostEvent(&event);
        !           234:        // fprintf(stderr, "b");
        !           235:        break;
        !           236:       case ButtonRelease:
        !           237:        event.type = ev_mouse;
        !           238:        event.data1 =
        !           239:            (X_event.xbutton.state & Button1Mask)
        !           240:            | (X_event.xbutton.state & Button2Mask ? 2 : 0)
        !           241:            | (X_event.xbutton.state & Button3Mask ? 4 : 0);
        !           242:        // suggest parentheses around arithmetic in operand of |
        !           243:        event.data1 =
        !           244:            event.data1
        !           245:            ^ (X_event.xbutton.button == Button1 ? 1 : 0)
        !           246:            ^ (X_event.xbutton.button == Button2 ? 2 : 0)
        !           247:            ^ (X_event.xbutton.button == Button3 ? 4 : 0);
        !           248:        event.data2 = event.data3 = 0;
        !           249:        D_PostEvent(&event);
        !           250:        // fprintf(stderr, "bu");
        !           251:        break;
        !           252:       case MotionNotify:
        !           253:        event.type = ev_mouse;
        !           254:        event.data1 =
        !           255:            (X_event.xmotion.state & Button1Mask)
        !           256:            | (X_event.xmotion.state & Button2Mask ? 2 : 0)
        !           257:            | (X_event.xmotion.state & Button3Mask ? 4 : 0);
        !           258:        event.data2 = (X_event.xmotion.x - lastmousex) << 2;
        !           259:        event.data3 = (lastmousey - X_event.xmotion.y) << 2;
        !           260: 
        !           261:        if (event.data2 || event.data3)
        !           262:        {
        !           263:            lastmousex = X_event.xmotion.x;
        !           264:            lastmousey = X_event.xmotion.y;
        !           265:            if (X_event.xmotion.x != X_width/2 &&
        !           266:                X_event.xmotion.y != X_height/2)
        !           267:            {
        !           268:                D_PostEvent(&event);
        !           269:                // fprintf(stderr, "m");
        !           270:                mousemoved = false;
        !           271:            } else
        !           272:            {
        !           273:                mousemoved = true;
        !           274:            }
        !           275:        }
        !           276:        break;
        !           277:        
        !           278:       case Expose:
        !           279:       case ConfigureNotify:
        !           280:        break;
        !           281:        
        !           282:       default:
        !           283:        if (doShm && X_event.type == X_shmeventtype) shmFinished = true;
        !           284:        break;
1.1       root      285:     }
1.1.1.2   root      286: 
1.1.1.3 ! root      287: */
        !           288: }
1.1       root      289: 
1.1.1.3 ! root      290: /*
        !           291: Cursor
        !           292: createnullcursor
        !           293: ( Display*     display,
        !           294:   Window       root )
        !           295: {
        !           296:     Pixmap cursormask;
        !           297:     XGCValues xgc;
        !           298:     GC gc;
        !           299:     XColor dummycolour;
        !           300:     Cursor cursor;
        !           301: 
        !           302:     cursormask = XCreatePixmap(display, root, 1, 1, 1 ); // depth
        !           303:     xgc.function = GXclear;
        !           304:     gc =  XCreateGC(display, cursormask, GCFunction, &xgc);
        !           305:     XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
        !           306:     dummycolour.pixel = 0;
        !           307:     dummycolour.red = 0;
        !           308:     dummycolour.flags = 04;
        !           309:     cursor = XCreatePixmapCursor(display, cursormask, cursormask,
        !           310:                                 &dummycolour,&dummycolour, 0,0);
        !           311:     XFreePixmap(display,cursormask);
        !           312:     XFreeGC(display,gc);
        !           313:     return cursor;
        !           314: }
        !           315: */
1.1       root      316: 
                    317: //
                    318: // I_StartTic
                    319: //
1.1.1.3 ! root      320: void I_StartTic (void)
        !           321: {
        !           322: /*
        !           323:     if (!X_display)
        !           324:        return;
        !           325:     while (XPending(X_display))
        !           326: */
        !           327:        // 11.4.98 dlw UNUSED EMPTY FUNCTION:  I_GetEvent();
        !           328: 
        !           329: /*
        !           330:     // Warp the pointer back to the middle of the window
        !           331:     //  or it will wander off - that is, the game will
        !           332:     //  loose input focus within X11.
        !           333:     if (grabMouse)
        !           334:     {
        !           335:        if (!--doPointerWarp)
        !           336:        {
        !           337:            XWarpPointer( X_display,
        !           338:                          None,
        !           339:                          X_mainWindow,
        !           340:                          0, 0,
        !           341:                          0, 0,
        !           342:                          X_width/2, X_height/2);
1.1       root      343: 
1.1.1.3 ! root      344:            doPointerWarp = POINTER_WARP_COUNTDOWN;
        !           345:        }
        !           346:     }
        !           347: */
1.1       root      348: 
1.1.1.3 ! root      349:     //mousemoved = false;
        !           350: }
1.1       root      351: 
                    352: 
                    353: //
                    354: // I_UpdateNoBlit
                    355: //
                    356: void I_UpdateNoBlit (void)
                    357: {
                    358:     // what is this?
                    359: }
                    360: 
1.1.1.3 ! root      361: //
        !           362: // I_FinishUpdate
        !           363: //
        !           364: char MsgText[256];
        !           365: void WriteDebug(char *);
        !           366: 
1.1.1.2   root      367: void I_FinishUpdate(void)
1.1.1.3 ! root      368: {
        !           369: /* 11.4.98 Unused so kill a couple bytes from exe
        !           370:     
        !           371:        static int      lasttic;
1.1       root      372:     int                tics;
                    373:     int                i;
                    374:     // UNUSED static unsigned char *bigscreen=0;
                    375: 
                    376:     // draws little dots on the bottom of the screen
                    377:     if (devparm)
                    378:     {
1.1.1.3 ! root      379:                i = I_GetTime();
        !           380:                tics = i - lasttic;
        !           381:                lasttic = i;
        !           382:                if (tics > 20) tics = 20;
        !           383:                
        !           384:                for (i = 0; i < tics*2; i += 2)
        !           385:                        screens[0][ (SCREENHEIGHT-1)*SCREENWIDTH + i] = 0xff;
        !           386:                // for ( ; i < 20*2; i += 2)    multiply?? dlw
        !           387:                for ( ; i < 40; i += 2)
        !           388:                        screens[0][ (SCREENHEIGHT-1)*SCREENWIDTH + i] = 0x0;
        !           389:     }
        !           390:     RenderScene();
        !           391: 
        !           392: 11.4.98 */
        !           393: 
        !           394: 
        !           395: /*
        !           396: 
        !           397:     // scales the screen size before blitting it
        !           398:     if (multiply == 2)
        !           399:     {
        !           400:        unsigned int *olineptrs[2];
        !           401:        unsigned int *ilineptr;
        !           402:        int x, y, i;
        !           403:        unsigned int twoopixels;
        !           404:        unsigned int twomoreopixels;
        !           405:        unsigned int fouripixels;
        !           406: 
        !           407:        ilineptr = (unsigned int *) (screens[0]);
        !           408:        for (i=0 ; i<2 ; i++)
        !           409:            olineptrs[i] = (unsigned int *) &image->data[i*X_width];
        !           410: 
        !           411:        y = SCREENHEIGHT;
        !           412:        while (y--)
        !           413:        {
        !           414:            x = SCREENWIDTH;
        !           415:            do
        !           416:            {
        !           417:                fouripixels = *ilineptr++;
        !           418:                twoopixels =    (fouripixels & 0xff000000)
        !           419:                    |   ((fouripixels>>8) & 0xffff00)
        !           420:                    |   ((fouripixels>>16) & 0xff);
        !           421:                twomoreopixels =        ((fouripixels<<16) & 0xff000000)
        !           422:                    |   ((fouripixels<<8) & 0xffff00)
        !           423:                    |   (fouripixels & 0xff);
        !           424: #ifdef __BIG_ENDIAN__
        !           425:                *olineptrs[0]++ = twoopixels;
        !           426:                *olineptrs[1]++ = twoopixels;
        !           427:                *olineptrs[0]++ = twomoreopixels;
        !           428:                *olineptrs[1]++ = twomoreopixels;
        !           429: #else
        !           430:                *olineptrs[0]++ = twomoreopixels;
        !           431:                *olineptrs[1]++ = twomoreopixels;
        !           432:                *olineptrs[0]++ = twoopixels;
        !           433:                *olineptrs[1]++ = twoopixels;
        !           434: #endif
        !           435:            } while (x-=4);
        !           436:            olineptrs[0] += X_width/4;
        !           437:            olineptrs[1] += X_width/4;
        !           438:        }
        !           439: 
        !           440:     }
        !           441:     else if (multiply == 3)
        !           442:     {
        !           443:        unsigned int *olineptrs[3];
        !           444:        unsigned int *ilineptr;
        !           445:        int x, y, i;
        !           446:        unsigned int fouropixels[3];
        !           447:        unsigned int fouripixels;
        !           448: 
        !           449:        ilineptr = (unsigned int *) (screens[0]);
        !           450:        for (i=0 ; i<3 ; i++)
        !           451:            olineptrs[i] = (unsigned int *) &image->data[i*X_width];
        !           452: 
        !           453:        y = SCREENHEIGHT;
        !           454:        while (y--)
        !           455:        {
        !           456:            x = SCREENWIDTH;
        !           457:            do
        !           458:            {
        !           459:                fouripixels = *ilineptr++;
        !           460:                fouropixels[0] = (fouripixels & 0xff000000)
        !           461:                    |   ((fouripixels>>8) & 0xff0000)
        !           462:                    |   ((fouripixels>>16) & 0xffff);
        !           463:                fouropixels[1] = ((fouripixels<<8) & 0xff000000)
        !           464:                    |   (fouripixels & 0xffff00)
        !           465:                    |   ((fouripixels>>8) & 0xff);
        !           466:                fouropixels[2] = ((fouripixels<<16) & 0xffff0000)
        !           467:                    |   ((fouripixels<<8) & 0xff00)
        !           468:                    |   (fouripixels & 0xff);
        !           469: #ifdef __BIG_ENDIAN__
        !           470:                *olineptrs[0]++ = fouropixels[0];
        !           471:                *olineptrs[1]++ = fouropixels[0];
        !           472:                *olineptrs[2]++ = fouropixels[0];
        !           473:                *olineptrs[0]++ = fouropixels[1];
        !           474:                *olineptrs[1]++ = fouropixels[1];
        !           475:                *olineptrs[2]++ = fouropixels[1];
        !           476:                *olineptrs[0]++ = fouropixels[2];
        !           477:                *olineptrs[1]++ = fouropixels[2];
        !           478:                *olineptrs[2]++ = fouropixels[2];
        !           479: #else
        !           480:                *olineptrs[0]++ = fouropixels[2];
        !           481:                *olineptrs[1]++ = fouropixels[2];
        !           482:                *olineptrs[2]++ = fouropixels[2];
        !           483:                *olineptrs[0]++ = fouropixels[1];
        !           484:                *olineptrs[1]++ = fouropixels[1];
        !           485:                *olineptrs[2]++ = fouropixels[1];
        !           486:                *olineptrs[0]++ = fouropixels[0];
        !           487:                *olineptrs[1]++ = fouropixels[0];
        !           488:                *olineptrs[2]++ = fouropixels[0];
        !           489: #endif
        !           490:            } while (x-=4);
        !           491:            olineptrs[0] += 2*X_width/4;
        !           492:            olineptrs[1] += 2*X_width/4;
        !           493:            olineptrs[2] += 2*X_width/4;
        !           494:        }
1.1       root      495: 
                    496:     }
1.1.1.3 ! root      497:     else if (multiply == 4)
        !           498:     {
        !           499:        // Broken. Gotta fix this some day.
        !           500:        void Expand4(unsigned *, double *);
        !           501:        Expand4 ((unsigned *)(screens[0]), (double *) (image->data));
        !           502:     }
        !           503: 
        !           504:     if (doShm)
        !           505:     {
        !           506: 
        !           507:        if (!XShmPutImage(      X_display,
        !           508:                                X_mainWindow,
        !           509:                                X_gc,
        !           510:                                image,
        !           511:                                0, 0,
        !           512:                                0, 0,
        !           513:                                X_width, X_height,
        !           514:                                True ))
        !           515:            I_Error("XShmPutImage() failed\n");
        !           516: 
        !           517:        // wait for it to finish and processes all input events
        !           518:        shmFinished = false;
        !           519:        do
        !           520:        {
        !           521:            I_GetEvent();
        !           522:        } while (!shmFinished);
        !           523: 
        !           524:     }
        !           525:     else
        !           526:     {
        !           527: 
        !           528:        // draw the image
        !           529:        XPutImage(      X_display,
        !           530:                        X_mainWindow,
        !           531:                        X_gc,
        !           532:                        image,
        !           533:                        0, 0,
        !           534:                        0, 0,
        !           535:                        X_width, X_height );
        !           536: 
        !           537:        // sync up with server
        !           538:        XSync(X_display, False);
        !           539: 
        !           540:     }
        !           541: */
        !           542: }
1.1       root      543: 
                    544: 
                    545: //
                    546: // I_ReadScreen
                    547: //
                    548: void I_ReadScreen (byte* scr)
                    549: {
1.1.1.3 ! root      550:     // original id code - page flipped??
        !           551:        memcpy(scr, screens[0], SCREENMULT);
        !           552: }
        !           553: 
        !           554: extern LPDIRECTDRAWPALETTE lpPalette;
        !           555: 
        !           556: //
        !           557: // Palette stuff.
        !           558: //
        !           559: //
        !           560: // I_SetPalette
        !           561: //
        !           562: void I_SetPalette(byte* palette)
        !           563:    {
        !           564:     PALETTEENTRY pe[256];
        !           565:     int i;
        !           566: 
        !           567:     for (i = 0; i < 256; i++)
        !           568:        {
        !           569:         pe[i].peRed = gammatable[usegamma][*palette++];
        !           570:         pe[i].peGreen = gammatable[usegamma][*palette++];
        !           571:         pe[i].peBlue = gammatable[usegamma][*palette++];
        !           572:        }
        !           573:     if (lpPalette != 0)
        !           574:         lpPalette->lpVtbl->SetEntries(lpPalette, 0, 0, 256, pe);
1.1       root      575: }
                    576: 
1.1.1.3 ! root      577: /*
        !           578: //
        !           579: // This function is probably redundant,
        !           580: //  if XShmDetach works properly.
        !           581: // ddt never detached the XShm memory,
        !           582: //  thus there might have been stale
        !           583: //  handles accumulating.
        !           584: //
        !           585: void grabsharedmemory(int size)
        !           586: {
1.1       root      587: 
1.1.1.3 ! root      588:   int                  key = ('d'<<24) | ('o'<<16) | ('o'<<8) | 'm';
        !           589:   struct shmid_ds      shminfo;
        !           590:   int                  minsize = 320*200;
        !           591:   int                  id;
        !           592:   int                  rc;
        !           593:   // UNUSED int done=0;
        !           594:   int                  pollution=5;
        !           595:   
        !           596:   // try to use what was here before
        !           597:   do
        !           598:   {
        !           599:     id = shmget((key_t) key, minsize, 0777); // just get the id
        !           600:     if (id != -1)
        !           601:     {
        !           602:       rc=shmctl(id, IPC_STAT, &shminfo); // get stats on it
        !           603:       if (!rc) 
        !           604:       {
        !           605:        if (shminfo.shm_nattch)
        !           606:        {
        !           607:          fprintf(stderr, "User %d appears to be running "
        !           608:                  "DOOM.  Is that wise?\n", shminfo.shm_cpid);
        !           609:          key++;
        !           610:        }
        !           611:        else
        !           612:        {
        !           613:          if (getuid() == shminfo.shm_perm.cuid)
        !           614:          {
        !           615:            rc = shmctl(id, IPC_RMID, 0);
        !           616:            if (!rc)
        !           617:              fprintf(stderr,
        !           618:                      "Was able to kill my old shared memory\n");
        !           619:            else
        !           620:              I_Error("Was NOT able to kill my old shared memory");
        !           621:            
        !           622:            id = shmget((key_t)key, size, IPC_CREAT|0777);
        !           623:            if (id==-1)
        !           624:              I_Error("Could not get shared memory");
        !           625:            
        !           626:            rc=shmctl(id, IPC_STAT, &shminfo);
        !           627:            
        !           628:            break;
        !           629:            
        !           630:          }
        !           631:          if (size >= shminfo.shm_segsz)
        !           632:          {
        !           633:            fprintf(stderr,
        !           634:                    "will use %d's stale shared memory\n",
        !           635:                    shminfo.shm_cpid);
        !           636:            break;
        !           637:          }
        !           638:          else
        !           639:          {
        !           640:            fprintf(stderr,
        !           641:                    "warning: can't use stale "
        !           642:                    "shared memory belonging to id %d, "
        !           643:                    "key=0x%x\n",
        !           644:                    shminfo.shm_cpid, key);
        !           645:            key++;
        !           646:          }
        !           647:        }
        !           648:       }
        !           649:       else
        !           650:       {
        !           651:        I_Error("could not get stats on key=%d", key);
        !           652:       }
        !           653:     }
        !           654:     else
        !           655:     {
        !           656:       id = shmget((key_t)key, size, IPC_CREAT|0777);
        !           657:       if (id==-1)
        !           658:       {
        !           659:        extern int errno;
        !           660:        fprintf(stderr, "errno=%d\n", errno);
        !           661:        I_Error("Could not get any shared memory");
        !           662:       }
        !           663:       break;
        !           664:     }
        !           665:   } while (--pollution);
        !           666:   
        !           667:   if (!pollution)
1.1       root      668:   {
1.1.1.3 ! root      669:     I_Error("Sorry, system too polluted with stale "
        !           670:            "shared memory segments.\n");
        !           671:     }  
        !           672:   
        !           673:   X_shminfo.shmid = id;
        !           674:   
        !           675:   // attach to the shared memory segment
        !           676:   image->data = X_shminfo.shmaddr = shmat(id, 0, 0);
        !           677:   
        !           678:   fprintf(stderr, "shared memory id=%d, addr=0x%x\n", id,
        !           679:          (int) (image->data));
        !           680: }
        !           681: */
        !           682: void I_InitGraphics(void)
        !           683: {
        !           684:     char*              displayname;
        !           685: //    char*            d;
        !           686:     int                        n;
        !           687:     int                        pnum;
        !           688:     int                        x=0;
        !           689:     int                        y=0;
        !           690:     
        !           691:     // warning: char format, different type arg
        !           692:     char               xsign=' ';
        !           693:     char               ysign=' ';
        !           694:     
        !           695: //    int                      oktodraw;
        !           696: //    unsigned long    attribmask;
        !           697:     //XSetWindowAttributes attribs;
        !           698:     //XGCValues                xgcvalues;
        !           699: //    int                      valuemask;
        !           700:     static int         firsttime=1;
        !           701: 
        !           702:     if (!firsttime)
        !           703:        return;
        !           704:     firsttime = 0;
        !           705: 
        !           706:     //signal(SIGINT, (void (*)(int)) I_Quit);
        !           707: 
        !           708:     if (M_CheckParm("-2"))
        !           709:        multiply = 2;
        !           710: 
        !           711:     if (M_CheckParm("-3"))
        !           712:        multiply = 3;
        !           713: 
        !           714:     if (M_CheckParm("-4"))
        !           715:        multiply = 4;
        !           716: 
        !           717:     //X_width = SCREENWIDTH * multiply;
        !           718:     //X_height = SCREENHEIGHT * multiply;
        !           719: 
        !           720:     // check for command-line display name
        !           721:     if ( (pnum=M_CheckParm("-disp")) ) // suggest parentheses around assignment
        !           722:        displayname = myargv[pnum+1];
        !           723:     else
        !           724:        displayname = 0;
        !           725: 
        !           726:     // check if the user wants to grab the mouse (quite unnice)
        !           727:     grabMouse = !!M_CheckParm("-grabmouse");
1.1       root      728: 
1.1.1.3 ! root      729:     // check for command-line geometry
        !           730:     if ( (pnum=M_CheckParm("-geom")) ) // suggest parentheses around assignment
1.1       root      731:     {
1.1.1.3 ! root      732:        // warning: char format, different type arg 3,5
        !           733:        n = sscanf(myargv[pnum+1], "%c%d%c%d", &xsign, &x, &ysign, &y);
        !           734:        
        !           735:        if (n==2)
        !           736:            x = y = 0;
        !           737:        else if (n==6)
        !           738:        {
        !           739:            if (xsign == '-')
        !           740:                x = -x;
        !           741:            if (ysign == '-')
        !           742:                y = -y;
        !           743:        }
        !           744:        else
        !           745:            I_Error("bad -geom parameter");
1.1       root      746:     }
1.1.1.3 ! root      747: /*
        !           748:     // open the display
        !           749:     X_display = XOpenDisplay(displayname);
        !           750:     if (!X_display)
        !           751:     {
        !           752:        if (displayname)
        !           753:            I_Error("Could not open display [%s]", displayname);
        !           754:        else
        !           755:            I_Error("Could not open display (DISPLAY=[%s])", getenv("DISPLAY"));
        !           756:     }
        !           757:     // use the default visual 
        !           758: //    X_screen = DefaultScreen(X_display);
        !           759: //    if (!XMatchVisualInfo(X_display, X_screen, 8, PseudoColor, &X_visualinfo))
        !           760: //     I_Error("xdoom currently only supports 256-color PseudoColor screens");
        !           761: //    X_visual = X_visualinfo.visual;
1.1       root      762: 
1.1.1.3 ! root      763:     // check for the MITSHM extension
        !           764: //    doShm = XShmQueryExtension(X_display);
1.1       root      765: 
1.1.1.3 ! root      766:     // even if it's available, make sure it's a local connection
        !           767:     if (doShm)
        !           768:     {
        !           769:        if (!displayname) displayname = (char *) getenv("DISPLAY");
        !           770:        if (displayname)
        !           771:        {
        !           772:            d = displayname;
        !           773:            while (*d && (*d != ':')) d++;
        !           774:            if (*d) *d = 0;
        !           775:            if (!(!strcasecmp(displayname, "unix") || !*displayname)) doShm = false;
        !           776:        }
        !           777:     }
        !           778: 
        !           779:     fprintf(stderr, "Using MITSHM extension\n");
        !           780: 
        !           781:     // create the colormap
        !           782:     X_cmap = XCreateColormap(X_display, RootWindow(X_display,
        !           783:                                                   X_screen), X_visual, AllocAll);
        !           784: 
        !           785:     // setup attributes for main window
        !           786:     attribmask = CWEventMask | CWColormap | CWBorderPixel;
        !           787:     attribs.event_mask =
        !           788: //     KeyPressMask
        !           789: //     | KeyReleaseMask
        !           790:        | PointerMotionMask | ButtonPressMask | ButtonReleaseMask
        !           791: //     | ExposureMask;
        !           792: 
        !           793: //    attribs.colormap = X_cmap;
        !           794: //    attribs.border_pixel = 0;
        !           795: 
        !           796:     // create the main window
        !           797:     X_mainWindow = XCreateWindow(      X_display,
        !           798:                                        RootWindow(X_display, X_screen),
        !           799:                                        x, y,
        !           800:                                        X_width, X_height,
        !           801:                                        0, // borderwidth
        !           802:                                        8, // depth
        !           803:                                        InputOutput,
        !           804:                                        X_visual,
        !           805:                                        attribmask,
        !           806:                                        &attribs );
        !           807: 
        !           808:     XDefineCursor(X_display, X_mainWindow,
        !           809:                  createnullcursor( X_display, X_mainWindow ) );
        !           810: 
        !           811:     // create the GC
        !           812:     valuemask = GCGraphicsExposures;
        !           813:     xgcvalues.graphics_exposures = False;
        !           814:     X_gc = XCreateGC(  X_display,
        !           815:                        X_mainWindow,
        !           816:                        valuemask,
        !           817:                        &xgcvalues );
        !           818: 
        !           819:     // map the window
        !           820:     XMapWindow(X_display, X_mainWindow);
        !           821:     // wait until it is OK to draw
        !           822:     oktodraw = 0;
        !           823:     while (!oktodraw)
        !           824:     {
        !           825: //     XNextEvent(X_display, &X_event);
        !           826: //     if (X_event.type == Expose
        !           827: //         && !X_event.xexpose.count)
        !           828:        {
        !           829:            oktodraw = 1;
        !           830:        }
        !           831:     }
        !           832: 
        !           833:     // grabs the pointer so it is restricted to this window
        !           834:     if (grabMouse)
        !           835:        XGrabPointer(X_display, X_mainWindow, True,
        !           836:                     ButtonPressMask|ButtonReleaseMask|PointerMotionMask,
        !           837:                     GrabModeAsync, GrabModeAsync,
        !           838:                     X_mainWindow, None, CurrentTime);
        !           839: 
        !           840:     if (doShm)
        !           841:     {
1.1       root      842: 
1.1.1.3 ! root      843:        X_shmeventtype = XShmGetEventBase(X_display) + ShmCompletion;
1.1       root      844: 
1.1.1.3 ! root      845:        // create the image
        !           846:        image = XShmCreateImage(        X_display,
        !           847:                                        X_visual,
        !           848:                                        8,
        !           849:                                        ZPixmap,
        !           850:                                        0,
        !           851:                                        &X_shminfo,
        !           852:                                        X_width,
        !           853:                                        X_height );
        !           854: 
        !           855:        grabsharedmemory(image->bytes_per_line * image->height);
        !           856: 
        !           857: 
        !           858:        // UNUSED
        !           859:        // create the shared memory segment
        !           860:        // X_shminfo.shmid = shmget (IPC_PRIVATE,
        !           861:        // image->bytes_per_line * image->height, IPC_CREAT | 0777);
        !           862:        // if (X_shminfo.shmid < 0)
        !           863:        // {
        !           864:        // perror("");
        !           865:        // I_Error("shmget() failed in InitGraphics()");
        !           866:        // }
        !           867:        // fprintf(stderr, "shared memory id=%d\n", X_shminfo.shmid);
        !           868:        // attach to the shared memory segment
        !           869:        // image->data = X_shminfo.shmaddr = shmat(X_shminfo.shmid, 0, 0);
        !           870:        
        !           871: 
        !           872:        if (!image->data)
        !           873:        {
        !           874:            perror("");
        !           875:            I_Error("shmat() failed in InitGraphics()");
        !           876:        }
        !           877: 
        !           878:        // get the X server to attach to it
        !           879:        if (!XShmAttach(X_display, &X_shminfo))
        !           880:            I_Error("XShmAttach() failed in InitGraphics()");
1.1       root      881: 
1.1.1.3 ! root      882:     }
        !           883:     else
        !           884:     {
        !           885:        image = XCreateImage(   X_display,
        !           886:                                X_visual,
        !           887:                                8,
        !           888:                                ZPixmap,
        !           889:                                0,
        !           890:                                (char*)malloc(X_width * X_height),
        !           891:                                X_width, X_height,
        !           892:                                8,
        !           893:                                X_width );
1.1       root      894: 
1.1.1.3 ! root      895:     }
        !           896: 
        !           897:     if (multiply == 1)
        !           898:        screens[0] = (unsigned char *) (image->data);
        !           899:     else
        !           900:        screens[0] = (unsigned char *) malloc (SCREENWIDTH * SCREENHEIGHT);
        !           901: */
        !           902: }
        !           903: 
        !           904: 
        !           905: unsigned       exptable[256];
        !           906: 
        !           907: void InitExpand (void)
        !           908: {
        !           909:     int                i;
        !           910:        
        !           911:     for (i=0 ; i<256 ; i++)
        !           912:        exptable[i] = i | (i<<8) | (i<<16) | (i<<24);
        !           913: }
        !           914: 
        !           915: double         exptable2[256*256];
        !           916: 
        !           917: void InitExpand2 (void)
        !           918: {
        !           919:     int                i;
        !           920:     int                j;
        !           921:     // UNUSED unsigned iexp, jexp;
        !           922:     double*    exp;
        !           923:     union
        !           924:     {
        !           925:        double          d;
        !           926:        unsigned        u[2];
        !           927:     } pixel;
        !           928:        
        !           929:     printf ("building exptable2...\n");
        !           930:     exp = exptable2;
        !           931:     for (i=0 ; i<256 ; i++)
        !           932:     {
        !           933:        pixel.u[0] = i | (i<<8) | (i<<16) | (i<<24);
        !           934:        for (j=0 ; j<256 ; j++)
        !           935:        {
        !           936:            pixel.u[1] = j | (j<<8) | (j<<16) | (j<<24);
        !           937:            *exp++ = pixel.d;
        !           938:        }
        !           939:     }
        !           940:     printf ("done.\n");
        !           941: }
1.1       root      942: 
                    943: int    inited;
                    944: 
1.1.1.3 ! root      945: void
        !           946: Expand4
        !           947: ( unsigned*    lineptr,
        !           948:   double*      xline )
        !           949: {
        !           950:     double     dpixel;
        !           951:     unsigned   x;
        !           952:     unsigned   y;
        !           953:     unsigned   fourpixels;
        !           954:     unsigned   step;
        !           955:     double*    exp;
        !           956:        
        !           957:     exp = exptable2;
        !           958:     if (!inited)
        !           959:     {
        !           960:        inited = 1;
        !           961:        InitExpand2 ();
        !           962:     }
        !           963:                
        !           964:                
        !           965:     step = 3*SCREENWIDTH/2;
        !           966:        
        !           967:     y = SCREENHEIGHT-1;
        !           968:     do
        !           969:     {
        !           970:        x = SCREENWIDTH;
        !           971: 
        !           972:        do
        !           973:        {
        !           974:            fourpixels = lineptr[0];
        !           975:                        
        !           976:            dpixel = *(double *)( (int)exp + ( (fourpixels&0xffff0000)>>13) );
        !           977:            xline[0] = dpixel;
        !           978:            xline[160] = dpixel;
        !           979:            xline[320] = dpixel;
        !           980:            xline[480] = dpixel;
        !           981:                        
        !           982:            dpixel = *(double *)( (int)exp + ( (fourpixels&0xffff)<<3 ) );
        !           983:            xline[1] = dpixel;
        !           984:            xline[161] = dpixel;
        !           985:            xline[321] = dpixel;
        !           986:            xline[481] = dpixel;
        !           987: 
        !           988:            fourpixels = lineptr[1];
        !           989:                        
        !           990:            dpixel = *(double *)( (int)exp + ( (fourpixels&0xffff0000)>>13) );
        !           991:            xline[2] = dpixel;
        !           992:            xline[162] = dpixel;
        !           993:            xline[322] = dpixel;
        !           994:            xline[482] = dpixel;
        !           995:                        
        !           996:            dpixel = *(double *)( (int)exp + ( (fourpixels&0xffff)<<3 ) );
        !           997:            xline[3] = dpixel;
        !           998:            xline[163] = dpixel;
        !           999:            xline[323] = dpixel;
        !          1000:            xline[483] = dpixel;
        !          1001: 
        !          1002:            fourpixels = lineptr[2];
        !          1003:                        
        !          1004:            dpixel = *(double *)( (int)exp + ( (fourpixels&0xffff0000)>>13) );
        !          1005:            xline[4] = dpixel;
        !          1006:            xline[164] = dpixel;
        !          1007:            xline[324] = dpixel;
        !          1008:            xline[484] = dpixel;
        !          1009:                        
        !          1010:            dpixel = *(double *)( (int)exp + ( (fourpixels&0xffff)<<3 ) );
        !          1011:            xline[5] = dpixel;
        !          1012:            xline[165] = dpixel;
        !          1013:            xline[325] = dpixel;
        !          1014:            xline[485] = dpixel;
        !          1015: 
        !          1016:            fourpixels = lineptr[3];
        !          1017:                        
        !          1018:            dpixel = *(double *)( (int)exp + ( (fourpixels&0xffff0000)>>13) );
        !          1019:            xline[6] = dpixel;
        !          1020:            xline[166] = dpixel;
        !          1021:            xline[326] = dpixel;
        !          1022:            xline[486] = dpixel;
        !          1023:                        
        !          1024:            dpixel = *(double *)( (int)exp + ( (fourpixels&0xffff)<<3 ) );
        !          1025:            xline[7] = dpixel;
        !          1026:            xline[167] = dpixel;
        !          1027:            xline[327] = dpixel;
        !          1028:            xline[487] = dpixel;
        !          1029: 
        !          1030:            lineptr+=4;
        !          1031:            xline+=8;
        !          1032:        } while (x-=16);
        !          1033:        xline += step;
        !          1034:     } while (y--);
        !          1035: }
        !          1036: 
1.1       root     1037: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.