PrintingServicesUnix.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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;
  60. string ppd_filename;
  61. PPD_FILE ppd;
  62. if (cups_installed == false || (printer == null) || (printer == String.Empty)) {
  63. return;
  64. }
  65. ptr = cupsGetPPD (printer);
  66. ppd_filename = Marshal.PtrToStringAnsi (ptr);
  67. ppd_handle = ppdOpenFile (ppd_filename);
  68. //Console.WriteLine ("File: {0}", ppd_filename);
  69. ppd = (PPD_FILE) Marshal.PtrToStructure (ppd_handle, typeof (PPD_FILE));
  70. settings.landscape_angle = ppd.landscape;
  71. settings.supports_color = (ppd.color_device == 0) ? false : true;
  72. ppdClose (ppd_handle);
  73. }
  74. internal override void LoadPrinterResolutions (string printer, PrinterSettings settings)
  75. {
  76. settings.PrinterResolutions.Clear ();
  77. LoadDefaultResolutions (settings.PrinterResolutions);
  78. }
  79. internal override void LoadPrinterPaperSizes (string printer, PrinterSettings settings)
  80. {
  81. IntPtr ptr, ppd_handle;
  82. string ppd_filename, real_name;
  83. PPD_FILE ppd;
  84. PPD_SIZE size;
  85. PaperSize ps;
  86. PaperKind kind = PaperKind.Custom;
  87. settings.PaperSizes.Clear ();
  88. ptr = cupsGetPPD (printer);
  89. ppd_filename = Marshal.PtrToStringAnsi (ptr);
  90. ppd_handle = ppdOpenFile (ppd_filename);
  91. ppd = (PPD_FILE) Marshal.PtrToStructure (ppd_handle, typeof (PPD_FILE));
  92. ptr = ppd.sizes;
  93. float w, h;
  94. for (int i = 0; i < ppd.num_sizes; i++) {
  95. size = (PPD_SIZE) Marshal.PtrToStructure (ptr, typeof (PPD_SIZE));
  96. real_name = GetPaperSizeName (ppd, size.name);
  97. ptr = new IntPtr (ptr.ToInt64 () + Marshal.SizeOf (size));
  98. w = size.width * 100 / 72;
  99. h = size.length * 100 / 72;
  100. ps = new PaperSize (real_name, (int) w, (int) h);
  101. // TODO: Convert from name to paper kind enum
  102. ps.SetKind (kind);
  103. settings.PaperSizes.Add (ps);
  104. }
  105. ppdClose (ppd_handle);
  106. }
  107. internal override bool StartPage (GraphicsPrinter gr)
  108. {
  109. return true;
  110. }
  111. internal override bool EndPage (GraphicsPrinter gr)
  112. {
  113. GdipGetPostScriptSavePage (gr.Hdc);
  114. return true;
  115. }
  116. string tmpfile;
  117. internal override bool EndDoc (GraphicsPrinter gr)
  118. {
  119. DOCINFO doc = (DOCINFO) doc_info[gr.Hdc];
  120. gr.Graphics.Dispose (); // Dispose object to force surface finish
  121. cupsPrintFile (doc.settings.PrinterName, doc.filename, doc.title, 0, IntPtr.Zero);
  122. doc_info.Remove (gr.Hdc);
  123. if (tmpfile != null) {
  124. try { File.Delete (tmpfile); }
  125. catch { }
  126. }
  127. return true;
  128. }
  129. internal override bool StartDoc (GraphicsPrinter gr, string doc_name, string output_file)
  130. {
  131. DOCINFO doc = (DOCINFO) doc_info[gr.Hdc];
  132. doc.title = doc_name;
  133. return true;
  134. }
  135. internal override IntPtr CreateGraphicsContext (PrinterSettings settings)
  136. {
  137. IntPtr graphics = IntPtr.Zero;
  138. string name;
  139. if (!settings.PrintToFile) {
  140. StringBuilder sb = new StringBuilder (1024);
  141. int length = sb.Capacity;
  142. cupsTempFile (sb, length);
  143. name = sb.ToString ();
  144. tmpfile = name;
  145. }
  146. else
  147. name = settings.PrintFileName;
  148. GdipGetPostScriptGraphicsContext (name,
  149. settings.DefaultPageSettings.PaperSize.Width / 100 * 72,
  150. settings.DefaultPageSettings.PaperSize.Height / 100 * 72,
  151. // Harcoded dpy's
  152. 300, 300, ref graphics);
  153. DOCINFO doc = new DOCINFO ();
  154. doc.filename = name.ToString();
  155. doc.settings = settings;
  156. doc_info.Add (graphics, doc);
  157. return graphics;
  158. }
  159. // Properties
  160. internal override PrinterSettings.StringCollection InstalledPrinters {
  161. get {
  162. int n_printers;
  163. IntPtr dests = IntPtr.Zero, ptr_printers, ptr_printer;
  164. string str;
  165. PrinterSettings.StringCollection col = new PrinterSettings.StringCollection (new string[] {});
  166. if (cups_installed == false)
  167. return col;
  168. n_printers = cupsGetDests (ref dests);
  169. ptr_printers = dests;
  170. for (int i = 0; i < n_printers; i++) {
  171. ptr_printer = (IntPtr) Marshal.ReadInt32 (ptr_printers);
  172. str = Marshal.PtrToStringAnsi (ptr_printer);
  173. Marshal.FreeHGlobal (ptr_printer);
  174. ptr_printers = new IntPtr (ptr_printers.ToInt64 () + 20 /*size of CUPS_DEST*/);
  175. col.Add (str);
  176. }
  177. Marshal.FreeHGlobal (dests);
  178. return col;
  179. }
  180. }
  181. internal override string DefaultPrinter {
  182. get {
  183. IntPtr str;
  184. if (cups_installed == false)
  185. return string.Empty;
  186. str = cupsGetDefault ();
  187. return Marshal.PtrToStringAnsi (str);
  188. }
  189. }
  190. // Private functions
  191. private string GetPaperSizeName (PPD_FILE ppd, string name)
  192. {
  193. string rslt = name;
  194. PPD_GROUP group;
  195. PPD_OPTION option;
  196. PPD_CHOICE choice;
  197. IntPtr ptr, ptr_opt, ptr_choice;
  198. ptr = ppd.groups;
  199. for (int i = 0; i < ppd.num_groups; i++) {
  200. group = (PPD_GROUP) Marshal.PtrToStructure (ptr, typeof (PPD_GROUP));
  201. //Console.WriteLine ("Size text:{0} name:{1} opts {2}", group.text, group.name, group.num_options);
  202. ptr = new IntPtr (ptr.ToInt64 () + Marshal.SizeOf (group));
  203. ptr_opt = group.options;
  204. for (int n = 0; n < group.num_options; n++) {
  205. option = (PPD_OPTION) Marshal.PtrToStructure (ptr_opt, typeof (PPD_OPTION));
  206. ptr_opt = new IntPtr (ptr_opt.ToInt64 () + Marshal.SizeOf (option));
  207. //Console.WriteLine (" key:{0} def:{1} text: {2}", option.keyword, option.defchoice, option.text);
  208. if (!option.keyword.Equals ("PageSize"))
  209. continue;
  210. ptr_choice = option.choices;
  211. for (int c = 0; c < option.num_choices; c++) {
  212. choice = (PPD_CHOICE) Marshal.PtrToStructure (ptr_choice, typeof (PPD_CHOICE));
  213. ptr_choice = new IntPtr (ptr_choice.ToInt64 () + Marshal.SizeOf (choice));
  214. //Console.WriteLine (" choice:{0} - text: {1}", choice.choice, choice.text);
  215. if (name.Equals (choice.choice)) {
  216. rslt = choice.text;
  217. break;
  218. }
  219. }
  220. }
  221. }
  222. return rslt;
  223. }
  224. // TODO
  225. internal override void GetPrintDialogInfo (string printer, ref string port, ref string type, ref string status, ref string comment)
  226. {
  227. status = "Ready";
  228. }
  229. //
  230. // DllImports
  231. //
  232. [DllImport("libcups", CharSet=CharSet.Ansi)]
  233. static extern int cupsGetDests (ref IntPtr dests);
  234. [DllImport("libcups", CharSet=CharSet.Ansi)]
  235. static extern IntPtr cupsTempFile (StringBuilder sb, int len);
  236. [DllImport("libcups", CharSet=CharSet.Ansi)]
  237. static extern IntPtr cupsGetDefault ();
  238. [DllImport("libcups", CharSet=CharSet.Ansi)]
  239. static extern int cupsPrintFile (string printer, string filename, string title, int num_options, IntPtr options);
  240. [DllImport("libcups", CharSet=CharSet.Ansi)]
  241. static extern IntPtr cupsGetPPD (string printer);
  242. [DllImport("libcups", CharSet=CharSet.Ansi)]
  243. static extern IntPtr ppdOpenFile (string filename);
  244. [DllImport("libcups")]
  245. static extern void ppdClose (IntPtr ppd);
  246. [DllImport("gdiplus.dll", CharSet=CharSet.Ansi)]
  247. static extern int GdipGetPostScriptGraphicsContext (string filename, int with, int height, double dpix, double dpiy, ref IntPtr graphics);
  248. [DllImport("gdiplus.dll")]
  249. static extern int GdipGetPostScriptSavePage (IntPtr graphics);
  250. //Struct
  251. public struct DOCINFO
  252. {
  253. public PrinterSettings settings;
  254. public string title;
  255. public string filename;
  256. }
  257. public struct PPD_SIZE
  258. {
  259. public int marked;
  260. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=42)]
  261. public string name;
  262. public float width;
  263. public float length;
  264. public float left;
  265. public float bottom;
  266. public float right;
  267. public float top;
  268. }
  269. public struct PPD_GROUP
  270. {
  271. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=40)]
  272. public string text;
  273. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=42)]
  274. public string name;
  275. public int num_options;
  276. public IntPtr options;
  277. public int num_subgroups;
  278. public IntPtr subgrups;
  279. }
  280. public struct PPD_OPTION
  281. {
  282. public byte conflicted;
  283. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=41)]
  284. public string keyword;
  285. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=41)]
  286. public string defchoice;
  287. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=81)]
  288. public string text;
  289. public int ui;
  290. public int section;
  291. public float order;
  292. public int num_choices;
  293. public IntPtr choices;
  294. }
  295. public struct PPD_CHOICE
  296. {
  297. public byte marked;
  298. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=41)]
  299. public string choice;
  300. [MarshalAs(UnmanagedType.ByValTStr, SizeConst=81)]
  301. public string text;
  302. public IntPtr code;
  303. public IntPtr option;
  304. }
  305. public struct PPD_FILE
  306. {
  307. public int language_level;
  308. public int color_device;
  309. public int variable_sizes;
  310. public int accurate_screens;
  311. public int contone_only;
  312. public int landscape;
  313. public int model_number;
  314. public int manual_copies;
  315. public int throughput;
  316. public int colorspace;
  317. public IntPtr patches;
  318. public int num_emulations;
  319. public IntPtr emulations;
  320. public IntPtr jcl_begin;
  321. public IntPtr jcl_ps;
  322. public IntPtr jcl_end;
  323. public IntPtr lang_encoding;
  324. public IntPtr lang_version;
  325. public IntPtr modelname;
  326. public IntPtr ttrasterizer;
  327. public IntPtr manufacturer;
  328. public IntPtr product;
  329. public IntPtr nickname;
  330. public IntPtr shortnickname;
  331. public int num_groups;
  332. public IntPtr groups;
  333. public int num_sizes;
  334. public IntPtr sizes;
  335. /* There is more data after this that we are not using*/
  336. }
  337. }
  338. }