2004-04-11 14:58:08 +00:00
|
|
|
/*
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
|
|
|
* Copyright (C) 2004 Chris Schoeneman
|
|
|
|
*
|
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* found in the file COPYING that should have accompanied this file.
|
|
|
|
*
|
|
|
|
* This package is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "COSXClipboard.h"
|
2004-05-15 19:41:46 +00:00
|
|
|
#include "COSXClipboardUTF16Converter.h"
|
|
|
|
#include "COSXClipboardTextConverter.h"
|
|
|
|
#include "CLog.h"
|
|
|
|
#include "XArch.h"
|
2004-04-11 14:58:08 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// COSXClipboard
|
|
|
|
//
|
|
|
|
|
2004-05-15 19:41:46 +00:00
|
|
|
COSXClipboard::COSXClipboard() :
|
|
|
|
m_time(0),
|
|
|
|
m_scrap(NULL)
|
2004-04-11 14:58:08 +00:00
|
|
|
{
|
2004-05-15 19:41:46 +00:00
|
|
|
m_converters.push_back(new COSXClipboardUTF16Converter);
|
|
|
|
m_converters.push_back(new COSXClipboardTextConverter);
|
2004-04-11 14:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
COSXClipboard::~COSXClipboard()
|
|
|
|
{
|
2004-05-15 19:41:46 +00:00
|
|
|
clearConverters();
|
2004-04-11 14:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
COSXClipboard::empty()
|
|
|
|
{
|
2004-05-15 19:41:46 +00:00
|
|
|
LOG((CLOG_DEBUG "empty clipboard"));
|
|
|
|
assert(m_scrap != NULL);
|
|
|
|
|
|
|
|
OSStatus err = ClearScrap(&m_scrap);
|
2007-06-17 11:19:18 +00:00
|
|
|
if (err != noErr) {
|
|
|
|
LOG((CLOG_DEBUG "failed to grab clipboard"));
|
|
|
|
return false;
|
|
|
|
}
|
2004-05-15 19:41:46 +00:00
|
|
|
|
2007-06-17 11:19:18 +00:00
|
|
|
// we own the clipboard
|
2004-05-15 19:41:46 +00:00
|
|
|
err = PutScrapFlavor(
|
|
|
|
m_scrap,
|
|
|
|
getOwnershipFlavor(),
|
|
|
|
kScrapFlavorMaskNone,
|
|
|
|
0,
|
2007-06-17 11:19:18 +00:00
|
|
|
0);
|
2004-05-15 19:41:46 +00:00
|
|
|
if (err != noErr) {
|
|
|
|
LOG((CLOG_DEBUG "failed to grab clipboard"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-04-11 14:58:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-05-15 19:41:46 +00:00
|
|
|
COSXClipboard::add(EFormat format, const CString & data)
|
2004-04-11 14:58:08 +00:00
|
|
|
{
|
2004-05-15 19:41:46 +00:00
|
|
|
LOG((CLOG_DEBUG "add %d bytes to clipboard format: %d", data.size(), format));
|
|
|
|
|
|
|
|
for (ConverterList::const_iterator index = m_converters.begin();
|
|
|
|
index != m_converters.end(); ++index) {
|
|
|
|
|
|
|
|
IOSXClipboardConverter* converter = *index;
|
|
|
|
|
|
|
|
// skip converters for other formats
|
|
|
|
if (converter->getFormat() == format) {
|
|
|
|
CString osXData = converter->fromIClipboard(data);
|
|
|
|
ScrapFlavorType flavorType = converter->getOSXFormat();
|
|
|
|
|
|
|
|
PutScrapFlavor(
|
|
|
|
m_scrap,
|
|
|
|
flavorType,
|
|
|
|
kScrapFlavorMaskNone,
|
|
|
|
osXData.size(),
|
|
|
|
osXData.data());
|
|
|
|
}
|
|
|
|
}
|
2004-04-11 14:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2004-05-15 19:41:46 +00:00
|
|
|
COSXClipboard::open(Time time) const
|
2004-04-11 14:58:08 +00:00
|
|
|
{
|
2004-05-15 19:41:46 +00:00
|
|
|
LOG((CLOG_DEBUG "open clipboard"));
|
|
|
|
m_time = time;
|
|
|
|
OSStatus err = GetCurrentScrap(&m_scrap);
|
|
|
|
return (err == noErr);
|
2004-04-11 14:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
COSXClipboard::close() const
|
|
|
|
{
|
2004-05-15 19:41:46 +00:00
|
|
|
LOG((CLOG_DEBUG "close clipboard"));
|
|
|
|
m_scrap = NULL;
|
2004-04-11 14:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IClipboard::Time
|
|
|
|
COSXClipboard::getTime() const
|
|
|
|
{
|
2004-05-15 19:41:46 +00:00
|
|
|
return m_time;
|
2004-04-11 14:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2004-05-15 19:41:46 +00:00
|
|
|
COSXClipboard::has(EFormat format) const
|
2004-04-11 14:58:08 +00:00
|
|
|
{
|
2004-05-15 19:41:46 +00:00
|
|
|
assert(m_scrap != NULL);
|
|
|
|
|
|
|
|
for (ConverterList::const_iterator index = m_converters.begin();
|
|
|
|
index != m_converters.end(); ++index) {
|
|
|
|
IOSXClipboardConverter* converter = *index;
|
|
|
|
if (converter->getFormat() == format) {
|
|
|
|
ScrapFlavorFlags flags;
|
|
|
|
ScrapFlavorType type = converter->getOSXFormat();
|
|
|
|
|
|
|
|
if (GetScrapFlavorFlags(m_scrap, type, &flags) == noErr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-11 14:58:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CString
|
2004-05-15 19:41:46 +00:00
|
|
|
COSXClipboard::get(EFormat format) const
|
|
|
|
{
|
|
|
|
CString result;
|
|
|
|
|
|
|
|
// find the converter for the first clipboard format we can handle
|
|
|
|
IOSXClipboardConverter* converter = NULL;
|
|
|
|
for (ConverterList::const_iterator index = m_converters.begin();
|
|
|
|
index != m_converters.end(); ++index) {
|
|
|
|
converter = *index;
|
|
|
|
|
|
|
|
ScrapFlavorFlags flags;
|
|
|
|
ScrapFlavorType type = converter->getOSXFormat();
|
|
|
|
|
|
|
|
if (converter->getFormat() == format &&
|
|
|
|
GetScrapFlavorFlags(m_scrap, type, &flags) == noErr) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
converter = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if no converter then we don't recognize any formats
|
|
|
|
if (converter == NULL) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the clipboard data.
|
|
|
|
char* buffer = NULL;
|
|
|
|
try {
|
|
|
|
Size flavorSize;
|
|
|
|
OSStatus err = GetScrapFlavorSize(m_scrap,
|
|
|
|
converter->getOSXFormat(), &flavorSize);
|
|
|
|
if (err != noErr) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = new char[flavorSize];
|
|
|
|
if (buffer == NULL) {
|
|
|
|
throw memFullErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = GetScrapFlavorData(m_scrap,
|
|
|
|
converter->getOSXFormat(), &flavorSize, buffer);
|
|
|
|
if (err != noErr) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = CString(buffer, flavorSize);
|
|
|
|
}
|
|
|
|
catch (OSStatus err) {
|
|
|
|
LOG((CLOG_DEBUG "exception thrown in COSXClipboard::get MacError (%d)", err));
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
LOG((CLOG_DEBUG "unknown exception in COSXClipboard::get"));
|
|
|
|
RETHROW_XTHREAD
|
|
|
|
}
|
|
|
|
delete[] buffer;
|
|
|
|
|
|
|
|
return converter->toIClipboard(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
COSXClipboard::clearConverters()
|
|
|
|
{
|
|
|
|
for (ConverterList::iterator index = m_converters.begin();
|
|
|
|
index != m_converters.end(); ++index) {
|
|
|
|
delete *index;
|
|
|
|
}
|
|
|
|
m_converters.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
COSXClipboard::isOwnedBySynergy()
|
|
|
|
{
|
|
|
|
ScrapFlavorFlags flags;
|
|
|
|
ScrapRef scrap;
|
|
|
|
OSStatus err = GetCurrentScrap(&scrap);
|
|
|
|
if (err == noErr) {
|
|
|
|
err = GetScrapFlavorFlags(scrap, getOwnershipFlavor() , &flags);
|
|
|
|
}
|
|
|
|
return (err == noErr);
|
|
|
|
}
|
|
|
|
|
|
|
|
ScrapFlavorType
|
|
|
|
COSXClipboard::getOwnershipFlavor()
|
2004-04-11 14:58:08 +00:00
|
|
|
{
|
2004-05-15 19:41:46 +00:00
|
|
|
return 'Syne';
|
2004-04-11 14:58:08 +00:00
|
|
|
}
|