diff options
-rw-r--r-- | src/xcb_errors.c | 18 | ||||
-rw-r--r-- | src/xcb_errors.h | 10 | ||||
-rw-r--r-- | tests/test.c | 60 |
3 files changed, 66 insertions, 22 deletions
diff --git a/src/xcb_errors.c b/src/xcb_errors.c index c6f87d4..0c6c742 100644 --- a/src/xcb_errors.c +++ b/src/xcb_errors.c @@ -147,7 +147,7 @@ const char *xcb_errors_get_name_for_minor_code(xcb_errors_context_t *ctx, } const char *xcb_errors_get_name_for_event(xcb_errors_context_t *ctx, - uint8_t event_code) + uint8_t event_code, const char **extension) { struct extension_info_t *best = NULL; struct extension_info_t *next = ctx->extensions; @@ -167,15 +167,20 @@ const char *xcb_errors_get_name_for_event(xcb_errors_context_t *ctx, best = current; } - if (best == NULL || best->first_event == 0) + if (best == NULL || best->first_event == 0) { /* Nothing found */ + if (extension) + *extension = NULL; return get_strings_entry(xproto_info.strings_events, event_code); + } + if (extension) + *extension = best->name; return get_strings_entry(best->static_info.strings_events, event_code - best->first_event); } const char *xcb_errors_get_name_for_error(xcb_errors_context_t *ctx, - uint8_t error_code) + uint8_t error_code, const char **extension) { struct extension_info_t *best = NULL; struct extension_info_t *next = ctx->extensions; @@ -195,9 +200,14 @@ const char *xcb_errors_get_name_for_error(xcb_errors_context_t *ctx, best = current; } - if (best == NULL || best->first_error == 0) + if (best == NULL || best->first_error == 0) { /* Nothing found */ + if (extension) + *extension = NULL; return get_strings_entry(xproto_info.strings_errors, error_code); + } + if (extension) + *extension = best->name; return get_strings_entry(best->static_info.strings_errors, error_code - best->first_error); } diff --git a/src/xcb_errors.h b/src/xcb_errors.h index a9c8d0d..177b04c 100644 --- a/src/xcb_errors.h +++ b/src/xcb_errors.h @@ -90,24 +90,30 @@ const char *xcb_errors_get_name_for_minor_code(xcb_errors_context_t *ctx, * * @param ctx An errors context, created with @ref xcb_errors_context_new () * @param event_code The response_type of an event. + * @param extension Will be set to the name of the extension that generated this + * event or NULL for unknown errors or core X11 errors. This argument may be + * NULL. * @return A string allocated in static storage that contains a name for this * major code. This will never return NULL, but other functions in this library * may. */ const char *xcb_errors_get_name_for_event(xcb_errors_context_t *ctx, - uint8_t event_code); + uint8_t event_code, const char **extension); /** * Get the name corresponding to some error. * * @param ctx An errors context, created with @ref xcb_errors_context_new () * @param error_code The error_code of an error reply. + * @param extension Will be set to the name of the extension that generated this + * event or NULL for unknown errors or core X11 errors. This argument may be + * NULL. * @return A string allocated in static storage that contains a name for this * major code. This will never return NULL, but other functions in this library * may. */ const char *xcb_errors_get_name_for_error(xcb_errors_context_t *ctx, - uint8_t error_code); + uint8_t error_code, const char **extension); #ifdef __cplusplus } diff --git a/tests/test.c b/tests/test.c index fae85a1..04794b6 100644 --- a/tests/test.c +++ b/tests/test.c @@ -41,25 +41,53 @@ static int check_request(xcb_errors_context_t *ctx, uint8_t opcode, const char * return 0; } -static int check_error(xcb_errors_context_t *ctx, uint8_t error, const char *expected) +static int check_error(xcb_errors_context_t *ctx, uint8_t error, + const char *expected, const char *expected_extension) { - const char *actual = xcb_errors_get_name_for_error(ctx, error); + const char *actual, *actual_extension, *tmp; + actual = xcb_errors_get_name_for_error(ctx, error, &actual_extension); + if (actual_extension != expected_extension && + strcmp(actual_extension, expected_extension) != 0) { + fprintf(stderr, "For error %d: Expected ext %s, got %s\n", error, + expected_extension, actual_extension); + return 1; + } if (strcmp(actual, expected) != 0) { fprintf(stderr, "For error %d: Expected %s, got %s\n", error, expected, actual); return 1; } + tmp = xcb_errors_get_name_for_error(ctx, error, NULL); + if (tmp != actual) { + fprintf(stderr, "For error %d: Passing NULL made a difference: %s vs %s\n", + error, actual, tmp); + return 1; + } return 0; } -static int check_event(xcb_errors_context_t *ctx, uint8_t event, const char *expected) +static int check_event(xcb_errors_context_t *ctx, uint8_t event, + const char *expected, const char *expected_extension) { - const char *actual = xcb_errors_get_name_for_event(ctx, event); + const char *actual, *actual_extension, *tmp; + actual = xcb_errors_get_name_for_event(ctx, event, &actual_extension); + if (actual_extension != expected_extension && + strcmp(actual_extension, expected_extension) != 0) { + fprintf(stderr, "For event %d: Expected ext %s, got %s\n", event, + expected_extension, actual_extension); + return 1; + } if (strcmp(actual, expected) != 0) { fprintf(stderr, "For event %d: Expected %s, got %s\n", event, expected, actual); return 1; } + tmp = xcb_errors_get_name_for_event(ctx, event, NULL); + if (tmp != actual) { + fprintf(stderr, "For event %d: Passing NULL made a difference: %s vs %s\n", + event, actual, tmp); + return 1; + } return 0; } @@ -111,10 +139,10 @@ static int test_randr(xcb_connection_t *c, xcb_errors_context_t *ctx) } err |= check_request(ctx, reply->major_opcode, "RandR"); - err |= check_error(ctx, reply->first_error + 0, "BadOutput"); - err |= check_error(ctx, reply->first_error + 3, "BadProvider"); - err |= check_event(ctx, reply->first_event + 0, "ScreenChangeNotify"); - err |= check_event(ctx, reply->first_event + 1, "Notify"); + err |= check_error(ctx, reply->first_error + 0, "BadOutput", "RandR"); + err |= check_error(ctx, reply->first_error + 3, "BadProvider", "RandR"); + err |= check_event(ctx, reply->first_event + 0, "ScreenChangeNotify", "RandR"); + err |= check_event(ctx, reply->first_event + 1, "Notify", "RandR"); err |= check_minor(ctx, reply->major_opcode, 0, "QueryVersion"); err |= check_minor(ctx, reply->major_opcode, 1, "Unknown (1)"); err |= check_minor(ctx, reply->major_opcode, 33, "GetProviderInfo"); @@ -151,15 +179,15 @@ static int test_valid_connection(void) err |= check_minor(ctx, XCB_CREATE_WINDOW, 42, NULL); err |= check_minor(ctx, XCB_CREATE_WINDOW, 0xffff, NULL); - err |= check_error(ctx, XCB_REQUEST, "Request"); - err |= check_error(ctx, XCB_IMPLEMENTATION, "Implementation"); - err |= check_error(ctx, 18, "Unknown (18)"); - err |= check_error(ctx, 127, "Unknown (127)"); + err |= check_error(ctx, XCB_REQUEST, "Request", NULL); + err |= check_error(ctx, XCB_IMPLEMENTATION, "Implementation", NULL); + err |= check_error(ctx, 18, "Unknown (18)", NULL); + err |= check_error(ctx, 127, "Unknown (127)", NULL); - err |= check_event(ctx, XCB_KEY_PRESS, "KeyPress"); - err |= check_event(ctx, XCB_KEY_RELEASE, "KeyRelease"); - err |= check_event(ctx, XCB_GE_GENERIC, "GeGeneric"); - err |= check_event(ctx, 36, "Unknown (36)"); + err |= check_event(ctx, XCB_KEY_PRESS, "KeyPress", NULL); + err |= check_event(ctx, XCB_KEY_RELEASE, "KeyRelease", NULL); + err |= check_event(ctx, XCB_GE_GENERIC, "GeGeneric", NULL); + err |= check_event(ctx, 36, "Unknown (36)", NULL); err |= test_randr(c, ctx); |