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