;***************************
;*     Joymouse V1.0       *
;*                         *
;* CLI Kommando zum Setzen *
;* des Mausports und -typs *
;*                         *
;*    Marco Brandt  1994   *
;***************************

; Dieses Programm wurde nur fuer private Zwecke erstellt.
; Deswegen wird keinerlei Garantie fuer die Funktion uebernommen

start:
	move.l	d0,arglen        ;Laenge der Kommandozeile
	move.l	a0,argstring     ;Argument-String

	move.l	4,a6             ;Exec-Base
	sub.l	a1,a1            ;0 fuer eigenen Task
	jsr	-294(a6)         ;Exec: Findtask
	add.l	#92,d0           ;pr_Msgport (Nur bei Prozessen)
	lea	port(pc),a1
	move.l	d0,(a1)          ;In die IOreq-Struktur schreiben
	
	moveq	#0,d0
	moveq	#0,d1
	lea	devname(pc),a0   ;Name des input.device
	lea	iorequest(pc),a1
	jsr	-444(a6)         ;Exec: OpenDevice
	
	lea	dosname(pc),a1   ;dos.library oeffnen
	move.l	4,a6
	move.l	#0,d0
	jsr	-552(a6)	 ;Exec: Openlibrary
        move.l	d0,dosbase

	jsr	parsecommline    ;Unterprogramm zum Untersuchen der
	tst.l	d0               ;Kommandozeile
	beq	nochange


	move.w	#15,c		 ;Kommando: Maustyp setzen
	move.l	#mtype,dat       ;einer der GPCT_* Werte
	move.l	4,a6
	lea	iorequest(pc),a1
	jsr	-456(a6)	 ;Exec: DOIO

	move.w	#14,c            ;Kommando: Mausport setzen
	move.l	#mport,dat       ;Port 0 oder 1
	move.l	4,a6
	lea	iorequest(pc),a1
	jsr	-456(a6)       	 ;Exec: DOIO

nochange:
	move.l	4,a6             ;input.device schliessen
	lea	iorequest(pc),a1
	jsr	-450(a6)         ;Exec: Closedevice

	move.l	dosbase,a1       ;dos.library schliessen
	move.l	4,a6
	jsr	-414(a6)	 ;Exec: Closelibrary

	move.l	#0,d0   	 ;Returncode OK
	rts

parsecommline:
	move.l	argstring,a0     ;Kommandozeile wiederholen
	move.l	arglen,d0
	sub.l	#1,d0
	tst.l	d0
	beq	noargs           ;Aufruf ohne Argumente

l1:	tst.l	d0               ;Noch ein Argument da ?
	beq	nextparse1       ;wenn nicht, dann naechtes suchen
	cmp.b	#'0',(a0)
	beq	portset0         ;In der ganzen Kommandozeile nach
	add.l	#1,a0            ;einer 0 ausschau halten
	sub.l	#1,d0
        bra	l1

nextparse1:
	move.l	argstring,a0
	move.l	arglen,d0
	sub.l	#1,d0
l2:
	tst.l	d0             	 ;jetzt das gleiche mit einer 1
	beq	nextparse2
	cmp.b	#'1',(a0)
	beq	portset1
	add.l	#1,a0
	sub.l	#1,d0
        bra	l2

nextparse2:
	move.l	argstring,a0
	move.l	arglen,d0
	sub.l	#1,d0
l3:                            	 ;jetzt wird nach M oder m gesucht
	tst.l	d0
	beq	nextparse3
	cmp.b	#'m',(a0)
	beq	mouseset
	cmp.b	#'M',(a0)
	beq	mouseset
	add.l	#1,a0
	sub.l	#1,d0
        bra	l3

nextparse3:
	move.l	argstring,a0
	move.l	arglen,d0
	sub.l	#1,d0
l4:
	tst.l	d0               ;usw.
	beq	nextparse4
	cmp.b	#'r',(a0)
	beq	reljoyset
	cmp.b	#'R',(a0)
	beq	reljoyset
	add.l	#1,a0
	sub.l	#1,d0
        bra	l4

nextparse4:
	move.l	argstring,a0
	move.l	arglen,d0
	sub.l	#1,d0
l5:
	tst.l	d0
	beq	nextparse5
	cmp.b	#'a',(a0)
	beq	absjoyset
	cmp.b	#'A',(a0)
	beq	absjoyset
	add.l	#1,a0
	sub.l	#1,d0
        bra	l5

nextparse5:	
	jmp	noargs        ; hier wurden gar keine Argumete gefunden

endparse:
	move.l	#1,d0
	rts                   ;Argumente gefunden, erfolgreich zurueck

portset0:
	move.b	#0,mport      ;siehe Variable mport
	bra	nextparse2

portset1:
	move.b	#1,mport
	bra	nextparse2

mouseset:
	move.b	#1,mtype     ;siehe variable mtype
	bra	endparse

reljoyset:
	move.b	#2,mtype
	bra	endparse

absjoyset:
	move.b	#3,mtype
	bra	endparse





noargs:
	move.l	dosbase,a6	
	jsr	-60(a6)		;DOS: output
	move.l	d0,d1
	move.l	#usestring,d2   ;"Anleitung" Ausgeben
	move.l	#95,d3
	jsr	-48(a6)		;DOS: write
	move.l	4,a6
	move.l	#0,d0           ;0 zurueck, da sich nichts aendert
	rts

	
mtype:
	dc.b	0       ;Definiert in devices/gameport
	even            ;GPCT_MOUSE=1,GPCT_RELJOYSTICK=2
			;GPCT_ABSJOYSTICK=3,GPCT_NOCONTROLLER=0
			
mport:
	dc.b	0       ;0 oder 1 fuer den entsprechenden Port
	even

verstring:
	dc.b	0,'$VER: Joymouse 1.0 by Taurus (30.Okt.94)',0
	even

devname:
	dc.b	'input.device',0
	even

iorequest:
	dc.l	0	;hier ist eine node-struct
	dc.l	0
	dc.b	5	;type message?
	dc.b	0
	dc.l	0	;name
port:	dc.l	0	;reply-port
	dc.w	0	;laenge der message
	dc.l	0	;ab hier io(device)
	dc.l	0	;unit
c:	dc.w	0	;command
	dc.b	0	;flags
	dc.b	0	;error
	dc.l	0	;actual
	dc.l	1	;length
dat:	dc.l	0	;data
        dc.l	0	;offset
	even

dosbase:
	dc.l	0
	even

dosname:
	dc.b	'dos.library',0
	even
arglen:
	dc.l	0
	even
argstring:
	dc.l	0
	even

usestring:
	dc.b	10,'Joymouse V1.0 by Taurus-Software',10
	dc.b	'USAGE: joymouse  PORT   TYPE',10
	dc.b	'                [0/1]  [M/R/A]',10,10
	even

	end	
