Subversion Repositories Kolibri OS

Rev

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