Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

MousePipe.cpp

Go to the documentation of this file.
00001 // MousePipe.cpp: implementation of the MousePipe class.
00004 
00005 
00006 #include "stdafx.h"
00007 #include "MousePipe.h"
00008 
00014 #define BUFFER_LIES_INT_FACTOR 7    
00015 
00019 #define QUEUE_LENGTH 4 
00020 
00024 #define BUFFER_SIZE sizeof(mouseMessage) * QUEUE_LENGTH * BUFFER_LIES_INT_FACTOR
00025 
00027 // Construction/Destruction
00029 
00034 MousePipe::MousePipe(const TString &name)
00035 :PServer(TString( TEXT("Mouse") ) + name, true, BUFFER_SIZE)
00036 {
00037     semName = TString( TEXT("MOUSESEM") ) + name;
00038     numberInQueue = NULL;
00039 }
00040 
00044 MousePipe::~MousePipe()
00045 {
00046     if(numberInQueue != NULL)
00047         CloseHandle(numberInQueue);
00048 }
00049 
00051 // Member Functions
00053 
00061 bool MousePipe::queueMessage(const int x, const int y, const int buttonState)
00062 {
00063     if( !isConnected() ){
00064         return false;
00065     }
00066 
00067     mouseMessage msg;
00068     msg.x = x;
00069     msg.y = y;
00070     msg.button = buttonState;
00071 
00072     DWORD bytesWritten;
00073     if( ReleaseSemaphore(numberInQueue, 1, NULL) ){ 
00074         // WriteFile will return very quickly since there is space in the buffer
00075         return WriteFile(hPipe, &msg, sizeof(mouseMessage), &bytesWritten, NULL)
00076             ? bytesWritten == sizeof(mouseMessage) : false;
00077     } else { // no space in buffer
00078         return false;
00079     }
00080 }
00081 
00086 bool MousePipe::serve()
00087 {
00088     if( !PServer::serve() ) {
00089         return false;
00090     } else {
00091         numberInQueue = CreateSemaphore(
00092             NULL,           // default security
00093             0,             // initial count ie 0 messages in pipe
00094             QUEUE_LENGTH,   // maximum count
00095             semName);
00096 
00097         if(numberInQueue == NULL){
00098             disconnect();
00099             return false;
00100         }
00101 
00102         return true;
00103     }   
00104 }
00105 
00110 void MousePipe::disconnect()
00111 {   
00112     PServer::disconnect();
00113 
00114     if(numberInQueue != NULL)
00115         CloseHandle(numberInQueue);
00116 }

Generated on Mon Mar 25 06:29:59 2002 for Window Dressing by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001