NativeUnsafeMethods.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. using __u32 = System.UInt32;
  2. using __s32 = System.Int32;
  3. using __u16 = System.UInt16;
  4. using System;
  5. using System.Runtime.InteropServices;
  6. // ReSharper disable FieldCanBeMadeReadOnly.Local
  7. // ReSharper disable ArrangeTypeMemberModifiers
  8. // ReSharper disable BuiltInTypeReferenceStyle
  9. // ReSharper disable InconsistentNaming
  10. namespace Avalonia.LinuxFramebuffer
  11. {
  12. unsafe class NativeUnsafeMethods
  13. {
  14. [DllImport("libc", EntryPoint = "open", SetLastError = true)]
  15. public static extern int open(string pathname, int flags, int mode);
  16. [DllImport("libc", EntryPoint = "close", SetLastError = true)]
  17. public static extern int close(int fd);
  18. [DllImport("libc", EntryPoint = "ioctl", SetLastError = true)]
  19. public static extern int ioctl(int fd, FbIoCtl code, void* arg);
  20. [DllImport("libc", EntryPoint = "mmap", SetLastError = true)]
  21. public static extern IntPtr mmap(IntPtr addr, IntPtr length, int prot, int flags,
  22. int fd, IntPtr offset);
  23. [DllImport("libc", EntryPoint = "munmap", SetLastError = true)]
  24. public static extern int munmap(IntPtr addr, IntPtr length);
  25. [DllImport("libc", EntryPoint = "memcpy", SetLastError = true)]
  26. public static extern int memcpy(IntPtr dest, IntPtr src, IntPtr length);
  27. [DllImport("libc", EntryPoint = "select", SetLastError = true)]
  28. public static extern int select(int nfds, void* rfds, void* wfds, void* exfds, IntPtr* timevals);
  29. [DllImport("libevdev.so.2", EntryPoint = "libevdev_new_from_fd", SetLastError = true)]
  30. public static extern int libevdev_new_from_fd(int fd, out IntPtr dev);
  31. [DllImport("libevdev.so.2", EntryPoint = "libevdev_has_event_type", SetLastError = true)]
  32. public static extern int libevdev_has_event_type(IntPtr dev, EvType type);
  33. [DllImport("libevdev.so.2", EntryPoint = "libevdev_next_event", SetLastError = true)]
  34. public static extern int libevdev_next_event(IntPtr dev, int flags, out input_event ev);
  35. [DllImport("libevdev.so.2", EntryPoint = "libevdev_get_name", SetLastError = true)]
  36. public static extern IntPtr libevdev_get_name(IntPtr dev);
  37. [DllImport("libevdev.so.2", EntryPoint = "libevdev_get_abs_info", SetLastError = true)]
  38. public static extern input_absinfo* libevdev_get_abs_info(IntPtr dev, int code);
  39. }
  40. enum FbIoCtl : uint
  41. {
  42. FBIOGET_VSCREENINFO = 0x4600,
  43. FBIOPUT_VSCREENINFO = 0x4601,
  44. FBIOGET_FSCREENINFO = 0x4602,
  45. FBIOGET_VBLANK = 0x80204612u,
  46. FBIO_WAITFORVSYNC = 0x40044620,
  47. FBIOPAN_DISPLAY = 0x4606
  48. }
  49. [Flags]
  50. enum VBlankFlags
  51. {
  52. FB_VBLANK_VBLANKING = 0x001 /* currently in a vertical blank */,
  53. FB_VBLANK_HBLANKING = 0x002 /* currently in a horizontal blank */,
  54. FB_VBLANK_HAVE_VBLANK = 0x004 /* vertical blanks can be detected */,
  55. FB_VBLANK_HAVE_HBLANK = 0x008 /* horizontal blanks can be detected */,
  56. FB_VBLANK_HAVE_COUNT = 0x010 /* global retrace counter is available */,
  57. FB_VBLANK_HAVE_VCOUNT = 0x020 /* the vcount field is valid */,
  58. FB_VBLANK_HAVE_HCOUNT = 0x040 /* the hcount field is valid */,
  59. FB_VBLANK_VSYNCING = 0x080 /* currently in a vsync */,
  60. FB_VBLANK_HAVE_VSYNC = 0x100 /* verical syncs can be detected */
  61. }
  62. unsafe struct fb_vblank {
  63. public VBlankFlags flags; /* FB_VBLANK flags */
  64. __u32 count; /* counter of retraces since boot */
  65. __u32 vcount; /* current scanline position */
  66. __u32 hcount; /* current scandot position */
  67. fixed __u32 reserved[4]; /* reserved for future compatibility */
  68. };
  69. [StructLayout(LayoutKind.Sequential)]
  70. unsafe struct fb_fix_screeninfo
  71. {
  72. public fixed byte id[16]; /* identification string eg "TT Builtin" */
  73. public IntPtr smem_start; /* Start of frame buffer mem */
  74. /* (physical address) */
  75. public __u32 smem_len; /* Length of frame buffer mem */
  76. public __u32 type; /* see FB_TYPE_* */
  77. public __u32 type_aux; /* Interleave for interleaved Planes */
  78. public __u32 visual; /* see FB_VISUAL_* */
  79. public __u16 xpanstep; /* zero if no hardware panning */
  80. public __u16 ypanstep; /* zero if no hardware panning */
  81. public __u16 ywrapstep; /* zero if no hardware ywrap */
  82. public __u32 line_length; /* length of a line in bytes */
  83. public IntPtr mmio_start; /* Start of Memory Mapped I/O */
  84. /* (physical address) */
  85. public __u32 mmio_len; /* Length of Memory Mapped I/O */
  86. public __u32 accel; /* Type of acceleration available */
  87. public fixed __u16 reserved[3]; /* Reserved for future compatibility */
  88. };
  89. [StructLayout(LayoutKind.Sequential)]
  90. struct fb_bitfield
  91. {
  92. public __u32 offset; /* beginning of bitfield */
  93. public __u32 length; /* length of bitfield */
  94. public __u32 msb_right; /* != 0 : Most significant bit is */
  95. /* right */
  96. };
  97. [StructLayout(LayoutKind.Sequential)]
  98. unsafe struct fb_var_screeninfo
  99. {
  100. public __u32 xres; /* visible resolution */
  101. public __u32 yres;
  102. public __u32 xres_virtual; /* virtual resolution */
  103. public __u32 yres_virtual;
  104. public __u32 xoffset; /* offset from virtual to visible */
  105. public __u32 yoffset; /* resolution */
  106. public __u32 bits_per_pixel; /* guess what */
  107. public __u32 grayscale; /* != 0 Graylevels instead of colors */
  108. public fb_bitfield red; /* bitfield in fb mem if true color, */
  109. public fb_bitfield green; /* else only length is significant */
  110. public fb_bitfield blue;
  111. public fb_bitfield transp; /* transparency */
  112. public __u32 nonstd; /* != 0 Non standard pixel format */
  113. public __u32 activate; /* see FB_ACTIVATE_* */
  114. public __u32 height; /* height of picture in mm */
  115. public __u32 width; /* width of picture in mm */
  116. public __u32 accel_flags; /* acceleration flags (hints) */
  117. /* Timing: All values in pixclocks, except pixclock (of course) */
  118. public __u32 pixclock; /* pixel clock in ps (pico seconds) */
  119. public __u32 left_margin; /* time from sync to picture */
  120. public __u32 right_margin; /* time from picture to sync */
  121. public __u32 upper_margin; /* time from sync to picture */
  122. public __u32 lower_margin;
  123. public __u32 hsync_len; /* length of horizontal sync */
  124. public __u32 vsync_len; /* length of vertical sync */
  125. public __u32 sync; /* see FB_SYNC_* */
  126. public __u32 vmode; /* see FB_VMODE_* */
  127. public fixed __u32 reserved[6]; /* Reserved for future compatibility */
  128. };
  129. enum EvType
  130. {
  131. EV_SYN = 0x00,
  132. EV_KEY = 0x01,
  133. EV_REL = 0x02,
  134. EV_ABS = 0x03,
  135. EV_MSC = 0x04,
  136. EV_SW = 0x05,
  137. EV_LED = 0x11,
  138. EV_SND = 0x12,
  139. EV_REP = 0x14,
  140. EV_FF = 0x15,
  141. EV_PWR = 0x16,
  142. EV_FF_STATUS = 0x17,
  143. }
  144. [StructLayout(LayoutKind.Sequential)]
  145. struct input_event
  146. {
  147. private IntPtr crap1, crap2;
  148. public ushort type, code;
  149. public int value;
  150. }
  151. [StructLayout(LayoutKind.Sequential)]
  152. unsafe struct fd_set
  153. {
  154. public int count;
  155. public fixed int fds [256];
  156. }
  157. enum AxisEventCode
  158. {
  159. REL_X = 0x00,
  160. REL_Y = 0x01,
  161. REL_Z = 0x02,
  162. REL_RX = 0x03,
  163. REL_RY = 0x04,
  164. REL_RZ = 0x05,
  165. REL_HWHEEL = 0x06,
  166. REL_DIAL = 0x07,
  167. REL_WHEEL = 0x08,
  168. REL_MISC = 0x09,
  169. REL_MAX = 0x0f
  170. }
  171. enum AbsAxis
  172. {
  173. ABS_X = 0x00,
  174. ABS_Y = 0x01,
  175. ABS_Z = 0x02,
  176. ABS_RX = 0x03,
  177. ABS_RY = 0x04,
  178. ABS_RZ = 0x05,
  179. ABS_THROTTLE = 0x06,
  180. ABS_RUDDER = 0x07,
  181. ABS_WHEEL = 0x08,
  182. ABS_GAS = 0x09,
  183. ABS_BRAKE = 0x0a,
  184. ABS_HAT0X = 0x10,
  185. ABS_HAT0Y = 0x11,
  186. ABS_HAT1X = 0x12,
  187. ABS_HAT1Y = 0x13,
  188. ABS_HAT2X = 0x14,
  189. ABS_HAT2Y = 0x15,
  190. ABS_HAT3X = 0x16,
  191. ABS_HAT3Y = 0x17,
  192. ABS_PRESSURE = 0x18,
  193. ABS_DISTANCE = 0x19,
  194. ABS_TILT_X = 0x1a,
  195. ABS_TILT_Y = 0x1b,
  196. ABS_TOOL_WIDTH = 0x1c
  197. }
  198. enum EvKey
  199. {
  200. BTN_LEFT = 0x110,
  201. BTN_RIGHT = 0x111,
  202. BTN_MIDDLE = 0x112
  203. }
  204. [StructLayout(LayoutKind.Sequential)]
  205. struct input_absinfo
  206. {
  207. public __s32 value;
  208. public __s32 minimum;
  209. public __s32 maximum;
  210. public __s32 fuzz;
  211. public __s32 flat;
  212. public __s32 resolution;
  213. }
  214. }