test/testmessage.c
author Jørgen P. Tjernø <jorgen@valvesoftware.com>
Wed, 05 Jun 2013 12:48:44 -0700
changeset 7277 1290cd7f34af
parent 7088 664d8532219b
child 7456 193c9a1723ad
permissions -rw-r--r--
Mac: Fix invalid PS3 controller mapping.

PS3 controller had swapped D-Pad down & left buttons, as well as X & Y buttons.
Thanks to Alex Szpakowski for the bug report and fix.

Fixes http://bugzilla.libsdl.org/show_bug.cgi?id=1891
     1 /*
     2   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 
    13 /* Simple test of the SDL MessageBox API*/
    14 
    15 #include <stdio.h>
    16 #include <stdlib.h>
    17 #include <signal.h>
    18 
    19 #include "SDL.h"
    20 #include "SDL_thread.h"
    21 
    22 static int alive = 0;
    23 
    24 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    25 static void
    26 quit(int rc)
    27 {
    28     SDL_Quit();
    29     exit(rc);
    30 }
    31 
    32 static int
    33 button_messagebox(void *eventNumber)
    34 {
    35     const SDL_MessageBoxButtonData buttons[] = {
    36         {
    37             SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT,
    38             0,
    39             "OK"
    40         },{
    41             SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT,
    42             1,
    43             "Cancel"
    44         },
    45     };
    46 
    47     SDL_MessageBoxData data = {
    48         SDL_MESSAGEBOX_INFORMATION,
    49         NULL, // no parent window
    50         "Custom MessageBox",
    51         "This is a custom messagebox",
    52         2,
    53         buttons,
    54         NULL // Default color scheme
    55     };
    56 
    57     int button = -1;
    58     int success = 0;
    59     if (eventNumber) {
    60         data.message = "This is a custom messagebox from a background thread.";
    61     }
    62 
    63     success =SDL_ShowMessageBox(&data, &button);
    64     if (success == -1) {
    65         printf("Error Presenting MessageBox: %s\n", SDL_GetError());
    66         if (eventNumber) {
    67             SDL_UserEvent event;
    68             event.type = (intptr_t)eventNumber;
    69             SDL_PushEvent((SDL_Event*)&event);
    70             return 1;
    71         } else {
    72             quit(2);
    73         }
    74     }
    75     printf("Pressed button: %d, %s\n", button, button == 1 ? "Cancel" : "OK");
    76 
    77     if (eventNumber) {
    78         SDL_UserEvent event;
    79         event.type = (intptr_t)eventNumber;
    80         SDL_PushEvent((SDL_Event*)&event);
    81     }
    82 
    83     return 0;
    84 }
    85 
    86 int
    87 main(int argc, char *argv[])
    88 {
    89     int success;
    90 
    91     /* Load the SDL library */
    92     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    93         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    94         return (1);
    95     }
    96 
    97     success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
    98                 "Simple MessageBox",
    99                 "This is a simple error MessageBox",
   100                 NULL);
   101     if (success == -1) {
   102         printf("Error Presenting MessageBox: %s\n", SDL_GetError());
   103         quit(1);
   104     }
   105 
   106     success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
   107                 "Simple MessageBox",
   108                 "This is a simple MessageBox with a newline:\r\nHello world!",
   109                 NULL);
   110     if (success == -1) {
   111         printf("Error Presenting MessageBox: %s\n", SDL_GetError());
   112         quit(1);
   113     }
   114 
   115     /* Google says this is Traditional Chinese for "beef with broccoli" */
   116     success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
   117                 "UTF-8 Simple MessageBox",
   118                 "Unicode text: '牛肉西蘭花' ...",
   119                 NULL);
   120     if (success == -1) {
   121         printf("Error Presenting MessageBox: %s\n", SDL_GetError());
   122         quit(1);
   123     }
   124 
   125     /* Google says this is Traditional Chinese for "beef with broccoli" */
   126     success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
   127                 "UTF-8 Simple MessageBox",
   128                 "Unicode text and newline:\r\n'牛肉西蘭花'\n'牛肉西蘭花'",
   129                 NULL);
   130     if (success == -1) {
   131         printf("Error Presenting MessageBox: %s\n", SDL_GetError());
   132         quit(1);
   133     }
   134 
   135     button_messagebox(NULL);
   136 
   137     /* Technically this isn't a supported operation for the API, but it doesn't
   138      * hurt for it to work.
   139      */
   140     {
   141         int status = 0;
   142         SDL_Event event;
   143         intptr_t eventNumber = SDL_RegisterEvents(1);
   144         SDL_Thread* thread = SDL_CreateThread(&button_messagebox, "MessageBox", (void*)eventNumber);
   145 
   146         while (SDL_WaitEvent(&event))
   147         {
   148             if (event.type == eventNumber) {
   149                 break;
   150             }
   151         }
   152 
   153         SDL_WaitThread(thread, &status);
   154 
   155         printf("Message box thread return %i\n", status);
   156     }
   157 
   158     SDL_Quit();
   159     return (0);
   160 }