{--------------------------------------------------------}
{        Discavox - rotinas utilitarias
{--------------------------------------------------------}

unit disUtil;

interface
uses
    winTypes, winProcs, strings, shellApi,
    windos, dvcrt, dvwin, dvhora,
    disvars, dismsg, disfala, discomm;

procedure saiLink (s: string; espera: integer);
function discaTelefone (numeroTel: string): boolean;
procedure desligaTelefone (falando: boolean);
function executaProg (nomeProg, nomeDir, nomeArq: string): boolean;
procedure esperaProgVoltar;


implementation
var
    meuNome: array [0..144] of char;

{--------------------------------------------------------}
{                   sai string no link
{--------------------------------------------------------}

procedure saiLink (s: string; espera: integer);
var i: integer;
    c: char;
begin
    for i := 1 to length (s) do
        begin
            esclink (s[i]);
            delay (50);
        end;

    for i := 1 to espera div 100 do
        begin
            while chegouLink do
                begin
                    lelink(c);
                    write (c);
                    if c = #$0a then
                        sintclek;
                end;
            delay (100);
        end;
end;

{--------------------------------------------------------}
{                     disca telefone
{--------------------------------------------------------}

function discaTelefone (numeroTel: string): boolean;
var toneOuPulse: char;
    c: char;
    s: string;
label fim;
begin
    discaTelefone := false;

    toneOuPulse := 'T';
    if pulse then toneOuPulse := 'P';
    saiLink ('ATH1'+ENTER, 500);
    saiLink ('ATD'+ toneOuPulse + numeroTel+ENTER, 1000);
    {falaCondensada (true);}

    repeat
        s := '';
        repeat
            if chegoulink then
                begin
                    lelink (c);
                    write (c);
                    if c <> #$0a then
                        s := s + c;
                    acumulaPalavra (c);
                end;
        until (c = ENTER) or keypressed;

        if (copy (s, 1, 4)  = 'BUSY') or
           (copy (s, 1, 10) = 'NO CARRIER') or
           keypressed then
               goto fim;

    until copy (s, 1, 7) = 'CONNECT';

    discaTelefone := true;

fim:
    {falaCondensada (false);}
end;

{--------------------------------------------------------}
{                   desliga telefone
{--------------------------------------------------------}

procedure desligaTelefone (falando: boolean);
begin
    if falando then
        mensagem ('DIDESLIG', 1);   {...Desligando...}
    saiLink ('+++', 3000);
    saiLink ('ATH0'+ENTER, 500);
    saiLink ('ATS0=0'+ENTER, 500);
end;

{-------------------------------------------------------------}
{                       executa um programa
{-------------------------------------------------------------}

function executaProg (nomeProg, nomeDir, nomeArq: string): boolean;
var
    pprog, pdir, pparam: array [0..100] of char;
    erro: tHandle;

begin
    executaProg := true;
    strPcopy (pprog, nomeProg);
    strPCopy (pdir, nomeDir);
    strPcopy (pparam, nomeArq);

    GetWindowText(crtWindow, meuNome, 100);

    erro := ShellExecute(crtWindow, 'open', pprog, pparam, pdir, SW_SHOWNORMAL);
    if erro < 32 then
        begin
            textBackground (RED);
            mensagem ('CTNAOEXE', 0);  {'Não foi possível executar esse programa, erro #'}
            sintWriteInt (erro);
            textBackground (RED);
            writeln;
            executaProg := false;
        end;
end;

{--------------------------------------------------------}
{        espera o titulo da janela ficar o mesmo
{--------------------------------------------------------}

procedure esperaProgVoltar;
var nomeNovo: array [0..100] of char;
begin
    repeat
        delay (2000);   { nao remova este delay }
        GetWindowText (getFocus, nomeNovo, 100);
    until strComp (meuNome, nomeNovo) <> 0;

    repeat
        delay (1000);
        GetWindowText (getFocus, nomeNovo, 100);
    until strComp (meuNome, nomeNovo) = 0;
end;

begin
end.
