Subversion Repositories Kolibri OS

Rev

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