PrintingServicesUnix.cs 11 KB

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