You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

593 lines
22 KiB

  1. /*
  2. * libcucul .NET bindings for libcucul
  3. * Copyright (c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org>
  4. * 2007 Sam Hocevar <sam@zoy.org>
  5. * All Rights Reserved
  6. *
  7. * $Id$
  8. *
  9. * This library is free software. It comes without any warranty, to
  10. * the extent permitted by applicable law. You can redistribute it
  11. * and/or modify it under the terms of the Do What The Fuck You Want
  12. * To Public License, Version 2, as published by Sam Hocevar. See
  13. * http://sam.zoy.org/wtfpl/COPYING for more details.
  14. */
  15. using System;
  16. using System.Runtime.InteropServices;
  17. using System.Security;
  18. namespace Cucul
  19. {
  20. /* Static libcucul stuff that does not fit in any object */
  21. public static class Libcucul
  22. {
  23. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  24. SuppressUnmanagedCodeSecurity]
  25. private static extern int cucul_rand(int min, int max);
  26. public static int Rand(int min, int max)
  27. {
  28. return cucul_rand(min, max);
  29. }
  30. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  31. SuppressUnmanagedCodeSecurity]
  32. private static extern IntPtr cucul_get_version();
  33. public static string getVersion()
  34. {
  35. return Marshal.PtrToStringAnsi(cucul_get_version());
  36. }
  37. public const int BLACK = 0x00,
  38. BLUE = 0x01,
  39. GREEN = 0x02,
  40. CYAN = 0x03,
  41. RED = 0x04,
  42. MAGENTA = 0x05,
  43. BROWN = 0x06,
  44. LIGHTGRAY = 0x07,
  45. DARKGRAY = 0x08,
  46. LIGHTBLUE = 0x09,
  47. LIGHTGREEN = 0x0a,
  48. LIGHTCYAN = 0x0b,
  49. LIGHTRED = 0x0c,
  50. LIGHTMAGENTA = 0x0d,
  51. YELLOW = 0x0e,
  52. WHITE = 0x0f,
  53. DEFAULT = 0x10,
  54. TRANSPARENT = 0x20;
  55. public const int BOLD = 0x01,
  56. ITALICS = 0x02,
  57. UNDERLINE = 0x04,
  58. BLINK = 0x08;
  59. }
  60. public class CuculCanvas : IDisposable
  61. {
  62. public readonly IntPtr _cv;
  63. /* libcucul basic functions */
  64. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  65. SuppressUnmanagedCodeSecurity]
  66. private static extern IntPtr cucul_create_canvas(int w, int h);
  67. public CuculCanvas()
  68. {
  69. _cv = cucul_create_canvas(0, 0);
  70. }
  71. public CuculCanvas(int w, int h)
  72. {
  73. _cv = cucul_create_canvas(w, h);
  74. }
  75. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  76. SuppressUnmanagedCodeSecurity]
  77. private static extern int cucul_free_canvas(IntPtr cv);
  78. public void Dispose()
  79. {
  80. cucul_free_canvas(_cv);
  81. GC.SuppressFinalize(this);
  82. }
  83. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  84. SuppressUnmanagedCodeSecurity]
  85. private static extern int cucul_set_canvas_size(IntPtr cv,
  86. int w, int h);
  87. public void setSize(int w, int h)
  88. {
  89. cucul_set_canvas_size(_cv, w, h);
  90. }
  91. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  92. SuppressUnmanagedCodeSecurity]
  93. private static extern int cucul_get_canvas_width(IntPtr cv);
  94. public int width
  95. {
  96. get { return cucul_get_canvas_width(_cv); }
  97. set { cucul_set_canvas_size(_cv, value,
  98. cucul_get_canvas_height(_cv)); }
  99. }
  100. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  101. SuppressUnmanagedCodeSecurity]
  102. private static extern int cucul_get_canvas_height(IntPtr cv);
  103. public int height
  104. {
  105. get { return cucul_get_canvas_height(_cv); }
  106. set { cucul_set_canvas_size(_cv, cucul_get_canvas_width(_cv),
  107. value); }
  108. }
  109. /* canvas drawing */
  110. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  111. SuppressUnmanagedCodeSecurity]
  112. private static extern int cucul_gotoxy(IntPtr cv, int x, int y);
  113. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  114. SuppressUnmanagedCodeSecurity]
  115. private static extern int cucul_get_cursor_x(IntPtr cv);
  116. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  117. SuppressUnmanagedCodeSecurity]
  118. private static extern int cucul_get_cursor_y(IntPtr cv);
  119. public int cursorX
  120. {
  121. get { return cucul_get_cursor_x(_cv); }
  122. set { cucul_gotoxy(_cv, value, cucul_get_cursor_y(_cv)); }
  123. }
  124. public int cursorY
  125. {
  126. get { return cucul_get_cursor_y(_cv); }
  127. set { cucul_gotoxy(_cv, cucul_get_cursor_x(_cv), value); }
  128. }
  129. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  130. SuppressUnmanagedCodeSecurity]
  131. private static extern int cucul_put_char(IntPtr cv,
  132. int x, int y, int c);
  133. public int putChar(int x, int y, int c)
  134. {
  135. return cucul_put_char(_cv, x, y, c);
  136. }
  137. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  138. SuppressUnmanagedCodeSecurity]
  139. private static extern int cucul_get_char(IntPtr cv, int x, int y);
  140. public int getChar(int x, int y)
  141. {
  142. return cucul_get_char(_cv, x, y);
  143. }
  144. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  145. SuppressUnmanagedCodeSecurity]
  146. private static extern int cucul_put_str(IntPtr cv,
  147. int x, int y, string c);
  148. public int putStr(int x, int y, string c)
  149. {
  150. return cucul_put_str(_cv, x, y, c);
  151. }
  152. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  153. SuppressUnmanagedCodeSecurity]
  154. private static extern int cucul_get_attr(IntPtr cv, int x, int y);
  155. public int getAttr(int x, int y)
  156. {
  157. return cucul_get_attr(_cv, x, y);
  158. }
  159. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  160. SuppressUnmanagedCodeSecurity]
  161. private static extern int cucul_set_attr(IntPtr cv, int a);
  162. public int setAttr(int a)
  163. {
  164. return cucul_set_attr(_cv, a);
  165. }
  166. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  167. SuppressUnmanagedCodeSecurity]
  168. private static extern int cucul_put_attr(IntPtr cv,
  169. int x, int y, int a);
  170. public int putAttr(int x, int y, int a)
  171. {
  172. return cucul_put_attr(_cv, x, y, a);
  173. }
  174. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  175. SuppressUnmanagedCodeSecurity]
  176. private static extern int cucul_set_color_ansi(IntPtr cv,
  177. byte fg, byte bg);
  178. public int setColorAnsi(int fg, int bg)
  179. {
  180. return cucul_set_color_ansi(_cv, (byte)fg, (byte)bg);
  181. }
  182. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  183. SuppressUnmanagedCodeSecurity]
  184. private static extern int cucul_set_color_argb(IntPtr cv,
  185. int fg, int bg);
  186. public int setColorArgb(int fg, int bg)
  187. {
  188. return cucul_set_color_argb(_cv, fg, bg);
  189. }
  190. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  191. SuppressUnmanagedCodeSecurity]
  192. private static extern int cucul_clear_canvas(IntPtr cv);
  193. public int Clear()
  194. {
  195. return cucul_clear_canvas(_cv);
  196. }
  197. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  198. SuppressUnmanagedCodeSecurity]
  199. private static extern int cucul_set_canvas_handle(IntPtr cv,
  200. int x, int y);
  201. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  202. SuppressUnmanagedCodeSecurity]
  203. private static extern int cucul_get_canvas_handle_x(IntPtr cv);
  204. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  205. SuppressUnmanagedCodeSecurity]
  206. private static extern int cucul_get_canvas_handle_y(IntPtr cv);
  207. public int handleX
  208. {
  209. get { return cucul_get_canvas_handle_x(_cv); }
  210. set { cucul_set_canvas_handle(_cv, value,
  211. cucul_get_canvas_handle_y(_cv)); }
  212. }
  213. public int handleY
  214. {
  215. get { return cucul_get_canvas_handle_y(_cv); }
  216. set { cucul_set_canvas_handle(_cv, cucul_get_canvas_handle_x(_cv),
  217. value); }
  218. }
  219. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  220. SuppressUnmanagedCodeSecurity]
  221. private static extern int cucul_blit(IntPtr cv, int x, int y,
  222. IntPtr cv1, IntPtr cv2);
  223. public int Blit(int x, int y, CuculCanvas canvas)
  224. {
  225. return cucul_blit(_cv, x, y, canvas._cv, IntPtr.Zero);
  226. }
  227. public int Blit(int x, int y, CuculCanvas cv, CuculCanvas mask)
  228. {
  229. return cucul_blit(_cv, x, y, cv._cv, mask._cv);
  230. }
  231. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  232. SuppressUnmanagedCodeSecurity]
  233. private static extern int cucul_set_canvas_boundaries(IntPtr cv,
  234. int x, int y,
  235. int h, int w);
  236. public int setBoundaries(int x, int y, int h, int w)
  237. {
  238. return cucul_set_canvas_boundaries(_cv, x, y, h, w);
  239. }
  240. /* canvas transformation */
  241. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  242. SuppressUnmanagedCodeSecurity]
  243. private static extern int cucul_invert(IntPtr cv);
  244. public int Invert()
  245. {
  246. return cucul_invert(_cv);
  247. }
  248. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  249. SuppressUnmanagedCodeSecurity]
  250. private static extern int cucul_flip(IntPtr cv);
  251. public int Flip()
  252. {
  253. return cucul_flip(_cv);
  254. }
  255. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  256. SuppressUnmanagedCodeSecurity]
  257. private static extern int cucul_flop(IntPtr cv);
  258. public int Flop()
  259. {
  260. return cucul_flop(_cv);
  261. }
  262. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  263. SuppressUnmanagedCodeSecurity]
  264. private static extern int cucul_rotate_180(IntPtr cv);
  265. public int Rotate180()
  266. {
  267. return cucul_rotate_180(_cv);
  268. }
  269. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  270. SuppressUnmanagedCodeSecurity]
  271. private static extern int cucul_rotate_left(IntPtr cv);
  272. public int RotateLeft()
  273. {
  274. return cucul_rotate_left(_cv);
  275. }
  276. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  277. SuppressUnmanagedCodeSecurity]
  278. private static extern int cucul_rotate_right(IntPtr cv);
  279. public int RotateRight()
  280. {
  281. return cucul_rotate_right(_cv);
  282. }
  283. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  284. SuppressUnmanagedCodeSecurity]
  285. private static extern int cucul_stretch_left(IntPtr cv);
  286. public int StretchLeft()
  287. {
  288. return cucul_stretch_left(_cv);
  289. }
  290. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  291. SuppressUnmanagedCodeSecurity]
  292. private static extern int cucul_stretch_right(IntPtr cv);
  293. public int StretchRight()
  294. {
  295. return cucul_stretch_right(_cv);
  296. }
  297. /* primitives drawing */
  298. /* FIXME: highly incomplete */
  299. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  300. SuppressUnmanagedCodeSecurity]
  301. private static extern int cucul_draw_line(IntPtr cv, int x1, int y1, int x2, int y2, int c);
  302. public int drawLine(int x1, int y1, int x2, int y2, int c)
  303. {
  304. return cucul_draw_line(_cv, x1, y1, x2, y2, c);
  305. }
  306. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  307. SuppressUnmanagedCodeSecurity]
  308. private static extern int cucul_draw_polyline(IntPtr cv, int[] x, int[] y, int n, IntPtr c);
  309. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  310. SuppressUnmanagedCodeSecurity]
  311. private static extern int cucul_draw_thin_line(IntPtr cv, int x1, int y1, int x2, int y2);
  312. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  313. SuppressUnmanagedCodeSecurity]
  314. private static extern int cucul_draw_thin_polyline(IntPtr cv, int[] x, int[] y, int n);
  315. /* frame handling */
  316. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  317. SuppressUnmanagedCodeSecurity]
  318. private static extern int cucul_get_frame_count(IntPtr cv);
  319. public int getFrameCount()
  320. {
  321. return cucul_get_frame_count(_cv);
  322. }
  323. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  324. SuppressUnmanagedCodeSecurity]
  325. private static extern int cucul_set_frame(IntPtr cv, int f);
  326. public int setFrame(int f)
  327. {
  328. return cucul_set_frame(_cv, f);
  329. }
  330. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  331. SuppressUnmanagedCodeSecurity]
  332. private static extern string cucul_get_frame_name(IntPtr cv);
  333. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  334. SuppressUnmanagedCodeSecurity]
  335. private static extern int cucul_set_frame_name(IntPtr cv, string n);
  336. public string frameName
  337. {
  338. get { return cucul_get_frame_name(_cv); }
  339. set { cucul_set_frame_name(_cv, value); }
  340. }
  341. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  342. SuppressUnmanagedCodeSecurity]
  343. private static extern int cucul_create_frame(IntPtr cv, int f);
  344. public int createFrame(int f)
  345. {
  346. return cucul_create_frame(_cv, f);
  347. }
  348. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  349. SuppressUnmanagedCodeSecurity]
  350. private static extern int cucul_free_frame(IntPtr cv, int f);
  351. public int freeFrame(int f)
  352. {
  353. return cucul_free_frame(_cv, f);
  354. }
  355. /* bitmap dithering */
  356. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  357. SuppressUnmanagedCodeSecurity]
  358. private static extern int cucul_dither_bitmap(IntPtr c, int x, int y,
  359. int w, int h,
  360. IntPtr d, IntPtr data);
  361. public int ditherBitmap(int x, int y, int w, int h, CuculDither d,
  362. object data)
  363. {
  364. GCHandle gch = GCHandle.Alloc(data, GCHandleType.Pinned);
  365. int ret = cucul_dither_bitmap(_cv, x, y, w, h, d._dither,
  366. gch.AddrOfPinnedObject());
  367. gch.Free();
  368. return ret;
  369. }
  370. }
  371. public class CuculAttr
  372. {
  373. private int _attr;
  374. public CuculAttr(int attr)
  375. {
  376. attr = _attr;
  377. }
  378. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  379. SuppressUnmanagedCodeSecurity]
  380. private static extern byte cucul_attr_to_ansi(Int32 a);
  381. public byte toAnsi()
  382. {
  383. return cucul_attr_to_ansi(_attr);
  384. }
  385. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  386. SuppressUnmanagedCodeSecurity]
  387. private static extern byte cucul_attr_to_ansi_fg(Int32 a);
  388. public byte toAnsiFg()
  389. {
  390. return cucul_attr_to_ansi_fg(_attr);
  391. }
  392. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  393. SuppressUnmanagedCodeSecurity]
  394. private static extern byte cucul_attr_to_ansi_bg(Int32 a);
  395. public byte toAnsiBg()
  396. {
  397. return cucul_attr_to_ansi_bg(_attr);
  398. }
  399. }
  400. public class CuculDither : IDisposable
  401. {
  402. public readonly IntPtr _dither;
  403. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  404. SuppressUnmanagedCodeSecurity]
  405. private static extern IntPtr cucul_create_dither(int bpp, int w,
  406. int h, int pitch,
  407. ulong rmask,
  408. ulong gmask,
  409. ulong bmask,
  410. ulong amask);
  411. public CuculDither(int bpp, int w,int h, int pitch,
  412. uint rmask, uint gmask, uint bmask, uint amask)
  413. {
  414. _dither = cucul_create_dither(bpp, w, h, pitch,
  415. rmask, gmask, bmask, amask);
  416. }
  417. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  418. SuppressUnmanagedCodeSecurity]
  419. private static extern int cucul_free_dither(IntPtr d);
  420. public void Dispose()
  421. {
  422. cucul_free_dither(_dither);
  423. GC.SuppressFinalize(this);
  424. }
  425. /* TODO: fix this shit */
  426. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  427. SuppressUnmanagedCodeSecurity]
  428. private static extern int cucul_set_dither_palette(IntPtr d,
  429. int[] r, int[] g,
  430. int[] b, int[] a);
  431. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  432. SuppressUnmanagedCodeSecurity]
  433. private static extern int cucul_set_dither_brightness(IntPtr d, float b);
  434. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  435. SuppressUnmanagedCodeSecurity]
  436. private static extern int cucul_set_dither_gamma(IntPtr d, float g);
  437. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  438. SuppressUnmanagedCodeSecurity]
  439. private static extern int cucul_set_dither_contrast(IntPtr d, float c);
  440. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  441. SuppressUnmanagedCodeSecurity]
  442. private static extern int cucul_set_dither_invert(IntPtr d, int i);
  443. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  444. SuppressUnmanagedCodeSecurity]
  445. private static extern int cucul_set_dither_antialias(IntPtr d, string s);
  446. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  447. SuppressUnmanagedCodeSecurity]
  448. private static extern string[] cucul_get_dither_antialias_list(IntPtr d);
  449. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  450. SuppressUnmanagedCodeSecurity]
  451. private static extern int cucul_set_dither_color(IntPtr d, string s);
  452. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  453. SuppressUnmanagedCodeSecurity]
  454. private static extern string[] cucul_get_dither_color_list(IntPtr d);
  455. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  456. SuppressUnmanagedCodeSecurity]
  457. private static extern int cucul_set_dither_charset(IntPtr d, string s);
  458. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  459. SuppressUnmanagedCodeSecurity]
  460. private static extern string[] cucul_get_dither_charset_list(IntPtr d);
  461. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  462. SuppressUnmanagedCodeSecurity]
  463. private static extern int cucul_set_dither_mode(IntPtr d, string s);
  464. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  465. SuppressUnmanagedCodeSecurity]
  466. private static extern string[] cucul_get_dither_mode_list(IntPtr d);
  467. public int setBrightness(float b)
  468. {
  469. return cucul_set_dither_brightness(_dither, b);
  470. }
  471. public int setGamma(float g)
  472. {
  473. return cucul_set_dither_gamma(_dither, g);
  474. }
  475. public int setContrast(float c)
  476. {
  477. return cucul_set_dither_contrast(_dither, c);
  478. }
  479. public int setInvert(int i)
  480. {
  481. return cucul_set_dither_invert(_dither, i);
  482. }
  483. public int setAntialias(string s)
  484. {
  485. return cucul_set_dither_antialias(_dither, s);
  486. }
  487. public int setColor(string s)
  488. {
  489. return cucul_set_dither_color(_dither, s);
  490. }
  491. public int setCharset(string s)
  492. {
  493. return cucul_set_dither_charset(_dither, s);
  494. }
  495. public int setMode(string s)
  496. {
  497. return cucul_set_dither_mode(_dither, s);
  498. }
  499. /* <FIXME> */
  500. public string[] getAntialiasList()
  501. {
  502. return cucul_get_dither_antialias_list(_dither);
  503. }
  504. public string[] getColorList()
  505. {
  506. return cucul_get_dither_color_list(_dither);
  507. }
  508. public string[] getCharsetList()
  509. {
  510. return cucul_get_dither_charset_list(_dither);
  511. }
  512. public string[] getModeList()
  513. {
  514. return cucul_get_dither_mode_list(_dither);
  515. }
  516. /* </FIXME> */
  517. }
  518. }