Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

IClipboard.cpp

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

Generated on Fri Nov 6 00:21:15 2009 for synergy-plus by  doxygen 1.3.9.1