extension.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /***********************************************************
  2. Copyright 1987, 1998 The Open Group
  3. Permission to use, copy, modify, distribute, and sell this software and its
  4. documentation for any purpose is hereby granted without fee, provided that
  5. the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation.
  8. The above copyright notice and this permission notice shall be included in
  9. all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  14. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. Except as contained in this notice, the name of The Open Group shall not be
  17. used in advertising or otherwise to promote the sale, use or other dealings
  18. in this Software without prior written authorization from The Open Group.
  19. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
  20. All Rights Reserved
  21. Permission to use, copy, modify, and distribute this software and its
  22. documentation for any purpose and without fee is hereby granted,
  23. provided that the above copyright notice appear in all copies and that
  24. both that copyright notice and this permission notice appear in
  25. supporting documentation, and that the name of Digital not be
  26. used in advertising or publicity pertaining to distribution of the
  27. software without specific, written prior permission.
  28. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  29. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  30. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  31. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  32. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  33. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  34. SOFTWARE.
  35. ******************************************************************/
  36. #ifdef HAVE_DIX_CONFIG_H
  37. #include <dix-config.h>
  38. #endif
  39. #include <X11/X.h>
  40. #include <X11/Xproto.h>
  41. #include "misc.h"
  42. #include "dixstruct.h"
  43. #include "extnsionst.h"
  44. #include "gcstruct.h"
  45. #include "scrnintstr.h"
  46. #include "dispatch.h"
  47. #define EXTENSION_BASE 128
  48. #define EXTENSION_EVENT_BASE 64
  49. #define LAST_EVENT 128
  50. #define LAST_ERROR 255
  51. ScreenProcEntry AuxillaryScreenProcs[MAXSCREENS];
  52. static ExtensionEntry **extensions = (ExtensionEntry **) NULL;
  53. int lastEvent = EXTENSION_EVENT_BASE;
  54. static int lastError = FirstExtensionError;
  55. static unsigned int NumExtensions = 0;
  56. extern int extensionPrivateLen;
  57. extern unsigned *extensionPrivateSizes;
  58. extern unsigned totalExtensionSize;
  59. static void
  60. InitExtensionPrivates(ExtensionEntry * ext)
  61. {
  62. char *ptr;
  63. DevUnion *ppriv;
  64. unsigned *sizes;
  65. unsigned size;
  66. int i;
  67. if (totalExtensionSize == sizeof(ExtensionEntry))
  68. ppriv = (DevUnion *) NULL;
  69. else
  70. ppriv = (DevUnion *) (ext + 1);
  71. ext->devPrivates = ppriv;
  72. sizes = extensionPrivateSizes;
  73. ptr = (char *) (ppriv + extensionPrivateLen);
  74. for (i = extensionPrivateLen; --i >= 0; ppriv++, sizes++) {
  75. if ((size = *sizes)) {
  76. ppriv->ptr = (pointer) ptr;
  77. ptr += size;
  78. }
  79. else
  80. ppriv->ptr = (pointer) NULL;
  81. }
  82. }
  83. _X_EXPORT ExtensionEntry *
  84. AddExtension(char *name, int NumEvents, int NumErrors,
  85. int (*MainProc) (ClientPtr c1),
  86. int (*SwappedMainProc) (ClientPtr c2),
  87. void (*CloseDownProc) (ExtensionEntry * e),
  88. unsigned short (*MinorOpcodeProc) (ClientPtr c3))
  89. {
  90. int i;
  91. ExtensionEntry *ext, **newexts;
  92. if (!MainProc || !SwappedMainProc || !CloseDownProc || !MinorOpcodeProc)
  93. return ((ExtensionEntry *) NULL);
  94. if ((lastEvent + NumEvents > LAST_EVENT) ||
  95. (unsigned) (lastError + NumErrors > LAST_ERROR))
  96. return ((ExtensionEntry *) NULL);
  97. ext = malloc(totalExtensionSize);
  98. if (!ext)
  99. return ((ExtensionEntry *) NULL);
  100. bzero(ext, totalExtensionSize);
  101. InitExtensionPrivates(ext);
  102. ext->name = malloc(strlen(name) + 1);
  103. ext->num_aliases = 0;
  104. ext->aliases = (char **) NULL;
  105. if (!ext->name) {
  106. free(ext);
  107. return ((ExtensionEntry *) NULL);
  108. }
  109. strcpy(ext->name, name);
  110. i = NumExtensions;
  111. newexts = (ExtensionEntry **) realloc(extensions,
  112. (i + 1) * sizeof(ExtensionEntry *));
  113. if (!newexts) {
  114. free(ext->name);
  115. free(ext);
  116. return ((ExtensionEntry *) NULL);
  117. }
  118. NumExtensions++;
  119. extensions = newexts;
  120. extensions[i] = ext;
  121. ext->index = i;
  122. ext->base = i + EXTENSION_BASE;
  123. ext->CloseDown = CloseDownProc;
  124. ext->MinorOpcode = MinorOpcodeProc;
  125. ProcVector[i + EXTENSION_BASE] = MainProc;
  126. SwappedProcVector[i + EXTENSION_BASE] = SwappedMainProc;
  127. if (NumEvents) {
  128. ext->eventBase = lastEvent;
  129. ext->eventLast = lastEvent + NumEvents;
  130. lastEvent += NumEvents;
  131. }
  132. else {
  133. ext->eventBase = 0;
  134. ext->eventLast = 0;
  135. }
  136. if (NumErrors) {
  137. ext->errorBase = lastError;
  138. ext->errorLast = lastError + NumErrors;
  139. lastError += NumErrors;
  140. }
  141. else {
  142. ext->errorBase = 0;
  143. ext->errorLast = 0;
  144. }
  145. return (ext);
  146. }
  147. static int
  148. FindExtension(char *extname, int len)
  149. {
  150. int i, j;
  151. for (i = 0; i < NumExtensions; i++) {
  152. if ((strlen(extensions[i]->name) == len) &&
  153. !strncmp(extname, extensions[i]->name, len))
  154. break;
  155. for (j = extensions[i]->num_aliases; --j >= 0;) {
  156. if ((strlen(extensions[i]->aliases[j]) == len) &&
  157. !strncmp(extname, extensions[i]->aliases[j], len))
  158. break;
  159. }
  160. if (j >= 0)
  161. break;
  162. }
  163. return ((i == NumExtensions) ? -1 : i);
  164. }
  165. _X_EXPORT void
  166. DeclareExtensionSecurity(char *extname, Bool secure)
  167. {
  168. }
  169. _X_EXPORT unsigned short
  170. StandardMinorOpcode(ClientPtr client)
  171. {
  172. return ((xReq *) client->requestBuffer)->data;
  173. }
  174. _X_EXPORT unsigned short
  175. MinorOpcodeOfRequest(ClientPtr client)
  176. {
  177. unsigned char major;
  178. major = ((xReq *) client->requestBuffer)->reqType;
  179. if (major < EXTENSION_BASE)
  180. return 0;
  181. major -= EXTENSION_BASE;
  182. if (major >= NumExtensions)
  183. return 0;
  184. return (*extensions[major]->MinorOpcode) (client);
  185. }
  186. void
  187. CloseDownExtensions()
  188. {
  189. int i, j;
  190. for (i = NumExtensions - 1; i >= 0; i--) {
  191. (*extensions[i]->CloseDown) (extensions[i]);
  192. NumExtensions = i;
  193. free(extensions[i]->name);
  194. for (j = extensions[i]->num_aliases; --j >= 0;)
  195. free(extensions[i]->aliases[j]);
  196. free(extensions[i]->aliases);
  197. free(extensions[i]);
  198. }
  199. free(extensions);
  200. extensions = (ExtensionEntry **) NULL;
  201. lastEvent = EXTENSION_EVENT_BASE;
  202. lastError = FirstExtensionError;
  203. for (i = 0; i < MAXSCREENS; i++) {
  204. ScreenProcEntry *spentry = &AuxillaryScreenProcs[i];
  205. while (spentry->num) {
  206. spentry->num--;
  207. free(spentry->procList[spentry->num].name);
  208. }
  209. free(spentry->procList);
  210. spentry->procList = (ProcEntryPtr) NULL;
  211. }
  212. }
  213. int
  214. ProcQueryExtension(ClientPtr client)
  215. {
  216. xQueryExtensionReply reply = {0};
  217. int i;
  218. REQUEST(xQueryExtensionReq);
  219. REQUEST_FIXED_SIZE(xQueryExtensionReq, stuff->nbytes);
  220. reply.type = X_Reply;
  221. reply.length = 0;
  222. reply.major_opcode = 0;
  223. reply.sequenceNumber = client->sequence;
  224. if (!NumExtensions)
  225. reply.present = xFalse;
  226. else {
  227. i = FindExtension((char *) &stuff[1], stuff->nbytes);
  228. if (i < 0
  229. )
  230. reply.present = xFalse;
  231. else {
  232. reply.present = xTrue;
  233. reply.major_opcode = extensions[i]->base;
  234. reply.first_event = extensions[i]->eventBase;
  235. reply.first_error = extensions[i]->errorBase;
  236. }
  237. }
  238. WriteReplyToClient(client, sizeof(xQueryExtensionReply), &reply);
  239. return (client->noClientException);
  240. }
  241. int
  242. ProcListExtensions(ClientPtr client)
  243. {
  244. xListExtensionsReply reply = {0};
  245. char *bufptr, *buffer;
  246. int total_length = 0;
  247. REQUEST_SIZE_MATCH(xReq);
  248. reply.type = X_Reply;
  249. reply.nExtensions = 0;
  250. reply.length = 0;
  251. reply.sequenceNumber = client->sequence;
  252. buffer = NULL;
  253. if (NumExtensions) {
  254. int i, j;
  255. for (i = 0; i < NumExtensions; i++) {
  256. total_length += strlen(extensions[i]->name) + 1;
  257. reply.nExtensions += 1 + extensions[i]->num_aliases;
  258. for (j = extensions[i]->num_aliases; --j >= 0;)
  259. total_length += strlen(extensions[i]->aliases[j]) + 1;
  260. }
  261. reply.length = (total_length + 3) >> 2;
  262. buffer = bufptr = (char *) ALLOCATE_LOCAL(total_length);
  263. if (!buffer)
  264. return (BadAlloc);
  265. for (i = 0; i < NumExtensions; i++) {
  266. int len;
  267. *bufptr++ = len = strlen(extensions[i]->name);
  268. memmove(bufptr, extensions[i]->name, len);
  269. bufptr += len;
  270. for (j = extensions[i]->num_aliases; --j >= 0;) {
  271. *bufptr++ = len = strlen(extensions[i]->aliases[j]);
  272. memmove(bufptr, extensions[i]->aliases[j], len);
  273. bufptr += len;
  274. }
  275. }
  276. }
  277. WriteReplyToClient(client, sizeof(xListExtensionsReply), &reply);
  278. if (reply.length) {
  279. WriteToClient(client, total_length, buffer);
  280. DEALLOCATE_LOCAL(buffer);
  281. }
  282. return (client->noClientException);
  283. }
  284. ExtensionLookupProc
  285. LookupProc(char *name, GCPtr pGC)
  286. {
  287. int i;
  288. ScreenProcEntry *spentry;
  289. spentry = &AuxillaryScreenProcs[pGC->pScreen->myNum];
  290. if (spentry->num) {
  291. for (i = 0; i < spentry->num; i++)
  292. if (strcmp(name, spentry->procList[i].name) == 0)
  293. return (spentry->procList[i].proc);
  294. }
  295. return (ExtensionLookupProc) NULL;
  296. }
  297. Bool
  298. RegisterProc(char *name, GC * pGC, ExtensionLookupProc proc)
  299. {
  300. return RegisterScreenProc(name, pGC->pScreen, proc);
  301. }
  302. Bool
  303. RegisterScreenProc(char *name, ScreenPtr pScreen, ExtensionLookupProc proc)
  304. {
  305. ScreenProcEntry *spentry;
  306. ProcEntryPtr procEntry = (ProcEntryPtr) NULL;
  307. char *newname;
  308. int i;
  309. spentry = &AuxillaryScreenProcs[pScreen->myNum];
  310. /* first replace duplicates */
  311. if (spentry->num) {
  312. for (i = 0; i < spentry->num; i++)
  313. if (strcmp(name, spentry->procList[i].name) == 0) {
  314. procEntry = &spentry->procList[i];
  315. break;
  316. }
  317. }
  318. if (procEntry)
  319. procEntry->proc = proc;
  320. else {
  321. newname = malloc(strlen(name) + 1);
  322. if (!newname)
  323. return FALSE;
  324. procEntry = (ProcEntryPtr)
  325. realloc(spentry->procList,
  326. sizeof(ProcEntryRec) * (spentry->num + 1));
  327. if (!procEntry) {
  328. free(newname);
  329. return FALSE;
  330. }
  331. spentry->procList = procEntry;
  332. procEntry += spentry->num;
  333. procEntry->name = newname;
  334. strcpy(newname, name);
  335. procEntry->proc = proc;
  336. spentry->num++;
  337. }
  338. return TRUE;
  339. }