Subversion Repositories Kolibri OS

Rev

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