PrintingServicesUnix.cs 11 KB

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