summaryrefslogtreecommitdiffstats
path: root/common/rfb/CapsContainer.cxx
blob: d9d2ea504daae18abac69baa649e8b1a44f12770 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* Copyright (C) 2003-2006 Constantin Kaplinsky. All Rights Reserved.
 * 
 * This 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 software 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 software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

#include <rfb/CapsContainer.h>

using namespace rfb;

//
// The constructor.
//

CapsContainer::CapsContainer(int maxCaps)
{
  m_maxSize = maxCaps;
  m_listSize = 0;
  m_plist = new rdr::U32[m_maxSize];
}

//
// The destructor.
//

CapsContainer::~CapsContainer()
{
  delete[] m_plist;

  // Remove char[] strings allocated by the new[] operator.
  std::map<rdr::U32,char*>::const_iterator iter;
  for (iter = m_descMap.begin(); iter != m_descMap.end(); iter++) {
    delete[] iter->second;
  }
}

//
// Add information about a particular capability into the object. There are
// two functions to perform this task. These functions overwrite capability
// records with the same code.
//

void
CapsContainer::add(rdr::U32 code, const char *vendor, const char *name,
                   const char *desc)
{
  // Fill in an rfbCapabilityInfo structure and pass it to the overloaded
  // function.
  CapabilityInfo capinfo;
  capinfo.code = code;
  memcpy(capinfo.vendorSignature, vendor, 4);
  memcpy(capinfo.nameSignature, name, 8);
  add(&capinfo, desc);
}

void
CapsContainer::add(const CapabilityInfo *capinfo, const char *desc)
{
  m_infoMap[capinfo->code] = *capinfo;
  m_enableMap[capinfo->code] = false;

  if (isKnown(capinfo->code)) {
    delete[] m_descMap[capinfo->code];
  }
  char *desc_copy = 0;
  if (desc != 0) {
    desc_copy = new char[strlen(desc) + 1];
    strcpy(desc_copy, desc);
  }
  m_descMap[capinfo->code] = desc_copy;
}

//
// Check if a capability with the specified code was added earlier.
//

bool
CapsContainer::isKnown(rdr::U32 code) const
{
  return (m_descMap.find(code) != m_descMap.end());
}

//
// Fill in a rfbCapabilityInfo structure with contents corresponding to the
// specified code. Returns true on success, false if the specified code is
// not known.
//

bool
CapsContainer::getInfo(rdr::U32 code, CapabilityInfo *capinfo) const
{
  if (isKnown(code)) {
    *capinfo = m_infoMap.find(code)->second;
    return true;
  }

  return false;
}

//
// Get a description string for the specified capability code. Returns 0
// either if the code is not known, or if there is no description for this
// capability.
//

char *
CapsContainer::getDescription(rdr::U32 code) const
{
  return (isKnown(code)) ? m_descMap.find(code)->second : 0;
}

//
// Mark the specified capability as "enabled". This function checks "vendor"
// and "name" signatures in the existing record and in the argument structure
// and enables the capability only if both records are the same.
//

bool
CapsContainer::enable(const CapabilityInfo *capinfo)
{
  if (!isKnown(capinfo->code))
    return false;

  const CapabilityInfo *known = &(m_infoMap[capinfo->code]);
  if ( memcmp(known->vendorSignature, capinfo->vendorSignature, 4) != 0 ||
       memcmp(known->nameSignature, capinfo->nameSignature, 8) != 0 ) {
    m_enableMap[capinfo->code] = false;
    return false;
  }

  m_enableMap[capinfo->code] = true;
  if (m_listSize < m_maxSize) {
    m_plist[m_listSize++] = capinfo->code;
  }
  return true;
}

//
// Check if the specified capability is known and enabled.
//

bool
CapsContainer::isEnabled(rdr::U32 code) const
{
  return (isKnown(code)) ? m_enableMap.find(code)->second : false;
}

//
// Return the capability code at the specified index.
// If the index is not valid, return 0.
//

rdr::U32
CapsContainer::getByOrder(int idx) const
{
  return (idx < m_listSize) ? m_plist[idx] : 0;
}