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.
 
 
 
 
 
 

285 lines
8.9 KiB

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