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

Last change on this file since 2047 was 2047, checked in by Sam Hocevar, 13 years ago
  • Rewrote the C# test in a more object-oriented way.
  • Property svn:keywords set to Id
File size: 6.3 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 2047 2007-11-24 19:56:33Z 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
24  enum Keys
25  {
26    CACA_KEY_UNKNOWN = 0x00, /**< Unknown key. */
27
28    /* The following keys have ASCII equivalents */
29    CACA_KEY_BACKSPACE = 0x08, /**< The backspace key. */
30    CACA_KEY_TAB =       0x09, /**< The tabulation key. */
31    CACA_KEY_RETURN =    0x0d, /**< The return key. */
32    CACA_KEY_PAUSE =     0x13, /**< The pause key. */
33    CACA_KEY_ESCAPE =    0x1b, /**< The escape key. */
34    CACA_KEY_DELETE =    0x7f, /**< The delete key. */
35
36    /* The following keys do not have ASCII equivalents but have been
37     * chosen to match the SDL equivalents */
38    CACA_KEY_UP =    0x111, /**< The up arrow key. */
39    CACA_KEY_DOWN =  0x112, /**< The down arrow key. */
40    CACA_KEY_LEFT =  0x113, /**< The left arrow key. */
41    CACA_KEY_RIGHT = 0x114, /**< The right arrow key. */
42
43    CACA_KEY_INSERT =   0x115, /**< The insert key. */
44    CACA_KEY_HOME =     0x116, /**< The home key. */
45    CACA_KEY_END =      0x117, /**< The end key. */
46    CACA_KEY_PAGEUP =   0x118, /**< The page up key. */
47    CACA_KEY_PAGEDOWN = 0x119, /**< The page down key. */
48
49    CACA_KEY_F1 =  0x11a, /**< The F1 key. */
50    CACA_KEY_F2 =  0x11b, /**< The F2 key. */
51    CACA_KEY_F3 =  0x11c, /**< The F3 key. */
52    CACA_KEY_F4 =  0x11d, /**< The F4 key. */
53    CACA_KEY_F5 =  0x11e, /**< The F5 key. */
54    CACA_KEY_F6 =  0x11f, /**< The F6 key. */
55    CACA_KEY_F7 =  0x120, /**< The F7 key. */
56    CACA_KEY_F8 =  0x121, /**< The F8 key. */
57    CACA_KEY_F9 =  0x122, /**< The F9 key. */
58    CACA_KEY_F10 = 0x123, /**< The F10 key. */
59    CACA_KEY_F11 = 0x124, /**< The F11 key. */
60    CACA_KEY_F12 = 0x125, /**< The F12 key. */
61    CACA_KEY_F13 = 0x126, /**< The F13 key. */
62    CACA_KEY_F14 = 0x127, /**< The F14 key. */
63    CACA_KEY_F15 = 0x128  /**< The F15 key. */
64  }
65  public unsafe class Event
66    {
67    public enum type
68      {
69        NONE =          0x0000, /**< No event. */
70
71        KEY_PRESS =     0x0001, /**< A key was pressed. */
72        KEY_RELEASE =   0x0002, /**< A key was released. */
73        MOUSE_PRESS =   0x0004, /**< A mouse button was pressed. */
74        MOUSE_RELEASE = 0x0008, /**< A mouse button was released. */
75        MOUSE_MOTION =  0x0010, /**< The mouse was moved. */
76        RESIZE =        0x0020, /**< The window was resized. */
77        QUIT =          0x0040, /**< The user requested to quit. */
78
79        ANY =           0xffff  /**< Bitmask for any event. */
80      };
81
82    }
83
84
85  public unsafe class CacaDisplay : IDisposable
86    {
87    [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
88      public static extern IntPtr caca_create_display(IntPtr cv);
89    [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
90      public static extern void caca_free_display(IntPtr dp);
91    [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
92      public static extern void caca_refresh_display(IntPtr dp);
93    [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
94      public static extern void caca_set_display_time(IntPtr dp, Int32 d);
95    [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
96      public static extern Int32 caca_get_display_time(IntPtr dp);
97    [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
98      public static extern Int32 caca_get_display_width(IntPtr dp);
99    [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
100      public static extern Int32 caca_get_display_height(IntPtr dp);
101    [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
102      public static extern Int32 caca_set_display_title(IntPtr dp, string t);
103    [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
104      public static extern Int32 caca_get_event(IntPtr k, Event.type t, Event e, Int32 timeout);
105    [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
106      public static extern Int32 caca_get_mouse_x(IntPtr k);
107    [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
108      public static extern Int32 caca_get_mouse_y(IntPtr k);
109    [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
110      public static extern void caca_set_mouse(IntPtr k, bool status);
111
112
113
114
115
116    IntPtr _cv;
117    IntPtr _dp;
118
119    public CacaDisplay(CuculCanvas cv)
120    {
121        _cv = cv._cv;
122        _dp = caca_create_display(_cv);
123    }
124    public void Dispose()
125    {
126        caca_free_display(_dp);
127        GC.SuppressFinalize(this);
128    }
129    public void Refresh()
130    {
131        caca_refresh_display(_dp);
132    }
133    public void setDisplayTime(Int32 d)
134    {
135        caca_set_display_time(_dp, d);
136    }
137    public Int32 getDisplayTime()
138    {
139        return caca_get_display_time(_dp);
140    }
141    public Int32 getDisplayWidth()
142    {
143        return caca_get_display_width(_dp);
144    }
145    public Int32 getDisplayHeight()
146    {
147        return caca_get_display_height(_dp);
148    }
149    public Int32 setDisplayTitle(string t)
150    {
151        return caca_set_display_title(_dp, t);
152    }
153    public Int32 getEvent(Event.type t, Event e, Int32 timeout)
154    {
155        return caca_get_event(_dp, t, e, timeout);
156    }
157    public Int32 getMouseX()
158    {
159        return caca_get_mouse_x(_dp);
160    }
161    public Int32 getMouseY()
162    {
163        return caca_get_mouse_y(_dp);
164    }
165    public void caca_set_mouse(bool status)
166    {
167        caca_set_mouse(_dp, status);
168    }
169
170
171
172
173
174
175
176    public IntPtr get_caca_t()
177    {
178        return _dp;
179    }
180    }
181}
Note: See TracBrowser for help on using the repository browser.