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