Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

CArchTimeWindows.cpp

00001 /*
00002  * synergy -- mouse and keyboard sharing utility
00003  * Copyright (C) 2002 Chris Schoeneman
00004  * 
00005  * This package is free software you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * found in the file COPYING that should have accompanied this file.
00008  * 
00009  * This package is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  */
00014 
00015 // avoid getting a lot a crap from mmsystem.h that we don't need
00016 #define MMNODRV         // Installable driver support
00017 #define MMNOSOUND       // Sound support
00018 #define MMNOWAVE        // Waveform support
00019 #define MMNOMIDI        // MIDI support
00020 #define MMNOAUX         // Auxiliary audio support
00021 #define MMNOMIXER       // Mixer support
00022 #define MMNOJOY         // Joystick support
00023 #define MMNOMCI         // MCI support
00024 #define MMNOMMIO        // Multimedia file I/O support
00025 #define MMNOMMSYSTEM    // General MMSYSTEM functions
00026 
00027 #define WIN32_LEAN_AND_MEAN
00028 
00029 #include "CArchTimeWindows.h"
00030 #include <windows.h>
00031 #include <mmsystem.h>
00032 
00033 typedef WINMMAPI DWORD (WINAPI *PTimeGetTime)(void);
00034 
00035 static double           s_freq       = 0.0;
00036 static HINSTANCE        s_mmInstance = NULL;
00037 static PTimeGetTime     s_tgt        = NULL;
00038 
00039 
00040 //
00041 // CArchTimeWindows
00042 //
00043 
00044 CArchTimeWindows::CArchTimeWindows()
00045 {
00046     assert(s_freq == 0.0 || s_mmInstance == NULL);
00047 
00048     LARGE_INTEGER freq;
00049     if (QueryPerformanceFrequency(&freq) && freq.QuadPart != 0) {
00050         s_freq = 1.0 / static_cast<double>(freq.QuadPart);
00051     }
00052     else {
00053         // load winmm.dll and get timeGetTime
00054         s_mmInstance = LoadLibrary("winmm");
00055         if (s_mmInstance != NULL) {
00056             s_tgt = (PTimeGetTime)GetProcAddress(s_mmInstance, "timeGetTime");
00057         }
00058     }
00059 }
00060 
00061 CArchTimeWindows::~CArchTimeWindows()
00062 {
00063     s_freq = 0.0;
00064     if (s_mmInstance == NULL) {
00065         FreeLibrary(reinterpret_cast<HMODULE>(s_mmInstance));
00066         s_tgt        = NULL;
00067         s_mmInstance = NULL;
00068     }
00069 }
00070 
00071 double
00072 CArchTimeWindows::time()
00073 {
00074     // get time.  we try three ways, in order of descending precision
00075     if (s_freq != 0.0) {
00076         LARGE_INTEGER c;
00077         QueryPerformanceCounter(&c);
00078         return s_freq * static_cast<double>(c.QuadPart);
00079     }
00080     else if (s_tgt != NULL) {
00081         return 0.001 * static_cast<double>(s_tgt());
00082     }
00083     else {
00084         return 0.001 * static_cast<double>(GetTickCount());
00085     }
00086 }

Generated on Fri Nov 6 00:21:13 2009 for synergy-plus by  doxygen 1.3.9.1