2 Copyright © 2012, The AROS Development Team. All rights reserved.
5 Desc: Print multiple files with optional formfeed
9 #include <proto/exec.h>
10 #include <proto/dos.h>
11 #include <proto/alib.h>
12 #include <proto/datatypes.h>
13 #include <proto/icon.h>
15 #include <workbench/startup.h>
16 #include <datatypes/datatypesclass.h>
17 #include <datatypes/pictureclass.h>
20 #include <aros/debug.h>
22 #define USAGE "Usage: PrintFiles [-f] [-u N] file [file] [file...] (-f=formfeed -u=unit number)\n"
24 const char *vers
= "$VER: PrintFiles 1.1 (12.03.2012)";
26 static struct MsgPort
*mp
;
27 static union printerIO
*io
;
29 char __stdiowin
[]="CON:/30/400/100/PrintFiles/AUTO/CLOSE/WAIT";
32 static BOOL
initdevice(ULONG unit
)
34 D(bug("[PrintFiles] init unit %d\n", unit
));
36 if ((mp
= CreateMsgPort()))
38 if ((io
= CreateIORequest(mp
, sizeof(union printerIO
))))
40 if (0 == OpenDevice("printer.device", unit
, (struct IORequest
*)io
, 0))
46 Printf("Can't open printer.device %ld\n", unit
);
51 PutStr("Can't create IO request\n");
56 PutStr("Can't create message port\n");
62 static void cleanupdevice(void)
66 CloseDevice((struct IORequest
*)io
);
78 static void printfile(STRPTR filename
, BOOL formfeed
)
84 if ((o
= NewDTObject(filename
, PDTA_Remap
, FALSE
, TAG_END
)))
86 struct TagItem tags
[] = {
87 { DTA_Special
, SPECIAL_ASPECT
| SPECIAL_CENTER
},
90 msg
.MethodID
= DTM_PRINT
;
92 msg
.dtp_PIO
= (union printerIO
*)io
;
93 msg
.dtp_AttrList
= tags
;
94 D(bug("[PrintFiles] Trying to print %s\n", filename
));
95 if (0 == DoDTMethodA(o
, NULL
, NULL
, (Msg
)&msg
))
99 D(bug("[PrintFiles] Sending formfeed\n"));
100 io
->ios
.io_Length
= 1;
101 io
->ios
.io_Data
= "\x0C";
102 io
->ios
.io_Command
= CMD_WRITE
;
103 DoIO((struct IORequest
*)io
);
108 Printf("Failed to print %s\n", filename
);
116 static void read_icon(struct WBArg
*wbarg
, BOOL
*formfeed
, ULONG
*unit
)
118 struct DiskObject
*dobj
;
125 dobj
= GetDiskObject(wbarg
->wa_Name
);
128 toolarray
= dobj
->do_ToolTypes
;
130 if (FindToolType(toolarray
, "FORMFEED"))
134 result
= FindToolType(toolarray
, "UNIT");
137 StrToLong(result
, unit
);
139 FreeDiskObject(dobj
);
144 int main(int argc
, char **argv
)
147 BOOL formfeed
= FALSE
;
152 // started from Workbench
153 struct WBStartup
*wbmsg
= (struct WBStartup
*)argv
;
154 struct WBArg
*wbarg
= wbmsg
->sm_ArgList
;
155 BPTR olddir
= (BPTR
)-1;
157 D(bug("[PrintFiles] numargs %d wa_lock %lx wa_name %s\n", wbmsg
->sm_NumArgs
, wbarg
[0].wa_Lock
, wbarg
[0].wa_Name
));
158 if (wbmsg
->sm_NumArgs
> 1 && wbarg
[0].wa_Lock
&& *wbarg
[0].wa_Name
)
160 // handle program's icon
161 olddir
= CurrentDir(wbarg
->wa_Lock
);
162 read_icon(wbarg
, &formfeed
, &unit
);
163 if (olddir
!= (BPTR
)-1)
165 if (initdevice(unit
))
167 // handle project icons
168 for (i
= 1; i
< wbmsg
->sm_NumArgs
; i
++)
170 D(bug("[PrintFiles] i %d wa_lock %lx wa_name %s\n", i
, wbarg
[i
].wa_Lock
, wbarg
[i
].wa_Name
));
172 if ((wbarg
[i
].wa_Lock
) && (*wbarg
[i
].wa_Name
) )
174 olddir
= CurrentDir(wbarg
[i
].wa_Lock
);
176 printfile(wbarg
[i
].wa_Name
, formfeed
);
178 if (olddir
!= (BPTR
)-1)
189 if (argc
== 1 || argv
[1][0] == '?')
197 while (i
< argc
&& argv
[i
][0] == '-')
199 if (argv
[i
][1] == 'f')
203 else if (argv
[i
][1] == 'u' && (i
+ 1 < argc
))
206 StrToLong(argv
[i
], &unit
);
217 if (initdevice(unit
))
221 printfile(argv
[i
], formfeed
);