diff options
author | Bryce Harrington <bryce@canonical.com> | 2012-01-10 03:01:27 -0800 |
---|---|---|
committer | Bryce Harrington <bryce@canonical.com> | 2012-01-10 03:58:53 -0800 |
commit | b0f6d213045feab3cff448e2e5a1116adb5c729e (patch) | |
tree | af04700fb7322138cbd6c3b13220cc50ec20f40f | |
parent | 7ef125dacfa5fba10818f5c47d189d0f860ee2cf (diff) |
Refactor reflection support into library.
This follows the same API style as rotation.
-rw-r--r-- | examples/xrandr.c | 36 | ||||
-rw-r--r-- | include/X11/extensions/XrandrUtils.h | 9 | ||||
-rw-r--r-- | man/XrandrUtils.man | 18 | ||||
-rw-r--r-- | src/XrandrUtils.c | 56 | ||||
-rw-r--r-- | test/.gitignore | 1 | ||||
-rw-r--r-- | test/Makefile.am | 3 | ||||
-rw-r--r-- | test/reflections.c | 130 |
7 files changed, 221 insertions, 32 deletions
diff --git a/examples/xrandr.c b/examples/xrandr.c index c6ad767..e7c2295 100644 --- a/examples/xrandr.c +++ b/examples/xrandr.c @@ -51,13 +51,6 @@ static Bool properties = False; static Bool grab_server = True; static Bool no_primary = False; -static char *reflections[5] = { - "normal", - "x", - "y", - "xy", - "\n"}; - /* subpixel order */ static char *order[6] = { "unknown", @@ -173,23 +166,6 @@ static inline double dmin (double x, double y) return x < y ? x : y; } -static char * -reflection_name (Rotation rotation) -{ - rotation &= (RR_Reflect_X|RR_Reflect_Y); - switch (rotation) { - case 0: - return "none"; - case RR_Reflect_X: - return "X axis"; - case RR_Reflect_Y: - return "Y axis"; - case RR_Reflect_X|RR_Reflect_Y: - return "X and Y axis"; - } - return "invalid reflection"; -} - typedef enum _changes { changes_none = 0, changes_crtc = (1 << 0), @@ -1113,7 +1089,7 @@ set_output_info (output_t *output, RROutput xid, XRROutputInfo *output_info) fatal ("output %s cannot use rotation \"%s\" reflection \"%s\"\n", output->output.string, XRURotationName (output->rotation), - reflection_name (output->rotation)); + XRUReflectionName (output->rotation)); /* set gamma */ if (!(output->changes & changes_gamma)) @@ -2301,10 +2277,8 @@ main (int argc, char **argv) if (!strcmp ("--reflect", argv[i]) || !strcmp ("--reflection", argv[i])) { if (++i>=argc) usage (); if (!output) usage (); - for (dirind = 0; dirind < 4; dirind++) { - if (strcmp (reflections[dirind], argv[i]) == 0) break; - } - if (dirind == 4) + dirind = XRUGetReflectionIndex(argv[i]); + if ((dirind < 0) || (dirind > 3)) usage (); output->rotation &= ~(RR_Reflect_X|RR_Reflect_Y); output->rotation |= dirind * RR_Reflect_X; @@ -2918,7 +2892,7 @@ main (int argc, char **argv) printf (" %s", XRURotationName (output->rotation)); if (output->rotation & (RR_Reflect_X|RR_Reflect_Y)) - printf (" %s", reflection_name (output->rotation)); + printf (" %s", XRUReflectionName (output->rotation)); } } if (rotations != RR_Rotate_0 || verbose) @@ -3292,7 +3266,7 @@ main (int argc, char **argv) XRURotationName (current_rotation)); printf("Current reflection - %s\n", - reflection_name (current_rotation)); + XRUReflectionName (current_rotation)); printf ("Rotations possible - "); for (i = 0; i < 4; i ++) { diff --git a/include/X11/extensions/XrandrUtils.h b/include/X11/extensions/XrandrUtils.h index c48de2e..7b33769 100644 --- a/include/X11/extensions/XrandrUtils.h +++ b/include/X11/extensions/XrandrUtils.h @@ -55,4 +55,13 @@ XRUGetRotationIndex(char * rotation_name); Rotation XRUGetRotation(int dirind); +const char * +XRUReflectionName(Rotation rotation); + +int +XRUGetReflectionIndex(char * reflection_name); + +Rotation +XRUGetReflection(int dirind); + #endif /* _XRANDR_UTILS_H_ */ diff --git a/man/XrandrUtils.man b/man/XrandrUtils.man index 0fd1a46..7acf0e9 100644 --- a/man/XrandrUtils.man +++ b/man/XrandrUtils.man @@ -52,6 +52,24 @@ int XRUGetRotationIndex(char * rotation_name); */ Rotation XRUGetRotation(int dirind); +/** + * Return string name for the given rotation, + * or "invalid reflection" on error. + */ +const char * XRUReflectionName(Rotation rotation); + +/** + * Look up reflection name and return an index number, + * or -1 if name could not be found. + */ +int XRUGetReflectionIndex(char * reflection_name); + +/** + * Return the reflection matching the given index as + * a Rotation, or 0 if not valid. + */ +Rotation XRUGetReflection(int dirind); + .sp .fi diff --git a/src/XrandrUtils.c b/src/XrandrUtils.c index a00ad1c..0654977 100644 --- a/src/XrandrUtils.c +++ b/src/XrandrUtils.c @@ -40,6 +40,15 @@ static /* const */ char * ru_direction[5] = { "inverted", "right", "\n"}; + +static /* const */ char * ru_reflection[5] = { + "normal", + "x", + "y", + "xy", + "\n"}; + +/* Rotations */ const char * XRURotationName(Rotation rotation) { @@ -72,3 +81,50 @@ XRUGetRotation(int dirind) return 0; return (1 << dirind); } + + +/* Reflections */ +const char * +XRUReflectionName(Rotation rotation) +{ + rotation &= (RR_Reflect_X|RR_Reflect_Y); + switch (rotation) { + case 0: + return "none"; + case RR_Reflect_X: + return "X axis"; + case RR_Reflect_Y: + return "Y axis"; + case RR_Reflect_X|RR_Reflect_Y: + return "X and Y axis"; + } + return "invalid reflection"; +} + +int +XRUGetReflectionIndex(char * reflection_name) +{ + int dirind; + if (!reflection_name) + return -1; + for (dirind = 0; dirind < 4; dirind++) + if (strcmp (ru_reflection[dirind], reflection_name) == 0) + return dirind; + return -1; +} + +Rotation +XRUGetReflection(int dirind) +{ + switch (dirind) { + case 0: + return 0; + case 1: + return RR_Reflect_X; + case 2: + return RR_Reflect_Y; + case 3: + return (RR_Reflect_X|RR_Reflect_Y); + } + return 0; +} diff --git a/test/.gitignore b/test/.gitignore index 3819c8e..f3329d2 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -1,3 +1,4 @@ geometry xrandr rotations +reflections diff --git a/test/Makefile.am b/test/Makefile.am index 9c11fda..0b723c9 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,4 +1,4 @@ -check_PROGRAMS = geometry rotations +check_PROGRAMS = geometry rotations reflections TESTS = $(check_PROGRAMS) @@ -15,6 +15,7 @@ TEST_LDADD = @RANDR_UTILS_LIBS@ geometry_LDADD = $(TEST_LDADD) rotations_LDADD = $(TEST_LDADD) $(RANDR_UTIL_LIB) +reflections_LDADD = $(TEST_LDADD) $(RANDR_UTIL_LIB) $(RANDR_UTIL_LIB): @cd $(top_builddir)/src/ && $(MAKE) libXrandrUtils.la diff --git a/test/reflections.c b/test/reflections.c new file mode 100644 index 0000000..9be4f7b --- /dev/null +++ b/test/reflections.c @@ -0,0 +1,130 @@ +/** + * Copyright © 2012 Canonical, Ltd. + * + * 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. + */ + +/** + * Test reflections and reflection name helper functions. + */ + +#include <string.h> +#include <assert.h> +#include <stdio.h> + +#include "XrandrUtils.h" + +/** + * Lookup reflection names + */ +static void reflection_names(void) +{ + Rotation normal = 0; + Rotation x = RR_Reflect_X; + Rotation y = RR_Reflect_Y; + Rotation xy = RR_Reflect_X|RR_Reflect_Y; + + assert(0 == strcmp("none", XRUReflectionName( normal ))); + assert(0 == strcmp("X axis", XRUReflectionName( x ))); + assert(0 == strcmp("Y axis", XRUReflectionName( y ))); + assert(0 == strcmp("X and Y axis", XRUReflectionName( xy ))); +} + +/** + * Verify that out of range name lookups produce an appropriate value or error + */ +static void reflection_names_invalid(void) +{ + assert(0 == strcmp("none", XRUReflectionName((Rotation) 0 ))); + assert(0 == strcmp("none", XRUReflectionName((Rotation) 1 << 8 ))); +} + +/** + * Lookup reflection indexes by their string name + */ +static void reflection_indexes_by_name(void) +{ + assert(0 == XRUGetReflectionIndex("normal")); + assert(1 == XRUGetReflectionIndex("x")); + assert(2 == XRUGetReflectionIndex("y")); + assert(3 == XRUGetReflectionIndex("xy")); +} + +/** + * Verify garbage name lookups produce an appropriate error + */ +static void reflection_indexes_by_invalid_name(void) +{ + assert(-1 == XRUGetReflectionIndex(NULL)); + assert(-1 == XRUGetReflectionIndex("")); + assert(-1 == XRUGetReflectionIndex(" ")); + assert(-1 == XRUGetReflectionIndex("z")); + assert(-1 == XRUGetReflectionIndex("invalid")); + assert(-1 == XRUGetReflectionIndex("NORMAL")); + assert(-1 == XRUGetReflectionIndex("uni©ode")); +} + +/** + * Check unterminated string behavior + */ +static void reflection_indexes_by_unterminated_name(void) +{ + char x_unterminated[2] = "x"; + x_unterminated[1] = ' '; + + assert(-1 == XRUGetReflectionIndex(x_unterminated)); +} + +/** + * Lookup reflections by index + */ +static void reflection_by_index(void) +{ + assert(0 == XRUGetReflection(0)); + assert(RR_Reflect_X == XRUGetReflection(1)); + assert(RR_Reflect_Y == XRUGetReflection(2)); + assert((RR_Reflect_X|RR_Reflect_Y) == XRUGetReflection(3)); +} + +/** + * Verify out of range indexes produce appropriate errors + */ +static void reflection_by_invalid_index(void) +{ + assert(0 == XRUGetReflection(-1)); + assert(0 == XRUGetReflection( 4)); + assert(0 == XRUGetReflection(42)); + assert(0 == XRUGetReflection(-100)); +} + +int main(int argc, char** argv) +{ + reflection_names(); + reflection_names_invalid(); + + reflection_indexes_by_name(); + reflection_indexes_by_invalid_name(); + reflection_indexes_by_unterminated_name(); + + reflection_by_index(); + reflection_by_invalid_index(); + + return 0; +} |