• Main Page
  • Classes
  • Files
  • File List

IClipboard.cpp

00001 /*
00002  * synergy -- mouse and keyboard sharing utility
00003  * Copyright (C) 2012 Bolton Software Ltd.
00004  * Copyright (C) 2004 Chris Schoeneman
00005  * 
00006  * This package is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * found in the file COPYING that should have accompanied this file.
00009  * 
00010  * This package is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #include "IClipboard.h"
00020 #include "stdvector.h"
00021 
00022 //
00023 // IClipboard
00024 //
00025 
00026 void
00027 IClipboard::unmarshall(IClipboard* clipboard, const CString& data, Time time)
00028 {
00029     assert(clipboard != NULL);
00030 
00031     const char* index = data.data();
00032 
00033     // clear existing data
00034     clipboard->open(time);
00035     clipboard->empty();
00036 
00037     // read the number of formats
00038     const UInt32 numFormats = readUInt32(index);
00039     index += 4;
00040 
00041     // read each format
00042     for (UInt32 i = 0; i < numFormats; ++i) {
00043         // get the format id
00044         IClipboard::EFormat format =
00045             static_cast<IClipboard::EFormat>(readUInt32(index));
00046         index += 4;
00047 
00048         // get the size of the format data
00049         UInt32 size = readUInt32(index);
00050         index += 4;
00051 
00052         // save the data if it's a known format.  if either the client
00053         // or server supports more clipboard formats than the other
00054         // then one of them will get a format >= kNumFormats here.
00055         if (format <IClipboard::kNumFormats) {
00056             clipboard->add(format, CString(index, size));
00057         }
00058         index += size;
00059     }
00060 
00061     // done
00062     clipboard->close();
00063 }
00064 
00065 CString
00066 IClipboard::marshall(const IClipboard* clipboard)
00067 {
00068     assert(clipboard != NULL);
00069 
00070     CString data;
00071 
00072     std::vector<CString> formatData;
00073     formatData.resize(IClipboard::kNumFormats);
00074     // FIXME -- use current time
00075     clipboard->open(0);
00076 
00077     // compute size of marshalled data
00078     UInt32 size = 4;
00079     UInt32 numFormats = 0;
00080     for (UInt32 format = 0; format != IClipboard::kNumFormats; ++format) {
00081         if (clipboard->has(static_cast<IClipboard::EFormat>(format))) {
00082             ++numFormats;
00083             formatData[format] =
00084                 clipboard->get(static_cast<IClipboard::EFormat>(format));
00085             size += 4 + 4 + (UInt32)formatData[format].size();
00086         }
00087     }
00088 
00089     // allocate space
00090     data.reserve(size);
00091 
00092     // marshall the data
00093     writeUInt32(&data, numFormats);
00094     for (UInt32 format = 0; format != IClipboard::kNumFormats; ++format) {
00095         if (clipboard->has(static_cast<IClipboard::EFormat>(format))) {
00096             writeUInt32(&data, format);
00097             writeUInt32(&data, (UInt32)formatData[format].size());
00098             data += formatData[format];
00099         }
00100     }
00101     clipboard->close();
00102 
00103     return data;
00104 }
00105 
00106 bool
00107 IClipboard::copy(IClipboard* dst, const IClipboard* src)
00108 {
00109     assert(dst != NULL);
00110     assert(src != NULL);
00111 
00112     return copy(dst, src, src->getTime());
00113 }
00114 
00115 bool
00116 IClipboard::copy(IClipboard* dst, const IClipboard* src, Time time)
00117 {
00118     assert(dst != NULL);
00119     assert(src != NULL);
00120 
00121     bool success = false;
00122     if (src->open(time)) {
00123         if (dst->open(time)) {
00124             if (dst->empty()) {
00125                 for (SInt32 format = 0;
00126                                 format != IClipboard::kNumFormats; ++format) {
00127                     IClipboard::EFormat eFormat = (IClipboard::EFormat)format;
00128                     if (src->has(eFormat)) {
00129                         dst->add(eFormat, src->get(eFormat));
00130                     }
00131                 }
00132                 success = true;
00133             }
00134             dst->close();
00135         }
00136         src->close();
00137     }
00138 
00139     return success;
00140 }
00141 
00142 UInt32
00143 IClipboard::readUInt32(const char* buf)
00144 {
00145     const unsigned char* ubuf = reinterpret_cast<const unsigned char*>(buf);
00146     return  (static_cast<UInt32>(ubuf[0]) << 24) |
00147             (static_cast<UInt32>(ubuf[1]) << 16) |
00148             (static_cast<UInt32>(ubuf[2]) <<  8) |
00149              static_cast<UInt32>(ubuf[3]);
00150 }
00151 
00152 void
00153 IClipboard::writeUInt32(CString* buf, UInt32 v)
00154 {
00155     *buf += static_cast<UInt8>((v >> 24) & 0xff);
00156     *buf += static_cast<UInt8>((v >> 16) & 0xff);
00157     *buf += static_cast<UInt8>((v >>  8) & 0xff);
00158     *buf += static_cast<UInt8>( v        & 0xff);
00159 }

Generated on Fri May 24 2013 00:00:04 for Synergy by  doxygen 1.7.1