blob: 8551207d29bc73db43a1d9b54b572029f170778c [file] [log] [blame]
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +00001/* Copyright (C) 2003-2006 Constantin Kaplinsky. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19#include <rfb/CapsContainer.h>
20
21using namespace rfb;
22
23//
24// The constructor.
25//
26
27CapsContainer::CapsContainer(int maxCaps)
28{
29 m_maxSize = maxCaps;
30 m_listSize = 0;
31 m_plist = new rdr::U32[m_maxSize];
32}
33
34//
35// The destructor.
36//
37
38CapsContainer::~CapsContainer()
39{
40 delete[] m_plist;
41
42 // Remove char[] strings allocated by the new[] operator.
43 std::map<rdr::U32,char*>::const_iterator iter;
44 for (iter = m_descMap.begin(); iter != m_descMap.end(); iter++) {
45 delete[] iter->second;
46 }
47}
48
49//
50// Add information about a particular capability into the object. There are
51// two functions to perform this task. These functions overwrite capability
52// records with the same code.
53//
54
55void
56CapsContainer::add(rdr::U32 code, const char *vendor, const char *name,
57 const char *desc)
58{
59 // Fill in an rfbCapabilityInfo structure and pass it to the overloaded
60 // function.
61 CapabilityInfo capinfo;
62 capinfo.code = code;
63 memcpy(capinfo.vendorSignature, vendor, 4);
64 memcpy(capinfo.nameSignature, name, 8);
65 add(&capinfo, desc);
66}
67
68void
69CapsContainer::add(const CapabilityInfo *capinfo, const char *desc)
70{
71 m_infoMap[capinfo->code] = *capinfo;
72 m_enableMap[capinfo->code] = false;
73
74 if (isKnown(capinfo->code)) {
75 delete[] m_descMap[capinfo->code];
76 }
77 char *desc_copy = 0;
78 if (desc != 0) {
79 desc_copy = new char[strlen(desc) + 1];
80 strcpy(desc_copy, desc);
81 }
82 m_descMap[capinfo->code] = desc_copy;
83}
84
85//
86// Check if a capability with the specified code was added earlier.
87//
88
89bool
90CapsContainer::isKnown(rdr::U32 code) const
91{
92 return (m_descMap.find(code) != m_descMap.end());
93}
94
95//
96// Fill in a rfbCapabilityInfo structure with contents corresponding to the
97// specified code. Returns true on success, false if the specified code is
98// not known.
99//
100
101bool
102CapsContainer::getInfo(rdr::U32 code, CapabilityInfo *capinfo) const
103{
104 if (isKnown(code)) {
105 *capinfo = m_infoMap.find(code)->second;
106 return true;
107 }
108
109 return false;
110}
111
112//
113// Get a description string for the specified capability code. Returns 0
114// either if the code is not known, or if there is no description for this
115// capability.
116//
117
118char *
119CapsContainer::getDescription(rdr::U32 code) const
120{
121 return (isKnown(code)) ? m_descMap.find(code)->second : 0;
122}
123
124//
125// Mark the specified capability as "enabled". This function checks "vendor"
126// and "name" signatures in the existing record and in the argument structure
127// and enables the capability only if both records are the same.
128//
129
130bool
131CapsContainer::enable(const CapabilityInfo *capinfo)
132{
133 if (!isKnown(capinfo->code))
134 return false;
135
136 const CapabilityInfo *known = &(m_infoMap[capinfo->code]);
137 if ( memcmp(known->vendorSignature, capinfo->vendorSignature, 4) != 0 ||
138 memcmp(known->nameSignature, capinfo->nameSignature, 8) != 0 ) {
139 m_enableMap[capinfo->code] = false;
140 return false;
141 }
142
143 m_enableMap[capinfo->code] = true;
144 if (m_listSize < m_maxSize) {
145 m_plist[m_listSize++] = capinfo->code;
146 }
147 return true;
148}
149
150//
151// Check if the specified capability is known and enabled.
152//
153
154bool
155CapsContainer::isEnabled(rdr::U32 code) const
156{
157 return (isKnown(code)) ? m_enableMap.find(code)->second : false;
158}
159
160//
161// Return the capability code at the specified index.
162// If the index is not valid, return 0.
163//
164
165rdr::U32
166CapsContainer::getByOrder(int idx) const
167{
168 return (idx < m_listSize) ? m_plist[idx] : 0;
169}
170