source: libcaca/trunk/csharp/Caca.cs @ 2076

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