src/SDL_assert.c
author Edward Rudd <urkle@outoforder.cc>
Wed, 26 Sep 2012 14:08:46 -0400
changeset 6462 5e09ac1aba80
parent 6305 601b0e251822
child 6621 25504f9ab078
child 8316 88f011703f39
permissions -rwxr-xr-x
allocate *Hint structures per Xlib docs
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
6138
4c64952a58fb Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6051
diff changeset
     3
  Copyright (C) 1997-2012 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
*/
6217
6952b11b7f46 Make sure that we use consistent configuration options on platforms like Windows so that command line builds and IDE builds have ABI compatibility.
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
    21
#include "SDL_config.h"
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
3651
cb5b1aedb5a7 Fixed include ordering
Sam Lantinga <slouken@libsdl.org>
parents: 3648
diff changeset
    23
#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
    24
#include "SDL_atomic.h"
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    25
#include "SDL_assert.h"
4472
791b3256fb22 Mostly cleaned up warnings with -Wmissing-prototypes
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
    26
#include "SDL_assert_c.h"
3671
0d6f520c0eb9 Fixed building under Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 3670
diff changeset
    27
#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
    28
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    29
#ifdef __WIN32__
5090
327f181542f1 Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
    30
#include "core/windows/SDL_windows.h"
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    31
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    32
#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
    33
#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
    34
#endif
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    35
#else  /* fprintf, _exit(), etc. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
#include <stdio.h>
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
#include <stdlib.h>
3648
a9d830c05998 Fixed build problems with gcc __attribute__.
Ryan C. Gordon <icculus@icculus.org>
parents: 3647
diff changeset
    38
#include <unistd.h>
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    39
#endif
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    40
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    41
static SDL_assert_state
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    42
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
    43
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    44
/*
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    45
 * 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
    46
 *  generate a report later.
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    47
 */
5541
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    48
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
    49
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    50
static SDL_mutex *assertion_mutex = NULL;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
    51
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
    52
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
    53
3648
a9d830c05998 Fixed build problems with gcc __attribute__.
Ryan C. Gordon <icculus@icculus.org>
parents: 3647
diff changeset
    54
#ifdef __GNUC__
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
    55
static void
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
    56
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
    57
#endif
a9d830c05998 Fixed build problems with gcc __attribute__.
Ryan C. Gordon <icculus@icculus.org>
parents: 3647
diff changeset
    58
a9d830c05998 Fixed build problems with gcc __attribute__.
Ryan C. Gordon <icculus@icculus.org>
parents: 3647
diff changeset
    59
static void
a9d830c05998 Fixed build problems with gcc __attribute__.
Ryan C. Gordon <icculus@icculus.org>
parents: 3647
diff changeset
    60
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
    61
{
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    62
#ifdef __WIN32__
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    63
    /* Format into a buffer for OutputDebugStringA(). */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    64
    char buf[1024];
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    65
    char *startptr;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    66
    char *ptr;
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    67
    LPTSTR tstr;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    68
    int len;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    69
    va_list ap;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    70
    va_start(ap, fmt);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    71
    len = (int) SDL_vsnprintf(buf, sizeof (buf), fmt, ap);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    72
    va_end(ap);
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
    /* Visual C's vsnprintf() may not null-terminate the buffer. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    75
    if ((len >= sizeof (buf)) || (len < 0)) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    76
        buf[sizeof (buf) - 1] = '\0';
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    77
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    78
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    79
    /* Write it, sorting out the Unix newlines... */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
    startptr = buf;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    81
    for (ptr = startptr; *ptr; ptr++) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    82
        if (*ptr == '\n') {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    83
            *ptr = '\0';
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    84
            tstr = WIN_UTF8ToString(startptr);
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    85
            OutputDebugString(tstr);
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    86
            SDL_free(tstr);
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    87
            OutputDebugString(TEXT("\r\n"));
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    88
            startptr = ptr+1;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    89
        }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    90
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    91
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    92
    /* catch that last piece if it didn't have a newline... */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    93
    if (startptr != ptr) {
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    94
        tstr = WIN_UTF8ToString(startptr);
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    95
        OutputDebugString(tstr);
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
    96
        SDL_free(tstr);
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    97
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    98
#else
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    99
    /* Unix has it easy. Just dump it to stderr. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   100
    va_list ap;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   101
    va_start(ap, fmt);
3669
46d27a9571fa Whoops, that should be vfprintf(), not fprintf(). :)
Ryan C. Gordon <icculus@icculus.org>
parents: 3664
diff changeset
   102
    vfprintf(stderr, fmt, ap);
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   103
    va_end(ap);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   104
    fflush(stderr);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   105
#endif
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   106
}
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
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   109
#ifdef __WIN32__
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   110
static SDL_assert_state SDL_Windows_AssertChoice = SDL_ASSERTION_ABORT;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   111
static const SDL_assert_data *SDL_Windows_AssertData = NULL;
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
static LRESULT CALLBACK
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   114
SDL_Assertion_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   115
{
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   116
    switch (msg)
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   117
    {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   118
        case WM_CREATE:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   119
        {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   120
            /* !!! FIXME: all this code stinks. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   121
            const SDL_assert_data *data = SDL_Windows_AssertData;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   122
            char buf[1024];
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   123
            LPTSTR tstr;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   124
            const int w = 100;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   125
            const int h = 25;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   126
            const int gap = 10;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   127
            int x = gap;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   128
            int y = 50;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   129
            int len;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   130
            int i;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   131
            static const struct { 
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   132
                LPCTSTR name;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   133
                SDL_assert_state state;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   134
            } buttons[] = {
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   135
                {TEXT("Abort"), SDL_ASSERTION_ABORT },
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   136
                {TEXT("Break"), SDL_ASSERTION_BREAK },
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   137
                {TEXT("Retry"), SDL_ASSERTION_RETRY },
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   138
                {TEXT("Ignore"), SDL_ASSERTION_IGNORE },
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   139
                {TEXT("Always Ignore"), SDL_ASSERTION_ALWAYS_IGNORE },
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   140
            };
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   141
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   142
            len = (int) SDL_snprintf(buf, sizeof (buf), 
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   143
                         "Assertion failure at %s (%s:%d), triggered %u time%s:\r\n  '%s'",
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   144
                         data->function, data->filename, data->linenum,
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   145
                         data->trigger_count, (data->trigger_count == 1) ? "" : "s",
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   146
                         data->condition);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   147
            if ((len < 0) || (len >= sizeof (buf))) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   148
                buf[sizeof (buf) - 1] = '\0';
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   149
            }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   150
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   151
            tstr = WIN_UTF8ToString(buf);
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   152
            CreateWindow(TEXT("STATIC"), tstr,
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   153
                         WS_VISIBLE | WS_CHILD | SS_LEFT,
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   154
                         x, y, 550, 100,
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   155
                         hwnd, (HMENU) 1, NULL, NULL);
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   156
            SDL_free(tstr);
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   157
            y += 110;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   158
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   159
            for (i = 0; i < (sizeof (buttons) / sizeof (buttons[0])); i++) {
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   160
                CreateWindow(TEXT("BUTTON"), buttons[i].name,
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   161
                         WS_VISIBLE | WS_CHILD,
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   162
                         x, y, w, h,
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   163
                         hwnd, (HMENU) buttons[i].state, NULL, NULL);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   164
                x += w + gap;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   165
            }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   166
            break;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   167
        }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   168
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   169
        case WM_COMMAND:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   170
            SDL_Windows_AssertChoice = ((SDL_assert_state) (LOWORD(wParam)));
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   171
            SDL_Windows_AssertData = NULL;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   172
            break;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   173
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   174
        case WM_DESTROY:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   175
            SDL_Windows_AssertData = NULL;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   176
            break;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   177
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   178
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   179
    return DefWindowProc(hwnd, msg, wParam, lParam);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   180
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   181
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   182
static SDL_assert_state
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   183
SDL_PromptAssertion_windows(const SDL_assert_data *data)
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   184
{
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   185
    HINSTANCE hInstance = 0;  /* !!! FIXME? */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   186
    HWND hwnd;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   187
    MSG msg;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   188
    WNDCLASS wc = {0};
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
    SDL_Windows_AssertChoice = SDL_ASSERTION_ABORT;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   191
    SDL_Windows_AssertData = data;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   192
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   193
    wc.lpszClassName = TEXT("SDL_assert");
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   194
    wc.hInstance = hInstance ;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   195
    wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   196
    wc.lpfnWndProc = SDL_Assertion_WndProc;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   197
    wc.hCursor = LoadCursor(0, IDC_ARROW);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   198
  
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   199
    RegisterClass(&wc);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   200
    hwnd = CreateWindow(wc.lpszClassName, TEXT("SDL assertion failure"),
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   201
                 WS_OVERLAPPEDWINDOW | WS_VISIBLE,
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   202
                 150, 150, 570, 260, 0, 0, hInstance, 0);  
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   203
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   204
    while (GetMessage(&msg, NULL, 0, 0) && (SDL_Windows_AssertData != NULL)) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   205
        TranslateMessage(&msg);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   206
        DispatchMessage(&msg);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   207
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   208
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   209
    DestroyWindow(hwnd);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   210
    UnregisterClass(wc.lpszClassName, hInstance);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   211
    return SDL_Windows_AssertChoice;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   212
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   213
#endif
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   214
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   215
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   216
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
   217
{
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   218
    /* (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
   219
       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
   220
    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
   221
    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
   222
        data->next = triggered_assertions;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   223
        triggered_assertions = data;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   224
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   225
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   226
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   227
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   228
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
   229
{
5541
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   230
    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
   231
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   232
    /* 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
   233
    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
   234
        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
   235
        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
   236
5541
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   237
        while (item != NULL) {
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   238
            debug_print(
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   239
                "'%s'\n"
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   240
                "    * %s (%s:%d)\n"
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   241
                "    * 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
   242
                "    * always ignore: %s.\n",
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   243
                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
   244
                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
   245
                (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
   246
                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
   247
            item = item->next;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   248
        }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   249
        debug_print("\n");
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   250
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   251
        SDL_ResetAssertionReport();
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   252
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   253
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   254
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   255
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
   256
{
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   257
#ifdef __WIN32__
6305
601b0e251822 SDL_ExitProcess() was ignoring exit code parameter.
Ryan C. Gordon <icculus@icculus.org>
parents: 6217
diff changeset
   258
    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
   259
#else
6305
601b0e251822 SDL_ExitProcess() was ignoring exit code parameter.
Ryan C. Gordon <icculus@icculus.org>
parents: 6217
diff changeset
   260
    _exit(exitcode);
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   261
#endif
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   262
}
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   263
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   264
static void SDL_AbortAssertion(void)
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   265
{
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   266
    SDL_Quit();
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   267
    SDL_ExitProcess(42);
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   268
}
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   269
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   270
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   271
static SDL_assert_state
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   272
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
   273
{
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   274
    const char *envr;
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   275
    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
   276
    SDL_Window *window;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   277
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   278
    (void) userdata;  /* unused in default handler. */
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
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
    debug_print("\n\n"
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   281
                "Assertion failure at %s (%s:%d), 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
   282
                "  '%s'\n"
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   283
                "\n",
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   284
                data->function, data->filename, data->linenum,
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   285
                data->trigger_count, (data->trigger_count == 1) ? "" : "s",
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   286
                data->condition);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   287
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
   288
    /* 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
   289
    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
   290
    if (envr != NULL) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   291
        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
   292
            return SDL_ASSERTION_ABORT;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   293
        } 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
   294
            return SDL_ASSERTION_BREAK;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   295
        } 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
   296
            return SDL_ASSERTION_RETRY;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   297
        } 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
   298
            return SDL_ASSERTION_IGNORE;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   299
        } 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
   300
            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
   301
        } else {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   302
            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
   303
        }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   304
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   305
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   306
    /* Leave fullscreen mode, if possible (scary!) */
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   307
    window = SDL_GetFocusWindow();
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   308
    if (window) {
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   309
        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
   310
            SDL_MinimizeWindow(window);
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   311
        } else {
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   312
            /* !!! 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
   313
            /* No need to mess with the window */
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   314
            window = 0;
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   315
        }
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   316
    }
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   317
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   318
    /* platform-specific UI... */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   319
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5064
diff changeset
   320
#ifdef __WIN32__
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   321
    state = SDL_PromptAssertion_windows(data);
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   322
6051
cb1f941ce38e Mac OS X: Fixed build when compiling without Cocoa support.
Ryan C. Gordon <icculus@icculus.org>
parents: 5547
diff changeset
   323
#elif defined __MACOSX__ && defined SDL_VIDEO_DRIVER_COCOA
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   324
    /* This has to be done in an Objective-C (*.m) file, so we call out. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   325
    extern SDL_assert_state SDL_PromptAssertion_cocoa(const SDL_assert_data *);
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   326
    state = SDL_PromptAssertion_cocoa(data);
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   327
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   328
#else
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   329
    /* this is a little hacky. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   330
    for ( ; ; ) {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   331
        char buf[32];
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   332
        fprintf(stderr, "Abort/Break/Retry/Ignore/AlwaysIgnore? [abriA] : ");
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   333
        fflush(stderr);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   334
        if (fgets(buf, sizeof (buf), stdin) == NULL) {
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   335
            break;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   336
        }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   337
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   338
        if (SDL_strcmp(buf, "a") == 0) {
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   339
            state = SDL_ASSERTION_ABORT;
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   340
            break;
5547
4ccecd0901e2 Assert code's stdio interface was reading from the wrong variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 5541
diff changeset
   341
        } else if (SDL_strcmp(buf, "b") == 0) {
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   342
            state = SDL_ASSERTION_BREAK;
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   343
            break;
5547
4ccecd0901e2 Assert code's stdio interface was reading from the wrong variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 5541
diff changeset
   344
        } else if (SDL_strcmp(buf, "r") == 0) {
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   345
            state = SDL_ASSERTION_RETRY;
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   346
            break;
5547
4ccecd0901e2 Assert code's stdio interface was reading from the wrong variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 5541
diff changeset
   347
        } else if (SDL_strcmp(buf, "i") == 0) {
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   348
            state = SDL_ASSERTION_IGNORE;
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   349
            break;
5547
4ccecd0901e2 Assert code's stdio interface was reading from the wrong variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 5541
diff changeset
   350
        } else if (SDL_strcmp(buf, "A") == 0) {
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   351
            state = SDL_ASSERTION_ALWAYS_IGNORE;
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   352
            break;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   353
        }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   354
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   355
#endif
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   356
3657
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   357
    /* Re-enter fullscreen mode */
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   358
    if (window) {
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   359
        SDL_RestoreWindow(window);
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   360
    }
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   361
eaea59cee6f2 Leave fullscreen mode to pop an assertion dialog
Sam Lantinga <slouken@libsdl.org>
parents: 3656
diff changeset
   362
    return state;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   363
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   364
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   365
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   366
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
   367
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
   368
                    int line)
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   369
{
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   370
    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
   371
    static SDL_SpinLock spinlock = 0;
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   372
    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
   373
3662
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   374
    SDL_AtomicLock(&spinlock);
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   375
    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
   376
        assertion_mutex = SDL_CreateMutex();
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   377
        if (assertion_mutex == NULL) {
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   378
            SDL_AtomicUnlock(&spinlock);
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   379
            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
   380
        }
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   381
    }
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
diff changeset
   382
    SDL_AtomicUnlock(&spinlock);
6a0b3048f271 Handle assertion failures when SDL_Init() isn't called.
Ryan C. Gordon <icculus@icculus.org>
parents: 3661
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
    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
   385
        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
   386
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   387
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   388
    /* 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
   389
    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
   390
        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
   391
        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
   392
        data->linenum = line;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   393
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   394
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   395
    SDL_AddAssertionToReport(data);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   396
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   397
    assertion_running++;
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   398
    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
   399
        if (assertion_running == 2) {
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   400
            SDL_AbortAssertion();
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   401
        } else if (assertion_running == 3) {  /* Abort asserted! */
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   402
            SDL_ExitProcess(42);
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   403
        } else {
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   404
            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
   405
        }
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   406
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   407
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   408
    if (!data->always_ignore) {
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   409
        state = assertion_handler(data, assertion_userdata);
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   410
    }
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   411
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   412
    switch (state)
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   413
    {
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   414
        case SDL_ASSERTION_ABORT:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   415
            SDL_AbortAssertion();
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   416
            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
   417
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   418
        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
   419
            state = SDL_ASSERTION_IGNORE;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   420
            data->always_ignore = 1;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   421
            break;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   422
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   423
        case SDL_ASSERTION_IGNORE:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   424
        case SDL_ASSERTION_RETRY:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   425
        case SDL_ASSERTION_BREAK:
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   426
            break;  /* macro handles these. */
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   427
    }
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   428
3661
22b6a0c7ea6e Handle assertion failures during assertion handler.
Ryan C. Gordon <icculus@icculus.org>
parents: 3657
diff changeset
   429
    assertion_running--;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   430
    SDL_UnlockMutex(assertion_mutex);
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   431
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   432
    return state;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   433
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   434
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   435
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   436
int SDL_AssertionsInit(void)
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   437
{
3664
8bdc37b1a52a Handle assert init/quit better.
Ryan C. Gordon <icculus@icculus.org>
parents: 3662
diff changeset
   438
    /* this is a no-op at the moment. */
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   439
    return 0;
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   440
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   441
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   442
void SDL_AssertionsQuit(void)
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   443
{
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   444
    SDL_GenerateAssertionReport();
3664
8bdc37b1a52a Handle assert init/quit better.
Ryan C. Gordon <icculus@icculus.org>
parents: 3662
diff changeset
   445
    if (assertion_mutex != NULL) {
8bdc37b1a52a Handle assert init/quit better.
Ryan C. Gordon <icculus@icculus.org>
parents: 3662
diff changeset
   446
        SDL_DestroyMutex(assertion_mutex);
8bdc37b1a52a Handle assert init/quit better.
Ryan C. Gordon <icculus@icculus.org>
parents: 3662
diff changeset
   447
        assertion_mutex = NULL;
8bdc37b1a52a Handle assert init/quit better.
Ryan C. Gordon <icculus@icculus.org>
parents: 3662
diff changeset
   448
    }
3670
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   449
}
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   450
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   451
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
   452
{
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   453
    if (handler != NULL) {
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   454
        assertion_handler = handler;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   455
        assertion_userdata = userdata;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   456
    } else {
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   457
        assertion_handler = SDL_PromptAssertion;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   458
        assertion_userdata = NULL;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   459
    }
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   460
}
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   461
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   462
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
   463
{
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   464
    return triggered_assertions;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   465
}
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   466
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   467
void SDL_ResetAssertionReport(void)
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   468
{
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   469
    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
   470
    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
   471
    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
   472
        next = (SDL_assert_data *) item->next;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   473
        item->always_ignore = SDL_FALSE;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   474
        item->trigger_count = 0;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   475
        item->next = NULL;
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   476
    }
62b6a5b99918 Clean up assertion API for public use.
Ryan C. Gordon <icculus@icculus.org>
parents: 3669
diff changeset
   477
5541
b63f1383f8c9 Removed assertion list terminator (just do it like a normal linked list).
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   478
    triggered_assertions = NULL;
3647
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   479
}
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   480
c5925cd41955 First pass at Ryan's assertion code, minor tweaks to come.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   481
/* vi: set ts=4 sw=4 expandtab: */