#include <stdio.h>
#include <libraries/asl.h>
#include <libraries/reqtools.h>
#include <intuition/screens.h>
#include <graphics/Gfx.h>
#include <graphics/rastport.h>
#include <libraries/asl.h>
#include "TMP_MUI.h"
#include "structs.h"

struct Library *MUIMasterBase;

int __nocommandline=1;

extern char *VolList[];
extern char *LoadedVolList[];

APTR my_BitMapObject;

BOOL MUI3=TRUE;

char    *pubscr[256];
int     i;

struct tm_settings dftm =
{
    1L,
    20L,
    256L,
    0L,
    3,
    7,
    1,
    1,
    0,
    1,
    "topaz.font",
    8,
    TRUE,
    FALSE,
    "",
    "",
    0,
    0,
    0,
    0,
    0,
    1,
    0,
    0,

    1,

    "Chip",
    "Fast",
    "Retina",
    "Virtual",

    0,

    FALSE,
    0,

    0,

    8,
    5,
    8,

    FALSE,

    "%td Bytes free",

    FALSE,
    TRUE,
    TRUE,

    "Memory",

    "%tlc | %tlf",
    "%tc | %tf",

    FALSE,
    FALSE,

    TRUE,

    0,

    "%tkd KB, %p%% free",

    10,

    TRUE,
    TRUE,

    0,
    0,

    "TinyMeter_DIGITAL.font",
    17,
    "TinyMeter_DIGITAL.font",
    11,

    0,

    1,
    0,
    0,

    FALSE,

    TRUE,

    2,

    "CPU",
    "%p %% free",
    0,

    FALSE,
    FALSE

};

char stri_2[2048];
char str_2[16384];



struct Library *ReqToolsBase;

docol1(UBYTE col, struct ObjApp *app, UBYTE pen)
{
    char    *text[8]={"Additional Memory",
                 "Base Memory",
                 "Dummy Memory",
                 "Text in gauges",
                 "Other texts (Labels & Clock)",
                 "Gauge background",
                 "Background",
                  0};
    struct Screen   *my_screen;
    ULONG           colors[3];
    char            my_str[32];

    GetAttr(MUIA_Window_Screen,app->WI_Main,(ULONG *)&my_screen);
    GetRGB32(my_screen->ViewPort.ColorMap,col,1,colors);
    if(MUI3) sprintf_exec(&stri_2[(ULONG)pen*64],"\33I[2:p%ld] %s\0",col,text[pen]);
    else sprintf_exec(&stri_2[(ULONG)pen*64],"\33I[2:%08.8lx,%08.8lx,%08.8lx] %s\0",colors[0],colors[1],colors[2],text[pen]);
    DoMethod(app->LV_Colors,MUIM_List_InsertSingle,&stri_2[(ULONG)pen*64],MUIV_List_Insert_Bottom);
}


docol(UBYTE col, struct ObjApp *app, UBYTE pen)
{
    char    *text[8]={"Additional Memory",
                 "Base Memory",
                 "Dummy Memory",
                 "Text in gauges",
                 "Other texts (Labels & Clock)",
                 "Gauge background",
                 "Background",
                  0};
    struct Screen   *my_screen;
    ULONG           colors[3];
    char            my_str[32];

    GetAttr(MUIA_Window_Screen,app->WI_Main,(ULONG *)&my_screen);
    GetRGB32(my_screen->ViewPort.ColorMap,col,1,colors);
    if(MUI3) sprintf_exec(&stri_2[(ULONG)pen*64],"\33I[2:p%ld] %s\0",col,text[pen]);
    else sprintf_exec(&stri_2[(ULONG)pen*64],"\33I[2:%08.8lx,%08.8lx,%08.8lx] %s\0",colors[0],colors[1],colors[2],text[pen]);
}

putinset(struct ObjApp *app)
{
    ULONG  i;
    struct Screen   *my_screen;
    struct List     *PubScreenList;
    struct PubScreenNode *ScreenNode;
    char    *my_str,*my_str1;
    ULONG   *my_ptr;
    ULONG           colors[6];

    my_str=(char *)malloc(256L);
    my_str1=(char *)malloc(256L);

    DoMethod(app->LV_SelVolList,MUIM_List_Insert,LoadedVolList,-1,MUIV_List_Insert_Bottom);
    DoMethod(app->LV_AvailVolList,MUIM_List_Insert,VolList,-1,MUIV_List_Insert_Bottom);
    DoMethod(app->LV_AvailVolList,MUIM_List_Sort,0);
    set(app->LV_AvailVolList,MUIA_List_Active,MUIV_List_Active_Top);

    set(app->STR_WinPosX,MUIA_String_Integer,dftm.x_pos);
    set(app->STR_WinPosY,MUIA_String_Integer,dftm.y_pos);
    set(app->STR_WinSizX,MUIA_String_Integer,dftm.x_siz);

    docol1(dftm.add_mem_col,app,0);
    docol1(dftm.base_mem_col,app,1);
    docol1(dftm.miss_mem_col,app,2);
    docol1(dftm.lowl_col,app,3);
    docol1(dftm.text_col,app,4);
    docol1(dftm.back_col,app,5);
    docol1(dftm.all_back_col,app,6);

    GetAttr(MUIA_Window_Screen,app->WI_Main,(ULONG *)&my_screen);

    for(i=0;i<(1<<my_screen->RastPort.BitMap->Depth);i++)
    {
        GetRGB32(my_screen->ViewPort.ColorMap,i,1,&colors[0]);
        if(MUI3)sprintf_exec(&str_2[(ULONG)i*48],"\33I[2:p%ld]  Pen %ld\0",i,i);
        else sprintf_exec(&str_2[(ULONG)i*48],"\33I[2:%08.8lx,%08.8lx,%08.8lx]  Pen %ld\0",colors[0],colors[1],colors[2],i);
        DoMethod(app->LV_ACol,MUIM_List_InsertSingle,&str_2[(ULONG)i*48],MUIV_List_Insert_Bottom);
    }

    set(app->LV_Colors,MUIA_List_Active,MUIV_List_Active_Top);


    PubScreenList = LockPubScreenList();
    i=0;
    for(ScreenNode=(struct PubScreenNode *)PubScreenList->lh_Head;ScreenNode->psn_Node.ln_Succ;ScreenNode=(struct PubScreenNode *)ScreenNode->psn_Node.ln_Succ)
    {
        if(pubscr[i]==0)pubscr[i]=(char *)malloc(256L);
        strcpy(pubscr[i],ScreenNode->psn_Node.ln_Name);
        i++;

    }
    if(pubscr[i])free(pubscr[i]);
    pubscr[i]=0L;
    UnlockPubScreenList();
    DoMethod(app->LV_PublicScreen,MUIM_List_Insert,pubscr,-1,MUIV_List_Insert_Bottom);

    set(app->STR_GO_PublicScreen,MUIA_String_Contents,&dftm.pubscreen_name[0]);

    set(app->SL_MemRefresh,MUIA_Slider_Level,dftm.RefreshTime);
    set(app->SL_VolRefresh,MUIA_Slider_Level,dftm.VolRefreshTime);
    set(app->SL_Wait,MUIA_Slider_Level,dftm.WaitSeconds);
    set(app->SL_YSize,MUIA_Slider_Level,100+dftm.y_siz);

    set(app->CY_Moving,MUIA_Cycle_Active,dftm.WindowMove-1);
    set(app->CY_Winhandle,MUIA_Cycle_Active,dftm.WindowBackFront);

    set(app->STR_DescChipMem,MUIA_String_Contents,&dftm.ChipText[0]);
    set(app->STR_DescFastMem,MUIA_String_Contents,&dftm.FastText[0]);
    set(app->STR_DescMem,MUIA_String_Contents,&dftm.AllText[0]);
    set(app->STR_DescRetina,MUIA_String_Contents,&dftm.RetinaText[0]);
    set(app->STR_DescIdle,MUIA_String_Contents,&dftm.IdleText[0]);

    strcpy(my_str1,dftm.font_name);
    i=0;
    while((my_str1[i]!='.')&(my_str1[i]!=0)){i++;}
    my_str1[i]=0;
    sprintf_exec(my_str,"%s/%ld",my_str1,(ULONG)dftm.font_size);
    set(app->STR_PA_Font,MUIA_String_Contents,&my_str[0]);

    set(app->CY_BackPic,MUIA_Cycle_Active,dftm.back_pic);
    if(!dftm.back_pic)
    {
        set(app->STR_PA_BackPic,MUIA_Disabled, TRUE);
        set(app->PA_BackPic,    MUIA_Disabled, TRUE);
    }
    set(app->STR_PA_BackPic,MUIA_String_Contents,&dftm.background_picture[0]);
    set(app->RA_BorderType,MUIA_Radio_Active,(UWORD)dftm.border_double);
    set(app->CH_3dGauge,MUIA_Selected,dftm._3DGauge);
    set(app->CH_ShadowText,MUIA_Selected,dftm.ShadowText);
    set(app->CH_BorderGauges,MUIA_Selected,dftm.GaugesBorder);
    set(app->SL_BWidth,MUIA_Slider_Level,dftm.BorderX);
    set(app->SL_BHeight,MUIA_Slider_Level,dftm.BorderY);
    set(app->SL_Space,MUIA_Slider_Level,dftm.SpaceY);

    set(app->STR_MemFormat,MUIA_String_Contents,&dftm.TextFormat[0]);
    set(app->STR_VolFormat,MUIA_String_Contents,&dftm.VolumeTextFormat[0]);
    set(app->STR_IdleFormat,MUIA_String_Contents,&dftm.IdleFormat[0]);
    set(app->CY_MemIndent,MUIA_Cycle_Active,dftm.MemIndent);
    set(app->CY_VolIndent,MUIA_Cycle_Active,dftm.VolIndent);
    set(app->CY_IdleIndent,MUIA_Cycle_Active,dftm.IdleIndent);

    set(app->STR_Info1Format,MUIA_String_Contents,&dftm.InfoTextFormat_1[0]);
    set(app->STR_Info2Format,MUIA_String_Contents,&dftm.InfoTextFormat_2[0]);
    set(app->CY_InfoIndent,MUIA_Cycle_Active,dftm.InfIndent);

    strcpy(my_str1,dftm.Clockfont_name);
    i=0;
    while((my_str1[i]!='.')&(my_str1[i]!=0)){i++;}
    my_str1[i]=0;
    sprintf_exec(my_str,"%s/%ld",my_str1,(ULONG)dftm.Clockfont_size);
    set(app->STR_PA_DigitalClock,MUIA_String_Contents,&my_str[0]);

    strcpy(my_str1,dftm.Clocksmallfont_name);
    i=0;
    while((my_str1[i]!='.')&(my_str1[i]!=0)){i++;}
    my_str1[i]=0;
    sprintf_exec(my_str,"%s/%ld",my_str1,(ULONG)dftm.Clocksmallfont_size);
    set(app->STR_PA_DigitalDate,MUIA_String_Contents,&my_str[0]);

    set(app->CY_ClockIndent,MUIA_Cycle_Active,dftm.ClockIndent&15);
    set(app->CY_ClockPos,MUIA_Cycle_Active,(dftm.ClockIndent&16)>>4);

    set(app->CH_TextInGauges,MUIA_Selected,dftm.DisplayNums);
    set(app->CH_NoGauges,MUIA_Selected,dftm.NoGauges);
    set(app->CH_NoBase,MUIA_Selected,dftm.NoBaseMem);
    set(app->CH_TextInGauges,MUIA_Selected,dftm.DisplayNums);

    set(app->CH_DisplayMem,MUIA_Selected,dftm.DisplayAll);
    set(app->CH_DisplayChip,MUIA_Selected,dftm.DisplayChip);
    set(app->CH_DisplayFast,MUIA_Selected,dftm.DisplayFast);
    set(app->CH_DisplayRetina,MUIA_Selected,dftm.DisplayRetina);
    set(app->CH_DisplayInfo1,MUIA_Selected,dftm.DisplayInfo_1);
    set(app->CH_DisplayInfo2,MUIA_Selected,dftm.DisplayInfo_2);
    set(app->CH_DisIdle,MUIA_Selected,dftm.DisplayIdle);

    set(app->CH_DisplayClock,MUIA_Selected,dftm.DisplayDIGITAL);
    set(app->CH_DisplayDate,MUIA_Selected,dftm.DisplayDATE);
    set(app->RA_ClockFormat,MUIA_Radio_Active,(UWORD)dftm.ClockFormat);
    set(app->RA_DateFormat,MUIA_Radio_Active,(UWORD)dftm.DateFormat);

    set(app->SL_Colums,MUIA_Slider_Level,dftm.colums);

    free(my_str);
    free(my_str1);
}

getset(struct ObjApp *app)
{
    char    *my_str,*my_ptr;
    ULONG   my_lng;

    my_str=(char *)malloc(256L);

    GetAttr(MUIA_String_Contents,app->STR_GO_PublicScreen,(ULONG *)&my_ptr);strcpy(&dftm.pubscreen_name[0],my_ptr);

    GetAttr(MUIA_String_Integer,app->STR_WinPosX,(ULONG *)&my_lng);dftm.x_pos=(UWORD)my_lng;
    GetAttr(MUIA_String_Integer,app->STR_WinPosY,(ULONG *)&my_lng);dftm.y_pos=(UWORD)my_lng;
    GetAttr(MUIA_String_Integer,app->STR_WinSizX,(ULONG *)&my_lng);dftm.x_siz=(UWORD)my_lng;

    GetAttr(MUIA_Slider_Level,app->SL_MemRefresh,(ULONG *)&my_lng);dftm.RefreshTime=(UBYTE)my_lng;
    GetAttr(MUIA_Slider_Level,app->SL_VolRefresh,(ULONG *)&my_lng);dftm.VolRefreshTime=(UBYTE)my_lng;
    GetAttr(MUIA_Slider_Level,app->SL_Wait,&my_lng);dftm.WaitSeconds=(UBYTE)my_lng;

    GetAttr(MUIA_Cycle_Active,app->CY_Moving,(ULONG *)&my_lng);dftm.WindowMove=(UBYTE)my_lng+1;
    GetAttr(MUIA_Cycle_Active,app->CY_Winhandle,(ULONG *)&my_lng);dftm.WindowBackFront=(UBYTE)my_lng;

    GetAttr(MUIA_String_Contents,app->STR_DescChipMem,(ULONG *)&my_ptr);strcpy(&dftm.ChipText[0],my_ptr);
    GetAttr(MUIA_String_Contents,app->STR_DescFastMem,(ULONG *)&my_ptr);strcpy(&dftm.FastText[0],my_ptr);
    GetAttr(MUIA_String_Contents,app->STR_DescMem,(ULONG *)&my_ptr);strcpy(&dftm.AllText[0],my_ptr);
    GetAttr(MUIA_String_Contents,app->STR_DescRetina,(ULONG *)&my_ptr);strcpy(&dftm.RetinaText[0],my_ptr);
    GetAttr(MUIA_String_Contents,app->STR_DescIdle,(ULONG *)&my_ptr);strcpy(&dftm.IdleText[0],my_ptr);

    GetAttr(MUIA_String_Contents,app->STR_PA_Font,(ULONG *)&my_ptr);
    i=0;while((my_ptr[i]!='/')&(my_ptr[i]!=0)) { dftm.font_name[i]=my_ptr[i];i++;} dftm.font_name[i++]=0;
    strcat(dftm.font_name,".font");
    sscanf(&my_ptr[i],"%ld",&my_lng);dftm.font_size=(UWORD)my_lng;

    GetAttr(MUIA_String_Contents,app->STR_PA_BackPic,(ULONG *)&my_ptr);strcpy(&dftm.background_picture[0],my_ptr);
    GetAttr(MUIA_Cycle_Active,app->CY_BackPic,(ULONG *)&my_lng);dftm.back_pic=(WORD)my_lng;

    GetAttr(MUIA_Radio_Active,app->RA_BorderType,&my_lng);dftm.border_double=(UWORD)my_lng;
    GetAttr(MUIA_Selected,app->CH_3dGauge,&my_lng);dftm._3DGauge=(BOOL)my_lng;
    GetAttr(MUIA_Selected,app->CH_ShadowText,&my_lng);dftm.ShadowText=(BOOL)my_lng;
    GetAttr(MUIA_Selected,app->CH_BorderGauges,&my_lng);dftm.GaugesBorder=(BOOL)my_lng;
    GetAttr(MUIA_Slider_Level,app->SL_BWidth,&my_lng);dftm.BorderX=(UBYTE)my_lng;
    GetAttr(MUIA_Slider_Level,app->SL_BHeight,&my_lng);dftm.BorderY=(UBYTE)my_lng;
    GetAttr(MUIA_Slider_Level,app->SL_Space,&my_lng);dftm.SpaceY=(UBYTE)my_lng;
    GetAttr(MUIA_Slider_Level,app->SL_YSize,&my_lng);dftm.y_siz=((ULONG)my_lng-100L);

    GetAttr(MUIA_String_Contents,app->STR_MemFormat,(ULONG *)&my_ptr);strcpy(&dftm.TextFormat[0],my_ptr);
    GetAttr(MUIA_String_Contents,app->STR_VolFormat,(ULONG *)&my_ptr);strcpy(&dftm.VolumeTextFormat[0],my_ptr);
    GetAttr(MUIA_String_Contents,app->STR_IdleFormat,(ULONG *)&my_ptr);strcpy(&dftm.IdleFormat[0],my_ptr);
    GetAttr(MUIA_Cycle_Active,app->CY_MemIndent,(ULONG *)&my_lng);dftm.MemIndent=(UBYTE)my_lng;
    GetAttr(MUIA_Cycle_Active,app->CY_VolIndent,(ULONG *)&my_lng);dftm.VolIndent=(UBYTE)my_lng;
    GetAttr(MUIA_Cycle_Active,app->CY_IdleIndent,(ULONG *)&my_lng);dftm.IdleIndent=(UBYTE)my_lng;

    GetAttr(MUIA_String_Contents,app->STR_Info1Format,(ULONG *)&my_ptr);strcpy(&dftm.InfoTextFormat_1[0],my_ptr);
    GetAttr(MUIA_String_Contents,app->STR_Info2Format,(ULONG *)&my_ptr);strcpy(&dftm.InfoTextFormat_2[0],my_ptr);
    GetAttr(MUIA_Cycle_Active,app->CY_InfoIndent,&my_lng);dftm.InfIndent=(BOOL)my_lng;

    GetAttr(MUIA_String_Contents,app->STR_PA_DigitalClock,(ULONG *)&my_ptr);
    i=0;while((my_ptr[i]!='/')&(my_ptr[i]!=0)) { dftm.Clockfont_name[i]=my_ptr[i];i++;} dftm.Clockfont_name[i++]=0;
    strcat(dftm.Clockfont_name,".font");
    sscanf(&my_ptr[i],"%ld",&my_lng);dftm.Clockfont_size=(UWORD)my_lng;
    GetAttr(MUIA_String_Contents,app->STR_PA_DigitalDate,(ULONG *)&my_ptr);
    i=0;while((my_ptr[i]!='/')&(my_ptr[i]!=0)) { dftm.Clocksmallfont_name[i]=my_ptr[i];i++;} dftm.Clocksmallfont_name[i++]=0;
    strcat(dftm.Clocksmallfont_name,".font");
    sscanf(&my_ptr[i],"%ld",&my_lng);dftm.Clocksmallfont_size=(UWORD)my_lng;
    GetAttr(MUIA_Cycle_Active,app->CY_ClockIndent,&my_lng);dftm.ClockIndent=(UBYTE)my_lng;
    GetAttr(MUIA_Cycle_Active,app->CY_ClockPos,&my_lng);dftm.ClockIndent=dftm.ClockIndent|((UBYTE)(my_lng<<4));

    GetAttr(MUIA_Selected,app->CH_TextInGauges,&my_lng); dftm.DisplayNums    =(BOOL)my_lng;
    GetAttr(MUIA_Selected,app->CH_NoGauges,&my_lng);     dftm.NoGauges       =(BOOL)my_lng;
    GetAttr(MUIA_Selected,app->CH_NoBase,&my_lng);       dftm.NoBaseMem      =(BOOL)my_lng;
    GetAttr(MUIA_Selected,app->CH_TextInGauges,&my_lng); dftm.DisplayNums    =(BOOL)my_lng;

    GetAttr(MUIA_Selected,app->CH_DisplayMem,&my_lng);   dftm.DisplayAll     =(BOOL)my_lng;
    GetAttr(MUIA_Selected,app->CH_DisplayChip,&my_lng);  dftm.DisplayChip    =(BOOL)my_lng;
    GetAttr(MUIA_Selected,app->CH_DisplayFast,&my_lng);  dftm.DisplayFast    =(BOOL)my_lng;
    GetAttr(MUIA_Selected,app->CH_DisplayRetina,&my_lng);dftm.DisplayRetina  =(BOOL)my_lng;
    GetAttr(MUIA_Selected,app->CH_DisplayInfo1,&my_lng); dftm.DisplayInfo_1  =(BOOL)my_lng;
    GetAttr(MUIA_Selected,app->CH_DisplayInfo2,&my_lng); dftm.DisplayInfo_2  =(BOOL)my_lng;
    GetAttr(MUIA_Selected,app->CH_DisIdle,&my_lng);      dftm.DisplayIdle  =(BOOL)my_lng;

    GetAttr(MUIA_Selected,app->CH_DisplayClock,&my_lng); dftm.DisplayDIGITAL =(BOOL)my_lng;
    GetAttr(MUIA_Selected,app->CH_DisplayDate,&my_lng);  dftm.DisplayDATE    =(BOOL)my_lng;

    GetAttr(MUIA_Radio_Active,app->RA_ClockFormat,&my_lng); dftm.ClockFormat =(UWORD)my_lng;
    GetAttr(MUIA_Radio_Active,app->RA_DateFormat,&my_lng);  dftm.DateFormat  =(UWORD)my_lng;

    GetAttr(MUIA_Slider_Level,app->SL_Colums,&my_lng);dftm.colums=(UBYTE)my_lng;

    free(my_str);
}

savevols(int which,struct ObjApp *app)
{
    UBYTE   *my_file;
    char    *entry;

    my_file=(UBYTE *)Open("ENV:TinyMeter_Volumes",MODE_NEWFILE);
    if(my_file)
    {
        for (i=0;;i++)
        {
            DoMethod(app->LV_SelVolList,MUIM_List_GetEntry,i,&entry);
            if (!entry) break;
            FPuts(my_file,entry);
            FPutC(my_file,10L);
        }
        Close(my_file);

        if(which)
        {
            my_file=(UBYTE *)Open("ENVARC:TinyMeter_Volumes",MODE_NEWFILE);
            if(my_file)
            {
                for (i=0;;i++)
                {
                    DoMethod(app->LV_SelVolList,MUIM_List_GetEntry,i,&entry);
                    if (!entry) break;
                    FPuts(my_file,entry);
                    FPutC(my_file,10L);
                }
                Close(my_file);
            }
        }
    }

}

extern UWORD tmp_logoData[];
extern ULONG tmp_logoPaletteRGB32[];

int main()
{
    ULONG signals;
    BOOL running = TRUE;
    struct ObjApp *app;
    struct Window *tmpwin;
    UBYTE actcol;
    char *picturedata;
    struct BitMap my_bmp;


    APTR  my_VolList,my_LoadVolList;

/*    OpenTinyMeterPrefsCatalog(0L, 0L);*/

    if (!(MUIMasterBase = OpenLibrary(MUIMASTER_NAME,11)))
    {
        MUI3=FALSE;
        if (!(MUIMasterBase = OpenLibrary(MUIMASTER_NAME,MUIMASTER_VMIN)))
        {
          puts("Failed to open "MUIMASTER_NAME".");
          exit(1);
        }  
    }

    if(!(ReqToolsBase=(struct Library *)OpenLibrary("reqtools.library",0L)))
    {
        puts("Failed to open retools.library.");
        exit(1);
    }

    if(InitDoSettings())
    {
        load_settings();
        loadsavedvols();
        getsystemvols();

        if(!(picturedata=(char *)AllocVec(3456,MEMF_CHIP)))exit();

        CopyMem(&tmp_logoData[0],picturedata,3456);

        InitBitMap(&my_bmp,3,41,192);
        my_bmp.Planes[0]=&picturedata[0];
        my_bmp.Planes[1]=&picturedata[1152];
        my_bmp.Planes[2]=&picturedata[2304];

        my_BitMapObject= BitmapObject,
            MUIA_Bitmap_Bitmap,         &my_bmp,
            MUIA_Bitmap_Width,          41,
            MUIA_Bitmap_Height,         192,
            MUIA_Bitmap_SourceColors,   &tmp_logoPaletteRGB32[1],
            MUIA_FixHeight, 192,
            MUIA_FixWidth, 41,
        End;

        app = CreateApp();
        if (!app) 
        {
            puts("Failed to create Application.");
            exit(1);
        }  

        putinset(app);

        while (running)
        {
            switch (DoMethod(app->App,MUIM_Application_Input,&signals))
            {
              case  TM_CANCEL:
                    load_settings();
                    DoSettings(0L);
                    running = FALSE;
                    break;

              case  TM_SELCOL:
                    {
                        ULONG first,second;
                        GetAttr(MUIA_List_Active,app->LV_Colors,(ULONG *)&first);
                        GetAttr(MUIA_List_Active,app->LV_ACol,(ULONG *)&second);
                        switch (first)
                        {
                            case 0:
                                dftm.add_mem_col=(UBYTE)second;
                                break;
                            case 1:
                                dftm.base_mem_col=(UBYTE)second;
                                break;
                            case 2:
                                dftm.miss_mem_col=(UBYTE)second;
                                break;
                            case 3:
                                dftm.lowl_col=(UBYTE)second;
                                break;
                            case 4:
                                dftm.text_col=(UBYTE)second;
                                break;
                            case 5:
                                dftm.back_col=(UBYTE)second;
                                break;
                            case 6:
                                dftm.all_back_col=(UBYTE)second;
                                break;
                        }
                        docol(second,app,first);
                        DoMethod(app->LV_Colors,MUIM_List_Redraw,MUIV_List_Redraw_Active);
                    }
                    break;

              case  TM_ADDVOL:
                    {
                        char    *my_ptr;
                        DoMethod(app->LV_AvailVolList,MUIM_List_GetEntry,MUIV_List_GetEntry_Active,&my_ptr);
                        DoMethod(app->LV_SelVolList,MUIM_List_InsertSingle,my_ptr,MUIV_List_Insert_Bottom);
                    }
                    break;
              case  TM_REMVOL:
                    DoMethod(app->LV_SelVolList,MUIM_List_Remove,MUIV_List_Remove_Active);
                    break;
              case  TM_UPVOL:
                    DoMethod(app->LV_SelVolList,MUIM_List_Move,MUIV_List_Move_Active,MUIV_List_Move_Previous);
                    set(app->LV_SelVolList,MUIA_List_Active,MUIV_List_Active_Up);
                    break;
              case  TM_DOWNVOL:
                    DoMethod(app->LV_SelVolList,MUIM_List_Move,MUIV_List_Move_Active,MUIV_List_Move_Next);
                    set(app->LV_SelVolList,MUIA_List_Active,MUIV_List_Active_Down);
                    break;

              case  TM_SELPUB:
                    {
                        char    *my_ptr;
                        DoMethod(app->LV_PublicScreen,MUIM_List_GetEntry,MUIV_List_GetEntry_Active,&my_ptr);
                        set(app->STR_GO_PublicScreen,MUIA_String_Contents,my_ptr);
                    }
                    break;

              case  TM_SNAP:
                    tmpwin=(struct Window *)DoSettings(1L);
                    if(tmpwin)
                    {
                        set(app->STR_WinPosX,MUIA_String_Integer,tmpwin->LeftEdge);
                        set(app->STR_WinPosY,MUIA_String_Integer,tmpwin->TopEdge);
                        set(app->STR_WinSizX,MUIA_String_Integer,tmpwin->Width);
                    }
                    break;

              case  TM_TEST:
                    getset(app);
                    savevols(0,app);
                    DoSettings(0L);
                    break;
              case  TM_SAVE:
                    getset(app);
                    savevols(1,app);
                    save_settings(1);
                    DoSettings(0L);
                    running = FALSE;
                    break;
              case  TM_USE:
                    getset(app);
                    savevols(0,app);
                    save_settings(0);
                    DoSettings(0L);
                    running = FALSE;
                    break;
            }
            if (running && signals) Wait(signals);
        }

        DisposeApp(app);

        for(i=0;i<256;i++)if(pubscr[i])free(pubscr[i]);

        freevols();
        ClearDoSettings();
    }

    if (MUIMasterBase) CloseLibrary(MUIMasterBase);
    if (ReqToolsBase)  CloseLibrary((struct Library *)ReqToolsBase);

/*    CloseTinyMeterPrefsCatalog();*/
}

