/* * This Software is the original work of Daniel TUNG * This Software is submitted in partial fulfillment of the * requirements for the degree of BCSE. * Dept. Computer Science, Monash University 2000 * * Copyright (c) 2000 beetung@cs.monash.edu.au * beetung@geocities.com * */ #ifndef FASTBUF_H #define FASTBUF_H #include extern "C" { #include #include #include #include #include #include }; #include "autoline.h" //// //// A special class for testing XShm enabled flag only. //// //// -- inheritance is wasteful cos we have to //// perform the same set of procedures on every //// object initialized //// //// -- Instead, we use it as a single static inside Fastbuf //// //////////////////////////////////////////////////////////// class XShmTester { //////////////////////////////////////////////////////////// private: Display *dpy; // Display bool weHaveXShm; // bool variable public: XShmTester(); operator bool (void) { return weHaveXShm; } private: //----------------------------- bool isXShmEnabled(void); // is it? void NoXShmError(void); // pump error message to screen //----------------------------- }; //// //// Fastbuf, XImage buffer allocation, using XShm Extension //// //// //////////////////////////////////////////////////////////// class Fastbuf { //////////////////////////////////////////////////////////// private: Display *dpy; // Display XShmSegmentInfo shminfo; // XShm Segments static XShmTester hasXShm; // test to see if have XShm once only public: XImage *image; // XShm Allocated XImage unsigned char *pixel; // For normal X, instead of XShm bool bAlloc; enum XimType { XSHM, BASIC_X }; /// /// A holder to hold all the nodes for track ID! /// int& nodeCount; // node holder increments this one! not by fastbuf static LListHolder< BaseNode > nodeHolder; //------------- BaseNode* addNode(int y, int x) { //------------- // we have to find a way to set this the buffer to *this before calling // the ctor /// /// this is a quick fix, shouldnt really do it!!!!!!!! /// refer to comment appended after this line BaseNode ::baseFrame = (int *)*this; /// the problem is that we have to set the static fastbuf pointer /// 1st before we call the ctor to autoline<> which will make use of /// the fastbuf pointer to try initialize the line (2 ends). we have /// to figure out a way to set the baseframe pointer before we call the /// autline<> ctor!!!! cerr << "calling autoline ctor(..) " << endl; BaseNode *p =new AutoLine (y,x) ; cerr << " autoline ctor(..) ... completed" << endl; nodeHolder.Append( p ); return p; } //------------- inline void swimNodes(BaseNode* p) { p->findEdge(); } inline void drawNodes(BaseNode* p) { p->draw(); } //------------- inline void swimNodes (void) { setAsBaseFrame(); nodeHolder.findEdge(); } inline void drawNodes (void) { setAsBaseFrame(); nodeHolder.draw(); } inline int getNodeCenter(void) { setAsBaseFrame();return nodeHolder.AllCenterX(); } inline void setAsBaseFrame (void) { /// /// this is a quick fix, shouldnt really do it!!!!!!!! /// refer to comment on Append(..) BaseNode ::baseFrame = (int *)*this; // nodeHolder.setBaseFrame( reinterpret_cast(image->data) ); } //------------- public: //// ctor //------------- Fastbuf(); Fastbuf(char*& bufPtr); Fastbuf(Fastbuf& fb); ~Fastbuf() ; //------------- //// //// Various casting operators overloaded //// //----------------------------- // inline char*& getDataAddress() { return image->data; } // inline XImage*& getXImageAddress() { return image; } //----------------------------- operator int (void) { return (int)image->data[0]; } operator unsigned int (void) { return (unsigned int)image->data[0]; } operator char& (void) { return image->data[0]; } operator unsigned char (void) { return (unsigned char)image->data[0]; } //----------------------------- operator int* (void) { return reinterpret_cast (image->data); } operator unsigned int* (void) { return reinterpret_cast(image->data); } operator char*&(void) { return (image->data); } operator unsigned char*(void) { return reinterpret_cast(image->data); } //----------------------------- operator XImage*& (void) { return image; } //----------------------------- //// /// This fn initialize the Shared memory for XShm and setup the /// appropriate mode. By default (argument), it creates a 288x384x8bpp /// data array for storing an image for a single frame for our CCC project. /// Do a search on XShm for more info on Shm-mit X extension! /// &beetung 2000 //------------------------------------ void newXShmImage(int ySize =288, int xSize = 384) { //------------------------------------ image=XShmCreateImage( dpy,None,24,ZPixmap, (char*)&pixel,&shminfo, xSize, ySize); image->byte_order = MSBFirst ; image->bitmap_bit_order = MSBFirst ; shminfo.shmid =shmget (IPC_PRIVATE, xSize*ySize*4, IPC_CREAT | 0777); shminfo.shmaddr =image->data = static_cast( shmat(shminfo.shmid, NULL, 0)); shminfo.readOnly =False; if (XShmAttach(dpy, &shminfo)) { #ifdef DEBUG cout << "Fastbuf::setXShm ....Attached OK" << endl; #endif } XSync(dpy, False); shmctl(shminfo.shmid, IPC_RMID, 0); } //----------------------------- void newXImage(char* bufPtr = 0, int ySize = 288,int xSize = 384) { //----------------------------- if (!bufPtr) { pixel = new unsigned char [384*288*4]; bAlloc = true; } else { pixel = (unsigned char*)bufPtr; bAlloc = false; } image =XCreateImage( dpy,None,24,ZPixmap, 0,(char*)pixel, xSize, ySize,32,0); } //----------------------------- void* mXCopy(char* src, int size){ //----------------------------- /* XXXX cout <<" XCopy() size : " << size << endl; cout <<" XCopy() size working : " << (388*284*4)<< endl; */ return memcpy(image->data,src,388*284*4); } }; #endif