Subversion Repositories Kolibri OS

Rev

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

  1. ////////////////////////////////////////////
  2. //           KolibriOS Syscalls           //
  3. //               sys/kos.h                //
  4. // Based on kos32sys and other wrappers   //
  5. //                                        //
  6. // Syscalls scheme: kos_XxxYyy            //
  7. //              (e.g. kos_DrawWindow)     //
  8. ////////////////////////////////////////////
  9.  
  10. // Some code was written by maxcodehack
  11.  
  12. #include <stdint.h>
  13. #include <string.h>
  14.  
  15. /*********************** Types *************************/
  16. typedef unsigned int color_t;
  17.  
  18. // Struct for App running
  19. #pragma pack(push,1)
  20. typedef struct
  21. {
  22. unsigned        p00;
  23. unsigned        p04;
  24. char            *p08;
  25. unsigned        p12;
  26. unsigned        p16;
  27. char            p20;
  28. char            *p21;
  29. } kos_Struct70;
  30. #pragma pack(pop)
  31.  
  32. // Blitter struct
  33. struct blit_call
  34. {
  35.         int dstx;
  36.         int dsty;
  37.         int w;
  38.         int h;
  39.  
  40.         int srcx;
  41.         int srcy;
  42.         int srcw;
  43.         int srch;
  44.  
  45.         void *bitmap;
  46.         int   stride;
  47. };
  48.  
  49. // Process info for sysfn 9
  50. #pragma pack(push, 1)
  51. struct proc_info
  52. {
  53.                 unsigned long cpu_usage;
  54.                 unsigned short pos_in_stack;
  55.                 unsigned short slot;
  56.                 unsigned short reserved;
  57.                 char name[12];
  58.                 unsigned long address;
  59.                 unsigned long memory_usage;
  60.                 unsigned long ID;
  61.                 unsigned long left,top;
  62.                 unsigned long width,height;
  63.                 unsigned short thread_state;
  64.                 unsigned short reserved2;
  65.                 unsigned long cleft, ctop, cwidth, cheight;
  66.                 unsigned char window_state;
  67.                 unsigned char reserved3[1024-71];
  68. };
  69. #pragma pack(pop)
  70.  
  71. // Color struct for sysfn 48
  72. struct kolibri_system_colors {
  73.   color_t frame_area;
  74.   color_t grab_bar;
  75.   color_t grab_bar_button;
  76.   color_t grab_button_text;
  77.   color_t grab_text;
  78.   color_t work_area;
  79.   color_t work_button;
  80.   color_t work_button_text;
  81.   color_t work_text;
  82.   color_t work_graph;
  83. };
  84.  
  85. typedef union __attribute__((packed))
  86. {
  87.         uint32_t val;
  88.         struct
  89.         {
  90.                 short  x;
  91.                 short  y;
  92.         };
  93. } pos_t;
  94.  
  95.  
  96. /*********************** Window Syscalls *************************/
  97. // Start drawing
  98. static inline void kos_BeginDraw(void)
  99. {
  100.         __asm__ __volatile__(
  101.         "int $0x40" ::"a"(12),"b"(1));
  102. };
  103.  
  104. // End drawing
  105. static inline void kos_EndDraw(void)
  106. {
  107.         __asm__ __volatile__(
  108.         "int $0x40" ::"a"(12),"b"(2));
  109. };
  110.  
  111. // Draw window
  112. static inline void kos_DrawWindow(int x, int y, int w, int h, const char *title,
  113.                                            color_t bgcolor, uint32_t style)
  114. {
  115.         __asm__ __volatile__(
  116.         "int $0x40"
  117.         ::"a"(0),
  118.          "b"((x << 16) | ((w-1) & 0xFFFF)),
  119.          "c"((y << 16) | ((h-1) & 0xFFFF)),
  120.          "d"((style << 24) | (bgcolor & 0xFFFFFF)),
  121.          "D"(title),
  122.          "S"(0) : "memory");
  123. };
  124.  
  125. // Set window layer behaviour
  126. #define ZPOS_DESKTOP     -2
  127. #define ZPOS_ALWAYS_BACK -1
  128. #define ZPOS_NORMAL      0
  129. #define ZPOS_ALWAYS_TOP  1
  130.  
  131. static inline void kos_SetWindowLayerBehaviour(int zpos)
  132. {
  133.         __asm__ __volatile__(
  134.         "int $0x40"
  135.         ::"a"(18),
  136.          "b"(25),
  137.          "c"(2),
  138.          "d"(-1),
  139.          "S"(zpos) : "memory");
  140. };
  141.  
  142. // Change window size
  143. #define OLD -1
  144. static inline void kos_ChangeWindow(int new_x, int new_y, int new_w, int new_h)
  145. {
  146.         __asm__ __volatile__(
  147.                 "int $0x40"
  148.                 ::"a"(67), "b"(new_x), "c"(new_y), "d"(new_w),"S"(new_h)
  149.         );
  150. }
  151.  
  152. /*********************** Other GUI functions *************************/
  153. // Draw text
  154. static inline void kos_DrawText(int x, int y, const char *text, color_t color)
  155. {
  156.         __asm__ __volatile__(
  157.         "int $0x40"
  158.         ::"a"(4),"d"(text),
  159.           "b"((x << 16) | y),
  160.           "S"(strlen(text)),"c"(color)
  161.          :"memory");
  162. }
  163.  
  164. // Draw button
  165. static inline void kos_DrawButton(int x, int y, int w, int h, int id, color_t color)
  166. {
  167.         __asm__ __volatile__(
  168.         "int $0x40"
  169.         ::"a"(8),
  170.           "b"(x * 65536 + w),
  171.           "c"(y * 65536 + h),
  172.           "d"(id),
  173.           "S"(color));
  174. };
  175.  
  176. // Draw button with text
  177. void kos_DrawButtonWithText(int x, int y, int w, int h, int id, color_t color, const char* text)
  178. {
  179.         kos_DrawButton(x, y, w, h, id, color);
  180.  
  181.         int tx = ((((-strlen(text))*8)+w)/2)+x;
  182.         int ty = h/2-7+y;
  183.  
  184.         kos_DrawText(tx, ty, text, 0x90000000);
  185. };
  186.  
  187. // Draw line
  188. static inline void kos_DrawLine(int x_start, int y_start, int x_end, int y_end, color_t color)
  189. {
  190.         __asm__ __volatile__(
  191.         "int $0x40"
  192.         ::"a"(38), "d"(color),
  193.           "b"((x_start << 16) | x_end),
  194.           "c"((y_start << 16) | y_end));
  195. }
  196.  
  197. // Draw bar
  198. static inline void kos_DrawBar(int x, int y, int w, int h, color_t color)
  199. {
  200.         __asm__ __volatile__(
  201.         "int $0x40"
  202.         ::"a"(13), "d"(color),
  203.           "b"((x << 16) | w),
  204.           "c"((y << 16) | h));
  205. }
  206.  
  207. // Put one pixel
  208. void kos_PutPixel(int x, int y, color_t color) {
  209.         __asm__ __volatile__("int $0x40"
  210.                          ::"a"(1),
  211.                           "b"(x),
  212.                           "c"(y),
  213.                           "d"(color));
  214. }
  215.  
  216. // Draw bitmap image
  217. static inline void kos_DrawBitmap(void *bitmap, int x, int y, int w, int h)
  218. {
  219.         __asm__ __volatile__(
  220.         "int $0x40"
  221.         ::"a"(7), "b"(bitmap),
  222.           "c"((w << 16) | h),
  223.           "d"((x << 16) | y));
  224. }
  225.  
  226. // Blitter
  227. static inline void Blit(void *bitmap, int dst_x, int dst_y,
  228.                                                 int src_x, int src_y, int w, int h,
  229.                                                 int src_w, int src_h, int stride)
  230. {
  231.         volatile struct blit_call bc;
  232.  
  233.         bc.dstx = dst_x;
  234.         bc.dsty = dst_y;
  235.         bc.w    = w;
  236.         bc.h    = h;
  237.         bc.srcx = src_x;
  238.         bc.srcy = src_y;
  239.         bc.srcw = src_w;
  240.         bc.srch = src_h;
  241.         bc.stride = stride;
  242.         bc.bitmap = bitmap;
  243.  
  244.         __asm__ __volatile__(
  245.         "int $0x40"
  246.         ::"a"(73),"b"(0),"c"(&bc.dstx));
  247. };
  248.  
  249. // Get screen part as image
  250. static inline void kos_ScreenShot(char* image, int x, int y, int w, int h)
  251. {
  252.         __asm__ __volatile__(
  253.         "int $0x40"
  254.         ::"a"(36),
  255.          "b"(image),
  256.          "c"(w*65536+h),
  257.          "d"(x*65536+y) : "memory");
  258. };
  259.  
  260. /*********************** Skin *************************/
  261. // Return skin height
  262. static inline uint32_t kos_SkinHeight(void)
  263. {
  264.         uint32_t height;
  265.  
  266.         __asm__ __volatile__(
  267.         "int $0x40 \n\t"
  268.         :"=a"(height)
  269.         :"a"(48),"b"(4));
  270.         return height;
  271. };
  272.  
  273. /*********************** Mouse *************************/
  274. // Get mouse position
  275. #define POS_SCREEN 0
  276. #define POS_WINDOW 1
  277.  
  278. static inline
  279. pos_t kos_GetMousePos(int origin)
  280. {
  281.         pos_t val;
  282.  
  283.         __asm__ __volatile__(
  284.         "int $0x40 \n\t"
  285.         "rol $16, %%eax"
  286.         :"=a"(val)
  287.         :"a"(37),"b"(origin));
  288.         return val;
  289. }
  290.  
  291. // Get mouse buttons
  292. static inline
  293. uint32_t kos_GetMouseButtons(void)
  294. {
  295.         uint32_t val;
  296.  
  297.         __asm__ __volatile__(
  298.         "int $0x40"
  299.         :"=a"(val)
  300.         :"a"(37),"b"(2));
  301.         return val;
  302. };
  303.  
  304. // Get mouse wheels
  305. static inline
  306. uint32_t kos_GetMouseWheels(void)
  307. {
  308.         uint32_t val;
  309.  
  310.         __asm__ __volatile__(
  311.         "int $0x40 \n\t"
  312.         :"=a"(val)
  313.         :"a"(37),"b"(7));
  314.         return val;
  315. };
  316.  
  317. // Load cursor
  318. static inline uint32_t kos_LoadCursor(void *path, uint32_t flags)
  319. {
  320.         uint32_t  val;
  321.         __asm__ __volatile__(
  322.         "int $0x40"
  323.         :"=a"(val)
  324.         :"a"(37), "b"(4), "c"(path), "d"(flags));
  325.         return val;
  326. }
  327.  
  328. // Set cursor
  329. static inline uint32_t kos_SetCursor(uint32_t cursor)
  330. {
  331.         uint32_t  old;
  332.         __asm__ __volatile__(
  333.         "int $0x40"
  334.         :"=a"(old)
  335.         :"a"(37), "b"(5), "c"(cursor));
  336.         return old;
  337. };
  338.  
  339. // Destroy cursor
  340. static inline int kos_DestroyCursor(uint32_t cursor)
  341. {
  342.         int ret;
  343.         __asm__ __volatile__(
  344.         "int $0x40"
  345.         :"=a"(ret)
  346.         :"a"(37), "b"(6), "c"(cursor)
  347.         :"memory");
  348.         return ret;
  349. };
  350.  
  351. /*********************** OS Events *************************/
  352. #define evReDraw  1
  353. #define evKey     2
  354. #define evButton  3
  355. #define evExit    4
  356. #define evDesktop 5
  357. #define evMouse   6
  358. #define evIPC     7
  359. #define evNetwork 8
  360. #define evDebug   9
  361.  
  362. static inline
  363. uint32_t kos_WaitForEventTimeout(uint32_t time)
  364. {
  365.         uint32_t val;
  366.         __asm__ __volatile__(
  367.         "int $0x40"
  368.         :"=a"(val)
  369.         :"a"(23), "b"(time));
  370.         return val;
  371. };
  372.  
  373. static inline uint32_t kos_CheckForEvent(void)
  374. {
  375.         uint32_t val;
  376.         __asm__ __volatile__(
  377.         "int $0x40"
  378.         :"=a"(val)
  379.         :"a"(11));
  380.         return val;
  381. };
  382.  
  383. static inline uint32_t kos_WaitForEvent(void)
  384. {
  385.         uint32_t val;
  386.         __asm__ __volatile__(
  387.         "int $0x40"
  388.         :"=a"(val)
  389.         :"a"(10));
  390.         return val;
  391. };
  392.  
  393. /*********************** Eventmask *************************/
  394. #define EVM_REDRAW        1
  395. #define EVM_KEY           2
  396. #define EVM_BUTTON        4
  397. #define EVM_EXIT          8
  398. #define EVM_BACKGROUND    16
  399. #define EVM_MOUSE         32
  400. #define EVM_IPC           64
  401. #define EVM_STACK         128
  402. #define EVM_DEBUG         256
  403. #define EVM_STACK2        512
  404. #define EVM_MOUSE_FILTER  0x80000000
  405. #define EVM_CURSOR_FILTER 0x40000000
  406.  
  407. static inline uint32_t kos_SetMaskForEvents(uint32_t event_mask)
  408. {
  409.         uint32_t  old_event_mask;
  410.         __asm__ __volatile__(
  411.         "int $0x40"
  412.         :"=a"(old_event_mask)
  413.         :"a"(40),"b"(event_mask));
  414.  
  415.         return old_event_mask;
  416. };
  417.  
  418. /*********************** Other *************************/
  419. // Get key
  420. int kos_GetKey()
  421. {
  422.         unsigned short key;
  423.         __asm__ __volatile__("int $0x40":"=a"(key):"0"(2));
  424.         if(!(key & 0xFF)) return (key>>8)&0xFF; else return 0;
  425. }
  426.  
  427. // Get pressed button ID
  428. static inline
  429. uint32_t kos_GetButtonID(void)
  430. {
  431.         uint32_t val;
  432.         __asm__ __volatile__(
  433.         "int $0x40"
  434.         :"=a"(val)
  435.         :"a"(17));
  436.         return val>>8;
  437. };
  438.  
  439. // Sleep..
  440. static inline void kos_Delay(uint32_t time)
  441. {
  442.         __asm__ __volatile__(
  443.         "int $0x40"
  444.         ::"a"(5), "b"(time)
  445.         :"memory");
  446. };
  447.  
  448. // Get screen size
  449. static inline
  450. pos_t kos_ScreenSize()
  451. {
  452.         pos_t size;
  453.         __asm__ __volatile__(
  454.         "int $0x40"
  455.         :"=a"(size)
  456.         :"a"(14));
  457.  
  458.         return size;
  459. };
  460.  
  461. // Get system color table
  462. static inline void kos_GetSystemColors(struct kolibri_system_colors *color_table)
  463. {
  464.         __asm__ __volatile__ ("int $0x40"
  465.         :
  466.         :"a"(48),"b"(3),"c"(color_table),"d"(40)
  467.         );
  468. }
  469.  
  470. // SysFn 9
  471. static inline void kos_ProcessInfo(char *info)
  472. {
  473.         __asm__ __volatile__(
  474.         "int $0x40"
  475.         :
  476.         :"a"(9), "b"(info), "c"(-1)
  477.         :"memory");
  478. };
  479.  
  480. void kos_RunApp(char* app, char* param)
  481. {
  482.         kos_Struct70 r;
  483.         r.p00 = 7;
  484.         r.p04 = 0;
  485.         r.p08 = param;
  486.         r.p12 = 0;
  487.         r.p16 = 0;
  488.         r.p20 = 0;
  489.         r.p21 = app;
  490.         __asm__ __volatile__ ("int $0x40"::"a"(70), "b"(&r));
  491. }
  492.