\NeedsTeXFormat{LaTeX2e}[1995/12/01]
\ProvidesPackage{rtsched}[2005/01/01]
\typeout{'rtsched' style for Latex and PSTricks}
\typeout{Easily draw real-time schedules in TeX/Latex.}
\typeout{(c) 2005, Giuseppe Lipari, Pisa, Italy}
\typeout{}

\RequirePackage{multido}
\RequirePackage{pstricks}
\RequirePackage{keyval}

%%
%% These can be changed at any time
%% 
\def\RTWindowLength{10cm}  % window length
\def\RTWindowHeight{4.5cm} % window height
\def\RTTaskLabelSize{8pt} % Size in pt of the task label
\def\RTNumberLabelSize{8pt} % Size in pt of the number labels
\def\RTTaskDefSymbol{\tau}
\def\RTDefNumberOffset{0}
\def\RTTaskDefFill{solid}
\def\RTTaskDefColor{gray}
\def\RTDefLineColor{black}
\def\RTDefXScale{1}
\def\RTDefWriteSymbols{0}
\def\RTExecLabel{\ }
\def\RTTaskSymbol{\RTTaskDefSymbol}
\def\RTTaskFill{\RTTaskDefFill}
\def\RTTaskColor{\RTTaskDefColor}
\def\RTLineColor{\RTDefLineColor}
\def\RTXScale{\RTDefXScale}
\def\RTWriteSymbols{\RTDefWriteSymbols}
\def\RTNumberOffset{\RTDefNumberOffset}

%%
%% temporaries
\newdimen\sx % slot length
\newdimen\sy % slot height
\newdimen\xx % x position
\newdimen\yy % y position
\newdimen\xxx % x position
\newdimen\yyy % y position
\newcount\nsx % number of horizontal slots
\newcount\nsy % number of vertical slots
\newcount\nhl % number of horizonatal lines
\newcount\nvl % number of vertical lines
\newcount\tmp % temp

\newcounter{myLabel}

\def\RTGrid{\@ifnextchar[\@mygrid@begin{\@mygrid@begin[]}}

\def\RTGridBegin{\@ifnextchar[\@mygrid@begin{\@mygrid@begin[]}}
\def\@mygrid@begin[#1]#2#3{\setkeys{RT}{#1}%
    \nsx = #3 \advance \nsx by 2 %
    \nvl = \nsx \advance \nvl by 1 %
    \sx = \RTWindowLength \divide \sx by \nsx %
    \nsy = #2 \multiply \nsy by 3 \advance \nsy by 1 %
    \sy = \RTWindowHeight \divide \sy by \nsy %
    \nhl = \nsy \advance \nhl by 1 %
    \advance \nsy by -1 \advance \nsx by -1
    \begin{pspicture}(0,0)(\RTWindowLength,\RTWindowHeight) %
      \multips(0,\sy)(0,\sy){\nsy}{\psline[linecolor=lightgray]{-}(0,0)(\RTWindowLength,0)}%
      \multips(\sx,0)(\sx,0){\nsx}{\psline[linecolor=lightgray]{-}(0,0)(0,\RTWindowHeight)}%
      \psframe(0,0)(\RTWindowLength,\RTWindowHeight)%
      \tmp = #3 \divide \tmp by 2 \advance \tmp by 1%
      \pssetlength{\xx}{\sx}%
      \setcounter{myLabel}{\RTNumberOffset}
      \multido{\n=0+1}{\tmp}{%
	\uput{4pt}[d](\xx,0){%
	  \usefont{T1}{phv}{m}{n}%
	  \fontsize{\RTNumberLabelSize}{\RTNumberLabelSize}%
	  \selectfont{\arabic{myLabel}}}%
	\psaddtolength{\xx}{\sx}%
	\psaddtolength{\xx}{\sx}%
	\addtocounter{myLabel}{2}
      }%
      %% Task Names
      \setcounter{myLabel}{1}
      \pssetlength{\yy}{\RTWindowHeight}%
      \psaddtolength{\yy}{-\sy}
      \multido{\n=0+1}{#2}{%
	\uput{4pt}[d](-\sx,\yy){
	  \usefont{T1}{phv}{m}{n}
	  \fontsize{\RTTaskLabelSize}{\RTTaskLabelSize}
	  \if\RTWriteSymbols\RTDefWriteSymbols\selectfont{$\RTTaskSymbol_{\arabic{myLabel}}$}
          \else\relax
          \fi
        }%
	\psaddtolength{\yy}{-3\sy}%
	\addtocounter{myLabel}{1}
      }%    
      %% Horizontal axes
      \xx = \sx \xxx = \RTWindowLength \advance \xxx by -\sx
      \yy = \sy
      \multido{\n=0+1}{#2}{
	\psline{->}(\xx,\yy)(\xxx,\yy)
	\advance \yy by 3\sy
      }
      \psframe(0,0)(\RTWindowLength,\RTWindowHeight)
}

\define@key{RT}{height}[6cm]{\def\RTWindowHeight{#1}}
\define@key{RT}{width}[11cm]{\def\RTWindowLength{#1}}
\define@key{RT}{labelsize}[8pt]{\def\RTTaskLabelSize{#1}}
\define@key{RT}{numbersize}[8pt]{\def\RTNumberLabelSize{#1}}
\define@key{RT}{symb}[{\tau}]{\def\RTTaskSymbol{#1}}
\define@key{RT}{color}[\RTTaskDefColor]{\def\RTTaskColor{#1}}
\define@key{RT}{linecolor}[\RTDefLineColor]{\def\RTLineColor{#1}}
\define@key{RT}{execlabel}[\ ]{\def\RTExecLabel{#1}}
\define@key{RT}{fillstyle}[\RTTaskDefFill]{\def\RTTaskFill{#1}}
\define@key{RT}{nocommand}{}
\define@key{RT}{xscale}[\RTDefXScale]{\def\RTXScale{#1}}
\define@key{RT}{nosymbols}[\RTDefWriteSymbols]{\def\RTWriteSymbols{#1}}
\define@key{RT}{numoffset}[\RTDefNumberOffset]{\def\RTNumberOffset{#1}}

\def\@RTDefaultValues{%
  \setkeys{RT}{height}%
  \setkeys{RT}{width}%
  \setkeys{RT}{labelsize}%
  \setkeys{RT}{numbersize}%
  \setkeys{RT}{symb}%
  \setkeys{RT}{fillstyle}%
  \setkeys{RT}{color}
  \setkeys{RT}{linecolor}%
  \setkeys{RT}{xscale}%
  \setkeys{RT}{nosymbols}%
}

\def\@RTExecDefaultValues{
  \setkeys{RT}{color}%
  \setkeys{RT}{execlabel}%
  \setkeys{RT}{linecolor}%
}

\def\RTGridEnd{
    \end{pspicture}%
    \@RTDefaultValues%
}

\def\endRTGrid{
    \end{pspicture}%
    \@RTDefaultValues%
}

\newcommand{\RTSet}[1]{
  \setkeys{RT}{#1}
}

\newcommand{\RTSetDefault}{
  \@RTDefaultValues
}

%% internal
\newcommand{\@compute@xx}[1]{
  \xx = #1\sx
  \divide \xx by \RTXScale
  \advance \xx by \sx
}

\newcommand{\@compute@xxx}[1]{
  \xxx = #1\sx
  \divide \xxx by \RTXScale
  \advance \xxx by \sx
}

\newcommand{\@compute@yy}[1]{
  \yy = -3\sy \multiply \yy by #1
  \advance \yy by \RTWindowHeight
}


%% Task Arrival: draws an up arrow at the specified slot
%% #1  : task number from 1 to n
%% #2  : slot number from 0 to l-1
\newcommand{\TaskArrival}[3][nocommand=1]{%
    \setkeys{RT}{#1}%
    \@compute@yy{#2}
    \@compute@xx{#3}
    \yyy = \yy \advance \yyy by 2\sy%
    \psline[linecolor=\RTLineColor]{->}(\xx,\yy)(\xx,\yyy)%
    \@RTExecDefaultValues%
}

%% Task Arrival and deadline: draws an up arrow and a down arrow at the specified slots
%% #1  : task number from 1 to n
%% #2  : slot number from 0 to l-1
%% #3  : relative deadline
\newcommand{\TaskArrDead}[4][nocommand=1]{
  \setkeys{RT}{#1}%
  \@compute@yy{#2}
  \@compute@xx{#3}
  \yyy = \yy \advance \yyy by 2\sy
  \psline[linecolor=\RTLineColor]{->}(\xx,\yy)(\xx,\yyy)
  \advance \xx by #4\sx
  \psline[linecolor=\RTLineColor]{->}(\xx,\yyy)(\xx,\yy)
  \@RTExecDefaultValues
}


%% Task Deadline: draws a down arrow at the specified slot
%% #1  : task number from 1 to n
%% #2  : slot number from 0 to l-1
\newcommand{\TaskDeadline}[3][nocommand=1]{
  \setkeys{RT}{#1}%
  \@compute@yy{#2}
  \@compute@xx{#3}
  \yyy = \yy \advance \yyy by 2\sy
  \psline[linecolor=\RTLineColor]{<-}(\xx,\yy)(\xx,\yyy)
  \@RTExecDefaultValues
}

\newcommand{\TaskExecution}[4][nocommand=1]{
  \setkeys{RT}{#1}%
  \@compute@yy{#2}
  \@compute@xx{#3}
  \yyy = \yy \advance \yyy by \sy
  \@compute@xxx{#4}
  \psframe[fillstyle=\RTTaskFill,fillcolor=\RTTaskColor,linecolor=\RTLineColor](\xx,\yy)(\xxx,\yyy)
  \advance \xx by \xxx \xx = .5\xx
  \uput{2pt}[u](\xx,\yy){%
    \usefont{T1}{phv}{m}{n}%
    \fontsize{\RTNumberLabelSize}{\RTNumberLabelSize}%
    \selectfont{\RTExecLabel}%
  }
  \@RTExecDefaultValues
}

% Task Lock
% par 1:  task 
% par 2:  time
% par 3:  resource
%
\newcommand{\TaskLock}[4][nocommand=1]{
  \setkeys{RT}{#1}%
  \@compute@yy{#2}
  \@compute@xx{#3}
  \yyy = \yy \advance \yyy by \sy
  \uput{2pt}[u](\xx,\yyy){%
    \usefont{T1}{phv}{m}{n}%
    \fontsize{\RTNumberLabelSize}{\RTNumberLabelSize}%
    \selectfont{L(#4)}%
  }
  \@RTExecDefaultValues
}

% \TaskUnlock
% par 1:  task 
% par 2:  time
% par 3:  resource
%
\newcommand{\TaskUnlock}[4][nocommand=1]{
  \setkeys{RT}{#1}%
  \@compute@yy{#2}
  \@compute@xx{#3}
  \yyy = \yy \advance \yyy by \sy
  \uput{2pt}[u](\xx,\yyy){%
    \usefont{T1}{phv}{m}{n}%
    \fontsize{\RTNumberLabelSize}{\RTNumberLabelSize}%
    \selectfont{U(#4)}%
  }
  \@RTExecDefaultValues
}

% 
% par 1:  task 
% par 2:  time
% par 3:  resource
%
\newcommand{\TaskLabel}[4][nocommand=1]{
  \setkeys{RT}{#1}%
  \@compute@yy{#2}
  \@compute@xx{#3}
  \yyy = \yy \advance \yyy by \sy
  \uput{2pt}[u](\xx,\yyy){%
    \usefont{T1}{phv}{m}{n}%
    \fontsize{\RTNumberLabelSize}{\RTNumberLabelSize}%
    \selectfont{{#4}}%
  }
  \@RTExecDefaultValues
}

% 
% par 1:  higher priority task (from) 
% par 2:  lower priority task (to)
% par 3:  time
%
\newcommand{\Inherit}[4][nocommand=1]{
  \setkeys{RT}{#1}%
  \@compute@yy{#2}
  \@compute@xx{#4}
  \yyy = -3\sy \multiply \yyy by #3 \advance \yyy by \sy
  \advance \yyy by \RTWindowHeight
  \psline[linestyle=dashed,linewidth=1.5pt]{->}(\xx,\yy)(\xx,\yyy)
  \@RTExecDefaultValues
}

% 
% par 1:  activating task (from) 
% par 2:  signal start time 
% par 3:  activated task (to)
% par 4:  signal arrival time
%
\newcommand{\Activation}[5][nocommand=1]{
  \setkeys{RT}{#1}%
  \@compute@yy{#2}
  \@compute@xx{#3}
  \yyy = -3\sy \multiply \yyy by #4 %\advance \yyy by \sy
  \advance \yyy by \RTWindowHeight
  \xxx = #5\sx %\advance \xxx by \sx
  \psline[linestyle=dashed,linewidth=1pt]{->}(\xx,\yy)(\xxx,\yyy)
}

% 
% par 1:  time1
% par 2:  time2
%
\newcommand{\RTBox}[3][nocommand=1]{
  \setkeys{RT}{#1}%
  \yy = 0cm
  \advance \yy by \RTWindowHeight
  \yyy = 0cm
  \@compute@xx{#2}
  \@compute@xxx{#3}
  \psframe[fillstyle=crosshatch,hatchwidth=.2pt,hatchsep=2pt,hatchcolor=lightgray,linewidth=0pt,framearc=.2](\xx,\yy)(\xxx,\yyy)
  \@RTExecDefaultValues
}

%
% par 1: task
% par 2: first instant (arrival)
% par 3: second instant (deadline)
%
\newcommand{\TaskInterval}[4][nocommand=1]{
  \setkeys{RT}{#1}%
  \@compute@yy{#2}
  \advance \yy by 2.5\sy
  \@compute@xx{#3}
  \@compute@xxx{#4}
  % da cambiare con psarc
  \psline[linecolor=\RTLineColor]{-}(\xx,\yy)(\xxx,\yy)
  \@RTExecDefaultValues  
}

\endinput

