PrintingServicesUnix.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. //
  2. // Copyright (C) 2005 Novell, Inc. http://www.novell.com
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. // Author:
  24. //
  25. // Jordi Mas i Hernandez, [email protected]
  26. //
  27. using System.Runtime.InteropServices;
  28. using System.Collections;
  29. using System.Drawing.Printing;
  30. using System.ComponentModel;
  31. using System.Drawing.Imaging;
  32. using System.Text;
  33. using System.IO;
  34. namespace System.Drawing.Printing
  35. {
  36. internal class PrintingServicesUnix : PrintingServices
  37. {
  38. private Hashtable doc_info = new Hashtable ();
  39. private bool cups_installed;
  40. internal PrintingServicesUnix ()
  41. {
  42. CheckCupsInstalled ();
  43. }
  44. private void CheckCupsInstalled ()
  45. {
  46. try {
  47. cupsGetDefault ();
  48. }
  49. catch (DllNotFoundException) {
  50. Console.WriteLine("libcups not found. To have printing support, you need cups installed");
  51. cups_installed = false;
  52. return;
  53. }
  54. cups_installed = true;
  55. }
  56. // Methods
  57. internal override void LoadPrinterSettings (string printer, PrinterSettings settings)
  58. {
  59. IntPtr ptr, ppd_handle, ptr_opt, ptr_choice;
  60. string ppd_filename;
  61. PPD_FILE ppd;
  62. PPD_OPTION option;
  63. PPD_CHOICE choice;
  64. if (cups_installed == false || (printer == null) || (printer == String.Empty))
  65. return;
  66. ptr = cupsGetPPD (printer);
  67. ppd_filename = Marshal.PtrToStringAnsi (ptr);
  68. ppd_handle = ppdOpenFile (ppd_filename);
  69. //Console.WriteLine ("File: {0}", ppd_filename);
  70. ppd = (PPD_FILE) Marshal.PtrToStructure (ppd_handle, typeof (PPD_FILE));
  71. settings.landscape_angle = ppd.landscape;
  72. settings.supports_color = (ppd.color_device == 0) ? false : true;
  73. // Default paper source
  74. ptr_opt = ppdFindOption (ppd_handle, "InputSlot");
  75. if (ptr_opt != IntPtr.Zero) {
  76. option = (PPD_OPTION) Marshal.PtrToStructure (ptr_opt, typeof (PPD_OPTION));
  77. ptr_choice = option.choices;
  78. for (int c = 0; c < option.num_choices; c++) {
  79. choice = (PPD_CHOICE) Marshal.PtrToStructure (ptr_choice, typeof (PPD_CHOICE));
  80. ptr_choice = new IntPtr (ptr_choice.ToInt64 () + Marshal.SizeOf (choice));
  81. if (choice.choice == option.defchoice) {
  82. foreach (PaperSource paper_source in settings.PaperSources) {
  83. if (paper_source.SourceName == choice.text) {
  84. settings.DefaultPageSettings.PaperSource = paper_source;
  85. break;
  86. }
  87. }
  88. break;
  89. }
  90. }
  91. }
  92. // Default paper size
  93. ptr_opt = ppdFindOption (ppd_handle, "PageSize");
  94. if (ptr_opt != IntPtr.Zero) {
  95. option = (PPD_OPTION) Marshal.PtrToStructure (ptr_opt, typeof (PPD_OPTION));
  96. ptr_choice = option.choices;
  97. for (int c = 0; c < option.num_choices; c++) {
  98. choice = (PPD_CHOICE) Marshal.PtrToStructure (ptr_choice, typeof (PPD_CHOICE));
  99. ptr_choice = new IntPtr (ptr_choice.ToInt64 () + Marshal.SizeOf (choice));
  100. if (choice.choice == option.defchoice) {
  101. foreach (PaperSize paper_size in settings.PaperSizes) {
  102. if (paper_size.PaperName == choice.text) {
  103. settings.DefaultPageSettings.PaperSize = paper_size;
  104. break;
  105. }
  106. }
  107. break;
  108. }
  109. }
  110. }
  111. ppdClose (ppd_handle);
  112. }
  113. internal override void LoadPrinterResolutions (string printer, PrinterSettings settings)
  114. {
  115. settings.PrinterResolutions.Clear ();
  116. LoadDefaultResolutions (settings.PrinterResolutions);
  117. }
  118. internal override void LoadPrinterPaperSizes (string printer, PrinterSettings settings)
  119. {
  120. IntPtr ptr, ppd_handle;
  121. string ppd_filename, real_name;
  122. PPD_FILE ppd;
  123. PPD_SIZE size;
  124. PaperSize ps;
  125. PaperKind kind = PaperKind.Custom;
  126. settings.PaperSizes.Clear ();
  127. ptr = cupsGetPPD (printer);
  128. ppd_filename = Marshal.PtrToStringAnsi (ptr);
  129. ppd_handle = ppdOpenFile (ppd_filename);
  130. ppd = (PPD_FILE) Marshal.PtrToStructure (ppd_handle, typeof (PPD_FILE));
  131. ptr = ppd.sizes;
  132. float w, h;
  133. for (int i = 0; i < ppd.num_sizes; i++) {
  134. size = (PPD_SIZE) Marshal.PtrToStructure (ptr, typeof (PPD_SIZE));
  135. real_name = GetPaperSizeName (ppd_handle, size.name);
  136. ptr = new IntPtr (ptr.ToInt64 () + Marshal.SizeOf (size));
  137. w = size.width * 100 / 72;
  138. h = size.length * 100 / 72;
  139. ps = new PaperSize (real_name, (int) w, (int) h);
  140. // TODO: Convert from name to paper kind enum
  141. ps.SetKind (kind);
  142. settings.PaperSizes.Add (ps);
  143. }
  144. ppdClose (ppd_handle);
  145. }
  146. internal override void LoadPrinterPaperSources (string printer, PrinterSettings settings)
  147. {
  148. IntPtr ptr, ppd_handle, ptr_opt, ptr_choice;
  149. string ppd_filename;
  150. PPD_OPTION option;
  151. PPD_CHOICE choice;
  152. if (cups_installed == false || (printer == null) || (printer == String.Empty))
  153. return;
  154. ptr = cupsGetPPD (printer);
  155. ppd_filename = Marshal.PtrToStringAnsi (ptr);
  156. ppd_handle = ppdOpenFile (ppd_filename);
  157. ptr_opt = ppdFindOption (ppd_handle, "InputSlot");
  158. if (ptr_opt == IntPtr.Zero) {
  159. ppdClose (ppd_handle);
  160. return;
  161. }
  162. option = (PPD_OPTION) Marshal.PtrToStructure (ptr_opt, typeof (PPD_OPTION));
  163. //Console.WriteLine (" OPTION key:{0} def:{1} text: {2}", option.keyword, option.defchoice, option.text);
  164. ptr_choice = option.choices;
  165. for (int c = 0; c < option.num_choices; c++) {
  166. choice = (PPD_CHOICE) Marshal.PtrToStructure (ptr_choice, typeof (PPD_CHOICE));
  167. ptr_choice = new IntPtr (ptr_choice.ToInt64 () + Marshal.SizeOf (choice));
  168. //Console.WriteLine (" choice:{0} - text: {1}", choice.choice, choice.text);
  169. settings.PaperSources.Add (new PaperSource (choice.text, PaperSourceKind.Custom));
  170. }
  171. ppdClose (ppd_handle);
  172. }
  173. internal override bool StartPage (GraphicsPrinter gr)
  174. {
  175. return true;
  176. }
  177. internal override bool EndPage (GraphicsPrinter gr)
  178. {
  179. GdipGetPostScriptSavePage (gr.Hdc);
  180. return true;
  181. }
  182. string tmpfile;
  183. internal override bool EndDoc (GraphicsPrinter gr)
  184. {
  185. DOCINFO doc = (DOCINFO) doc_info[gr.Hdc];
  186. gr.Graphics.Dispose (); // Dispose object to force surface finish
  187. cupsPrintFile (doc.settings.PrinterName, doc.filename, doc.title, 0, IntPtr.Zero);
  188. doc_info.Remove (gr.Hdc);
  189. if (tmpfile != null) {
  190. try { File.Delete (tmpfile); }
  191. catch { }
  192. }
  193. return true;
  194. }
  195. internal override bool StartDoc (GraphicsPrinter gr, string doc_name, string output_file)
  196. {
  197. DOCINFO doc = (DOCINFO) doc_info[gr.Hdc];
  198. doc.title = doc_name;
  199. return true;
  200. }
  201. internal override IntPtr CreateGraphicsContext (PrinterSettings settings)
  202. {
  203. IntPtr graphics = IntPtr.Zero;
  204. string name;
  205. if (!settings.PrintToFile) {
  206. StringBuilder sb = new StringBuilder (1024);
  207. int length = sb.Capacity;
  208. cupsTempFile (sb, length);
  209. name = sb.ToString ();
  210. tmpfile = name;
  211. }
  212. else
  213. name = settings.PrintFileName;
  214. GdipGetPostScriptGraphicsContext (name,
  215. settings.DefaultPageSettings.PaperSize.Width / 100 * 72,
  216. settings.DefaultPageSettings.PaperSize.Height / 100 * 72,
  217. // Harcoded dpy's
  218. 300, 300, ref graphics);
  219. DOCINFO doc = new DOCINFO ();
  220. doc.filename = name.ToString();
  221. doc.settings = settings;
  222. doc_info.Add (graphics, doc);
  223. return graphics;
  224. }
  225. // Properties
  226. internal override PrinterSettings.StringCollection InstalledPrinters {
  227. get {
  228. int n_printers;
  229. IntPtr dests = IntPtr.Zero, ptr_printers, ptr_printer;
  230. string str;
  231. PrinterSettings.StringCollection col = new PrinterSettings.StringCollection (new string[] {});
  232. if (cups_installed == false)
  233. return col;
  234. n_printers = cupsGetDests (ref dests);
  235. ptr_printers = dests;
  236. for (int i = 0; i < n_printers; i++) {
  237. ptr_printer = (IntPtr) Marshal.ReadInt32 (ptr_printers);
  238. str = Marshal.PtrToStringAnsi (ptr_printer);
  239. Marshal.FreeHGlobal (ptr_printer);
  240. ptr_printers = new IntPtr (ptr_printers.ToInt64 () + 20 /*size of CUPS_DEST*/);
  241. col.Add (str);
  242. }
  243. Marshal.FreeHGlobal (dests);
  244. return col;
  245. }
  246. }
  247. internal override string DefaultPrinter {
  248. get {
  249. IntPtr str;
  250. if (cups_installed == false)
  251. return string.Empty;
  252. str = cupsGetDefault ();
  253. return Marshal.PtrToStringAnsi (str);
  254. }
  255. }
  256. // Private functions
  257. private string GetPaperSizeName (IntPtr ppd, string name)
  258. {
  259. string rslt = name;
  260. PPD_OPTION option;
  261. PPD_CHOICE choice;
  262. IntPtr ptr_opt, ptr_choice;
  263. ptr_opt = ppdFindOption (ppd, "PageSize");
  264. if (ptr_opt == IntPtr.Zero) {
  265. return rslt;
  266. }
  267. option = (PPD_OPTION) Marshal.PtrToStructure (ptr_opt, typeof (PPD_OPTION));
  268. ptr_choice = option.choices;
  269. for (int c = 0; c < option.num_choices; c++) {
  270. choice = (PPD_CHOICE) Marshal.PtrToStructure (ptr_choice, typeof (PPD_CHOICE));
  271. ptr_choice = new IntPtr (ptr_choice.ToInt64 () + Marshal.SizeOf (choice));
  272. if (name.Equals (choice.choice)) {
  273. rslt = choice.text;
  274. break;
  275. }
  276. }
  277. return rslt;
  278. }
  279. // TODO
  280. internal override void GetPrintDialogInfo (string printer, ref string port, ref string type, ref string status, ref string comment)
  281. {
  282. status = "Ready";
  283. }
  284. //
  285. // DllImports
  286. //
  287. [DllImport("libcups", CharSet=CharSet.Ansi)]
  288. static extern int cupsGetDests (ref IntPtr dests);
  289. [DllImport("libcups", CharSet=CharSet.Ansi)]
  290. static extern IntPtr cupsTempFile (StringBuilder sb, int len);
  291. [DllImport("libcups", CharSet=CharSet.Ansi)]
  292. static extern IntPtr cupsGetDefault ();
  293. [DllImport("libcups", CharSet=CharSet.Ansi)]
  294. static extern int cupsPrintFile (string printer, string filename, string title, int num_options, IntPtr options);
  295. [DllImport("libcups", CharSet=CharSet.Ansi)]
  296. static extern IntPtr cupsGetPPD (string printer);
  297. [DllImport("libcups", CharSet=CharSet.Ansi)]
  298. static extern IntPtr ppdOpenFile (string filename);
  299. [DllImport("libcups", CharSet=CharSet.Ansi)]
  300. static extern IntPtr ppdFindOption (IntPtr ppd_file, string keyword);
  301. [DllImport("libcups")]
  302. static extern void ppdClose (IntPtr ppd);
  303. [DllImport("gdiplus.dll", CharSet=CharSet.Ansi)]
  304. static extern int GdipGetPostScriptGraphicsContext (string filename, int with, int height, double dpix, double dpiy, ref IntPtr graphics);
  305. [DllImport("gdiplus.dll")]
  306. static extern int GdipGetPostScriptSavePage (IntPtr graphics);
  307. //Struct
  308. public struct DOCINFO
  309. {
  310. public PrinterSettings settings;
  311. public string title;
  312. public string filename;
  313. }
  314. public struct PPD_SIZE
  315. {
  316. public int marked;
  317. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=42)]
  318. public string name;
  319. public float width;
  320. public float length;
  321. public float left;
  322. public float bottom;
  323. public float right;
  324. public float top;
  325. }
  326. public struct PPD_GROUP
  327. {
  328. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=40)]
  329. public string text;
  330. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=42)]
  331. public string name;
  332. public int num_options;
  333. public IntPtr options;
  334. public int num_subgroups;
  335. public IntPtr subgrups;
  336. }
  337. public struct PPD_OPTION
  338. {
  339. public byte conflicted;
  340. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=41)]
  341. public string keyword;
  342. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=41)]
  343. public string defchoice;
  344. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=81)]
  345. public string text;
  346. public int ui;
  347. public int section;
  348. public float order;
  349. public int num_choices;
  350. public IntPtr choices;
  351. }
  352. public struct PPD_CHOICE
  353. {
  354. public byte marked;
  355. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=41)]
  356. public string choice;
  357. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=81)]
  358. public string text;
  359. public IntPtr code;
  360. public IntPtr option;
  361. }
  362. public struct PPD_FILE
  363. {
  364. public int language_level;
  365. public int color_device;
  366. public int variable_sizes;
  367. public int accurate_screens;
  368. public int contone_only;
  369. public int landscape;
  370. public int model_number;
  371. public int manual_copies;
  372. public int throughput;
  373. public int colorspace;
  374. public IntPtr patches;
  375. public int num_emulations;
  376. public IntPtr emulations;
  377. public IntPtr jcl_begin;
  378. public IntPtr jcl_ps;
  379. public IntPtr jcl_end;
  380. public IntPtr lang_encoding;
  381. public IntPtr lang_version;
  382. public IntPtr modelname;
  383. public IntPtr ttrasterizer;
  384. public IntPtr manufacturer;
  385. public IntPtr product;
  386. public IntPtr nickname;
  387. public IntPtr shortnickname;
  388. public int num_groups;
  389. public IntPtr groups;
  390. public int num_sizes;
  391. public IntPtr sizes;
  392. /* There is more data after this that we are not using*/
  393. }
  394. }
  395. }