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.
 
 
 
 
 
 

306 lines
9.8 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 int caca_get_event_key_utf32(IntPtr ev);
  133. public int 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 IntPtr _cv;
  182. private IntPtr _dp;
  183. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  184. SuppressUnmanagedCodeSecurity]
  185. private static extern IntPtr caca_create_display(IntPtr cv);
  186. public CacaDisplay(CuculCanvas cv)
  187. {
  188. _cv = cv._cv;
  189. _dp = caca_create_display(_cv);
  190. }
  191. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  192. SuppressUnmanagedCodeSecurity]
  193. private static extern void caca_free_display(IntPtr dp);
  194. public void Dispose()
  195. {
  196. caca_free_display(_dp);
  197. GC.SuppressFinalize(this);
  198. }
  199. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  200. SuppressUnmanagedCodeSecurity]
  201. private static extern void caca_refresh_display(IntPtr dp);
  202. public void Refresh()
  203. {
  204. caca_refresh_display(_dp);
  205. }
  206. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  207. SuppressUnmanagedCodeSecurity]
  208. private static extern void caca_set_display_time(IntPtr dp, int d);
  209. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  210. SuppressUnmanagedCodeSecurity]
  211. private static extern int caca_get_display_time(IntPtr dp);
  212. public int DisplayTime
  213. {
  214. get { return caca_get_display_time(_dp); }
  215. set { caca_set_display_time(_dp, value); }
  216. }
  217. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  218. SuppressUnmanagedCodeSecurity]
  219. private static extern int caca_get_event(IntPtr dp, int t,
  220. IntPtr cevent,
  221. int timeout);
  222. public CacaEvent getEvent(CacaEventType t, int timeout)
  223. {
  224. CacaEvent e = new CacaEvent();
  225. caca_get_event(_dp, (int)t, e.cevent, timeout);
  226. return e;
  227. }
  228. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  229. SuppressUnmanagedCodeSecurity]
  230. private static extern int caca_get_display_width(IntPtr dp);
  231. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  232. SuppressUnmanagedCodeSecurity]
  233. private static extern int caca_get_display_height(IntPtr dp);
  234. public Size Size
  235. {
  236. get { return new Size(caca_get_display_width(_dp),
  237. caca_get_display_height(_dp)); }
  238. }
  239. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  240. SuppressUnmanagedCodeSecurity]
  241. private static extern int caca_set_display_title(IntPtr dp, string t);
  242. public string Title
  243. {
  244. set { caca_set_display_title(_dp, value); }
  245. }
  246. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  247. SuppressUnmanagedCodeSecurity]
  248. private static extern void caca_set_mouse(IntPtr k, bool status);
  249. public bool Mouse
  250. {
  251. set { caca_set_mouse(_dp, value); }
  252. }
  253. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  254. SuppressUnmanagedCodeSecurity]
  255. private static extern int caca_get_mouse_x(IntPtr k);
  256. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  257. SuppressUnmanagedCodeSecurity]
  258. private static extern int caca_get_mouse_y(IntPtr k);
  259. public Point MousePos
  260. {
  261. get { return new Point(caca_get_mouse_x(_dp),
  262. caca_get_mouse_y(_dp)); }
  263. }
  264. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  265. SuppressUnmanagedCodeSecurity]
  266. private static extern void caca_set_cursor(IntPtr k, bool status);
  267. public bool Cursor
  268. {
  269. set { caca_set_cursor(_dp, value); }
  270. }
  271. }
  272. }