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