Debian Codegraph @

libSoXt/src/SoXtClipboard.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*
38 * Copyright (C) 1990-95 Silicon Graphics, Inc.
39 *
40 _______________________________________________________________________
41 ______________ S I L I C O N G R A P H I C S I N C . ____________
42 |
43 | $Revision: 1.4 $
44 |
45 | Classes : SoXtClipboard
46 |
47 | Author(s) : David Mott
48 |
49 ______________ S I L I C O N G R A P H I C S I N C . ____________
50 _______________________________________________________________________
51 */
52
53 #include <sys/types.h>
54 #include <sys/stat.h>
55 #include <fcntl.h>
56 #include <cstdlib>
57 #include <unistd.h>
58
59 #include <Inventor/SbDict.h>
60 #include <Inventor/misc/SoByteStream.h>
61 #include <Inventor/SoDB.h>
62 #include <Inventor/SoInput.h>
63 #include <Inventor/SoPath.h>
64 #include <Inventor/Xt/SoXtClipboard.h>
65
66 #include <Inventor/errors/SoDebugError.h>
67 #include "SoXtImportInterest.h"
68 #include <Xm/AtomMgr.h>
69 #include <X11/StringDefs.h>
70
71 // This list will contain SoXtClipboard pointers.
72 // The key into this dictionary is an X selection (e.g. XA_PRIMARY),
73 // and the return value is which SoXtClipboard owns that selection, if any.
74 // We need this because our import/export functions are static, and
75 // thus are not tied to any one SoXtClipboard class instance.
76 SbDict *SoXtClipboard::selOwnerList = NULL;
77
78 #define _ATOM_(widget,name) (XmInternAtom(XtDisplay(widget),name,False))
79
80 ////////////////////////////////////////////////////////////////////////
81 //
82 // Description:
83 // Constructor
84 //
85 SoXtClipboard::SoXtClipboard(
86 Widget w, // widget for copy/paste data transfers
87 Atom selectionAtom) // the x selection: XA_SECONDARY, etc.
88 //
89 ////////////////////////////////////////////////////////////////////////
90 {
91 if (selOwnerList == NULL)
92 selOwnerList = new SbDict;
93
94 // _XA_CLIPBOARD_ lets us know to use the X internal atom "CLIPBOARD".
95 if (selectionAtom == _XA_CLIPBOARD_) {
96 selectionAtom = XmInternAtom(XtDisplay(w), "CLIPBOARD", False);
97 }
98
99 widget = w;
100 clipboardAtom = selectionAtom;
101 eventTime = CurrentTime;
102
103 // copyBuffer is used to transfer copy/paste data in binary format.
104 copyBuffer = NULL;
105
106 // Default paste interest.
107 pasteInterest = new SoXtImportInterestList;
108 pasteInterest->append(_ATOM_(widget, "INVENTOR_2_1"), pasteImportCB, this);
109 pasteInterest->append(_ATOM_(widget, "INVENTOR_2_1_FILE"), pasteImportCB, this);
110 pasteInterest->append(_ATOM_(widget, "VRML_1_0"), pasteImportCB, this);
111 pasteInterest->append(_ATOM_(widget, "VRML_1_0_FILE"), pasteImportCB, this);
112 pasteInterest->append(_ATOM_(widget, "INVENTOR"), pasteImportCB, this);
113 pasteInterest->append(_ATOM_(widget, "INVENTOR_FILE"), pasteImportCB, this);
114 pasteInterest->append(_ATOM_(widget, "INVENTOR_2_0"), pasteImportCB, this);
115 pasteInterest->append(_ATOM_(widget, "INVENTOR_2_0_FILE"), pasteImportCB, this);
116 pasteInterest->append(XA_STRING, pasteImportCB, this);
117
118 // Default copy interest
119 copyInterest = new SbPList();
120 copyInterest->append((void *)_ATOM_(widget, "INVENTOR_2_1"));
121 copyInterest->append((void *)_ATOM_(widget, "INVENTOR_2_1_FILE"));
122 copyInterest->append((void *)_ATOM_(widget, "VRML_1_0"));
123 copyInterest->append((void *)_ATOM_(widget, "VRML_1_0_FILE"));
124 copyInterest->append((void *)_ATOM_(widget, "INVENTOR"));
125 copyInterest->append((void *)_ATOM_(widget, "INVENTOR_FILE"));
126 copyInterest->append((void *)_ATOM_(widget, "INVENTOR_2_0"));
127 copyInterest->append((void *)_ATOM_(widget, "INVENTOR_2_0_FILE"));
128 copyInterest->append((void *) XA_STRING);
129
130 // By default, don't pass empty path lists to callbacks
131 emptyListOK = FALSE;
132 }
133
134 ////////////////////////////////////////////////////////////////////////
135 //
136 // Description:
137 // Destructor - if we own the X selection, lose it.
138 //
139 SoXtClipboard::~SoXtClipboard()
140 //
141 ////////////////////////////////////////////////////////////////////////
142 {
143 // see if we own the selection
144 void *data = NULL;
145 selOwnerList->find(clipboardAtom, data);
146
147 if ((SoXtClipboard *)data == this) {
148 // we own the selection. we should support persistence (i.e. the
149 // data should be around for someone to paste even after we are
150 // destroyed), but we don't, so lose the selection.
151 XtDisownSelection(widget, clipboardAtom, CurrentTime);
152 selOwnerList->remove(clipboardAtom);
153 }
154
155 delete pasteInterest;
156 delete copyBuffer;
157 }
158
159 ////////////////////////////////////////////////////////////////////////
160 //
161 // Description:
162 // Copy the byte stream into our copyBuffer, and tell the X server
163 // that we now own the X selection identified by clipboardAtom (clipboardAtom was
164 // passed during construction).
165 //
166 // Use: protected
167 //
168 void
169 SoXtClipboard::copy(SoByteStream *byteStream, Time t)
170 //
171 ////////////////////////////////////////////////////////////////////////
172 {
173 eventTime = t;
174
175 delete copyBuffer;
176 copyBuffer = byteStream;
177 copyDataType = _ATOM_(widget, "INVENTOR_2_1");
178
179 // SoWWWInline won't write it's data out unless the alternateRep field is set.
180 // Run through each node in each path, and do this so that the resulting file
181 // has all the data it can have.
182 //???
183
184 // We copy Inventor 2.1 data.
185 // We will convert to other formats on request.
186 if (copyBuffer != NULL) {
187 // tell the X server that we own the selection
188 XtOwnSelection(widget,
189 clipboardAtom,
190 eventTime,
191 (XtConvertSelectionProc) SoXtClipboard::exportSelection,
192 (XtLoseSelectionProc) SoXtClipboard::loseSelection,
193 (XtSelectionDoneProc) NULL);
194
195 // make sure we own the selection
196 if (XGetSelectionOwner(XtDisplay(widget), clipboardAtom) == XtWindow(widget)) {
197 // we were granted ownership - associate 'this' with the
198 // selection atom in our dictionary of X selection owners
199 selOwnerList->enter(clipboardAtom, this);
200 }
201 }
202 }
203
204 ////////////////////////////////////////////////////////////////////////
205 //
206 // Description:
207 // Copy the users data into our copyBuffer, and tell the X server
208 // that we now own the X selection identified by clipboardAtom (clipboardAtom was
209 // passed during construction). The data is not necessarily Inventor.
210 //
211 // Use: protected
212 //
213 void
214 SoXtClipboard::copy(Atom dataType, void *data, uint32_t numBytes, Time t)
215 //
216 ////////////////////////////////////////////////////////////////////////
217 {
218 eventTime = t;
219
220 delete copyBuffer;
221 copyBuffer = new SoByteStream();
222 copyBuffer->copy(data, numBytes);
223 copyDataType = dataType;
224
225 // This is unknown data to us...
226 if (data != NULL && numBytes > 0) {
227 // tell the X server that we own the selection
228 XtOwnSelection(widget,
229 clipboardAtom,
230 eventTime,
231 (XtConvertSelectionProc) SoXtClipboard::exportSelection,
232 (XtLoseSelectionProc) SoXtClipboard::loseSelection,
233 (XtSelectionDoneProc) NULL);
234
235 // make sure we own the selection
236 if (XGetSelectionOwner(XtDisplay(widget), clipboardAtom) == XtWindow(widget)) {
237 // we were granted ownership - associate 'this' with the
238 // selection atom in our dictionary of X selection owners
239 selOwnerList->enter(clipboardAtom, this);
240 }
241 }
242 }
243
244 ////////////////////////////////////////////////////////////////////////
245 //
246 // Description:
247 // Copy the path list onto this clipboard.
248 //
249 // Use: public
250 //
251 void
252 SoXtClipboard::copy(SoPathList *pathList, Time t)
253 //
254 ////////////////////////////////////////////////////////////////////////
255 {
256 SoByteStream *bs = new SoByteStream;
257 bs->convert(pathList);
258 copy(bs, t);
259 }
260
261 ////////////////////////////////////////////////////////////////////////
262 //
263 // Description:
264 // Copy the path onto this clipboard.
265 //
266 // Use: public
267 //
268 void
269 SoXtClipboard::copy(SoPath *path, Time t)
270 //
271 ////////////////////////////////////////////////////////////////////////
272 {
273 SoByteStream *bs = new SoByteStream;
274 bs->convert(path);
275 copy(bs, t);
276 }
277
278 ////////////////////////////////////////////////////////////////////////
279 //
280 // Description:
281 // Copy the node onto this clipboard.
282 //
283 // Use: public
284 //
285 void
286 SoXtClipboard::copy(SoNode *node, Time t)
287 //
288 ////////////////////////////////////////////////////////////////////////
289 {
290 SoByteStream *bs = new SoByteStream;
291 bs->convert(node);
292 copy(bs, t);
293 }
294
295
296 ////////////////////////////////////////////////////////////////////////
297 //
298 // Description:
299 // Paste whatever is in the X selection identified by clipboardAtom.
300 // We do this by requesting the selection data from the X server,
301 // then return to the main event loop. We will be notified via an event
302 // when the data is ready.
303 //
304 // Use: public
305 //
306 void
307 SoXtClipboard::paste(
308 Time t,
309 SoXtClipboardPasteCB *pasteDoneFunc,
310 void *pasteUserData)
311 //
312 ////////////////////////////////////////////////////////////////////////
313 {
314 // Save values for later
315 eventTime = t;
316 callbackFunc = pasteDoneFunc;
317 userData = pasteUserData;
318
319 // if we own the selection, we already have the data,
320 // so we can bypass the server - simply paste what is in our cutbuffer
321 SoXtClipboard *clipboard;
322 void *data;
323 selOwnerList->find(clipboardAtom, data);
324 clipboard = (SoXtClipboard *)data;
325
326 if (clipboard == this) {
327 // We own the selection! Is it INventor data?
328 if (copyBuffer->isRawData()) {
329 //??? see if the app is interested in this data...
330 }
331 else {
332 SoPathList *pathList;
333 pathList = SoByteStream::unconvert(copyBuffer);
334
335 // let the app know the paste is done
336 if (callbackFunc != NULL)
337 (*callbackFunc)(userData, pathList);
338 }
339 }
340 else {
341 // Find out what data targets the selection owner supports.
342 // (Of course, we have the server ask the selection owner for us.)
343 XtGetSelectionValue(widget,
344 clipboardAtom,
345 _ATOM_(widget, "TARGETS"),
346 (XtSelectionCallbackProc) SoXtClipboard::importSelectionTargets,
347 (XtPointer) this,
348 eventTime);
349 }
350 }
351
352 ////////////////////////////////////////////////////////////////////////
353 //
354 // Description:
355 // Get the TARGETS we support into a table.
356 //
357 void
358 SoXtClipboard::getExportTargets(
359 char **value,
360 uint32_t *length)
361 //
362 ////////////////////////////////////////////////////////////////////////
363 {
364 // Return the selection targets which we support
365 // If the user supplied the copy data type, we serve that up.
366 // Otherwise, we publicize all the Inventor types we support.
367 if (copyBuffer->isRawData()) {
368 // User data!
369 // (malloc the table since the requestor will own the data)
370 Atom *table = (Atom *) malloc(sizeof(Atom));
371 table[0] = copyDataType;
372
373 *value = (char *) table;
374 *length = 1; // number of items in our TARGETS table
375 }
376
377 else {
378 // Inventor data!
379 // (malloc the table since the requestor will own the data)
380 Atom *table = (Atom *) malloc(copyInterest->getLength() * sizeof(Atom));
381 for (int i = 0; i < copyInterest->getLength(); i++)
382 table[i] = (Atom)(*copyInterest)[i];
383
384 *value = (char *) table;
385 *length = copyInterest->getLength();
386 }
387 }
388
389 ////////////////////////////////////////////////////////////////////////
390 //
391 // Description:
392 // Export the copied data to the X server so another application may
393 // paste. This callback is registered by copy().
394 //
395 // NOTE: we do not own the data we return (i.e. *value), so we have to
396 // copy our data buffer here.
397 //
398 // Use: static, private
399 //
400 Boolean
401 SoXtClipboard::exportSelection(
402 Widget widget,
403 Atom *selectionAtom,
404 Atom *target,
405 Atom *returnTarget,
406 char **value,
407 uint32_t *length,
408 int *format)
409 //
410 ////////////////////////////////////////////////////////////////////////
411 {
412 Boolean ok = False;
413
414 #ifdef DEBUG
415 if (selectionAtom == NULL) {
416 SoDebugError::post("SoXtClipboard::exportSelection",
417 "- selectionAtom is NULL");
418 return FALSE;
419 }
420 if (target == NULL) {
421 SoDebugError::post("SoXtClipboard::exportSelection",
422 "- target is NULL");
423 return FALSE;
424 }
425 if (value == NULL) {
426 SoDebugError::post("SoXtClipboard::exportSelection",
427 "- value is NULL");
428 return FALSE;
429 }
430 if (length == NULL) {
431 SoDebugError::post("SoXtClipboard::exportSelection",
432 "- length is NULL");
433 return FALSE;
434 }
435 if (format == NULL) {
436 SoDebugError::post("SoXtClipboard::exportSelection",
437 "- format is NULL");
438 return FALSE;
439 }
440 if (returnTarget == NULL) {
441 SoDebugError::post("SoXtClipboard::exportSelection",
442 "- returnTarget is NULL");
443 return FALSE;
444 }
445 #endif
446
447 // get the clipboard pointer for this object
448 SoXtClipboard *clipboard;
449 void *data;
450 if (! selOwnerList->find(*selectionAtom, data)) {
451 #ifdef DEBUG
452 SoDebugError::post("SoXtClipboard::exportSelection",
453 "- cannot find selection owner");
454 #endif
455 return FALSE;
456 }
457 clipboard = (SoXtClipboard *)data;
458
459
460 // Do we have any data?
461 if (clipboard->copyBuffer == NULL)
462 return False;
463 if (clipboard->copyBuffer->getData() == NULL || clipboard->copyBuffer->getNumBytes() <= 0)
464 return False;
465
466 // The most likely case - the pasting app wants to know what data types we have.
467 if (*target == _ATOM_(widget, "TARGETS")) {
468 clipboard->getExportTargets(value, length);
469 *format = 32;
470 *returnTarget = *target;
471 ok = True;
472 }
473
474 // The xclipboard case - pasting app just wants text. Give them the
475 // ascii scene graph!
476 else if (*target == XA_STRING) {
477 // convert the copyBuffer byte stream into a path list,
478 // then convert the path list to a byte stream, but in ascii format.
479 SoPathList *pathList;
480 pathList = SoByteStream::unconvert(clipboard->copyBuffer);
481
482 if (pathList != NULL) {
483 SoByteStream *asciiBuffer = new SoByteStream;
484 asciiBuffer->convert(pathList, FALSE); // FALSE == ascii format
485
486 // Copy the data buffer since the requestor will own the data
487 uint32_t numBytes = asciiBuffer->getNumBytes();
488 if (numBytes > 0) {
489 char *exportData = (char *) malloc((size_t) numBytes);
490
491 if (exportData != NULL) {
492 memcpy(exportData, asciiBuffer->getData(), (int) numBytes);
493
494 *value = exportData;
495 *length = numBytes;
496 *format = 8;
497 *returnTarget = XA_STRING;
498 ok = True;
499 }
500 }
501 delete asciiBuffer;
502 delete pathList;
503 }
504 }
505
506 // If our copyBuffer is a user defined type, then we do no conversion.
507 // Just serve it up.
508 else if (clipboard->copyBuffer->isRawData()) {
509 // Do we have the data that the pasting app wants?
510 if (clipboard->copyDataType == *target) {
511 // Copy the data buffer since the requestor will own the data
512 char *exportData = (char *) malloc((size_t) clipboard->copyBuffer->getNumBytes());
513 if (exportData != NULL) {
514 memcpy(exportData,
515 clipboard->copyBuffer->getData(),
516 (int)clipboard->copyBuffer->getNumBytes());
517 *value = exportData;
518 *length = clipboard->copyBuffer->getNumBytes();
519 *format = 8;
520 *returnTarget = clipboard->copyDataType;
521 ok = True;
522 }
523 }
524 }
525
526 else {
527 // If the data is one of the Inventor types, we need to convert to
528 // the requested type.
529 if (convertData(
530 widget,
531 clipboard->copyBuffer->getData(),
532 clipboard->copyBuffer->getNumBytes(),
533 *target, value, length))
534 {
535 *format = 8;
536 *returnTarget = *target;
537 ok = True;
538 }
539 }
540
541 return ok;
542 }
543
544 ////////////////////////////////////////////////////////////////////////
545 //
546 // Convert our Inventor 2.1 data to the specified target.
547 //
548 // static,public
549 //
550 SbBool
551 SoXtClipboard::convertData(
552 Widget widget,
553 void *srcData,
554 uint32_t srcNumBytes,
555 Atom desiredType,
556 char **returnData,
557 uint32_t *returnNumBytes)
558 //
559 ////////////////////////////////////////////////////////////////////////
560 {
561 // Do we know how to convert to the desired target?
562 if (! (desiredType == _ATOM_(widget, "INVENTOR_2_1") ||
563 desiredType == _ATOM_(widget, "INVENTOR_2_1_FILE") ||
564 desiredType == _ATOM_(widget, "VRML_1_0") ||
565 desiredType == _ATOM_(widget, "VRML_1_0_FILE") ||
566 desiredType == _ATOM_(widget, "INVENTOR") ||
567 desiredType == _ATOM_(widget, "INVENTOR_FILE") ||
568 desiredType == _ATOM_(widget, "INVENTOR_2_0") ||
569 desiredType == _ATOM_(widget, "INVENTOR_2_0_FILE"))) {
570 // It's some type we don't know
571 *returnData = NULL;
572 *returnNumBytes = 0;
573
574 #ifdef DEBUG
575 SoDebugError::post("SoXtClipboard::convertData",
576 "desired conversion type not supported");
577 #endif
578 return FALSE;
579 }
580
581 // OK, we think we can support the destination target.
582 char *exportData = NULL;
583 uint32_t numBytes = 0;
584
585 // Run through our supported targets and see if we can convert.
586 //??? It would be nice if we put conversion callbacks in the copyInterest list,
587 //??? then we could just run through the list and blindly convert.
588
589 //??? should we cache our results?
590
591 ////////////////////////////////////////////////////////////////////////
592 //
593 // Inventor 2.1 - no conversion needed
594 // (For VRML 1.0 destination - we should probably convert ivToVRML???)
595 //
596 ////////////////////////////////////////////////////////////////////////
597 if (desiredType == _ATOM_(widget, "INVENTOR_2_1") ||
598 desiredType == _ATOM_(widget, "VRML_1_0")) {
599 exportData = (char *) malloc((size_t) srcNumBytes);
600 if (exportData != NULL) {
601 memcpy(exportData, srcData, (int) srcNumBytes);
602 *returnData = exportData;
603 *returnNumBytes = srcNumBytes;
604 return True;
605 }
606 else {
607 #ifdef DEBUG
608 SoDebugError::post("SoXtClipboard::convertData",
609 "could not allocate %d bytes for return buffer", srcNumBytes);
610 #endif
611 return False;
612 }
613 }
614
615 // Put our 2.1 data into a file
616 // Write the bytes to a tmp file which will be the input file to ivdowngrade
617 static char *tmpdir = NULL;
618 if (tmpdir == NULL) {
619 tmpdir = getenv("TMPDIR");
620 if (tmpdir == NULL)
621 tmpdir = "/tmp";
622 }
623
624 // Create temp filename
625 char *filename = tempnam(tmpdir, "IV21");
626 SbString tmpfile(filename);
627 free(filename);
628
629 // Write 2.1 data to the file
630 if (! writeToFile(tmpfile, srcData, srcNumBytes))
631 return False;
632
633 ////////////////////////////////////////////////////////////////////////
634 //
635 // If the destination target is a 2.1 file, we're done
636 // (For VRML 1.0 destination - we should probably convert ivToVRML???)
637 //
638 ////////////////////////////////////////////////////////////////////////
639 if (desiredType == _ATOM_(widget, "INVENTOR_2_1_FILE") ||
640 desiredType == _ATOM_(widget, "VRML_1_0_FILE")) {
641 exportData = strdup(tmpfile.getString());
642 numBytes = tmpfile.getLength() + 1; // +1 to grab the \0
643 *returnData = exportData;
644 *returnNumBytes = numBytes;
645 return True;
646 }
647
648 // Make sure ivdowngrade exists
649 //???
650
651 // Create temp output filename
652 filename = tempnam(tmpdir, "IV20");
653 SbString tmpfile2(filename);
654 free(filename);
655
656 // Convert to the 2.0 format
657 #define BUFSIZE 512
658 char conversionCmd[BUFSIZE];
659 const char *infile = tmpfile.getString();
660 const char *outfile = tmpfile2.getString();
661 sprintf(conversionCmd, IVBINDIR "/ivdowngrade -v 2.0 %s %s", infile, outfile);
662
663 if (system(conversionCmd) == -1) {
664 #ifdef DEBUG
665 SoDebugError::post("SoXtClipboard::convertData",
666 "command <%s> failed", conversionCmd);
667 #endif
668
669 unlink(infile);
670 unlink(outfile);
671 return False;
672 }
673
674 // Converson succeeded! Done with the input file
675 //??? NOTE: we could cache this for future conversions...
676 unlink(infile); // remove the file
677
678 ////////////////////////////////////////////////////////////////////////
679 //
680 // If the destination target is a 2.0 file, we're done
681 //
682 ////////////////////////////////////////////////////////////////////////
683 if (desiredType == _ATOM_(widget, "INVENTOR_FILE") ||
684 desiredType == _ATOM_(widget, "INVENTOR_2_0_FILE") ) {
685 exportData = strdup(tmpfile2.getString());
686 numBytes = tmpfile2.getLength() + 1; // +1 to grab the \0
687 *returnData = exportData;
688 *returnNumBytes = numBytes;
689 return True; // success!
690 }
691
692 // Read the output file into the buffer
693 // First, open the file.
694 int fd = open(outfile, O_RDONLY);
695 if (fd < 0) {
696 #ifdef DEBUG
697 SoDebugError::post("SoXtClipboard::convertData",
698 "could not open output file %s for reading", outfile);
699 #endif
700
701 unlink(outfile); // remove the file
702 return False;
703 }
704
705 // Get file size
706 uint32_t len = lseek(fd, 0, SEEK_END);
707 lseek(fd, 0, SEEK_SET); // seek back to beginning
708
709 // Get buffer to hold data
710 void *buf = malloc(len);
711 if (buf == NULL) {
712 #ifdef DEBUG
713 SoDebugError::post("SoXtClipboard::convertData",
714 "could not malloc %d bytes", len);
715 #endif
716
717 unlink(outfile); // remove the file
718 return False;
719 }
720
721 // Read that file
722 if (read(fd, buf, len) == -1) {
723 #ifdef DEBUG
724 SoDebugError::post("SoXtClipboard::convertData",
725 "could not read %d bytes from output file %s", len, outfile);
726 #endif
727
728 unlink(outfile); // remove the file
729 return False;
730 }
731
732 // Done with the output file
733 unlink(outfile); // remove the file
734
735 ////////////////////////////////////////////////////////////////////////
736 //
737 // If the destination target is a 2.0 object, we are done!
738 //
739 ////////////////////////////////////////////////////////////////////////
740 if (desiredType == _ATOM_(widget, "INVENTOR") ||
741 desiredType == _ATOM_(widget, "INVENTOR_2_0") ) {
742 *returnData = (char *)buf;
743 *returnNumBytes = len;
744 return True; // success!
745 }
746
747 // If we get to here, we did not succeed. Bummer.
748 return False;
749 }
750
751 ////////////////////////////////////////////////////////////////////////
752 //
753 // Write the copyBuffer to the tmp file
754 //
755 // static protected
756 //
757 SbBool
758 SoXtClipboard::writeToFile(
759 SbString &tmpfile,
760 void *srcData,
761 uint32_t srcNumBytes)
762 //
763 ////////////////////////////////////////////////////////////////////////
764 {
765 const char *filename = tmpfile.getString();
766 int fd = creat(filename, S_IRWXU); // create for reading/writing
767 if (fd <= 0) {
768 #ifdef DEBUG
769 SoDebugError::post("SoXtClipboard::writeToFile",
770 "could not open tmp input file %s", filename);
771 #endif
772
773 return False;
774 }
775
776 // Write to temp file
777 if (write(fd, srcData, srcNumBytes) == -1) {
778 #ifdef DEBUG
779 SoDebugError::post("SoXtClipboard::writeToFile",
780 "could not write %d bytes to tmp input file %s",
781 srcNumBytes, filename);
782 #endif
783
784 close(fd);
785 unlink(filename); // remove the file
786 return False;
787 }
788
789 close(fd);
790 return True;
791 }
792
793
794 ////////////////////////////////////////////////////////////////////////
795 //
796 // Description:
797 // Import the targets supported by the selection owner. If we see
798 // a target we recognize, then we ask for the selection data in that
799 // format target. This callback is registered by pasteSelection().
800 //
801 // Use: static, private
802 //
803 void
804 SoXtClipboard::importSelectionTargets(
805 Widget widget,
806 SoXtClipboard *clipboard,
807 Atom *selectionAtom,
808 Atom *target,
809 char *value,
810 uint32_t *length,
811 int *format)
812 //
813 ////////////////////////////////////////////////////////////////////////
814 {
815 #ifdef DEBUG
816 if (clipboard == NULL) {
817 SoDebugError::post("SoXtClipboard::importSelectionTargets",
818 "- clipboard is NULL");
819 return;
820 }
821 if (target == NULL) {
822 SoDebugError::post("SoXtClipboard::importSelectionTargets",
823 "- target is NULL");
824 return;
825 }
826 if (! clipboard->emptyListOK) {
827 if (value == NULL) {
828 SoDebugError::post("SoXtClipboard::importSelectionTargets",
829 "- value is NULL");
830 return;
831 }
832 if (length == NULL) {
833 SoDebugError::post("SoXtClipboard::importSelectionTargets",
834 "- length is NULL");
835 return;
836 }
837 if (format == NULL) {
838 SoDebugError::post("SoXtClipboard::importSelectionTargets",
839 "- format is NULL");
840 return;
841 }
842 }
843 #endif
844
845 // If no targets are supported
846 if (*length == 0) {
847
848 // Invoke the callback if an empty list is ok
849 if (clipboard->emptyListOK) {
850 SoPathList emptyList;
851 (*clipboard->callbackFunc)(clipboard->userData, &emptyList);
852 }
853 return;
854 }
855
856 Atom supportedAtom = 0;
857 if (*target == _ATOM_(widget, "TARGETS") || *target == XA_ATOM) {
858 if (*format == 32) {
859 Atom *lval = (Atom *) value;
860 supportedAtom = clipboard->chooseFromImportTargets(lval, *length);
861 }
862 #ifdef DEBUG
863 else {
864 SoDebugError::post("SoXtClipboard::importSelectionTargets",
865 "- received unknown format %d from clipboard owner", *format );
866 }
867 #endif
868 }
869 else {
870 // The selection owner does not support the TARGETS inquiry.
871 // Let's see if they at least support INVENTOR.
872 //??? what should we do here?
873 supportedAtom = _ATOM_(widget, "INVENTOR");
874 }
875
876 // if the selection owner supports a target we like, ask for the data
877 if (supportedAtom != 0) {
878 XtGetSelectionValue(clipboard->widget,
879 *selectionAtom,
880 supportedAtom,
881 (XtSelectionCallbackProc) SoXtClipboard::importSelection,
882 (XtPointer) clipboard,
883 clipboard->eventTime);
884 }
885
886 // According to Xt doc, the requestor (that's us) owns the data
887 // and should free it when done.
888
889 //??? Put in this change as a workaround to bug #306340
890 //??? to prevent core dumps. It seems that this sometimes frees
891 //??? memory here that it should not.
892
893 //??? XtFree(value);
894 }
895
896 ////////////////////////////////////////////////////////////////////////
897 //
898 // See if any of the copy apps supported targets are something we are
899 // interested in pasting. Algorithm is: see if the first copy target
900 // matches any of our paste targets. If not, check the second copy
901 // target, etc.
902 //
903 Atom
904 SoXtClipboard::chooseFromImportTargets(
905 Atom *supportedTargets,
906 int length)
907 //
908 ////////////////////////////////////////////////////////////////////////
909 {
910 Atom chosenType = 0;
911 int i;
912
913 // See if anything in the supported targets matches our paste interest
914 for (i = 0; i < length; i++) {
915 for (int j = 0; j < pasteInterest->getLength(); j++) {
916 if (supportedTargets[i] == pasteInterest->getDataType(j)) {
917 chosenType = supportedTargets[i];
918 break;
919 }
920 }
921
922 if (chosenType != 0) break;
923 }
924
925 return chosenType;
926 }
927
928 ////////////////////////////////////////////////////////////////////////
929 //
930 // Description:
931 // Import the current selection from the X server, and paste it
932 // into the scene graph. This callback is registered by importSelectionTargets().
933 //
934 // Use: static, private
935 //
936 void
937 SoXtClipboard::importSelection(
938 Widget /*w*/,
939 SoXtClipboard *clipboard,
940 Atom * /*selectionAtom*/,
941 Atom *target,
942 char *value,
943 uint32_t *length,
944 int * /*format*/)
945 //
946 ////////////////////////////////////////////////////////////////////////
947 {
948 #ifdef DEBUG
949 if (clipboard == NULL) {
950 SoDebugError::post("SoXtClipboard::importSelection",
951 "- clipboard is NULL");
952 return;
953 }
954 if (target == NULL) {
955 SoDebugError::post("SoXtClipboard::importSelection",
956 "- target is NULL");
957 return;
958 }
959 if (! clipboard->emptyListOK) {
960 if (value == NULL) {
961 SoDebugError::post("SoXtClipboard::importSelection",
962 "- value is NULL");
963 return;
964 }
965 if (length == NULL) {
966 SoDebugError::post("SoXtClipboard::importSelection",
967 "- length is NULL");
968 return;
969 }
970 }
971 #endif
972
973 if (*length == 0 && ! clipboard->emptyListOK)
974 return; // nothing to paste!
975
976 if (clipboard->callbackFunc == NULL) return; // no callback to invoke!
977
978 // See if we are interested in the paste target
979 const SoXtImportInterest *interest =
980 clipboard->pasteInterest->get(clipboard->pasteInterest->find(*target));
981
982 // If there is interest, invoke the paste interest callback to read the data
983 if (interest != NULL) {
984 (*interest->getCallBack())(
985 interest->getUserData(),
986 *target,
987 (void *) value,
988 *length);
989 }
990 // If there is no interest, but an empty list is ok, invoke the
991 // callback with an empty list
992 else if (clipboard->emptyListOK) {
993 // Invoke the callback if an empty list is ok
994 SoPathList emptyList;
995 (*clipboard->callbackFunc)(clipboard->userData, &emptyList);
996 }
997
998 // According to Xt doc, the requestor (that's us) owns the data
999 // and should free it when done.
1000 //??? Put in this change as a workaround to bug #306340
1001 //??? to prevent core dumps. It seems that this sometimes frees
1002 //??? memory here that it should not.
1003
1004 //??? XtFree(value);
1005 }
1006
1007 ////////////////////////////////////////////////////////////////////////
1008 //
1009 // Description:
1010 // This is called when the user drops data on this widget (drag and drop).
1011 //
1012 // Use: protected
1013 //
1014 void
1015 SoXtClipboard::addPasteInterest(
1016 Atom dataType,
1017 SoXtClipboardImportCB *pasteImportFunc,
1018 void *userData)
1019 //
1020 ////////////////////////////////////////////////////////////////////////
1021 {
1022 pasteInterest->append(dataType, pasteImportFunc, userData);
1023 }
1024
1025 ////////////////////////////////////////////////////////////////////////
1026 //
1027 // Description:
1028 // This is called when the user pastes data in this widget.
1029 //
1030 // Use: protected
1031 //
1032 void
1033 SoXtClipboard::pasteImport(Atom dataType, void *data, uint32_t numBytes)
1034 //
1035 ////////////////////////////////////////////////////////////////////////
1036 {
1037 // Did we get any data?
1038 SoPathList *pathList = readData(widget, dataType, data, numBytes);
1039
1040 // Did we get Inventor data?
1041 if (pathList == NULL) {
1042
1043 if (emptyListOK) {
1044 SoPathList emptyList;
1045 (*callbackFunc)(userData, &emptyList);
1046 }
1047
1048 #ifdef DEBUG
1049 else
1050 SoDebugError::post("SoXtClipboard::paste",
1051 "could not get Inventor data");
1052 #endif
1053 }
1054
1055 // send the data to the app
1056 else (*callbackFunc)(userData, pathList);
1057 }
1058
1059 ////////////////////////////////////////////////////////////////////////
1060 //
1061 // Description:
1062 // Convert target data we just imported via paste or drop to Inventor.
1063 // This will free data when done.
1064 //
1065 // Use: static
1066 //
1067 SoPathList *
1068 SoXtClipboard::readData(
1069 Widget w,
1070 Atom target,
1071 void *data,
1072 uint32_t numBytes)
1073 //
1074 ////////////////////////////////////////////////////////////////////////
1075 {
1076 SoPathList *pathList = NULL;
1077
1078 if (data != NULL) {
1079 if (target == _ATOM_(w, "INVENTOR_2_1") ||
1080 target == _ATOM_(w, "VRML_1_0") ||
1081 target == _ATOM_(w, "INVENTOR") ||
1082 target == _ATOM_(w, "INVENTOR_2_0")) {
1083
1084 pathList = SoByteStream::unconvert(data, numBytes);
1085 }
1086
1087 else if (target == _ATOM_(w, "INVENTOR_2_1_FILE") ||
1088 target == _ATOM_(w, "VRML_1_0_FILE") ||
1089 target == _ATOM_(w, "INVENTOR_FILE") ||
1090 target == _ATOM_(w, "INVENTOR_2_0_FILE")) {
1091
1092 readFile(pathList, (const char *) data);
1093 }
1094
1095 else if (target == XA_STRING) {
1096 // Try to read Inventor data out of this
1097 // if the string is in Inventor ascii interchange format
1098 if (SoDB::isValidHeader((const char *)data)) {
1099 // yes, the string we are importing is actually Inventor data!
1100 pathList = SoByteStream::unconvert(data, numBytes);
1101 }
1102 }
1103
1104 XtFree((char *)data);
1105 }
1106
1107 return pathList;
1108 }
1109
1110 ////////////////////////////////////////////////////////////////////////
1111 //
1112 // Description:
1113 // Read the file and add to the pathList.
1114 //
1115 // Use: static protected
1116 //
1117 void
1118 SoXtClipboard::readFile(SoPathList *&pathList, const char *filename)
1119 //
1120 ////////////////////////////////////////////////////////////////////////
1121 {
1122 //??? file could be DXF or some other format. May need to run a converter.
1123 //??? do the routeprint stuff to figure out what to do here!
1124
1125 SoInput in;
1126 SoNode *node;
1127 SoPath *path;
1128
1129 // Open the file.
1130 if (! in.openFile(filename)) {
1131 #ifdef DEBUG
1132 SoDebugError::post("SoXtClipboard::paste", "could not open file %s", filename);
1133 #endif
1134
1135 return;
1136 }
1137
1138 // Make sure there's a pathList
1139 if (pathList == NULL)
1140 pathList = new SoPathList;
1141
1142 // Try reading nodes.
1143 do {
1144 if (SoDB::read(&in, node)) {
1145 if (node != NULL) {
1146 // Read a node! Add to the pathList
1147 path = new SoPath(node);
1148 pathList->append(path);
1149 }
1150 }
1151 } while (node != NULL);
1152
1153 in.closeFile();
1154
1155 // If we succeeded in reading stuff, return.
1156 if (pathList->getLength() > 0)
1157 return;
1158
1159 // Open the file again.
1160 if (! in.openFile(filename))
1161 return;
1162
1163 // If couldn't read nodes, try reading paths.
1164 do {
1165 if (SoDB::read(&in, path)) {
1166 if (path != NULL) {
1167 // Read a path! Add to the pathList
1168 pathList->append(path);
1169 }
1170 }
1171 } while (path != NULL);
1172
1173 in.closeFile();
1174
1175 // If we never added to the pathList, get rid of it
1176 if (pathList->getLength() == 0) {
1177 delete pathList;
1178 pathList = NULL;
1179
1180 #ifdef DEBUG
1181 SoDebugError::post("SoXtClipboard::paste", "could not read Inventor data from file %s", filename);
1182 #endif
1183 }
1184 }
1185
1186
1187 ////////////////////////////////////////////////////////////////////////
1188 //
1189 // Description:
1190 // Called by the X server when we are no longer the owner of the
1191 // selection. We take this opportunity to nuke our local copyBuffer.
1192 // This callback is registered by copySelection().
1193 //
1194 // Use: static, private
1195 //
1196 void
1197 SoXtClipboard::loseSelection(
1198 Widget /*w*/,
1199 Atom *selectionAtom)
1200 //
1201 ////////////////////////////////////////////////////////////////////////
1202 {
1203 // get the clipboard pointer for this object
1204 SoXtClipboard *clipboard;
1205 void *data;
1206 selOwnerList->find(*selectionAtom, data);
1207 clipboard = (SoXtClipboard *)data;
1208
1209 // get the clipboard pointer for this object
1210 if (clipboard != NULL) {
1211 if (clipboard->copyBuffer != NULL) {
1212 delete clipboard->copyBuffer;
1213 clipboard->copyBuffer = NULL;
1214 }
1215
1216 selOwnerList->remove(*selectionAtom);
1217 }
1218 }
1219