xres.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. Copyright (c) 2002 XFree86 Inc
  3. */
  4. #ifdef HAVE_DIX_CONFIG_H
  5. #include <dix-config.h>
  6. #endif
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <X11/X.h>
  10. #include <X11/Xproto.h>
  11. #include "misc.h"
  12. #include "os.h"
  13. #include "dixstruct.h"
  14. #include "extnsionst.h"
  15. #include "swaprep.h"
  16. #include <X11/extensions/XResproto.h>
  17. #include "pixmapstr.h"
  18. #include "windowstr.h"
  19. #include "gcstruct.h"
  20. #include "extinit.h"
  21. static int
  22. ProcXResQueryVersion(ClientPtr client)
  23. {
  24. xXResQueryVersionReply rep;
  25. REQUEST_SIZE_MATCH(xXResQueryVersionReq);
  26. rep.type = X_Reply;
  27. rep.length = 0;
  28. rep.sequenceNumber = client->sequence;
  29. rep.server_major = XRES_MAJOR_VERSION;
  30. rep.server_minor = XRES_MINOR_VERSION;
  31. if (client->swapped) {
  32. swaps(&rep.sequenceNumber);
  33. swapl(&rep.length);
  34. swaps(&rep.server_major);
  35. swaps(&rep.server_minor);
  36. }
  37. WriteToClient(client, sizeof(xXResQueryVersionReply), (char *) &rep);
  38. return (client->noClientException);
  39. }
  40. static int
  41. ProcXResQueryClients(ClientPtr client)
  42. {
  43. /* REQUEST(xXResQueryClientsReq); */
  44. xXResQueryClientsReply rep;
  45. int *current_clients;
  46. int i, num_clients;
  47. REQUEST_SIZE_MATCH(xXResQueryClientsReq);
  48. current_clients = ALLOCATE_LOCAL((currentMaxClients - 1) * sizeof(int));
  49. num_clients = 0;
  50. for (i = 1; i < currentMaxClients; i++) {
  51. if (clients[i]) {
  52. current_clients[num_clients] = i;
  53. num_clients++;
  54. }
  55. }
  56. rep.type = X_Reply;
  57. rep.sequenceNumber = client->sequence;
  58. rep.num_clients = num_clients;
  59. rep.length = rep.num_clients * sz_xXResClient >> 2;
  60. if (client->swapped) {
  61. swaps(&rep.sequenceNumber);
  62. swapl(&rep.length);
  63. swapl(&rep.num_clients);
  64. }
  65. WriteToClient(client, sizeof(xXResQueryClientsReply), (char *) &rep);
  66. if (num_clients) {
  67. xXResClient scratch;
  68. for (i = 0; i < num_clients; i++) {
  69. scratch.resource_base = clients[current_clients[i]]->clientAsMask;
  70. scratch.resource_mask = RESOURCE_ID_MASK;
  71. if (client->swapped) {
  72. swapl(&scratch.resource_base);
  73. swapl(&scratch.resource_mask);
  74. }
  75. WriteToClient(client, sz_xXResClient, (char *) &scratch);
  76. }
  77. }
  78. DEALLOCATE_LOCAL(current_clients);
  79. return (client->noClientException);
  80. }
  81. static void
  82. ResFindAllRes(pointer value, XID id, RESTYPE type, pointer cdata)
  83. {
  84. int *counts = (int *) cdata;
  85. counts[(type & TypeMask) - 1]++;
  86. }
  87. static int
  88. ProcXResQueryClientResources(ClientPtr client)
  89. {
  90. REQUEST(xXResQueryClientResourcesReq);
  91. xXResQueryClientResourcesReply rep;
  92. int i, clientID, num_types;
  93. int *counts;
  94. REQUEST_SIZE_MATCH(xXResQueryClientResourcesReq);
  95. clientID = CLIENT_ID(stuff->xid);
  96. /* we could remove the (clientID == 0) check if we wanted to allow
  97. probing the X-server's resource usage */
  98. if (!clientID || (clientID >= currentMaxClients) || !clients[clientID]) {
  99. client->errorValue = stuff->xid;
  100. return BadValue;
  101. }
  102. counts = ALLOCATE_LOCAL((lastResourceType + 1) * sizeof(int));
  103. memset(counts, 0, (lastResourceType + 1) * sizeof(int));
  104. FindAllClientResources(clients[clientID], ResFindAllRes, counts);
  105. num_types = 0;
  106. for (i = 0; i <= lastResourceType; i++) {
  107. if (counts[i])
  108. num_types++;
  109. }
  110. rep.type = X_Reply;
  111. rep.sequenceNumber = client->sequence;
  112. rep.num_types = num_types;
  113. rep.length = rep.num_types * sz_xXResType >> 2;
  114. if (client->swapped) {
  115. swaps(&rep.sequenceNumber);
  116. swapl(&rep.length);
  117. swapl(&rep.num_types);
  118. }
  119. WriteToClient(client, sizeof(xXResQueryClientResourcesReply),
  120. (char *) &rep);
  121. if (num_types) {
  122. xXResType scratch;
  123. for (i = 0; i < lastResourceType; i++) {
  124. if (!counts[i])
  125. continue;
  126. if (!ResourceNames[i + 1]) {
  127. char buf[40];
  128. snprintf(buf, sizeof(buf), "Unregistered resource %i", i + 1);
  129. RegisterResourceName(i + 1, buf);
  130. }
  131. scratch.resource_type = ResourceNames[i + 1];
  132. scratch.count = counts[i];
  133. if (client->swapped) {
  134. swapl(&scratch.resource_type);
  135. swapl(&scratch.count);
  136. }
  137. WriteToClient(client, sz_xXResType, (char *) &scratch);
  138. }
  139. }
  140. DEALLOCATE_LOCAL(counts);
  141. return (client->noClientException);
  142. }
  143. static unsigned long
  144. ResGetApproxPixmapBytes(PixmapPtr pix)
  145. {
  146. unsigned long nPixels;
  147. int bytesPerPixel;
  148. bytesPerPixel = pix->drawable.bitsPerPixel >> 3;
  149. nPixels = pix->drawable.width * pix->drawable.height;
  150. /* Divide by refcnt as pixmap could be shared between clients,
  151. * so total pixmap mem is shared between these.
  152. */
  153. return (nPixels * bytesPerPixel) / pix->refcnt;
  154. }
  155. static void
  156. ResFindPixmaps(pointer value, XID id, pointer cdata)
  157. {
  158. unsigned long *bytes = (unsigned long *) cdata;
  159. PixmapPtr pix = (PixmapPtr) value;
  160. *bytes += ResGetApproxPixmapBytes(pix);
  161. }
  162. static void
  163. ResFindWindowPixmaps(pointer value, XID id, pointer cdata)
  164. {
  165. unsigned long *bytes = (unsigned long *) cdata;
  166. WindowPtr pWin = (WindowPtr) value;
  167. if (pWin->backgroundState == BackgroundPixmap)
  168. *bytes += ResGetApproxPixmapBytes(pWin->background.pixmap);
  169. if (pWin->border.pixmap != NULL && !pWin->borderIsPixel)
  170. *bytes += ResGetApproxPixmapBytes(pWin->border.pixmap);
  171. }
  172. static void
  173. ResFindGCPixmaps(pointer value, XID id, pointer cdata)
  174. {
  175. unsigned long *bytes = (unsigned long *) cdata;
  176. GCPtr pGC = (GCPtr) value;
  177. if (pGC->stipple != NULL)
  178. *bytes += ResGetApproxPixmapBytes(pGC->stipple);
  179. if (pGC->tile.pixmap != NULL && !pGC->tileIsPixel)
  180. *bytes += ResGetApproxPixmapBytes(pGC->tile.pixmap);
  181. }
  182. static int
  183. ProcXResQueryClientPixmapBytes(ClientPtr client)
  184. {
  185. REQUEST(xXResQueryClientPixmapBytesReq);
  186. xXResQueryClientPixmapBytesReply rep;
  187. int clientID;
  188. unsigned long bytes;
  189. REQUEST_SIZE_MATCH(xXResQueryClientPixmapBytesReq);
  190. clientID = CLIENT_ID(stuff->xid);
  191. /* we could remove the (clientID == 0) check if we wanted to allow
  192. probing the X-server's resource usage */
  193. if (!clientID || (clientID >= currentMaxClients) || !clients[clientID]) {
  194. client->errorValue = stuff->xid;
  195. return BadValue;
  196. }
  197. bytes = 0;
  198. FindClientResourcesByType(clients[clientID], RT_PIXMAP, ResFindPixmaps,
  199. (pointer) (&bytes));
  200. /*
  201. * Make sure win background pixmaps also held to account.
  202. */
  203. FindClientResourcesByType(clients[clientID], RT_WINDOW,
  204. ResFindWindowPixmaps, (pointer) (&bytes));
  205. /*
  206. * GC Tile & Stipple pixmaps too.
  207. */
  208. FindClientResourcesByType(clients[clientID], RT_GC,
  209. ResFindGCPixmaps, (pointer) (&bytes));
  210. rep.type = X_Reply;
  211. rep.sequenceNumber = client->sequence;
  212. rep.length = 0;
  213. rep.bytes = bytes;
  214. #ifdef _XSERVER64
  215. rep.bytes_overflow = bytes >> 32;
  216. #else
  217. rep.bytes_overflow = 0;
  218. #endif
  219. if (client->swapped) {
  220. swaps(&rep.sequenceNumber);
  221. swapl(&rep.length);
  222. swapl(&rep.bytes);
  223. swapl(&rep.bytes_overflow);
  224. }
  225. WriteToClient(client, sizeof(xXResQueryClientPixmapBytesReply),
  226. (char *) &rep);
  227. return (client->noClientException);
  228. }
  229. static void
  230. ResResetProc(ExtensionEntry * extEntry)
  231. {
  232. }
  233. static int
  234. ProcResDispatch(ClientPtr client)
  235. {
  236. REQUEST(xReq);
  237. switch (stuff->data) {
  238. case X_XResQueryVersion:
  239. return ProcXResQueryVersion(client);
  240. case X_XResQueryClients:
  241. return ProcXResQueryClients(client);
  242. case X_XResQueryClientResources:
  243. return ProcXResQueryClientResources(client);
  244. case X_XResQueryClientPixmapBytes:
  245. return ProcXResQueryClientPixmapBytes(client);
  246. default:
  247. break;
  248. }
  249. return BadRequest;
  250. }
  251. static int
  252. SProcXResQueryVersion(ClientPtr client)
  253. {
  254. REQUEST_SIZE_MATCH(xXResQueryVersionReq);
  255. return ProcXResQueryVersion(client);
  256. }
  257. static int
  258. SProcXResQueryClientResources(ClientPtr client)
  259. {
  260. REQUEST(xXResQueryClientResourcesReq);
  261. REQUEST_SIZE_MATCH(xXResQueryClientResourcesReq);
  262. swapl(&stuff->xid);
  263. return ProcXResQueryClientResources(client);
  264. }
  265. static int
  266. SProcXResQueryClientPixmapBytes(ClientPtr client)
  267. {
  268. REQUEST(xXResQueryClientPixmapBytesReq);
  269. REQUEST_SIZE_MATCH(xXResQueryClientPixmapBytesReq);
  270. swapl(&stuff->xid);
  271. return ProcXResQueryClientPixmapBytes(client);
  272. }
  273. static int
  274. SProcResDispatch(ClientPtr client)
  275. {
  276. REQUEST(xReq);
  277. swaps(&stuff->length);
  278. switch (stuff->data) {
  279. case X_XResQueryVersion:
  280. return SProcXResQueryVersion(client);
  281. case X_XResQueryClients: /* nothing to swap */
  282. return ProcXResQueryClients(client);
  283. case X_XResQueryClientResources:
  284. return SProcXResQueryClientResources(client);
  285. case X_XResQueryClientPixmapBytes:
  286. return SProcXResQueryClientPixmapBytes(client);
  287. default:
  288. break;
  289. }
  290. return BadRequest;
  291. }
  292. void
  293. ResExtensionInit(INITARGS)
  294. {
  295. (void) AddExtension(XRES_NAME, 0, 0,
  296. ProcResDispatch, SProcResDispatch,
  297. ResResetProc, StandardMinorOpcode);
  298. RegisterResourceName(RT_NONE, "NONE");
  299. RegisterResourceName(RT_WINDOW, "WINDOW");
  300. RegisterResourceName(RT_PIXMAP, "PIXMAP");
  301. RegisterResourceName(RT_GC, "GC");
  302. RegisterResourceName(RT_FONT, "FONT");
  303. RegisterResourceName(RT_CURSOR, "CURSOR");
  304. RegisterResourceName(RT_COLORMAP, "COLORMAP");
  305. RegisterResourceName(RT_CMAPENTRY, "COLORMAP ENTRY");
  306. RegisterResourceName(RT_OTHERCLIENT, "OTHER CLIENT");
  307. RegisterResourceName(RT_PASSIVEGRAB, "PASSIVE GRAB");
  308. }