Dnd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 <Dt/UserMsg.h>
  61. #include "Dnd.h"
  62. #include "DndP.h"
  63. #include "DtSvcLock.h"
  64. /*********************************************************************
  65. *
  66. * Data Transfer Selection Targets
  67. *
  68. *********************************************************************/
  69. Atom XA_TARGETS;
  70. Atom XA_TIMESTAMP;
  71. Atom XA_MULTIPLE;
  72. Atom XA_DELETE;
  73. Atom XA_NULL;
  74. Atom XA_TEXT;
  75. Atom XA_HOST_NAME;
  76. Atom XA_SUN_FILE_HOST_NAME;
  77. Atom XA_SUN_ENUM_COUNT;
  78. Atom XA_SUN_DATA_LABEL;
  79. Atom XA_SUN_SELN_READONLY;
  80. Atom XA_SUN_ATM_FILE_NAME;
  81. Atom XA_SUN_ATM_METHODS;
  82. static void
  83. dndInitTargets(
  84. Display *dpy)
  85. {
  86. _DtSvcProcessLock();
  87. if (XA_TARGETS != 0) {
  88. _DtSvcProcessUnlock();
  89. return;
  90. }
  91. XA_TARGETS = DtGetAtom(dpy,"TARGETS");
  92. XA_TIMESTAMP = DtGetAtom(dpy,"TIMESTAMP");
  93. XA_MULTIPLE = DtGetAtom(dpy,"MULTIPLE");
  94. XA_DELETE = DtGetAtom(dpy,"DELETE");
  95. XA_NULL = DtGetAtom(dpy,"NULL");
  96. XA_TEXT = DtGetAtom(dpy,"TEXT");
  97. XA_HOST_NAME = DtGetAtom(dpy,"HOST_NAME");
  98. XA_SUN_FILE_HOST_NAME = DtGetAtom(dpy,"_SUN_FILE_HOST_NAME");
  99. XA_SUN_ENUM_COUNT = DtGetAtom(dpy,"_SUN_ENUMERATION_COUNT");
  100. XA_SUN_DATA_LABEL = DtGetAtom(dpy,"_SUN_DATA_LABEL");
  101. XA_SUN_SELN_READONLY = DtGetAtom(dpy,"_SUN_SELN_IS_READONLY");
  102. XA_SUN_ATM_FILE_NAME = DtGetAtom(dpy,"_SUN_ATM_FILE_NAME");
  103. XA_SUN_ATM_METHODS = DtGetAtom(dpy,
  104. "_SUN_ALTERNATE_TRANSPORT_METHODS");
  105. _DtSvcProcessUnlock();
  106. }
  107. /*********************************************************************
  108. *
  109. * Data Transfer Protocol Method Functions
  110. *
  111. *********************************************************************/
  112. static DtDndProtocol dndProtocolList[] = {
  113. DtDND_FILENAME_TRANSFER,
  114. DtDND_BUFFER_TRANSFER,
  115. DtDND_TEXT_TRANSFER,
  116. DtDND_NOOP_TRANSFER
  117. };
  118. extern DtDndMethods *_DtDndBuffTransferProtocolInitialize(Display*);
  119. extern DtDndMethods *_DtDndFileTransferProtocolInitialize(Display*);
  120. extern DtDndMethods *_DtDndTextTransferProtocolInitialize(Display*);
  121. /*
  122. * dndGetTransferMethods
  123. *
  124. * Return the transfer methods for a particular protocol
  125. */
  126. static DtDndMethods *
  127. dndGetTransferMethods(
  128. Display * display,
  129. DtDndProtocol protocol)
  130. {
  131. DtDndMethods * methods = NULL;
  132. switch (protocol) {
  133. case DtDND_BUFFER_TRANSFER:
  134. methods = _DtDndBuffTransferProtocolInitialize(display);
  135. break;
  136. case DtDND_FILENAME_TRANSFER:
  137. methods = _DtDndFileTransferProtocolInitialize(display);
  138. break;
  139. case DtDND_TEXT_TRANSFER:
  140. methods = _DtDndTextTransferProtocolInitialize(display);
  141. break;
  142. }
  143. return methods;
  144. }
  145. /*
  146. * _DtDndCreateExportTransfer
  147. *
  148. * Returns a transfer suitable for use by a drag initiator
  149. */
  150. DtDndTransfer *
  151. _DtDndCreateExportTransfer(
  152. DtDragInfo * dtDragInfo)
  153. {
  154. Display * dpy = XtDisplayOfObject(dtDragInfo->dragInitiator);
  155. DtDndTransfer * transfer;
  156. dndInitTargets(dpy);
  157. transfer = (DtDndTransfer *)XtMalloc(sizeof(DtDndTransfer));
  158. transfer->methods = dndGetTransferMethods(dpy,dtDragInfo->protocol);
  159. (*transfer->methods->getExportTargets)(dtDragInfo,
  160. &(transfer->targets), &(transfer->numTargets));
  161. return transfer;
  162. }
  163. /*
  164. * _DtDndCreateImportTransfers
  165. *
  166. * Create the list of transfers specified by the protocols.
  167. */
  168. DtDndTransfer *
  169. _DtDndCreateImportTransfers(
  170. DtDropInfo * dtDropInfo,
  171. Cardinal * numTransfers)
  172. {
  173. Display * dpy = XtDisplayOfObject(dtDropInfo->dropReceiver);
  174. DtDndTransfer * transfers;
  175. DtDndTransfer * transfer;
  176. Cardinal ii, jj;
  177. dndInitTargets(dpy);
  178. *numTransfers = 0;
  179. for (ii = 0; dndProtocolList[ii] != DtDND_NOOP_TRANSFER; ii++) {
  180. if (dtDropInfo->protocols & dndProtocolList[ii]) {
  181. (*numTransfers)++;
  182. }
  183. }
  184. if (*numTransfers == 0)
  185. return (DtDndTransfer *)NULL;
  186. transfers = (DtDndTransfer *)XtMalloc(
  187. *numTransfers * sizeof(DtDndTransfer));
  188. for (ii = 0, jj = 0; dndProtocolList[ii] != DtDND_NOOP_TRANSFER; ii++) {
  189. if (dtDropInfo->protocols & dndProtocolList[ii]) {
  190. transfer = &transfers[jj++];
  191. transfer->methods =
  192. dndGetTransferMethods(dpy,dndProtocolList[ii]);
  193. (*transfer->methods->getImportTargets)(dtDropInfo,
  194. &(transfer->targets), &(transfer->numTargets));
  195. }
  196. }
  197. return transfers;
  198. }
  199. /*
  200. * _DtDndDestroyTransfers
  201. *
  202. * Destroy the transfer list created by
  203. * _DtDndCreateExportTransfer() or _DtDndCreateImportTransfers()
  204. */
  205. void
  206. _DtDndDestroyTransfers(
  207. DtDndTransfer * transfers,
  208. Cardinal numTransfers)
  209. {
  210. Cardinal ii;
  211. for (ii = 0; ii < numTransfers; ii++) {
  212. XtFree((char *)transfers[ii].targets);
  213. }
  214. XtFree((char *)transfers);
  215. }
  216. /*
  217. * _DtDndTransferFromTargets
  218. *
  219. * Returns the transfer method that matches the target list
  220. */
  221. DtDndTransfer *
  222. _DtDndTransferFromTargets(
  223. DtDndTransfer * transfers,
  224. Cardinal numTransfers,
  225. Atom * targets,
  226. Cardinal numTargets)
  227. {
  228. Cardinal ii, jj, kk;
  229. for (ii = 0; ii < numTransfers; ii++) {
  230. for (jj = 0; jj < transfers[ii].numTargets; jj++) {
  231. for (kk = 0; kk < numTargets; kk++) {
  232. if (transfers[ii].targets[jj] == targets[kk]) {
  233. return &transfers[ii];
  234. }
  235. }
  236. }
  237. }
  238. return (DtDndTransfer *)NULL;
  239. }
  240. /*
  241. * _DtDndTransferAdd
  242. *
  243. * Add the targets to the requested transfer targets list
  244. * and call XmDropTransferAdd() to request them
  245. */
  246. void
  247. _DtDndTransferAdd(
  248. Widget dropTransfer,
  249. DtDropInfo * dtDropInfo,
  250. Atom * transferTargets,
  251. Cardinal numTransferTargets)
  252. {
  253. DtTransferInfo *transferInfo = dtDropInfo->transferInfo;
  254. XmDropTransferEntryRec * transferEntries;
  255. Cardinal numTransfers;
  256. int ii, jj;
  257. transferEntries = (XmDropTransferEntryRec *)
  258. XtMalloc(numTransferTargets * sizeof(XmDropTransferEntryRec));
  259. numTransfers = transferInfo->numTransferTargets + numTransferTargets;
  260. transferInfo->transferTargets = (Atom *)XtRealloc(
  261. (char *)transferInfo->transferTargets,
  262. numTransfers * sizeof(Atom));
  263. jj = transferInfo->numTransferTargets;
  264. for (ii = 0; ii < numTransferTargets; ii++) {
  265. transferEntries[ii].target = transferTargets[ii];
  266. transferEntries[ii].client_data = (XtPointer)dtDropInfo;
  267. transferInfo->transferTargets[ii+jj] = transferTargets[ii];
  268. }
  269. transferInfo->numTransferTargets = numTransfers;
  270. XmDropTransferAdd(dropTransfer, transferEntries, numTransferTargets);
  271. XtFree((char *)transferEntries);
  272. }
  273. /*********************************************************************
  274. *
  275. * Misc Debugging Functions
  276. *
  277. *********************************************************************/
  278. #ifdef DEBUG
  279. void
  280. _DtDndPrintTargets(
  281. Display *display,
  282. Atom *targets,
  283. Cardinal numTargets)
  284. {
  285. Cardinal ii;
  286. char *name;
  287. for (ii = 0; ii < numTargets; ii++) {
  288. name = XGetAtomName(display, targets[ii]);
  289. if (name) {
  290. printf("%s ", name);
  291. XFree(name);
  292. } else {
  293. printf("(null) ");
  294. }
  295. }
  296. printf("\n");
  297. }
  298. void
  299. _DtDndPrintTransfers(
  300. Display *display,
  301. DtDndTransfer *transfers,
  302. Cardinal numTransfers)
  303. {
  304. Cardinal ii;
  305. for (ii = 0; ii < numTransfers; ii++) {
  306. printf("%s\tTargets: ",transfers[ii].methods->name);
  307. _DtDndPrintTargets(display,
  308. transfers[ii].targets,transfers[ii].numTargets);
  309. }
  310. }
  311. #endif
  312. /*********************************************************************
  313. *
  314. * Misc Utility Functions
  315. *
  316. *********************************************************************/
  317. /*
  318. * Copy CallbackList
  319. */
  320. XtCallbackList
  321. _DtDndCopyCallbackList(
  322. XtCallbackList callbacks)
  323. {
  324. XtCallbackList cl;
  325. int ii, count;
  326. if (callbacks == NULL)
  327. return NULL;
  328. count = 1;
  329. for (cl = callbacks; cl->callback != NULL; cl++) {
  330. count++;
  331. }
  332. cl = (XtCallbackList)XtMalloc(count * sizeof(XtCallbackRec));
  333. for (ii = 0; ii < count; ii++) {
  334. cl[ii].callback = callbacks[ii].callback;
  335. cl[ii].closure = callbacks[ii].closure;
  336. }
  337. return cl;
  338. }
  339. /*
  340. * Call CallbackList
  341. */
  342. void
  343. _DtDndCallCallbackList(
  344. Widget widget,
  345. XtCallbackList callbacks,
  346. XtPointer calldata)
  347. {
  348. XtCallbackList cl;
  349. if (callbacks == NULL)
  350. return;
  351. for (cl = callbacks; cl->callback != NULL; cl++) {
  352. (*cl->callback)(widget, cl->closure, calldata);
  353. }
  354. }
  355. /*
  356. * Misc Varargs Utility Functions
  357. */
  358. int
  359. _DtDndCountVarArgs(
  360. va_list vaList)
  361. {
  362. XtPointer argPtr;
  363. Cardinal argCount;
  364. argCount = 0;
  365. for (argPtr = va_arg(vaList,String);
  366. argPtr != NULL;
  367. argPtr = va_arg(vaList,String)) {
  368. va_arg(vaList, XtArgVal);
  369. argCount++;
  370. }
  371. return argCount;
  372. }
  373. void
  374. _DtDndArgListFromVarArgs(
  375. va_list vaList,
  376. Cardinal maxArgs,
  377. ArgList *argListReturn,
  378. Cardinal *argCountReturn)
  379. {
  380. ArgList argList;
  381. Cardinal argCount;
  382. XtPointer argPtr;
  383. if (0 == maxArgs)
  384. {
  385. *argListReturn = NULL;
  386. *argCountReturn = 0;
  387. return;
  388. }
  389. argList = (ArgList)XtMalloc((unsigned)(maxArgs * sizeof(Arg)));
  390. argCount = 0;
  391. for (argPtr = va_arg(vaList,String);
  392. argPtr != NULL;
  393. argPtr = va_arg(vaList,String)) {
  394. XtSetArg(argList[argCount], argPtr, va_arg(vaList,XtArgVal));
  395. argCount++;
  396. }
  397. *argListReturn = argList;
  398. *argCountReturn = argCount;
  399. }
  400. /*
  401. * Returns the network node/host name.
  402. */
  403. String
  404. _DtDndGetHostName()
  405. {
  406. static char *nodename;
  407. _DtSvcProcessLock();
  408. if (nodename == NULL) {
  409. struct utsname un;
  410. if (uname(&un) == -1) {
  411. _DtSimpleError(DtProgName,DtError, NULL, "uname", NULL);
  412. nodename = XtNewString("nodename");
  413. } else {
  414. nodename = XtNewString(un.nodename);
  415. }
  416. }
  417. _DtSvcProcessUnlock();
  418. return nodename;
  419. }