Subversion Repositories Kolibri OS

Rev

Rev 9836 | Rev 9874 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. #ifndef _KSYS_H_
  2. #define _KSYS_H_
  3.  
  4. /* Copyright (C) KolibriOS team 2004-2021. All rights reserved. */
  5. /* Distributed under terms of the GNU General Public License    */
  6.  
  7. /* This file contains basic wrappers over KolibriOS system calls. */
  8. /* See sysfuncs.txt file for details. */
  9.  
  10. /*
  11.  * This file was created with you in mind. Lest you reinvent the wheel.
  12.  * If for some reason there is not enough wrapper add! I ask you to stick to the same style: snake_case.
  13.  * Structure names must start with "ksys_" and end with "_t".
  14.  * All wrappers must start with the "_ksys_" prefix.
  15.  * I consider it mandatory to place the wrappers in the correct order in the official documentation.
  16.  * Enjoy writing your code :)
  17.  
  18.  * Warning! The end of the file is the old definitions of function/structure names.
  19.  * They are for compatibility... Better not to use them.
  20. */
  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. typedef union {
  197.     uint8_t raw_data[24];
  198.     struct {
  199.         uint32_t id;
  200.         uint8_t data[20];
  201.     };
  202. } ksys_signal_info_t;
  203.  
  204. #pragma pack(pop)
  205.  
  206. typedef rgb_t ksys_bitmap_t;
  207.  
  208. enum KSYS_FS_ERRORS {
  209.     KSYS_FS_ERR_SUCCESS = 0, // Success
  210.     KSYS_FS_ERR_1 = 1,       // Base and/or partition of a hard disk is not defined (fn21.7 & fn21.8)
  211.     KSYS_FS_ERR_2 = 2,       // Function is not supported for the given file system
  212.     KSYS_FS_ERR_3 = 3,       // Unknown file system
  213.     KSYS_FS_ERR_4 = 4,       // Reserved, is never returned in the current implementation
  214.     KSYS_FS_ERR_5 = 5,       // File not found
  215.     KSYS_FS_ERR_EOF = 6,     // End of file, EOF
  216.     KSYS_FS_ERR_7 = 7,       // Pointer lies outside of application memory
  217.     KSYS_FS_ERR_8 = 8,       // Disk is full
  218.     KSYS_FS_ERR_9 = 9,       // FAT table is destroyed
  219.     KSYS_FS_ERR_10 = 10,     // Access denied
  220.     KSYS_FS_ERR_11 = 11      // Device error
  221. };
  222.  
  223. enum KSYS_EVENTS {
  224.     KSYS_EVENT_NONE = 0,     /* Event queue is empty */
  225.     KSYS_EVENT_REDRAW = 1,   /* Window and window elements should be redrawn */
  226.     KSYS_EVENT_KEY = 2,      /* A key on the keyboard was pressed */
  227.     KSYS_EVENT_BUTTON = 3,   /* A button was clicked with the mouse */
  228.     KSYS_EVENT_DESKTOP = 5,  /* Desktop redraw finished */
  229.     KSYS_EVENT_MOUSE = 6,    /* Mouse activity (movement, button press) was detected */
  230.     KSYS_EVENT_IPC = 7,      /* Interprocess communication notify */
  231.     KSYS_EVENT_NETWORK = 8,  /* Network event */
  232.     KSYS_EVENT_DEBUG = 9,    /* Debug subsystem event */
  233.     KSYS_EVENT_IRQBEGIN = 16 /* 16..31 IRQ0..IRQ15 interrupt =IRQBEGIN+IRQn */
  234. };
  235.  
  236. enum KSYS_FILE_ENCODING {
  237.     KSYS_FILE_CP866 = 1,
  238.     KSYS_FILE_UTF16LE = 2,
  239.     KSYS_FILE_UTF8 = 3
  240. };
  241.  
  242. enum KSYS_TITLE_ENCODING {
  243.     KSYS_TITLE_CP866 = 1,
  244.     KSYS_TITLE_UTF16LE = 2,
  245.     KSYS_TITLE_UTF8 = 3
  246. };
  247.  
  248. enum KSYS_SCANCODES {
  249.     KSYS_SCANCODE_ANY = 0,
  250.     KSYS_SCANCODE_ESC = 1,
  251.     KSYS_SCANCODE_1 = 2,
  252.     KSYS_SCANCODE_2 = 3,
  253.     KSYS_SCANCODE_3 = 4,
  254.     KSYS_SCANCODE_4 = 5,
  255.     KSYS_SCANCODE_5 = 6,
  256.     KSYS_SCANCODE_6 = 7,
  257.     KSYS_SCANCODE_7 = 8,
  258.     KSYS_SCANCODE_8 = 9,
  259.     KSYS_SCANCODE_9 = 10,
  260.     KSYS_SCANCODE_0 = 11,
  261.     KSYS_SCANCODE_MINUS = 12,
  262.     KSYS_SCANCODE_EQUAL = 13,
  263.     KSYS_SCANCODE_BACKSPACE = 14,
  264.     KSYS_SCANCODE_TAB = 15,
  265.     KSYS_SCANCODE_Q = 16,
  266.     KSYS_SCANCODE_W = 17,
  267.     KSYS_SCANCODE_E = 18,
  268.     KSYS_SCANCODE_R = 19,
  269.     KSYS_SCANCODE_T = 20,
  270.     KSYS_SCANCODE_Y = 21,
  271.     KSYS_SCANCODE_U = 22,
  272.     KSYS_SCANCODE_I = 23,
  273.     KSYS_SCANCODE_O = 24,
  274.     KSYS_SCANCODE_P = 25,
  275.     KSYS_SCANCODE_LBRACE = 26,
  276.     KSYS_SCANCODE_RBRACE = 27,
  277.     KSYS_SCANCODE_ENTER = 28,
  278.     KSYS_SCANCODE_EXT_NUMPAD_ENTER = 28,
  279.     KSYS_SCANCODE_LCTRL = 29,
  280.     KSYS_SCANCODE_EXT_RCTRL = 29,
  281.     KSYS_SCANCODE_A = 30,
  282.     KSYS_SCANCODE_S = 31,
  283.     KSYS_SCANCODE_D = 32,
  284.     KSYS_SCANCODE_F = 33,
  285.     KSYS_SCANCODE_G = 34,
  286.     KSYS_SCANCODE_H = 35,
  287.     KSYS_SCANCODE_J = 36,
  288.     KSYS_SCANCODE_K = 37,
  289.     KSYS_SCANCODE_L = 38,
  290.     KSYS_SCANCODE_SEMICOLON = 39,
  291.     KSYS_SCANCODE_SQUOTE = 40,
  292.     KSYS_SCANCODE_BQUOTE = 41,
  293.     KSYS_SCANCODE_LSHIFT = 42,
  294.     KSYS_SCANCODE_BACKSLASH = 43,
  295.     KSYS_SCANCODE_Z = 44,
  296.     KSYS_SCANCODE_X = 45,
  297.     KSYS_SCANCODE_C = 46,
  298.     KSYS_SCANCODE_V = 47,
  299.     KSYS_SCANCODE_B = 48,
  300.     KSYS_SCANCODE_N = 49,
  301.     KSYS_SCANCODE_M = 50,
  302.     KSYS_SCANCODE_COMMA = 51,
  303.     KSYS_SCANCODE_POINT = 52,
  304.     KSYS_SCANCODE_SLASH = 53,
  305.     KSYS_SCANCODE_EXT_NUMPAD_DIV = 53,
  306.     KSYS_SCANCODE_RSHIFT = 54,
  307.     KSYS_SCANCODE_NUMPAD_MULT = 55,
  308.     KSYS_SCANCODE_EXT_PRTSCR = 55,
  309.     KSYS_SCANCODE_LALT = 56,
  310.     KSYS_SCANCODE_EXT_RALT = 56,
  311.     KSYS_SCANCODE_SPACE = 57,
  312.     KSYS_SCANCODE_CAPSLOCK = 58,
  313.     KSYS_SCANCODE_F1 = 59,
  314.     KSYS_SCANCODE_F2 = 60,
  315.     KSYS_SCANCODE_F3 = 61,
  316.     KSYS_SCANCODE_F4 = 62,
  317.     KSYS_SCANCODE_F5 = 63,
  318.     KSYS_SCANCODE_F6 = 64,
  319.     KSYS_SCANCODE_F7 = 65,
  320.     KSYS_SCANCODE_F8 = 66,
  321.     KSYS_SCANCODE_F9 = 67,
  322.     KSYS_SCANCODE_F10 = 68,
  323.     KSYS_SCANCODE_NUMLOCK = 69,
  324.     KSYS_SCANCODE_SCRLOCK = 70,
  325.     KSYS_SCANCODE_NUMPAD_7 = 71,
  326.     KSYS_SCANCODE_EXT_HOME = 71,
  327.     KSYS_SCANCODE_NUMPAD_8 = 72,
  328.     KSYS_SCANCODE_EXT_UP = 72,
  329.     KSYS_SCANCODE_NUMPAD_9 = 73,
  330.     KSYS_SCANCODE_EXT_PGUP = 73,
  331.     KSYS_SCANCODE_NUMPAD_MINUS = 74,
  332.     KSYS_SCANCODE_NUMPAD_4 = 75,
  333.     KSYS_SCANCODE_EXT_LEFT = 75,
  334.     KSYS_SCANCODE_NUMPAD_5 = 76,
  335.     KSYS_SCANCODE_NUMPAD_6 = 77,
  336.     KSYS_SCANCODE_EXT_RIGHT = 77,
  337.     KSYS_SCANCODE_NUMPAD_PLUS = 78,
  338.     KSYS_SCANCODE_NUMPAD_1 = 79,
  339.     KSYS_SCANCODE_EXT_END = 79,
  340.     KSYS_SCANCODE_NUMPAD_2 = 80,
  341.     KSYS_SCANCODE_EXT_DOWN = 80,
  342.     KSYS_SCANCODE_NUMPAD_3 = 81,
  343.     KSYS_SCANCODE_EXT_PGDOWN = 81,
  344.     KSYS_SCANCODE_NUMPAD_0 = 82,
  345.     KSYS_SCANCODE_EXT_INSERT = 82,
  346.     KSYS_SCANCODE_NUMPAD_COMMA = 83,
  347.     KSYS_SCANCODE_EXT_DELETE = 83,
  348.     //84-86 doesn't exist
  349.     KSYS_SCANCODE_F11 = 87,
  350.     KSYS_SCANCODE_F12 = 88,
  351.     //89,90 doesn't exist
  352.     KSYS_SCANCODE_EXT_LWIN = 91,
  353.     KSYS_SCANCODE_EXT_RWIN = 92,
  354.     KSYS_SCANCODE_EXT_MENU = 93,
  355.     KSYS_SCANCODE_EXT = 0xE0,
  356.  
  357.     KSYS_SCANCODE_UNK_M_UP = 250 // Is it needed?
  358. };
  359.  
  360. KOSAPI int __strcmp(const char* s1, const char* s2)
  361. {
  362.     while ((*s1) && (*s1 == *s2)) {
  363.         ++s1;
  364.         ++s2;
  365.     }
  366.     return (*(unsigned char*)s1 - *(unsigned char*)s2);
  367. }
  368.  
  369. /* ####################################################################### */
  370. /* ############### С wrappers for system calls Kolibri OS ################ */
  371. /* ####################################################################### */
  372.  
  373. /*=============== Function -1 - terminate thread/process ===============*/
  374.  
  375. KOSAPI void _ksys_exit(void)
  376. {
  377.     asm_inline("int $0x40" ::"a"(-1));
  378. }
  379.  
  380. /*============== Function 0 - define and draw the window. ==============*/
  381.  
  382. 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)
  383. {
  384.     asm_inline(
  385.         "int $0x40"
  386.         :
  387.         : "a"(0),
  388.         "b"((x << 16) | ((w - 1) & 0xFFFF)),
  389.         "c"((y << 16) | ((h - 1) & 0xFFFF)),
  390.         "d"((style << 24) | (workcolor & 0xFFFFFF)),
  391.         "D"(name),
  392.         "S"(0)
  393.         : "memory");
  394. }
  395.  
  396. /*================ Function 1 - put pixel in the window. ===============*/
  397.  
  398. KOSAPI void _ksys_draw_pixel(uint32_t x, uint32_t y, ksys_color_t color)
  399. {
  400.     asm_inline("int $0x40" ::"a"(1), "b"(x), "c"(y), "d"(color));
  401. }
  402.  
  403. /*============ Function 2 - get the code of the pressed key. ===========*/
  404.  
  405. KOSAPI ksys_oskey_t _ksys_get_key(void)
  406. {
  407.     ksys_oskey_t val;
  408.     asm_inline(
  409.         "int $0x40"
  410.         : "=a"(val.val)
  411.         : "a"(2));
  412.     return val;
  413. }
  414.  
  415. /*==================== Function 3 - get system time. ===================*/
  416.  
  417. KOSAPI ksys_time_t _ksys_get_time(void)
  418. {
  419.     ksys_time_t c_time;
  420.     asm_inline(
  421.         "int $0x40"
  422.         : "=a"(c_time)
  423.         : "a"(3)
  424.         : "memory");
  425.     return c_time;
  426. }
  427.  
  428. /*=================== Function 4 - draw text string. ===================*/
  429. KOSAPI void _ksys_draw_text(const char* text, uint32_t x, uint32_t y, uint32_t len, ksys_color_t color)
  430. {
  431.     asm_inline(
  432.         "int $0x40"
  433.         :
  434.         : "a"(4), "d"(text),
  435.         "b"((x << 16) | y),
  436.         "S"(len), "c"(color)
  437.         : "memory");
  438. }
  439.  
  440. /*========================= Function 5 - delay. ========================*/
  441.  
  442. KOSAPI void _ksys_delay(uint32_t time)
  443. {
  444.     asm_inline(
  445.         "int $0x40"
  446.         :
  447.         : "a"(5), "b"(time)
  448.         : "memory");
  449. }
  450.  
  451. /*=============== Function 7 - draw image in the window. ===============*/
  452.  
  453. KOSAPI void _ksys_draw_bitmap(void* bitmap, int x, int y, int w, int h)
  454. {
  455.     asm_inline(
  456.         "int $0x40"
  457.         :
  458.         : "a"(7), "b"(bitmap),
  459.         "c"((w << 16) | h),
  460.         "d"((x << 16) | y)
  461.         : "memory");
  462. }
  463.  
  464. /*=============== Function 8 - define/delete the button. ===============*/
  465.  
  466. KOSAPI void _ksys_define_button(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t id, ksys_color_t color)
  467. {
  468.     asm_inline(
  469.         "int $0x40"
  470.         :
  471.         : "a"(8),
  472.         "b"((x << 16) + w),
  473.         "c"((y << 16) + h),
  474.         "d"(id),
  475.         "S"(color));
  476. };
  477.  
  478. KOSAPI void _ksys_delete_button(uint32_t id)
  479. {
  480.     asm_inline("int $0x40" ::"a"(8), "d"(id & 0x00FFFFFF | 0x80000000));
  481. }
  482.  
  483. /*============ Function 9 - information on execution thread. ===========*/
  484.  
  485. #define KSYS_THIS_SLOT -1
  486.  
  487. enum KSYS_SLOT_STATES {
  488.     KSYS_SLOT_STATE_RUNNING = 0,
  489.     KSYS_SLOT_STATE_SUSPENDED = 1,
  490.     KSYS_SLOT_STATE_SUSPENDED_WAIT_EVENT = 2,
  491.     KSYS_SLOT_STATE_NORMAL_TERM = 3,
  492.     KSYS_SLOT_STATE_EXCEPT_TERM = 4,
  493.     KSYS_SLOT_STATE_WAIT_EVENT = 5,
  494.     KSYS_SLOT_STATE_FREE = 9
  495. };
  496.  
  497. KOSAPI int _ksys_thread_info(ksys_thread_t* table, int slot)
  498. {
  499.     int val;
  500.     asm_inline(
  501.         "int $0x40"
  502.         : "=a"(val)
  503.         : "a"(9), "b"(table), "c"(slot)
  504.         : "memory");
  505.     return val;
  506. }
  507.  
  508. /*==================== Function 10 - wait for event. ===================*/
  509.  
  510. KOSAPI uint32_t _ksys_wait_event(void)
  511. {
  512.     uint32_t val;
  513.     asm_inline(
  514.         "int $0x40"
  515.         : "=a"(val)
  516.         : "a"(10));
  517.     return val;
  518. }
  519.  
  520. /*=============== Function 11 - check for event, no wait. ==============*/
  521.  
  522. KOSAPI uint32_t _ksys_check_event(void)
  523. {
  524.     uint32_t val;
  525.     asm_inline(
  526.         "int $0x40"
  527.         : "=a"(val)
  528.         : "a"(11));
  529.     return val;
  530. }
  531.  
  532. /*=============== Function 12 - begin/end window redraw. ===============*/
  533.  
  534. KOSAPI void _ksys_start_draw(void)
  535. {
  536.     asm_inline("int $0x40" ::"a"(12), "b"(1));
  537. }
  538.  
  539. KOSAPI void _ksys_end_draw(void)
  540. {
  541.     asm_inline("int $0x40" ::"a"(12), "b"(2));
  542. }
  543.  
  544. /*============ Function 13 - draw a rectangle in the window. ===========*/
  545.  
  546. KOSAPI void _ksys_draw_bar(uint32_t x, uint32_t y, uint32_t w, uint32_t h, ksys_color_t color)
  547. {
  548.     asm_inline(
  549.         "int $0x40"
  550.         :
  551.         : "a"(13), "d"(color),
  552.         "b"((x << 16) | w),
  553.         "c"((y << 16) | h));
  554. }
  555.  
  556. /*=================== Function 14 - get screen size. ===================*/
  557.  
  558. KOSAPI ksys_pos_t _ksys_screen_size(void)
  559. {
  560.     ksys_pos_t size;
  561.     asm_inline(
  562.         "int $0x40"
  563.         : "=a"(size)
  564.         : "a"(14));
  565.     return size;
  566. }
  567.  
  568. /*== Function 15, subfunction 1 - set a size of the background image. ==*/
  569.  
  570. KOSAPI void _ksys_bg_set_size(uint32_t w, uint32_t h)
  571. {
  572.     asm_inline("int $0x40" ::"a"(15), "b"(1), "c"(w), "d"(h));
  573. }
  574.  
  575. /*=== Function 15, subfunction 2 - put pixel on the background image. ==*/
  576.  
  577. KOSAPI void _ksys_bg_put_pixel(uint32_t x, uint32_t y, uint32_t w, ksys_color_t color)
  578. {
  579.     asm_inline("int $0x40" ::"a"(15), "b"(2), "c"((x + y * w) * 3), "d"(color));
  580. }
  581.  
  582. /*=========== Function 15, subfunction 3 - redraw background. ==========*/
  583.  
  584. KOSAPI void _ksys_bg_redraw(void)
  585. {
  586.     asm_inline("int $0x40" ::"a"(15), "b"(3));
  587. }
  588.  
  589. /*== Function 15, subfunction 4 - set drawing mode for the background. =*/
  590.  
  591. enum KSYS_BG_MODES {
  592.     KSYS_BG_MODE_PAVE = 1,
  593.     KSYS_BG_MODE_STRETCH = 2
  594. };
  595.  
  596. KOSAPI void _ksys_bg_set_mode(uint32_t mode)
  597. {
  598.     asm_inline("int $0x40" ::"a"(15), "b"(4), "c"(mode));
  599. }
  600.  
  601. /*===================== Function 15, subfunction 5 =====================*/
  602. /*============ Put block of pixels on the background image. ============*/
  603.  
  604. KOSAPI void _ksys_bg_put_bitmap(ksys_bitmap_t* bitmap, size_t bitmap_size, uint32_t x, uint32_t y, uint32_t w)
  605. {
  606.     asm_inline(
  607.         "int $0x40"
  608.         :
  609.         : "a"(15), "b"(5), "c"(bitmap), "d"((x + y * w) * 3), "S"(bitmap_size));
  610. }
  611.  
  612. /*===================== Function 15, subfunction 6 =====================*/
  613. /*======= Map background data to the address space of process. ==========*/
  614.  
  615. KOSAPI ksys_bitmap_t* _ksys_bg_get_map(void)
  616. {
  617.     ksys_bitmap_t* bitmap;
  618.     asm_inline(
  619.         "int $0x40"
  620.         : "=a"(bitmap)
  621.         : "a"(15), "b"(6));
  622.     return bitmap;
  623. }
  624.  
  625. /*===== Function 15, subfunction 7 - close mapped background data. =====*/
  626.  
  627. KOSAPI int _ksys_bg_close_map(ksys_bitmap_t* bitmap)
  628. {
  629.     int status; // 1 - OK, 0 - ERROR
  630.     asm_inline(
  631.         "int $0x40"
  632.         : "=a"(status)
  633.         : "a"(15), "b"(7), "c"(bitmap));
  634.     return status;
  635. }
  636.  
  637. /*===================== Function 15, subfunction 9 =====================*/
  638. /*============= Redraws a rectangular part of the background ===========*/
  639.  
  640. KOSAPI void _ksys_bg_redraw_bar(ksys_pos_t angle1, ksys_pos_t angle2)
  641. {
  642.     asm_inline(
  643.         "int $0x40"
  644.         :
  645.         : "a"(15), "b"(9),
  646.         "c"(angle1.x * (1 << 16) + angle2.x),
  647.         "d"(angle1.y * (1 << 16) + angle2.y));
  648. }
  649.  
  650. /*=============== Function 16 - save ramdisk on a floppy. ==============*/
  651.  
  652. KOSAPI int _ksys_save_ramdisk_fd(uint32_t floppy_id)
  653. {
  654.     int status; // 0 - OK, 1 - ERROR
  655.     asm_inline(
  656.         "int $0x40"
  657.         : "=a"(status)
  658.         : "a"(16), "b"(floppy_id));
  659.     return status;
  660. }
  661.  
  662. /*======= Function 17 - get the identifier of the pressed button. ======*/
  663.  
  664. KOSAPI uint32_t _ksys_get_button(void)
  665. {
  666.     unsigned val;
  667.     asm_inline(
  668.         "int $0x40"
  669.         : "=a"(val)
  670.         : "a"(17));
  671.     return val >> 8;
  672. }
  673.  
  674. /*===================== Function 18, subfunction 1 =====================*/
  675. /*============= Make deactive the window of the given thread. ==========*/
  676.  
  677. KOSAPI void _ksys_unfocus_window(int slot)
  678. {
  679.     asm_inline("int $0x40" ::"a"(18), "b"(1), "c"(slot));
  680. }
  681.  
  682. /*= Function 18, subfunction 2 - terminate process/thread by the slot. =*/
  683.  
  684. KOSAPI void _ksys_kill_by_slot(int slot)
  685. {
  686.     asm_inline(
  687.         "int $0x40" ::"a"(18), "b"(2), "c"(slot));
  688. }
  689.  
  690. /*===================== Function 18, subfunction 3 =====================*/
  691. /*============= Make active the window of the given thread. ============*/
  692.  
  693. KOSAPI void _ksys_focus_window(int slot)
  694. {
  695.     asm_inline("int $0x40" ::"a"(18), "b"(3), "c"(slot));
  696. }
  697.  
  698. /*===================== Function 18, subfunction 4 =====================*/
  699. /*=========== Get counter of idle time units per one second. ===========*/
  700.  
  701. KOSAPI uint32_t _ksys_get_idle(void)
  702. {
  703.     uint32_t sec;
  704.     asm_inline(
  705.         "int $0x40"
  706.         : "=a"(sec)
  707.         : "a"(18), "b"(4));
  708.     return sec;
  709. }
  710.  
  711. /*========== Function 18, subfunction 5 - get CPU clock rate. ==========*/
  712. /*================ modulo 2^32 clock ticks = 4GHz ======================*/
  713.  
  714. KOSAPI uint32_t _ksys_get_cpu_clock(void)
  715. {
  716.     uint32_t clock;
  717.     asm_inline(
  718.         "int $0x40"
  719.         : "=a"(clock)
  720.         : "a"(18), "b"(5));
  721.     return clock;
  722. }
  723.  
  724. /* Function 18, subfunction 6 - save ramdisk to the file on hard drive. */
  725.  
  726. KOSAPI uint32_t _ksys_save_ramdisk_hd(const char* ramdisk_path)
  727. {
  728.     uint32_t fs_err;
  729.     asm_inline(
  730.         "int $0x40"
  731.         : "=a"(fs_err)
  732.         : "a"(18), "b"(6), "c"(ramdisk_path));
  733.     return fs_err;
  734. }
  735.  
  736. /* Function 18, subfunction 9 - system shutdown with the parameter. */
  737.  
  738. enum KSYS_SHD_PARAM {
  739.     KSYS_SHD_POWEROFF = 2,
  740.     KSYS_SHD_REBOOT = 3,
  741.     KSYS_SHD_RESTART_KRN = 4
  742. };
  743.  
  744. KOSAPI void _ksys_shutdown(uint32_t shd_param)
  745. {
  746.     asm_inline("int $0x40" ::"a"(18), "b"(9), "c"(shd_param));
  747. }
  748.  
  749. /*========= Function 18, subfunction 16 - get size of free RAM. ========*/
  750.  
  751. KOSAPI size_t _ksys_get_ram_size(void)
  752. {
  753.     size_t size;
  754.     asm_inline(
  755.         "int $0x40"
  756.         : "=a"(size)
  757.         : "a"(18), "b"(16));
  758.     return size;
  759. }
  760.  
  761. /*======== Function 18, subfunction 17 - get full amount of RAM. =======*/
  762.  
  763. KOSAPI size_t _ksys_get_full_ram(void)
  764. {
  765.     size_t size;
  766.     asm_inline(
  767.         "int $0x40"
  768.         : "=a"(size)
  769.         : "a"(18), "b"(17));
  770.     return size;
  771. }
  772.  
  773. /*===================== Function 18, subfunction 18 ====================*/
  774. /*============= Terminate process/thread by the identifier. ============*/
  775.  
  776. KOSAPI void _ksys_kill_by_pid(uint32_t PID)
  777. {
  778.     asm_inline("int $0x40" ::"a"(18), "b"(18), "c"(PID));
  779. }
  780.  
  781. /*========= Fuction 18, subfunction 19 - get/set mouse settings. ========*/
  782.  
  783. typedef enum KSYS_MOUSE_SETTINGS {
  784.     KSYS_MOUSE_GET_SPEED = 0,              // Get mouse speed
  785.     KSYS_MOUSE_SET_SPEED = 1,              // Set mouse speed
  786.     KSYS_MOUSE_GET_SENS = 2,               // Get mouse sensitivity
  787.     KSYS_MOUSE_SET_SENS = 3,               // Set mouse sensitivity
  788.     KSYS_MOUSE_SET_POS = 4,                // Set the position of the mouse cursor
  789.     KSYS_MOUSE_SIM_STATE = 5,              // Simulate the state of the mouse buttons
  790.     KSYS_MOUSE_GET_DOUBLE_CLICK_DELAY = 6, // Get double click delay.
  791.     KSYS_MOUSE_SET_DOUBLE_CLICK_DELAY = 7  // Set double click delay.
  792. } ksys_mouse_settings_t;
  793.  
  794. KOSAPI uint32_t _ksys_set_mouse_settings(ksys_mouse_settings_t settings, uint32_t arg)
  795. {
  796.     uint32_t retval;
  797.     asm_inline(
  798.         "int $0x40"
  799.         : "=a"(retval)
  800.         : "a"(18), "b"(19), "c"(settings), "d"(arg)
  801.         : "memory");
  802.     return retval;
  803. }
  804.  
  805. #define _ksys_set_mouse_pos(X, Y) _ksys_set_mouse_settings(KSYS_MOUSE_SET_POS, X * 65536 + Y)
  806.  
  807. /*===================== Function 18, subfunction 21 ====================*/
  808. /*=====Get the slot number of the process / thread by identifier.. =====*/
  809.  
  810. KOSAPI int _ksys_get_thread_slot(int PID)
  811. {
  812.     int val;
  813.     asm_inline(
  814.         "int $0x40"
  815.         : "=a"(val)
  816.         : "a"(18), "b"(21), "c"(PID));
  817.     return val;
  818. }
  819.  
  820. /*============= Function 23 - wait for event with timeout. =============*/
  821.  
  822. KOSAPI uint32_t _ksys_wait_event_timeout(uint32_t timeout)
  823. {
  824.     unsigned val;
  825.     asm_inline(
  826.         "int $0x40"
  827.         : "=a"(val)
  828.         : "a"(23), "b"(timeout));
  829.     return val;
  830. }
  831.  
  832. /*=== Function 26, subfunction 9 - get the value of the time counter. ==*/
  833.  
  834. KOSAPI uint32_t _ksys_get_tick_count(void)
  835. {
  836.     unsigned val;
  837.     asm_inline(
  838.         "int $0x40"
  839.         : "=a"(val)
  840.         : "a"(26), "b"(9));
  841.     return val;
  842. }
  843.  
  844. /*===================== Function 26, subfunction 10 ====================*/
  845. /*========== Get the value of the high precision time counter. =========*/
  846.  
  847. KOSAPI uint64_t _ksys_get_ns_count(void)
  848. {
  849.     uint64_t val;
  850.     asm_inline(
  851.         "int $0x40"
  852.         : "=A"(val)
  853.         : "a"(26), "b"(10));
  854.     return val;
  855. }
  856.  
  857. /*=================== Function 29 - get system date. ===================*/
  858.  
  859. KOSAPI ksys_date_t _ksys_get_date(void)
  860. {
  861.     ksys_date_t val;
  862.     asm_inline(
  863.         "int $0x40"
  864.         : "=a"(val)
  865.         : "a"(29));
  866.     return val;
  867. }
  868.  
  869. /*===========+ Function 30 - work with the current folder.==============*/
  870. /*--------- Subfunction 1 - set current folder for the thread. ---------*/
  871.  
  872. KOSAPI void _ksys_setcwd(char* dir)
  873. {
  874.     asm_inline("int $0x40" ::"a"(30), "b"(1), "c"(dir));
  875. }
  876.  
  877. /*--------- Subfunction 2 - get current folder for the thread. ---------*/
  878.  
  879. KOSAPI int _ksys_getcwd(char* buf, int bufsize)
  880. {
  881.     int val;
  882.     asm_inline(
  883.         "int $0x40"
  884.         : "=a"(val)
  885.         : "a"(30), "b"(2), "c"(buf), "d"(bufsize));
  886.     return val;
  887. }
  888.  
  889. /* ---- Subfunction 3 - install the add.system directory for the kernel ------*/
  890.  
  891. KOSAPI int _ksys_set_kernel_dir(ksys_dir_key_t* table)
  892. {
  893.     int val;
  894.     asm_inline(
  895.         "int $0x40"
  896.         : "=a"(val)
  897.         : "a"(30), "b"(3), "c"(table)
  898.         : "memory");
  899.     return val;
  900. }
  901.  
  902. /*=================== Function 37 - work with mouse. ===================*/
  903.  
  904. enum KSYS_MOUSE_POS {
  905.     KSYS_MOUSE_SCREEN_POS = 0,
  906.     KSYS_MOUSE_WINDOW_POS = 1
  907. };
  908.  
  909. KOSAPI ksys_pos_t _ksys_get_mouse_pos(int origin)
  910. {
  911.     ksys_pos_t pos;
  912.     asm_inline(
  913.         "int $0x40"
  914.         : "=a"(pos)
  915.         : "a"(37), "b"(origin));
  916.     return pos;
  917. }
  918.  
  919. enum KSYS_MOUSE_BUTTON {
  920.     KSYS_MOUSE_LBUTTON_PRESSED = (1 << 0),
  921.     KSYS_MOUSE_RBUTTON_PRESSED = (1 << 1),
  922.     KSYS_MOUSE_MBUTTON_PRESSED = (1 << 2),
  923.     KSYS_MOUSE_4BUTTON_PRESSED = (1 << 3),
  924.     KSYS_MOUSE_5BUTTON_PRESSED = (1 << 4)
  925. };
  926.  
  927. KOSAPI uint32_t _ksys_get_mouse_buttons(void) // subfunction 2 - states of the mouse buttons
  928. {
  929.     uint32_t val;
  930.     asm_inline(
  931.         "int $0x40"
  932.         : "=a"(val)
  933.         : "a"(37), "b"(2));
  934.     return val;
  935. }
  936.  
  937. KOSAPI uint32_t _ksys_get_mouse_eventstate(void) // subfunction 3 - states and events of the mouse buttons
  938. {
  939.     uint32_t val;
  940.     asm_inline(
  941.         "int $0x40"
  942.         : "=a"(val)
  943.         : "a"(37), "b"(3));
  944.     return val;
  945. }
  946.  
  947. enum KSYS_CURSOR_SRC {
  948.     KSYS_CURSOR_FROM_FILE = 0,
  949.     KSYS_CURSOR_FROM_MEM = 1,
  950.     KSYS_CURSOR_INDIRECT = 2
  951. };
  952.  
  953. KOSAPI void* _ksys_load_cursor(void* path, uint32_t flags) // subfunction 4 - load cursor
  954. {
  955.     void* val;
  956.     asm_inline(
  957.         "int $0x40"
  958.         : "=a"(val)
  959.         : "a"(37), "b"(4), "c"(path), "d"(flags)
  960.         : "memory");
  961.     return val;
  962. }
  963.  
  964. KOSAPI void* _ksys_set_cursor(void* cursor) // subfunction 5 - set cursor
  965. {
  966.     void* old;
  967.     asm_inline(
  968.         "int $0x40"
  969.         : "=a"(old)
  970.         : "a"(37), "b"(5), "c"(cursor));
  971.     return old;
  972. }
  973.  
  974. KOSAPI int _ksys_delete_cursor(void* cursor) // subfunction 6 - delete cursor
  975. {
  976.     int ret;
  977.     asm_inline(
  978.         "int $0x40"
  979.         : "=a"(ret)
  980.         : "a"(37), "b"(6), "c"(cursor)
  981.         : "memory");
  982.     return ret;
  983. }
  984.  
  985. KOSAPI uint32_t _ksys_get_mouse_wheels(void) // subfunction 7 - get scroll data
  986. {
  987.     uint32_t val;
  988.     asm_inline(
  989.         "int $0x40"
  990.         : "=a"(val)
  991.         : "a"(37), "b"(7));
  992.     return val;
  993. }
  994.  
  995. /*=========== Function 40 - set the mask for expected events. ==========*/
  996.  
  997. enum KSYS_EVENT_MASK {
  998.     KSYS_EVM_REDRAW = 1,
  999.     KSYS_EVM_KEY = 2,
  1000.     KSYS_EVM_BUTTON = 4,
  1001.     KSYS_EVM_EXIT = 8,
  1002.     KSYS_EVM_BACKGROUND = 16,
  1003.     KSYS_EVM_MOUSE = 32,
  1004.     KSYS_EVM_IPC = 64,
  1005.     KSYS_EVM_STACK = 128,
  1006.     KSYS_EVM_DEBUG = 256,
  1007.     KSYS_EVM_STACK2 = 512,
  1008.     KSYS_EVM_MOUSE_FILTER = 0x80000000,
  1009.     KSYS_EVM_CURSOR_FILTER = 0x40000000,
  1010. };
  1011.  
  1012. KOSAPI uint32_t _ksys_set_event_mask(uint32_t mask)
  1013. {
  1014.     unsigned val;
  1015.     asm_inline(
  1016.         "int $0x40"
  1017.         : "=a"(val)
  1018.         : "a"(40), "b"(mask));
  1019.     return val;
  1020. }
  1021.  
  1022. /*====================== Function 38 - draw line. ======================*/
  1023.  
  1024. KOSAPI void _ksys_draw_line(int xs, int ys, int xe, int ye, ksys_color_t color)
  1025. {
  1026.     asm_inline(
  1027.         "int $0x40"
  1028.         :
  1029.         : "a"(38), "d"(color),
  1030.         "b"((xs << 16) | xe),
  1031.         "c"((ys << 16) | ye));
  1032. }
  1033.  
  1034. /*============= Function 47 - draw a number in the window. =============*/
  1035.  
  1036. KOSAPI void _ksys_draw_number(int number, int x, int y, int len, ksys_color_t color)
  1037. {
  1038.     unsigned fmt;
  1039.     fmt = len << 16 | 0x80000000; // no leading zeros + width
  1040.     asm_inline(
  1041.         "int $0x40"
  1042.         :
  1043.         : "a"(47), "b"(fmt), "c"(number), "d"((x << 16) | y), "S"(color));
  1044. }
  1045.  
  1046. KOSAPI void _ksys_draw_number_bg(unsigned number, int x, int y, int len, ksys_color_t color, ksys_color_t bg)
  1047. {
  1048.     unsigned fmt;
  1049.     fmt = len << 16 | 0x80000000; // no leading zeros + width
  1050.     asm_inline(
  1051.         "int $0x40"
  1052.         :
  1053.         : "a"(47), "b"(fmt), "c"(number), "d"((x << 16) | y), "S"(color), "D"(bg));
  1054. }
  1055.  
  1056. /*====== Function 48, subfunction 3 - get standard window colors. ======*/
  1057.  
  1058. KOSAPI void _ksys_get_system_colors(ksys_colors_table_t* color_table)
  1059. {
  1060.     asm_inline("int $0x40" ::"a"(48), "b"(3), "c"(color_table), "d"(40));
  1061. }
  1062.  
  1063. /*============ Function 48, subfunction 4 - get skin height. ===========*/
  1064.  
  1065. KOSAPI uint32_t _ksys_get_skin_height()
  1066. {
  1067.     unsigned height;
  1068.     asm_inline(
  1069.         "int $0x40"
  1070.         : "=a"(height)
  1071.         : "a"(48), "b"(4));
  1072.     return height;
  1073. }
  1074.  
  1075. /*==================== Function 51 - create thread. ====================*/
  1076.  
  1077. KOSAPI int _ksys_create_thread(void* thread_entry, void* stack_top)
  1078. {
  1079.     int val;
  1080.     asm_inline(
  1081.         "int $0x40"
  1082.         : "=a"(val)
  1083.         : "a"(51), "b"(1), "c"(thread_entry), "d"(stack_top)
  1084.         : "memory");
  1085.     return val;
  1086. }
  1087.  
  1088. /*==================== Function 54, subfunction 0 ======================*/
  1089. /*============== Get the number of slots in the clipboard. =============*/
  1090.  
  1091. enum KSYS_CLIP_ENCODING {
  1092.     KSYS_CLIP_UTF8 = 0,
  1093.     KSYS_CLIP_CP866 = 1,
  1094.     KSYS_CLIP_CP1251 = 2
  1095. };
  1096.  
  1097. enum KSYS_CLIP_TYPES {
  1098.     KSYS_CLIP_TEXT = 0,
  1099.     KSYS_CLIP_IMAGE = 1,
  1100.     KSYS_CLIP_RAW = 2
  1101. };
  1102.  
  1103. KOSAPI int _ksys_clip_num(void)
  1104. {
  1105.     unsigned val;
  1106.     asm_inline(
  1107.         "int $0x40"
  1108.         : "=a"(val)
  1109.         : "a"(54), "b"(0));
  1110.     return val;
  1111. }
  1112.  
  1113. /*==================== Function 54, subfunction 1 ======================*/
  1114. /*================= Read the data from the clipboard. ==================-*/
  1115.  
  1116. KOSAPI char* _ksys_clip_get(int n) // returned buffer must be freed by _ksys_free()
  1117. {
  1118.     char* val;
  1119.     asm_inline(
  1120.         "int $0x40"
  1121.         : "=a"(val)
  1122.         : "a"(54), "b"(1), "c"(n));
  1123.     return val;
  1124. }
  1125.  
  1126. /*==================== Function 54, subfunction 2 ======================*/
  1127. /*================= Write the data to the clipboard. ===================*/
  1128.  
  1129. KOSAPI int _ksys_clip_set(int n, char* buffer)
  1130. {
  1131.     unsigned val;
  1132.     asm_inline(
  1133.         "int $0x40"
  1134.         : "=a"(val)
  1135.         : "a"(54), "b"(2), "c"(n), "d"(buffer)
  1136.         : "memory");
  1137.     return val;
  1138. }
  1139.  
  1140. /*===================== Function 54, subfunction 3 =====================*/
  1141. /*================ Delete the last slot in the clipboard ===============*/
  1142.  
  1143. KOSAPI int _ksys_clip_pop()
  1144. {
  1145.     unsigned val;
  1146.     asm_inline(
  1147.         "int $0x40"
  1148.         : "=a"(val)
  1149.         : "a"(54), "b"(3));
  1150.     return val;
  1151. }
  1152.  
  1153. /*===================== Function 54, subfunction 4 =====================*/
  1154. /*===================== Alarm reset the lock buffer ====================*/
  1155.  
  1156. KOSAPI int _ksys_clip_unlock()
  1157. {
  1158.     unsigned val;
  1159.     asm_inline(
  1160.         "int $0x40"
  1161.         : "=a"(val)
  1162.         : "a"(54), "b"(4));
  1163.     return val;
  1164. }
  1165.  
  1166. /*============== Function 63 - work with the debug board. ==============*/
  1167.  
  1168. KOSAPI void _ksys_debug_putc(char c)
  1169. {
  1170.     asm_inline("int $0x40" ::"a"(63), "b"(1), "c"(c));
  1171. }
  1172.  
  1173. KOSAPI void _ksys_debug_puts(const char* s)
  1174. {
  1175.     unsigned i = 0;
  1176.     while (*(s + i)) {
  1177.         asm_inline("int $0x40" ::"a"(63), "b"(1), "c"(*(s + i)));
  1178.         i++;
  1179.     }
  1180. }
  1181.  
  1182. /*========= Function 65 - draw image with the palette in window. =============*/
  1183.  
  1184. KOSAPI void ksys_draw_bitmap_palette(void* bitmap, int x, int y, int w, int h, int bpp, void* palette, int offset)
  1185. {
  1186.     asm_inline(
  1187.         "pushl %%ebp,\n\t"            // save EBP register
  1188.         "movl 0x24(%%ebp), %%ebp\n\t" // 0x24 - "offset" param
  1189.         "int $0x40\n\t"
  1190.         "popl %%ebp" // restore EBP register
  1191.         :
  1192.         : "a"(65),
  1193.         "b"(bitmap),
  1194.         "c"((w << 16) + h),
  1195.         "d"((x << 16) + y),
  1196.         "S"(bpp),
  1197.         "D"(palette));
  1198. }
  1199.  
  1200. /*========= Function 66, subfunction 1 - set keyboard input mode. ==============*/
  1201.  
  1202. typedef enum KSYS_KEY_INPUT_MODE {
  1203.     KSYS_KEY_INPUT_MODE_ASCII = 0,
  1204.     KSYS_KEY_INPUT_MODE_SCANC = 1,
  1205. } ksys_key_input_mode_t;
  1206.  
  1207. KOSAPI void _ksys_set_key_input_mode(ksys_key_input_mode_t mode)
  1208. {
  1209.     asm_inline("int $0x40" ::"a"(66), "b"(1), "c"(mode));
  1210. }
  1211.  
  1212. /*========= Function 66, subfunction 3 - get the state of the control keys. ========*/
  1213.  
  1214. enum KSYS_CONTROL_KEYS {
  1215.     KSYS_CONTROL_LSHIFT = (1 << 0),
  1216.     KSYS_CONTROL_RSHIFT = (1 << 1),
  1217.     KSYS_CONTROL_LCTRL = (1 << 2),
  1218.     KSYS_CONTROL_RCTRL = (1 << 3),
  1219.     KSYS_CONTROL_LALT = (1 << 4),
  1220.     KSYS_CONTROL_RALT = (1 << 5),
  1221.     KSYS_CONTROL_CAPS = (1 << 6),
  1222.     KSYS_CONTROL_NUM_LOCK = (1 << 7),
  1223.     KSYS_CONTROL_SCROLL_LOCK = (1 << 8)
  1224. };
  1225.  
  1226. KOSAPI uint32_t _ksys_get_control_key_state(void)
  1227. {
  1228.     uint32_t key_state;
  1229.     asm_inline(
  1230.         "int $0x40"
  1231.         : "=a"(key_state)
  1232.         : "a"(66), "b"(3));
  1233.     return key_state;
  1234. }
  1235.  
  1236. /*========= Function 66, subfunction 4 - set system-wide hotkey. ========*/
  1237.  
  1238. enum KSYS_SYS_HOTKEY_CONTROL_KEY_STATES {
  1239.     KSYS_SYS_HOTKEY_SHIFT_NONE = 0x0,
  1240.     KSYS_SYS_HOTKEY_SHIFT_ONE = 0x1,
  1241.     KSYS_SYS_HOTKEY_SHIFT_BOTH = 0x2,
  1242.     KSYS_SYS_HOTKEY_SHIFT_LEFTONLY = 0x3,
  1243.     KSYS_SYS_HOTKEY_SHIFT_RIGHTONLY = 0x4,
  1244.  
  1245.     KSYS_SYS_HOTKEY_CTRL_NONE = 0x00,
  1246.     KSYS_SYS_HOTKEY_CTRL_ONE = 0x10,
  1247.     KSYS_SYS_HOTKEY_CTRL_BOTH = 0x20,
  1248.     KSYS_SYS_HOTKEY_CTRL_LEFTONLY = 0x30,
  1249.     KSYS_SYS_HOTKEY_CTRL_RIGHTONLY = 0x40,
  1250.  
  1251.     KSYS_SYS_HOTKEY_ALT_NONE = 0x000,
  1252.     KSYS_SYS_HOTKEY_ALT_ONE = 0x100,
  1253.     KSYS_SYS_HOTKEY_ALT_BOTH = 0x200,
  1254.     KSYS_SYS_HOTKEY_ALT_LEFTONLY = 0x300,
  1255.     KSYS_SYS_HOTKEY_ALT_RIGHTONLY = 0x400,
  1256. };
  1257.  
  1258. KOSAPI int _ksys_set_sys_hotkey(uint8_t scancode, uint16_t control_key_states)
  1259. {
  1260.     int res;
  1261.     asm_inline(
  1262.         "int $0x40"
  1263.         : "=a"(res)
  1264.         : "a"(66), "b"(4), "c"(scancode), "d"(control_key_states)
  1265.         : "memory");
  1266.     return res;
  1267. }
  1268.  
  1269. /*========= Function 66, subfunction 5 - delete installed hotkey. ========*/
  1270.  
  1271. KOSAPI int _ksys_del_sys_hotkey(uint8_t scancode, uint16_t control_key_states)
  1272. {
  1273.     int res;
  1274.     asm_inline(
  1275.         "int $0x40"
  1276.         : "=a"(res)
  1277.         : "a"(66), "b"(5), "c"(scancode), "d"(control_key_states)
  1278.         : "memory");
  1279.     return res;
  1280. }
  1281.  
  1282. /*========= Function 67 - change position/sizes of the window. =========*/
  1283.  
  1284. KOSAPI void _ksys_change_window(int new_x, int new_y, int new_w, int new_h)
  1285. {
  1286.     asm_inline("int $0x40" ::"a"(67), "b"(new_x), "c"(new_y), "d"(new_w), "S"(new_h));
  1287. }
  1288.  
  1289. /*===== Function 68, subfunction 1 - switch to the next thread of execution ====*/
  1290.  
  1291. KOSAPI void _ksys_thread_yield(void)
  1292. {
  1293.     asm_inline("int $0x40" ::"a"(68), "b"(1));
  1294. }
  1295.  
  1296. /*======== Function 68, subfunction 12 - allocate memory block. ========*/
  1297.  
  1298. KOSAPI void* _ksys_alloc(size_t size)
  1299. {
  1300.     void* val;
  1301.     asm_inline(
  1302.         "int $0x40"
  1303.         : "=a"(val)
  1304.         : "a"(68), "b"(12), "c"(size));
  1305.     return val;
  1306. }
  1307.  
  1308. /*========== Function 68, subfunction 13 - free memory block. ==========*/
  1309.  
  1310. KOSAPI int _ksys_free(void* mem)
  1311. {
  1312.     int val;
  1313.     asm_inline(
  1314.         "int $0x40"
  1315.         : "=a"(val)
  1316.         : "a"(68), "b"(13), "c"(mem));
  1317.     return val;
  1318. }
  1319.  
  1320. /*====== Function 68, subfunction 14 - Wait signal from other applications/drivers. =====*/
  1321.  
  1322. KOSAPI void _ksys_wait_signal(ksys_signal_info_t* signal)
  1323. {
  1324.     asm_inline(
  1325.         "int $0x40"
  1326.         :
  1327.         : "a"(68), "b"(14), "c"(signal)
  1328.         : "memory");
  1329. }
  1330.  
  1331. /*============= Function 68, subfunction 16 - load driver. =============*/
  1332.  
  1333. KOSAPI ksys_drv_hand_t _ksys_load_driver(char* driver_name)
  1334. {
  1335.     ksys_drv_hand_t driver_h;
  1336.     asm_inline(
  1337.         "int $0x40"
  1338.         : "=a"(driver_h)
  1339.         : "a"(68), "b"(16), "c"(driver_name));
  1340.     return driver_h;
  1341. }
  1342.  
  1343. /*============ Function 68, subfunction 17 - driver control. ===========*/
  1344.  
  1345. KOSAPI int _ksys_driver_control(ksys_ioctl_t* ioctl)
  1346. {
  1347.     int status;
  1348.     asm_inline(
  1349.         "int $0x40"
  1350.         : "=a"(status)
  1351.         : "a"(68), "b"(17), "c"(ioctl)
  1352.         : "memory");
  1353.     return status;
  1354. }
  1355.  
  1356. /*== Function 68, subfunction 18 - subfunction 19 - load DLL (MS COFF) ==*/
  1357.  
  1358. KOSAPI ksys_dll_t* _ksys_dlopen(const char* path)
  1359. {
  1360.     ksys_dll_t* table;
  1361.     asm_inline(
  1362.         "int $0x40"
  1363.         : "=a"(table)
  1364.         : "a"(68), "b"(19), "c"(path)
  1365.         : "memory");
  1366.     return table;
  1367. }
  1368.  
  1369. /* It is not a system call, it serves as an auxiliary tool*/
  1370.  
  1371. KOSAPI void* _ksys_dlsym(ksys_dll_t* table, const char* fun_name)
  1372. {
  1373.     unsigned i = 0;
  1374.     while (1) {
  1375.         if (!(table + i)->func_name) {
  1376.             break;
  1377.         } else {
  1378.             if (!__strcmp(fun_name, (table + i)->func_name)) {
  1379.                 return (table + i)->func_ptr;
  1380.             }
  1381.         }
  1382.         i++;
  1383.     }
  1384.     return NULL;
  1385. }
  1386.  
  1387. /* Function 68, subfunction 20 - reallocate memory block.*/
  1388.  
  1389. KOSAPI void* _ksys_realloc(void* mem, size_t size)
  1390. {
  1391.     void* val;
  1392.     asm_inline(
  1393.         "int $0x40"
  1394.         : "=a"(val)
  1395.         : "a"(68), "b"(20), "c"(size), "d"(mem)
  1396.         : "memory");
  1397.     return val;
  1398. }
  1399.  
  1400. /* Function 68, subfunction 21 - load driver by full name. */
  1401.  
  1402. KOSAPI ksys_drv_hand_t _ksys_load_driver_opt(char* driver_path, char* cmd_line)
  1403. {
  1404.     ksys_drv_hand_t driver_h;
  1405.     asm_inline(
  1406.         "int $0x40"
  1407.         : "=a"(driver_h)
  1408.         : "a"(68), "b"(21), "c"(driver_path), "d"(cmd_line));
  1409.     return driver_h;
  1410. }
  1411.  
  1412. /*======== Function 68, subfunction 22 - open named memory area. =======*/
  1413.  
  1414. enum KSYS_SHM_MODE {
  1415.     KSYS_SHM_OPEN = 0x00,
  1416.     KSYS_SHM_OPEN_ALWAYS = 0x04,
  1417.     KSYS_SHM_CREATE = 0x08,
  1418.     KSYS_SHM_READ = 0x00,
  1419.     KSYS_SHM_WRITE = 0x01,
  1420. };
  1421.  
  1422. KOSAPI int _ksys_shm_open(char* name, int mode, int size, char** new_shm)
  1423. {
  1424.     int error;
  1425.     asm_inline(
  1426.         "int $0x40"
  1427.         : "=a"(*new_shm), "=d"(error)
  1428.         : "a"(68), "b"(22), "c"(name), "d"(size), "S"(mode));
  1429.     return error;
  1430. }
  1431.  
  1432. /*======= Function 68, subfunction 23 - close named memory area. =======*/
  1433.  
  1434. KOSAPI void _ksys_shm_close(char* shm_name)
  1435. {
  1436.     asm_inline(
  1437.         "int $0x40"
  1438.         :
  1439.         : "a"(68), "b"(23), "c"(shm_name));
  1440. }
  1441.  
  1442. /*====== Function 68, subfunction 26 - release memory pages ============*/
  1443.  
  1444. KOSAPI int* _ksys_unmap(void* base, size_t offset, size_t size)
  1445. {
  1446.     int* val;
  1447.     asm_inline(
  1448.         "int $0x40"
  1449.         : "=a"(val)
  1450.         : "a"(68), "b"(26), "c"(base), "d"(offset), "S"(size));
  1451.     return val;
  1452. }
  1453.  
  1454. /*========== Function 68, subfunction 27 - load file ===================*/
  1455.  
  1456. KOSAPI ksys_ufile_t _ksys_load_file(const char* path)
  1457. {
  1458.     ksys_ufile_t uf;
  1459.     asm_inline(
  1460.         "int $0x40"
  1461.         : "=a"(uf.data), "=d"(uf.size)
  1462.         : "a"(68), "b"(27), "c"(path)
  1463.         : "memory");
  1464.     return uf;
  1465. }
  1466.  
  1467. /*==== Function 68, subfunction 28 - load file, specifying the encoding ===*/
  1468.  
  1469. KOSAPI ksys_ufile_t _ksys_load_file_enc(const char* path, unsigned file_encoding)
  1470. {
  1471.     ksys_ufile_t uf;
  1472.     asm_inline(
  1473.         "int $0x40"
  1474.         : "=a"(uf.data), "=d"(uf.size)
  1475.         : "a"(68), "b"(28), "c"(path), "d"(file_encoding)
  1476.         : "memory");
  1477.     return uf;
  1478. }
  1479.  
  1480. /*==== Function 70 - work with file system with long names support. ====*/
  1481.  
  1482. KOSAPI ksys70_status_t _ksys70(const ksys70_t* k)
  1483. {
  1484.     ksys70_status_t status;
  1485.     asm_inline(
  1486.         "int $0x40"
  1487.         : "=a"(status.status), "=b"(status.rw_bytes)
  1488.         : "a"(70), "b"(k)
  1489.         : "memory");
  1490.     return status;
  1491. }
  1492.  
  1493. /*====== Function 70, subfunction 0 - read file with long names support. ======*/
  1494.  
  1495. KOSAPI ksys70_status_t _ksys_file_read(const char* name, uint64_t offset, uint32_t size, void* buf)
  1496. {
  1497.     ksys70_t k;
  1498.     k.p00 = 0;
  1499.     k.p04 = offset;
  1500.     k.p12 = size;
  1501.     k.buf16 = buf;
  1502.     k.p20 = 0;
  1503.     k.p21 = name;
  1504.     return _ksys70(&k);
  1505. }
  1506.  
  1507. /*===================== Function 70, subfunction 2 =====================*/
  1508. /*============ Create/rewrite file with long names support. ============*/
  1509.  
  1510. KOSAPI int _ksys_file_create(const char* name)
  1511. {
  1512.     ksys70_t k;
  1513.     k.p00 = 2;
  1514.     k.p04dw = 0;
  1515.     k.p08dw = 0;
  1516.     k.p12 = 0;
  1517.     k.p21 = name;
  1518.     return _ksys70(&k).status;
  1519. }
  1520.  
  1521. /*===================== Function 70, subfunction 3 =====================*/
  1522. /*=========== Write to existing file with long names support. ==========*/
  1523.  
  1524. KOSAPI ksys70_status_t _ksys_file_write(const char* name, uint64_t offset, uint32_t size, const void* buf)
  1525. {
  1526.     ksys70_t k;
  1527.     k.p00 = 3;
  1528.     k.p04 = offset;
  1529.     k.p12 = size;
  1530.     k.cbuf16 = buf;
  1531.     k.p20 = 0;
  1532.     k.p21 = name;
  1533.     return _ksys70(&k);
  1534. }
  1535.  
  1536. /*========== Function 70, subfunction 5 - get information on file/folder. =====*/
  1537.  
  1538. KOSAPI int _ksys_file_info(const char* name, ksys_bdfe_t* bdfe)
  1539. {
  1540.     ksys70_t k;
  1541.     k.p00 = 5;
  1542.     k.p04dw = 0;
  1543.     k.p08dw = 0;
  1544.     k.p12 = 0;
  1545.     k.bdfe = bdfe;
  1546.     k.p20 = 0;
  1547.     k.p21 = name;
  1548.     return _ksys70(&k).status;
  1549. }
  1550.  
  1551. #define _ksys_dir_info _ksys_file_info
  1552.  
  1553. /*=========== Function 70, subfunction 7 - start application. ===========*/
  1554.  
  1555. KOSAPI int _ksys_exec(const char* app_name, char* args)
  1556. {
  1557.     ksys70_t file_opt;
  1558.     file_opt.p00 = 7;
  1559.     file_opt.p04dw = 0;
  1560.     file_opt.p08dw = (uint32_t)args;
  1561.  
  1562.     file_opt.p12 = 0;
  1563.     file_opt.p16 = 0;
  1564.     file_opt.p20 = 0;
  1565.  
  1566.     file_opt.p21 = app_name;
  1567.     return _ksys70(&file_opt).status;
  1568. }
  1569.  
  1570. /*========== Function 70, subfunction 8 - delete file/folder. ==========*/
  1571.  
  1572. KOSAPI int _ksys_file_delete(const char* name)
  1573. {
  1574.     ksys70_t k;
  1575.     k.p00 = 8;
  1576.     k.p20 = 0;
  1577.     k.p21 = name;
  1578.     return _ksys70(&k).status;
  1579. }
  1580.  
  1581. /*============= Function 70, subfunction 9 - create folder. =============*/
  1582.  
  1583. KOSAPI int _ksys_mkdir(const char* path)
  1584. {
  1585.     ksys70_t dir_opt;
  1586.     dir_opt.p00 = 9;
  1587.     dir_opt.p21 = path;
  1588.     return _ksys70(&dir_opt).status;
  1589. }
  1590.  
  1591. /*============= Function 70, subfunction 10 - rename/move. =============*/
  1592.  
  1593. KOSAPI int _ksys_file_rename(const char* name, const char* new_name)
  1594. {
  1595.     ksys70_t k;
  1596.     k.p00 = 10;
  1597.     k.new_name = new_name;
  1598.     k.p20 = 0;
  1599.     k.p21 = name;
  1600.     return _ksys70(&k).status;
  1601. }
  1602.  
  1603. #define _ksys_dir_rename _ksys_file_rename
  1604.  
  1605. /*============= Function 71, subfunction 1 - set window title =============*/
  1606.  
  1607. KOSAPI void _ksys_set_window_title(const char* title)
  1608. {
  1609.     asm_inline(
  1610.         "int $0x40"
  1611.         :
  1612.         : "a"(71), "b"(1), "c"(title)
  1613.         : "memory");
  1614. }
  1615.  
  1616. #define _ksys_clear_window_title() _ksys_set_window_title(NULL)
  1617.  
  1618. /*============= Function 77, subfunction 0 - create futex object =============*/
  1619.  
  1620. KOSAPI uint32_t _ksys_futex_create(int* futex_ctrl)
  1621. {
  1622.     uint32_t futex_desc;
  1623.     asm_inline(
  1624.         "int $0x40"
  1625.         : "=a"(futex_desc)
  1626.         : "a"(77), "b"(0), "c"(futex_ctrl)
  1627.         : "memory");
  1628.     return futex_desc;
  1629. }
  1630.  
  1631. /*============= Function 77, subfunction 1 - destroy futex object =============*/
  1632.  
  1633. KOSAPI int _ksys_futex_destroy(uint32_t futex_desc)
  1634. {
  1635.     int res;
  1636.     asm_inline(
  1637.         "int $0x40"
  1638.         : "=a"(res)
  1639.         : "a"(77), "b"(1), "c"(futex_desc));
  1640.     return res;
  1641. }
  1642.  
  1643. /*============= Function 77, subfunction 2 - futex wait =============*/
  1644.  
  1645. KOSAPI int _ksys_futex_wait(uint32_t futex_desc, int ctrl_val, int timeout)
  1646. {
  1647.     int res;
  1648.     asm_inline(
  1649.         "int $0x40"
  1650.         : "=a"(res)
  1651.         : "a"(77), "b"(2), "c"(futex_desc), "d"(ctrl_val), "S"(timeout));
  1652.     return res;
  1653. }
  1654.  
  1655. /*============= Function 77, subfunction 3 - futex wake =============*/
  1656.  
  1657. KOSAPI int _ksys_futex_wake(uint32_t futex_desc, int max_wake_count)
  1658. {
  1659.     int count;
  1660.     asm_inline(
  1661.         "int $0x40"
  1662.         : "=a"(count)
  1663.         : "a"(77), "b"(3), "c"(futex_desc), "d"(max_wake_count));
  1664.     return count;
  1665. }
  1666.  
  1667. /*============= Function 77, subfunction 10 - read from file to buffer =============*/
  1668.  
  1669. KOSAPI int _ksys_posix_read(int pipefd, void* buff, int n)
  1670. {
  1671.     int count;
  1672.     asm_inline(
  1673.         "int $0x40"
  1674.         : "=a"(count)
  1675.         : "a"(77), "b"(10), "c"(pipefd), "d"(buff), "S"(n)
  1676.         : "memory");
  1677.     return count;
  1678. }
  1679.  
  1680. /*============= Function 77, subfunction 11 - write from buffer to file =============*/
  1681.  
  1682. KOSAPI int _ksys_posix_write(int pipefd, void* buff, int n)
  1683. {
  1684.     int count;
  1685.     asm_inline(
  1686.         "int $0x40"
  1687.         : "=a"(count)
  1688.         : "a"(77), "b"(11), "c"(pipefd), "d"(buff), "S"(n)
  1689.         : "memory");
  1690.     return count;
  1691. }
  1692.  
  1693. /*============= Function 77, subfunction 13 - create new pipe =============*/
  1694.  
  1695. KOSAPI int _ksys_posix_pipe2(int pipefd[2], int flags)
  1696. {
  1697.     int err;
  1698.     asm_inline(
  1699.         "int $0x40"
  1700.         : "=a"(err)
  1701.         : "a"(77), "b"(13), "c"(pipefd), "d"(flags)
  1702.         : "memory");
  1703.     return err;
  1704. }
  1705.  
  1706. /* ######### Old names of functions and structures. Do not use again! ##########*/
  1707.  
  1708. #define _ksys_get_event     _ksys_wait_event
  1709. #define _ksys_file_get_info _ksys_file_info
  1710.  
  1711. static inline int _ksys_file_read_file(const char* name, unsigned long long offset, unsigned size, void* buff, unsigned* bytes_read)
  1712. {
  1713.     ksys70_status_t res = _ksys_file_read(name, offset, size, buff);
  1714.     *bytes_read = res.rw_bytes;
  1715.     return res.status;
  1716. }
  1717.  
  1718. static inline int _ksys_file_write_file(const char* name, unsigned long long offset, unsigned size, const void* buff, unsigned* bytes_write)
  1719. {
  1720.     ksys70_status_t res = _ksys_file_write(name, offset, size, buff);
  1721.     *bytes_write = res.rw_bytes;
  1722.     return res.status;
  1723. }
  1724.  
  1725. #endif // _KSYS_H_
  1726.