├── .gitignore ├── icon.png ├── include ├── http.h ├── common.h ├── draw.h ├── json.h ├── util.h ├── error.h ├── linkedlist.h └── screen.h ├── res ├── audio.wav ├── banner icon.png └── Forecast.rsf ├── romfs ├── sun.png ├── bot_bg.png ├── cloud.png ├── menubar.png ├── top_bg.png ├── question.png ├── menu_overlay.png ├── sun_with_cloud.png ├── sun_with_rain.png ├── cloud_with_rain.png ├── cloud_with_snow.png ├── cloud_with_lightning.png ├── cloud_with_some_rain.png ├── cloud_with_lots_of_rain.png └── config.json ├── source ├── base │ ├── stb_image.c │ ├── default.v.pica │ ├── linkedlist.c │ ├── util.c │ └── screen.c ├── main.c ├── http.c ├── draw.c └── json.c ├── README.md ├── Makefile └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | /mockups 2 | -------------------------------------------------------------------------------- /icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NatTupper/Forecast/HEAD/icon.png -------------------------------------------------------------------------------- /include/http.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | char* http_download(const char *url); 4 | -------------------------------------------------------------------------------- /res/audio.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NatTupper/Forecast/HEAD/res/audio.wav -------------------------------------------------------------------------------- /romfs/sun.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NatTupper/Forecast/HEAD/romfs/sun.png -------------------------------------------------------------------------------- /source/base/stb_image.c: -------------------------------------------------------------------------------- 1 | #define STB_IMAGE_IMPLEMENTATION 2 | #include "stb_image.h" -------------------------------------------------------------------------------- /romfs/bot_bg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NatTupper/Forecast/HEAD/romfs/bot_bg.png -------------------------------------------------------------------------------- /romfs/cloud.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NatTupper/Forecast/HEAD/romfs/cloud.png -------------------------------------------------------------------------------- /romfs/menubar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NatTupper/Forecast/HEAD/romfs/menubar.png -------------------------------------------------------------------------------- /romfs/top_bg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NatTupper/Forecast/HEAD/romfs/top_bg.png -------------------------------------------------------------------------------- /res/banner icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NatTupper/Forecast/HEAD/res/banner icon.png -------------------------------------------------------------------------------- /romfs/question.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NatTupper/Forecast/HEAD/romfs/question.png -------------------------------------------------------------------------------- /romfs/menu_overlay.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NatTupper/Forecast/HEAD/romfs/menu_overlay.png -------------------------------------------------------------------------------- /romfs/sun_with_cloud.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NatTupper/Forecast/HEAD/romfs/sun_with_cloud.png -------------------------------------------------------------------------------- /romfs/sun_with_rain.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NatTupper/Forecast/HEAD/romfs/sun_with_rain.png -------------------------------------------------------------------------------- /romfs/cloud_with_rain.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NatTupper/Forecast/HEAD/romfs/cloud_with_rain.png -------------------------------------------------------------------------------- /romfs/cloud_with_snow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NatTupper/Forecast/HEAD/romfs/cloud_with_snow.png -------------------------------------------------------------------------------- /romfs/cloud_with_lightning.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NatTupper/Forecast/HEAD/romfs/cloud_with_lightning.png -------------------------------------------------------------------------------- /romfs/cloud_with_some_rain.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NatTupper/Forecast/HEAD/romfs/cloud_with_some_rain.png -------------------------------------------------------------------------------- /romfs/cloud_with_lots_of_rain.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NatTupper/Forecast/HEAD/romfs/cloud_with_lots_of_rain.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Forecast 2 | ### A weather app for the 3DS 3 | 4 | Credit to Steveice10 for making a lot the functionality in FBI that made this app possible. 5 | -------------------------------------------------------------------------------- /include/common.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define COLOR_WHITE 0xFFFFFFFF 4 | #define COLOR_BLACK 0xFFFFFFFF 5 | 6 | #define FONTSIZE_SMALL 0.5 7 | #define FONTSIZE_MEDIUM 0.75 8 | #define FONTSIZE_LARGE 0.9 9 | -------------------------------------------------------------------------------- /include/draw.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "json.h" 4 | 5 | void draw_top_frame(); 6 | void draw_bottom_frame(); 7 | 8 | void draw_weather_top(weather_t* weather); 9 | void draw_weather_bottom(weather_t* weather, conf_t conf, bool drawMenu); 10 | void draw_icon(int id); 11 | -------------------------------------------------------------------------------- /romfs/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "api_key": "4121cc6770416a4535826cab7e35fb29", 3 | "units": "imperial", 4 | "places":[ 5 | { 6 | "name":"(New place)", 7 | "zipcode": -1, 8 | "lat": -1.0, 9 | "lon": -1.0 10 | }, 11 | { 12 | "name":"(New place)", 13 | "zipcode": -1, 14 | "lat": -1.0, 15 | "lon": -1.0 16 | }, 17 | { 18 | "name":"(New place)", 19 | "zipcode": -1, 20 | "lat": -1.0, 21 | "lon": -1.0 22 | } 23 | ] 24 | } 25 | -------------------------------------------------------------------------------- /include/json.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | typedef struct{ 6 | const char* name; 7 | int zipcode; 8 | double lat; 9 | double lon; 10 | } place_t; 11 | 12 | typedef struct{ 13 | char* api_key; 14 | char* units; 15 | place_t* places; 16 | } conf_t; 17 | 18 | typedef struct{ 19 | int id; 20 | char* desc; 21 | double temp; 22 | int humidity; 23 | double wind_speed; 24 | char* name; 25 | char* country; 26 | int units; 27 | } weather_t; 28 | 29 | double* get_geocoords(); 30 | weather_t* get_weather(conf_t conf, int index); 31 | conf_t get_config(); 32 | void set_config(conf_t conf); 33 | 34 | place_t set_place(place_t place); 35 | -------------------------------------------------------------------------------- /include/util.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define APP_NAME "Pocket-NLSE" 4 | 5 | typedef struct linked_list_s linked_list; 6 | typedef enum {STREAM_STDOUT, STREAM_FILE} stream_t; 7 | 8 | void util_panic(const char* s, ...); 9 | 10 | FS_Path* util_make_path_utf8(const char* path); 11 | void util_free_path_utf8(FS_Path* path); 12 | 13 | FS_Path util_make_binary_path(const void* data, u32 size); 14 | 15 | Result util_open_archive(FS_Archive* archive, FS_ArchiveID id, FS_Path path); 16 | Result util_ref_archive(FS_Archive archive); 17 | Result util_close_archive(FS_Archive archive); 18 | 19 | void util_escape_file_name(char* out, const char* in, size_t size); 20 | 21 | void log_reset(); 22 | void log_set_stream(stream_t stream); 23 | void log_output(char* output, ...); 24 | -------------------------------------------------------------------------------- /source/base/default.v.pica: -------------------------------------------------------------------------------- 1 | ; Uniforms 2 | .fvec projection[4] 3 | 4 | ; Constants 5 | .constf myconst(0.0, 1.0, -1.0, 0.1) 6 | .constf myconst2(0.3, 0.0, 0.0, 0.0) 7 | .alias zeros myconst.xxxx ; Vector full of zeros 8 | .alias ones myconst.yyyy ; Vector full of ones 9 | 10 | ; Outputs 11 | .out outpos position 12 | .out outtc0 texcoord0 13 | 14 | ; Inputs (defined as aliases for convenience) 15 | .alias inpos v0 16 | .alias intex v1 17 | 18 | .bool test 19 | 20 | .proc main 21 | ; Force the w component of inpos to be 1.0 22 | mov r0.xyz, inpos 23 | mov r0.w, ones 24 | 25 | ; outpos = projectionMatrix * inpos 26 | dp4 outpos.x, projection[0], r0 27 | dp4 outpos.y, projection[1], r0 28 | dp4 outpos.z, projection[2], r0 29 | dp4 outpos.w, projection[3], r0 30 | 31 | mov outtc0, intex 32 | 33 | end 34 | .end -------------------------------------------------------------------------------- /include/error.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define R_CANCELLED MAKERESULT(RL_PERMANENT, RS_CANCELED, RM_APPLICATION, 1) 4 | #define R_ERRNO MAKERESULT(RL_PERMANENT, RS_INTERNAL, RM_APPLICATION, 2) 5 | #define R_HTTP_RESPONSE_CODE MAKERESULT(RL_PERMANENT, RS_INTERNAL, RM_APPLICATION, 3) 6 | #define R_WRONG_SYSTEM MAKERESULT(RL_PERMANENT, RS_NOTSUPPORTED, RM_APPLICATION, 4) 7 | #define R_INVALID_ARGUMENT MAKERESULT(RL_PERMANENT, RS_INVALIDARG, RM_APPLICATION, 5) 8 | #define R_THREAD_CREATE_FAILED MAKERESULT(RL_PERMANENT, RS_INTERNAL, RM_APPLICATION, 6) 9 | #define R_PARSE_FAILED MAKERESULT(RL_PERMANENT, RS_INTERNAL, RM_APPLICATION, 7) 10 | #define R_BAD_DATA MAKERESULT(RL_PERMANENT, RS_INTERNAL, RM_APPLICATION, 8) 11 | 12 | #define R_NOT_IMPLEMENTED MAKERESULT(RL_PERMANENT, RS_INTERNAL, RM_APPLICATION, RD_NOT_IMPLEMENTED) 13 | #define R_OUT_OF_MEMORY MAKERESULT(RL_FATAL, RS_OUTOFRESOURCE, RM_APPLICATION, RD_OUT_OF_MEMORY) 14 | #define R_OUT_OF_RANGE MAKERESULT(RL_PERMANENT, RS_INVALIDARG, RM_APPLICATION, RD_OUT_OF_RANGE) 15 | -------------------------------------------------------------------------------- /include/linkedlist.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | typedef struct linked_list_node_s { 6 | struct linked_list_node_s* prev; 7 | struct linked_list_node_s* next; 8 | void* value; 9 | } linked_list_node; 10 | 11 | typedef struct linked_list_s { 12 | linked_list_node* first; 13 | linked_list_node* last; 14 | unsigned int size; 15 | } linked_list; 16 | 17 | typedef struct linked_list_iter_s { 18 | linked_list* list; 19 | linked_list_node* curr; 20 | linked_list_node* next; 21 | } linked_list_iter; 22 | 23 | void linked_list_init(linked_list* list); 24 | void linked_list_destroy(linked_list* list); 25 | 26 | unsigned int linked_list_size(linked_list* list); 27 | void linked_list_clear(linked_list* list); 28 | bool linked_list_contains(linked_list* list, void* value); 29 | void* linked_list_get(linked_list* list, unsigned int index); 30 | bool linked_list_add(linked_list* list, void* value); 31 | bool linked_list_add_at(linked_list* list, unsigned int index, void* value); 32 | bool linked_list_remove(linked_list* list, void* value); 33 | bool linked_list_remove_at(linked_list* list, unsigned int index); 34 | void linked_list_sort(linked_list* list, int (*compare)(const void** p1, const void** p2)); 35 | 36 | void linked_list_iterate(linked_list* list, linked_list_iter* iter); 37 | 38 | void linked_list_iter_restart(linked_list_iter* iter); 39 | bool linked_list_iter_has_next(linked_list_iter* iter); 40 | void* linked_list_iter_next(linked_list_iter* iter); 41 | void linked_list_iter_remove(linked_list_iter* iter); -------------------------------------------------------------------------------- /include/screen.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #define TOP_SCREEN_WIDTH 400 4 | #define TOP_SCREEN_HEIGHT 240 5 | 6 | #define BOTTOM_SCREEN_WIDTH 320 7 | #define BOTTOM_SCREEN_HEIGHT 240 8 | 9 | #define MAX_TEXTURES 1024 10 | 11 | #define TEXTURE_TOP_SCREEN_BG 1 12 | #define TEXTURE_BOTTOM_SCREEN_BG 2 13 | #define TEXTURE_CLOUD 3 14 | #define TEXTURE_CLOUD_WITH_LIGHTNING 4 15 | #define TEXTURE_CLOUD_WITH_LOTS_OF_RAIN 5 16 | #define TEXTURE_CLOUD_WITH_RAIN 6 17 | #define TEXTURE_CLOUD_WITH_SNOW 7 18 | #define TEXTURE_CLOUD_WITH_SOME_RAIN 8 19 | #define TEXTURE_SUN 9 20 | #define TEXTURE_SUN_WITH_CLOUD 10 21 | #define TEXTURE_SUN_WITH_RAIN 11 22 | #define TEXTURE_QUESTION 12 23 | #define TEXTURE_MENUBAR 13 24 | #define TEXTURE_MENU_OVERLAY 14 25 | 26 | #define TEXTURE_AUTO_START 32 27 | 28 | #define NUM_COLORS 11 29 | 30 | #define COLOR_TEXT 0 31 | #define COLOR_NAND 1 32 | #define COLOR_SD 2 33 | #define COLOR_GAME_CARD 3 34 | #define COLOR_DS_TITLE 4 35 | #define COLOR_FILE 5 36 | #define COLOR_DIRECTORY 6 37 | #define COLOR_ENABLED 7 38 | #define COLOR_DISABLED 8 39 | #define COLOR_INSTALLED 9 40 | #define COLOR_NOT_INSTALLED 10 41 | 42 | void screen_init(); 43 | void screen_exit(); 44 | void screen_load_texture(u32 id, void* data, u32 size, u32 width, u32 height, GPU_TEXCOLOR format, bool linearFilter); 45 | u32 screen_load_texture_auto(void* data, u32 size, u32 width, u32 height, GPU_TEXCOLOR format, bool linearFilter); 46 | void screen_load_texture_file(u32 id, const char* path, bool linearFilter); 47 | u32 screen_load_texture_file_auto(const char* path, bool linearFilter); 48 | void screen_load_texture_tiled(u32 id, void* tiledData, u32 size, u32 width, u32 height, GPU_TEXCOLOR format, bool linearFilter); 49 | u32 screen_load_texture_tiled_auto(void* tiledData, u32 size, u32 width, u32 height, GPU_TEXCOLOR format, bool linearFilter); 50 | void screen_unload_texture(u32 id); 51 | void screen_get_texture_size(u32* width, u32* height, u32 id); 52 | void screen_begin_frame(); 53 | void screen_end_frame(); 54 | void screen_select(gfxScreen_t screen); 55 | void screen_set_scissor(bool enabled, u32 x, u32 y, u32 width, u32 height); 56 | void screen_draw_texture(u32 id, float x, float y, float width, float height); 57 | void screen_draw_texture_crop(u32 id, float x, float y, float width, float height); 58 | void screen_get_string_size(float* width, float* height, const char* text, float scaleX, float scaleY); 59 | void screen_get_string_size_wrap(float* width, float* height, const char* text, float scaleX, float scaleY, float wrapX); 60 | void screen_draw_string(const char* text, float x, float y, float scaleX, float scaleY, u32 colorId, bool centerLines); 61 | void screen_draw_string_wrap(const char* text, float x, float y, float scaleX, float scaleY, u32 colorId, bool centerLines, float wrapX); 62 | -------------------------------------------------------------------------------- /source/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include <3ds.h> 7 | #include 8 | 9 | #include "common.h" 10 | #include "util.h" 11 | #include "screen.h" 12 | #include "draw.h" 13 | #include "json.h" 14 | 15 | void cleanup(){ 16 | httpcExit(); 17 | screen_exit(); 18 | romfsExit(); 19 | gfxExit(); 20 | } 21 | 22 | void init(){ 23 | log_reset(); 24 | //log_set_stream(STREAM_STDOUT); 25 | log_set_stream(STREAM_FILE); 26 | gfxInitDefault(); 27 | romfsInit(); 28 | screen_init(); 29 | httpcInit(0); // Buffer size when POST/PUT. 30 | } 31 | 32 | int main(){ 33 | init(); 34 | weather_t* weather = NULL; 35 | conf_t conf; 36 | bool menuExpanded = false; 37 | 38 | screen_begin_frame(); 39 | screen_select(GFX_TOP); 40 | draw_top_frame(); 41 | screen_select(GFX_BOTTOM); 42 | draw_bottom_frame(); 43 | 44 | conf = get_config(); 45 | 46 | weather = get_weather(conf, 0); 47 | if(weather == NULL){ 48 | log_output("weather was NULL\n"); 49 | cleanup(); 50 | return 0; 51 | } 52 | screen_end_frame(); 53 | 54 | // Main loop 55 | while (aptMainLoop()){ 56 | u32 kDown; 57 | 58 | screen_begin_frame(); 59 | 60 | screen_select(GFX_TOP); 61 | draw_weather_top(weather); 62 | screen_select(GFX_BOTTOM); 63 | draw_weather_bottom(weather, conf, menuExpanded); 64 | 65 | hidScanInput(); 66 | 67 | kDown = hidKeysDown(); 68 | if(kDown & KEY_START){ 69 | screen_end_frame(); 70 | break; 71 | } 72 | if(kDown & KEY_TOUCH){ 73 | touchPosition pos; 74 | hidTouchRead(&pos); 75 | 76 | if(pos.py >= 192 && pos.py < BOTTOM_SCREEN_HEIGHT){ 77 | weather->units = (pos.px >= 0 && pos.px < BOTTOM_SCREEN_WIDTH/2)?0:1; 78 | conf.units = (weather->units == 0)?"imperial":"metric"; 79 | } 80 | if(menuExpanded == false && 81 | pos.px >= 0 && pos.px < 39 && pos.py >= 0 && pos.py < 29){ 82 | menuExpanded = true; 83 | } 84 | else if(menuExpanded == true){ 85 | if(pos.px >= 0 && pos.px < 39 && pos.py >= 0 && pos.py < 29){ 86 | menuExpanded = false; 87 | } 88 | else if(pos.px >= 0 && pos.px < TOP_SCREEN_WIDTH/2){ 89 | if(pos.py >= 34+17 && pos.py < 34+17*2){ 90 | if(strcmp(conf.places[0].name, "(New place)") == 0){ 91 | conf.places[0] = set_place(conf.places[0]); 92 | } 93 | free(weather); 94 | weather = get_weather(conf, 0); 95 | } 96 | else if(pos.py >= 34+17*2 && pos.py < 34+17*3){ 97 | if(strcmp(conf.places[1].name, "(New place)") == 0){ 98 | conf.places[1] = set_place(conf.places[1]); 99 | } 100 | free(weather); 101 | weather = get_weather(conf, 1); 102 | } 103 | else if(pos.py >= 34+17*3 && pos.py < 34+17*4){ 104 | if(strcmp(conf.places[2].name, "(New place)") == 0){ 105 | conf.places[2] = set_place(conf.places[2]); 106 | } 107 | free(weather); 108 | weather = get_weather(conf, 2); 109 | } 110 | else if(pos.py >= BOTTOM_SCREEN_HEIGHT/2+17 && 111 | pos.py < BOTTOM_SCREEN_HEIGHT/2+17*2){ 112 | weather->units = 0; 113 | conf.units = "imperial"; 114 | } 115 | else if(pos.py >= BOTTOM_SCREEN_HEIGHT/2+17*2 && 116 | pos.py < BOTTOM_SCREEN_HEIGHT/2+17*3){ 117 | weather->units = 1; 118 | conf.units = "metric"; 119 | } 120 | } 121 | } 122 | } 123 | 124 | screen_end_frame(); 125 | } 126 | 127 | set_config(conf); 128 | // Exit services 129 | cleanup(); 130 | return 0; 131 | } 132 | -------------------------------------------------------------------------------- /source/http.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include <3ds.h> 7 | 8 | #include "util.h" 9 | #include "http.h" 10 | 11 | char* http_download(const char *url) 12 | { 13 | Result ret=0; 14 | httpcContext context; 15 | char *newurl=NULL; 16 | u32 statuscode=0; 17 | u32 contentsize=0, readsize=0, size=0; 18 | u8 *buf, *lastbuf; 19 | 20 | log_output("Downloading %s\n",url); 21 | 22 | do { 23 | ret = httpcOpenContext(&context, HTTPC_METHOD_GET, url, 1); 24 | //log_output("return from httpcOpenContext: %"PRId32"\n",ret); 25 | 26 | // This disables SSL cert verification, so https:// will be usable 27 | ret = httpcSetSSLOpt(&context, SSLCOPT_DisableVerify); 28 | //log_output("return from httpcSetSSLOpt: %"PRId32"\n",ret); 29 | 30 | // Enable Keep-Alive connections (on by default, pending ctrulib merge) 31 | // ret = httpcSetKeepAlive(&context, HTTPC_KEEPALIVE_ENABLED); 32 | // log_output("return from httpcSetKeepAlive: %"PRId32"\n",ret); 33 | 34 | // Set a User-Agent header so websites can identify your application 35 | ret = httpcAddRequestHeaderField(&context, "User-Agent", "httpc-example/1.0.0"); 36 | //log_output("return from httpcAddRequestHeaderField: %"PRId32"\n",ret); 37 | 38 | // Tell the server we can support Keep-Alive connections. 39 | // This will delay connection teardown momentarily (typically 5s) 40 | // in case there is another request made to the same server. 41 | ret = httpcAddRequestHeaderField(&context, "Connection", "Keep-Alive"); 42 | //log_output("return from httpcAddRequestHeaderField: %"PRId32"\n",ret); 43 | 44 | ret = httpcBeginRequest(&context); 45 | if(ret!=0){ 46 | httpcCloseContext(&context); 47 | if(newurl!=NULL) free(newurl); 48 | return NULL; 49 | } 50 | 51 | ret = httpcGetResponseStatusCode(&context, &statuscode); 52 | if(ret!=0){ 53 | httpcCloseContext(&context); 54 | if(newurl!=NULL) free(newurl); 55 | return NULL; 56 | } 57 | 58 | if ((statuscode >= 301 && statuscode <= 303) || (statuscode >= 307 && statuscode <= 308)) { 59 | if(newurl==NULL) newurl = malloc(0x1000); // One 4K page for new URL 60 | if (newurl==NULL){ 61 | httpcCloseContext(&context); 62 | return NULL; 63 | } 64 | ret = httpcGetResponseHeader(&context, "Location", newurl, 0x1000); 65 | url = newurl; // Change pointer to the url that we just learned 66 | //log_output("redirecting to url: %s\n",url); 67 | httpcCloseContext(&context); // Close this context before we try the next 68 | } 69 | } while ((statuscode >= 301 && statuscode <= 303) || (statuscode >= 307 && statuscode <= 308)); 70 | 71 | if(statuscode!=200){ 72 | //log_output("URL returned status: %"PRId32"\n", statuscode); 73 | httpcCloseContext(&context); 74 | if(newurl!=NULL) free(newurl); 75 | return NULL; 76 | } 77 | 78 | // This relies on an optional Content-Length header and may be 0 79 | ret=httpcGetDownloadSizeState(&context, NULL, &contentsize); 80 | if(ret!=0){ 81 | httpcCloseContext(&context); 82 | if(newurl!=NULL) free(newurl); 83 | return NULL; 84 | } 85 | 86 | //log_output("reported size: %"PRId32"\n",contentsize); 87 | 88 | // Start with a single page buffer 89 | buf = (u8*)malloc(0x1000); 90 | if(buf==NULL){ 91 | httpcCloseContext(&context); 92 | if(newurl!=NULL) free(newurl); 93 | return NULL; 94 | } 95 | 96 | do { 97 | // This download loop resizes the buffer as data is read. 98 | ret = httpcDownloadData(&context, buf+size, 0x1000, &readsize); 99 | size += readsize; 100 | if (ret == (s32)HTTPC_RESULTCODE_DOWNLOADPENDING){ 101 | lastbuf = buf; // Save the old pointer, in case realloc() fails. 102 | buf = realloc(buf, size + 0x1000); 103 | if(buf==NULL){ 104 | httpcCloseContext(&context); 105 | free(lastbuf); 106 | if(newurl!=NULL) free(newurl); 107 | return NULL; 108 | } 109 | } 110 | } while (ret == (s32)HTTPC_RESULTCODE_DOWNLOADPENDING); 111 | 112 | if(ret!=0){ 113 | httpcCloseContext(&context); 114 | if(newurl!=NULL) free(newurl); 115 | free(buf); 116 | return NULL; 117 | } 118 | 119 | // Resize the buffer back down to our actual final size 120 | lastbuf = buf; 121 | buf = realloc(buf, size+1); 122 | if(buf==NULL){ // realloc() failed. 123 | httpcCloseContext(&context); 124 | free(lastbuf); 125 | if(newurl!=NULL) free(newurl); 126 | return NULL; 127 | } 128 | buf[size] = '\0'; 129 | 130 | //log_output("downloaded size: %"PRId32"\n",size); 131 | 132 | httpcCloseContext(&context); 133 | if (newurl!=NULL) free(newurl); 134 | 135 | return (char*)buf; 136 | } 137 | -------------------------------------------------------------------------------- /source/draw.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include <3ds.h> 7 | 8 | #include "common.h" 9 | #include "screen.h" 10 | #include "draw.h" 11 | 12 | static char* get_time(){ 13 | time_t unixTime = time(NULL); 14 | struct tm* timeStruct = gmtime((const time_t*)&unixTime); 15 | char* buf = calloc(10, 1); 16 | 17 | sprintf(buf, "%02d:%02d", timeStruct->tm_hour, timeStruct->tm_min); 18 | 19 | return buf; 20 | } 21 | 22 | void draw_top_frame(){ 23 | screen_draw_texture(TEXTURE_TOP_SCREEN_BG, 0, 0, 400, 240); 24 | } 25 | 26 | void draw_bottom_frame(){ 27 | float width; 28 | char* curTime; 29 | 30 | screen_draw_texture(TEXTURE_BOTTOM_SCREEN_BG, 0, 0, 320, 240); 31 | screen_draw_texture(TEXTURE_MENUBAR, 0, 0, 320, 29); 32 | curTime = get_time(); 33 | screen_get_string_size(&width, NULL, curTime, 0.75f, 0.75f); 34 | screen_draw_string(curTime, BOTTOM_SCREEN_WIDTH-width-4, 4, 0.75f, 0.75f, COLOR_WHITE, false); 35 | screen_get_string_size(&width, NULL, "Press START to exit.", 0.75f, 0.75f); 36 | screen_draw_string("Press START to exit.", BOTTOM_SCREEN_WIDTH/2-width/2, 210, 0.75f, 0.75f, COLOR_WHITE, false); 37 | } 38 | 39 | void draw_weather_top(weather_t* weather){ 40 | int units = weather->units; 41 | int id = weather->id; 42 | char* desc = weather->desc; 43 | double temp = (units == 0)?(1.8*(weather->temp - 273) + 32):(weather->temp - 273); 44 | char* name = weather->name; 45 | char* country = weather->country; 46 | 47 | char* tempString = calloc(10, 1); 48 | char* locationString = calloc(strlen(name)+2+strlen(country)+1, 1); 49 | float locationWidth; 50 | float locationXCoord; 51 | 52 | if(units == 0){ 53 | snprintf(tempString, 10, "%.02lf°F", temp); 54 | } 55 | else{ 56 | snprintf(tempString, 10, "%.02lf°C", temp); 57 | } 58 | snprintf(locationString, strlen(name)+2+strlen(country)+1, "%s, %s", name, country); 59 | screen_get_string_size(&locationWidth, NULL, locationString, 1.0f, 1.0f); 60 | locationXCoord = TOP_SCREEN_WIDTH/2 - locationWidth/2; 61 | 62 | draw_top_frame(); 63 | draw_icon(id); 64 | float tempWidth; 65 | float descWidth; 66 | screen_get_string_size(&tempWidth, NULL, tempString, 1.0f, 1.0f); 67 | screen_draw_string(tempString, 230, 85, 1.0f, 1.0f, COLOR_WHITE, false); 68 | screen_get_string_size(&descWidth, NULL, desc, 0.75, 0.75); 69 | screen_draw_string(desc, 230+tempWidth/2-descWidth/2, 111, 0.75f, 0.75f, COLOR_WHITE, false); 70 | screen_draw_string(locationString, locationXCoord, 177, 1.0f, 1.0f, COLOR_WHITE, false); 71 | } 72 | 73 | void draw_weather_bottom(weather_t* weather, conf_t conf, bool drawMenu){ 74 | int humidity = weather->humidity; 75 | int units = weather->units; 76 | double wind_speed = (units == 0)?(weather->wind_speed / 0.44704):(weather->wind_speed); 77 | char* places[3] = { 78 | (char*)conf.places[0].name, 79 | (char*)conf.places[1].name, 80 | (char*)conf.places[2].name 81 | }; 82 | 83 | char* humidityString = calloc(20, 1); 84 | char* windSpeedString = calloc(25, 1); 85 | 86 | if(units == 0){ 87 | snprintf(windSpeedString, 25, "Wind speed: %.02lf mph", wind_speed); 88 | } 89 | else{ 90 | snprintf(windSpeedString, 25, "Wind speed: %.02lf m/s", wind_speed); 91 | } 92 | snprintf(humidityString, 20, "Humidity: %d%%", humidity); 93 | 94 | draw_bottom_frame(); 95 | float width; 96 | float height; 97 | screen_get_string_size(&width, &height, humidityString, 0.75f, 0.75f); 98 | screen_draw_string(humidityString, BOTTOM_SCREEN_WIDTH/2-width/2, BOTTOM_SCREEN_HEIGHT/2-height-1, 0.75f, 0.75f, COLOR_WHITE, false); 99 | screen_get_string_size(&width, &height, windSpeedString, 0.75f, 0.75f); 100 | screen_draw_string(windSpeedString, BOTTOM_SCREEN_WIDTH/2-width/2, BOTTOM_SCREEN_HEIGHT/2+1, 0.75f, 0.75f, COLOR_WHITE, false); 101 | 102 | if(drawMenu == true){ 103 | screen_draw_texture(TEXTURE_MENU_OVERLAY, 0, 29, 320, 211); 104 | screen_draw_string("Places:", 4, 34, 0.6f, 0.6f, COLOR_WHITE, false); 105 | int i; 106 | for(i = 0; i < 3; i++){ 107 | screen_draw_string(places[i], 8, 34+17*(i+1), 0.6f, 0.6f, COLOR_WHITE, false); 108 | } 109 | screen_draw_string("Units", 4, BOTTOM_SCREEN_HEIGHT/2, 0.6f, 0.6f, COLOR_WHITE, false); 110 | screen_draw_string("Imperial", 8, BOTTOM_SCREEN_HEIGHT/2+17, 0.6f, 0.6f, COLOR_WHITE, false); 111 | screen_draw_string("Metric", 8, BOTTOM_SCREEN_HEIGHT/2+17*2, 0.6f, 0.6f, COLOR_WHITE, false); 112 | } 113 | } 114 | 115 | void draw_icon(int id){ 116 | if(id >= 200 && id <= 232) 117 | screen_draw_texture(TEXTURE_CLOUD_WITH_LIGHTNING, 30, 35, 148, 140); 118 | else if(id >= 300 && id <= 321) 119 | screen_draw_texture(TEXTURE_CLOUD_WITH_SOME_RAIN, 30, 35, 148, 156); 120 | else if(id >= 500 && id <= 501) 121 | screen_draw_texture(TEXTURE_CLOUD_WITH_RAIN, 30, 35, 148, 146); 122 | else if(id >= 502 && id <= 531) 123 | screen_draw_texture(TEXTURE_CLOUD_WITH_LOTS_OF_RAIN, 30, 35, 148, 154); 124 | else if(id >= 600 && id <= 622) 125 | screen_draw_texture(TEXTURE_CLOUD_WITH_SNOW, 30, 35, 148, 165); 126 | else if(id == 800) 127 | screen_draw_texture(TEXTURE_SUN, 30, 35, 143, 142); 128 | else if(id >= 801 && id <= 804) 129 | screen_draw_texture(TEXTURE_SUN_WITH_CLOUD, 30, 35, 170, 122); 130 | else 131 | screen_draw_texture(TEXTURE_QUESTION, 30, 35, 66, 129); 132 | } 133 | -------------------------------------------------------------------------------- /source/base/linkedlist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "linkedlist.h" 5 | 6 | void linked_list_init(linked_list* list) { 7 | list->first = NULL; 8 | list->last = NULL; 9 | list->size = 0; 10 | } 11 | 12 | void linked_list_destroy(linked_list* list) { 13 | linked_list_clear(list); 14 | } 15 | 16 | unsigned int linked_list_size(linked_list* list) { 17 | return list->size; 18 | } 19 | 20 | void linked_list_clear(linked_list* list) { 21 | linked_list_node* node = list->first; 22 | while(node != NULL) { 23 | linked_list_node* next = node->next; 24 | free(node); 25 | node = next; 26 | } 27 | 28 | list->first = NULL; 29 | list->last = NULL; 30 | list->size = 0; 31 | } 32 | 33 | bool linked_list_contains(linked_list* list, void* value) { 34 | linked_list_node* node = list->first; 35 | while(node != NULL) { 36 | if(node->value == value) { 37 | return true; 38 | } 39 | 40 | node = node->next; 41 | } 42 | 43 | return false; 44 | } 45 | 46 | static linked_list_node* linked_list_get_node(linked_list* list, unsigned int index) { 47 | if(index < 0 || index >= list->size) { 48 | return NULL; 49 | } 50 | 51 | linked_list_node* node = NULL; 52 | 53 | if(index > (list->size - 1) / 2) { 54 | node = list->last; 55 | unsigned int pos = list->size - 1; 56 | while(node != NULL && pos != index) { 57 | node = node->prev; 58 | pos--; 59 | } 60 | } else { 61 | node = list->first; 62 | unsigned int pos = 0; 63 | while(node != NULL && pos != index) { 64 | node = node->next; 65 | pos++; 66 | } 67 | } 68 | 69 | return node; 70 | } 71 | 72 | void* linked_list_get(linked_list* list, unsigned int index) { 73 | linked_list_node* node = linked_list_get_node(list, index); 74 | return node != NULL ? node->value : NULL; 75 | } 76 | 77 | bool linked_list_add(linked_list* list, void* value) { 78 | linked_list_node* node = (linked_list_node*) calloc(1, sizeof(linked_list_node)); 79 | if(node == NULL) { 80 | return false; 81 | } 82 | 83 | node->value = value; 84 | node->next = NULL; 85 | 86 | if(list->first == NULL || list->last == NULL) { 87 | node->prev = NULL; 88 | 89 | list->first = node; 90 | list->last = node; 91 | } else { 92 | node->prev = list->last; 93 | 94 | list->last->next = node; 95 | list->last = node; 96 | } 97 | 98 | list->size++; 99 | return true; 100 | } 101 | 102 | bool linked_list_add_at(linked_list* list, unsigned int index, void* value) { 103 | linked_list_node* node = (linked_list_node*) calloc(1, sizeof(linked_list_node)); 104 | if(node == NULL) { 105 | return false; 106 | } 107 | 108 | node->value = value; 109 | 110 | if(index == 0) { 111 | node->prev = NULL; 112 | node->next = list->first; 113 | 114 | list->first = node; 115 | } else { 116 | linked_list_node* prev = linked_list_get_node(list, index - 1); 117 | if(prev == NULL) { 118 | free(node); 119 | return false; 120 | } 121 | 122 | node->prev = prev; 123 | node->next = prev->next; 124 | 125 | prev->next = node; 126 | } 127 | 128 | if(node->next != NULL) { 129 | node->next->prev = node; 130 | } else { 131 | list->last = node; 132 | } 133 | 134 | list->size++; 135 | return true; 136 | } 137 | 138 | static void linked_list_remove_node(linked_list* list, linked_list_node* node) { 139 | if(node->prev != NULL) { 140 | node->prev->next = node->next; 141 | } 142 | 143 | if(node->next != NULL) { 144 | node->next->prev = node->prev; 145 | } 146 | 147 | if(list->first == node) { 148 | list->first = node->next; 149 | } 150 | 151 | if(list->last == node) { 152 | list->last = node->prev; 153 | } 154 | 155 | list->size--; 156 | 157 | free(node); 158 | } 159 | 160 | bool linked_list_remove(linked_list* list, void* value) { 161 | bool found = false; 162 | 163 | linked_list_node* node = list->first; 164 | while(node != NULL) { 165 | linked_list_node* next = node->next; 166 | 167 | if(node->value == value) { 168 | found = true; 169 | 170 | linked_list_remove_node(list, node); 171 | } 172 | 173 | node = next; 174 | } 175 | 176 | return found; 177 | } 178 | 179 | bool linked_list_remove_at(linked_list* list, unsigned int index) { 180 | linked_list_node* node = linked_list_get_node(list, index); 181 | if(node == NULL) { 182 | return false; 183 | } 184 | 185 | linked_list_remove_node(list, node); 186 | return true; 187 | } 188 | 189 | void linked_list_sort(linked_list* list, int (*compare)(const void** p1, const void** p2)) { 190 | unsigned int count = list->size; 191 | 192 | void** elements = (void**) calloc(count, sizeof(void*)); 193 | if(elements != NULL) { 194 | unsigned int num = 0; 195 | linked_list_node* node = list->first; 196 | while(node != NULL && num < count) { 197 | elements[num++] = node->value; 198 | node = node->next; 199 | } 200 | 201 | linked_list_clear(list); 202 | 203 | qsort(elements, num, sizeof(void*), (int (*)(const void* p1, const void* p2)) compare); 204 | 205 | for(unsigned int i = 0; i < num; i++) { 206 | linked_list_add(list, elements[i]); 207 | } 208 | 209 | free(elements); 210 | } 211 | } 212 | 213 | void linked_list_iterate(linked_list* list, linked_list_iter* iter) { 214 | iter->list = list; 215 | linked_list_iter_restart(iter); 216 | } 217 | 218 | void linked_list_iter_restart(linked_list_iter* iter) { 219 | if(iter->list == NULL) { 220 | return; 221 | } 222 | 223 | iter->curr = NULL; 224 | iter->next = iter->list->first; 225 | } 226 | 227 | bool linked_list_iter_has_next(linked_list_iter* iter) { 228 | return iter->next != NULL; 229 | } 230 | 231 | void* linked_list_iter_next(linked_list_iter* iter) { 232 | if(iter->next == NULL) { 233 | return NULL; 234 | } 235 | 236 | iter->curr = iter->next; 237 | iter->next = iter->next->next; 238 | return iter->curr->value; 239 | } 240 | 241 | void linked_list_iter_remove(linked_list_iter* iter) { 242 | if(iter->curr == NULL) { 243 | return; 244 | } 245 | 246 | linked_list_remove_node(iter->list, iter->curr); 247 | iter->curr = NULL; 248 | } -------------------------------------------------------------------------------- /source/base/util.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include <3ds.h> 8 | 9 | #include "util.h" 10 | #include "error.h" 11 | #include "linkedlist.h" 12 | 13 | static stream_t stream_out; 14 | 15 | extern void cleanup(); 16 | 17 | static int util_get_line_length(PrintConsole* console, const char* str) { 18 | int lineLength = 0; 19 | while(*str != 0) { 20 | if(*str == '\n') { 21 | break; 22 | } 23 | 24 | lineLength++; 25 | if(lineLength >= console->consoleWidth - 1) { 26 | break; 27 | } 28 | 29 | str++; 30 | } 31 | 32 | return lineLength; 33 | } 34 | 35 | static int util_get_lines(PrintConsole* console, const char* str) { 36 | int lines = 1; 37 | int lineLength = 0; 38 | while(*str != 0) { 39 | if(*str == '\n') { 40 | lines++; 41 | lineLength = 0; 42 | } else { 43 | lineLength++; 44 | if(lineLength >= console->consoleWidth - 1) { 45 | lines++; 46 | lineLength = 0; 47 | } 48 | } 49 | 50 | str++; 51 | } 52 | 53 | return lines; 54 | } 55 | 56 | void util_panic(const char* s, ...) { 57 | va_list list; 58 | va_start(list, s); 59 | 60 | char buf[1024]; 61 | vsnprintf(buf, 1024, s, list); 62 | 63 | va_end(list); 64 | 65 | gspWaitForVBlank(); 66 | 67 | u16 width; 68 | u16 height; 69 | for(int i = 0; i < 2; i++) { 70 | memset(gfxGetFramebuffer(GFX_TOP, GFX_LEFT, &width, &height), 0, (size_t) (width * height * 3)); 71 | memset(gfxGetFramebuffer(GFX_TOP, GFX_RIGHT, &width, &height), 0, (size_t) (width * height * 3)); 72 | memset(gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, &width, &height), 0, (size_t) (width * height * 3)); 73 | 74 | gfxSwapBuffers(); 75 | } 76 | 77 | PrintConsole* console = consoleInit(GFX_TOP, NULL); 78 | 79 | const char* header_format = "%s has encountered a fatal error!"; 80 | char* header = calloc(strlen(header_format)-2+strlen(APP_NAME)+1, 1); 81 | sprintf(header, header_format, APP_NAME); 82 | const char* footer = "Press any button to exit."; 83 | 84 | printf("\x1b[0;0H"); 85 | for(int i = 0; i < console->consoleWidth; i++) { 86 | printf("-"); 87 | } 88 | 89 | printf("\x1b[%d;0H", console->consoleHeight - 1); 90 | for(int i = 0; i < console->consoleWidth; i++) { 91 | printf("-"); 92 | } 93 | 94 | printf("\x1b[0;%dH%s", (console->consoleWidth - util_get_line_length(console, header)) / 2, header); 95 | printf("\x1b[%d;%dH%s", console->consoleHeight - 1, (console->consoleWidth - util_get_line_length(console, footer)) / 2, footer); 96 | 97 | int bufRow = (console->consoleHeight - util_get_lines(console, buf)) / 2; 98 | char* str = buf; 99 | while(*str != 0) { 100 | if(*str == '\n') { 101 | bufRow++; 102 | str++; 103 | continue; 104 | } else { 105 | int lineLength = util_get_line_length(console, str); 106 | 107 | char old = *(str + lineLength); 108 | *(str + lineLength) = '\0'; 109 | printf("\x1b[%d;%dH%s", bufRow, (console->consoleWidth - lineLength) / 2, str); 110 | *(str + lineLength) = old; 111 | 112 | bufRow++; 113 | str += lineLength; 114 | } 115 | } 116 | 117 | gfxFlushBuffers(); 118 | gspWaitForVBlank(); 119 | 120 | while(aptMainLoop()) { 121 | hidScanInput(); 122 | if(hidKeysDown() & ~KEY_TOUCH) { 123 | break; 124 | } 125 | 126 | gspWaitForVBlank(); 127 | } 128 | 129 | cleanup(); 130 | exit(1); 131 | } 132 | 133 | FS_Path* util_make_path_utf8(const char* path) { 134 | size_t len = strlen(path); 135 | 136 | u16* utf16 = (u16*) calloc(len + 1, sizeof(u16)); 137 | if(utf16 == NULL) { 138 | return NULL; 139 | } 140 | 141 | ssize_t utf16Len = utf8_to_utf16(utf16, (const uint8_t*) path, len); 142 | 143 | FS_Path* fsPath = (FS_Path*) calloc(1, sizeof(FS_Path)); 144 | if(fsPath == NULL) { 145 | free(utf16); 146 | return NULL; 147 | } 148 | 149 | fsPath->type = PATH_UTF16; 150 | fsPath->size = (utf16Len + 1) * sizeof(u16); 151 | fsPath->data = utf16; 152 | 153 | return fsPath; 154 | } 155 | 156 | void util_free_path_utf8(FS_Path* path) { 157 | free((void*) path->data); 158 | free(path); 159 | } 160 | 161 | FS_Path util_make_binary_path(const void* data, u32 size) { 162 | FS_Path path = {PATH_BINARY, size, data}; 163 | return path; 164 | } 165 | 166 | typedef struct { 167 | FS_Archive archive; 168 | u32 refs; 169 | } archive_ref; 170 | 171 | static linked_list opened_archives; 172 | 173 | Result util_open_archive(FS_Archive* archive, FS_ArchiveID id, FS_Path path) { 174 | if(archive == NULL) { 175 | return R_INVALID_ARGUMENT; 176 | } 177 | 178 | Result res = 0; 179 | 180 | FS_Archive arch = 0; 181 | if(R_SUCCEEDED(res = FSUSER_OpenArchive(&arch, id, path))) { 182 | if(R_SUCCEEDED(res = util_ref_archive(arch))) { 183 | *archive = arch; 184 | } else { 185 | FSUSER_CloseArchive(arch); 186 | } 187 | } 188 | 189 | return res; 190 | } 191 | 192 | Result util_ref_archive(FS_Archive archive) { 193 | linked_list_iter iter; 194 | linked_list_iterate(&opened_archives, &iter); 195 | 196 | while(linked_list_iter_has_next(&iter)) { 197 | archive_ref* ref = (archive_ref*) linked_list_iter_next(&iter); 198 | if(ref->archive == archive) { 199 | ref->refs++; 200 | return 0; 201 | } 202 | } 203 | 204 | Result res = 0; 205 | 206 | archive_ref* ref = (archive_ref*) calloc(1, sizeof(archive_ref)); 207 | if(ref != NULL) { 208 | ref->archive = archive; 209 | ref->refs = 1; 210 | 211 | linked_list_add(&opened_archives, ref); 212 | } else { 213 | res = R_OUT_OF_MEMORY; 214 | } 215 | 216 | return res; 217 | } 218 | 219 | Result util_close_archive(FS_Archive archive) { 220 | linked_list_iter iter; 221 | linked_list_iterate(&opened_archives, &iter); 222 | 223 | while(linked_list_iter_has_next(&iter)) { 224 | archive_ref* ref = (archive_ref*) linked_list_iter_next(&iter); 225 | if(ref->archive == archive) { 226 | ref->refs--; 227 | 228 | if(ref->refs == 0) { 229 | linked_list_iter_remove(&iter); 230 | free(ref); 231 | } else { 232 | return 0; 233 | } 234 | } 235 | } 236 | 237 | return FSUSER_CloseArchive(archive); 238 | } 239 | 240 | void log_reset(){ 241 | FILE* output_file; 242 | struct stat st = {0}; 243 | 244 | if(stat("/weather", &st) == -1){ 245 | mkdir("/weather", 0755); 246 | } 247 | 248 | output_file = fopen("/weather/weather.log", "w"); 249 | 250 | fclose(output_file); 251 | } 252 | 253 | void log_set_stream(stream_t stream){ 254 | stream_out = stream; 255 | } 256 | 257 | void log_output(char* output, ...){ 258 | FILE* output_file; 259 | struct stat st = {0}; 260 | va_list args; 261 | char buffer[2048]; 262 | 263 | if(stat("/weather", &st) == -1){ 264 | mkdir("/weather", 0755); 265 | } 266 | 267 | output_file = fopen("/weather/weather.log", "a"); 268 | 269 | memset(buffer, '\0', 2048); 270 | va_start(args, output); 271 | vsnprintf(buffer, 2048, output, args); 272 | if(stream_out == STREAM_FILE) 273 | fprintf(output_file, buffer); 274 | else 275 | printf(buffer); 276 | va_end(args); 277 | 278 | fclose(output_file); 279 | } 280 | -------------------------------------------------------------------------------- /res/Forecast.rsf: -------------------------------------------------------------------------------- 1 | BasicInfo: 2 | Title : Forecast 3 | ProductCode : CTR-P-FCST 4 | Logo : Homebrew # Nintendo / Licensed / Distributed / iQue / iQueForSystem / Homebrew 5 | 6 | RomFs: 7 | # Specifies the root path of the read only file system to include in the ROM. 8 | RootPath : romfs 9 | 10 | TitleInfo: 11 | Category : Application 12 | UniqueId : 0xE2C77 13 | 14 | Option: 15 | UseOnSD : true # true if App is to be installed to SD 16 | FreeProductCode : true # Removes limitations on ProductCode 17 | MediaFootPadding : false # If true CCI files are created with padding 18 | EnableCrypt : false # Enables encryption for NCCH and CIA 19 | EnableCompress : true # Compresses where applicable (currently only exefs:/.code) 20 | 21 | AccessControlInfo: 22 | CoreVersion : 2 23 | 24 | # Exheader Format Version 25 | DescVersion : 2 26 | 27 | # Minimum Required Kernel Version (below is for 4.5.0) 28 | ReleaseKernelMajor : "02" 29 | ReleaseKernelMinor : "33" 30 | 31 | # ExtData 32 | UseExtSaveData : false # enables ExtData 33 | #ExtSaveDataId : 0x300 # only set this when the ID is different to the UniqueId 34 | 35 | # FS:USER Archive Access Permissions 36 | # Uncomment as required 37 | FileSystemAccess: 38 | #- CategorySystemApplication 39 | #- CategoryHardwareCheck 40 | - CategoryFileSystemTool 41 | #- Debug 42 | #- TwlCardBackup 43 | #- TwlNandData 44 | - Boss 45 | - DirectSdmc 46 | #- Core 47 | #- CtrNandRo 48 | - CtrNandRw 49 | #- CtrNandRoWrite 50 | #- CategorySystemSettings 51 | #- CardBoard 52 | #- ExportImportIvs 53 | #- DirectSdmcWrite 54 | #- SwitchCleanup 55 | - SaveDataMove 56 | #- Shop 57 | #- Shell 58 | #- CategoryHomeMenu 59 | 60 | # Process Settings 61 | MemoryType : Application # Application/System/Base 62 | SystemMode : 64MB # 64MB(Default)/96MB/80MB/72MB/32MB 63 | IdealProcessor : 0 64 | AffinityMask : 1 65 | Priority : 16 66 | MaxCpu : 0 # Let system decide 67 | HandleTableSize : 0x200 68 | DisableDebug : false 69 | EnableForceDebug : false 70 | CanWriteSharedPage : true 71 | CanUsePrivilegedPriority : false 72 | CanUseNonAlphabetAndNumber : true 73 | PermitMainFunctionArgument : true 74 | CanShareDeviceMemory : true 75 | RunnableOnSleep : false 76 | SpecialMemoryArrange : true 77 | 78 | # New3DS Exclusive Process Settings 79 | SystemModeExt : 124MB # Legacy(Default)/124MB/178MB Legacy:Use Old3DS SystemMode 80 | CpuSpeed : 804MHz # 268MHz(Default)/804MHz 81 | EnableL2Cache : true # false(default)/true 82 | CanAccessCore2 : true 83 | 84 | # Virtual Address Mappings 85 | IORegisterMapping: 86 | - 1ff00000-1ff7ffff # DSP memory 87 | MemoryMapping: 88 | - 1f000000-1f5fffff:r # VRAM 89 | 90 | # Accessible SVCs, : 91 | SystemCallAccess: 92 | ArbitrateAddress: 34 93 | Break: 60 94 | CancelTimer: 28 95 | ClearEvent: 25 96 | ClearTimer: 29 97 | CloseHandle: 35 98 | ConnectToPort: 45 99 | ControlMemory: 1 100 | CreateAddressArbiter: 33 101 | CreateEvent: 23 102 | CreateMemoryBlock: 30 103 | CreateMutex: 19 104 | CreateSemaphore: 21 105 | CreateThread: 8 106 | CreateTimer: 26 107 | DuplicateHandle: 39 108 | ExitProcess: 3 109 | ExitThread: 9 110 | GetCurrentProcessorNumber: 17 111 | GetHandleInfo: 41 112 | GetProcessId: 53 113 | GetProcessIdOfThread: 54 114 | GetProcessIdealProcessor: 6 115 | GetProcessInfo: 43 116 | GetResourceLimit: 56 117 | GetResourceLimitCurrentValues: 58 118 | GetResourceLimitLimitValues: 57 119 | GetSystemInfo: 42 120 | GetSystemTick: 40 121 | GetThreadContext: 59 122 | GetThreadId: 55 123 | GetThreadIdealProcessor: 15 124 | GetThreadInfo: 44 125 | GetThreadPriority: 11 126 | MapMemoryBlock: 31 127 | OutputDebugString: 61 128 | QueryMemory: 2 129 | ReleaseMutex: 20 130 | ReleaseSemaphore: 22 131 | SendSyncRequest1: 46 132 | SendSyncRequest2: 47 133 | SendSyncRequest3: 48 134 | SendSyncRequest4: 49 135 | SendSyncRequest: 50 136 | SetThreadPriority: 12 137 | SetTimer: 27 138 | SignalEvent: 24 139 | SleepThread: 10 140 | UnmapMemoryBlock: 32 141 | WaitSynchronization1: 36 142 | WaitSynchronizationN: 37 143 | Backdoor: 123 144 | 145 | # Service List 146 | # Maximum 34 services (32 if firmware is prior to 9.3.0) 147 | ServiceAccessControl: 148 | - cfg:u 149 | - fs:USER 150 | - gsp::Gpu 151 | - hid:USER 152 | - ndm:u 153 | - pxi:dev 154 | - APT:U 155 | - ac:u 156 | - act:u 157 | - am:net 158 | - boss:U 159 | - cam:u 160 | - cecd:u 161 | - csnd:SND 162 | - frd:u 163 | - http:C 164 | - ir:USER 165 | - ir:u 166 | - ir:rst 167 | - ldr:ro 168 | - mic:u 169 | - news:u 170 | - nfc:u 171 | - nim:aoc 172 | - nwm::UDS 173 | - ptm:u 174 | - qtm:u 175 | - soc:U 176 | - ssl:C 177 | - y2r:u 178 | 179 | 180 | SystemControlInfo: 181 | SaveDataSize: 0KB 182 | RemasterVersion: $(VERSION) 183 | StackSize: 0x40000 184 | 185 | # Modules that run services listed above should be included below 186 | # Maximum 48 dependencies 187 | # If a module is listed that isn't present on the 3DS, the title will get stuck at the logo (3ds waves) 188 | # So act, nfc and qtm are commented for 4.x support. Uncomment if you need these. 189 | # : 190 | Dependency: 191 | ac: 0x0004013000002402 192 | #act: 0x0004013000003802 193 | am: 0x0004013000001502 194 | boss: 0x0004013000003402 195 | camera: 0x0004013000001602 196 | cecd: 0x0004013000002602 197 | cfg: 0x0004013000001702 198 | codec: 0x0004013000001802 199 | csnd: 0x0004013000002702 200 | dlp: 0x0004013000002802 201 | dsp: 0x0004013000001a02 202 | friends: 0x0004013000003202 203 | gpio: 0x0004013000001b02 204 | gsp: 0x0004013000001c02 205 | hid: 0x0004013000001d02 206 | http: 0x0004013000002902 207 | i2c: 0x0004013000001e02 208 | ir: 0x0004013000003302 209 | mcu: 0x0004013000001f02 210 | mic: 0x0004013000002002 211 | ndm: 0x0004013000002b02 212 | news: 0x0004013000003502 213 | #nfc: 0x0004013000004002 214 | nim: 0x0004013000002c02 215 | nwm: 0x0004013000002d02 216 | pdn: 0x0004013000002102 217 | ps: 0x0004013000003102 218 | ptm: 0x0004013000002202 219 | #qtm: 0x0004013020004202 220 | ro: 0x0004013000003702 221 | socket: 0x0004013000002e02 222 | spi: 0x0004013000002302 223 | ssl: 0x0004013000002f02 224 | -------------------------------------------------------------------------------- /source/json.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include <3ds.h> 6 | #include 7 | 8 | #include "common.h" 9 | #include "util.h" 10 | #include "http.h" 11 | #include "json.h" 12 | #include "draw.h" 13 | #include "screen.h" 14 | 15 | double* get_geocoords(){ 16 | json_t* root; 17 | json_t* child; 18 | char* url = "http://ip-api.com/json"; 19 | char* data; 20 | double* coords = malloc(2*sizeof(double)); 21 | 22 | data = http_download(url); 23 | 24 | root = json_loads(data, 0, NULL); 25 | if(!root){ 26 | log_output("Failed to load json\n"); 27 | log_output("data: %s\n", data); 28 | return NULL; 29 | } 30 | 31 | child = json_object_get(root, "lat"); 32 | if(!child){ 33 | json_decref(root); 34 | log_output("Failed to get object \"lat\"\n"); 35 | return NULL; 36 | } 37 | coords[0] = json_number_value(child); 38 | 39 | child = json_object_get(root, "lon"); 40 | if(!child){ 41 | json_decref(root); 42 | log_output("Failed to get object \"lon\"\n"); 43 | return NULL; 44 | } 45 | coords[1] = json_number_value(child); 46 | 47 | if(coords[0] == 0.0f || coords[1] == 0.0f){ 48 | json_decref(root); 49 | json_decref(child); 50 | log_output("json_number_value_failed\n"); 51 | return NULL; 52 | } 53 | 54 | json_decref(root); 55 | json_decref(child); 56 | return coords; 57 | } 58 | 59 | weather_t* get_weather(conf_t conf, int index){ 60 | weather_t* weather = malloc(sizeof(weather_t)); 61 | char* baseZipUrl = "http://api.openweathermap.org/data/2.5/weather?zip=%05d&appid=%s"; 62 | char* baseCoordsUrl = "http://api.openweathermap.org/data/2.5/weather?lat=%lf&lon=%lf&appid=%s"; 63 | double lat; 64 | double lon; 65 | json_t* root; 66 | json_t* child; 67 | char* url = calloc(256, 1); 68 | 69 | if(conf.places[index].zipcode == -1 && conf.places[index].lat == -1.0){ 70 | double* coords = get_geocoords(); 71 | if(coords == NULL){ 72 | log_output("coords are NULL\n"); 73 | return NULL; 74 | } 75 | lat = coords[0]; 76 | lon = coords[1]; 77 | sprintf(url, baseCoordsUrl, lat, lon, conf.api_key); 78 | } 79 | else if(conf.places[index].zipcode == -1){ 80 | sprintf(url, baseCoordsUrl, conf.places[index].lat, conf.places[index].lon, conf.api_key); 81 | } 82 | else{ 83 | sprintf(url, baseZipUrl, conf.places[index].zipcode, conf.api_key); 84 | } 85 | 86 | if(strcmp("imperial", conf.units) == 0) 87 | weather->units = 0; 88 | else 89 | weather->units = 1; 90 | 91 | float width; 92 | float height; 93 | screen_get_string_size(&width, &height, "Loading...", 1.0f, 1.0f); 94 | screen_draw_string("Loading...", BOTTOM_SCREEN_WIDTH/2-width/2, BOTTOM_SCREEN_HEIGHT/2-height/2, 1.0f, 1.0f, COLOR_WHITE, false); 95 | screen_end_frame(); 96 | char* text = http_download(url); 97 | screen_begin_frame(); 98 | screen_select(GFX_BOTTOM); 99 | screen_draw_string("Loading...", BOTTOM_SCREEN_WIDTH/2-width/2, BOTTOM_SCREEN_HEIGHT/2-height/2, 1.0f, 1.0f, COLOR_WHITE, false); 100 | 101 | root = json_loads(text, 0, NULL); 102 | if(!root){ 103 | log_output("Json failed to load.\n"); 104 | log_output("text: %s\n", text); 105 | return NULL; 106 | } 107 | free(text); 108 | 109 | //find id 110 | child = json_object_get(root, "weather"); 111 | child = json_array_get(child, 0); 112 | child = json_object_get(child, "id"); 113 | if(!json_is_number(child)){ 114 | log_output("id is not a number!\n"); 115 | return NULL; 116 | } 117 | weather->id = (int)json_number_value(child); 118 | 119 | //find desc 120 | child = json_object_get(root, "weather"); 121 | child = json_array_get(child, 0); 122 | child = json_object_get(child, "description"); 123 | if(!json_is_string(child)){ 124 | log_output("description is not a string!\n"); 125 | return NULL; 126 | } 127 | weather->desc = (char*)json_string_value(child); 128 | 129 | //find temp 130 | child = json_object_get(root, "main"); 131 | child = json_object_get(child, "temp"); 132 | if(!json_is_number(child)){ 133 | log_output("temp is not a number!\n"); 134 | return NULL; 135 | } 136 | weather->temp = json_number_value(child); 137 | 138 | //find humidity 139 | child = json_object_get(root, "main"); 140 | child = json_object_get(child, "humidity"); 141 | if(!json_is_number(child)){ 142 | log_output("humidity is not number!\n"); 143 | return NULL; 144 | } 145 | weather->humidity = (int)json_number_value(child); 146 | 147 | //find wind speed 148 | child = json_object_get(root, "wind"); 149 | child = json_object_get(child, "speed"); 150 | if(!json_is_number(child)){ 151 | log_output("speed is not number!\n"); 152 | return NULL; 153 | } 154 | weather->wind_speed = json_number_value(child); 155 | 156 | //find name 157 | child = json_object_get(root, "name"); 158 | if(!json_is_string(child)){ 159 | log_output("name is not a string!\n"); 160 | return NULL; 161 | } 162 | weather->name = (char*)json_string_value(child); 163 | 164 | //find country 165 | child = json_object_get(root, "sys"); 166 | child = json_object_get(child, "country"); 167 | if(!json_is_string(child)){ 168 | log_output("country is not a string!\n"); 169 | return NULL; 170 | } 171 | weather->country = (char*)json_string_value(child); 172 | 173 | return weather; 174 | } 175 | 176 | conf_t get_config(){ 177 | const char* romfsPath = "romfs:/config.json"; 178 | const char* sdmcPath = "/weather/config.json"; 179 | json_t* root; 180 | json_t* child; 181 | json_t* subchild; 182 | conf_t conf; 183 | 184 | if(access(sdmcPath, F_OK) != -1) 185 | root = json_load_file(sdmcPath, 0, NULL); 186 | else 187 | root = json_load_file(romfsPath, 0, NULL); 188 | if(!root){ 189 | log_output("root is NULL\n"); 190 | return conf; 191 | } 192 | 193 | conf.places = malloc(3*sizeof(place_t)); 194 | 195 | child = json_object_get(root, "api_key"); 196 | if(!child){ 197 | log_output("api_key is NULL\n"); 198 | return conf; 199 | } 200 | conf.api_key = (char*)json_string_value(child); 201 | child = json_object_get(root, "units"); 202 | if(!child){ 203 | log_output("units is NULL\n"); 204 | return conf; 205 | } 206 | conf.units = (char*)json_string_value(child); 207 | root = json_object_get(root, "places"); 208 | int i; 209 | for(i = 0; i < 3; i++){ 210 | child = json_array_get(root, i); 211 | 212 | subchild = json_object_get(child, "name"); 213 | conf.places[i].name = json_string_value(subchild); 214 | 215 | subchild = json_object_get(child, "zipcode"); 216 | conf.places[i].zipcode = json_integer_value(subchild); 217 | 218 | subchild = json_object_get(child, "lat"); 219 | conf.places[i].lat = json_real_value(subchild); 220 | 221 | subchild = json_object_get(child, "lon"); 222 | conf.places[i].lon = json_real_value(subchild); 223 | } 224 | 225 | return conf; 226 | } 227 | 228 | void set_config(conf_t conf){ 229 | json_t* root; 230 | json_t* childA; 231 | json_t* childB; 232 | json_t* childC; 233 | 234 | root = json_object(); 235 | 236 | childA = json_string(conf.api_key); 237 | json_object_set(root, "api_key", childA); 238 | 239 | childA = json_string(conf.units); 240 | json_object_set(root, "units", childA); 241 | 242 | childA = json_array(); 243 | int i; 244 | for(i = 0; i < 3; i++){ 245 | childB = json_object(); 246 | json_array_append(childA, childB); 247 | 248 | childC = json_string(conf.places[i].name); 249 | json_object_set(childB, "name", childC); 250 | 251 | childC = json_integer(conf.places[i].zipcode); 252 | json_object_set(childB, "zipcode", childC); 253 | 254 | childC = json_real(conf.places[i].lat); 255 | json_object_set(childB, "lat", childC); 256 | 257 | childC = json_real(conf.places[i].lon); 258 | json_object_set(childB, "lon", childC); 259 | } 260 | 261 | json_object_set(root, "places", childA); 262 | 263 | json_dump_file(root, "/weather/config.json", 0); 264 | } 265 | 266 | place_t set_place(place_t place){ 267 | SwkbdState swkbd; 268 | char* mybuf; 269 | 270 | mybuf = calloc(10, 1); 271 | swkbdInit(&swkbd, SWKBD_TYPE_NUMPAD, 2, 5); 272 | swkbdSetHintText(&swkbd, "Enter zipcode"); 273 | swkbdSetButton(&swkbd, SWKBD_BUTTON_LEFT, "Cancel", false); 274 | swkbdSetButton(&swkbd, SWKBD_BUTTON_RIGHT, "OK", true); 275 | swkbdInputText(&swkbd, mybuf, 10); 276 | if(strlen(mybuf) < 1){ 277 | return place; 278 | } 279 | place.zipcode = atoi(mybuf); 280 | free(mybuf); 281 | 282 | mybuf = calloc(60, 1); 283 | swkbdInit(&swkbd, SWKBD_TYPE_NORMAL, 2, -1); 284 | swkbdSetHintText(&swkbd, "Enter a name for this place"); 285 | swkbdSetButton(&swkbd, SWKBD_BUTTON_LEFT, "Cancel", false); 286 | swkbdSetButton(&swkbd, SWKBD_BUTTON_RIGHT, "OK", true); 287 | swkbdInputText(&swkbd, mybuf, 60); 288 | if(strlen(mybuf) < 1){ 289 | return place; 290 | } 291 | place.name = mybuf; 292 | 293 | return place; 294 | } 295 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | #--------------------------------------------------------------------------------- 2 | .SUFFIXES: 3 | #--------------------------------------------------------------------------------- 4 | 5 | ifeq ($(strip $(DEVKITARM)),) 6 | $(error "Please set DEVKITARM in your environment. export DEVKITARM=devkitARM") 7 | endif 8 | 9 | TOPDIR ?= $(CURDIR) 10 | include $(DEVKITARM)/3ds_rules 11 | 12 | #--------------------------------------------------------------------------------- 13 | # TARGET is the name of the output 14 | # BUILD is the directory where object files & intermediate files will be placed 15 | # SOURCES is a list of directories containing source code 16 | # DATA is a list of directories containing data files 17 | # INCLUDES is a list of directories containing header files 18 | # 19 | # NO_SMDH: if set to anything, no SMDH file is generated. 20 | # ROMFS is the directory which contains the RomFS, relative to the Makefile (Optional) 21 | # APP_TITLE is the name of the app stored in the SMDH file (Optional) 22 | # APP_DESCRIPTION is the description of the app stored in the SMDH file (Optional) 23 | # APP_AUTHOR is the author of the app stored in the SMDH file (Optional) 24 | # ICON is the filename of the icon (.png), relative to the project folder. 25 | # If not set, it attempts to use one of the following (in this order): 26 | # - .png 27 | # - icon.png 28 | # - /default_icon.png 29 | #--------------------------------------------------------------------------------- 30 | TARGET := $(notdir $(CURDIR)) 31 | BUILD := build 32 | SOURCES := source source/base 33 | DATA := data 34 | INCLUDES := include 35 | ROMFS := romfs 36 | 37 | APP_TITLE := Forecast 38 | APP_DESCRIPTION := A weather app for the 3DS 39 | APP_AUTHOR := NatTupper 40 | 41 | #--------------------------------------------------------------------------------- 42 | # options for code generation 43 | #--------------------------------------------------------------------------------- 44 | ARCH := -march=armv6k -mtune=mpcore -mfloat-abi=hard -mtp=soft 45 | 46 | CFLAGS := -g -Wall -O2 -mword-relocations \ 47 | -fomit-frame-pointer -ffunction-sections \ 48 | $(ARCH) 49 | 50 | CFLAGS += $(INCLUDE) -DARM11 -D_3DS -Wno-misleading-indentation 51 | 52 | CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11 53 | 54 | ASFLAGS := -g $(ARCH) 55 | LDFLAGS = -specs=3dsx.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) 56 | 57 | LIBS := -ljansson -lcitro3d -lctru -lm 58 | 59 | #--------------------------------------------------------------------------------- 60 | # list of directories containing libraries, this must be the top level containing 61 | # include and lib 62 | #--------------------------------------------------------------------------------- 63 | LIBDIRS := $(CTRULIB) $(PORTLIBS) 64 | 65 | 66 | #--------------------------------------------------------------------------------- 67 | # no real need to edit anything past this point unless you need to add additional 68 | # rules for different file extensions 69 | #--------------------------------------------------------------------------------- 70 | ifneq ($(BUILD),$(notdir $(CURDIR))) 71 | #--------------------------------------------------------------------------------- 72 | 73 | export OUTPUT := $(CURDIR)/$(TARGET) 74 | export TOPDIR := $(CURDIR) 75 | 76 | export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ 77 | $(foreach dir,$(DATA),$(CURDIR)/$(dir)) 78 | 79 | export DEPSDIR := $(CURDIR)/$(BUILD) 80 | 81 | CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) 82 | CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) 83 | SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) 84 | PICAFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.v.pica))) 85 | SHLISTFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.shlist))) 86 | BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) 87 | 88 | #--------------------------------------------------------------------------------- 89 | # use CXX for linking C++ projects, CC for standard C 90 | #--------------------------------------------------------------------------------- 91 | ifeq ($(strip $(CPPFILES)),) 92 | #--------------------------------------------------------------------------------- 93 | export LD := $(CC) 94 | #--------------------------------------------------------------------------------- 95 | else 96 | #--------------------------------------------------------------------------------- 97 | export LD := $(CXX) 98 | #--------------------------------------------------------------------------------- 99 | endif 100 | #--------------------------------------------------------------------------------- 101 | 102 | export OFILES := $(addsuffix .o,$(BINFILES)) \ 103 | $(PICAFILES:.v.pica=.shbin.o) $(SHLISTFILES:.shlist=.shbin.o) \ 104 | $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o) 105 | 106 | export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ 107 | $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ 108 | -I$(CURDIR)/$(BUILD) 109 | 110 | export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) 111 | 112 | ifeq ($(strip $(ICON)),) 113 | icons := $(wildcard *.png) 114 | ifneq (,$(findstring $(TARGET).png,$(icons))) 115 | export APP_ICON := $(TOPDIR)/$(TARGET).png 116 | else 117 | ifneq (,$(findstring icon.png,$(icons))) 118 | export APP_ICON := $(TOPDIR)/icon.png 119 | endif 120 | endif 121 | else 122 | export APP_ICON := $(TOPDIR)/$(ICON) 123 | endif 124 | 125 | ifeq ($(strip $(NO_SMDH)),) 126 | export _3DSXFLAGS += --smdh=$(CURDIR)/$(TARGET).smdh 127 | endif 128 | 129 | ifneq ($(ROMFS),) 130 | export _3DSXFLAGS += --romfs=$(CURDIR)/$(ROMFS) 131 | endif 132 | 133 | .PHONY: $(BUILD) clean all 134 | 135 | #--------------------------------------------------------------------------------- 136 | all: $(BUILD) 137 | 138 | $(BUILD): 139 | @[ -d $@ ] || mkdir -p $@ 140 | @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile 141 | 142 | #--------------------------------------------------------------------------------- 143 | clean: 144 | @echo clean ... 145 | @rm -fr $(BUILD) $(TARGET).3dsx $(OUTPUT).smdh $(TARGET).elf $(TARGET).cia 146 | 147 | cia: 148 | @bannertool makebanner -i res/banner\ icon.png -a res/audio.wav -o banner.bnr 149 | @bannertool makesmdh -s "Forecast" -l "Forecast" -p "NatTupper" -i icon.png -o icon.icn 150 | @makerom -f cia -o Forecast.cia -rsf res/Forecast.rsf -target t -exefslogo -elf Forecast.elf -icon icon.icn -banner banner.bnr 151 | @rm -f banner.bnr icon.icn 152 | 153 | 154 | #--------------------------------------------------------------------------------- 155 | else 156 | 157 | DEPENDS := $(OFILES:.o=.d) 158 | 159 | #--------------------------------------------------------------------------------- 160 | # main targets 161 | #--------------------------------------------------------------------------------- 162 | ifeq ($(strip $(NO_SMDH)),) 163 | $(OUTPUT).3dsx : $(OUTPUT).elf $(OUTPUT).smdh 164 | else 165 | $(OUTPUT).3dsx : $(OUTPUT).elf 166 | endif 167 | 168 | $(OUTPUT).elf : $(OFILES) 169 | 170 | #--------------------------------------------------------------------------------- 171 | # you need a rule like this for each extension you use as binary data 172 | #--------------------------------------------------------------------------------- 173 | %.bin.o : %.bin 174 | #--------------------------------------------------------------------------------- 175 | @echo $(notdir $<) 176 | @$(bin2o) 177 | 178 | #--------------------------------------------------------------------------------- 179 | # rules for assembling GPU shaders 180 | #--------------------------------------------------------------------------------- 181 | define shader-as 182 | $(eval CURBIN := $(patsubst %.shbin.o,%.shbin,$(notdir $@))) 183 | picasso -o $(CURBIN) $1 184 | bin2s $(CURBIN) | $(AS) -o $@ 185 | echo "extern const u8" `(echo $(CURBIN) | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`"_end[];" > `(echo $(CURBIN) | tr . _)`.h 186 | echo "extern const u8" `(echo $(CURBIN) | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`"[];" >> `(echo $(CURBIN) | tr . _)`.h 187 | echo "extern const u32" `(echo $(CURBIN) | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`_size";" >> `(echo $(CURBIN) | tr . _)`.h 188 | endef 189 | 190 | %.shbin.o : %.v.pica %.g.pica 191 | @echo $(notdir $^) 192 | @$(call shader-as,$^) 193 | 194 | %.shbin.o : %.v.pica 195 | @echo $(notdir $<) 196 | @$(call shader-as,$<) 197 | 198 | %.shbin.o : %.shlist 199 | @echo $(notdir $<) 200 | @$(call shader-as,$(foreach file,$(shell cat $<),$(dir $<)/$(file))) 201 | 202 | -include $(DEPENDS) 203 | 204 | #--------------------------------------------------------------------------------------- 205 | endif 206 | #--------------------------------------------------------------------------------------- 207 | -------------------------------------------------------------------------------- /source/base/screen.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include <3ds.h> 8 | #include 9 | 10 | #include "stb_image.h" 11 | #include "screen.h" 12 | #include "util.h" 13 | 14 | #include "default_shbin.h" 15 | 16 | GX_TRANSFER_FORMAT gpuToGxFormat[13] = { 17 | GX_TRANSFER_FMT_RGBA8, 18 | GX_TRANSFER_FMT_RGB8, 19 | GX_TRANSFER_FMT_RGB5A1, 20 | GX_TRANSFER_FMT_RGB565, 21 | GX_TRANSFER_FMT_RGBA4, 22 | GX_TRANSFER_FMT_RGBA8, // Unsupported 23 | GX_TRANSFER_FMT_RGBA8, // Unsupported 24 | GX_TRANSFER_FMT_RGBA8, // Unsupported 25 | GX_TRANSFER_FMT_RGBA8, // Unsupported 26 | GX_TRANSFER_FMT_RGBA8, // Unsupported 27 | GX_TRANSFER_FMT_RGBA8, // Unsupported 28 | GX_TRANSFER_FMT_RGBA8, // Unsupported 29 | GX_TRANSFER_FMT_RGBA8 // Unsupported 30 | }; 31 | 32 | static bool c3dInitialized; 33 | 34 | static bool shaderInitialized; 35 | static DVLB_s* dvlb; 36 | static shaderProgram_s program; 37 | 38 | static C3D_RenderTarget* target_top; 39 | static C3D_RenderTarget* target_bottom; 40 | static C3D_Mtx projection_top; 41 | static C3D_Mtx projection_bottom; 42 | 43 | static struct { 44 | bool initialized; 45 | C3D_Tex tex; 46 | u32 width; 47 | u32 height; 48 | u32 pow2Width; 49 | u32 pow2Height; 50 | } textures[MAX_TEXTURES]; 51 | 52 | static C3D_Tex* glyphSheets; 53 | 54 | static FILE* screen_open_resource(const char* path) { 55 | u32 realPathSize = strlen(path) + 8; 56 | char realPath[realPathSize]; 57 | 58 | snprintf(realPath, realPathSize, "%s", path); 59 | 60 | return fopen(realPath, "rb"); 61 | } 62 | 63 | void screen_init() { 64 | if(!C3D_Init(C3D_DEFAULT_CMDBUF_SIZE * 4)) { 65 | util_panic("Failed to initialize the GPU."); 66 | return; 67 | } 68 | 69 | c3dInitialized = true; 70 | 71 | target_top = C3D_RenderTargetCreate(240, 400, GPU_RB_RGBA8, GPU_RB_DEPTH24_STENCIL8); 72 | if(target_top == NULL) { 73 | util_panic("Failed to initialize the top screen target."); 74 | return; 75 | } 76 | 77 | u32 displayFlags = GX_TRANSFER_FLIP_VERT(0) | GX_TRANSFER_OUT_TILED(0) | GX_TRANSFER_RAW_COPY(0) | GX_TRANSFER_IN_FORMAT(GX_TRANSFER_FMT_RGBA8) | GX_TRANSFER_OUT_FORMAT(GX_TRANSFER_FMT_RGB8) | GX_TRANSFER_SCALING(GX_TRANSFER_SCALE_NO); 78 | 79 | C3D_RenderTargetSetClear(target_top, C3D_CLEAR_ALL, 0, 0); 80 | C3D_RenderTargetSetOutput(target_top, GFX_TOP, GFX_LEFT, displayFlags); 81 | 82 | target_bottom = C3D_RenderTargetCreate(240, 320, GPU_RB_RGBA8, GPU_RB_DEPTH24_STENCIL8); 83 | if(target_bottom == NULL) { 84 | util_panic("Failed to initialize the bottom screen target."); 85 | return; 86 | } 87 | 88 | C3D_RenderTargetSetClear(target_bottom, C3D_CLEAR_ALL, 0, 0); 89 | C3D_RenderTargetSetOutput(target_bottom, GFX_BOTTOM, GFX_LEFT, displayFlags); 90 | 91 | dvlb = DVLB_ParseFile((u32*) default_shbin, default_shbin_size); 92 | if(dvlb == NULL) { 93 | util_panic("Failed to parse shader."); 94 | return; 95 | } 96 | 97 | Result progInitRes = shaderProgramInit(&program); 98 | if(R_FAILED(progInitRes)) { 99 | util_panic("Failed to initialize shader program: 0x%08lX", progInitRes); 100 | return; 101 | } 102 | 103 | shaderInitialized = true; 104 | 105 | Result progSetVshRes = shaderProgramSetVsh(&program, &dvlb->DVLE[0]); 106 | if(R_FAILED(progSetVshRes)) { 107 | util_panic("Failed to set up vertex shader: 0x%08lX", progInitRes); 108 | return; 109 | } 110 | 111 | C3D_BindProgram(&program); 112 | 113 | C3D_AttrInfo* attrInfo = C3D_GetAttrInfo(); 114 | if(attrInfo == NULL) { 115 | util_panic("Failed to retrieve attribute info."); 116 | return; 117 | } 118 | 119 | AttrInfo_Init(attrInfo); 120 | AttrInfo_AddLoader(attrInfo, 0, GPU_FLOAT, 3); 121 | AttrInfo_AddLoader(attrInfo, 1, GPU_FLOAT, 2); 122 | 123 | C3D_TexEnv* env = C3D_GetTexEnv(0); 124 | if(env == NULL) { 125 | util_panic("Failed to retrieve combiner settings."); 126 | return; 127 | } 128 | 129 | C3D_TexEnvSrc(env, C3D_Both, GPU_TEXTURE0, 0, 0); 130 | C3D_TexEnvOp(env, C3D_Both, 0, 0, 0); 131 | C3D_TexEnvFunc(env, C3D_Both, GPU_REPLACE); 132 | 133 | C3D_DepthTest(true, GPU_GEQUAL, GPU_WRITE_ALL); 134 | 135 | Mtx_OrthoTilt(&projection_top, 0.0, 400.0, 240.0, 0.0, 0.0, 1.0, true); 136 | Mtx_OrthoTilt(&projection_bottom, 0.0, 320.0, 240.0, 0.0, 0.0, 1.0, true); 137 | 138 | Result fontMapRes = fontEnsureMapped(); 139 | if(R_FAILED(fontMapRes)) { 140 | util_panic("Failed to map system font: 0x%08lX", fontMapRes); 141 | return; 142 | } 143 | 144 | TGLP_s* glyphInfo = fontGetGlyphInfo(); 145 | glyphSheets = calloc(glyphInfo->nSheets, sizeof(C3D_Tex)); 146 | if(glyphSheets == NULL) { 147 | util_panic("Failed to allocate font glyph texture data."); 148 | return; 149 | } 150 | 151 | for(int i = 0; i < glyphInfo->nSheets; i++) { 152 | C3D_Tex* tex = &glyphSheets[i]; 153 | tex->data = fontGetGlyphSheetTex(i); 154 | tex->fmt = (GPU_TEXCOLOR) glyphInfo->sheetFmt; 155 | tex->size = glyphInfo->sheetSize; 156 | tex->width = glyphInfo->sheetWidth; 157 | tex->height = glyphInfo->sheetHeight; 158 | tex->param = GPU_TEXTURE_MAG_FILTER(GPU_LINEAR) | GPU_TEXTURE_MIN_FILTER(GPU_LINEAR) | GPU_TEXTURE_WRAP_S(GPU_CLAMP_TO_EDGE) | GPU_TEXTURE_WRAP_T(GPU_CLAMP_TO_EDGE); 159 | } 160 | 161 | screen_load_texture_file(TEXTURE_TOP_SCREEN_BG, "romfs:/top_bg.png", true); 162 | screen_load_texture_file(TEXTURE_BOTTOM_SCREEN_BG, "romfs:/bot_bg.png", true); 163 | screen_load_texture_file(TEXTURE_CLOUD, "romfs:/cloud.png", true); 164 | screen_load_texture_file(TEXTURE_CLOUD_WITH_LIGHTNING, "romfs:/cloud_with_lightning.png", true); 165 | screen_load_texture_file(TEXTURE_CLOUD_WITH_LOTS_OF_RAIN, "romfs:/cloud_with_lots_of_rain.png", true); 166 | screen_load_texture_file(TEXTURE_CLOUD_WITH_RAIN, "romfs:/cloud_with_rain.png", true); 167 | screen_load_texture_file(TEXTURE_CLOUD_WITH_SNOW, "romfs:/cloud_with_snow.png", true); 168 | screen_load_texture_file(TEXTURE_CLOUD_WITH_SOME_RAIN, "romfs:/cloud_with_some_rain.png", true); 169 | screen_load_texture_file(TEXTURE_SUN, "romfs:/sun.png", true); 170 | screen_load_texture_file(TEXTURE_SUN_WITH_CLOUD, "romfs:/sun_with_cloud.png", true); 171 | screen_load_texture_file(TEXTURE_SUN_WITH_RAIN, "romfs:/sun_with_rain.png", true); 172 | screen_load_texture_file(TEXTURE_QUESTION, "romfs:/question.png", true); 173 | screen_load_texture_file(TEXTURE_MENUBAR, "romfs:/menubar.png", true); 174 | screen_load_texture_file(TEXTURE_MENU_OVERLAY, "romfs:/menu_overlay.png", true); 175 | } 176 | 177 | void screen_exit() { 178 | for(u32 id = 0; id < MAX_TEXTURES; id++) { 179 | screen_unload_texture(id); 180 | } 181 | 182 | if(glyphSheets != NULL) { 183 | free(glyphSheets); 184 | glyphSheets = NULL; 185 | } 186 | 187 | if(shaderInitialized) { 188 | shaderProgramFree(&program); 189 | shaderInitialized = false; 190 | } 191 | 192 | if(dvlb != NULL) { 193 | DVLB_Free(dvlb); 194 | dvlb = NULL; 195 | } 196 | 197 | if(target_top != NULL) { 198 | C3D_RenderTargetDelete(target_top); 199 | target_top = NULL; 200 | } 201 | 202 | if(target_bottom != NULL) { 203 | C3D_RenderTargetDelete(target_bottom); 204 | target_bottom = NULL; 205 | } 206 | 207 | if(c3dInitialized) { 208 | C3D_Fini(); 209 | c3dInitialized = false; 210 | } 211 | } 212 | 213 | static u32 screen_next_pow_2(u32 i) { 214 | i--; 215 | i |= i >> 1; 216 | i |= i >> 2; 217 | i |= i >> 4; 218 | i |= i >> 8; 219 | i |= i >> 16; 220 | i++; 221 | 222 | return i; 223 | } 224 | 225 | void screen_load_texture(u32 id, void* data, u32 size, u32 width, u32 height, GPU_TEXCOLOR format, bool linearFilter) { 226 | if(id >= MAX_TEXTURES) { 227 | util_panic("Attempted to load buffer to invalid texture ID \"%lu\".", id); 228 | return; 229 | } 230 | 231 | u32 pow2Width = screen_next_pow_2(width); 232 | if(pow2Width < 64) { 233 | pow2Width = 64; 234 | } 235 | 236 | u32 pow2Height = screen_next_pow_2(height); 237 | if(pow2Height < 64) { 238 | pow2Height = 64; 239 | } 240 | 241 | u32 pixelSize = size / width / height; 242 | 243 | u8* pow2Tex = linearAlloc(pow2Width * pow2Height * pixelSize); 244 | if(pow2Tex == NULL) { 245 | util_panic("Failed to allocate temporary texture buffer."); 246 | return; 247 | } 248 | 249 | memset(pow2Tex, 0, pow2Width * pow2Height * pixelSize); 250 | 251 | for(u32 x = 0; x < width; x++) { 252 | for(u32 y = 0; y < height; y++) { 253 | u32 dataPos = (y * width + x) * pixelSize; 254 | u32 pow2TexPos = (y * pow2Width + x) * pixelSize; 255 | 256 | for(u32 i = 0; i < pixelSize; i++) { 257 | pow2Tex[pow2TexPos + i] = ((u8*) data)[dataPos + i]; 258 | } 259 | } 260 | } 261 | 262 | textures[id].initialized = true; 263 | textures[id].width = width; 264 | textures[id].height = height; 265 | textures[id].pow2Width = pow2Width; 266 | textures[id].pow2Height = pow2Height; 267 | 268 | if(!C3D_TexInit(&textures[id].tex, (int) pow2Width, (int) pow2Height, format)) { 269 | util_panic("Failed to initialize texture."); 270 | return; 271 | } 272 | 273 | C3D_TexSetFilter(&textures[id].tex, linearFilter ? GPU_LINEAR : GPU_NEAREST, GPU_NEAREST); 274 | 275 | Result flushRes = GSPGPU_FlushDataCache(pow2Tex, pow2Width * pow2Height * 4); 276 | if(R_FAILED(flushRes)) { 277 | util_panic("Failed to flush texture buffer: 0x%08lX", flushRes); 278 | return; 279 | } 280 | 281 | C3D_SafeDisplayTransfer((u32*) pow2Tex, GX_BUFFER_DIM(pow2Width, pow2Height), (u32*) textures[id].tex.data, GX_BUFFER_DIM(pow2Width, pow2Height), GX_TRANSFER_FLIP_VERT(1) | GX_TRANSFER_OUT_TILED(1) | GX_TRANSFER_RAW_COPY(0) | GX_TRANSFER_IN_FORMAT((u32) gpuToGxFormat[format]) | GX_TRANSFER_OUT_FORMAT((u32) gpuToGxFormat[format]) | GX_TRANSFER_SCALING(GX_TRANSFER_SCALE_NO)); 282 | gspWaitForPPF(); 283 | 284 | linearFree(pow2Tex); 285 | } 286 | 287 | u32 screen_load_texture_auto(void* tiledData, u32 size, u32 width, u32 height, GPU_TEXCOLOR format, bool linearFilter) { 288 | int id = -1; 289 | for(int i = TEXTURE_AUTO_START; i < MAX_TEXTURES; i++) { 290 | if(!textures[i].initialized) { 291 | id = i; 292 | break; 293 | } 294 | } 295 | 296 | if(id == -1) { 297 | util_panic("Attempted to load auto texture from buffer without free textures."); 298 | return 0; 299 | } 300 | 301 | screen_load_texture((u32) id, tiledData, size, width, height, format, linearFilter); 302 | return (u32) id; 303 | } 304 | 305 | void screen_load_texture_file(u32 id, const char* path, bool linearFilter) { 306 | if(id >= MAX_TEXTURES) { 307 | util_panic("Attempted to load path \"%s\" to invalid texture ID \"%lu\".", path, id); 308 | return; 309 | } 310 | 311 | FILE* fd = screen_open_resource(path); 312 | if(fd == NULL) { 313 | util_panic("Failed to load PNG file \"%s\": %s", path, strerror(errno)); 314 | return; 315 | } 316 | 317 | int width; 318 | int height; 319 | int depth; 320 | u8* image = stbi_load_from_file(fd, &width, &height, &depth, STBI_rgb_alpha); 321 | fclose(fd); 322 | 323 | if(image == NULL || depth != STBI_rgb_alpha) { 324 | util_panic("Failed to load PNG file \"%s\".", path); 325 | return; 326 | } 327 | 328 | for(u32 x = 0; x < width; x++) { 329 | for(u32 y = 0; y < height; y++) { 330 | u32 pos = (y * width + x) * 4; 331 | 332 | u8 c1 = image[pos + 0]; 333 | u8 c2 = image[pos + 1]; 334 | u8 c3 = image[pos + 2]; 335 | u8 c4 = image[pos + 3]; 336 | 337 | image[pos + 0] = c4; 338 | image[pos + 1] = c3; 339 | image[pos + 2] = c2; 340 | image[pos + 3] = c1; 341 | } 342 | } 343 | 344 | screen_load_texture(id, image, (u32) (width * height * 4), (u32) width, (u32) height, GPU_RGBA8, linearFilter); 345 | 346 | free(image); 347 | } 348 | 349 | u32 screen_load_texture_file_auto(const char* path, bool linearFilter) { 350 | int id = -1; 351 | for(int i = TEXTURE_AUTO_START; i < MAX_TEXTURES; i++) { 352 | if(!textures[i].initialized) { 353 | id = i; 354 | break; 355 | } 356 | } 357 | 358 | if(id == -1) { 359 | util_panic("Attempted to load auto texture from path \"%s\" without free textures.", path); 360 | return 0; 361 | } 362 | 363 | screen_load_texture_file((u32) id, path, linearFilter); 364 | return (u32) id; 365 | } 366 | 367 | static u32 screen_tiled_texture_index(u32 x, u32 y, u32 w, u32 h) { 368 | return (((y >> 3) * (w >> 3) + (x >> 3)) << 6) + ((x & 1) | ((y & 1) << 1) | ((x & 2) << 1) | ((y & 2) << 2) | ((x & 4) << 2) | ((y & 4) << 3)); 369 | } 370 | 371 | void screen_load_texture_tiled(u32 id, void* tiledData, u32 size, u32 width, u32 height, GPU_TEXCOLOR format, bool linearFilter) { 372 | if(id >= MAX_TEXTURES) { 373 | util_panic("Attempted to load tiled data to invalid texture ID \"%lu\".", id); 374 | return; 375 | } 376 | 377 | u8* untiledData = (u8*) calloc(1, size); 378 | if(untiledData == NULL) { 379 | util_panic("Failed to allocate buffer for texture untiling."); 380 | return; 381 | } 382 | 383 | u32 pixelSize = size / width / height; 384 | 385 | for(u32 x = 0; x < width; x++) { 386 | for(u32 y = 0; y < height; y++) { 387 | u32 tiledDataPos = screen_tiled_texture_index(x, y, width, height) * pixelSize; 388 | u32 untiledDataPos = (y * width + x) * pixelSize; 389 | 390 | for(u32 i = 0; i < pixelSize; i++) { 391 | untiledData[untiledDataPos + i] = ((u8*) tiledData)[tiledDataPos + i]; 392 | } 393 | } 394 | } 395 | 396 | screen_load_texture(id, untiledData, size, width, height, format, linearFilter); 397 | 398 | free(untiledData); 399 | } 400 | 401 | u32 screen_load_texture_tiled_auto(void* tiledData, u32 size, u32 width, u32 height, GPU_TEXCOLOR format, bool linearFilter) { 402 | int id = -1; 403 | for(int i = TEXTURE_AUTO_START; i < MAX_TEXTURES; i++) { 404 | if(!textures[i].initialized) { 405 | id = i; 406 | break; 407 | } 408 | } 409 | 410 | if(id == -1) { 411 | util_panic("Attempted to load auto texture from tiled data without free textures."); 412 | return 0; 413 | } 414 | 415 | screen_load_texture_tiled((u32) id, tiledData, size, width, height, format, linearFilter); 416 | return (u32) id; 417 | } 418 | 419 | void screen_unload_texture(u32 id) { 420 | if(id >= MAX_TEXTURES) { 421 | util_panic("Attempted to unload invalid texture ID \"%lu\".", id); 422 | return; 423 | } 424 | 425 | if(textures[id].initialized) { 426 | C3D_TexDelete(&textures[id].tex); 427 | 428 | textures[id].initialized = false; 429 | textures[id].width = 0; 430 | textures[id].height = 0; 431 | textures[id].pow2Width = 0; 432 | textures[id].pow2Height = 0; 433 | } 434 | } 435 | 436 | void screen_get_texture_size(u32* width, u32* height, u32 id) { 437 | if(id >= MAX_TEXTURES || !textures[id].initialized) { 438 | util_panic("Attempted to get size of invalid texture ID \"%lu\".", id); 439 | return; 440 | } 441 | 442 | if(width) { 443 | *width = textures[id].width; 444 | } 445 | 446 | if(height) { 447 | *height = textures[id].height; 448 | } 449 | } 450 | 451 | void screen_begin_frame() { 452 | if(!C3D_FrameBegin(C3D_FRAME_SYNCDRAW)) { 453 | util_panic("Failed to begin frame."); 454 | return; 455 | } 456 | } 457 | 458 | void screen_end_frame() { 459 | C3D_FrameEnd(0); 460 | } 461 | 462 | void screen_select(gfxScreen_t screen) { 463 | if(!C3D_FrameDrawOn(screen == GFX_TOP ? target_top : target_bottom)) { 464 | util_panic("Failed to select render target."); 465 | return; 466 | } 467 | 468 | C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, shaderInstanceGetUniformLocation(program.vertexShader, "projection"), screen == GFX_TOP ? &projection_top : &projection_bottom); 469 | } 470 | 471 | void screen_set_scissor(bool enabled, u32 x, u32 y, u32 width, u32 height) { 472 | C3D_SetScissor(enabled ? GPU_SCISSOR_NORMAL : GPU_SCISSOR_DISABLE, 240 - (y + height), x, 240 - y, x + width); 473 | } 474 | 475 | static void screen_draw_quad(float x1, float y1, float x2, float y2, float tx1, float ty1, float tx2, float ty2) { 476 | C3D_ImmDrawBegin(GPU_TRIANGLES); 477 | 478 | C3D_ImmSendAttrib(x1, y1, 0.5f, 0.0f); 479 | C3D_ImmSendAttrib(tx1, ty1, 0.0f, 0.0f); 480 | 481 | C3D_ImmSendAttrib(x2, y2, 0.5f, 0.0f); 482 | C3D_ImmSendAttrib(tx2, ty2, 0.0f, 0.0f); 483 | 484 | C3D_ImmSendAttrib(x2, y1, 0.5f, 0.0f); 485 | C3D_ImmSendAttrib(tx2, ty1, 0.0f, 0.0f); 486 | 487 | C3D_ImmSendAttrib(x1, y1, 0.5f, 0.0f); 488 | C3D_ImmSendAttrib(tx1, ty1, 0.0f, 0.0f); 489 | 490 | C3D_ImmSendAttrib(x1, y2, 0.5f, 0.0f); 491 | C3D_ImmSendAttrib(tx1, ty2, 0.0f, 0.0f); 492 | 493 | C3D_ImmSendAttrib(x2, y2, 0.5f, 0.0f); 494 | C3D_ImmSendAttrib(tx2, ty2, 0.0f, 0.0f); 495 | 496 | C3D_ImmDrawEnd(); 497 | } 498 | 499 | void screen_draw_texture(u32 id, float x, float y, float width, float height) { 500 | if(id >= MAX_TEXTURES || !textures[id].initialized) { 501 | util_panic("Attempted to draw invalid texture ID \"%lu\".", id); 502 | return; 503 | } 504 | 505 | C3D_TexBind(0, &textures[id].tex); 506 | screen_draw_quad(x, y, x + width, y + height, 0, 0, (float) textures[id].width / (float) textures[id].pow2Width, (float) textures[id].height / (float) textures[id].pow2Height); 507 | } 508 | 509 | void screen_draw_texture_crop(u32 id, float x, float y, float width, float height) { 510 | if(id >= MAX_TEXTURES || !textures[id].initialized) { 511 | util_panic("Attempted to draw invalid texture ID \"%lu\".", id); 512 | return; 513 | } 514 | 515 | C3D_TexBind(0, &textures[id].tex); 516 | screen_draw_quad(x, y, x + width, y + height, 0, 0, width / (float) textures[id].pow2Width, height / (float) textures[id].pow2Height); 517 | } 518 | 519 | static void screen_get_string_size_internal(float* width, float* height, const char* text, float scaleX, float scaleY, bool oneLine, bool wrap, float wrapX) { 520 | float w = 0; 521 | float h = 0; 522 | float lineWidth = 0; 523 | 524 | if(text != NULL) { 525 | h = scaleY * fontGetInfo()->lineFeed; 526 | 527 | const uint8_t* p = (const uint8_t*) text; 528 | const uint8_t* lastAlign = p; 529 | u32 code = 0; 530 | ssize_t units = -1; 531 | while(*p && (units = decode_utf8(&code, p)) != -1 && code > 0) { 532 | p += units; 533 | 534 | if(code == '\n' || (wrap && lineWidth + scaleX * fontGetCharWidthInfo(fontGlyphIndexFromCodePoint(code))->charWidth >= wrapX)) { 535 | lastAlign = p; 536 | 537 | if(lineWidth > w) { 538 | w = lineWidth; 539 | } 540 | 541 | lineWidth = 0; 542 | 543 | if(oneLine) { 544 | break; 545 | } 546 | 547 | h += scaleY * fontGetInfo()->lineFeed; 548 | } 549 | 550 | if(code != '\n') { 551 | u32 num = 1; 552 | if(code == '\t') { 553 | code = ' '; 554 | num = 4 - (p - units - lastAlign) % 4; 555 | 556 | lastAlign = p; 557 | } 558 | 559 | lineWidth += (scaleX * fontGetCharWidthInfo(fontGlyphIndexFromCodePoint(code))->charWidth) * num; 560 | } 561 | } 562 | } 563 | 564 | if(width) { 565 | *width = lineWidth > w ? lineWidth : w; 566 | } 567 | 568 | if(height) { 569 | *height = h; 570 | } 571 | } 572 | 573 | void screen_get_string_size(float* width, float* height, const char* text, float scaleX, float scaleY) { 574 | screen_get_string_size_internal(width, height, text, scaleX, scaleY, false, false, 0); 575 | } 576 | 577 | void screen_get_string_size_wrap(float* width, float* height, const char* text, float scaleX, float scaleY, float wrapX) { 578 | screen_get_string_size_internal(width, height, text, scaleX, scaleY, false, true, wrapX); 579 | } 580 | 581 | static void screen_draw_string_internal(const char* text, float x, float y, float scaleX, float scaleY, u32 colorId, bool centerLines, bool wrap, float wrapX) { 582 | if(text == NULL) { 583 | return; 584 | } 585 | 586 | C3D_TexEnv* env = C3D_GetTexEnv(0); 587 | if(env == NULL) { 588 | util_panic("Failed to retrieve combiner settings."); 589 | return; 590 | } 591 | 592 | C3D_TexEnvSrc(env, C3D_RGB, GPU_CONSTANT, 0, 0); 593 | C3D_TexEnvSrc(env, C3D_Alpha, GPU_TEXTURE0, GPU_CONSTANT, 0); 594 | C3D_TexEnvOp(env, C3D_Both, 0, 0, 0); 595 | C3D_TexEnvFunc(env, C3D_RGB, GPU_REPLACE); 596 | C3D_TexEnvFunc(env, C3D_Alpha, GPU_MODULATE); 597 | C3D_TexEnvColor(env, colorId); 598 | 599 | float stringWidth; 600 | screen_get_string_size_internal(&stringWidth, NULL, text, scaleX, scaleY, false, wrap, wrapX); 601 | 602 | float lineWidth; 603 | screen_get_string_size_internal(&lineWidth, NULL, text, scaleX, scaleY, true, wrap, wrapX); 604 | 605 | float currX = x; 606 | if(centerLines) { 607 | currX += (stringWidth - lineWidth) / 2; 608 | } 609 | 610 | int lastSheet = -1; 611 | 612 | const uint8_t* p = (const uint8_t*) text; 613 | const uint8_t* lastAlign = p; 614 | u32 code = 0; 615 | ssize_t units = -1; 616 | while(*p && (units = decode_utf8(&code, p)) != -1 && code > 0) { 617 | p += units; 618 | 619 | if(code == '\n' || (wrap && currX + scaleX * fontGetCharWidthInfo(fontGlyphIndexFromCodePoint(code))->charWidth >= wrapX)) { 620 | lastAlign = p; 621 | 622 | screen_get_string_size_internal(&lineWidth, NULL, (const char*) p, scaleX, scaleY, true, wrap, wrapX); 623 | 624 | currX = x; 625 | if(centerLines) { 626 | currX += (stringWidth - lineWidth) / 2; 627 | } 628 | 629 | y += scaleY * fontGetInfo()->lineFeed; 630 | } 631 | 632 | if(code != '\n') { 633 | u32 num = 1; 634 | if(code == '\t') { 635 | code = ' '; 636 | num = 4 - (p - units - lastAlign) % 4; 637 | 638 | lastAlign = p; 639 | } 640 | 641 | fontGlyphPos_s data; 642 | fontCalcGlyphPos(&data, fontGlyphIndexFromCodePoint(code), GLYPH_POS_CALC_VTXCOORD, scaleX, scaleY); 643 | 644 | if(data.sheetIndex != lastSheet) { 645 | lastSheet = data.sheetIndex; 646 | C3D_TexBind(0, &glyphSheets[lastSheet]); 647 | } 648 | 649 | for(u32 i = 0; i < num; i++) { 650 | screen_draw_quad(currX + data.vtxcoord.left, y + data.vtxcoord.top, currX + data.vtxcoord.right, y + data.vtxcoord.bottom, data.texcoord.left, data.texcoord.top, data.texcoord.right, data.texcoord.bottom); 651 | 652 | currX += data.xAdvance; 653 | } 654 | } 655 | } 656 | 657 | env = C3D_GetTexEnv(0); 658 | if(env == NULL) { 659 | util_panic("Failed to retrieve combiner settings."); 660 | return; 661 | } 662 | 663 | C3D_TexEnvSrc(env, C3D_Both, GPU_TEXTURE0, 0, 0); 664 | C3D_TexEnvOp(env, C3D_Both, 0, 0, 0); 665 | C3D_TexEnvFunc(env, C3D_Both, GPU_REPLACE); 666 | } 667 | 668 | void screen_draw_string(const char* text, float x, float y, float scaleX, float scaleY, u32 colorId, bool centerLines) { 669 | screen_draw_string_internal(text, x, y, scaleX, scaleY, colorId, centerLines, false, 0); 670 | } 671 | 672 | void screen_draw_string_wrap(const char* text, float x, float y, float scaleX, float scaleY, u32 colorId, bool centerLines, float wrapX) { 673 | screen_draw_string_internal(text, x, y, scaleX, scaleY, colorId, centerLines, true, wrapX); 674 | } 675 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------