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.
 
 
 
 
 
 

577 line
21 KiB

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