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

UpdateHandler Class Reference

An object running a separate update thread. More...

#include <UpdateHandler.h>

List of all members.

Public Methods

bool setCapture (const TString &, const TString &, const int, const int)
 Sets capture information.

bool setCaptureName (const TString &)
 Sets just the capture name.

bool setCaptureDir (const TString &)
 Sets the capture directory.

bool setCaptureLimit (const int)
 Sets the capture limit.

bool setCaptureFrequency (const int)
 Sets the capture Frequency.

void disableCapture ()
 Stops the capture of screen images.

bool enableCapture ()
 Enables the capture of screen images.

const TString getCaptureName () const
 get the capture name TString.

const TString getCaptureDir () const
 get the capture directory TString.

int getCaptureLimit () const
 get the capture limit.

int getCaptureFrequency () const
 get the cpature Frequency.

void stop (const bool=true)
bool run ()
 UpdateHandler (ScreenPipe &, BitmapScreen &, HWND)
virtual ~UpdateHandler ()

Static Private Methods

DWORD WINAPI update (LPVOID)

Private Attributes

HANDLE hUpdateThread
 Handle to the update thread.

volatile bool exit
 true when update thread should terminate.

BitmapScreenscreen
 Pointer to the BitmapScreen.

ScreenPipepipe
 Pointer to the ScreenPipe.

const HWND hWnd
 Handle to the main window.

HANDLE captureMutex
 A Handle to a Mutex protecting the capture details during capture.

TString captureName
 The base name (to which the frame number is appended) of the .bmp file.

TString captureDir
 The directory for storing the captures.

int captureEvery
 The capture Frequency.

int captureLimit
 The number of captures to perform.

volatile bool capture
 Boolean indicating that screen captures should be performed.


Detailed Description

The updater reads new screens from the pipe and writes them to the BitmapScreen. Depending on capture settings the bitmap is also written to disk.

Definition at line 24 of file UpdateHandler.h.


Constructor & Destructor Documentation

UpdateHandler::UpdateHandler ScreenPipe   s,
BitmapScreen   b,
HWND    h
 

Takes a reference to a ScreenPipe and BitmapScreen and initialises the object.

Parameters:
s  : The screen pipe object
b  : The bitmap screen object
h  : The Handle the window

Definition at line 21 of file UpdateHandler.cpp.

References capture, captureEvery, captureLimit, captureMutex, captureName, hUpdateThread, pipe, and screen.

00022 :hWnd(h)
00023 {
00024     pipe = &s;
00025     screen = &b;
00026     hUpdateThread = NULL;
00027 
00028     captureName = TEXT("ScreenCap_");
00029     captureEvery = 1;
00030     captureLimit = 50;
00031     capture = false;
00032 
00033     captureMutex = CreateMutex(NULL, false, NULL);
00034 }

UpdateHandler::~UpdateHandler   [virtual]
 

Object Destructor

Definition at line 39 of file UpdateHandler.cpp.

References captureMutex, hUpdateThread, and stop().

00040 {
00041     stop(true);
00042     CloseHandle(captureMutex);
00043     CloseHandle(hUpdateThread);
00044 }


Member Function Documentation

bool UpdateHandler::run  
 

Starts the update thread

Definition at line 53 of file UpdateHandler.cpp.

References exit, hUpdateThread, and update().

Referenced by Cls_OnCommand().

00054 {
00055     DWORD exitCode;
00056     GetExitCodeThread(hUpdateThread, &exitCode);
00057 
00058     if( exitCode == STILL_ACTIVE )
00059         return true;
00060 
00061     exit = false;
00062 
00063     CloseHandle(hUpdateThread);
00064 
00065     hUpdateThread = chBEGINTHREADEX(
00066         NULL,           // Default Security, non inheritable
00067         0,              // use default stack size  
00068         &update,        // thread function
00069         this,           // thread argument
00070         0,              // use default creation flags 
00071         NULL);          // don't need thread identifier
00072     
00073     if(hUpdateThread == NULL){
00074         return false;
00075     }
00076 
00077     return true;
00078 }

void UpdateHandler::stop const bool    force = true
 

Stops the update thread

Definition at line 83 of file UpdateHandler.cpp.

References exit, and hUpdateThread.

Referenced by Cls_OnCommand(), and ~UpdateHandler().

00084 {
00085     exit = true;    // Signal thread to stop
00086 
00087     DWORD exitCode;
00088     GetExitCodeThread(hUpdateThread, &exitCode);
00089 
00090     // If still active after a second terminate the thread
00091     if( force && exitCode == STILL_ACTIVE ){
00092         if( WaitForSingleObject(hUpdateThread, 1000) == WAIT_TIMEOUT )
00094             TerminateThread(hUpdateThread, 0);
00095     }
00096 
00097 }

DWORD WINAPI UpdateHandler::update LPVOID    param [static, private]
 

The update thread. Takes possesion of the capture Mutex and loops the reading of a screen buffer and possible capture of the image until told to quit.

Definition at line 104 of file UpdateHandler.cpp.

References capture, captureDir, captureEvery, captureLimit, captureMutex, captureName, BitmapScreen::DoneReading(), BitmapScreen::DoneWriting(), exit, BitmapScreen::getReadBuffer(), BitmapScreen::getWriteBuffer(), hWnd, VScreen::invalidate(), pipe, ScreenPipe::readToDIB(), DIBitmap::saveFile(), screen, SP_ERROR_CREATING_BITMAP, SP_INVALID_DATA, SP_NOT_CONNECTED, SP_SUCCESS, SP_UNKNOWN_ERROR, and update().

Referenced by run(), and update().

00105 {
00106     UpdateHandler* update = (UpdateHandler*) param;
00107 
00108     WaitForSingleObject(update->captureMutex, INFINITE);
00109 
00110     if( !(!update->captureDir) ){ // not empty
00111         if( !SetCurrentDirectory(update->captureDir) )
00112             update->capture = false;
00113     }
00114 
00115     if( !update->captureName ){ // is empty
00116         update->capture = false;
00117     }
00118     
00119     int screenNum = -1;  // screenNo incremented before use
00120     int captureNum = 1;
00121     bool success;
00122     ULARGE_INTEGER freeBytes, notNeeded;
00123 
00124     DIBitmap *writeBuffer;
00125     const DIBitmap *readBuffer;
00126 
00127     DWORD readResult;
00128     while(!update->exit){
00129 
00130         writeBuffer = update->screen->getWriteBuffer();
00131         
00132         readResult = update->pipe->readToDIB(*writeBuffer);
00133 
00134         update->screen->DoneWriting();
00135 
00136         switch(readResult){
00137             case SP_SUCCESS:
00138                 break;
00139             case SP_UNKNOWN_ERROR:
00140             case SP_ERROR_CREATING_BITMAP:
00141             case SP_NOT_CONNECTED:
00142             case SP_INVALID_DATA:
00143             default:
00144                 return false;   // Abandons mutex
00145         }
00146 
00147         update->screen->invalidate(update->hWnd);
00148         
00149         // restart loop if signaled to exit
00150         if(update->exit)
00151             continue;
00152         
00153         screenNum++;
00154 
00155         // Check that we wish to capture this screen
00156         if( !update->capture || !((screenNum % update->captureEvery) == 0) )
00157             continue;
00158         
00159         // Check we haven't reached capture limit;
00160         if( update->captureLimit != 0 && captureNum > update->captureLimit ){
00161             update->capture = false;
00162             continue;
00163         }
00164 
00165         readBuffer = update->screen->getReadBuffer();
00166 
00167         // Write buffer to disk   
00168         success = readBuffer->saveFile(
00169             update->captureName + TString(screenNum) + TString(TEXT(".bmp")) ); 
00170         
00171         update->screen->DoneReading();
00172 
00173         captureNum++;
00174 
00175         if( !success ){
00176             GetDiskFreeSpaceEx(NULL, &freeBytes, &notNeeded, NULL);
00177             if(freeBytes.QuadPart < 1024 * 1024){ // less than a megabyte
00178                 update->capture = false; // Stop capturing
00179             }
00180         }
00181 
00182     }
00183 
00184     ReleaseMutex(update->captureMutex);
00185 
00186     return true;
00187 }


The documentation for this class was generated from the following files:
Generated on Mon Mar 25 06:30:00 2002 for Window Dressing by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001