2 * Copyright (C) 2012, The AROS Development Team. All rights reserved.
3 * Author: Jason S. McMullan <jason.mcmullan@gmail.com>
5 * Licensed under the AROS PUBLIC LICENSE (APL) Version 1.1
8 #include <aros/debug.h>
10 #include <exec/errors.h>
12 #include <proto/exec.h>
13 #include <proto/dos.h>
14 #include <proto/asl.h>
16 #include LC_LIBDEFS_FILE
18 #include "printtotool_intern.h"
20 static BPTR
OpenTool(struct PrintToToolBase
*PrintToToolBase
, CONST_STRPTR tool
)
24 if ((pipe_w
= Open("PIPE:*", MODE_NEWFILE
))) {
26 if (NameFromFH(pipe_w
, buff
, sizeof buff
)) {
27 if ((pipe_r
= Open(buff
, MODE_OLDFILE
))) {
28 BPTR con
= Open("CON://///AUTO/CLOSE/WAIT", MODE_NEWFILE
);
29 if (SystemTags(tool
, SYS_Input
, pipe_r
,
39 D(bug("%s: Can't open read side of %s\n", __func__
, buff
));
42 D(bug("%s: Can't get name of autopipe\n", __func__
));
46 D(bug("%s: Can't open autopipe\n", __func__
));
52 BOOL
pu_OpenFile(struct PrintToToolBase
*PrintToToolBase
,
53 struct PrintToToolUnit
*pu
)
55 struct FileRequester
*fr
;
58 ObtainSemaphore(&pu
->pu_Lock
);
59 if (!pu
->pu_FileReq
) {
60 fr
= AllocAslRequestTags(ASL_FileRequest
,
61 ASLFR_TitleText
,"Tool to print to...",
62 ASLFR_InitialDrawer
,"C:",
63 ASLFR_RejectIcons
, TRUE
,
66 D(bug("%s: Can't create requestor\n"));
67 ReleaseSemaphore(&pu
->pu_Lock
);
68 return IOERR_OPENFAIL
;
75 D(bug("fr = %p\n", fr
));
76 ok
= AslRequestTags(fr
, TAG_END
);
77 D(bug("ok = %d (%s %s)\n", ok
, fr
-fr_Drawer
, fr
->fr_File
));
83 BPTR lock
= Lock(fr
->fr_Drawer
, SHARED_LOCK
);
85 return IOERR_OPENFAIL
;
86 here
= CurrentDir(lock
);
88 here
= CurrentDir(BNULL
);
93 pu
->pu_File
= OpenTool(PrintToToolBase
, fr
->fr_File
);
97 D(bug("opened = %d\n", opened
));
102 if (pu
->pu_File
== BNULL
) {
103 ReleaseSemaphore(&pu
->pu_Lock
);
104 return IOERR_OPENFAIL
;
110 AROS_LH1(void, BeginIO
,
111 AROS_LHA(struct IOStdReq
*, io
, A1
),
112 struct PrintToToolBase
*, PrintToToolBase
, 5, PrintToTool
)
116 struct PrintToToolUnit
*pu
= (struct PrintToToolUnit
*)io
->io_Unit
;
119 D(bug("BeginIO: io_Command = %d\n", io
->io_Command
));
120 D(bug("pu_File=%p\n", BADDR(pu
->pu_File
)));
121 if (pu
->pu_File
== BNULL
) {
122 err
= pu_OpenFile(PrintToToolBase
, pu
);
124 D(bug("err = %d\n", err
));
130 switch (io
->io_Command
) {
136 err
= Write(pu
->pu_File
, io
->io_Data
, io
->io_Length
);
138 io
->io_Error
= IoErr();
146 io
->io_Error
= IOERR_NOCMD
;
151 if (!(io
->io_Flags
& IOF_QUICK
))
152 ReplyMsg(&io
->io_Message
);
158 AROS_LH1(LONG
, AbortIO
,
159 AROS_LHA(struct IORequest
*, io
, A1
),
160 struct PrintToToolBase
*, PrintToToolBase
, 6, PrintToTool
)
167 static int GM_UNIQUENAME(Init
)(struct PrintToToolBase
*PrintToToolBase
)
171 for (i
= 0; i
< PRINTTOTOOL_UNITS
; i
++) {
172 InitSemaphore(&PrintToToolBase
->pt_Unit
[i
].pu_Lock
);
175 if ((PrintToToolBase
->pt_DOSBase
= OpenLibrary("dos.library", 35))) {
176 if ((PrintToToolBase
->pt_AslBase
= OpenLibrary("asl.library", 36))) {
180 CloseLibrary(PrintToToolBase
->pt_DOSBase
);
186 static int GM_UNIQUENAME(Expunge
)(struct PrintToToolBase
*PrintToToolBase
)
190 for (i
= 0; i
< PRINTTOTOOL_UNITS
; i
++) {
191 FreeFileRequest(PrintToToolBase
->pt_Unit
[i
].pu_FileReq
);
194 CloseLibrary(PrintToToolBase
->pt_AslBase
);
195 CloseLibrary(PrintToToolBase
->pt_DOSBase
);
196 D(bug("expunged\n"));
201 static int GM_UNIQUENAME(Open
)(struct PrintToToolBase
*PrintToToolBase
, struct IOStdReq
* io
, ULONG unitnum
, ULONG flags
)
203 D(bug("open unit %d\n", unitnum
));
204 if (unitnum
< 0 || unitnum
>= PRINTTOTOOL_UNITS
)
207 io
->io_Unit
= (struct Unit
*)&PrintToToolBase
->pt_Unit
[unitnum
];
208 D(bug("io unit %p\n", io
->io_Unit
));
212 static int GM_UNIQUENAME(Close
)(struct PrintToToolBase
*PrintToToolBase
, struct IOStdReq
*io
)
214 struct PrintToToolUnit
*pu
= (struct PrintToToolUnit
*)io
->io_Unit
;
216 D(bug("close: %p , %p\n", pu
, BADDR(pu
->pu_File
)));
218 if (pu
->pu_File
== BNULL
)
223 ReleaseSemaphore(&pu
->pu_Lock
);
228 ADD2OPENDEV(GM_UNIQUENAME(Open
), 0)
229 ADD2CLOSEDEV(GM_UNIQUENAME(Close
), 0)
231 ADD2INITLIB(GM_UNIQUENAME(Init
), 0)
232 ADD2EXPUNGELIB(GM_UNIQUENAME(Expunge
), 0)