alloc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <draw.h>
  12. Image*
  13. allocimage(Display *d, Rectangle r, uint32_t chan, int repl, uint32_t val)
  14. {
  15. Image* i;
  16. i = _allocimage(nil, d, r, chan, repl, val, 0, 0);
  17. if (i)
  18. setmalloctag(i, getcallerpc());
  19. return i;
  20. }
  21. Image*
  22. _allocimage(Image *ai, Display *d, Rectangle r, uint32_t chan, int repl,
  23. uint32_t val, int screenid, int refresh)
  24. {
  25. uint8_t *a;
  26. char *err;
  27. Image *i;
  28. Rectangle clipr;
  29. int id;
  30. int depth;
  31. err = 0;
  32. i = 0;
  33. if(chan == 0){
  34. werrstr("bad channel descriptor");
  35. return nil;
  36. }
  37. depth = chantodepth(chan);
  38. if(depth == 0){
  39. err = "bad channel descriptor";
  40. Error:
  41. if(err)
  42. werrstr("allocimage: %s", err);
  43. else
  44. werrstr("allocimage: %r");
  45. free(i);
  46. return 0;
  47. }
  48. /* flush pending data so we don't get error allocating the image */
  49. flushimage(d, 0);
  50. a = bufimage(d, 1+4+4+1+4+1+4*4+4*4+4);
  51. if(a == 0)
  52. goto Error;
  53. d->imageid++;
  54. id = d->imageid;
  55. a[0] = 'b';
  56. BPLONG(a+1, id);
  57. BPLONG(a+5, screenid);
  58. a[9] = refresh;
  59. BPLONG(a+10, chan);
  60. a[14] = repl;
  61. BPLONG(a+15, r.min.x);
  62. BPLONG(a+19, r.min.y);
  63. BPLONG(a+23, r.max.x);
  64. BPLONG(a+27, r.max.y);
  65. if(repl)
  66. /* huge but not infinite, so various offsets will leave it huge, not overflow */
  67. clipr = Rect(-0x3FFFFFFF, -0x3FFFFFFF, 0x3FFFFFFF, 0x3FFFFFFF);
  68. else
  69. clipr = r;
  70. BPLONG(a+31, clipr.min.x);
  71. BPLONG(a+35, clipr.min.y);
  72. BPLONG(a+39, clipr.max.x);
  73. BPLONG(a+43, clipr.max.y);
  74. BPLONG(a+47, val);
  75. if(flushimage(d, 0) < 0)
  76. goto Error;
  77. if(ai)
  78. i = ai;
  79. else{
  80. i = malloc(sizeof(Image));
  81. if(i == nil){
  82. a = bufimage(d, 1+4);
  83. if(a){
  84. a[0] = 'f';
  85. BPLONG(a+1, id);
  86. flushimage(d, 0);
  87. }
  88. goto Error;
  89. }
  90. }
  91. i->display = d;
  92. i->id = id;
  93. i->depth = depth;
  94. i->chan = chan;
  95. i->r = r;
  96. i->clipr = clipr;
  97. i->repl = repl;
  98. i->screen = 0;
  99. i->next = 0;
  100. return i;
  101. }
  102. Image*
  103. namedimage(Display *d, char *name)
  104. {
  105. uint8_t *a;
  106. char *err, buf[12*12+1];
  107. Image *i;
  108. int id, n;
  109. uint32_t chan;
  110. err = 0;
  111. i = 0;
  112. n = strlen(name);
  113. if(n >= 256){
  114. err = "name too long";
  115. Error:
  116. if(err)
  117. werrstr("namedimage: %s", err);
  118. else
  119. werrstr("namedimage: %r");
  120. if(i)
  121. free(i);
  122. return 0;
  123. }
  124. /* flush pending data so we don't get error allocating the image */
  125. flushimage(d, 0);
  126. a = bufimage(d, 1+4+1+n);
  127. if(a == 0)
  128. goto Error;
  129. d->imageid++;
  130. id = d->imageid;
  131. a[0] = 'n';
  132. BPLONG(a+1, id);
  133. a[5] = n;
  134. memmove(a+6, name, n);
  135. if(flushimage(d, 0) < 0)
  136. goto Error;
  137. if(pread(d->ctlfd, buf, sizeof buf, 0) < 12*12)
  138. goto Error;
  139. buf[12*12] = '\0';
  140. i = malloc(sizeof(Image));
  141. if(i == nil){
  142. Error1:
  143. a = bufimage(d, 1+4);
  144. if(a){
  145. a[0] = 'f';
  146. BPLONG(a+1, id);
  147. flushimage(d, 0);
  148. }
  149. goto Error;
  150. }
  151. i->display = d;
  152. i->id = id;
  153. if((chan=strtochan(buf+2*12))==0){
  154. werrstr("bad channel '%.12s' from devdraw", buf+2*12);
  155. goto Error1;
  156. }
  157. i->chan = chan;
  158. i->depth = chantodepth(chan);
  159. i->repl = atoi(buf+3*12);
  160. i->r.min.x = atoi(buf+4*12);
  161. i->r.min.y = atoi(buf+5*12);
  162. i->r.max.x = atoi(buf+6*12);
  163. i->r.max.y = atoi(buf+7*12);
  164. i->clipr.min.x = atoi(buf+8*12);
  165. i->clipr.min.y = atoi(buf+9*12);
  166. i->clipr.max.x = atoi(buf+10*12);
  167. i->clipr.max.y = atoi(buf+11*12);
  168. i->screen = 0;
  169. i->next = 0;
  170. return i;
  171. }
  172. int
  173. nameimage(Image *i, char *name, int in)
  174. {
  175. uint8_t *a;
  176. int n;
  177. n = strlen(name);
  178. a = bufimage(i->display, 1+4+1+1+n);
  179. if(a == 0)
  180. return 0;
  181. a[0] = 'N';
  182. BPLONG(a+1, i->id);
  183. a[5] = in;
  184. a[6] = n;
  185. memmove(a+7, name, n);
  186. if(flushimage(i->display, 0) < 0)
  187. return 0;
  188. return 1;
  189. }
  190. int
  191. _freeimage1(Image *i)
  192. {
  193. uint8_t *a;
  194. Display *d;
  195. Image *w;
  196. if(i == 0 || i->display == 0)
  197. return 0;
  198. /* make sure no refresh events occur on this if we block in the write */
  199. d = i->display;
  200. /* flush pending data so we don't get error deleting the image */
  201. flushimage(d, 0);
  202. a = bufimage(d, 1+4);
  203. if(a == 0)
  204. return -1;
  205. a[0] = 'f';
  206. BPLONG(a+1, i->id);
  207. if(i->screen){
  208. w = d->windows;
  209. if(w == i)
  210. d->windows = i->next;
  211. else
  212. while(w){
  213. if(w->next == i){
  214. w->next = i->next;
  215. break;
  216. }
  217. w = w->next;
  218. }
  219. }
  220. if(flushimage(d, i->screen!=0) < 0)
  221. return -1;
  222. return 0;
  223. }
  224. int
  225. freeimage(Image *i)
  226. {
  227. int ret;
  228. ret = _freeimage1(i);
  229. free(i);
  230. return ret;
  231. }