Main Page | Modules | Data Structures | File List | Data Fields | Globals | Related Pages | Examples

mc.c

Example of Memory Card browsing, file_type().
/*
 * mc.c - "Memory Card" Turbo application
 *
 * Copyright (C) 2003 BLADOX, s.r.o.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

#include <config.h>
#include <turbo/turbo.h>

#include <stdlib.h>
#include <string.h>


/* *INDENT-OFF* */

lc_char PROGMEM lc_MC[]={ 
        LC_EN("Memory Card") 
        LC_CZ("Pametova karta") 
        LC_END
};

lc_char PROGMEM lc_Label[]={
        LC_EN("Label:")
        LC_CZ("Oznaceni:")
        LC_END
};

lc_char PROGMEM lc_Capacity[]={ 
        LC_EN("Capacity") 
        LC_CZ("Kapacita") 
        LC_END
};

lc_char PROGMEM lc_Free[]={ 
        LC_EN("Free ") 
        LC_CZ("Volno ") 
        LC_END
};

lc_char PROGMEM lc_Blocks[]={ 
        LC_EN(" Blocks") 
        LC_CZ(" bloku") 
        LC_END
};

lc_char PROGMEM lc_Label_Rename[]={ 
        LC_EN("Rename") 
        LC_CZ("Prejmenovat") 
        LC_END
};

lc_char PROGMEM lc_Detail[]={ 
        LC_EN("Detail") 
        LC_END
};

lc_char PROGMEM lc_Select[]={ 
        LC_EN("Select") 
        LC_CZ("Vyber") 
        LC_END
};

/* *INDENT-ON* */

u8 mc_ctx (SCtx * ctx, u8 action);

SNode label_node = {
  NULL,
  mc_ctx,
  NULL
};

b32 tmp_b;

u8 mc_select (SCtx * ctx, u8 action)
{
  if (action == APP_ENTER)
  {
    u8 *res;
    t_file_cb *cb = file_type (tmp_b);
    u8 j;

    j = APP_USE_DEFAULT;
    if (cb != NULL)
      j = cb (tmp_b, FILE_SELECT);

    if (j != APP_USE_DEFAULT)
    {
      if (j == APP_OK)
        j = APP_BACK;
      return j;
    }

    return display_more (tmp_b);
  }
  return APP_OK;
}

u8 mc_protect (SCtx * ctx, u8 action)
{
  if (action == APP_ENTER)
  {
    u8 *res;
    u8 j, l, i;
    u8 key[16];

    i = password (key, 16, NULL);
    if (i != APP_OK)
      return i;

    l = mc_file_encrypt (tmp_b, key, INODE_TYPE_PROTECTED_KEY_DATA);
    if (l != NO_ERROR)
      return perror (l);
    return APP_BACK;
  }
  return APP_OK;
}

u8 mc_unprotect (SCtx * ctx, u8 action)
{
  if (action == APP_ENTER)
  {
    u8 *res;
    u8 j, i, l;
    u8 key[16];

    i = password (key, 16, NULL);
    if (i != APP_OK)
      return i;

    l = mc_file_decrypt (tmp_b, key);
    if (l != NO_ERROR)
      return perror (l);
    return APP_BACK;
  }
  return APP_OK;
}

u8 PROGMEM t_d1[] = "Path: ";
u8 PROGMEM t_d2[] = "Regular File\n";
u8 PROGMEM t_d4[] = "Protected data\n";
u8 PROGMEM t_d5[] = "Protected all\n";
u8 PROGMEM t_d6[] = "Type: ";
u8 PROGMEM t_d7[] = "Len: ";
u8 PROGMEM t_d8[] = "Unknown";
u8 PROGMEM t_d12[] = "Inode: ";
u8 PROGMEM t_d13[] = "Directory\n";

u8 mc_detail (SCtx * ctx, u8 action)
{
  if (action == APP_ENTER)
  {
    u8 *r;
    u8 *t = buf_B ();
    u8 *pth = buf_A ();
    u8 ext[3];
    Inode *ino = inode ();
    u8 i, j, l;
    u8 p = 0;
    u32 pr;
    t_file_cb *cb = file_type (tmp_b);

    j = APP_USE_DEFAULT;
    if (cb != NULL)
      j = cb (tmp_b, FILE_DETAIL);

    if (j != APP_USE_DEFAULT)
      return j;

    r = t;
    // file/link/protected...
    get_inode (ino, tmp_b);
    if (ino->type == INODE_TYPE_FILE)
      r = sprints (r, t_d2);
    else if (ino->type == INODE_TYPE_PROTECTED_KEY_DATA)
      r = sprints (r, t_d4);
    else if (ino->type == INODE_TYPE_PROTECTED_DATA_LEN_TYPE)
      r = sprints (r, t_d5);
    else if (ino->type == INODE_TYPE_DIR)
      r = sprints (r, t_d13);

    // path
    pr = tmp_b;

    while (pr != root () && p < 255)
    {
      get_inode (ino, pr);
      l = strlen (ino->text);
      for (i = 0; i < l; i++)
        pth[p++] = ino->text[l - 1 - i];
      pth[p++] = '/';
      pr = ino_parent (ino);
    }

    r = sprints (r, t_d1);
    for (i = 0; i < p; i++)
      r = sprintc (r, pth[p - i - 1]);
    r = sprintc (r, '\n');

    r = sprints (r, t_d12);
    r = sprintch (r, fs_ino_block (tmp_b) >> 8);
    r = sprintch (r, fs_ino_block (tmp_b));
    r = sprintch (r, fs_ino_index (tmp_b));
    r = sprintc (r, '\n');

    get_inode (ino, tmp_b);
    if (ino->type == INODE_TYPE_FILE
        || ino->type == INODE_TYPE_PROTECTED_KEY_DATA)
    {
      // extension
      r = sprints (r, t_d6);
      mc_get_type (tmp_b, ext);
      if (ext[0] == 0 || ext[1] == 0 || ext[2] == 0)
        r = sprints (r, t_d8);
      else
        for (i = 0; i < 3; i++)
          r = sprintc (r, ext[i]);
      r = sprintc (r, '\n');
      // len
      r = sprints (r, t_d7);
      r = sprinti (r, get_len (tmp_b));
    }

    r = sprintc (r, '\0');
    if (display_text (t, NULL) == APP_END)
      return APP_END;
    return APP_BACK;
  }
  return APP_OK;
}

SNodeP mc_n_rename;
SNodeP mc_n_capacity;
SNodeP mc_n_detail;
SNodeP mc_n_select;
SNodeP mc_n_capacity;
SNodeP mc_n_protect;
SNodeP mc_n_unprotect;

u8 mc_df (SCtx * ctx, u8 action)
{
  if (action == APP_ENTER)
  {
    Inode *ino = inode ();

    tmp_b = ctx->eD;

    get_inode (ino, tmp_b);
    if (ino->type == INODE_TYPE_DIR)
    {
      cd_i (tmp_b);
      spider_append_r (ctx, &mc_n_detail);
      ctx->n_default = 2;
    }
    else
    {
      if (ino->type == INODE_TYPE_FILE)
        spider_append_r (ctx, &mc_n_protect);
      else if (ino->type == INODE_TYPE_PROTECTED_KEY_DATA
               || ino->type == INODE_TYPE_PROTECTED_DATA_LEN_TYPE)
        spider_append_r (ctx, &mc_n_unprotect);

      spider_append_r (ctx, &mc_n_detail);
      spider_append_r (ctx, &mc_n_select);
    }

  }
  else if (action == APP_LEAVE)
  {
    spider_clear_r (ctx);
  }
  return APP_OK;
}

u8 mc_capacity (SCtx * ctx, u8 action)
{
  if (action == APP_ENTER)
  {
    u8 *r;
    u8 *t = buf_B ();
    u8 i, l, j;
    u8 p = 0;

    r = t;
    r = sprints (r, locale (lc_Free));
    r = sprinti (r, mc_free ());
    r = sprintc (r, '/');
    r = sprinti (r, mc_pages () * mc_blocks ());
    r = sprints (r, locale (lc_Blocks));
    r = sprintc (r, '\n');

    r = sprintc (r, '\0');
    if (display_text (t, NULL) == APP_END)
      return APP_END;
    return APP_BACK;
  }
  return APP_OK;
}

u8 mc_ctx (SCtx * ctx, u8 action)
{
  if (action == APP_ENTER)
  {
    cd_i (root ());
    ctx->eD = pwd ();

    spider_append_r (ctx, &mc_n_rename);
    spider_append_r (ctx, &mc_n_capacity);
  }
  else if (action == APP_LEAVE)
  {
    spider_clear_r (ctx);
  }

  return APP_OK;
}

u8 mc_label (SCtx * ctx, u8 action)
{
  if (action == APP_ENTER)
  {
    u8 *res;
    u8 i;
    u8 lbl[15];
    u8 l1[15];

    i = strlen (locale (lc_Label));
    memcpy (lbl, locale (lc_Label), i + 1);
    mc_get_label (l1);
    res = get_input (lbl, 0, 14, l1, Q_GET_INPUT_ALPHABET);
    if (res == ENULL)
      return APP_END;
    if (res != NULL)
    {
      i = res[0] - 1;
      res++;                    //len
      res++;                    //dcs
      res[i] = '\0';
      mc_set_label (res);
      mc_get_label (label_node.text);
      set_up_menu ();
    }
    return APP_BACK;
  }
  return APP_OK;
}

SNodeP mc_n_rename = { lc_Label_Rename, mc_label };
SNodeP mc_n_capacity = { lc_Capacity, mc_capacity };
SNodeP mc_n_select = { lc_Select, mc_select };
SNodeP mc_n_detail = { lc_Detail, mc_detail };
SNodeP mc_n_protect = { LC_PROTECT, mc_protect };
SNodeP mc_n_unprotect = { LC_UNPROTECT, mc_unprotect };

void action_menu (void *x)
{
  SCtx *c = spider_init ();
  u8 i = mc_detect ();

  if (i != MC_OK)
  {
    if (i == MC_NOT_PRESENT)
      perror (ERR_MC_NOT_PRESENT);
    else
      perror (ERR_MC_NOT_READY);
    return;
  }

  if (c == NULL)
  {
    perror (ERR_NO_RAM);
    return;
  }

  c->n = &label_node;
  c->df_fnc = mc_df;
  c->style = SPIDER_STYLE_LS_AL;

  spider (c);
}

void turbo_handler (u8 action, void *data)
{
  switch (action)
  {
    case ACTION_APP_UNREGISTER:
      if (label_node.text)
        free (label_node.text);
      break;
    case ACTION_APP_INIT:
      label_node.text = malloc (15);
      if (label_node.text)
      {
        if (mc_detect () == MC_OK)
          mc_get_label (label_node.text);
        else
          memcpy (label_node.text, locale (lc_MC),
                  strlen (locale (lc_MC)) + 1);
      }
      break;
    case ACTION_INSERT_MENU:
      if (label_node.text)
        insert_menu (label_node.text);
      else
        insert_menu (locale (lc_MC));
      break;
    case ACTION_MENU_SELECTION:
      stk_thread (action_menu, NULL);
      break;
    default:
      break;
  }
}


Copyright © 2004-2006 BLADOX
Turbo version 1.2