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.
 
 
 
 
 
 

320 lines
10 KiB

  1. /*
  2. * libcaca .NET bindings for libcaca
  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. using System.Drawing;
  19. using Cucul;
  20. namespace Caca
  21. {
  22. /* Static libcaca stuff that does not fit in any object */
  23. public static class Libcaca
  24. {
  25. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  26. SuppressUnmanagedCodeSecurity]
  27. private static extern IntPtr caca_get_version();
  28. public static string getVersion()
  29. {
  30. return Marshal.PtrToStringAnsi(caca_get_version());
  31. }
  32. }
  33. public enum CacaEventType
  34. {
  35. NONE = 0x0000,
  36. KEY_PRESS = 0x0001,
  37. KEY_RELEASE = 0x0002,
  38. MOUSE_PRESS = 0x0004,
  39. MOUSE_RELEASE = 0x0008,
  40. MOUSE_MOTION = 0x0010,
  41. RESIZE = 0x0020,
  42. QUIT = 0x0040,
  43. ANY = 0xffff,
  44. }
  45. public enum CacaEventKey
  46. {
  47. UNKNOWN = 0x00,
  48. CTRL_A = 0x01,
  49. CTRL_B = 0x02,
  50. CTRL_C = 0x03,
  51. CTRL_D = 0x04,
  52. CTRL_E = 0x05,
  53. CTRL_F = 0x06,
  54. CTRL_G = 0x07,
  55. BACKSPACE = 0x08,
  56. TAB = 0x09,
  57. CTRL_J = 0x0a,
  58. CTRL_K = 0x0b,
  59. CTRL_L = 0x0c,
  60. RETURN = 0x0d,
  61. CTRL_N = 0x0e,
  62. CTRL_O = 0x0f,
  63. CTRL_P = 0x10,
  64. CTRL_Q = 0x11,
  65. CTRL_R = 0x12,
  66. PAUSE = 0x13,
  67. CTRL_T = 0x14,
  68. CTRL_U = 0x15,
  69. CTRL_V = 0x16,
  70. CTRL_W = 0x17,
  71. CTRL_X = 0x18,
  72. CTRL_Y = 0x19,
  73. CTRL_Z = 0x1a,
  74. ESCAPE = 0x1b,
  75. DELETE = 0x7f,
  76. UP = 0x111,
  77. DOWN = 0x112,
  78. LEFT = 0x113,
  79. RIGHT = 0x114,
  80. INSERT = 0x115,
  81. HOME = 0x116,
  82. END = 0x117,
  83. PAGEUP = 0x118,
  84. PAGEDOWN = 0x119,
  85. F1 = 0x11a,
  86. F2 = 0x11b,
  87. F3 = 0x11c,
  88. F4 = 0x11d,
  89. F5 = 0x11e,
  90. F6 = 0x11f,
  91. F7 = 0x120,
  92. F8 = 0x121,
  93. F9 = 0x122,
  94. F10 = 0x123,
  95. F11 = 0x124,
  96. F12 = 0x125,
  97. F13 = 0x126,
  98. F14 = 0x127,
  99. F15 = 0x128,
  100. }
  101. public class CacaEvent : IDisposable
  102. {
  103. public IntPtr cevent;
  104. private IntPtr _utf8;
  105. public CacaEvent()
  106. {
  107. cevent = Marshal.AllocHGlobal(32);
  108. _utf8 = Marshal.AllocHGlobal(8);
  109. }
  110. public void Dispose()
  111. {
  112. Marshal.FreeHGlobal(cevent);
  113. Marshal.FreeHGlobal(_utf8);
  114. GC.SuppressFinalize(this);
  115. }
  116. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  117. SuppressUnmanagedCodeSecurity]
  118. private static extern int caca_get_event_type(IntPtr ev);
  119. public CacaEventType Type
  120. {
  121. get { return (CacaEventType)caca_get_event_type(cevent); }
  122. }
  123. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  124. SuppressUnmanagedCodeSecurity]
  125. private static extern int caca_get_event_key_ch(IntPtr ev);
  126. public int KeyCh
  127. {
  128. get { return caca_get_event_key_ch(cevent); }
  129. }
  130. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  131. SuppressUnmanagedCodeSecurity]
  132. private static extern uint caca_get_event_key_utf32(IntPtr ev);
  133. public uint KeyUtf32
  134. {
  135. get { return caca_get_event_key_utf32(cevent); }
  136. }
  137. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  138. SuppressUnmanagedCodeSecurity]
  139. private static extern int caca_get_event_key_utf8(IntPtr ev,
  140. IntPtr _utf8);
  141. public string KeyUtf8
  142. {
  143. get
  144. {
  145. caca_get_event_key_utf8(cevent, _utf8);
  146. return Marshal.PtrToStringAnsi(_utf8);
  147. }
  148. }
  149. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  150. SuppressUnmanagedCodeSecurity]
  151. private static extern int caca_get_event_mouse_button(IntPtr ev);
  152. public int MouseButton
  153. {
  154. get { return caca_get_event_mouse_button(cevent); }
  155. }
  156. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  157. SuppressUnmanagedCodeSecurity]
  158. private static extern int caca_get_event_mouse_x(IntPtr ev);
  159. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  160. SuppressUnmanagedCodeSecurity]
  161. private static extern int caca_get_event_mouse_y(IntPtr ev);
  162. public Point MousePos
  163. {
  164. get { return new Point(caca_get_event_mouse_x(cevent),
  165. caca_get_event_mouse_y(cevent)); }
  166. }
  167. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  168. SuppressUnmanagedCodeSecurity]
  169. private static extern int caca_get_event_resize_width(IntPtr ev);
  170. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  171. SuppressUnmanagedCodeSecurity]
  172. private static extern int caca_get_event_resize_height(IntPtr ev);
  173. public Size ResizeSize
  174. {
  175. get { return new Size(caca_get_event_resize_width(cevent),
  176. caca_get_event_resize_height(cevent)); }
  177. }
  178. }
  179. public class CacaDisplay : IDisposable
  180. {
  181. private CuculCanvas _cv;
  182. public CuculCanvas Canvas { get { return _cv; } }
  183. private IntPtr _c_cv;
  184. private IntPtr _c_dp;
  185. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  186. SuppressUnmanagedCodeSecurity]
  187. private static extern IntPtr caca_create_display(IntPtr cv);
  188. public CacaDisplay(CuculCanvas cv)
  189. {
  190. _cv = cv;
  191. _c_cv = _cv._c_cv;
  192. _c_dp = caca_create_display(_c_cv);
  193. }
  194. public CacaDisplay()
  195. {
  196. /* XXX: we do not call caca_create_display() with a NULL
  197. * argument because it's then impossible to create a CuculCanvas
  198. * and I don't want to add a weird constructor */
  199. _cv = new CuculCanvas();
  200. _c_cv = _cv._c_cv;
  201. _c_dp = caca_create_display(_c_cv);
  202. }
  203. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  204. SuppressUnmanagedCodeSecurity]
  205. private static extern int caca_free_display(IntPtr dp);
  206. public void Dispose()
  207. {
  208. caca_free_display(_c_dp);
  209. GC.SuppressFinalize(this);
  210. }
  211. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  212. SuppressUnmanagedCodeSecurity]
  213. private static extern int caca_refresh_display(IntPtr dp);
  214. public void Refresh()
  215. {
  216. caca_refresh_display(_c_dp);
  217. }
  218. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  219. SuppressUnmanagedCodeSecurity]
  220. private static extern int caca_set_display_time(IntPtr dp, int d);
  221. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  222. SuppressUnmanagedCodeSecurity]
  223. private static extern int caca_get_display_time(IntPtr dp);
  224. public int DisplayTime
  225. {
  226. get { return caca_get_display_time(_c_dp); }
  227. set { caca_set_display_time(_c_dp, value); }
  228. }
  229. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  230. SuppressUnmanagedCodeSecurity]
  231. private static extern int caca_get_event(IntPtr dp, uint t,
  232. IntPtr cevent,
  233. int timeout);
  234. public CacaEvent getEvent(CacaEventType t, int timeout)
  235. {
  236. CacaEvent e = new CacaEvent();
  237. caca_get_event(_c_dp, (uint)t, e.cevent, timeout);
  238. return e;
  239. }
  240. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  241. SuppressUnmanagedCodeSecurity]
  242. private static extern int caca_get_display_width(IntPtr dp);
  243. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  244. SuppressUnmanagedCodeSecurity]
  245. private static extern int caca_get_display_height(IntPtr dp);
  246. public Size Size
  247. {
  248. get { return new Size(caca_get_display_width(_c_dp),
  249. caca_get_display_height(_c_dp)); }
  250. }
  251. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  252. SuppressUnmanagedCodeSecurity]
  253. private static extern int caca_set_display_title(IntPtr dp, string t);
  254. public string Title
  255. {
  256. set { caca_set_display_title(_c_dp, value); }
  257. }
  258. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  259. SuppressUnmanagedCodeSecurity]
  260. private static extern int caca_set_mouse(IntPtr k, bool status);
  261. public bool Mouse
  262. {
  263. set { caca_set_mouse(_c_dp, value); }
  264. }
  265. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  266. SuppressUnmanagedCodeSecurity]
  267. private static extern int caca_get_mouse_x(IntPtr k);
  268. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  269. SuppressUnmanagedCodeSecurity]
  270. private static extern int caca_get_mouse_y(IntPtr k);
  271. public Point MousePos
  272. {
  273. get { return new Point(caca_get_mouse_x(_c_dp),
  274. caca_get_mouse_y(_c_dp)); }
  275. }
  276. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  277. SuppressUnmanagedCodeSecurity]
  278. private static extern int caca_set_cursor(IntPtr k, bool status);
  279. public bool Cursor
  280. {
  281. set { caca_set_cursor(_c_dp, value); }
  282. }
  283. }
  284. }