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

Last change on this file since 2078 was 2078, checked in by Sam Hocevar, 13 years ago
  • Use System.Drawing.Rectangle etc. for cucul-sharp and caca-sharp arguments, allowing for much cleaner code (at the expense of an extra dependency).
  • Property svn:keywords set to Id
File size: 9.8 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 2078 2007-11-27 00:58:16Z 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 int caca_get_event_key_utf32(IntPtr ev);
150        public int 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 IntPtr _cv;
204        private IntPtr _dp;
205
206        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
207         SuppressUnmanagedCodeSecurity]
208        private static extern IntPtr caca_create_display(IntPtr cv);
209        public CacaDisplay(CuculCanvas cv)
210        {
211            _cv = cv._cv;
212            _dp = caca_create_display(_cv);
213        }
214
215        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
216         SuppressUnmanagedCodeSecurity]
217        private static extern void caca_free_display(IntPtr dp);
218        public void Dispose()
219        {
220            caca_free_display(_dp);
221            GC.SuppressFinalize(this);
222        }
223
224        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
225         SuppressUnmanagedCodeSecurity]
226        private static extern void caca_refresh_display(IntPtr dp);
227        public void Refresh()
228        {
229            caca_refresh_display(_dp);
230        }
231
232        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
233         SuppressUnmanagedCodeSecurity]
234        private static extern void caca_set_display_time(IntPtr dp, int d);
235        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
236         SuppressUnmanagedCodeSecurity]
237        private static extern int caca_get_display_time(IntPtr dp);
238        public int displayTime
239        {
240            get { return caca_get_display_time(_dp); }
241            set { caca_set_display_time(_dp, value); }
242        }
243
244        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
245         SuppressUnmanagedCodeSecurity]
246        private static extern int caca_get_event(IntPtr dp, int t,
247                                                 IntPtr cevent,
248                                                 int timeout);
249        public CacaEvent getEvent(CacaEventType t, int timeout)
250        {
251            CacaEvent e = new CacaEvent();
252            caca_get_event(_dp, (int)t, e.cevent, timeout);
253            return e;
254        }
255
256        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
257         SuppressUnmanagedCodeSecurity]
258        private static extern int caca_get_display_width(IntPtr dp);
259        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
260         SuppressUnmanagedCodeSecurity]
261        private static extern int caca_get_display_height(IntPtr dp);
262        public Size Size
263        {
264            get { return new Size(caca_get_display_width(_dp),
265                                  caca_get_display_height(_dp)); }
266        }
267
268        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
269         SuppressUnmanagedCodeSecurity]
270        private static extern int caca_set_display_title(IntPtr dp, string t);
271        public string title
272        {
273            set { caca_set_display_title(_dp, value); }
274        }
275
276        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
277         SuppressUnmanagedCodeSecurity]
278        private static extern void caca_set_mouse(IntPtr k, bool status);
279        public bool mouse
280        {
281            set { caca_set_mouse(_dp, value); }
282        }
283
284        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
285         SuppressUnmanagedCodeSecurity]
286        private static extern int caca_get_mouse_x(IntPtr k);
287        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
288         SuppressUnmanagedCodeSecurity]
289        private static extern int caca_get_mouse_y(IntPtr k);
290        public Point mousePos
291        {
292            get { return new Point(caca_get_mouse_x(_dp),
293                                   caca_get_mouse_y(_dp)); }
294        }
295
296        [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
297         SuppressUnmanagedCodeSecurity]
298        private static extern void caca_set_cursor(IntPtr k, bool status);
299        public bool cursor
300        {
301            set { caca_set_cursor(_dp, value); }
302        }
303    }
304}
305
Note: See TracBrowser for help on using the repository browser.