Subversion Repositories Kolibri OS

Rev

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