| 1 |
/** |
| 2 |
* Furnace Tracker - multi-system chiptune tracker |
| 3 |
* Copyright (C) 2021-2025 tildearrow and contributors |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "util.h" |
| 21 |
#include "gui.h" |
| 22 |
|
| 23 |
#ifdef _WIN32 |
| 24 |
#include <windows.h> |
| 25 |
#include <shlobj.h> |
| 26 |
#include <shlwapi.h> |
| 27 |
#include "../utfutils.h" |
| 28 |
#else |
| 29 |
#include <unistd.h> |
| 30 |
#include <pwd.h> |
| 31 |
#include <sys/stat.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
String getHomeDir() { |
| 35 |
#ifdef IS_MOBILE |
| 36 |
|
| 37 |
#ifdef ANDROID |
| 38 |
return "/storage/emulated/0/"; |
| 39 |
#else |
| 40 |
return "/"; |
| 41 |
#endif |
| 42 |
|
| 43 |
#else |
| 44 |
String ret; |
| 45 |
char tempDir[4096]; |
| 46 |
|
| 47 |
#ifdef _WIN32 |
| 48 |
wchar_t* up=_wgetenv(L"USERPROFILE"); |
| 49 |
if (up!=NULL) { |
| 50 |
ret=utf16To8(up); |
| 51 |
ret+='\\'; |
| 52 |
} |
| 53 |
#else |
| 54 |
char* home=getenv("HOME"); |
| 55 |
if (home!=NULL) { |
| 56 |
ret=home; |
| 57 |
ret+='/'; |
| 58 |
} else { |
| 59 |
int uid=getuid(); |
| 60 |
struct passwd* entry=getpwuid(uid); |
| 61 |
if (entry!=NULL) { |
| 62 |
if (entry->pw_dir!=NULL) { |
| 63 |
ret=entry->pw_dir; |
| 64 |
ret+='/'; |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
#endif |
| 69 |
|
| 70 |
if (ret=="") { // fallback |
| 71 |
#ifdef _WIN32 |
| 72 |
GetCurrentDirectory(4095,tempDir); |
| 73 |
ret=tempDir; |
| 74 |
ret+='\\'; |
| 75 |
#else |
| 76 |
char* unused=getcwd(tempDir,4095); |
| 77 |
char* unused1=unused; // dang it compiler |
| 78 |
unused=unused1; |
| 79 |
ret=tempDir; |
| 80 |
ret+='/'; |
| 81 |
#endif |
| 82 |
} |
| 83 |
|
| 84 |
return ret; |
| 85 |
#endif |
| 86 |
} |
| 87 |
|
| 88 |
String getKeyName(int key, bool emptyNone) { |
| 89 |
if (key==0) { |
| 90 |
if (emptyNone) { |
| 91 |
return ""; |
| 92 |
} else { |
| 93 |
return _("<nothing>"); |
| 94 |
} |
| 95 |
} |
| 96 |
String ret; |
| 97 |
if (key&FURKMOD_CTRL) ret+=_("Ctrl-"); |
| 98 |
if (key&FURKMOD_META) ret+=META_MODIFIER_NAME; |
| 99 |
if (key&FURKMOD_ALT) ret+=_("Alt-"); |
| 100 |
if (key&FURKMOD_SHIFT) ret+=_("Shift-"); |
| 101 |
if ((key&FURK_MASK)==0xffffff) { |
| 102 |
ret+="..."; |
| 103 |
return ret; |
| 104 |
} |
| 105 |
const char* name=SDL_GetKeyName(key&FURK_MASK); |
| 106 |
if (name==NULL) { |
| 107 |
ret+=_("Unknown"); |
| 108 |
} else if (name[0]==0) { |
| 109 |
ret+=_("Unknown"); |
| 110 |
} else { |
| 111 |
ret+=name; |
| 112 |
} |
| 113 |
return ret; |
| 114 |
} |
| 115 |
|
| 116 |
double sinus(double x) { |
| 117 |
return sin(x); |
| 118 |
} |
| 119 |
double rectSin(double x) { |
| 120 |
return sin(x) > 0 ? sin(x) : 0; |
| 121 |
} |
| 122 |
double absSin(double x) { |
| 123 |
return fabs(sin(x)); |
| 124 |
} |
| 125 |
|
| 126 |
double square(double x) { |
| 127 |
return fmod(x, (2 * M_PI)) >= M_PI ? -1 : 1; |
| 128 |
} |
| 129 |
double rectSquare(double x) { |
| 130 |
return square(x) > 0 ? square(x) : 0; |
| 131 |
} |
| 132 |
|
| 133 |
double quartSin(double x) { |
| 134 |
return absSin(x) * rectSquare(2 * x); |
| 135 |
} |
| 136 |
double squiSin(double x) { |
| 137 |
return sin(x) >= 0 ? sin(2 * x) : 0; |
| 138 |
} |
| 139 |
double squiAbsSin(double x) { |
| 140 |
return fabs(squiSin(x)); |
| 141 |
} |
| 142 |
|
| 143 |
double saw(double x) { |
| 144 |
return atan(tan(x / 2)) / (M_PI / 2); |
| 145 |
} |
| 146 |
double rectSaw(double x) { |
| 147 |
return saw(x) > 0 ? saw(x) : 0; |
| 148 |
} |
| 149 |
double absSaw(double x) { |
| 150 |
return saw(x) < 0 ? saw(x) + 1 : saw(x); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
double cubSaw(double x) { |
| 155 |
return pow(saw(x), 3); |
| 156 |
} |
| 157 |
double rectCubSaw(double x) { |
| 158 |
return pow(rectSaw(x), 3); |
| 159 |
} |
| 160 |
double absCubSaw(double x) { |
| 161 |
return pow(absSaw(x), 3); |
| 162 |
} |
| 163 |
|
| 164 |
double cubSine(double x) { |
| 165 |
return pow(sin(x), 3); |
| 166 |
} |
| 167 |
double rectCubSin(double x) { |
| 168 |
return pow(rectSin(x), 3); |
| 169 |
} |
| 170 |
double absCubSin(double x) { |
| 171 |
return pow(absSin(x), 3); |
| 172 |
} |
| 173 |
double quartCubSin(double x) { |
| 174 |
return pow(quartSin(x), 3); |
| 175 |
} |
| 176 |
double squishCubSin(double x) { |
| 177 |
return pow(squiSin(x), 3); |
| 178 |
} |
| 179 |
double squishAbsCubSin(double x) { |
| 180 |
return pow(squiAbsSin(x), 3); |
| 181 |
} |
| 182 |
|
| 183 |
double triangle(double x) { |
| 184 |
return asin(sin(x)) / (M_PI / 2); |
| 185 |
} |
| 186 |
double rectTri(double x) { |
| 187 |
return triangle(x) > 0 ? triangle(x) : 0; |
| 188 |
} |
| 189 |
double absTri(double x) { |
| 190 |
return fabs(triangle(x)); |
| 191 |
} |
| 192 |
double quartTri(double x) { |
| 193 |
return absTri(x) * rectSquare(2 * x); |
| 194 |
} |
| 195 |
double squiTri(double x) { |
| 196 |
return sin(x) >= 0 ? triangle(2 * x) : 0; |
| 197 |
} |
| 198 |
double absSquiTri(double x) { |
| 199 |
return fabs(squiTri(x)); |
| 200 |
} |
| 201 |
|
| 202 |
double cubTriangle(double x) { |
| 203 |
return pow(triangle(x), 3); |
| 204 |
} |
| 205 |
double cubRectTri(double x) { |
| 206 |
return pow(rectTri(x), 3); |
| 207 |
} |
| 208 |
double cubAbsTri(double x) { |
| 209 |
return pow(absTri(x), 3); |
| 210 |
} |
| 211 |
double cubQuartTri(double x) { |
| 212 |
return pow(quartTri(x), 3); |
| 213 |
} |
| 214 |
double cubSquiTri(double x) { |
| 215 |
return pow(squiTri(x), 3); |
| 216 |
} |
| 217 |
double absCubSquiTri(double x) { |
| 218 |
return fabs(cubSquiTri(x)); |
| 219 |
} |
| 220 |
|
| 221 |
String getMultiKeysName(const int* keys, int keyCount, bool emptyNone) { |
| 222 |
String ret; |
| 223 |
for (int i=0; i<keyCount; i++) { |
| 224 |
if (keys[i]==0) continue; |
| 225 |
if (!ret.empty()) ret+=", "; |
| 226 |
ret+=getKeyName(keys[i]); |
| 227 |
} |
| 228 |
|
| 229 |
if (ret.empty()) { |
| 230 |
if (emptyNone) { |
| 231 |
return ""; |
| 232 |
} else { |
| 233 |
return _("<nothing>"); |
| 234 |
} |
| 235 |
} else { |
| 236 |
return ret; |
| 237 |
} |
| 238 |
} |