region.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. /*
  2. * Server-side region objects. Based on the X11 implementation.
  3. *
  4. * Copyright 1993, 1994, 1995, 2004 Alexandre Julliard
  5. * Modifications and additions: Copyright 1998 Huw Davies
  6. * 1999 Alex Korobka
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  21. *
  22. * Note:
  23. * This is a simplified version of the code, without all the explanations.
  24. * Check the equivalent GDI code to make sense of it.
  25. */
  26. /************************************************************************
  27. Copyright (c) 1987, 1988 X Consortium
  28. Permission is hereby granted, free of charge, to any person obtaining a copy
  29. of this software and associated documentation files (the "Software"), to deal
  30. in the Software without restriction, including without limitation the rights
  31. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  32. copies of the Software, and to permit persons to whom the Software is
  33. furnished to do so, subject to the following conditions:
  34. The above copyright notice and this permission notice shall be included in
  35. all copies or substantial portions of the Software.
  36. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  37. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  38. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  39. X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  40. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  41. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  42. Except as contained in this notice, the name of the X Consortium shall not be
  43. used in advertising or otherwise to promote the sale, use or other dealings
  44. in this Software without prior written authorization from the X Consortium.
  45. Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
  46. All Rights Reserved
  47. Permission to use, copy, modify, and distribute this software and its
  48. documentation for any purpose and without fee is hereby granted,
  49. provided that the above copyright notice appear in all copies and that
  50. both that copyright notice and this permission notice appear in
  51. supporting documentation, and that the name of Digital not be
  52. used in advertising or publicity pertaining to distribution of the
  53. software without specific, written prior permission.
  54. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  55. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  56. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  57. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  58. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  59. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  60. SOFTWARE.
  61. ************************************************************************/
  62. #include <stdarg.h>
  63. #include <stdlib.h>
  64. #include <string.h>
  65. #include "ntstatus.h"
  66. #define WIN32_NO_STATUS
  67. #include "winternl.h"
  68. #include "request.h"
  69. #include "user.h"
  70. struct region
  71. {
  72. int size;
  73. int num_rects;
  74. rectangle_t *rects;
  75. rectangle_t extents;
  76. };
  77. #define RGN_DEFAULT_RECTS 2
  78. #define EXTENTCHECK(r1, r2) \
  79. ((r1)->right > (r2)->left && \
  80. (r1)->left < (r2)->right && \
  81. (r1)->bottom > (r2)->top && \
  82. (r1)->top < (r2)->bottom)
  83. typedef int (*overlap_func_t)( struct region *reg, const rectangle_t *r1, const rectangle_t *r1End,
  84. const rectangle_t *r2, const rectangle_t *r2End, int top, int bottom );
  85. typedef int (*non_overlap_func_t)( struct region *reg, const rectangle_t *r,
  86. const rectangle_t *rEnd, int top, int bottom );
  87. static const rectangle_t empty_rect; /* all-zero rectangle for empty regions */
  88. /* add a rectangle to a region */
  89. static inline rectangle_t *add_rect( struct region *reg )
  90. {
  91. if (reg->num_rects >= reg->size)
  92. {
  93. rectangle_t *new_rect = realloc( reg->rects, 2 * sizeof(rectangle_t) * reg->size );
  94. if (!new_rect)
  95. {
  96. set_error( STATUS_NO_MEMORY );
  97. return NULL;
  98. }
  99. reg->rects = new_rect;
  100. reg->size *= 2;
  101. }
  102. return reg->rects + reg->num_rects++;
  103. }
  104. /* make sure all the rectangles are valid and that the region is properly y-x-banded */
  105. static inline int validate_rectangles( const rectangle_t *rects, unsigned int nb_rects )
  106. {
  107. const rectangle_t *ptr, *end;
  108. for (ptr = rects, end = rects + nb_rects; ptr < end; ptr++)
  109. {
  110. if (is_rect_empty( ptr )) return 0; /* empty rectangle */
  111. if (ptr == end - 1) break;
  112. if (ptr[0].top == ptr[1].top) /* same band */
  113. {
  114. if (ptr[0].bottom != ptr[1].bottom) return 0; /* not same y extent */
  115. if (ptr[0].right >= ptr[1].left) return 0; /* not properly x ordered */
  116. }
  117. else /* new band */
  118. {
  119. if (ptr[0].bottom > ptr[1].top) return 0; /* not properly y ordered */
  120. }
  121. }
  122. return 1;
  123. }
  124. /* attempt to merge the rects in the current band with those in the */
  125. /* previous one. Used only by region_op. */
  126. static int coalesce_region( struct region *pReg, int prevStart, int curStart )
  127. {
  128. int curNumRects;
  129. rectangle_t *pRegEnd = &pReg->rects[pReg->num_rects];
  130. rectangle_t *pPrevRect = &pReg->rects[prevStart];
  131. rectangle_t *pCurRect = &pReg->rects[curStart];
  132. int prevNumRects = curStart - prevStart;
  133. int bandtop = pCurRect->top;
  134. for (curNumRects = 0;
  135. (pCurRect != pRegEnd) && (pCurRect->top == bandtop);
  136. curNumRects++)
  137. {
  138. pCurRect++;
  139. }
  140. if (pCurRect != pRegEnd)
  141. {
  142. pRegEnd--;
  143. while (pRegEnd[-1].top == pRegEnd->top) pRegEnd--;
  144. curStart = pRegEnd - pReg->rects;
  145. pRegEnd = pReg->rects + pReg->num_rects;
  146. }
  147. if ((curNumRects == prevNumRects) && (curNumRects != 0))
  148. {
  149. pCurRect -= curNumRects;
  150. if (pPrevRect->bottom == pCurRect->top)
  151. {
  152. do
  153. {
  154. if ((pPrevRect->left != pCurRect->left) ||
  155. (pPrevRect->right != pCurRect->right)) return curStart;
  156. pPrevRect++;
  157. pCurRect++;
  158. prevNumRects -= 1;
  159. } while (prevNumRects != 0);
  160. pReg->num_rects -= curNumRects;
  161. pCurRect -= curNumRects;
  162. pPrevRect -= curNumRects;
  163. do
  164. {
  165. pPrevRect->bottom = pCurRect->bottom;
  166. pPrevRect++;
  167. pCurRect++;
  168. curNumRects -= 1;
  169. } while (curNumRects != 0);
  170. if (pCurRect == pRegEnd) curStart = prevStart;
  171. else do { *pPrevRect++ = *pCurRect++; } while (pCurRect != pRegEnd);
  172. }
  173. }
  174. return curStart;
  175. }
  176. /* apply an operation to two regions */
  177. /* check the GDI version of the code for explanations */
  178. static int region_op( struct region *newReg, const struct region *reg1, const struct region *reg2,
  179. overlap_func_t overlap_func,
  180. non_overlap_func_t non_overlap1_func,
  181. non_overlap_func_t non_overlap2_func )
  182. {
  183. int ybot, ytop, top, bot, prevBand, curBand;
  184. const rectangle_t *r1BandEnd, *r2BandEnd;
  185. const rectangle_t *r1 = reg1->rects;
  186. const rectangle_t *r2 = reg2->rects;
  187. const rectangle_t *r1End = r1 + reg1->num_rects;
  188. const rectangle_t *r2End = r2 + reg2->num_rects;
  189. rectangle_t *new_rects, *old_rects = newReg->rects;
  190. int new_size, ret = 0;
  191. new_size = max( reg1->num_rects, reg2->num_rects ) * 2;
  192. if (!(new_rects = mem_alloc( new_size * sizeof(*newReg->rects) ))) return 0;
  193. newReg->size = new_size;
  194. newReg->rects = new_rects;
  195. newReg->num_rects = 0;
  196. if (reg1->extents.top < reg2->extents.top)
  197. ybot = reg1->extents.top;
  198. else
  199. ybot = reg2->extents.top;
  200. prevBand = 0;
  201. do
  202. {
  203. curBand = newReg->num_rects;
  204. r1BandEnd = r1;
  205. while ((r1BandEnd != r1End) && (r1BandEnd->top == r1->top)) r1BandEnd++;
  206. r2BandEnd = r2;
  207. while ((r2BandEnd != r2End) && (r2BandEnd->top == r2->top)) r2BandEnd++;
  208. if (r1->top < r2->top)
  209. {
  210. top = max(r1->top,ybot);
  211. bot = min(r1->bottom,r2->top);
  212. if ((top != bot) && non_overlap1_func)
  213. {
  214. if (!non_overlap1_func( newReg, r1, r1BandEnd, top, bot )) goto done;
  215. }
  216. ytop = r2->top;
  217. }
  218. else if (r2->top < r1->top)
  219. {
  220. top = max(r2->top,ybot);
  221. bot = min(r2->bottom,r1->top);
  222. if ((top != bot) && non_overlap2_func)
  223. {
  224. if (!non_overlap2_func( newReg, r2, r2BandEnd, top, bot )) goto done;
  225. }
  226. ytop = r1->top;
  227. }
  228. else
  229. {
  230. ytop = r1->top;
  231. }
  232. if (newReg->num_rects != curBand)
  233. prevBand = coalesce_region(newReg, prevBand, curBand);
  234. ybot = min(r1->bottom, r2->bottom);
  235. curBand = newReg->num_rects;
  236. if (ybot > ytop)
  237. {
  238. if (!overlap_func( newReg, r1, r1BandEnd, r2, r2BandEnd, ytop, ybot )) goto done;
  239. }
  240. if (newReg->num_rects != curBand)
  241. prevBand = coalesce_region(newReg, prevBand, curBand);
  242. if (r1->bottom == ybot) r1 = r1BandEnd;
  243. if (r2->bottom == ybot) r2 = r2BandEnd;
  244. } while ((r1 != r1End) && (r2 != r2End));
  245. curBand = newReg->num_rects;
  246. if (r1 != r1End)
  247. {
  248. if (non_overlap1_func)
  249. {
  250. do
  251. {
  252. r1BandEnd = r1;
  253. while ((r1BandEnd < r1End) && (r1BandEnd->top == r1->top)) r1BandEnd++;
  254. if (!non_overlap1_func( newReg, r1, r1BandEnd, max(r1->top,ybot), r1->bottom ))
  255. goto done;
  256. r1 = r1BandEnd;
  257. } while (r1 != r1End);
  258. }
  259. }
  260. else if ((r2 != r2End) && non_overlap2_func)
  261. {
  262. do
  263. {
  264. r2BandEnd = r2;
  265. while ((r2BandEnd < r2End) && (r2BandEnd->top == r2->top)) r2BandEnd++;
  266. if (!non_overlap2_func( newReg, r2, r2BandEnd, max(r2->top,ybot), r2->bottom ))
  267. goto done;
  268. r2 = r2BandEnd;
  269. } while (r2 != r2End);
  270. }
  271. if (newReg->num_rects != curBand) coalesce_region(newReg, prevBand, curBand);
  272. if ((newReg->num_rects < (newReg->size / 2)) && (newReg->size > 2))
  273. {
  274. new_size = max( newReg->num_rects, RGN_DEFAULT_RECTS );
  275. if ((new_rects = realloc( newReg->rects, sizeof(*newReg->rects) * new_size )))
  276. {
  277. newReg->rects = new_rects;
  278. newReg->size = new_size;
  279. }
  280. }
  281. ret = 1;
  282. done:
  283. free( old_rects );
  284. return ret;
  285. }
  286. /* recalculate the extents of a region */
  287. static void set_region_extents( struct region *region )
  288. {
  289. rectangle_t *pRect, *pRectEnd;
  290. if (region->num_rects == 0)
  291. {
  292. region->extents.left = 0;
  293. region->extents.top = 0;
  294. region->extents.right = 0;
  295. region->extents.bottom = 0;
  296. return;
  297. }
  298. pRect = region->rects;
  299. pRectEnd = &pRect[region->num_rects - 1];
  300. region->extents.left = pRect->left;
  301. region->extents.top = pRect->top;
  302. region->extents.right = pRectEnd->right;
  303. region->extents.bottom = pRectEnd->bottom;
  304. while (pRect <= pRectEnd)
  305. {
  306. if (pRect->left < region->extents.left) region->extents.left = pRect->left;
  307. if (pRect->right > region->extents.right) region->extents.right = pRect->right;
  308. pRect++;
  309. }
  310. }
  311. /* handle an overlapping band for intersect_region */
  312. static int intersect_overlapping( struct region *pReg,
  313. const rectangle_t *r1, const rectangle_t *r1End,
  314. const rectangle_t *r2, const rectangle_t *r2End,
  315. int top, int bottom )
  316. {
  317. int left, right;
  318. while ((r1 != r1End) && (r2 != r2End))
  319. {
  320. left = max(r1->left, r2->left);
  321. right = min(r1->right, r2->right);
  322. if (left < right)
  323. {
  324. rectangle_t *rect = add_rect( pReg );
  325. if (!rect) return 0;
  326. rect->left = left;
  327. rect->top = top;
  328. rect->right = right;
  329. rect->bottom = bottom;
  330. }
  331. if (r1->right < r2->right) r1++;
  332. else if (r2->right < r1->right) r2++;
  333. else
  334. {
  335. r1++;
  336. r2++;
  337. }
  338. }
  339. return 1;
  340. }
  341. /* handle a non-overlapping band for subtract_region */
  342. static int subtract_non_overlapping( struct region *pReg, const rectangle_t *r,
  343. const rectangle_t *rEnd, int top, int bottom )
  344. {
  345. while (r != rEnd)
  346. {
  347. rectangle_t *rect = add_rect( pReg );
  348. if (!rect) return 0;
  349. rect->left = r->left;
  350. rect->top = top;
  351. rect->right = r->right;
  352. rect->bottom = bottom;
  353. r++;
  354. }
  355. return 1;
  356. }
  357. /* handle an overlapping band for subtract_region */
  358. static int subtract_overlapping( struct region *pReg,
  359. const rectangle_t *r1, const rectangle_t *r1End,
  360. const rectangle_t *r2, const rectangle_t *r2End,
  361. int top, int bottom )
  362. {
  363. int left = r1->left;
  364. while ((r1 != r1End) && (r2 != r2End))
  365. {
  366. if (r2->right <= left) r2++;
  367. else if (r2->left <= left)
  368. {
  369. left = r2->right;
  370. if (left >= r1->right)
  371. {
  372. r1++;
  373. if (r1 != r1End)
  374. left = r1->left;
  375. }
  376. else r2++;
  377. }
  378. else if (r2->left < r1->right)
  379. {
  380. rectangle_t *rect = add_rect( pReg );
  381. if (!rect) return 0;
  382. rect->left = left;
  383. rect->top = top;
  384. rect->right = r2->left;
  385. rect->bottom = bottom;
  386. left = r2->right;
  387. if (left >= r1->right)
  388. {
  389. r1++;
  390. if (r1 != r1End)
  391. left = r1->left;
  392. }
  393. else r2++;
  394. }
  395. else
  396. {
  397. if (r1->right > left)
  398. {
  399. rectangle_t *rect = add_rect( pReg );
  400. if (!rect) return 0;
  401. rect->left = left;
  402. rect->top = top;
  403. rect->right = r1->right;
  404. rect->bottom = bottom;
  405. }
  406. r1++;
  407. if (r1 != r1End)
  408. left = r1->left;
  409. }
  410. }
  411. while (r1 != r1End)
  412. {
  413. rectangle_t *rect = add_rect( pReg );
  414. if (!rect) return 0;
  415. rect->left = left;
  416. rect->top = top;
  417. rect->right = r1->right;
  418. rect->bottom = bottom;
  419. r1++;
  420. if (r1 != r1End) left = r1->left;
  421. }
  422. return 1;
  423. }
  424. /* handle a non-overlapping band for union_region */
  425. static int union_non_overlapping( struct region *pReg, const rectangle_t *r,
  426. const rectangle_t *rEnd, int top, int bottom )
  427. {
  428. while (r != rEnd)
  429. {
  430. rectangle_t *rect = add_rect( pReg );
  431. if (!rect) return 0;
  432. rect->left = r->left;
  433. rect->top = top;
  434. rect->right = r->right;
  435. rect->bottom = bottom;
  436. r++;
  437. }
  438. return 1;
  439. }
  440. /* handle an overlapping band for union_region */
  441. static int union_overlapping( struct region *pReg,
  442. const rectangle_t *r1, const rectangle_t *r1End,
  443. const rectangle_t *r2, const rectangle_t *r2End,
  444. int top, int bottom )
  445. {
  446. #define MERGERECT(r) \
  447. if ((pReg->num_rects != 0) && \
  448. (pReg->rects[pReg->num_rects-1].top == top) && \
  449. (pReg->rects[pReg->num_rects-1].bottom == bottom) && \
  450. (pReg->rects[pReg->num_rects-1].right >= r->left)) \
  451. { \
  452. if (pReg->rects[pReg->num_rects-1].right < r->right) \
  453. { \
  454. pReg->rects[pReg->num_rects-1].right = r->right; \
  455. } \
  456. } \
  457. else \
  458. { \
  459. rectangle_t *rect = add_rect( pReg ); \
  460. if (!rect) return 0; \
  461. rect->top = top; \
  462. rect->bottom = bottom; \
  463. rect->left = r->left; \
  464. rect->right = r->right; \
  465. } \
  466. r++;
  467. while ((r1 != r1End) && (r2 != r2End))
  468. {
  469. if (r1->left < r2->left)
  470. {
  471. MERGERECT(r1);
  472. }
  473. else
  474. {
  475. MERGERECT(r2);
  476. }
  477. }
  478. if (r1 != r1End)
  479. {
  480. do
  481. {
  482. MERGERECT(r1);
  483. } while (r1 != r1End);
  484. }
  485. else while (r2 != r2End)
  486. {
  487. MERGERECT(r2);
  488. }
  489. return 1;
  490. #undef MERGERECT
  491. }
  492. /* create an empty region */
  493. struct region *create_empty_region(void)
  494. {
  495. struct region *region;
  496. if (!(region = mem_alloc( sizeof(*region) ))) return NULL;
  497. if (!(region->rects = mem_alloc( RGN_DEFAULT_RECTS * sizeof(*region->rects) )))
  498. {
  499. free( region );
  500. return NULL;
  501. }
  502. region->size = RGN_DEFAULT_RECTS;
  503. region->num_rects = 0;
  504. region->extents.left = 0;
  505. region->extents.top = 0;
  506. region->extents.right = 0;
  507. region->extents.bottom = 0;
  508. return region;
  509. }
  510. /* create a region from request data */
  511. struct region *create_region_from_req_data( const void *data, data_size_t size )
  512. {
  513. unsigned int alloc_rects;
  514. struct region *region;
  515. const rectangle_t *rects = data;
  516. int nb_rects = size / sizeof(rectangle_t);
  517. /* special case: empty region can be specified by a single all-zero rectangle */
  518. if (nb_rects == 1 && !memcmp( rects, &empty_rect, sizeof(empty_rect) )) nb_rects = 0;
  519. if (!validate_rectangles( rects, nb_rects ))
  520. {
  521. set_error( STATUS_INVALID_PARAMETER );
  522. return NULL;
  523. }
  524. if (!(region = mem_alloc( sizeof(*region) ))) return NULL;
  525. alloc_rects = max( nb_rects, RGN_DEFAULT_RECTS );
  526. if (!(region->rects = mem_alloc( alloc_rects * sizeof(*region->rects) )))
  527. {
  528. free( region );
  529. return NULL;
  530. }
  531. region->size = alloc_rects;
  532. region->num_rects = nb_rects;
  533. memcpy( region->rects, rects, nb_rects * sizeof(*rects) );
  534. set_region_extents( region );
  535. return region;
  536. }
  537. /* free a region */
  538. void free_region( struct region *region )
  539. {
  540. free( region->rects );
  541. free( region );
  542. }
  543. /* set region to a simple rectangle */
  544. void set_region_rect( struct region *region, const rectangle_t *rect )
  545. {
  546. if (!is_rect_empty( rect ))
  547. {
  548. region->num_rects = 1;
  549. region->rects[0] = region->extents = *rect;
  550. }
  551. else
  552. {
  553. region->num_rects = 0;
  554. region->extents = empty_rect;
  555. }
  556. }
  557. /* retrieve the region data for sending to the client */
  558. rectangle_t *get_region_data( const struct region *region, data_size_t max_size, data_size_t *total_size )
  559. {
  560. const rectangle_t *data = region->rects;
  561. if (!(*total_size = region->num_rects * sizeof(rectangle_t)))
  562. {
  563. /* return a single empty rect for empty regions */
  564. *total_size = sizeof(empty_rect);
  565. data = &empty_rect;
  566. }
  567. if (max_size >= *total_size) return memdup( data, *total_size );
  568. set_error( STATUS_BUFFER_OVERFLOW );
  569. return NULL;
  570. }
  571. /* retrieve the region data for sending to the client and free the region at the same time */
  572. rectangle_t *get_region_data_and_free( struct region *region, data_size_t max_size, data_size_t *total_size )
  573. {
  574. rectangle_t *ret = region->rects;
  575. if (!(*total_size = region->num_rects * sizeof(rectangle_t)))
  576. {
  577. /* return a single empty rect for empty regions */
  578. *total_size = sizeof(empty_rect);
  579. if (max_size >= sizeof(empty_rect))
  580. {
  581. ret = memdup( &empty_rect, sizeof(empty_rect) );
  582. free( region->rects );
  583. }
  584. }
  585. if (max_size < *total_size)
  586. {
  587. free( region->rects );
  588. set_error( STATUS_BUFFER_OVERFLOW );
  589. ret = NULL;
  590. }
  591. free( region );
  592. return ret;
  593. }
  594. /* check if a given region is empty */
  595. int is_region_empty( const struct region *region )
  596. {
  597. return region->num_rects == 0;
  598. }
  599. /* get the extents rect of a region */
  600. void get_region_extents( const struct region *region, rectangle_t *rect )
  601. {
  602. *rect = region->extents;
  603. }
  604. /* add an offset to a region */
  605. void offset_region( struct region *region, int x, int y )
  606. {
  607. rectangle_t *rect, *end;
  608. if (!region->num_rects) return;
  609. for (rect = region->rects, end = rect + region->num_rects; rect < end; rect++)
  610. offset_rect( rect, x, y );
  611. offset_rect( &region->extents, x, y );
  612. }
  613. /* mirror a region relative to a window client rect */
  614. void mirror_region( const rectangle_t *client_rect, struct region *region )
  615. {
  616. int start, end, i, j;
  617. for (start = 0; start < region->num_rects; start = end + 1)
  618. {
  619. for (end = start; end < region->num_rects - 1; end++)
  620. if (region->rects[end + 1].top != region->rects[end].top) break;
  621. for (i = start, j = end; i < j; i++, j--)
  622. {
  623. rectangle_t rect = region->rects[j];
  624. region->rects[i] = region->rects[j];
  625. region->rects[j] = rect;
  626. mirror_rect( client_rect, &region->rects[j] );
  627. mirror_rect( client_rect, &region->rects[i] );
  628. }
  629. if (i == j) mirror_rect( client_rect, &region->rects[i] );
  630. }
  631. mirror_rect( client_rect, &region->extents );
  632. }
  633. /* scale a region for a given dpi factor */
  634. void scale_region( struct region *region, unsigned int dpi_from, unsigned int dpi_to )
  635. {
  636. rectangle_t *rect, *end;
  637. if (!region->num_rects) return;
  638. for (rect = region->rects, end = rect + region->num_rects; rect < end; rect++)
  639. scale_dpi_rect( rect, dpi_from, dpi_to );
  640. scale_dpi_rect( &region->extents, dpi_from, dpi_to );
  641. }
  642. /* make a copy of a region; returns dst or NULL on error */
  643. struct region *copy_region( struct region *dst, const struct region *src )
  644. {
  645. if (dst == src) return dst;
  646. if (dst->size < src->num_rects)
  647. {
  648. rectangle_t *rect = realloc( dst->rects, src->num_rects * sizeof(*rect) );
  649. if (!rect)
  650. {
  651. set_error( STATUS_NO_MEMORY );
  652. return NULL;
  653. }
  654. dst->rects = rect;
  655. dst->size = src->num_rects;
  656. }
  657. dst->num_rects = src->num_rects;
  658. dst->extents = src->extents;
  659. memcpy( dst->rects, src->rects, src->num_rects * sizeof(*dst->rects) );
  660. return dst;
  661. }
  662. /* compute the intersection of two regions into dst, which can be one of the source regions */
  663. struct region *intersect_region( struct region *dst, const struct region *src1,
  664. const struct region *src2 )
  665. {
  666. if (!src1->num_rects || !src2->num_rects || !EXTENTCHECK(&src1->extents, &src2->extents))
  667. {
  668. dst->num_rects = 0;
  669. dst->extents.left = 0;
  670. dst->extents.top = 0;
  671. dst->extents.right = 0;
  672. dst->extents.bottom = 0;
  673. return dst;
  674. }
  675. if (!region_op( dst, src1, src2, intersect_overlapping, NULL, NULL )) return NULL;
  676. set_region_extents( dst );
  677. return dst;
  678. }
  679. /* compute the subtraction of two regions into dst, which can be one of the source regions */
  680. struct region *subtract_region( struct region *dst, const struct region *src1,
  681. const struct region *src2 )
  682. {
  683. if (!src1->num_rects || !src2->num_rects || !EXTENTCHECK(&src1->extents, &src2->extents))
  684. return copy_region( dst, src1 );
  685. if (!region_op( dst, src1, src2, subtract_overlapping,
  686. subtract_non_overlapping, NULL )) return NULL;
  687. set_region_extents( dst );
  688. return dst;
  689. }
  690. /* compute the union of two regions into dst, which can be one of the source regions */
  691. struct region *union_region( struct region *dst, const struct region *src1,
  692. const struct region *src2 )
  693. {
  694. if (src1 == src2) return copy_region( dst, src1 );
  695. if (!src1->num_rects) return copy_region( dst, src2 );
  696. if (!src2->num_rects) return copy_region( dst, src1 );
  697. if ((src1->num_rects == 1) &&
  698. (src1->extents.left <= src2->extents.left) &&
  699. (src1->extents.top <= src2->extents.top) &&
  700. (src1->extents.right >= src2->extents.right) &&
  701. (src1->extents.bottom >= src2->extents.bottom))
  702. return copy_region( dst, src1 );
  703. if ((src2->num_rects == 1) &&
  704. (src2->extents.left <= src1->extents.left) &&
  705. (src2->extents.top <= src1->extents.top) &&
  706. (src2->extents.right >= src1->extents.right) &&
  707. (src2->extents.bottom >= src1->extents.bottom))
  708. return copy_region( dst, src2 );
  709. if (!region_op( dst, src1, src2, union_overlapping,
  710. union_non_overlapping, union_non_overlapping )) return NULL;
  711. dst->extents.left = min(src1->extents.left, src2->extents.left);
  712. dst->extents.top = min(src1->extents.top, src2->extents.top);
  713. dst->extents.right = max(src1->extents.right, src2->extents.right);
  714. dst->extents.bottom = max(src1->extents.bottom, src2->extents.bottom);
  715. return dst;
  716. }
  717. /* compute the exclusive or of two regions into dst, which can be one of the source regions */
  718. struct region *xor_region( struct region *dst, const struct region *src1,
  719. const struct region *src2 )
  720. {
  721. struct region *tmp = create_empty_region();
  722. if (!tmp) return NULL;
  723. if (!subtract_region( tmp, src1, src2 ) ||
  724. !subtract_region( dst, src2, src1 ) ||
  725. !union_region( dst, dst, tmp ))
  726. dst = NULL;
  727. free_region( tmp );
  728. return dst;
  729. }
  730. /* check if the given point is inside the region */
  731. int point_in_region( struct region *region, int x, int y )
  732. {
  733. const rectangle_t *ptr, *end;
  734. for (ptr = region->rects, end = region->rects + region->num_rects; ptr < end; ptr++)
  735. {
  736. if (ptr->top > y) return 0;
  737. if (ptr->bottom <= y) continue;
  738. /* now we are in the correct band */
  739. if (ptr->left > x) return 0;
  740. if (ptr->right <= x) continue;
  741. return 1;
  742. }
  743. return 0;
  744. }
  745. /* check if the given rectangle is (at least partially) inside the region */
  746. int rect_in_region( struct region *region, const rectangle_t *rect )
  747. {
  748. const rectangle_t *ptr, *end;
  749. for (ptr = region->rects, end = region->rects + region->num_rects; ptr < end; ptr++)
  750. {
  751. if (ptr->top >= rect->bottom) return 0;
  752. if (ptr->bottom <= rect->top) continue;
  753. if (ptr->left >= rect->right) continue;
  754. if (ptr->right <= rect->left) continue;
  755. return 1;
  756. }
  757. return 0;
  758. }