src/SDL_assert.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 12 Aug 2016 19:59:00 -0400
changeset 10266 c09f06c4e8c8
parent 9998 f67cf37e9cd4
permissions -rw-r--r--
emscripten: send fake mouse events for touches, like other targets do. (This really should be handled at the higher level and not in the individual targets, but this fixes the immediate bug.)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     2
  Simple DirectMedia Layer
9998
f67cf37e9cd4 Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     7
  arising from the use of this software.
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    11
  freely, subject to the following restrictions:
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
8093
b43765095a6f Make internal SDL sources include SDL_internal.h instead of SDL_config.h
Ryan C. Gordon <icculus@icculus.org>
parents: 7828
diff changeset
    21
#include "./SDL_internal.h"
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
7828
1451063c8ecd Fixed building using MinGW
Sam Lantinga <slouken@libsdl.org>
parents: 7588
diff changeset
    23
#if defined(__WIN32__)
1451063c8ecd Fixed building using MinGW
Sam Lantinga <slouken@libsdl.org>
parents: 7588
diff changeset
    24
#include "core/windows/SDL_windows.h"
1451063c8ecd Fixed building using MinGW
Sam Lantinga <slouken@libsdl.org>
parents: 7588
diff changeset
    25
#endif
1451063c8ecd Fixed building using MinGW
Sam Lantinga <slouken@libsdl.org>
parents: 7588
diff changeset
    26
3651
cb5b1aedb5a7 Fixed include ordering
Sam Lantinga <slouken@libsdl.org>
parents: 3648
diff changeset
    27
#include "SDL.h"
5006
8e8876e4aec6 Include windows.h in SDL_atomic.h by default, but don't include the atomic API in SDL.h
Sam Lantinga <slouken@libsdl.org>
parents: 4472
diff changeset
    28
#include "SDL_atomic.h"
6621
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
    29
#include "SDL_messagebox.h"
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
    30
#include "SDL_video.h"
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
#include "SDL_assert.h"
4472
791b3256fb22 Mostly cleaned up warnings with -Wmissing-prototypes
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
    32
#include "SDL_assert_c.h"
3671
0d6f520c0eb9 Fixed building under Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 3670
diff changeset
    33
#include "video/SDL_sysvideo.h"
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    34
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    35
#ifdef __WIN32__
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    36
#ifndef WS_OVERLAPPEDWINDOW
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    37
#define WS_OVERLAPPEDWINDOW 0
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    38
#endif
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    39
#else  /* fprintf, _exit(), etc. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    40
#include <stdio.h>
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    41
#include <stdlib.h>
8341
99c7c87774d5 WinRT: build fix for SDL_assert.c
David Ludwig <dludwig@pobox.com>
parents: 8337
diff changeset
    42
#if ! defined(__WINRT__)
3648
a9d830c05998 Fixed build problems with gcc __attribute__.
Ryan C. Gordon <icculus@icculus.org>
parents: 3647
diff changeset
    43
#include <unistd.h>
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    44
#endif
8341
99c7c87774d5 WinRT: build fix for SDL_assert.c
David Ludwig <dludwig@pobox.com>
parents: 8337
diff changeset
    45
#endif
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    46
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    47
static SDL_assert_state
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    48
SDL_PromptAssertion(const SDL_assert_data *data, void *userdata);
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    49
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    50
/*
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    51
 * We keep all triggered assertions in a singly-linked list so we can
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    52
 *  generate a report later.
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    53
 */
5541
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    54
static SDL_assert_data *triggered_assertions = NULL;
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    55
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    56
static SDL_mutex *assertion_mutex = NULL;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    57
static SDL_AssertionHandler assertion_handler = SDL_PromptAssertion;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    58
static void *assertion_userdata = NULL;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    59
3648
a9d830c05998 Fixed build problems with gcc __attribute__.
Ryan C. Gordon <icculus@icculus.org>
parents: 3647
diff changeset
    60
#ifdef __GNUC__
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
    61
static void
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
    62
debug_print(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
3648
a9d830c05998 Fixed build problems with gcc __attribute__.
Ryan C. Gordon <icculus@icculus.org>
parents: 3647
diff changeset
    63
#endif
a9d830c05998 Fixed build problems with gcc __attribute__.
Ryan C. Gordon <icculus@icculus.org>
parents: 3647
diff changeset
    64
a9d830c05998 Fixed build problems with gcc __attribute__.
Ryan C. Gordon <icculus@icculus.org>
parents: 3647
diff changeset
    65
static void
a9d830c05998 Fixed build problems with gcc __attribute__.
Ryan C. Gordon <icculus@icculus.org>
parents: 3647
diff changeset
    66
debug_print(const char *fmt, ...)
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    67
{
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    68
    va_list ap;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    69
    va_start(ap, fmt);
6621
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
    70
    SDL_LogMessageV(SDL_LOG_CATEGORY_ASSERT, SDL_LOG_PRIORITY_WARN, fmt, ap);
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    71
    va_end(ap);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    72
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    73
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    74
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    75
static void SDL_AddAssertionToReport(SDL_assert_data *data)
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    76
{
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    77
    /* (data) is always a static struct defined with the assert macros, so
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    78
       we don't have to worry about copying or allocating them. */
5541
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    79
    data->trigger_count++;
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    80
    if (data->trigger_count == 1) {  /* not yet added? */
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    81
        data->next = triggered_assertions;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    82
        triggered_assertions = data;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    83
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    84
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    85
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    86
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    87
static void SDL_GenerateAssertionReport(void)
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    88
{
5541
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    89
    const SDL_assert_data *item = triggered_assertions;
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    90
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    91
    /* only do this if the app hasn't assigned an assertion handler. */
5541
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    92
    if ((item != NULL) && (assertion_handler != SDL_PromptAssertion)) {
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    93
        debug_print("\n\nSDL assertion report.\n");
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    94
        debug_print("All SDL assertions between last init/quit:\n\n");
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    95
5541
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    96
        while (item != NULL) {
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    97
            debug_print(
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    98
                "'%s'\n"
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    99
                "    * %s (%s:%d)\n"
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   100
                "    * triggered %u time%s.\n"
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   101
                "    * always ignore: %s.\n",
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   102
                item->condition, item->function, item->filename,
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   103
                item->linenum, item->trigger_count,
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   104
                (item->trigger_count == 1) ? "" : "s",
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   105
                item->always_ignore ? "yes" : "no");
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   106
            item = item->next;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   107
        }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   108
        debug_print("\n");
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   109
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   110
        SDL_ResetAssertionReport();
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   111
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   112
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   113
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   114
static void SDL_ExitProcess(int exitcode)
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   115
{
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   116
#ifdef __WIN32__
6305
601b0e251822 SDL_ExitProcess() was ignoring exit code parameter.
Ryan C. Gordon <icculus@icculus.org>
parents: 6217
diff changeset
   117
    ExitProcess(exitcode);
3656
f17ea6f49745 Assume _exit() is available. It may be, even if unix isn't defined.
Sam Lantinga <slouken@libsdl.org>
parents: 3655
diff changeset
   118
#else
6305
601b0e251822 SDL_ExitProcess() was ignoring exit code parameter.
Ryan C. Gordon <icculus@icculus.org>
parents: 6217
diff changeset
   119
    _exit(exitcode);
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   120
#endif
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   121
}
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   122
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   123
static void SDL_AbortAssertion(void)
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   124
{
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   125
    SDL_Quit();
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   126
    SDL_ExitProcess(42);
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   127
}
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   128
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   129
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   130
static SDL_assert_state
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   131
SDL_PromptAssertion(const SDL_assert_data *data, void *userdata)
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   132
{
6759
b74da0b33530 SDL_assertion messages use "\n" instead of "\r\n" on non-Windows platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 6621
diff changeset
   133
#ifdef __WIN32__
b74da0b33530 SDL_assertion messages use "\n" instead of "\r\n" on non-Windows platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 6621
diff changeset
   134
    #define ENDLINE "\r\n"
b74da0b33530 SDL_assertion messages use "\n" instead of "\r\n" on non-Windows platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 6621
diff changeset
   135
#else
b74da0b33530 SDL_assertion messages use "\n" instead of "\r\n" on non-Windows platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 6621
diff changeset
   136
    #define ENDLINE "\n"
b74da0b33530 SDL_assertion messages use "\n" instead of "\r\n" on non-Windows platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 6621
diff changeset
   137
#endif
b74da0b33530 SDL_assertion messages use "\n" instead of "\r\n" on non-Windows platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 6621
diff changeset
   138
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   139
    const char *envr;
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   140
    SDL_assert_state state = SDL_ASSERTION_ABORT;
3685
64ce267332c6 Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents: 3671
diff changeset
   141
    SDL_Window *window;
6621
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   142
    SDL_MessageBoxData messagebox;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   143
    SDL_MessageBoxButtonData buttons[] = {
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   144
        {   0,  SDL_ASSERTION_RETRY,            "Retry" },
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   145
        {   0,  SDL_ASSERTION_BREAK,            "Break" },
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   146
        {   0,  SDL_ASSERTION_ABORT,            "Abort" },
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   147
        {   SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT,
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   148
                SDL_ASSERTION_IGNORE,           "Ignore" },
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   149
        {   SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT,
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   150
                SDL_ASSERTION_ALWAYS_IGNORE,    "Always Ignore" }
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   151
    };
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   152
    char *message;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   153
    int selected;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   154
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   155
    (void) userdata;  /* unused in default handler. */
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   156
6621
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   157
    message = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE);
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   158
    if (!message) {
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   159
        /* Uh oh, we're in real trouble now... */
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   160
        return SDL_ASSERTION_ABORT;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   161
    }
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   162
    SDL_snprintf(message, SDL_MAX_LOG_MESSAGE,
6759
b74da0b33530 SDL_assertion messages use "\n" instead of "\r\n" on non-Windows platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 6621
diff changeset
   163
                 "Assertion failure at %s (%s:%d), triggered %u %s:" ENDLINE
b74da0b33530 SDL_assertion messages use "\n" instead of "\r\n" on non-Windows platforms.
Ryan C. Gordon <icculus@icculus.org>
parents: 6621
diff changeset
   164
                    "  '%s'",
6621
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   165
                 data->function, data->filename, data->linenum,
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   166
                 data->trigger_count, (data->trigger_count == 1) ? "time" : "times",
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   167
                 data->condition);
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   168
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   169
    debug_print("\n\n%s\n\n", message);
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   170
3655
1cc7f0143c12 Moved SDL_FUNCTION out so it's always available, and added SDL_FILE and SDL_LINE
Sam Lantinga <slouken@libsdl.org>
parents: 3651
diff changeset
   171
    /* let env. variable override, so unit tests won't block in a GUI. */
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   172
    envr = SDL_getenv("SDL_ASSERT");
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   173
    if (envr != NULL) {
6621
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   174
        SDL_stack_free(message);
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   175
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   176
        if (SDL_strcmp(envr, "abort") == 0) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   177
            return SDL_ASSERTION_ABORT;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   178
        } else if (SDL_strcmp(envr, "break") == 0) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   179
            return SDL_ASSERTION_BREAK;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   180
        } else if (SDL_strcmp(envr, "retry") == 0) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   181
            return SDL_ASSERTION_RETRY;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   182
        } else if (SDL_strcmp(envr, "ignore") == 0) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   183
            return SDL_ASSERTION_IGNORE;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   184
        } else if (SDL_strcmp(envr, "always_ignore") == 0) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   185
            return SDL_ASSERTION_ALWAYS_IGNORE;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   186
        } else {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   187
            return SDL_ASSERTION_ABORT;  /* oh well. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   188
        }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   189
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   190
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   191
    /* Leave fullscreen mode, if possible (scary!) */
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   192
    window = SDL_GetFocusWindow();
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   193
    if (window) {
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   194
        if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) {
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   195
            SDL_MinimizeWindow(window);
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   196
        } else {
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   197
            /* !!! FIXME: ungrab the input if we're not fullscreen? */
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   198
            /* No need to mess with the window */
6621
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   199
            window = NULL;
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   200
        }
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   201
    }
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   202
6621
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   203
    /* Show a messagebox if we can, otherwise fall back to stdio */
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   204
    SDL_zero(messagebox);
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   205
    messagebox.flags = SDL_MESSAGEBOX_WARNING;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   206
    messagebox.window = window;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   207
    messagebox.title = "Assertion Failed";
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   208
    messagebox.message = message;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   209
    messagebox.numbuttons = SDL_arraysize(buttons);
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   210
    messagebox.buttons = buttons;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   211
6621
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   212
    if (SDL_ShowMessageBox(&messagebox, &selected) == 0) {
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   213
        if (selected == -1) {
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   214
            state = SDL_ASSERTION_IGNORE;
6621
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   215
        } else {
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   216
            state = (SDL_assert_state)selected;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   217
        }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   218
    }
6621
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   219
#ifdef HAVE_STDIO_H
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   220
    else
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   221
    {
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   222
        /* this is a little hacky. */
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   223
        for ( ; ; ) {
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   224
            char buf[32];
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   225
            fprintf(stderr, "Abort/Break/Retry/Ignore/AlwaysIgnore? [abriA] : ");
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   226
            fflush(stderr);
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   227
            if (fgets(buf, sizeof (buf), stdin) == NULL) {
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   228
                break;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   229
            }
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   230
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   231
            if (SDL_strcmp(buf, "a") == 0) {
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   232
                state = SDL_ASSERTION_ABORT;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   233
                break;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   234
            } else if (SDL_strcmp(buf, "b") == 0) {
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   235
                state = SDL_ASSERTION_BREAK;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   236
                break;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   237
            } else if (SDL_strcmp(buf, "r") == 0) {
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   238
                state = SDL_ASSERTION_RETRY;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   239
                break;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   240
            } else if (SDL_strcmp(buf, "i") == 0) {
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   241
                state = SDL_ASSERTION_IGNORE;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   242
                break;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   243
            } else if (SDL_strcmp(buf, "A") == 0) {
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   244
                state = SDL_ASSERTION_ALWAYS_IGNORE;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   245
                break;
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   246
            }
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   247
        }
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   248
    }
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   249
#endif /* HAVE_STDIO_H */
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   250
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   251
    /* Re-enter fullscreen mode */
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   252
    if (window) {
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   253
        SDL_RestoreWindow(window);
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   254
    }
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   255
6621
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   256
    SDL_stack_free(message);
25504f9ab078 Switched assert system to use new message box functionality
Sam Lantinga <slouken@libsdl.org>
parents: 6305
diff changeset
   257
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   258
    return state;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   259
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   260
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   261
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   262
SDL_assert_state
3655
1cc7f0143c12 Moved SDL_FUNCTION out so it's always available, and added SDL_FILE and SDL_LINE
Sam Lantinga <slouken@libsdl.org>
parents: 3651
diff changeset
   263
SDL_ReportAssertion(SDL_assert_data *data, const char *func, const char *file,
1cc7f0143c12 Moved SDL_FUNCTION out so it's always available, and added SDL_FILE and SDL_LINE
Sam Lantinga <slouken@libsdl.org>
parents: 3651
diff changeset
   264
                    int line)
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   265
{
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   266
    static int assertion_running = 0;
3662
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   267
    static SDL_SpinLock spinlock = 0;
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   268
    SDL_assert_state state = SDL_ASSERTION_IGNORE;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   269
3662
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   270
    SDL_AtomicLock(&spinlock);
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   271
    if (assertion_mutex == NULL) { /* never called SDL_Init()? */
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   272
        assertion_mutex = SDL_CreateMutex();
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   273
        if (assertion_mutex == NULL) {
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   274
            SDL_AtomicUnlock(&spinlock);
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   275
            return SDL_ASSERTION_IGNORE;   /* oh well, I guess. */
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   276
        }
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   277
    }
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   278
    SDL_AtomicUnlock(&spinlock);
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   279
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   280
    if (SDL_LockMutex(assertion_mutex) < 0) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   281
        return SDL_ASSERTION_IGNORE;   /* oh well, I guess. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   282
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   283
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   284
    /* doing this because Visual C is upset over assigning in the macro. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   285
    if (data->trigger_count == 0) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   286
        data->function = func;
3655
1cc7f0143c12 Moved SDL_FUNCTION out so it's always available, and added SDL_FILE and SDL_LINE
Sam Lantinga <slouken@libsdl.org>
parents: 3651
diff changeset
   287
        data->filename = file;
1cc7f0143c12 Moved SDL_FUNCTION out so it's always available, and added SDL_FILE and SDL_LINE
Sam Lantinga <slouken@libsdl.org>
parents: 3651
diff changeset
   288
        data->linenum = line;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   289
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   290
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   291
    SDL_AddAssertionToReport(data);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   292
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   293
    assertion_running++;
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   294
    if (assertion_running > 1) {   /* assert during assert! Abort. */
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   295
        if (assertion_running == 2) {
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   296
            SDL_AbortAssertion();
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   297
        } else if (assertion_running == 3) {  /* Abort asserted! */
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   298
            SDL_ExitProcess(42);
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   299
        } else {
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   300
            while (1) { /* do nothing but spin; what else can you do?! */ }
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   301
        }
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   302
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   303
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   304
    if (!data->always_ignore) {
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   305
        state = assertion_handler(data, assertion_userdata);
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   306
    }
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   307
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   308
    switch (state)
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   309
    {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   310
        case SDL_ASSERTION_ABORT:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   311
            SDL_AbortAssertion();
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   312
            return SDL_ASSERTION_IGNORE;  /* shouldn't return, but oh well. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   313
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   314
        case SDL_ASSERTION_ALWAYS_IGNORE:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   315
            state = SDL_ASSERTION_IGNORE;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   316
            data->always_ignore = 1;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   317
            break;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   318
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   319
        case SDL_ASSERTION_IGNORE:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   320
        case SDL_ASSERTION_RETRY:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   321
        case SDL_ASSERTION_BREAK:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   322
            break;  /* macro handles these. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   323
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   324
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   325
    assertion_running--;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   326
    SDL_UnlockMutex(assertion_mutex);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   327
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   328
    return state;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   329
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   330
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   331
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   332
void SDL_AssertionsQuit(void)
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   333
{
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   334
    SDL_GenerateAssertionReport();
3664
8bdc37b1a52a Handle assert init/quit better.
Ryan C. Gordon <icculus@icculus.org>
parents: 3662
diff changeset
   335
    if (assertion_mutex != NULL) {
8bdc37b1a52a Handle assert init/quit better.
Ryan C. Gordon <icculus@icculus.org>
parents: 3662
diff changeset
   336
        SDL_DestroyMutex(assertion_mutex);
8bdc37b1a52a Handle assert init/quit better.
Ryan C. Gordon <icculus@icculus.org>
parents: 3662
diff changeset
   337
        assertion_mutex = NULL;
8bdc37b1a52a Handle assert init/quit better.
Ryan C. Gordon <icculus@icculus.org>
parents: 3662
diff changeset
   338
    }
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   339
}
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   340
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   341
void SDL_SetAssertionHandler(SDL_AssertionHandler handler, void *userdata)
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   342
{
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   343
    if (handler != NULL) {
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   344
        assertion_handler = handler;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   345
        assertion_userdata = userdata;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   346
    } else {
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   347
        assertion_handler = SDL_PromptAssertion;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   348
        assertion_userdata = NULL;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   349
    }
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   350
}
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   351
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   352
const SDL_assert_data *SDL_GetAssertionReport(void)
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   353
{
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   354
    return triggered_assertions;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   355
}
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   356
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   357
void SDL_ResetAssertionReport(void)
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   358
{
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   359
    SDL_assert_data *next = NULL;
5541
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   360
    SDL_assert_data *item;
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   361
    for (item = triggered_assertions; item != NULL; item = next) {
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   362
        next = (SDL_assert_data *) item->next;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   363
        item->always_ignore = SDL_FALSE;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   364
        item->trigger_count = 0;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   365
        item->next = NULL;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   366
    }
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   367
5541
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   368
    triggered_assertions = NULL;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   369
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   370
8167
82ec2a95e664 Added SDL_GetAssertionHandler() and SDL_GetDefaultAssertionHandler().
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   371
SDL_AssertionHandler SDL_GetDefaultAssertionHandler(void)
82ec2a95e664 Added SDL_GetAssertionHandler() and SDL_GetDefaultAssertionHandler().
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   372
{
82ec2a95e664 Added SDL_GetAssertionHandler() and SDL_GetDefaultAssertionHandler().
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   373
    return SDL_PromptAssertion;
82ec2a95e664 Added SDL_GetAssertionHandler() and SDL_GetDefaultAssertionHandler().
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   374
}
82ec2a95e664 Added SDL_GetAssertionHandler() and SDL_GetDefaultAssertionHandler().
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   375
82ec2a95e664 Added SDL_GetAssertionHandler() and SDL_GetDefaultAssertionHandler().
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   376
SDL_AssertionHandler SDL_GetAssertionHandler(void **userdata)
82ec2a95e664 Added SDL_GetAssertionHandler() and SDL_GetDefaultAssertionHandler().
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   377
{
82ec2a95e664 Added SDL_GetAssertionHandler() and SDL_GetDefaultAssertionHandler().
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   378
    if (userdata != NULL) {
82ec2a95e664 Added SDL_GetAssertionHandler() and SDL_GetDefaultAssertionHandler().
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   379
        *userdata = assertion_userdata;
82ec2a95e664 Added SDL_GetAssertionHandler() and SDL_GetDefaultAssertionHandler().
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   380
    }
82ec2a95e664 Added SDL_GetAssertionHandler() and SDL_GetDefaultAssertionHandler().
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   381
    return assertion_handler;
82ec2a95e664 Added SDL_GetAssertionHandler() and SDL_GetDefaultAssertionHandler().
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   382
}
82ec2a95e664 Added SDL_GetAssertionHandler() and SDL_GetDefaultAssertionHandler().
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   383
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   384
/* vi: set ts=4 sw=4 expandtab: */