| 1 | /*
|
|---|
| 2 | * synergy -- mouse and keyboard sharing utility
|
|---|
| 3 | * Copyright (C) 2002 Chris Schoeneman
|
|---|
| 4 | *
|
|---|
| 5 | * This package is free software you can redistribute it and/or
|
|---|
| 6 | * modify it under the terms of the GNU General Public License
|
|---|
| 7 | * found in the file COPYING that should have accompanied this file.
|
|---|
| 8 | *
|
|---|
| 9 | * This package is distributed in the hope that it will be useful,
|
|---|
| 10 | * but WITHOUT ANY WARRANTY without even the implied warranty of
|
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 12 | * GNU General Public License for more details.
|
|---|
| 13 | */
|
|---|
| 14 |
|
|---|
| 15 | // avoid getting a lot a crap from mmsystem.h that we don't need
|
|---|
| 16 | #define MMNODRV // Installable driver support
|
|---|
| 17 | #define MMNOSOUND // Sound support
|
|---|
| 18 | #define MMNOWAVE // Waveform support
|
|---|
| 19 | #define MMNOMIDI // MIDI support
|
|---|
| 20 | #define MMNOAUX // Auxiliary audio support
|
|---|
| 21 | #define MMNOMIXER // Mixer support
|
|---|
| 22 | #define MMNOJOY // Joystick support
|
|---|
| 23 | #define MMNOMCI // MCI support
|
|---|
| 24 | #define MMNOMMIO // Multimedia file I/O support
|
|---|
| 25 | #define MMNOMMSYSTEM // General MMSYSTEM functions
|
|---|
| 26 |
|
|---|
| 27 | #define WIN32_LEAN_AND_MEAN
|
|---|
| 28 |
|
|---|
| 29 | #include "CArchTimeWindows.h"
|
|---|
| 30 | #include <windows.h>
|
|---|
| 31 | #include <mmsystem.h>
|
|---|
| 32 |
|
|---|
| 33 | typedef WINMMAPI DWORD (WINAPI *PTimeGetTime)(void);
|
|---|
| 34 |
|
|---|
| 35 | static double s_freq = 0.0;
|
|---|
| 36 | static HINSTANCE s_mmInstance = NULL;
|
|---|
| 37 | static PTimeGetTime s_tgt = NULL;
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 | //
|
|---|
| 41 | // CArchTimeWindows
|
|---|
| 42 | //
|
|---|
| 43 |
|
|---|
| 44 | CArchTimeWindows::CArchTimeWindows()
|
|---|
| 45 | {
|
|---|
| 46 | assert(s_freq == 0.0 || s_mmInstance == NULL);
|
|---|
| 47 |
|
|---|
| 48 | LARGE_INTEGER freq;
|
|---|
| 49 | if (QueryPerformanceFrequency(&freq) && freq.QuadPart != 0) {
|
|---|
| 50 | s_freq = 1.0 / static_cast<double>(freq.QuadPart);
|
|---|
| 51 | }
|
|---|
| 52 | else {
|
|---|
| 53 | // load winmm.dll and get timeGetTime
|
|---|
| 54 | s_mmInstance = LoadLibrary("winmm");
|
|---|
| 55 | if (s_mmInstance != NULL) {
|
|---|
| 56 | s_tgt = (PTimeGetTime)GetProcAddress(s_mmInstance, "timeGetTime");
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | CArchTimeWindows::~CArchTimeWindows()
|
|---|
| 62 | {
|
|---|
| 63 | s_freq = 0.0;
|
|---|
| 64 | if (s_mmInstance == NULL) {
|
|---|
| 65 | FreeLibrary(reinterpret_cast<HMODULE>(s_mmInstance));
|
|---|
| 66 | s_tgt = NULL;
|
|---|
| 67 | s_mmInstance = NULL;
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | double
|
|---|
| 72 | CArchTimeWindows::time()
|
|---|
| 73 | {
|
|---|
| 74 | // get time. we try three ways, in order of descending precision
|
|---|
| 75 | if (s_freq != 0.0) {
|
|---|
| 76 | LARGE_INTEGER c;
|
|---|
| 77 | QueryPerformanceCounter(&c);
|
|---|
| 78 | return s_freq * static_cast<double>(c.QuadPart);
|
|---|
| 79 | }
|
|---|
| 80 | else if (s_tgt != NULL) {
|
|---|
| 81 | return 0.001 * static_cast<double>(s_tgt());
|
|---|
| 82 | }
|
|---|
| 83 | else {
|
|---|
| 84 | return 0.001 * static_cast<double>(GetTickCount());
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|