Subversion Repositories Kolibri OS

Rev

Rev 6429 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. #ifndef __KOS_32_SYS_H__
  2. #define __KOS_32_SYS_H__
  3.  
  4. // file header taken from newlib
  5. // added many sys functions, compatible with tcc
  6.  
  7. //#include <newlib.h>
  8. //#include <stdint.h>
  9. #include <stddef.h>
  10. #include <stdarg.h>
  11. typedef unsigned int uint32_t;
  12. typedef int int32_t;
  13. typedef unsigned char uint8_t;
  14. typedef unsigned short int uint16_t;
  15. typedef unsigned long long uint64_t;
  16.  
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20.  
  21. //#ifdef CONFIG_DEBUF
  22. //  #define DBG(format,...) printf(format,##__VA_ARGS__)
  23. //#else
  24. //  #define DBG(format,...)
  25. //#endif
  26.  
  27. #define TYPE_3_BORDER_WIDTH  5
  28. #define WIN_STATE_MINIMIZED  0x02
  29. #define WIN_STATE_ROLLED     0x04
  30.  
  31. typedef  unsigned int color_t;
  32.  
  33.  
  34. typedef union __attribute__((packed)) pos_t
  35. {
  36.     uint32_t val;
  37.     struct
  38.     {
  39.         short  x;
  40.         short  y;
  41.     };
  42. } pos_t;
  43.  
  44.  
  45. typedef union __attribute__((packed)) oskey_t
  46. {
  47.     uint32_t val;
  48.     struct
  49.     {
  50.         uint8_t   state;
  51.         uint8_t   code;
  52.         uint16_t  ctrl_key;
  53.     };
  54. } oskey_t;
  55.  
  56. typedef struct
  57. {
  58.   unsigned      handle;
  59.   unsigned      io_code;
  60.   void          *input;
  61.   int           inp_size;
  62.   void          *output;
  63.   int           out_size;
  64. }ioctl_t;
  65.  
  66. static inline void begin_draw(void)
  67. {
  68.     __asm__ __volatile__(
  69.     "int $0x40" ::"a"(12),"b"(1));
  70. };
  71.  
  72. static inline
  73. void end_draw(void)
  74. {
  75.     __asm__ __volatile__(
  76.     "int $0x40" ::"a"(12),"b"(2));
  77. };
  78.  
  79. static inline
  80. void sys_create_window(int x, int y, int w, int h, const char *name,
  81.                        color_t workcolor, uint32_t style)
  82. {
  83.     __asm__ __volatile__(
  84.     "int $0x40"
  85.     ::"a"(0),
  86.      "b"((x << 16) | ((w-1) & 0xFFFF)),
  87.      "c"((y << 16) | ((h-1) & 0xFFFF)),
  88.      "d"((style << 24) | (workcolor & 0xFFFFFF)),
  89.      "D"(name),
  90.      "S"(0) : "memory");
  91. };
  92.  
  93. static inline
  94. void define_button(uint32_t x_w, uint32_t y_h, uint32_t id, uint32_t color)
  95. {
  96.     __asm__ __volatile__(
  97.     "int $0x40"
  98.     ::"a"(8),
  99.       "b"(x_w),
  100.       "c"(y_h),
  101.       "d"(id),
  102.       "S"(color));
  103. };
  104.  
  105. static inline
  106. void draw_line(int xs, int ys, int xe, int ye, color_t color)
  107. {
  108.     __asm__ __volatile__(
  109.     "int $0x40"
  110.     ::"a"(38), "d"(color),
  111.       "b"((xs << 16) | xe),
  112.       "c"((ys << 16) | ye));
  113. }
  114.  
  115. static inline
  116. void draw_bar(int x, int y, int w, int h, color_t color)
  117. {
  118.     __asm__ __volatile__(
  119.     "int $0x40"
  120.     ::"a"(13), "d"(color),
  121.       "b"((x << 16) | w),
  122.       "c"((y << 16) | h));
  123. }
  124.  
  125. static inline
  126. void draw_bitmap(void *bitmap, int x, int y, int w, int h)
  127. {
  128.     __asm__ __volatile__(
  129.     "int $0x40"
  130.     ::"a"(7), "b"(bitmap),
  131.       "c"((w << 16) | h),
  132.       "d"((x << 16) | y));
  133. }
  134.  
  135. static inline
  136. void draw_text_sys(const char *text, int x, int y, int len, color_t color)
  137. {
  138.     __asm__ __volatile__(
  139.     "int $0x40"
  140.     ::"a"(4),"d"(text),
  141.       "b"((x << 16) | y),
  142.       "S"(len),"c"(color)
  143.      :"memory");
  144. }
  145.  
  146. static inline
  147. uint32_t get_skin_height(void)
  148. {
  149.     uint32_t height;
  150.  
  151.     __asm__ __volatile__(
  152.     "int $0x40 \n\t"
  153.     :"=a"(height)
  154.     :"a"(48),"b"(4));
  155.     return height;
  156. };
  157.  
  158. /*
  159. // TinyC dont support aliasing of static inline funcs
  160. static inline void BeginDraw(void) __attribute__ ((alias ("begin_draw")));
  161. static inline void EndDraw(void) __attribute__ ((alias ("end_draw")));
  162. static inline void DrawWindow(int x, int y, int w, int h, const char *name,
  163.                               color_t workcolor, uint32_t style)
  164.                               __attribute__ ((alias ("sys_create_window")));
  165. static inline void DefineButton(void) __attribute__ ((alias ("define_button")));
  166. static inline void DrawLine(int xs, int ys, int xe, int ye, color_t color)
  167.                             __attribute__ ((alias ("draw_line")));
  168. static inline void DrawBar(int x, int y, int w, int h, color_t color)
  169.                            __attribute__ ((alias ("draw_bar")));
  170. static inline void DrawBitmap(void *bitmap, int x, int y, int w, int h)
  171.                               __attribute__ ((alias ("draw_bitmap")));
  172. static inline uint32_t GetSkinHeight(void) __attribute__ ((alias ("get_skin_height")));
  173. */
  174.  
  175. #define POS_SCREEN 0
  176. #define POS_WINDOW 1
  177.  
  178. static inline
  179. pos_t get_mouse_pos(int origin)
  180. {
  181.     pos_t val;
  182.  
  183.     __asm__ __volatile__(
  184.     "int $0x40 \n\t"
  185.     "rol $16, %%eax"
  186.     :"=a"(val)
  187.     :"a"(37),"b"(origin));
  188.     return val;
  189. }
  190.  
  191. static inline
  192. uint32_t get_mouse_buttons(void)
  193. {
  194.     uint32_t val;
  195.  
  196.     __asm__ __volatile__(
  197.     "int $0x40"
  198.     :"=a"(val)
  199.     :"a"(37),"b"(2));
  200.     return val;
  201. };
  202.  
  203. static inline
  204. uint32_t get_mouse_wheels(void)
  205. {
  206.     uint32_t val;
  207.  
  208.     __asm__ __volatile__(
  209.     "int $0x40 \n\t"
  210.     :"=a"(val)
  211.     :"a"(37),"b"(7));
  212.     return val;
  213. };
  214.  
  215. static inline uint32_t load_cursor(void *path, uint32_t flags)
  216. {
  217.     uint32_t  val;
  218.     __asm__ __volatile__(
  219.     "int $0x40"
  220.     :"=a"(val)
  221.     :"a"(37), "b"(4), "c"(path), "d"(flags));
  222.     return val;
  223. }
  224.  
  225. static inline uint32_t  set_cursor(uint32_t  cursor)
  226. {
  227.     uint32_t  old;
  228.     __asm__ __volatile__(
  229.     "int $0x40"
  230.     :"=a"(old)
  231.     :"a"(37), "b"(5), "c"(cursor));
  232.     return old;
  233. };
  234.  
  235. static inline int destroy_cursor(uint32_t cursor)
  236. {
  237.     int ret;
  238.     __asm__ __volatile__(
  239.     "int $0x40"
  240.     :"=a"(ret)
  241.     :"a"(37), "b"(6), "c"(cursor)
  242.     :"memory");
  243.     return ret;
  244. };
  245.  
  246. /*
  247. static inline pos_t GetMousePos(int origin) __attribute__ ((alias ("get_mouse_pos")));
  248. static inline uint32_t GetMouseButtons(void) __attribute__ ((alias ("get_mouse_buttons")));
  249. static inline uint32_t GetMouseWheels(void) __attribute__ ((alias ("get_mouse_wheels")));
  250.  
  251. static inline uint32_t  LoadCursor(void *path, uint32_t flags) __attribute__ ((alias ("load_cursor")));
  252. static inline uint32_t SetCursor(uint32_t  cursor) __attribute__ ((alias ("set_cursor")));
  253. static inline int DestroyCursor(uint32_t cursor) __attribute__ ((alias ("destroy_cursor")));
  254. */
  255.  
  256. static inline
  257. uint32_t wait_for_event(uint32_t time)
  258. {
  259.     uint32_t val;
  260.     __asm__ __volatile__(
  261.     "int $0x40"
  262.     :"=a"(val)
  263.     :"a"(23), "b"(time));
  264.     return val;
  265. };
  266.  
  267. static inline uint32_t check_os_event()
  268. {
  269.     uint32_t val;
  270.     __asm__ __volatile__(
  271.     "int $0x40"
  272.     :"=a"(val)
  273.     :"a"(11));
  274.     return val;
  275. };
  276.  
  277. static inline uint32_t get_os_event()
  278. {
  279.     uint32_t val;
  280.     __asm__ __volatile__(
  281.     "int $0x40"
  282.     :"=a"(val)
  283.     :"a"(10));
  284.     return val;
  285. };
  286. //static inline uint32_t GetOsEvent(void) __attribute__ ((alias ("get_os_event")));
  287.  
  288. static inline
  289. uint32_t get_tick_count(void)
  290. {
  291.     uint32_t val;
  292.     __asm__ __volatile__(
  293.     "int $0x40"
  294.     :"=a"(val)
  295.     :"a"(26),"b"(9));
  296.     return val;
  297. };
  298.  
  299. static inline
  300. uint64_t get_ns_count(void)
  301. {
  302.     uint64_t val;
  303.     __asm__ __volatile__(
  304.     "int $0x40"
  305.     :"=A"(val)
  306.     :"a"(26), "b"(10));
  307.     return val;
  308. };
  309.  
  310. static inline
  311. oskey_t get_key(void)
  312. {
  313.     oskey_t val;
  314.     __asm__ __volatile__(
  315.     "int $0x40"
  316.     :"=a"(val)
  317.     :"a"(2));
  318.     return val;
  319. }
  320.  
  321. static inline
  322. uint32_t get_os_button()
  323. {
  324.     uint32_t val;
  325.     __asm__ __volatile__(
  326.     "int $0x40"
  327.     :"=a"(val)
  328.     :"a"(17));
  329.     return val>>8;
  330. };
  331.  
  332. static inline uint32_t get_service(char *name)
  333. {
  334.     uint32_t retval = 0;
  335.     __asm__ __volatile__(
  336.     "int $0x40"
  337.     :"=a"(retval)
  338.     :"a"(68),"b"(16),"c"(name)
  339.     :"memory");
  340.  
  341.     return retval;
  342. };
  343.  
  344. static inline int call_service(ioctl_t *io)
  345. {
  346.     int retval;
  347.  
  348.     __asm__ __volatile__(
  349.     "int $0x40"
  350.     :"=a"(retval)
  351.     :"a"(68),"b"(17),"c"(io)
  352.     :"memory","cc");
  353.  
  354.     return retval;
  355. };
  356.  
  357.  
  358. static inline void yield(void)
  359. {
  360.     __asm__ __volatile__(
  361.     "int $0x40"
  362.     ::"a"(68), "b"(1));
  363. };
  364.  
  365. static inline void delay(uint32_t time)
  366. {
  367.     __asm__ __volatile__(
  368.     "int $0x40"
  369.     ::"a"(5), "b"(time)
  370.     :"memory");
  371. };
  372.  
  373. static inline
  374. void *user_alloc(size_t size)
  375. {
  376.     void  *val;
  377.     __asm__ __volatile__(
  378.     "int $0x40"
  379.     :"=a"(val)
  380.     :"a"(68),"b"(12),"c"(size));
  381.     return val;
  382. }
  383.  
  384. static inline
  385. int user_free(void *mem)
  386. {
  387.     int  val;
  388.     __asm__ __volatile__(
  389.     "int $0x40"
  390.     :"=a"(val)
  391.     :"a"(68),"b"(13),"c"(mem));
  392.     return val;
  393. }
  394.  
  395. static inline
  396. void* user_realloc(void *mem, size_t size)
  397. {
  398.     void *val;
  399.     __asm__ __volatile__(
  400.     "int $0x40"
  401.     :"=a"(val)
  402.     :"a"(68),"b"(20),"c"(size),"d"(mem)
  403.     :"memory");
  404.  
  405.     return val;
  406. };
  407.  
  408. static inline
  409. int *user_unmap(void *base, size_t offset, size_t size)
  410. {
  411.     int  *val;
  412.     __asm__ __volatile__(
  413.     "int $0x40"
  414.     :"=a"(val)
  415.     :"a"(68),"b"(26),"c"(base),"d"(offset),"S"(size));
  416.     return val;
  417. };
  418.  
  419. /*
  420. static inline void *UserAlloc(size_t size) __attribute__ ((alias ("user_alloc")));
  421. static inline int UserFree(void *mem) __attribute__ ((alias ("user_free")));
  422. static inline void* UserRealloc(void *mem, size_t size) __attribute__ ((alias ("user_realloc")));
  423. static inline int *UserUnmap(void *base, size_t offset, size_t size) __attribute__ ((alias ("user_unmap")));
  424. */
  425.  
  426. typedef union
  427. {
  428.     struct
  429.     {
  430.         void   *data;
  431.         size_t  size;
  432.     } x;
  433.     unsigned long long raw;
  434. }ufile_t;
  435.  
  436. static inline ufile_t load_file(const char *path)
  437. {
  438.     ufile_t uf;
  439.  
  440.     __asm__ __volatile__ (
  441.     "int $0x40"
  442.     :"=A"(uf.raw)
  443.     :"a" (68), "b"(27),"c"(path));
  444.  
  445.     return uf;
  446. };
  447. //static inline ufile_t LoadFile(const char *path) __attribute__ ((alias ("load_file")));
  448.  
  449. static inline int GetScreenSize()
  450. {
  451.     int retval;
  452.  
  453.     __asm__ __volatile__(
  454.     "int $0x40"
  455.     :"=a"(retval)
  456.     :"a"(61), "b"(1));
  457.     return retval;
  458. }
  459.  
  460.  
  461. static inline void get_proc_info(char *info)
  462. {
  463.     __asm__ __volatile__(
  464.     "int $0x40"
  465.     :
  466.     :"a"(9), "b"(info), "c"(-1)
  467.     :"memory");
  468. };
  469. //static inline void GetProcInfo(char *info) __attribute__ ((alias ("get_proc_info")));
  470.  
  471.  
  472. struct blit_call
  473. {
  474.     int dstx;
  475.     int dsty;
  476.     int w;
  477.     int h;
  478.  
  479.     int srcx;
  480.     int srcy;
  481.     int srcw;
  482.     int srch;
  483.  
  484.     void *bitmap;
  485.     int   stride;
  486. };
  487.  
  488. static inline void Blit(void *bitmap, int dst_x, int dst_y,
  489.                         int src_x, int src_y, int w, int h,
  490.                         int src_w, int src_h, int stride)
  491. {
  492.     volatile struct blit_call bc;
  493.  
  494.     bc.dstx = dst_x;
  495.     bc.dsty = dst_y;
  496.     bc.w    = w;
  497.     bc.h    = h;
  498.     bc.srcx = src_x;
  499.     bc.srcy = src_y;
  500.     bc.srcw = src_w;
  501.     bc.srch = src_h;
  502.     bc.stride = stride;
  503.     bc.bitmap = bitmap;
  504.  
  505.     __asm__ __volatile__(
  506.     "int $0x40"
  507.     ::"a"(73),"b"(0),"c"(&bc.dstx));
  508. };
  509.  
  510. int create_thread(int (*proc)(void *param), void *param, int stack_size);
  511.  
  512. void* load_library(const char *name);
  513.  
  514. void* get_proc_address(void *handle, const char *proc_name);
  515.  
  516. void enumerate_libraries(int (*callback)(void *handle, const char* name,
  517.                                          uint32_t base, uint32_t size, void *user_data),
  518.                          void *user_data);
  519.  
  520.  
  521. // May be next section need to be added in newlibc
  522.  
  523. enum KOLIBRI_GUI_EVENTS {
  524.     KOLIBRI_EVENT_NONE = 0,     /* Event queue is empty */
  525.     KOLIBRI_EVENT_REDRAW = 1,   /* Window and window elements should be redrawn */
  526.     KOLIBRI_EVENT_KEY = 2,      /* A key on the keyboard was pressed */
  527.     KOLIBRI_EVENT_BUTTON = 3,   /* A button was clicked with the mouse */
  528.     KOLIBRI_EVENT_DESKTOP = 5,  /* Desktop redraw finished */
  529.     KOLIBRI_EVENT_MOUSE = 6,    /* Mouse activity (movement, button press) was detected */
  530.     KOLIBRI_EVENT_IPC = 7,      /* Interprocess communication notify */
  531.     KOLIBRI_EVENT_NETWORK = 8,  /* Network event */
  532.     KOLIBRI_EVENT_DEBUG = 9,    /* Debug subsystem event */
  533.     KOLIBRI_EVENT_IRQBEGIN = 16 /* 16..31 IRQ0..IRQ15 interrupt =IRQBEGIN+IRQn */
  534. };
  535.  
  536.  
  537. // copied from /programs/system/shell/system/kolibri.c
  538. // fn's returned -1 as syserror, 1 as error, 0 as OK
  539. static inline
  540. int kol_clip_num()
  541. {
  542.     register uint32_t val;
  543.     asm volatile ("int $0x40":"=a"(val):"a"(54), "b"(0));
  544.     return val;
  545. }
  546.  
  547. static inline
  548. char* kol_clip_get(int n)
  549. // returned buffer must be freed by user_free()
  550. {
  551.     register char* val;
  552.     asm volatile ("int $0x40":"=a"(val):"a"(54), "b"(1), "c"(n));
  553.     return val;
  554. }
  555.  
  556. static inline
  557. int kol_clip_set(int n, char buffer[])
  558. {
  559.     register uint32_t val;
  560.     asm volatile ("int $0x40":"=a"(val):"a"(54), "b"(2), "c"(n), "d"(buffer));
  561.     return val;
  562. }
  563.  
  564. static inline
  565. int kol_clip_pop()
  566. {
  567.     register uint32_t val;
  568.     asm volatile ("int $0x40":"=a"(val):"a"(54), "b"(3));
  569.     return val;
  570. }
  571.  
  572. static inline
  573. int kol_clip_unlock()
  574. {
  575.     register uint32_t val;
  576.     asm volatile ("int $0x40":"=a"(val):"a"(54), "b"(4));
  577.     return val;
  578. }
  579.  
  580. struct kolibri_system_colors {
  581.   color_t frame_area;
  582.   color_t grab_bar;
  583.   color_t grab_bar_button;
  584.   color_t grab_button_text;
  585.   color_t grab_text;
  586.   color_t work_area;
  587.   color_t work_button;
  588.   color_t work_button_text;
  589.   color_t work_text;
  590.   color_t work_graph;
  591. };
  592.  
  593. static inline void get_system_colors(struct kolibri_system_colors *color_table)
  594. {
  595.   __asm__ volatile ("int $0x40"
  596.                     :
  597.                     :"a"(48),"b"(3),"c"(color_table),"d"(40)
  598.                     );
  599.  
  600.   /* color_table should point to the system color table */
  601. }
  602.  
  603. static inline void debug_board_write_byte(const char ch){
  604.     __asm__ __volatile__(
  605.     "int $0x40"
  606.     :
  607.     :"a"(63), "b"(1), "c"(ch));
  608. }
  609.  
  610.  
  611. static inline void draw_number_sys(int32_t number, int x, int y, int len, color_t color){
  612.     register uint32_t fmt;
  613.     fmt = len << 16 | 0x80000000; // no leading zeros + width
  614. //    fmt = len << 16 | 0x00000000; //  leading zeros + width
  615.     __asm__ __volatile__(
  616.     "int $0x40"
  617.     :
  618.     :"a"(47), "b"(fmt), "c"(number), "d"((x << 16) | y), "S"(color));
  619. }
  620.  
  621. static inline
  622. uint32_t get_mouse_eventstate(void)
  623. {
  624.     uint32_t val;
  625.  
  626.     __asm__ __volatile__(
  627.     "int $0x40"
  628.     :"=a"(val)
  629.     :"a"(37),"b"(3));
  630.     return val;
  631. };
  632.  
  633. static inline
  634. uint32_t set_event_mask(uint32_t mask)
  635. {
  636.     register uint32_t val;
  637.     asm volatile ("int $0x40":"=a"(val):"a"(40), "b"(mask));
  638.     return val;
  639. }
  640.  
  641. typedef void (*thread_proc)(void*);
  642.  
  643. static inline
  644. int start_thread(thread_proc proc, char* stack_top)
  645. {
  646.     register int val;
  647.     asm volatile ("int $0x40":"=a"(val):"a"(51), "b"(1), "c"(proc), "d"(stack_top));
  648.     return val;
  649. }
  650.  
  651. static inline
  652. void kos_exit()
  653. {
  654.     asm volatile ("int $0x40"::"a"(-1));
  655. }
  656.  
  657. static inline void focus_window(int slot){
  658.     asm volatile ("int $0x40"::"a"(18), "b"(3), "c"(slot));
  659. }
  660.  
  661. static inline int get_thread_slot(int tid){
  662.     register int val;
  663.     asm volatile ("int $0x40":"=a"(val):"a"(18), "b"(21), "c"(tid));
  664.     return val;
  665. }
  666.  
  667. static inline void set_current_folder(char* dir){
  668.     asm volatile ("int $0x40"::"a"(30), "b"(1), "c"(dir));
  669. }
  670.  
  671. static inline int get_current_folder(char* buf, int bufsize){
  672.     register int val;
  673.     asm volatile ("int $0x40":"=a"(val):"a"(30), "b"(2), "c"(buf), "d"(bufsize));
  674.     return val;
  675. }
  676.  
  677. /*
  678. static inline char *getcwd(char *buf, size_t size)
  679. {
  680.         int rc = get_current_folder(buf, size);
  681.         if (rc > size)
  682.         {
  683.                 errno = ERANGE;
  684.                 return 0;
  685.         }
  686.         else
  687.                 return buf;
  688. }
  689. */
  690. // end section
  691.  
  692.  
  693.  
  694. //added nonstatic inline because incomfortabre stepping in in debugger
  695. void __attribute__ ((noinline)) debug_board_write_str(const char* str);
  696. void __attribute__ ((noinline)) debug_board_printf(const char *format,...);
  697.  
  698. /* copy body to only one project file
  699. void __attribute__ ((noinline)) debug_board_write_str(const char* str){
  700.   while(*str)
  701.     debug_board_write_byte(*str++);
  702. }
  703.  
  704. void __attribute__ ((noinline)) debug_board_printf(const char *format,...)
  705. {
  706.         va_list ap;
  707.         char log_board[300];
  708.  
  709.         va_start (ap, format);
  710.         vsnprintf(log_board, sizeof log_board, format, ap);
  711.         va_end(ap);
  712.         debug_board_write_str(log_board);
  713. }
  714. */
  715.  
  716. #ifdef __cplusplus
  717. }
  718. #endif
  719.  
  720.  
  721. #endif
  722.  
  723.  
  724.  
  725.  
  726.  
  727.