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

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