Dnd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $TOG: Dnd.c /main/7 1998/10/23 13:47:10 mgreess $ */
  24. /*********************************************************************
  25. *
  26. * File: Dnd.c
  27. *
  28. * Description: Implemenation of DND Convenience API.
  29. *
  30. *********************************************************************
  31. *
  32. *+SNOTICE
  33. *
  34. * RESTRICTED CONFIDENTIAL INFORMATION:
  35. *
  36. * The information in this document is subject to special
  37. * restrictions in a confidential disclosure agreement between
  38. * HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
  39. * document outside HP, IBM, Sun, USL, SCO, or Univel without
  40. * Sun's specific written approval. This documment and all copies
  41. * and derivative works thereof must be returned or destroyed at
  42. * Sun's request.
  43. *
  44. * Copyright 1993 Sun Microsystems, Inc. All rights reserved.
  45. *
  46. * (c) Copyright 1993, 1994 Hewlett-Packard Company
  47. * (c) Copyright 1993, 1994 International Business Machines Corp.
  48. * (c) Copyright 1993, 1994 Sun Microsystems, Inc.
  49. * (c) Copyright 1993, 1994 Novell, Inc.
  50. *
  51. *+ENOTICE
  52. */
  53. #include <stdlib.h>
  54. #include <stdarg.h>
  55. #include <sys/utsname.h>
  56. #include <X11/Intrinsic.h>
  57. #include <Xm/Xm.h>
  58. #include <Xm/DragDrop.h>
  59. #include <Xm/DropTrans.h>
  60. #include <Xm/AtomMgr.h>
  61. #include <Dt/UserMsg.h>
  62. #include "Dnd.h"
  63. #include "DndP.h"
  64. #include "DtSvcLock.h"
  65. /*********************************************************************
  66. *
  67. * Data Transfer Selection Targets
  68. *
  69. *********************************************************************/
  70. Atom XA_TARGETS;
  71. Atom XA_TIMESTAMP;
  72. Atom XA_MULTIPLE;
  73. Atom XA_DELETE;
  74. Atom XA_NULL;
  75. Atom XA_TEXT;
  76. Atom XA_HOST_NAME;
  77. Atom XA_SUN_FILE_HOST_NAME;
  78. Atom XA_SUN_ENUM_COUNT;
  79. Atom XA_SUN_DATA_LABEL;
  80. Atom XA_SUN_SELN_READONLY;
  81. Atom XA_SUN_ATM_FILE_NAME;
  82. Atom XA_SUN_ATM_METHODS;
  83. static void
  84. dndInitTargets(
  85. Display *dpy)
  86. {
  87. _DtSvcProcessLock();
  88. if (XA_TARGETS != 0) {
  89. _DtSvcProcessUnlock();
  90. return;
  91. }
  92. XA_TARGETS = DtGetAtom(dpy,"TARGETS");
  93. XA_TIMESTAMP = DtGetAtom(dpy,"TIMESTAMP");
  94. XA_MULTIPLE = DtGetAtom(dpy,"MULTIPLE");
  95. XA_DELETE = DtGetAtom(dpy,"DELETE");
  96. XA_NULL = DtGetAtom(dpy,"NULL");
  97. XA_TEXT = DtGetAtom(dpy,"TEXT");
  98. XA_HOST_NAME = DtGetAtom(dpy,"HOST_NAME");
  99. XA_SUN_FILE_HOST_NAME = DtGetAtom(dpy,"_SUN_FILE_HOST_NAME");
  100. XA_SUN_ENUM_COUNT = DtGetAtom(dpy,"_SUN_ENUMERATION_COUNT");
  101. XA_SUN_DATA_LABEL = DtGetAtom(dpy,"_SUN_DATA_LABEL");
  102. XA_SUN_SELN_READONLY = DtGetAtom(dpy,"_SUN_SELN_IS_READONLY");
  103. XA_SUN_ATM_FILE_NAME = DtGetAtom(dpy,"_SUN_ATM_FILE_NAME");
  104. XA_SUN_ATM_METHODS = DtGetAtom(dpy,
  105. "_SUN_ALTERNATE_TRANSPORT_METHODS");
  106. _DtSvcProcessUnlock();
  107. }
  108. /*********************************************************************
  109. *
  110. * Data Transfer Protocol Method Functions
  111. *
  112. *********************************************************************/
  113. static DtDndProtocol dndProtocolList[] = {
  114. DtDND_FILENAME_TRANSFER,
  115. DtDND_BUFFER_TRANSFER,
  116. DtDND_TEXT_TRANSFER,
  117. DtDND_NOOP_TRANSFER
  118. };
  119. extern DtDndMethods *_DtDndBuffTransferProtocolInitialize(Display*);
  120. extern DtDndMethods *_DtDndFileTransferProtocolInitialize(Display*);
  121. extern DtDndMethods *_DtDndTextTransferProtocolInitialize(Display*);
  122. /*
  123. * dndGetTransferMethods
  124. *
  125. * Return the transfer methods for a particular protocol
  126. */
  127. static DtDndMethods *
  128. dndGetTransferMethods(
  129. Display * display,
  130. DtDndProtocol protocol)
  131. {
  132. DtDndMethods * methods = NULL;
  133. switch (protocol) {
  134. case DtDND_BUFFER_TRANSFER:
  135. methods = _DtDndBuffTransferProtocolInitialize(display);
  136. break;
  137. case DtDND_FILENAME_TRANSFER:
  138. methods = _DtDndFileTransferProtocolInitialize(display);
  139. break;
  140. case DtDND_TEXT_TRANSFER:
  141. methods = _DtDndTextTransferProtocolInitialize(display);
  142. break;
  143. }
  144. return methods;
  145. }
  146. /*
  147. * _DtDndCreateExportTransfer
  148. *
  149. * Returns a transfer suitable for use by a drag initiator
  150. */
  151. DtDndTransfer *
  152. _DtDndCreateExportTransfer(
  153. DtDragInfo * dtDragInfo)
  154. {
  155. Display * dpy = XtDisplayOfObject(dtDragInfo->dragInitiator);
  156. DtDndTransfer * transfer;
  157. dndInitTargets(dpy);
  158. transfer = (DtDndTransfer *)XtMalloc(sizeof(DtDndTransfer));
  159. transfer->methods = dndGetTransferMethods(dpy,dtDragInfo->protocol);
  160. (*transfer->methods->getExportTargets)(dtDragInfo,
  161. &(transfer->targets), &(transfer->numTargets));
  162. return transfer;
  163. }
  164. /*
  165. * _DtDndCreateImportTransfers
  166. *
  167. * Create the list of transfers specified by the protocols.
  168. */
  169. DtDndTransfer *
  170. _DtDndCreateImportTransfers(
  171. DtDropInfo * dtDropInfo,
  172. Cardinal * numTransfers)
  173. {
  174. Display * dpy = XtDisplayOfObject(dtDropInfo->dropReceiver);
  175. DtDndTransfer * transfers;
  176. DtDndTransfer * transfer;
  177. Cardinal ii, jj;
  178. dndInitTargets(dpy);
  179. *numTransfers = 0;
  180. for (ii = 0; dndProtocolList[ii] != DtDND_NOOP_TRANSFER; ii++) {
  181. if (dtDropInfo->protocols & dndProtocolList[ii]) {
  182. (*numTransfers)++;
  183. }
  184. }
  185. if (*numTransfers == 0)
  186. return (DtDndTransfer *)NULL;
  187. transfers = (DtDndTransfer *)XtMalloc(
  188. *numTransfers * sizeof(DtDndTransfer));
  189. for (ii = 0, jj = 0; dndProtocolList[ii] != DtDND_NOOP_TRANSFER; ii++) {
  190. if (dtDropInfo->protocols & dndProtocolList[ii]) {
  191. transfer = &transfers[jj++];
  192. transfer->methods =
  193. dndGetTransferMethods(dpy,dndProtocolList[ii]);
  194. (*transfer->methods->getImportTargets)(dtDropInfo,
  195. &(transfer->targets), &(transfer->numTargets));
  196. }
  197. }
  198. return transfers;
  199. }
  200. /*
  201. * _DtDndDestroyTransfers
  202. *
  203. * Destroy the transfer list created by
  204. * _DtDndCreateExportTransfer() or _DtDndCreateImportTransfers()
  205. */
  206. void
  207. _DtDndDestroyTransfers(
  208. DtDndTransfer * transfers,
  209. Cardinal numTransfers)
  210. {
  211. Cardinal ii;
  212. for (ii = 0; ii < numTransfers; ii++) {
  213. XtFree((char *)transfers[ii].targets);
  214. }
  215. XtFree((char *)transfers);
  216. }
  217. /*
  218. * _DtDndTransferFromTargets
  219. *
  220. * Returns the transfer method that matches the target list
  221. */
  222. DtDndTransfer *
  223. _DtDndTransferFromTargets(
  224. DtDndTransfer * transfers,
  225. Cardinal numTransfers,
  226. Atom * targets,
  227. Cardinal numTargets)
  228. {
  229. Cardinal ii, jj, kk;
  230. for (ii = 0; ii < numTransfers; ii++) {
  231. for (jj = 0; jj < transfers[ii].numTargets; jj++) {
  232. for (kk = 0; kk < numTargets; kk++) {
  233. if (transfers[ii].targets[jj] == targets[kk]) {
  234. return &transfers[ii];
  235. }
  236. }
  237. }
  238. }
  239. return (DtDndTransfer *)NULL;
  240. }
  241. /*
  242. * _DtDndTransferAdd
  243. *
  244. * Add the targets to the requested transfer targets list
  245. * and call XmDropTransferAdd() to request them
  246. */
  247. void
  248. _DtDndTransferAdd(
  249. Widget dropTransfer,
  250. DtDropInfo * dtDropInfo,
  251. Atom * transferTargets,
  252. Cardinal numTransferTargets)
  253. {
  254. DtTransferInfo *transferInfo = dtDropInfo->transferInfo;
  255. XmDropTransferEntryRec * transferEntries;
  256. Cardinal numTransfers;
  257. int ii, jj;
  258. transferEntries = (XmDropTransferEntryRec *)
  259. XtMalloc(numTransferTargets * sizeof(XmDropTransferEntryRec));
  260. numTransfers = transferInfo->numTransferTargets + numTransferTargets;
  261. transferInfo->transferTargets = (Atom *)XtRealloc(
  262. (char *)transferInfo->transferTargets,
  263. numTransfers * sizeof(Atom));
  264. jj = transferInfo->numTransferTargets;
  265. for (ii = 0; ii < numTransferTargets; ii++) {
  266. transferEntries[ii].target = transferTargets[ii];
  267. transferEntries[ii].client_data = (XtPointer)dtDropInfo;
  268. transferInfo->transferTargets[ii+jj] = transferTargets[ii];
  269. }
  270. transferInfo->numTransferTargets = numTransfers;
  271. XmDropTransferAdd(dropTransfer, transferEntries, numTransferTargets);
  272. XtFree((char *)transferEntries);
  273. }
  274. /*********************************************************************
  275. *
  276. * Misc Debugging Functions
  277. *
  278. *********************************************************************/
  279. #ifdef DEBUG
  280. void
  281. _DtDndPrintTargets(
  282. Display *display,
  283. Atom *targets,
  284. Cardinal numTargets)
  285. {
  286. Cardinal ii;
  287. char *name;
  288. for (ii = 0; ii < numTargets; ii++) {
  289. name = XGetAtomName(display, targets[ii]);
  290. if (name) {
  291. printf("%s ", name);
  292. XFree(name);
  293. } else {
  294. printf("(null) ");
  295. }
  296. }
  297. printf("\n");
  298. }
  299. void
  300. _DtDndPrintTransfers(
  301. Display *display,
  302. DtDndTransfer *transfers,
  303. Cardinal numTransfers)
  304. {
  305. Cardinal ii;
  306. for (ii = 0; ii < numTransfers; ii++) {
  307. printf("%s\tTargets: ",transfers[ii].methods->name);
  308. _DtDndPrintTargets(display,
  309. transfers[ii].targets,transfers[ii].numTargets);
  310. }
  311. }
  312. #endif
  313. /*********************************************************************
  314. *
  315. * Misc Utility Functions
  316. *
  317. *********************************************************************/
  318. /*
  319. * Copy CallbackList
  320. */
  321. XtCallbackList
  322. _DtDndCopyCallbackList(
  323. XtCallbackList callbacks)
  324. {
  325. XtCallbackList cl;
  326. int ii, count;
  327. if (callbacks == NULL)
  328. return NULL;
  329. count = 1;
  330. for (cl = callbacks; cl->callback != NULL; cl++) {
  331. count++;
  332. }
  333. cl = (XtCallbackList)XtMalloc(count * sizeof(XtCallbackRec));
  334. for (ii = 0; ii < count; ii++) {
  335. cl[ii].callback = callbacks[ii].callback;
  336. cl[ii].closure = callbacks[ii].closure;
  337. }
  338. return cl;
  339. }
  340. /*
  341. * Call CallbackList
  342. */
  343. void
  344. _DtDndCallCallbackList(
  345. Widget widget,
  346. XtCallbackList callbacks,
  347. XtPointer calldata)
  348. {
  349. XtCallbackList cl;
  350. if (callbacks == NULL)
  351. return;
  352. for (cl = callbacks; cl->callback != NULL; cl++) {
  353. (*cl->callback)(widget, cl->closure, calldata);
  354. }
  355. }
  356. /*
  357. * Misc Varargs Utility Functions
  358. */
  359. int
  360. _DtDndCountVarArgs(
  361. va_list vaList)
  362. {
  363. XtPointer argPtr;
  364. Cardinal argCount;
  365. argCount = 0;
  366. for (argPtr = va_arg(vaList,String);
  367. argPtr != NULL;
  368. argPtr = va_arg(vaList,String)) {
  369. va_arg(vaList, XtArgVal);
  370. argCount++;
  371. }
  372. return argCount;
  373. }
  374. void
  375. _DtDndArgListFromVarArgs(
  376. va_list vaList,
  377. Cardinal maxArgs,
  378. ArgList *argListReturn,
  379. Cardinal *argCountReturn)
  380. {
  381. ArgList argList;
  382. Cardinal argCount;
  383. XtPointer argPtr;
  384. if (0 == maxArgs)
  385. {
  386. *argListReturn = NULL;
  387. *argCountReturn = 0;
  388. return;
  389. }
  390. argList = (ArgList)XtMalloc((unsigned)(maxArgs * sizeof(Arg)));
  391. argCount = 0;
  392. for (argPtr = va_arg(vaList,String);
  393. argPtr != NULL;
  394. argPtr = va_arg(vaList,String)) {
  395. XtSetArg(argList[argCount], argPtr, va_arg(vaList,XtArgVal));
  396. argCount++;
  397. }
  398. *argListReturn = argList;
  399. *argCountReturn = argCount;
  400. }
  401. /*
  402. * Returns the network node/host name.
  403. */
  404. String
  405. _DtDndGetHostName(void)
  406. {
  407. static char *nodename;
  408. _DtSvcProcessLock();
  409. if (nodename == NULL) {
  410. struct utsname un;
  411. if (uname(&un) == -1) {
  412. _DtSimpleError(DtProgName,DtError, NULL, "uname", NULL);
  413. nodename = XtNewString("nodename");
  414. } else {
  415. nodename = XtNewString(un.nodename);
  416. }
  417. }
  418. _DtSvcProcessUnlock();
  419. return nodename;
  420. }