Subversion Repositories Kolibri OS

Rev

Rev 9093 | Rev 9137 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /* Copyright (C) KolibriOS team 2004-2021. All rights reserved. */
  2. /* Distributed under terms of the GNU General Public License    */
  3.  
  4. /* This file contains basic wrappers over KolibriOS system calls. */
  5. /* See sysfuncs.txt file for details. */
  6.  
  7. /*
  8.  * This file was created with you in mind. Lest you reinvent the wheel.
  9.  * If for some reason there is not enough wrapper add! I ask you to stick to the same style: snake_case.
  10.  * Structure names must start with "ksys_" and end with "_t".
  11.  * All wrappers must start with the "_ksys_" prefix.
  12.  * I consider it mandatory to place the wrappers in the correct order in the official documentation.
  13.  * Enjoy writing your code :)
  14. */
  15.  
  16. // Warning! The end of the file is the old definitions of function/structure names.
  17. // They are for compatibility... Better not to use them. */
  18.  
  19. #ifndef _KSYS_H_
  20. #define _KSYS_H_
  21.  
  22. #include <stddef.h>
  23.  
  24. #define asm_inline __asm__ __volatile__
  25.  
  26. /*============== General structures ==============*/
  27.  
  28. #pragma pack(push,1)
  29.  
  30. typedef struct {
  31.     uint8_t blue;
  32.     uint8_t green;
  33.     uint8_t red;
  34. }rgb_t;
  35.  
  36. typedef union {
  37.     uint32_t val;
  38.     struct{
  39.         uint8_t hour;
  40.         uint8_t min;
  41.         uint8_t sec;
  42.         uint8_t _zero;
  43.     };
  44. }ksys_time_t;
  45.  
  46. typedef union {
  47.     uint32_t val;
  48.     struct{
  49.         uint8_t  year;
  50.         uint8_t  month;
  51.         uint8_t  day;
  52.         uint8_t  _zero;
  53.     };
  54. }ksys_date_t;
  55.  
  56. typedef union{
  57.     uint32_t val;
  58.     struct{
  59.         uint16_t  x;
  60.         uint16_t  y;
  61.     };
  62. }ksys_pos_t;
  63.  
  64. typedef union ksys_oskey_t{
  65.     uint32_t val;
  66.     struct{
  67.         uint8_t state;
  68.         uint8_t code;
  69.         uint8_t ctrl_key;
  70.     };
  71. }ksys_oskey_t;
  72.  
  73. typedef struct{
  74.     void *data;
  75.     size_t size;
  76. }ksys_ufile_t;
  77.  
  78.  
  79. typedef struct{
  80.     uint32_t            p00;
  81.     union{
  82.         uint64_t        p04;
  83.         struct {
  84.             uint32_t    p04dw;
  85.             uint32_t    p08dw;
  86.         };
  87.     };
  88.     uint32_t            p12;
  89.     union {
  90.         uint32_t        p16;
  91.         const char     *new_name;
  92.         void           *bdfe;
  93.         void           *buf16;
  94.         const void     *cbuf16;
  95.     };
  96.     char                p20;
  97.     const char         *p21;
  98. }ksys70_t;
  99.  
  100. typedef struct {
  101.     uint32_t attributes;
  102.     uint32_t name_cp;
  103.     ksys_time_t creation_time;
  104.     ksys_date_t creation_date;
  105.     ksys_time_t last_access_time;
  106.     ksys_date_t last_access_date;
  107.     ksys_time_t last_modification_time;
  108.     ksys_date_t last_modification_date;
  109.     unsigned long long size;
  110.     char name[0];
  111. }ksys_bdfe_t;
  112.  
  113. typedef struct {
  114.   int cpu_usage;             //+0
  115.   int window_pos_info;       //+4
  116.   short int reserved1;       //+8
  117.   char name[12];             //+10
  118.   int memstart;              //+22
  119.   int memused;               //+26
  120.   int pid;                   //+30
  121.   int winx_start;            //+34
  122.   int winy_start;            //+38
  123.   int winx_size;             //+42
  124.   int winy_size;             //+46
  125.   short int slot_info;       //+50
  126.   short int reserved2;       //+52
  127.   int clientx;               //+54
  128.   int clienty;               //+58
  129.   int clientwidth;           //+62
  130.   int clientheight;          //+66
  131.   unsigned char window_state;//+70
  132.   char reserved3[1024-71];   //+71
  133. }ksys_thread_t;
  134.  
  135. typedef unsigned int ksys_color_t;
  136.  
  137. typedef struct{
  138.     ksys_color_t frame_area;
  139.     ksys_color_t grab_bar;
  140.     ksys_color_t grab_bar_button;
  141.     ksys_color_t grab_button_text;
  142.     ksys_color_t grab_text;
  143.     ksys_color_t work_area;
  144.     ksys_color_t work_button;
  145.     ksys_color_t work_button_text;
  146.     ksys_color_t work_text;
  147.     ksys_color_t work_graph;
  148. }ksys_colors_table_t;
  149.  
  150. typedef struct{
  151.     unsigned pid;      // PID of sending thread
  152.     unsigned datalen;  // data bytes
  153.     char     *data;    // data begin
  154. }ksys_ipc_msg;
  155.  
  156. typedef struct{
  157.     unsigned lock;              // nonzero is locked
  158.     unsigned used;              // used bytes in buffer
  159.     ksys_ipc_msg *data;         // data begin
  160. }ksys_ipc_buffer;
  161.  
  162. typedef struct {
  163.     char* func_name;
  164.     void* func_ptr;
  165. }ksys_dll_t;
  166.  
  167. typedef unsigned ksys_drv_hand_t;
  168.  
  169. typedef struct{
  170.     ksys_drv_hand_t handler;
  171.     unsigned func_num;
  172.     void* in_data_ptr;
  173.     unsigned in_data_size;
  174.     void* out_data_ptr;
  175.     unsigned out_data_size;
  176. }ksys_ioctl_t;
  177.  
  178. typedef struct{
  179.     char  key[64];
  180.     char path[64];
  181. }ksys_dir_key_t;
  182.  
  183. #pragma pack(pop)
  184.  
  185. typedef rgb_t ksys_bitmap_t;
  186.  
  187. enum KSYS_FS_ERRORS {
  188.     KSYS_FS_ERR_SUCCESS = 0,  // Success
  189.     KSYS_FS_ERR_1       = 1,  // Base and/or partition of a hard disk is not defined (fn21.7 & fn21.8)
  190.     KSYS_FS_ERR_2       = 2,  // Function is not supported for the given file system
  191.     KSYS_FS_ERR_3       = 3,  // Unknown file system
  192.     KSYS_FS_ERR_4       = 4,  // Reserved, is never returned in the current implementation
  193.     KSYS_FS_ERR_5       = 5,  // File not found
  194.     KSYS_FS_ERR_EOF     = 6,  // End of file, EOF
  195.     KSYS_FS_ERR_7       = 7,  // Pointer lies outside of application memory
  196.     KSYS_FS_ERR_8       = 8,  // Disk is full
  197.     KSYS_FS_ERR_9       = 9,  // FAT table is destroyed
  198.     KSYS_FS_ERR_10      = 10, // Access denied
  199.     KSYS_FS_ERR_11      = 11 // Device error
  200. };
  201.  
  202. enum KSYS_EVENTS {
  203.     KSYS_EVENT_NONE = 0,     /* Event queue is empty */
  204.     KSYS_EVENT_REDRAW = 1,   /* Window and window elements should be redrawn */
  205.     KSYS_EVENT_KEY = 2,      /* A key on the keyboard was pressed */
  206.     KSYS_EVENT_BUTTON = 3,   /* A button was clicked with the mouse */
  207.     KSYS_EVENT_DESKTOP = 5,  /* Desktop redraw finished */
  208.     KSYS_EVENT_MOUSE = 6,    /* Mouse activity (movement, button press) was detected */
  209.     KSYS_EVENT_IPC = 7,      /* Interprocess communication notify */
  210.     KSYS_EVENT_NETWORK = 8,  /* Network event */
  211.     KSYS_EVENT_DEBUG = 9,    /* Debug subsystem event */
  212.     KSYS_EVENT_IRQBEGIN = 16 /* 16..31 IRQ0..IRQ15 interrupt =IRQBEGIN+IRQn */
  213. };
  214.  
  215. enum KSYS_FILE_ENCODING{
  216.     KSYS_FILE_CP866 =1,
  217.     KSYS_FILE_UTF16LE = 2,
  218.     KSYS_FILE_UTF8 = 3
  219. };
  220.  
  221. static inline
  222. int _ksys_strcmp(const char * s1, const char * s2 )
  223. {
  224.     while ((*s1) && (*s1 == *s2)){ ++s1; ++s2; }
  225.     return(*(unsigned char*)s1 - *(unsigned char *)s2);
  226. }
  227.  
  228. /* ####################################################################### */
  229. /* ############### С wrappers for system calls Kolibri OS ################ */
  230. /* ####################################################################### */
  231.  
  232.  
  233. /*=============== Function -1 - terminate thread/process ===============*/
  234.  
  235. static inline
  236. void _ksys_exit(){
  237.     asm_inline("int $0x40"::"a"(-1));
  238. }
  239.  
  240. /*============== Function 0 - define and draw the window. ==============*/
  241.  
  242. static inline
  243. void _ksys_create_window(uint32_t x, uint32_t y, uint32_t w, uint32_t h, const char *name, ksys_color_t workcolor, uint32_t style) {
  244.     asm_inline(
  245.         "int $0x40"
  246.         ::"a"(0),
  247.         "b"((x << 16) | ((w-1) & 0xFFFF)),
  248.         "c"((y << 16) | ((h-1) & 0xFFFF)),
  249.         "d"((style << 24) | (workcolor & 0xFFFFFF)),
  250.         "D"(name),
  251.         "S"(0)
  252.         :"memory"
  253.      );
  254. };
  255.  
  256. /*================ Function 1 - put pixel in the window. ===============*/
  257.  
  258. static inline
  259. void _ksys_draw_pixel(uint32_t x, uint32_t y, ksys_color_t color){
  260.     asm_inline(
  261.         "int $0x40"
  262.         ::"a"(1), "b"(x), "c"(y), "d"(color)
  263.     );
  264. }
  265.  
  266. /*============ Function 2 - get the code of the pressed key. ===========*/
  267.  
  268. static inline
  269. ksys_oskey_t _ksys_get_key(void){
  270.     ksys_oskey_t val;
  271.     asm_inline(
  272.         "int $0x40"
  273.         :"=a"(val)
  274.         :"a"(2)
  275.     );
  276.     return val;
  277. }
  278.  
  279. /*==================== Function 3 - get system time. ===================*/
  280.  
  281. static inline
  282. ksys_time_t _ksys_get_time(){
  283.     ksys_time_t c_time;
  284.     asm_inline(
  285.         "int $0x40"
  286.         :"=a"(c_time)
  287.         :"a"(3)
  288.         :"memory"
  289.     );
  290.     return c_time;
  291. }
  292.  
  293. /*=================== Function 4 - draw text string. ===================*/
  294.  
  295. static inline
  296. void _ksys_draw_text(const char *text, uint32_t x, uint32_t y, uint32_t len, ksys_color_t color) {
  297.    asm_inline(
  298.         "int $0x40"
  299.         ::"a"(4),"d"(text),
  300.         "b"((x << 16) | y),
  301.         "S"(len),"c"(color)
  302.         :"memory"
  303.     );
  304. }
  305.  
  306. /*========================= Function 5 - delay. ========================*/
  307.  
  308. static inline
  309. void _ksys_delay(uint32_t time){
  310.     asm_inline(
  311.         "int $0x40"
  312.         ::"a"(5), "b"(time)
  313.         :"memory"
  314.     );
  315. }
  316.  
  317. /*=============== Function 7 - draw image in the window. ===============*/
  318.  
  319. static inline
  320. void _ksys_draw_bitmap(void *bitmap, int x, int y, int w, int h)
  321. {
  322.     asm_inline(
  323.         "int $0x40"
  324.         ::"a"(7), "b"(bitmap),
  325.         "c"((w << 16) | h),
  326.         "d"((x << 16) | y)
  327.         :"memory"
  328.     );
  329. }
  330.  
  331. /*=============== Function 8 - define/delete the button. ===============*/
  332.  
  333. static inline
  334. void _ksys_define_button(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t id, ksys_color_t color) {
  335.    asm_inline(
  336.         "int $0x40"
  337.         ::"a"(8),
  338.         "b"((x<<16)+w),
  339.         "c"((y<<16)+h),
  340.         "d"(id),
  341.         "S"(color)
  342.     );
  343. };
  344.  
  345. static inline
  346. void _ksys_delete_button(uint32_t id){
  347.     asm_inline(
  348.         "int $0x40"
  349.         ::"a"(8),"d"(id & 0x00FFFFFF | 0x80000000)
  350.     );
  351. }
  352.  
  353. /*============ Function 9 - information on execution thread. ===========*/
  354.  
  355. static inline
  356. int _ksys_thread_info(ksys_thread_t* table, int slot){
  357.     int val;
  358.     asm_inline(
  359.         "int $0x40"
  360.         :"=a"(val)
  361.         :"a"(9), "b"(table), "c"(slot)
  362.         :"memory"
  363.     );
  364.     return val;
  365. }
  366.  
  367. /*==================== Function 10 - wait for event. ===================*/
  368.  
  369. static inline
  370. uint32_t _ksys_wait_event(void){
  371.     uint32_t val;
  372.     asm_inline(
  373.         "int $0x40"
  374.         :"=a"(val)
  375.         :"a"(10)
  376.     );
  377.     return val;
  378. }
  379.  
  380. /*=============== Function 11 - check for event, no wait. ==============*/
  381.  
  382. static inline
  383. uint32_t _ksys_check_event(void){
  384.     uint32_t val;
  385.     asm_inline(
  386.         "int $0x40"
  387.         :"=a"(val)
  388.         :"a"(11)
  389.     );
  390.     return val;
  391. }
  392.  
  393. /*=============== Function 12 - begin/end window redraw. ===============*/
  394.  
  395. static inline
  396. void _ksys_start_draw(){
  397.    asm_inline("int $0x40"::"a"(12),"b"(1));
  398. }
  399.  
  400. static inline
  401. void _ksys_end_draw(){
  402.     asm_inline("int $0x40" ::"a"(12),"b"(2));
  403. }
  404.  
  405. /*============ Function 13 - draw a rectangle in the window. ===========*/
  406.  
  407. static inline
  408. void _ksys_draw_bar(uint32_t x, uint32_t y, uint32_t w, uint32_t h, ksys_color_t color){
  409.     asm_inline(
  410.         "int $0x40"
  411.         ::"a"(13), "d"(color),
  412.         "b"((x << 16) | w),
  413.         "c"((y << 16) | h)
  414.     );
  415. }
  416.  
  417. /*=================== Function 14 - get screen size. ===================*/
  418.  
  419. static inline
  420. ksys_pos_t _ksys_screen_size(){
  421.         ksys_pos_t size;
  422.     ksys_pos_t size_tmp;
  423.     asm_inline(
  424.         "int $0x40"
  425.         :"=a"(size_tmp)
  426.         :"a"(14)
  427.     );
  428.     size.x = size_tmp.y;
  429.     size.y = size_tmp.x;
  430.     return size;
  431. }
  432.  
  433. /*== Function 15, subfunction 1 - set a size of the background image. ==*/
  434.  
  435. static inline
  436. void _ksys_bg_set_size(uint32_t w, uint32_t h){
  437.     asm_inline(
  438.         "int $0x40"
  439.         ::"a"(15), "b"(1), "c"(w), "d"(h)
  440.     );
  441. }
  442.  
  443. /*=== Function 15, subfunction 2 - put pixel on the background image. ==*/
  444.  
  445. static inline
  446. void _ksys_bg_put_pixel(uint32_t x, uint32_t y, uint32_t w, ksys_color_t color){
  447.     asm_inline(
  448.         "int $0x40"
  449.         ::"a"(15), "b"(2), "c"((x+y*w)*3), "d"(color)
  450.     );
  451. }
  452.  
  453. /*=========== Function 15, subfunction 3 - redraw background. ==========*/
  454.  
  455. static inline
  456. void _ksys_bg_redraw(void){
  457.     asm_inline(
  458.         "int $0x40"
  459.         ::"a"(15), "b"(3)
  460.     );
  461. }
  462.  
  463. /*== Function 15, subfunction 4 - set drawing mode for the background. =*/
  464.  
  465. enum KSYS_BG_MODES{
  466.     KSYS_BG_MODE_PAVE=1,
  467.     KSYS_BG_MODE_STRETCH=2
  468. };
  469.  
  470. static inline
  471. void _ksys_bg_set_mode(uint32_t mode){
  472.     asm_inline(
  473.         "int $0x40"
  474.         ::"a"(15), "b"(4), "c"(mode)
  475.     );
  476. }
  477.  
  478. /*===================== Function 15, subfunction 5 =====================*/
  479. /*============ Put block of pixels on the background image. ============*/
  480.  
  481. static inline
  482. void _ksys_bg_put_bitmap(ksys_bitmap_t* bitmap, size_t bitmap_size, uint32_t x, uint32_t y, uint32_t w){
  483.     asm_inline(
  484.         "int $0x40"
  485.         ::"a"(15), "b"(5), "c"(bitmap), "d"((x+y*w)*3), "S"(bitmap_size)
  486.     );
  487. }
  488.  
  489. /*===================== Function 15, subfunction 6 =====================*/
  490. /*======= Map background data to the address space of process. ==========*/
  491.  
  492. static inline
  493. ksys_bitmap_t* _ksys_bg_get_map()
  494. {
  495.     ksys_bitmap_t *bitmap;
  496.     asm_inline(
  497.         "int $0x40"
  498.         :"=a"(bitmap)
  499.         :"a"(15), "b"(6)
  500.     );
  501.     return bitmap;
  502. }
  503.  
  504. /*===== Function 15, subfunction 7 - close mapped background data. =====*/
  505.  
  506. static inline
  507. int _ksys_bg_close_map(ksys_bitmap_t* bitmap)
  508. {  
  509.     int status; // 1 - OK, 0 - ERROR
  510.     asm_inline(
  511.         "int $0x40"
  512.         :"=a"(status)
  513.         :"a"(15), "b"(7), "c"(bitmap)
  514.     );
  515.     return status;
  516. }
  517.  
  518. /*===================== Function 15, subfunction 9 =====================*/
  519. /*============= Redraws a rectangular part of the background ===========*/
  520.  
  521. static inline
  522. void _ksys_bg_redraw_bar(ksys_pos_t angle1, ksys_pos_t angle2)
  523. {
  524.     asm_inline(
  525.         "int $0x40"
  526.         ::"a"(15), "b"(9),
  527.         "c"(angle1.x*(1<<16)+angle2.x),
  528.         "d"(angle1.y*(1<<16)+angle2.y)
  529.     );
  530. }
  531.  
  532. /*=============== Function 16 - save ramdisk on a floppy. ==============*/
  533.  
  534. static inline
  535. int _ksys_save_ramdisk_fd(uint32_t floppy_id)
  536. {
  537.     int status; // 0 - OK, 1 - ERROR
  538.     asm_inline(
  539.         "int $0x40"
  540.         :"=a"(status)
  541.         :"a"(16), "b"(floppy_id)
  542.     );
  543.     return status;    
  544. }
  545.  
  546. /*======= Function 17 - get the identifier of the pressed button. ======*/
  547.  
  548. static inline
  549. uint32_t _ksys_get_button()
  550. {
  551.     unsigned val;
  552.     asm_inline(
  553.         "int $0x40"
  554.         :"=a"(val)
  555.         :"a"(17)
  556.     );
  557.     return val>>8;
  558. }
  559.  
  560. /*===================== Function 18, subfunction 1 =====================*/
  561. /*============= Make deactive the window of the given thread. ==========*/
  562.  
  563. static inline
  564. void _ksys_unfocus_window(int slot)
  565. {
  566.     asm_inline(
  567.         "int $0x40"
  568.         ::"a"(18), "b"(1), "c"(slot)
  569.     );
  570. }
  571.  
  572. /*= Function 18, subfunction 2 - terminate process/thread by the slot. =*/
  573.  
  574. static inline
  575. void _ksys_kill_by_slot(int slot)
  576. {
  577.     asm_inline(
  578.         "int $0x40"
  579.         ::"a"(18), "b"(2), "c"(slot)
  580.     );
  581. }
  582.  
  583. /*===================== Function 18, subfunction 3 =====================*/
  584. /*============= Make active the window of the given thread. ============*/
  585.  
  586. static inline
  587. void _ksys_focus_window(int slot){
  588.     asm_inline(
  589.         "int $0x40"
  590.         ::"a"(18), "b"(3), "c"(slot)
  591.     );
  592. }
  593.  
  594. /*===================== Function 18, subfunction 4 =====================*/
  595. /*=========== Get counter of idle time units per one second. ===========*/
  596.  
  597. static inline
  598. uint32_t  _ksys_get_idle(void){
  599.     uint32_t sec;
  600.     asm_inline(
  601.         "int $0x40"
  602.         :"=a"(sec)
  603.         :"a"(18), "b"(4)
  604.     );
  605.     return sec;
  606. }
  607.  
  608. /*========== Function 18, subfunction 5 - get CPU clock rate. ==========*/
  609. /*================ modulo 2^32 clock ticks = 4GHz ======================*/
  610.  
  611. static inline
  612. uint32_t _ksys_get_cpu_clock(void){
  613.     uint32_t clock;
  614.     asm_inline(
  615.         "int $0x40"
  616.         :"=a"(clock)
  617.         :"a"(18), "b"(5)
  618.     );
  619.     return clock;
  620. }
  621.  
  622. /* Function 18, subfunction 6 - save ramdisk to the file on hard drive. */
  623.  
  624. static inline
  625. uint32_t _ksys_save_ramdisk_hd(const char* ramdisk_path){
  626.     uint32_t fs_err;
  627.     asm_inline(
  628.         "int $0x40"
  629.         :"=a"(fs_err)
  630.         :"a"(18), "b"(6), "c"(ramdisk_path)
  631.     );
  632.     return fs_err;
  633. }
  634.  
  635. /* Function 18, subfunction 9 - system shutdown with the parameter. */
  636.  
  637. enum KSYS_SHD_PARAM {
  638.     KSYS_SHD_POWEROFF    = 2,
  639.     KSYS_SHD_REBOOT      = 3,
  640.     KSYS_SHD_RESTART_KRN = 4
  641. };
  642.  
  643. static inline
  644. void _ksys_shutdown(uint32_t shd_param){
  645.     asm_inline(
  646.         "int $0x40"
  647.         ::"a"(18), "b"(9), "c"(shd_param)
  648.     );
  649. }
  650.  
  651. /*========= Function 18, subfunction 16 - get size of free RAM. ========*/
  652.  
  653. static inline
  654. size_t _ksys_get_ram_size(void){
  655.     size_t size;
  656.     asm_inline(
  657.         "int $0x40"
  658.         :"=a"(size)
  659.         :"a"(18), "b"(16)
  660.     );
  661.     return size;
  662. }
  663.  
  664. /*======== Function 18, subfunction 17 - get full amount of RAM. =======*/
  665.  
  666. static inline
  667. size_t _ksys_get_full_ram(void){
  668.     size_t size;
  669.     asm_inline(
  670.         "int $0x40"
  671.         :"=a"(size)
  672.         :"a"(18), "b"(17)
  673.     );
  674.     return size;
  675. }
  676.  
  677. /*===================== Function 18, subfunction 18 ====================*/
  678. /*============= Terminate process/thread by the identifier. ============*/
  679.  
  680. static inline
  681. void _ksys_kill_by_pid(uint32_t PID)
  682. {
  683.     asm_inline(
  684.         "int $0x40"
  685.         ::"a"(18), "b"(18), "c"(PID)
  686.     );
  687. }
  688.  
  689.  
  690. static inline
  691. int _ksys_get_thread_slot(int PID){
  692.     int val;
  693.     asm_inline(
  694.         "int $0x40"
  695.         :"=a"(val)
  696.         :"a"(18), "b"(21), "c"(PID)
  697.     );
  698.     return val;
  699. }
  700.  
  701. /*============= Function 23 - wait for event with timeout. =============*/
  702.  
  703. static inline
  704. uint32_t _ksys_wait_event_timeout(uint32_t timeout){
  705.     unsigned val;
  706.     asm_inline(
  707.         "int $0x40"
  708.         :"=a"(val)
  709.         :"a"(23), "b"(timeout)
  710.     );
  711.     return val;
  712. }
  713.  
  714. /*=== Function 26, subfunction 9 - get the value of the time counter. ==*/
  715.  
  716. static inline
  717. uint32_t _ksys_get_tick_count(){
  718.     unsigned val;
  719.     asm_inline(
  720.         "int $0x40"
  721.         :"=a"(val)
  722.         :"a"(26),"b"(9)
  723.     );
  724.     return val;
  725. }
  726.  
  727. /*===================== Function 26, subfunction 10 ====================*/
  728. /*========== Get the value of the high precision time counter. =========*/
  729.  
  730. static inline
  731. uint64_t  _ksys_get_ns_count(){
  732.     uint64_t val;
  733.     asm_inline(
  734.         "int $0x40"
  735.         :"=A"(val)
  736.         :"a"(26), "b"(10)
  737.     );
  738.     return val;
  739. }
  740.  
  741. /*=================== Function 29 - get system date. ===================*/
  742.  
  743. static inline
  744. ksys_date_t _ksys_get_date(){
  745.     ksys_date_t val;
  746.     asm_inline("int $0x40":"=a"(val):"a"(29));
  747.     return val;
  748. }
  749.  
  750. /*===========+ Function 30 - work with the current folder.==============*/
  751. /*--------- Subfunction 1 - set current folder for the thread. ---------*/
  752.  
  753. static inline
  754. void _ksys_setcwd(char* dir){
  755.     asm_inline(
  756.         "int $0x40"
  757.         ::"a"(30), "b"(1), "c"(dir)
  758.     );
  759. }
  760.  
  761. /*--------- Subfunction 2 - get current folder for the thread. ---------*/
  762.  
  763. static inline
  764. int _ksys_getcwd(char* buf, int bufsize){
  765.     register int val;
  766.     asm_inline(
  767.         "int $0x40"
  768.         :"=a"(val):"a"(30), "b"(2), "c"(buf), "d"(bufsize)
  769.     );
  770.     return val;
  771. }
  772.  
  773. /* ---- Subfunction 3 - install the add.system directory for the kernel ------*/
  774.  
  775. static inline
  776. int _ksys_set_kernel_dir(ksys_dir_key_t *table){
  777.     register int val;
  778.     asm_inline(
  779.         "int $0x40"
  780.         :"=a"(val)
  781.         :"a"(30), "b"(3), "c"(table)
  782.         :"memory"
  783.     );
  784.     return val;
  785. }
  786.  
  787. /*=================== Function 37 - work with mouse. ===================*/
  788.  
  789. enum KSYS_MOUSE_POS{
  790.     KSYS_MOUSE_SCREEN_POS = 0,
  791.     KSYS_MOUSE_WINDOW_POS = 1
  792. };
  793.  
  794. static inline
  795. ksys_pos_t _ksys_get_mouse_pos(int origin){
  796.     ksys_pos_t pos;
  797.     asm_inline(
  798.         "int $0x40 \n\t"
  799.         "rol $16, %%eax"
  800.         :"=a"(pos)
  801.         :"a"(37),"b"(origin)
  802.     );
  803.     return pos;
  804. }
  805.  
  806. static inline
  807. uint32_t _ksys_get_mouse_buttons(void){ // subfunction 2 - states of the mouse buttons
  808.     uint32_t val;
  809.     asm_inline(
  810.         "int $0x40"
  811.         :"=a"(val)
  812.         :"a"(37),"b"(2)
  813.     );
  814.     return val;
  815. }
  816.  
  817. static inline
  818. uint32_t _ksys_get_mouse_eventstate(void){   // subfunction 3 - states and events of the mouse buttons
  819.     uint32_t val;
  820.     asm_inline(
  821.         "int $0x40"
  822.         :"=a"(val)
  823.         :"a"(37),"b"(3)
  824.     );
  825.     return val;
  826. }
  827.  
  828. static inline
  829. uint32_t _ksys_load_cursor(void *path, uint32_t flags) { // subfunction 4 - load cursor
  830.     uint32_t val;
  831.     asm_inline(
  832.         "int $0x40"
  833.         :"=a"(val)
  834.         :"a"(37), "b"(4), "c"(path), "d"(flags)
  835.         :"memory"
  836.     );
  837.     return val;
  838. }
  839.  
  840. static inline
  841. uint32_t _ksys_set_cursor(uint32_t cursor){ // subfunction 5 - set curso
  842.     uint32_t old;
  843.     asm_inline(
  844.         "int $0x40"
  845.         :"=a"(old)
  846.         :"a"(37), "b"(5), "c"(cursor)
  847.     );
  848.     return old;
  849. }
  850.  
  851. static inline
  852. int _ksys_delete_cursor(uint32_t cursor){ // subfunction 6 - delete curso
  853.     int ret;
  854.     asm_inline(
  855.         "int $0x40"
  856.         :"=a"(ret)
  857.         :"a"(37), "b"(6), "c"(cursor)
  858.         :"memory"
  859.     );
  860.     return ret;
  861. }
  862.  
  863. static inline
  864. uint32_t _ksys_get_mouse_wheels(void)  // subfunction 7 - get scroll data
  865. {
  866.     uint32_t val;
  867.     asm_inline(
  868.         "int $0x40"
  869.         :"=a"(val)
  870.         :"a"(37),"b"(7)
  871.     );
  872.     return val;
  873. }
  874.  
  875. /*=========== Function 40 - set the mask for expected events. ==========*/
  876.  
  877. enum KSYS_EVENT_MASK{
  878.     KSYS_EVM_REDRAW = 1,
  879.     KSYS_EVM_KEY    = 2,
  880.     KSYS_EVM_BUTTON = 4,
  881.     KSYS_EVM_EXIT   = 8,
  882.     KSYS_EVM_BACKGROUND = 16,
  883.     KSYS_EVM_MOUSE      = 32,
  884.     KSYS_EVM_IPC        = 64,
  885.     KSYS_EVM_STACK      = 128,
  886.     KSYS_EVM_DEBUG      = 256,
  887.     KSYS_EVM_STACK2     = 512,
  888.     KSYS_EVM_MOUSE_FILTER  = 0x80000000,
  889.     KSYS_EVM_CURSOR_FILTER = 0x40000000,
  890. };
  891.  
  892. static inline
  893. uint32_t _ksys_set_event_mask(uint32_t mask){
  894.     unsigned val;
  895.     asm_inline(
  896.         "int $0x40"
  897.         :"=a"(val)
  898.         :"a"(40), "b"(mask)
  899.     );
  900.     return val;
  901. }
  902.  
  903. /*====================== Function 38 - draw line. ======================*/
  904.  
  905. static inline
  906. void _ksys_draw_line(int xs, int ys, int xe, int ye, ksys_color_t color)
  907. {
  908.     asm_inline(
  909.         "int $0x40"
  910.         ::"a"(38), "d"(color),
  911.         "b"((xs << 16) | xe),
  912.         "c"((ys << 16) | ye)
  913.     );
  914. }
  915.  
  916. /*============= Function 47 - draw a number in the window. =============*/
  917.  
  918. static inline
  919. void _ksys_draw_number(int number, int x, int y, int len, ksys_color_t color){
  920.     unsigned fmt;
  921.     fmt = len << 16 | 0x80000000; // no leading zeros + width
  922.     asm_inline(
  923.         "int $0x40"
  924.         ::"a"(47), "b"(fmt), "c"(number), "d"((x << 16) | y), "S"(color)
  925.     );
  926. }
  927.  
  928. static inline
  929. void _ksys_draw_number_bg(unsigned number, int x, int y, int len, ksys_color_t color, ksys_color_t bg){
  930.     unsigned fmt;
  931.     fmt = len << 16 | 0x80000000; // no leading zeros + width
  932.     asm_inline(
  933.         "int $0x40"
  934.         ::"a"(47), "b"(fmt), "c"(number), "d"((x << 16) | y), "S"(color), "D"(bg)
  935.     );
  936. }
  937.  
  938. /*====== Function 48, subfunction 3 - get standard window colors. ======*/
  939.  
  940. static inline
  941. void _ksys_get_system_colors(ksys_colors_table_t *color_table)
  942. {
  943.     asm_inline(
  944.        "int $0x40"
  945.         ::"a"(48),"b"(3),"c"(color_table),"d"(40)
  946.     );
  947. }
  948.  
  949. /*============ Function 48, subfunction 4 - get skin height. ===========*/
  950.  
  951. static inline
  952. uint32_t _ksys_get_skin_height(){
  953.     unsigned height;
  954.     asm_inline(
  955.         "int $0x40"
  956.         :"=a"(height)
  957.         :"a"(48),"b"(4)
  958.     );
  959.     return height;
  960. }
  961.  
  962. /*==================== Function 51 - create thread. ====================*/
  963.  
  964. static inline
  965. int _ksys_start_thread(void* thread_entry, void* stack_top){
  966.     int val;
  967.     asm_inline(
  968.         "int $0x40"
  969.         :"=a"(val)
  970.         :"a"(51), "b"(1), "c"(thread_entry), "d"(stack_top)
  971.     );
  972.     return val;
  973. }
  974.  
  975.  
  976. /*==================== Function 54, subfunction 0 ======================*/
  977. /*============== Get the number of slots in the clipboard. =============*/
  978.  
  979. enum KSYS_CLIP_ENCODING{
  980.     KSYS_CLIP_UTF8 = 0,
  981.     KSYS_CLIP_CP866 = 1,
  982.     KSYS_CLIP_CP1251 = 2
  983. };
  984.  
  985. enum KSYS_CLIP_TYPES{
  986.     KSYS_CLIP_TEXT = 0,
  987.     KSYS_CLIP_IMAGE = 1,
  988.     KSYS_CLIP_RAW = 2
  989. };
  990.  
  991. static inline
  992. int _ksys_clip_num(){
  993.     unsigned val;
  994.     asm_inline(
  995.         "int $0x40"
  996.         :"=a"(val)
  997.         :"a"(54), "b"(0)
  998.     );
  999.     return val;
  1000. }
  1001.  
  1002. /*==================== Function 54, subfunction 1 ======================*/
  1003. /*================= Read the data from the clipboard. ==================-*/
  1004.  
  1005. static inline
  1006. char* _ksys_clip_get(int n) {  // returned buffer must be freed by _ksys_free()
  1007.     char* val;
  1008.     asm_inline(
  1009.         "int $0x40"
  1010.         :"=a"(val)
  1011.         :"a"(54), "b"(1), "c"(n)
  1012.     );
  1013.     return val;
  1014. }
  1015.  
  1016. /*==================== Function 54, subfunction 2 ======================*/
  1017. /*================= Write the data to the clipboard. ===================*/
  1018.  
  1019. static inline
  1020. int _ksys_clip_set(int n, char *buffer){
  1021.     unsigned val;
  1022.     asm_inline(
  1023.         "int $0x40"
  1024.         :"=a"(val)
  1025.         :"a"(54), "b"(2), "c"(n), "d"(buffer)
  1026.         :"memory"
  1027.     );
  1028.     return val;
  1029. }
  1030.  
  1031. /*===================== Function 54, subfunction 3 =====================*/
  1032. /*================ Delete the last slot in the clipboard ===============*/
  1033.  
  1034. static inline
  1035. int _ksys_clip_pop(){
  1036.     unsigned val;
  1037.     asm_inline (
  1038.         "int $0x40"
  1039.         :"=a"(val)
  1040.         :"a"(54), "b"(3)
  1041.     );
  1042.     return val;
  1043. }
  1044.  
  1045. /*===================== Function 54, subfunction 4 =====================*/
  1046. /*===================== Alarm reset the lock buffer ====================*/
  1047.  
  1048. static inline
  1049. int _ksys_clip_unlock(){
  1050.     unsigned val;
  1051.     asm_inline(
  1052.         "int $0x40"
  1053.         :"=a"(val)
  1054.         :"a"(54), "b"(4)
  1055.     );
  1056.     return val;
  1057. }
  1058.  
  1059. /*============== Function 63 - work with the debug board. ==============*/
  1060.  
  1061. static inline
  1062. void _ksys_debug_putc(char c){
  1063.     asm_inline("int $0x40"::"a"(63), "b"(1), "c"(c));
  1064. }
  1065.  
  1066. static inline
  1067. void _ksys_debug_puts(char *s){
  1068.     unsigned i=0;
  1069.     while (*(s+i)){
  1070.         asm_inline ("int $0x40"::"a"(63), "b"(1), "c"(*(s+i)));
  1071.         i++;
  1072.     }
  1073. }
  1074.  
  1075. /*========= Function 67 - change position/sizes of the window. =========*/
  1076.  
  1077. static inline
  1078. void _ksys_change_window(int new_x, int new_y, int new_w, int new_h){
  1079.     asm_inline(
  1080.         "int $0x40"
  1081.         ::"a"(67), "b"(new_x), "c"(new_y), "d"(new_w),"S"(new_h)
  1082.     );
  1083. }
  1084.  
  1085. /*======== Function 68, subfunction 12 - allocate memory block. ========*/
  1086.  
  1087. static inline
  1088. void* _ksys_alloc(size_t size){
  1089.     void  *val;
  1090.     asm_inline(
  1091.         "int $0x40"
  1092.         :"=a"(val)
  1093.         :"a"(68),"b"(12),"c"(size)
  1094.     );
  1095.     return val;
  1096. }
  1097.  
  1098. /*========== Function 68, subfunction 13 - free memory block. ==========*/
  1099.  
  1100. static inline
  1101. int _ksys_free(void *mem){
  1102.     int val;
  1103.     asm_inline(
  1104.         "int $0x40"
  1105.         :"=a"(val)
  1106.         :"a"(68),"b"(13),"c"(mem)
  1107.     );
  1108.     return val;
  1109. }
  1110.  
  1111. /*============= Function 68, subfunction 16 - load driver. =============*/
  1112.  
  1113. static inline
  1114. ksys_drv_hand_t _ksys_load_driver(char *driver_name)
  1115. {
  1116.     ksys_drv_hand_t driver_h;
  1117.     asm_inline(
  1118.         "int $0x40"
  1119.         :"=a"(driver_h)
  1120.         :"a"(68), "b"(16), "c"(driver_name)
  1121.     );
  1122.     return driver_h;
  1123. }
  1124.  
  1125. /*============ Function 68, subfunction 17 - driver control. ===========*/
  1126.  
  1127. static inline
  1128. unsigned _ksys_driver_control(ksys_ioctl_t *ioctl)
  1129. {
  1130.     unsigned status;
  1131.     asm_inline(
  1132.         "int $0x40"
  1133.         :"=a"(status)
  1134.         :"a"(68), "b"(17), "c"(ioctl)
  1135.         :"memory"
  1136.     );
  1137.     return status;
  1138. }
  1139.  
  1140. /*== Function 68, subfunction 18 - subfunction 19 - load DLL (MS COFF) ==*/
  1141.  
  1142. static inline
  1143. ksys_dll_t* _ksys_dlopen(const char* path){
  1144.     ksys_dll_t *table;
  1145.     asm_inline(
  1146.         "int $0x40"
  1147.         :"=a"(table)
  1148.         :"a"(68),"b"(19), "c"(path)
  1149.         :"memory"
  1150.     );
  1151.     return table;
  1152. }
  1153.  
  1154. /* It is not a system call, it serves as an auxiliary tool*/
  1155.  
  1156. static inline
  1157. void* _ksys_dlsym(ksys_dll_t *table, const char* fun_name){
  1158.     unsigned i=0;
  1159.     while (1){
  1160.         if (!(table+i)->func_name){
  1161.             break;
  1162.         }else{
  1163.             if (!_ksys_strcmp(fun_name, (table+i)->func_name)){
  1164.                 return (table+i)->func_ptr;
  1165.             }
  1166.         }
  1167.         i++;
  1168.     }
  1169.     return NULL;
  1170. }
  1171.  
  1172. /* Function 68, subfunction 20 - reallocate memory block.*/
  1173.  
  1174. static inline
  1175. void* _ksys_realloc(void *mem, size_t size){
  1176.     void *val;
  1177.     asm_inline(
  1178.         "int $0x40"
  1179.         :"=a"(val)
  1180.         :"a"(68),"b"(20),"c"(size),"d"(mem)
  1181.         :"memory"
  1182.     );
  1183.     return val;
  1184. }
  1185.  
  1186. /* Function 68, subfunction 21 - load driver by full name. */
  1187.  
  1188. static inline
  1189. ksys_drv_hand_t _ksys_load_driver_opt(char *driver_path, char *cmd_line)
  1190. {
  1191.     ksys_drv_hand_t driver_h;
  1192.     asm_inline(
  1193.         "int $0x40"
  1194.         :"=a"(driver_h)
  1195.         :"a"(68), "b"(21), "c"(driver_path), "d"(cmd_line)
  1196.     );
  1197.     return driver_h;
  1198. }
  1199.  
  1200. /*======== Function 68, subfunction 22 - open named memory area. =======*/
  1201.  
  1202. enum KSYS_SHM_MODE{
  1203.     KSYS_SHM_OPEN = 0x00,
  1204.     KSYS_SHM_OPEN_ALWAYS = 0x04,
  1205.     KSYS_SHM_CREATE = 0x08,
  1206.     KSYS_SHM_READ = 0x00,
  1207.     KSYS_SHM_WRITE = 0x01,
  1208. };
  1209.  
  1210. static inline
  1211. int _ksys_shm_open(char *name, int mode, int size, char **new_shm)
  1212. {
  1213.     int error;
  1214.     asm_inline(
  1215.         "int $0x40"
  1216.         :"=a"(*new_shm), "=d"(error)
  1217.         :"a"(68), "b"(22), "c"(name), "d"(size), "S"(mode)
  1218.     );
  1219.     return error;
  1220. }
  1221.  
  1222. /*======= Function 68, subfunction 23 - close named memory area. =======*/
  1223.  
  1224. static inline
  1225. void _ksys_shm_close(char *shm_name)
  1226. {
  1227.     asm_inline(
  1228.         "int $0x40":
  1229.         :"a"(68), "b"(23), "c"(shm_name)
  1230.     );
  1231. }
  1232.  
  1233. /*====== Function 68, subfunction 26 - release memory pages ============*/
  1234.  
  1235. static inline
  1236. int* _ksys_unmap(void *base, size_t offset, size_t size){
  1237.     int  *val;
  1238.     asm_inline(
  1239.         "int $0x40"
  1240.         :"=a"(val)
  1241.         :"a"(68),"b"(26),"c"(base),"d"(offset),"S"(size)
  1242.     );
  1243.     return val;
  1244. }
  1245.  
  1246. /*========== Function 68, subfunction 27 - load file ===================*/
  1247.  
  1248. static inline
  1249. ksys_ufile_t _ksys_load_file(const char *path)
  1250. {
  1251.     ksys_ufile_t uf;
  1252.     asm_inline(
  1253.         "int $0x40"
  1254.         :"=a"(uf.data), "=d"(uf.size)
  1255.         :"a"(68), "b"(27),"c"(path)
  1256.         :"memory"
  1257.     );
  1258.     return uf;
  1259. }
  1260.  
  1261. /*==== Function 68, subfunction 28 - load file, specifying the encoding ===*/
  1262.  
  1263. static inline
  1264. ksys_ufile_t _ksys_load_file_enc(const char *path, unsigned file_encoding)
  1265. {
  1266.     ksys_ufile_t uf;
  1267.     asm_inline(
  1268.         "int $0x40"
  1269.         :"=a"(uf.data), "=d"(uf.size)
  1270.         :"a"(68), "b"(28),"c"(path), "d"(file_encoding)
  1271.         :"memory"
  1272.     );
  1273.     return uf;
  1274. }
  1275.  
  1276. /*==== Function 70 - work with file system with long names support. ====*/
  1277.  
  1278. static inline
  1279. int _ksys_work_files(const ksys70_t *k)
  1280. {
  1281.     int status;
  1282.     asm_inline(
  1283.         "int $0x40"
  1284.         :"=a"(status)
  1285.         :"a"(70), "b"(k)
  1286.         :"memory"
  1287.     );
  1288.     return status;
  1289. }
  1290.  
  1291. /*====== Function 70, subfunction 0 - read file with long names support. ======*/
  1292.  
  1293. static inline
  1294. int _ksys_file_read_file(const char *name, unsigned long long offset, unsigned size, void *buf, unsigned *bytes_read)
  1295. {
  1296.     ksys70_t k;
  1297.     k.p00 = 0;
  1298.     k.p04 = offset;
  1299.     k.p12 = size;
  1300.     k.buf16 = buf;
  1301.     k.p20 = 0;
  1302.     k.p21 = name;
  1303.     int status;
  1304.     unsigned bytes_read_v;
  1305.     asm_inline(
  1306.         "int $0x40"
  1307.         :"=a"(status), "=b"(bytes_read_v)
  1308.         :"a"(70), "b"(&k)
  1309.         :"memory"
  1310.     );
  1311.     if (!status) {
  1312.         *bytes_read = bytes_read_v;
  1313.     }
  1314.     return status;
  1315. }
  1316.  
  1317. /*===================== Function 70, subfunction 2 =====================*/
  1318. /*============ Create/rewrite file with long names support. ============*/
  1319.  
  1320. static inline
  1321. int _ksys_file_create(const char* name){
  1322.     ksys70_t k;
  1323.     k.p00 = 2;
  1324.     k.p12 = 0;
  1325.     k.p21 = name;
  1326.     return _ksys_work_files(&k);
  1327. }
  1328.  
  1329. /*===================== Function 70, subfunction 3 =====================*/
  1330. /*=========== Write to existing file with long names support. ==========*/
  1331.  
  1332. static inline
  1333. int _ksys_file_write_file(const char *name, unsigned long long offset, unsigned size, const void *buf, unsigned *bytes_written)
  1334. {
  1335.     ksys70_t k;
  1336.     k.p00 = 3;
  1337.     k.p04 = offset;
  1338.     k.p12 = size;
  1339.     k.cbuf16 = buf;
  1340.     k.p20 = 0;
  1341.     k.p21 = name;
  1342.     int status;
  1343.     unsigned bytes_written_v;
  1344.     asm_inline(
  1345.         "int $0x40"
  1346.         :"=a"(status), "=b"(bytes_written_v)
  1347.         :"a"(70), "b"(&k)
  1348.         :"memory"
  1349.     );
  1350.     if (!status) {
  1351.         *bytes_written = bytes_written_v;
  1352.     }
  1353.     return status;
  1354. }
  1355.  
  1356. /*========== Function 70, subfunction 5 - get information on file/folder. =====*/
  1357.  
  1358. static inline
  1359. int _ksys_file_get_info(const char *name, ksys_bdfe_t *bdfe)
  1360. {
  1361.     ksys70_t k;
  1362.     k.p00 = 5;
  1363.     k.bdfe = bdfe;
  1364.     k.p20 = 0;
  1365.     k.p21 = name;
  1366.     return _ksys_work_files(&k);
  1367. }
  1368.  
  1369. /*=========== Function 70, subfunction 7 - start application. ===========*/
  1370.  
  1371. static inline
  1372. int _ksys_exec(char *app_name, char *args)
  1373. {
  1374.     ksys70_t file_opt;
  1375.     file_opt.p00 = 7;
  1376.     file_opt.p04dw = 0;
  1377.     file_opt.p08dw = (unsigned)args;
  1378.     file_opt.p21 = app_name;
  1379.     return _ksys_work_files(&file_opt);
  1380. }
  1381.  
  1382. /*========== Function 70, subfunction 8 - delete file/folder. ==========*/
  1383.  
  1384. static inline
  1385. int _ksys_file_delete(const char *name)
  1386. {
  1387.     ksys70_t k;
  1388.     k.p00 = 8;
  1389.     k.p20 = 0;
  1390.     k.p21 = name;
  1391.     return _ksys_work_files(&k);
  1392. }
  1393.  
  1394. /*============= Function 70, subfunction 9 - create folder. ============*/
  1395.  
  1396. static inline
  1397. int _ksys_mkdir(const char *path)
  1398. {
  1399.     ksys70_t dir_opt;
  1400.     dir_opt.p00 = 9;
  1401.     dir_opt.p21 = path;
  1402.     return _ksys_work_files(&dir_opt);
  1403. }
  1404.  
  1405. /*============= Function 70, subfunction 10 - rename/move. =============*/
  1406.  
  1407. static inline
  1408. int _ksys_file_rename(const char *name, const char *new_name)
  1409. {
  1410.     ksys70_t k;
  1411.     k.p00 = 10;
  1412.     k.new_name = new_name;
  1413.     k.p20 = 0;
  1414.     k.p21 = name;
  1415.     return _ksys_work_files(&k);
  1416. }
  1417.  
  1418.  
  1419. /* ######### Old names of functions and structures. Do not use again! ##########*/
  1420.  
  1421. #define _ksys_get_event    _ksys_wait_event
  1422.  
  1423. #endif // _KSYS_H_
  1424.