src/dynapi/gendynapi.pl
author Sam Lantinga <slouken@libsdl.org>
Mon, 22 Jun 2015 23:36:06 -0700
changeset 9776 952ff8a5076f
parent 9619 b94b6d0bff0f
child 9998 f67cf37e9cd4
permissions -rwxr-xr-x
Fixed bug 3030 - SDL_RecreateWindow fails to restore title, icon, etc.

Adam M.

It loses the title and icon when window recreation fails. For instance, this may happen when trying to create an OpenGL ES window on a system that doesn't support it. But at that point, the title and icon have already been lost.
icculus@8094
     1
#!/usr/bin/perl -w
icculus@8094
     2
icculus@8094
     3
#  Simple DirectMedia Layer
slouken@9619
     4
#  Copyright (C) 1997-2015 Sam Lantinga <slouken@libsdl.org>
icculus@8094
     5
#
icculus@8094
     6
#  This software is provided 'as-is', without any express or implied
icculus@8094
     7
#  warranty.  In no event will the authors be held liable for any damages
icculus@8094
     8
#  arising from the use of this software.
icculus@8094
     9
#
icculus@8094
    10
#  Permission is granted to anyone to use this software for any purpose,
icculus@8094
    11
#  including commercial applications, and to alter it and redistribute it
icculus@8094
    12
#  freely, subject to the following restrictions:
icculus@8094
    13
#
icculus@8094
    14
#  1. The origin of this software must not be misrepresented; you must not
icculus@8094
    15
#     claim that you wrote the original software. If you use this software
icculus@8094
    16
#     in a product, an acknowledgment in the product documentation would be
icculus@8094
    17
#     appreciated but is not required.
icculus@8094
    18
#  2. Altered source versions must be plainly marked as such, and must not be
icculus@8094
    19
#     misrepresented as being the original software.
icculus@8094
    20
#  3. This notice may not be removed or altered from any source distribution.
icculus@8094
    21
icculus@8094
    22
# WHAT IS THIS?
icculus@8094
    23
#  When you add a public API to SDL, please run this script, make sure the
icculus@8094
    24
#  output looks sane (hg diff, it adds to existing files), and commit it.
icculus@8094
    25
#  It keeps the dynamic API jump table operating correctly.
icculus@8094
    26
icculus@8094
    27
# If you wanted this to be readable, you shouldn't have used perl.
icculus@8094
    28
icculus@8094
    29
use warnings;
icculus@8094
    30
use strict;
icculus@8094
    31
use File::Basename;
icculus@8094
    32
icculus@8094
    33
chdir(dirname(__FILE__) . '/../..');
icculus@8094
    34
my $sdl_dynapi_procs_h = "src/dynapi/SDL_dynapi_procs.h";
icculus@8094
    35
my $sdl_dynapi_overrides_h = "src/dynapi/SDL_dynapi_overrides.h";
icculus@8094
    36
icculus@8094
    37
my %existing = ();
icculus@8094
    38
if (-f $sdl_dynapi_procs_h) {
icculus@8094
    39
    open(SDL_DYNAPI_PROCS_H, '<', $sdl_dynapi_procs_h) or die("Can't open $sdl_dynapi_procs_h: $!\n");
icculus@8094
    40
    while (<SDL_DYNAPI_PROCS_H>) {
icculus@8094
    41
        if (/\ASDL_DYNAPI_PROC\(.*?,(.*?),/) {
icculus@8094
    42
            $existing{$1} = 1;
icculus@8094
    43
        }
icculus@8094
    44
    }
icculus@8094
    45
    close(SDL_DYNAPI_PROCS_H)
icculus@8094
    46
}
icculus@8094
    47
icculus@8094
    48
open(SDL_DYNAPI_PROCS_H, '>>', $sdl_dynapi_procs_h) or die("Can't open $sdl_dynapi_procs_h: $!\n");
icculus@8094
    49
open(SDL_DYNAPI_OVERRIDES_H, '>>', $sdl_dynapi_overrides_h) or die("Can't open $sdl_dynapi_overrides_h: $!\n");
icculus@8094
    50
icculus@8094
    51
opendir(HEADERS, 'include') or die("Can't open include dir: $!\n");
icculus@8094
    52
while (readdir(HEADERS)) {
icculus@8094
    53
    next if not /\.h\Z/;
icculus@8094
    54
    my $header = "include/$_";
icculus@8094
    55
    open(HEADER, '<', $header) or die("Can't open $header: $!\n");
icculus@8094
    56
    while (<HEADER>) {
icculus@8094
    57
        chomp;
icculus@8094
    58
        next if not /\A\s*extern\s+DECLSPEC/;
icculus@8094
    59
        my $decl = "$_ ";
icculus@8094
    60
        if (not $decl =~ /\)\s*;/) {
icculus@8094
    61
            while (<HEADER>) {
icculus@8094
    62
                chomp;
icculus@8094
    63
                s/\A\s+//;
icculus@8094
    64
                s/\s+\Z//;
icculus@8094
    65
                $decl .= "$_ ";
icculus@8094
    66
                last if /\)\s*;/;
icculus@8094
    67
            }
icculus@8094
    68
        }
icculus@8094
    69
icculus@8094
    70
        $decl =~ s/\s+\Z//;
icculus@8094
    71
        #print("DECL: [$decl]\n");
icculus@8094
    72
icculus@8094
    73
        if ($decl =~ /\A\s*extern\s+DECLSPEC\s+(const\s+|)(unsigned\s+|)(.*?)\s*(\*?)\s*SDLCALL\s+(.*?)\s*\((.*?)\);/) {
icculus@8094
    74
            my $rc = "$1$2$3$4";
icculus@8094
    75
            my $fn = $5;
icculus@8094
    76
icculus@8094
    77
            next if $existing{$fn};   # already slotted into the jump table.
icculus@8094
    78
icculus@8094
    79
            my @params = split(',', $6);
icculus@8094
    80
icculus@8094
    81
            #print("rc == '$rc', fn == '$fn', params == '$params'\n");
icculus@8094
    82
icculus@8094
    83
            my $retstr = ($rc eq 'void') ? '' : 'return';
icculus@8094
    84
            my $paramstr = '(';
icculus@8094
    85
            my $argstr = '(';
icculus@8094
    86
            my $i = 0;
icculus@8094
    87
            foreach (@params) {
icculus@8094
    88
                my $str = $_;
icculus@8094
    89
                $str =~ s/\A\s+//;
icculus@8094
    90
                $str =~ s/\s+\Z//;
icculus@8094
    91
                #print("1PARAM: $str\n");
icculus@8094
    92
                if ($str eq 'void') {
icculus@8094
    93
                    $paramstr .= 'void';
icculus@8094
    94
                } elsif ($str eq '...') {
icculus@8094
    95
                    if ($i > 0) {
icculus@8094
    96
                        $paramstr .= ', ';
icculus@8094
    97
                    }
icculus@8094
    98
                    $paramstr .= $str;
icculus@8094
    99
                } elsif ($str =~ /\A\s*((const\s+|)(unsigned\s+|)([a-zA-Z0-9_]*)\s*([\*\s]*))\s*(.*?)\Z/) {
icculus@8094
   100
                    #print("PARSED: [$1], [$2], [$3], [$4], [$5]\n");
icculus@8094
   101
                    my $type = $1;
icculus@8094
   102
                    my $var = $6;
icculus@8094
   103
                    $type =~ s/\A\s+//;
icculus@8094
   104
                    $type =~ s/\s+\Z//;
icculus@8094
   105
                    $var =~ s/\A\s+//;
icculus@8094
   106
                    $var =~ s/\s+\Z//;
icculus@8094
   107
                    $type =~ s/\s*\*\Z/*/g;
icculus@8094
   108
                    $type =~ s/\s*(\*+)\Z/ $1/;
icculus@8094
   109
                    #print("SPLIT: ($type, $var)\n");
icculus@8094
   110
                    my $name = chr(ord('a') + $i);
icculus@8094
   111
                    if ($i > 0) {
icculus@8094
   112
                        $paramstr .= ', ';
icculus@8094
   113
                        $argstr .= ',';
icculus@8094
   114
                    }
icculus@8094
   115
                    my $spc = ($type =~ /\*\Z/) ? '' : ' ';
icculus@8094
   116
                    $paramstr .= "$type$spc$name";
icculus@8094
   117
                    $argstr .= "$name";
icculus@8094
   118
                }
icculus@8094
   119
                $i++;
icculus@8094
   120
            }
icculus@8094
   121
icculus@8094
   122
            $paramstr = '(void' if ($i == 0);  # Just to make this consistent.
icculus@8094
   123
icculus@8094
   124
            $paramstr .= ')';
icculus@8094
   125
            $argstr .= ')';
icculus@8094
   126
icculus@8094
   127
            print("NEW: $decl\n");
icculus@8094
   128
            print SDL_DYNAPI_PROCS_H "SDL_DYNAPI_PROC($rc,$fn,$paramstr,$argstr,$retstr)\n";
icculus@8094
   129
            print SDL_DYNAPI_OVERRIDES_H "#define $fn ${fn}_REAL\n";
icculus@8094
   130
        } else {
icculus@8094
   131
            print("Failed to parse decl [$decl]!\n");
icculus@8094
   132
        }
icculus@8094
   133
    }
icculus@8094
   134
    close(HEADER);
icculus@8094
   135
}
icculus@8094
   136
closedir(HEADERS);
icculus@8094
   137
icculus@8094
   138
close(SDL_DYNAPI_PROCS_H);
icculus@8094
   139
close(SDL_DYNAPI_OVERRIDES_H);
icculus@8094
   140
icculus@8094
   141
# vi: set ts=4 sw=4 expandtab: