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 "printtofile_intern.h"
20 BOOL
pu_OpenFile(struct PrintToFileBase
*PrintToFileBase
,
21 struct PrintToFileUnit
*pu
)
23 struct FileRequester
*fr
;
26 ObtainSemaphore(&pu
->pu_Lock
);
27 if (!pu
->pu_FileReq
) {
28 fr
= AllocAslRequestTags(ASL_FileRequest
,
29 ASLFR_TitleText
,"File to print to...",
30 ASLFR_DoSaveMode
, TRUE
,
31 ASLFR_RejectIcons
, TRUE
,
34 ReleaseSemaphore(&pu
->pu_Lock
);
35 return IOERR_OPENFAIL
;
42 D(bug("fr = %p\n", fr
));
43 ok
= AslRequestTags(fr
, TAG_END
);
44 D(bug("ok = %d (%s %s)\n", ok
, fr
-fr_Drawer
, fr
->fr_File
));
50 BPTR lock
= Lock(fr
->fr_Drawer
, SHARED_LOCK
);
52 return IOERR_OPENFAIL
;
53 here
= CurrentDir(lock
);
55 here
= CurrentDir(BNULL
);
60 pu
->pu_File
= Open(fr
->fr_File
, MODE_NEWFILE
);
64 D(bug("pu->pu_File = %p\n", BADDR(pu
->pu_File
)));
69 if (pu
->pu_File
== BNULL
) {
70 ReleaseSemaphore(&pu
->pu_Lock
);
71 return IOERR_OPENFAIL
;
77 AROS_LH1(void, BeginIO
,
78 AROS_LHA(struct IOStdReq
*, io
, A1
),
79 struct PrintToFileBase
*, PrintToFileBase
, 5, PrintToFile
)
83 struct PrintToFileUnit
*pu
= (struct PrintToFileUnit
*)io
->io_Unit
;
86 D(bug("BeginIO: io_Command = %d\n", io
->io_Command
));
87 D(bug("pu_File=%p\n", BADDR(pu
->pu_File
)));
88 if (pu
->pu_File
== BNULL
) {
89 err
= pu_OpenFile(PrintToFileBase
, pu
);
91 D(bug("err = %d\n", err
));
97 switch (io
->io_Command
) {
103 err
= Write(pu
->pu_File
, io
->io_Data
, io
->io_Length
);
105 io
->io_Error
= IoErr();
113 io
->io_Error
= IOERR_NOCMD
;
118 if (!(io
->io_Flags
& IOF_QUICK
))
119 ReplyMsg(&io
->io_Message
);
125 AROS_LH1(LONG
, AbortIO
,
126 AROS_LHA(struct IORequest
*, io
, A1
),
127 struct PrintToFileBase
*, PrintToFileBase
, 6, PrintToFile
)
134 static int GM_UNIQUENAME(Init
)(struct PrintToFileBase
*PrintToFileBase
)
138 for (i
= 0; i
< PRINTTOFILE_UNITS
; i
++) {
139 InitSemaphore(&PrintToFileBase
->pf_Unit
[i
].pu_Lock
);
142 if ((PrintToFileBase
->pf_DOSBase
= OpenLibrary("dos.library", 35))) {
143 if ((PrintToFileBase
->pf_AslBase
= OpenLibrary("asl.library", 36))) {
147 CloseLibrary(PrintToFileBase
->pf_DOSBase
);
153 static int GM_UNIQUENAME(Expunge
)(struct PrintToFileBase
*PrintToFileBase
)
157 for (i
= 0; i
< PRINTTOFILE_UNITS
; i
++) {
158 FreeFileRequest(PrintToFileBase
->pf_Unit
[i
].pu_FileReq
);
161 CloseLibrary(PrintToFileBase
->pf_AslBase
);
162 CloseLibrary(PrintToFileBase
->pf_DOSBase
);
163 D(bug("expunged\n"));
168 static int GM_UNIQUENAME(Open
)(struct PrintToFileBase
*PrintToFileBase
, struct IOStdReq
* io
, ULONG unitnum
, ULONG flags
)
170 D(bug("open unit %d\n", unitnum
));
171 if (unitnum
< 0 || unitnum
>= PRINTTOFILE_UNITS
)
174 io
->io_Unit
= (struct Unit
*)&PrintToFileBase
->pf_Unit
[unitnum
];
175 D(bug("io unit %p\n", io
->io_Unit
));
179 static int GM_UNIQUENAME(Close
)(struct PrintToFileBase
*PrintToFileBase
, struct IOStdReq
*io
)
181 struct PrintToFileUnit
*pu
= (struct PrintToFileUnit
*)io
->io_Unit
;
183 D(bug("close: %p , %p\n", pu
, BADDR(pu
->pu_File
)));
185 if (pu
->pu_File
== BNULL
)
190 ReleaseSemaphore(&pu
->pu_Lock
);
195 ADD2OPENDEV(GM_UNIQUENAME(Open
), 0)
196 ADD2CLOSEDEV(GM_UNIQUENAME(Close
), 0)
198 ADD2INITLIB(GM_UNIQUENAME(Init
), 0)
199 ADD2EXPUNGELIB(GM_UNIQUENAME(Expunge
), 0)