;
;   Idle(namedescr,unit,idletime) -
;
;    returns the length of time in seconds that a terminal has been idle
;
;    Inputs -
;
;       namedescr - controller name string descriptor
;       unit      - address of unit number
;
;    Outputs -
;
;       idletime - idle time in seconds
;
;
	.Library  /SYS$LIBRARY:LIB.MLB/
	$ucbdef
	$ddbdef

lockaddrs:
	.address  idlekrnl
	.address  idlekrnlend

	.entry	idle,^M<r2>
	cmpl	(ap),#3
	bneq	badargs
	
	movl	4(ap),r2
	movl	4(r2),r2
	prober  #0,#10,(r2)
	beql	badargs

	$lkwset_s inadr=lockaddrs
	blbc	r0,err

	pushal	-20(sp)
	pushl	@8(ap)
	pushl	r2
	pushl	#3
	movl	sp,r2
	pushl	#0
	$cmkrnl_s routin=idlekrnl,arglst=(r2)
	movl	r0,r2
	
	$ulwset_s inadr=lockaddrs
	blbc	r0,err
	movl	r2,r0
	blbc	r0,err
	movl	(sp)+,@12(ap)
	movl	#1,r0
	ret

badargs:
	movl	#ss$_badparam,r0
	ret
	
err:	ret

	.entry	idlekrnl,^M<r2,r3,r10,r11>
	
	movl	ioc$gl_devlist,r11		; get head of ddb list
	brb	1$
4$:	movl	ddb$l_link(r11),r11		; skip to next ddb
1$:	tstl	r11				; done with ddb's ?
	beql	5$				; if so, return
	clrl	r1				; character index
	brb	6$
3$:	cmpb	@4(ap)[r1],ddb$t_name+1(r11)[r1] ; compare controller names
	bneq	4$				; go to next ddb if mismatch
	incl	r1				; next character
6$:	cmpb	r1,ddb$t_name(r11)		; is counted string exhausted?
	blssu	3$				; if not, check more
	movl	ddb$l_ucb(r11),r10		; get ucb head
	brb	10$				; loop through ucb's
12$:	cmpw	8(ap),ucb$w_unit(r10)		; do unit numbers match?
	beql	13$				; if so, we found it
	movl	ucb$l_link(r10),r10		; next ucb
10$:	tstl	r10				; is this list end?
	bneq	12$				; loop if not
5$:	movl	#ss$_nosuchdev,r0		; device not found
	ret					; back to user mode
;
;  This is the key section.  For a terminal UCB$L_DUETIM contains the
;  "abstim" at the time the last read was issued.  This is used for
;  timeout processing.  The idle time can be calculated by subtracting
;  UCB$L_DUETIM from the current "abstim" which is in EXE$GL_ABSTIM.
;  This only applies if UCB$V_BSY is set.  Therefore "idle time" is
;  the time since the last read was issued (if it still outstanding).
;
13$:	subl3	ucb$l_duetim(r10),exe$gl_abstim,r0
	bbs	#ucb$v_bsy,ucb$w_sts(r10),14$
	clrl	r0
14$:	movl	r0,@12(ap)
	movl	#1,r0
	ret

idlekrnlend:

	.end
