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

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