blob: cea5f3aaa1a758e16cae02b7777030deb584b114 [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
Constantin Kaplinskyce0907d2006-09-08 12:55:37 +000019//
20// CapsContainer class implementation.
21// FIXME: Extend the comment.
22//
23
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +000024#include <rfb/CapsContainer.h>
25
26using namespace rfb;
27
28//
29// The constructor.
30//
31
32CapsContainer::CapsContainer(int maxCaps)
Constantin Kaplinsky18076a92006-06-04 01:59:54 +000033: m_maxSize(maxCaps), m_listSize(0), m_plist(new rdr::U32[m_maxSize])
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +000034{
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +000035}
36
37//
38// The destructor.
39//
40
41CapsContainer::~CapsContainer()
42{
43 delete[] m_plist;
44
45 // Remove char[] strings allocated by the new[] operator.
46 std::map<rdr::U32,char*>::const_iterator iter;
47 for (iter = m_descMap.begin(); iter != m_descMap.end(); iter++) {
48 delete[] iter->second;
49 }
50}
51
52//
53// Add information about a particular capability into the object. There are
54// two functions to perform this task. These functions overwrite capability
55// records with the same code.
56//
57
58void
59CapsContainer::add(rdr::U32 code, const char *vendor, const char *name,
Constantin Kaplinskyad9dc712006-06-03 14:17:43 +000060 const char *desc)
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +000061{
62 // Fill in an rfbCapabilityInfo structure and pass it to the overloaded
63 // function.
64 CapabilityInfo capinfo;
65 capinfo.code = code;
66 memcpy(capinfo.vendorSignature, vendor, 4);
67 memcpy(capinfo.nameSignature, name, 8);
68 add(&capinfo, desc);
69}
70
71void
72CapsContainer::add(const CapabilityInfo *capinfo, const char *desc)
73{
74 m_infoMap[capinfo->code] = *capinfo;
75 m_enableMap[capinfo->code] = false;
76
77 if (isKnown(capinfo->code)) {
78 delete[] m_descMap[capinfo->code];
79 }
80 char *desc_copy = 0;
81 if (desc != 0) {
82 desc_copy = new char[strlen(desc) + 1];
83 strcpy(desc_copy, desc);
84 }
85 m_descMap[capinfo->code] = desc_copy;
86}
87
88//
89// Check if a capability with the specified code was added earlier.
90//
91
92bool
93CapsContainer::isKnown(rdr::U32 code) const
94{
95 return (m_descMap.find(code) != m_descMap.end());
96}
97
98//
99// Fill in a rfbCapabilityInfo structure with contents corresponding to the
100// specified code. Returns true on success, false if the specified code is
101// not known.
102//
103
104bool
105CapsContainer::getInfo(rdr::U32 code, CapabilityInfo *capinfo) const
106{
107 if (isKnown(code)) {
108 *capinfo = m_infoMap.find(code)->second;
109 return true;
110 }
111
112 return false;
113}
114
115//
116// Get a description string for the specified capability code. Returns 0
117// either if the code is not known, or if there is no description for this
118// capability.
119//
120
121char *
122CapsContainer::getDescription(rdr::U32 code) const
123{
124 return (isKnown(code)) ? m_descMap.find(code)->second : 0;
125}
126
127//
128// Mark the specified capability as "enabled". This function checks "vendor"
129// and "name" signatures in the existing record and in the argument structure
130// and enables the capability only if both records are the same.
131//
132
133bool
134CapsContainer::enable(const CapabilityInfo *capinfo)
135{
136 if (!isKnown(capinfo->code))
137 return false;
138
139 const CapabilityInfo *known = &(m_infoMap[capinfo->code]);
140 if ( memcmp(known->vendorSignature, capinfo->vendorSignature, 4) != 0 ||
141 memcmp(known->nameSignature, capinfo->nameSignature, 8) != 0 ) {
142 m_enableMap[capinfo->code] = false;
143 return false;
144 }
145
146 m_enableMap[capinfo->code] = true;
147 if (m_listSize < m_maxSize) {
148 m_plist[m_listSize++] = capinfo->code;
149 }
150 return true;
151}
152
153//
154// Check if the specified capability is known and enabled.
155//
156
157bool
158CapsContainer::isEnabled(rdr::U32 code) const
159{
160 return (isKnown(code)) ? m_enableMap.find(code)->second : false;
161}
162
163//
164// Return the capability code at the specified index.
165// If the index is not valid, return 0.
166//
167
168rdr::U32
169CapsContainer::getByOrder(int idx) const
170{
171 return (idx < m_listSize) ? m_plist[idx] : 0;
172}
173