1: /*
     2:  *  Guitar-ZyX(tm)::MasterControlProgram - portable guitar F/X controller
     3:  *  Copyright (C) 2009  Douglas McClendon
     4:  *
     5:  *  This program is free software: you can redistribute it and/or modify
     6:  *  it under the terms of the GNU General Public License as published by
     7:  *  the Free Software Foundation, version 3 of the License.
     8:  *
     9:  *  This program is distributed in the hope that it will be useful,
    10:  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    11:  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12:  *  GNU General Public License for more details.
    13:  *
    14:  *  You should have received a copy of the GNU General Public License
    15:  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16: */
    17: /*
    18: #############################################################################
    19: #############################################################################
    20: ## 
    21: ## gzmcpc::mode__ssid__input: ssid input mode
    22: ##
    23: #############################################################################
    24: ##
    25: ## Copyright 2008-2009 Douglas McClendon <dmc AT filteredperception DOT org>
    26: ##
    27: #############################################################################
    28: #############################################################################
    29: #
    30: */
    31: 
    32: 
    33: 
    34: 
    35: #include <nds.h> 
    36: 
    37: #include <stdio.h> 
    38: 
    39: #include <string.h> 
    40: 
    41: #include "debug.h" 
    42: 
    43: #include "configfiles.h" 
    44: 
    45: #include "graphics.h" 
    46: 
    47: #include "input.h" 
    48: 
    49: #include "modes.h" 
    50: 
    51: #include "mode__ssid__input.h" 
    52: 
    53: #include "network.h" 
    54: 
    55: #include "sound.h" 
    56: 
    57: 
    58: 
    59: #include "sounds.h" 
    60: 
    61: #include "sounds_bin.h" 
    62: 
    63: #include "resources/bitmaps/guitar-zyx.splash.main.h" 
    64: 
    65: 
    66: 
    67: 
    68: char ssid_input_buffer[SSID_MAXLEN + 1];
    69: int ssid_input_cursor = 0;
    70: 
    71: int console_shown;
    72: 
    73: 
    74: 
    75: 
    76: void mode__ssid__input___init(void) {
    77: 
    78:   console_shown = 0;
    79: 
    80:   // initialize main screen
    81:   videoSetMode(MODE_5_2D);
    82: 
    83:   // map main screen background fourth (128k) region to vram bank A
    84:   vramSetBankA(VRAM_A_MAIN_BG_0x06060000);
    85: 
    86:   // set the secondary/sub screen for text 
    87:   videoSetModeSub(MODE_5_2D);
    88: 
    89:   // use vram bank c for the secondary/sub-screen(background)
    90:   vramSetBankC(VRAM_C_SUB_BG);
    91: 
    92:   // default to fully faded to black
    93:   REG_BLDY = 31;
    94:   // set mainscreen to fade to black mode, utilizing bgs3
    95:   REG_BLDCNT = BLEND_FADE_BLACK | BLEND_SRC_BG3;
    96: 
    97:   // default to not faded to black
    98:   REG_BLDY_SUB = 31;
    99:   // set subscreen to fade to black mode, utilizing bgs3
   100:   REG_BLDCNT_SUB = BLEND_FADE_BLACK | BLEND_SRC_BG1;
   101: 
   102:   // XXX add a comment documenting args here
   103:   // initialize the console
   104:   // note: These should be the defaults used by consoleDemoInit()
   105:   //       (but BgSize_T_256x512 I'm not so sure about)
   106:   //       This is because these values are crafted to work with
   107:   //       the similar defaults from keyboardDemoInit(), which
   108:   //       I likewise copied.
   109:   // 22,3...
   110:   consoleInit(&bottom_screen, 
   111: 	      0, 
   112: 	      BgType_Text4bpp, 
   113: 	      BgSize_T_256x256, 
   114: 	      22, 
   115: 	      3, 
   116: 	      false,
   117: 	      true);
   118: 
   119:   consoleSelect(&bottom_screen);
   120: 
   121:   BG_PALETTE_SUB[255] = RGB15(0, 0, 0);
   122: 
   123:   // "This function sets the console to use sub display, 
   124:   // VRAM_C, and BG0 and enables MODE_0_2D on the sub display"
   125:   //  consoleDemoInit();
   126: 
   127:   //  keyboardDemoInit();
   128:   //  "Same as calling keyboardInit(NULL, 3, BgType_Text4bpp, 
   129:   //                                BgSize_T_256x512, 20, 0, false, true)"
   130: 
   131:   keyboardInit(&keyboard, 
   132: 	       1,
   133: 	       BgType_Text4bpp, 
   134: 	       BgSize_T_256x512, 
   135: 	       20, 
   136: 	       0, 
   137: 	       false, 
   138: 	       false);
   139: 
   140:   bgHide(keyboard.background);
   141: 
   142:   // mo experiment (just what a final true does on the kbInit)
   143:   KeyMap* map = keyboard.mappings[keyboard.state];
   144:   u16* pal = keyboard.keyboardOnSub ? BG_PALETTE_SUB : BG_PALETTE;
   145:   decompress(keyboard.tiles, bgGetGfxPtr(keyboard.background),  LZ77Vram);
   146:   dmaCopy(map->mapDataReleased, bgGetMapPtr(keyboard.background), 
   147: 	  map->width * map->height * keyboard.grid_height * keyboard.grid_width * 2 / 64);
   148:   dmaCopy(keyboard.palette, pal, keyboard.paletteLen);
   149:   // /mo
   150: 
   151:   keyboard.scrollSpeed = 1024;
   152: 
   153:   // note, when trying for fun swapping these, I think it exposed a desmume bug
   154:   bgSetPriority(keyboard.background, 3);
   155:   bgSetPriority(bottom_screen.bgId, 2);
   156:   bgHide(keyboard.background);
   157:   bgHide(bottom_screen.bgId);
   158: 
   159:   // the example code invokes this once, will try it that way first
   160:   //  keyboardShow();
   161: 
   162:   // kbshow exp
   163:   keyboard.visible = 1;
   164:   bgSetScroll(keyboard.background, 0, keyboard.offset_y);
   165:   bgUpdate();
   166: 
   167:   // /kbshow exp
   168: 
   169:   // init screen layer/background 3 
   170:   bg3 = bgInit(3, BgType_Bmp16, BgSize_B16_256x256, 24, 0);
   171:   // its initial priority, lowest (to emphasize lack of other enabled layers)
   172:   // priorities 0..3, 0 highest priority
   173:   bgSetPriority(bg3, 3);
   174: 
   175:   // load main splash screen into screen/background memory (bg3)
   176:   decompress(guitar_zyx_splash_mainBitmap, (u16*)bgGetGfxPtr(bg3),  LZ77Vram);
   177: 
   178:   iprintf("\n");
   179:   iprintf("================================\n");
   180:   // tailer header if this is an initial setting
   181:   if (! strncmp(ap_ssid, "unset", strlen(ap_ssid))) {
   182:   iprintf("   Initial WiFi Configuration   \n");
   183:   } else {
   184:   iprintf("       WiFi Configuration       \n");
   185:   }
   186:   iprintf("\n");
   187:   iprintf("Enter SSID, or 'wfc', then Rtrn\n");
   188:   iprintf("================================\n");
   189:   iprintf("\n");
   190:   iprintf("SSID> ");
   191:       
   192:   bgShow(keyboard.background);
   193:   bgShow(bottom_screen.bgId);
   194: 
   195: }
   196: 
   197: void mode__ssid__input___top_renderer(void) {
   198: 
   199:   unsigned char tfontint;
   200: 
   201:   // do a 1 second fade in
   202:   if (mode_ms < 3000) {
   203:     REG_BLDY = 31 - (31 * mode_ms/ 3000);
   204:     REG_BLDY_SUB = 31 - (31 * mode_ms/ 3000);
   205: 
   206:     tfontint = (unsigned char)((int)(font_intensity) * 
   207: 			       (mode_ms - 0) / 
   208: 			       3000);
   209: 
   210:     BG_PALETTE_SUB[255] = RGB15(tfontint / 3,
   211: 				tfontint,
   212: 				tfontint / 3);
   213: 
   214:   } else {
   215:     if (! console_shown) {
   216:       bgShow(keyboard.background);
   217:       bgShow(bottom_screen.bgId);
   218:       console_shown = 1;
   219:     }
   220:     // TODO: need fadeout mechanism on mode exit
   221:   }
   222: }
   223: 
   224: void mode__ssid__input___bot_renderer(void) {
   225:   
   226: 
   227: }
   228: 
   229: void mode__ssid__input___input_handler(void) {
   230: 
   231:   int key;
   232: 
   233:   // get possible keystroke
   234:   key = keyboardUpdate();
   235: 
   236:   // handle keystroke if necessary
   237:   if (key > 0) {
   238:     // handle return
   239:     // TODO: define magic key codes
   240:     if (key == 10) {
   241:       // experiment - smoother ui?
   242:       bgUpdate();
   243:       swiWaitForVBlank();
   244: 
   245:       snprintf(ap_ssid, SSID_MAXLEN, "%s", ssid_input_buffer);
   246:       // TODO write out new configfile
   247:       rewrite_defaults_with_new_ssid();
   248:       // go offline
   249:       netmode_go_offline();
   250:       // start trying to go online with the new ssid
   251:       network_state = NSM_START;
   252:       system_xmode_new(MODE_TPW__JAM);
   253:     } else if (key == 8) {
   254:       // do nothing on an extra backspace
   255:       if (ssid_input_cursor) {
   256: 	iprintf("%c", key);
   257: 	ssid_input_cursor--;
   258: 	ssid_input_buffer[ssid_input_cursor + 1] = '\0';
   259:       }
   260:     } else {
   261:       iprintf("%c", key);
   262:       ssid_input_buffer[ssid_input_cursor] = key;
   263:       ssid_input_buffer[ssid_input_cursor + 1] = '\0';
   264:       ssid_input_cursor++;
   265:     }
   266:   }
   267: 
   268:   //
   269:   // handle input
   270:   //
   271:   if ((heldkeys & KEY_L) && (heldkeys & KEY_R)) {
   272:     //
   273:     // held(L)+held(R)+something
   274:     //
   275: 
   276:   } else if (heldkeys & KEY_L) {
   277:     //
   278:     // held(L)+something
   279:     //
   280: 
   281:   } else if (heldkeys & KEY_R) {
   282:     //
   283:     // held(R)+something
   284:     //
   285: 
   286:   } else {
   287:     //
   288:     // no interesting modifer keys held
   289:     //
   290: 
   291:     if (downkeys & KEY_START) {
   292:     }
   293: 
   294:     if (downkeys & KEY_SELECT) {
   295:       system_xmode_new(MODE_TPW__JAM);
   296:     }
   297: 
   298:     if (downkeys & KEY_X) {
   299:       system_xmode_new(MODE_TPW__JAM);
   300:     }
   301: 
   302:     if (downkeys & KEY_Y) {
   303:       system_xmode_new(MODE_TPW__JAM);
   304:     }
   305: 
   306:     if (downkeys & KEY_A) {
   307:       system_xmode_new(MODE_TPW__JAM);
   308:     }
   309: 
   310:     if (downkeys & KEY_B) {
   311:       system_xmode_new(MODE_TPW__JAM);
   312:     }
   313: 
   314:   }
   315: 
   316: 
   317:   // touchpad handling is independent of modifiers (at the moment)
   318:   if ((downkeys & KEY_TOUCH) || (heldkeys & KEY_TOUCH)) {
   319: 
   320:   }
   321: 
   322: }
   323: 
   324: void mode__ssid__input___idle(void) {
   325:   //
   326:   // handle fadeout
   327:   //
   328:   if (mode != next_mode) {
   329:     if (((mode_ms - exit_mode_ms) > SSID__INPUT__TOP_BG_FADE_OUT_DURATION_MS) &&
   330: 	((mode_ms - exit_mode_ms) > SSID__INPUT__BOT_BG_FADE_OUT_DURATION_MS) &&
   331: 	((mode_ms - exit_mode_ms) > SSID__INPUT__BOT_TXT_FADE_OUT_DURATION_MS)) {
   332: 
   333:       // XXX: this should get moved elsewhere, and use dma
   334:       // XXX: not even remotely sure it is necessary or helps, just a theory
   335:       
   336:       // clear background image memory bank
   337:       // 256 * 256 * 2 = 128k 
   338:       dmaFillHalfWords(0,
   339: 		       (u16*)bgGetGfxPtr(bg3),
   340: 		       256 * 256 * 2);
   341:       //      memptr = (u16*)bgGetGfxPtr(bg3);
   342:       //      for (i = 0 ; i < (256 * 256) ; i++) memptr[i] = (u16)0;
   343:       
   344:       dmaFillHalfWords(0,
   345: 		       (u16*)bgGetGfxPtr(bgs2),
   346: 		       256 * 256);
   347: 
   348:       dmaFillHalfWords(0,
   349: 		       (u16*)bgGetGfxPtr(bgs0),
   350: 		       256 * 256 / 2);
   351: 
   352:       system_xmode_real();
   353:     }
   354:   }
   355: }
   356: 
   357: 
   358: void mode__ssid__input___exit(void) {
   359: 
   360: }
   361: