blob: a64a52486fa43974c887c9e1861a5fedea4390a0 [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.
Constantin Kaplinsky56b7fa12006-09-12 05:15:44 +000021// Member variables and functions are documented in CapsContainer.h.
Constantin Kaplinskyce0907d2006-09-08 12:55:37 +000022//
23
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +000024#include <rfb/CapsContainer.h>
25
26using namespace rfb;
27
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +000028CapsContainer::CapsContainer(int maxCaps)
Constantin Kaplinsky18076a92006-06-04 01:59:54 +000029: m_maxSize(maxCaps), m_listSize(0), m_plist(new rdr::U32[m_maxSize])
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +000030{
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +000031}
32
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +000033CapsContainer::~CapsContainer()
34{
35 delete[] m_plist;
36
37 // Remove char[] strings allocated by the new[] operator.
38 std::map<rdr::U32,char*>::const_iterator iter;
39 for (iter = m_descMap.begin(); iter != m_descMap.end(); iter++) {
40 delete[] iter->second;
41 }
42}
43
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +000044void
45CapsContainer::add(rdr::U32 code, const char *vendor, const char *name,
Constantin Kaplinskyad9dc712006-06-03 14:17:43 +000046 const char *desc)
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +000047{
48 // Fill in an rfbCapabilityInfo structure and pass it to the overloaded
49 // function.
50 CapabilityInfo capinfo;
51 capinfo.code = code;
52 memcpy(capinfo.vendorSignature, vendor, 4);
53 memcpy(capinfo.nameSignature, name, 8);
54 add(&capinfo, desc);
55}
56
57void
58CapsContainer::add(const CapabilityInfo *capinfo, const char *desc)
59{
60 m_infoMap[capinfo->code] = *capinfo;
61 m_enableMap[capinfo->code] = false;
62
63 if (isKnown(capinfo->code)) {
64 delete[] m_descMap[capinfo->code];
65 }
66 char *desc_copy = 0;
67 if (desc != 0) {
68 desc_copy = new char[strlen(desc) + 1];
69 strcpy(desc_copy, desc);
70 }
71 m_descMap[capinfo->code] = desc_copy;
72}
73
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +000074bool
75CapsContainer::isKnown(rdr::U32 code) const
76{
77 return (m_descMap.find(code) != m_descMap.end());
78}
79
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +000080bool
81CapsContainer::getInfo(rdr::U32 code, CapabilityInfo *capinfo) const
82{
83 if (isKnown(code)) {
84 *capinfo = m_infoMap.find(code)->second;
85 return true;
86 }
87
88 return false;
89}
90
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +000091char *
92CapsContainer::getDescription(rdr::U32 code) const
93{
94 return (isKnown(code)) ? m_descMap.find(code)->second : 0;
95}
96
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +000097bool
98CapsContainer::enable(const CapabilityInfo *capinfo)
99{
100 if (!isKnown(capinfo->code))
101 return false;
102
103 const CapabilityInfo *known = &(m_infoMap[capinfo->code]);
104 if ( memcmp(known->vendorSignature, capinfo->vendorSignature, 4) != 0 ||
105 memcmp(known->nameSignature, capinfo->nameSignature, 8) != 0 ) {
106 m_enableMap[capinfo->code] = false;
107 return false;
108 }
109
110 m_enableMap[capinfo->code] = true;
111 if (m_listSize < m_maxSize) {
112 m_plist[m_listSize++] = capinfo->code;
113 }
114 return true;
115}
116
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +0000117bool
118CapsContainer::isEnabled(rdr::U32 code) const
119{
120 return (isKnown(code)) ? m_enableMap.find(code)->second : false;
121}
122
Constantin Kaplinsky73fdc8c2006-06-03 13:18:37 +0000123rdr::U32
124CapsContainer::getByOrder(int idx) const
125{
126 return (idx < m_listSize) ? m_plist[idx] : 0;
127}
128