1 //********************************************************************************************
2 //*
3 //*    This file is part of Egoboo.
4 //*
5 //*    Egoboo is free software: you can redistribute it and/or modify it
6 //*    under the terms of the GNU General Public License as published by
7 //*    the Free Software Foundation, either version 3 of the License, or
8 //*    (at your option) any later version.
9 //*
10 //*    Egoboo is distributed in the hope that it will be useful, but
11 //*    WITHOUT ANY WARRANTY; without even the implied warranty of
12 //*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 //*    General Public License for more details.
14 //*
15 //*    You should have received a copy of the GNU General Public License
16 //*    along with Egoboo.  If not, see <http://www.gnu.org/licenses/>.
17 //*
18 //********************************************************************************************
19 
20 /// @file file_formats/scancode_file.c
21 /// @brief Functions to read and write Egoboo's basicdat/scantag.txt file
22 /// @details
23 
24 #include "scancode_file.h"
25 
26 #include "log.h"
27 #include "input.h"
28 
29 #include "egoboo_vfs.h"
30 #include "egoboo_fileutil.h"
31 
32 //--------------------------------------------------------------------------------------------
33 //--------------------------------------------------------------------------------------------
34 int       scantag_count = 0;
35 scantag_t scantag[MAXTAG];
36 
37 static void   scantag_reset();
38 static bool_t scantag_read_one( vfs_FILE *fileread );
39 
40 //--------------------------------------------------------------------------------------------
41 // Tag Reading
42 //--------------------------------------------------------------------------------------------
scantag_reset()43 void scantag_reset()
44 {
45     /// @details ZZ@> This function resets the tags
46     scantag_count = 0;
47 }
48 
49 //--------------------------------------------------------------------------------------------
scantag_read_one(vfs_FILE * fileread)50 bool_t scantag_read_one( vfs_FILE *fileread )
51 {
52     /// @details ZZ@> This function finds the next tag, returning btrue if it found one
53 
54     bool_t retval;
55 
56     retval = goto_colon( NULL, fileread, btrue ) && ( scantag_count < MAXTAG );
57     if ( retval )
58     {
59         fget_string( fileread, scantag[scantag_count].name, SDL_arraysize( scantag[scantag_count].name ) );
60         scantag[scantag_count].value = fget_int( fileread );
61         scantag_count++;
62     }
63 
64     return retval;
65 }
66 
67 //--------------------------------------------------------------------------------------------
scantag_read_all_vfs(const char * szFilename)68 void scantag_read_all_vfs( const char *szFilename )
69 {
70     /// @details ZZ@> This function reads the scancode.txt file
71     vfs_FILE* fileread;
72     int cnt;
73 
74     scantag_reset();
75 
76     fileread = vfs_openRead( szFilename );
77     if ( NULL == fileread )
78     {
79         log_error( "Cannot read %s.", szFilename );
80     }
81 
82     // read in all the scantags from the file
83     while ( scantag_read_one( fileread ) );
84 
85     vfs_close( fileread );
86 
87     // make extra scantags to support joystick buttons up to 32 bits
88     for ( cnt = 0; cnt < 32; cnt++ )
89     {
90         STRING str_tmp;
91 
92         snprintf( str_tmp, SDL_arraysize( str_tmp ), "JOY_%d", cnt );
93         if ( -1 != scantag_get_value( str_tmp ) ) continue;
94 
95         strncpy( scantag[scantag_count].name, str_tmp, SDL_arraysize( scantag[scantag_count].name ) );
96         scantag[scantag_count].value = 1 << cnt;
97         scantag_count++;
98     }
99 }
100 
101 //--------------------------------------------------------------------------------------------
scantag_get_value(const char * string)102 int scantag_get_value( const char *string )
103 {
104     /// @details ZZ@> This function matches the string with its tag, and returns the value...
105     ///    It will return 255 if there are no matches.
106 
107     int cnt;
108 
109     for ( cnt = 0; cnt < scantag_count; cnt++ )
110     {
111         if ( 0 == strcmp( string, scantag[cnt].name ) )
112         {
113             // They match
114             return scantag[cnt].value;
115         }
116     }
117 
118     // No matches
119     return -1;
120 }
121 
122 //--------------------------------------------------------------------------------------------
scantag_get_string(Sint32 device,Uint32 tag,bool_t is_key)123 const char* scantag_get_string( Sint32 device, Uint32 tag, bool_t is_key )
124 {
125     /// @details ZF@> This translates a input tag value to a string
126 
127     int cnt;
128 
129     if ( device >= INPUT_DEVICE_JOY ) device = INPUT_DEVICE_JOY;
130     if ( device == INPUT_DEVICE_KEYBOARD ) is_key = btrue;
131 
132     for ( cnt = 0; cnt < scantag_count; cnt++ )
133     {
134         // do not search invalid keys
135         if ( is_key )
136         {
137             if ( 'K' != scantag[cnt].name[0] ) continue;
138         }
139         else
140         {
141             switch ( device )
142             {
143                 case INPUT_DEVICE_MOUSE:
144                     if ( 'M' != scantag[cnt].name[0] ) continue;
145                     break;
146 
147                 case INPUT_DEVICE_JOY:
148                     if ( 'J' != scantag[cnt].name[0] ) continue;
149                     break;
150             }
151         };
152         if ( tag == scantag[cnt].value )
153         {
154             return scantag[cnt].name;
155         }
156     }
157 
158     // No matches
159     return "N/A";
160 }
161