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

Last change on this file since 2058 was 2058, checked in by Sam Hocevar, 13 years ago
  • Implemented C# bindings for ditherBitmap() and added it to the C# test.
  • Property svn:keywords set to Id
File size: 8.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 2058 2007-11-25 17:12:59Z 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    public enum CacaEventType
25    {
26        NONE =          0x0000,
27
28        KEY_PRESS =     0x0001,
29        KEY_RELEASE =   0x0002,
30        MOUSE_PRESS =   0x0004,
31        MOUSE_RELEASE = 0x0008,
32        MOUSE_MOTION =  0x0010,
33        RESIZE =        0x0020,
34        QUIT =          0x0040,
35
36        ANY =           0xffff,
37    }
38
39    public enum CacaEventKey
40    {
41        UNKNOWN = 0x00,
42
43        BACKSPACE = 0x08,
44        TAB =       0x09,
45        RETURN =    0x0d,
46        PAUSE =     0x13,
47        ESCAPE =    0x1b,
48        DELETE =    0x7f,
49
50        UP =    0x111,
51        DOWN =  0x112,
52        LEFT =  0x113,
53        RIGHT = 0x114,
54
55        INSERT =   0x115,
56        HOME =     0x116,
57        END =      0x117,
58        PAGEUP =   0x118,
59        PAGEDOWN = 0x119,
60
61        F1 =  0x11a,
62        F2 =  0x11b,
63        F3 =  0x11c,
64        F4 =  0x11d,
65        F5 =  0x11e,
66        F6 =  0x11f,
67        F7 =  0x120,
68        F8 =  0x121,
69        F9 =  0x122,
70        F10 = 0x123,
71        F11 = 0x124,
72        F12 = 0x125,
73        F13 = 0x126,
74        F14 = 0x127,
75        F15 = 0x128,
76    }
77
78    public class CacaEvent : IDisposable
79    {
80        public IntPtr cevent;
81        private IntPtr utf8;
82
83        public CacaEvent()
84        {
85            cevent = Marshal.AllocHGlobal(32);
86            utf8 = Marshal.AllocHGlobal(8);
87        }
88
89        public void Dispose()
90        {
91            Marshal.FreeHGlobal(cevent);
92            Marshal.FreeHGlobal(utf8);
93            GC.SuppressFinalize(this);
94        }
95
96        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
97         SuppressUnmanagedCodeSecurity]
98        private static extern int caca_get_event_type(IntPtr ev);
99        public CacaEventType type
100        {
101            get { return (CacaEventType)caca_get_event_type(cevent); }
102        }
103
104        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
105         SuppressUnmanagedCodeSecurity]
106        private static extern int caca_get_event_key_ch(IntPtr ev);
107        public int keyCh
108        {
109            get { return caca_get_event_key_ch(cevent); }
110        }
111
112        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
113         SuppressUnmanagedCodeSecurity]
114        private static extern int caca_get_event_key_utf32(IntPtr ev);
115        public int keyUtf32
116        {
117            get { return caca_get_event_key_utf32(cevent); }
118        }
119
120        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
121         SuppressUnmanagedCodeSecurity]
122        private static extern int caca_get_event_key_utf8(IntPtr ev,
123                                                          IntPtr utf8);
124        public string keyUtf8
125        {
126            get
127            {
128                caca_get_event_key_utf8(cevent, utf8);
129                return Marshal.PtrToStringUni(utf8);
130            }
131        }
132
133        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
134         SuppressUnmanagedCodeSecurity]
135        private static extern int caca_get_event_mouse_button(IntPtr ev);
136        public int mouseButton
137        {
138            get { return caca_get_event_mouse_button(cevent); }
139        }
140
141        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
142         SuppressUnmanagedCodeSecurity]
143        private static extern int caca_get_event_mouse_x(IntPtr ev);
144        public int mouseX
145        {
146            get { return caca_get_event_mouse_x(cevent); }
147        }
148
149        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
150         SuppressUnmanagedCodeSecurity]
151        private static extern int caca_get_event_mouse_y(IntPtr ev);
152        public int mouseY
153        {
154            get { return caca_get_event_mouse_y(cevent); }
155        }
156
157        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
158         SuppressUnmanagedCodeSecurity]
159        private static extern int caca_get_event_resize_width(IntPtr ev);
160        public int resizeWidth
161        {
162            get { return caca_get_event_resize_width(cevent); }
163        }
164
165        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
166         SuppressUnmanagedCodeSecurity]
167        private static extern int caca_get_event_resize_height(IntPtr ev);
168        public int resizeHeight
169        {
170            get { return caca_get_event_resize_height(cevent); }
171        }
172    }
173
174    public unsafe class CacaDisplay : IDisposable
175    {
176        private IntPtr _cv;
177        private IntPtr _dp;
178
179        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
180         SuppressUnmanagedCodeSecurity]
181        private static extern IntPtr caca_create_display(IntPtr cv);
182        public CacaDisplay(CuculCanvas cv)
183        {
184            _cv = cv._cv;
185            _dp = caca_create_display(_cv);
186        }
187
188        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
189         SuppressUnmanagedCodeSecurity]
190        private static extern void caca_free_display(IntPtr dp);
191        public void Dispose()
192        {
193            caca_free_display(_dp);
194            GC.SuppressFinalize(this);
195        }
196
197        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
198         SuppressUnmanagedCodeSecurity]
199        private static extern void caca_refresh_display(IntPtr dp);
200        public void Refresh()
201        {
202            caca_refresh_display(_dp);
203        }
204
205        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
206         SuppressUnmanagedCodeSecurity]
207        private static extern void caca_set_display_time(IntPtr dp, int d);
208        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
209         SuppressUnmanagedCodeSecurity]
210        private static extern int caca_get_display_time(IntPtr dp);
211        public int displayTime
212        {
213            get { return caca_get_display_time(_dp); }
214            set { caca_set_display_time(_dp, value); }
215        }
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
229        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
230         SuppressUnmanagedCodeSecurity]
231        private static extern int caca_get_display_width(IntPtr dp);
232        public int width
233        {
234            get { return caca_get_display_width(_dp); }
235        }
236
237        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
238         SuppressUnmanagedCodeSecurity]
239        private static extern int caca_get_display_height(IntPtr dp);
240        public int height
241        {
242            get { return caca_get_display_height(_dp); }
243        }
244
245        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
246         SuppressUnmanagedCodeSecurity]
247        private static extern int caca_set_display_title(IntPtr dp, string t);
248        public string title
249        {
250            set { caca_set_display_title(_dp, value); }
251        }
252
253        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
254         SuppressUnmanagedCodeSecurity]
255        private static extern void caca_set_mouse(IntPtr k, bool status);
256        public bool mouse
257        {
258            set { caca_set_mouse(_dp, value); }
259        }
260
261        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
262         SuppressUnmanagedCodeSecurity]
263        private static extern int caca_get_mouse_x(IntPtr k);
264        public int mouseX
265        {
266            get { return caca_get_mouse_x(_dp); }
267        }
268
269        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
270         SuppressUnmanagedCodeSecurity]
271        private static extern int caca_get_mouse_y(IntPtr k);
272        public int mouseY
273        {
274            get { return caca_get_mouse_y(_dp); }
275        }
276
277        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
278         SuppressUnmanagedCodeSecurity]
279        private static extern void caca_set_cursor(IntPtr k, bool status);
280        public bool cursor
281        {
282            set { caca_set_cursor(_dp, value); }
283        }
284    }
285}
Note: See TracBrowser for help on using the repository browser.