summaryrefslogtreecommitdiff
path: root/common/vdcommon.h
blob: ac58efe5a02d0f2ac452a4382336c9e60c253c2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
   Copyright (C) 2009 Red Hat, Inc.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of
   the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _H_VDCOMMON
#define _H_VDCOMMON

#if !defined __GNUC__
#pragma warning(disable:4200)
#endif

#include <errno.h>
#include <windows.h>
#include "spice/vd_agent.h"
#include "vdlog.h"

class Mutex {
public:
    Mutex() {
        InitializeCriticalSection(&_crit);
    }
    ~Mutex() {
        DeleteCriticalSection(&_crit);
    }
    void lock() {
        EnterCriticalSection(&_crit);
    }
    void unlock() {
        LeaveCriticalSection(&_crit);
    }
private:
    CRITICAL_SECTION _crit;
    // no copy
    Mutex(const Mutex&);
    void operator=(const Mutex&);
};

class MutexLocker {
public:
    MutexLocker(Mutex &mtx):_mtx(mtx) {
        _mtx.lock();
    }
    ~MutexLocker() {
        _mtx.unlock();
    }
private:
    Mutex &_mtx;
    // no copy
    MutexLocker(const MutexLocker&);
    void operator=(const MutexLocker&);
};
typedef Mutex mutex_t;

#define VD_AGENT_REGISTRY_KEY "SOFTWARE\\Red Hat\\Spice\\vdagent\\"
#define VD_AGENT_STOP_EVENT   TEXT("Global\\vdagent_stop_event")

/*
 * Note: OLDMSVCRT, which is defined (in the Makefile) for mingw builds, and
 * is not defined for Visual Studio builds.
 *
 * On Windows XP some those functions are missing from the msvcrt.dll
 * When compiled with mingw, the program fails to run due to missing functions.
 * One can link to a newer runtime dll, e.g. msvcr100.dll, but that would
 * require installing that DLL on the guest. That can be done by downloading
 * and installing Microsoft Visual C++ 2010 Redistributable Package.
 * (same for 110.dll and 2012 Redistributable Package, etc).
 *
 * Since we do not want to add this dependency, we use functions that are
 * available in msvcrt.dll (and use define in the code).
 *
 * Currently Visual Studio builds are built with /MT (static mode) such that
 * those functions are not required to be in that dll on the guest.
 */
#ifdef OLDMSVCRT
#ifndef _ftime_s
#define _ftime_s(timeb) _ftime(timeb)
#endif
#endif /* OLDMSVCRT */

#ifdef _MSC_VER // compiling with Visual Studio
#define HAVE_STRCAT_S 1
#define HAVE_STRCPY_S 1
#define HAVE_SWPRINTF_S 1
#endif

#ifdef HAVE_STRCAT_S
#define vdagent_strcat_s strcat_s
#else
errno_t vdagent_strcat_s(char *strDestination,
                         size_t numberOfElements,
                         const char *strSource);
#endif

#ifdef HAVE_STRCPY_S
#define vdagent_strcpy_s strcpy_s
#else
errno_t vdagent_strcpy_s(char *strDestination,
                         size_t numberOfElements,
                         const char *strSource);
#endif

#ifndef HAVE_SWPRINTF_S
int vdagent_swprintf_s(wchar_t *buf, size_t len, const wchar_t *format, ...);
#define swprintf_s vdagent_swprintf_s
#endif

#ifdef _MSC_VER // compiling with Visual Studio
#define snprintf         sprintf_s
#define sscanf           sscanf_s
#endif

enum SystemVersion {
    SYS_VER_UNSUPPORTED,
    SYS_VER_WIN_XP_CLASS, // also Server 2003/R2
    SYS_VER_WIN_7_CLASS,  // also Windows 8, Server 2012, Server 2008/R2 & Vista
};

SystemVersion supported_system_version();

#endif