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

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