123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- /**
- * Copyright © 2011 Red Hat, Inc.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice (including the next
- * paragraph) shall be included in all copies or substantial portions of the
- * Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- * DEALINGS IN THE SOFTWARE.
- */
- #ifdef HAVE_DIX_CONFIG_H
- #include <dix-config.h>
- #endif
- #include <stdio.h>
- #include <stdlib.h>
- #include <X11/X.h>
- #include <xfixesint.h>
- #include <X11/extensions/xfixeswire.h>
- static void
- _fixes_test_direction(struct PointerBarrier *barrier, int d[4], int permitted)
- {
- BOOL blocking;
- int i, j;
- int dir = barrier_get_direction(d[0], d[1], d[2], d[3]);
- barrier->directions = 0;
- blocking = barrier_is_blocking_direction(barrier, dir);
- assert(blocking);
- for (j = 0; j <= BarrierNegativeY; j++) {
- for (i = 0; i <= BarrierNegativeY; i++) {
- barrier->directions |= 1 << i;
- blocking = barrier_is_blocking_direction(barrier, dir);
- assert((barrier->directions & permitted) ==
- permitted ? !blocking : blocking);
- }
- }
- }
- static void
- fixes_pointer_barrier_direction_test(void)
- {
- struct PointerBarrier barrier;
- int x = 100;
- int y = 100;
- int directions[8][4] = {
- {x, y, x, y + 100}, /* S */
- {x + 50, y, x - 50, y + 100}, /* SW */
- {x + 100, y, x, y}, /* W */
- {x + 100, y + 50, x, y - 50}, /* NW */
- {x, y + 100, x, y}, /* N */
- {x - 50, y + 100, x + 50, y}, /* NE */
- {x, y, x + 100, y}, /* E */
- {x, y - 50, x + 100, y + 50}, /* SE */
- };
- barrier.x1 = x;
- barrier.x2 = x;
- barrier.y1 = y - 50;
- barrier.y2 = y + 49;
- _fixes_test_direction(&barrier, directions[0], BarrierPositiveY);
- _fixes_test_direction(&barrier, directions[1],
- BarrierPositiveY | BarrierNegativeX);
- _fixes_test_direction(&barrier, directions[2], BarrierNegativeX);
- _fixes_test_direction(&barrier, directions[3],
- BarrierNegativeY | BarrierNegativeX);
- _fixes_test_direction(&barrier, directions[4], BarrierNegativeY);
- _fixes_test_direction(&barrier, directions[5],
- BarrierPositiveX | BarrierNegativeY);
- _fixes_test_direction(&barrier, directions[6], BarrierPositiveX);
- _fixes_test_direction(&barrier, directions[7],
- BarrierPositiveY | BarrierPositiveX);
- }
- static void
- fixes_pointer_barriers_test(void)
- {
- struct PointerBarrier barrier;
- int x1, y1, x2, y2;
- double distance;
- int x = 100;
- int y = 100;
- /* vert barrier */
- barrier.x1 = x;
- barrier.x2 = x;
- barrier.y1 = y - 50;
- barrier.y2 = y + 50;
- /* across at half-way */
- x1 = x + 1;
- x2 = x - 1;
- y1 = y;
- y2 = y;
- assert(barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
- assert(distance == 1);
- /* definitely not across */
- x1 = x + 10;
- x2 = x + 5;
- assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
- /* across, but outside of y range */
- x1 = x + 1;
- x2 = x - 1;
- y1 = y + 100;
- y2 = y + 100;
- assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
- /* across, diagonally */
- x1 = x + 5;
- x2 = x - 5;
- y1 = y + 5;
- y2 = y - 5;
- assert(barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
- /* across but outside boundary, diagonally */
- x1 = x + 5;
- x2 = x - 5;
- y1 = y + 100;
- y2 = y + 50;
- assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
- /* edge case: startpoint of movement on barrier → blocking */
- x1 = x;
- x2 = x - 1;
- y1 = y;
- y2 = y;
- assert(barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
- /* edge case: startpoint of movement on barrier → not blocking, positive */
- x1 = x;
- x2 = x + 1;
- y1 = y;
- y2 = y;
- assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
- /* edge case: startpoint of movement on barrier → not blocking, negative */
- x1 = x - 1;
- x2 = x - 2;
- y1 = y;
- y2 = y;
- assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
- /* edge case: endpoint of movement on barrier → blocking */
- x1 = x + 1;
- x2 = x;
- y1 = y;
- y2 = y;
- assert(barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
- /* startpoint on barrier but outside y range */
- x1 = x;
- x2 = x - 1;
- y1 = y + 100;
- y2 = y + 100;
- assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
- /* endpoint on barrier but outside y range */
- x1 = x + 1;
- x2 = x;
- y1 = y + 100;
- y2 = y + 100;
- assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
- /* horizontal barrier */
- barrier.x1 = x - 50;
- barrier.x2 = x + 50;
- barrier.y1 = y;
- barrier.y2 = y;
- /* across at half-way */
- x1 = x;
- x2 = x;
- y1 = y - 1;
- y2 = y + 1;
- assert(barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
- /* definitely not across */
- y1 = y + 10;
- y2 = y + 5;
- assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
- /* across, but outside of y range */
- x1 = x + 100;
- x2 = x + 100;
- y1 = y + 1;
- y2 = y - 1;
- assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
- /* across, diagonally */
- y1 = y + 5;
- y2 = y - 5;
- x1 = x + 5;
- x2 = x - 5;
- assert(barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
- /* across but outside boundary, diagonally */
- y1 = y + 5;
- y2 = y - 5;
- x1 = x + 100;
- x2 = x + 50;
- assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
- /* edge case: startpoint of movement on barrier → blocking */
- y1 = y;
- y2 = y - 1;
- x1 = x;
- x2 = x;
- assert(barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
- /* edge case: startpoint of movement on barrier → not blocking, positive */
- y1 = y;
- y2 = y + 1;
- x1 = x;
- x2 = x;
- assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
- /* edge case: startpoint of movement on barrier → not blocking, negative */
- y1 = y - 1;
- y2 = y - 2;
- x1 = x;
- x2 = x;
- assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
- /* edge case: endpoint of movement on barrier → blocking */
- y1 = y + 1;
- y2 = y;
- x1 = x;
- x2 = x;
- assert(barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
- /* startpoint on barrier but outside y range */
- y1 = y;
- y2 = y - 1;
- x1 = x + 100;
- x2 = x + 100;
- assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
- /* endpoint on barrier but outside y range */
- y1 = y + 1;
- y2 = y;
- x1 = x + 100;
- x2 = x + 100;
- assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
- /* ray vert barrier */
- barrier.x1 = x;
- barrier.x2 = x;
- barrier.y1 = -1;
- barrier.y2 = y + 100;
- /* ray barrier simple case */
- y1 = y;
- y2 = y;
- x1 = x + 50;
- x2 = x - 50;
- assert(barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
- /* endpoint outside y range; should be blocked */
- y1 = y - 1000;
- y2 = y - 1000;
- x1 = x + 50;
- x2 = x - 50;
- assert(barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
- /* endpoint outside y range */
- y1 = y + 150;
- y2 = y + 150;
- x1 = x + 50;
- x2 = x - 50;
- assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
- }
- static void
- fixes_pointer_barrier_clamp_test(void)
- {
- struct PointerBarrier barrier;
- int x = 100;
- int y = 100;
- int cx, cy; /* clamped */
- /* vert barrier */
- barrier.x1 = x;
- barrier.x2 = x;
- barrier.y1 = y - 50;
- barrier.y2 = y + 49;
- barrier.directions = 0;
- cx = INT_MAX;
- cy = INT_MAX;
- barrier_clamp_to_barrier(&barrier, BarrierPositiveX, &cx, &cy);
- assert(cx == barrier.x1 - 1);
- assert(cy == INT_MAX);
- cx = 0;
- cy = INT_MAX;
- barrier_clamp_to_barrier(&barrier, BarrierNegativeX, &cx, &cy);
- assert(cx == barrier.x1);
- assert(cy == INT_MAX);
- /* horiz barrier */
- barrier.x1 = x - 50;
- barrier.x2 = x + 49;
- barrier.y1 = y;
- barrier.y2 = y;
- barrier.directions = 0;
- cx = INT_MAX;
- cy = INT_MAX;
- barrier_clamp_to_barrier(&barrier, BarrierPositiveY, &cx, &cy);
- assert(cx == INT_MAX);
- assert(cy == barrier.y1 - 1);
- cx = INT_MAX;
- cy = 0;
- barrier_clamp_to_barrier(&barrier, BarrierNegativeY, &cx, &cy);
- assert(cx == INT_MAX);
- assert(cy == barrier.y1);
- }
- int
- main(int argc, char **argv)
- {
- fixes_pointer_barriers_test();
- fixes_pointer_barrier_direction_test();
- fixes_pointer_barrier_clamp_test();
- return 0;
- }
|