Subversion Repositories Kolibri OS

Rev

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

  1. // Console.obj loading for kos32-gcc
  2. // Writed by rgimad and maxcodehack
  3. //
  4. // Usage:
  5. //       #include <sys/kos_LoadConsole.h>
  6. //       load_console();
  7. //
  8.  
  9.  
  10. #include <string.h>
  11. #include <stdlib.h>
  12.  
  13. #ifndef CONSOLE_OBJ_H
  14. #define CONSOLE_OBJ_H
  15.  
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19.  
  20. #ifndef NULL
  21. #define NULL 0
  22. #endif
  23.  
  24. #ifndef cdecl
  25. #define cdecl   __attribute__ ((cdecl))
  26. #endif
  27.  
  28. #ifndef stdcall
  29. #define stdcall __attribute__ ((stdcall))
  30. #endif
  31.  
  32. typedef unsigned int dword;
  33. typedef unsigned short word;
  34.  
  35. const char* imports[] = {
  36.         "START", "version", "con_init", "con_write_asciiz", "con_write_string",
  37.         "con_printf", "con_exit", "con_get_flags", "con_set_flags", "con_kbhit",
  38.         "con_getch", "con_getch2", "con_gets", "con_gets2", "con_get_font_height",
  39.         "con_get_cursor_height", "con_set_cursor_height",  "con_cls",
  40.         "con_get_cursor_pos", "con_set_cursor_pos", "con_set_title",
  41.         (char*)0
  42. };
  43.  
  44. dword *version;
  45.  
  46. typedef int (stdcall * con_gets2_callback)(int keycode, char** pstr, int* pn,
  47.         int* ppos);
  48.  
  49. void stdcall (*con_init)(dword wnd_width, dword wnd_height, dword scr_width, dword scr_height, const char* title) = 0;
  50. void stdcall (*con_exit)(int bCloseWindow) = 0;
  51. void stdcall (*con_set_title)(const char* title) = 0;
  52. void stdcall (*con_write_asciiz)(const char* str) = 0;
  53. void stdcall (*con_write_string)(const char* str, dword length) = 0;
  54. int cdecl (*con_printf)(const char* format, ...) = 0;
  55. dword stdcall (*con_get_flags)(void) = 0;
  56. dword stdcall (*con_set_flags)(dword new_flags) = 0;
  57. int stdcall (*con_get_font_height)(void) = 0;
  58. int stdcall (*con_get_cursor_height)(void) = 0;
  59. int stdcall (*con_set_cursor_height)(int new_height) = 0;
  60. int stdcall (*con_getch)(void) = 0;
  61. word stdcall (*con_getch2)(void) = 0;
  62. int stdcall (*con_kbhit)(void) = 0;
  63. char* stdcall (*con_gets)(char* str, int n) = 0;
  64. char* stdcall (*con_gets2)(con_gets2_callback callback, char* str, int n) = 0;
  65. void stdcall (*con_cls)() = 0;
  66. void stdcall (*con_get_cursor_pos)(int* px, int* py) = 0;
  67. void stdcall (*con_set_cursor_pos)(int x, int y) = 0;
  68.  
  69. const char lib_path[] = "/sys/lib/console.obj";
  70.  
  71. void* load_library(const char *name)
  72. {
  73.     void *table;
  74.     __asm__ __volatile__(
  75.     "int $0x40"
  76.     :"=a"(table)
  77.     :"a"(68), "b"(19), "c"(name));
  78.     return table;
  79. }
  80.  
  81. void *load_library_procedure(void *exports, const char *name)
  82. {
  83.         if (exports == NULL) { return 0; }
  84.         while (*(dword*)exports != 0)
  85.         {
  86.                 char *str1 = (char*)(*(dword*)exports);
  87.                 if (strcmp(str1, name) == 0)
  88.                 {
  89.             void *ptr = (void*)*(dword*)(exports + 4);
  90.                         return ptr;
  91.                 }
  92.                 exports += 8;
  93.         }
  94.         return 0;
  95. }
  96.  
  97. void output_debug_string(const char *s)
  98. {
  99.         unsigned int i = 0;
  100.         while(*(s + i))
  101.         {
  102.                 __asm__ __volatile__ ("int $0x40"::"a"(63), "b"(1), "c"(*(s + i)));
  103.                 i++;
  104.         }
  105. }
  106.  
  107. void load_console()
  108. {
  109.         void *lib = load_library(lib_path);
  110.  
  111.         if (!lib)
  112.         {
  113.                 output_debug_string("Console.obj loading error\r\n");
  114.                 exit(1);
  115.         }
  116.  
  117.     dword (*start_lib)(dword) = (dword(*)(dword))load_library_procedure(lib, imports[0]);
  118.  
  119.     version = (dword*)load_library_procedure(lib, imports[1]);
  120.  
  121.     con_init = (void stdcall(*)(dword,dword,dword,dword,const char*))load_library_procedure(lib, imports[2]);
  122.     con_write_asciiz = (void stdcall(*)(const char*))load_library_procedure(lib, imports[3]);
  123.     con_write_string = (void stdcall(*)(const char*,dword))load_library_procedure(lib, imports[4]);
  124.     con_printf = (int cdecl(*)(const char*,...))load_library_procedure(lib, imports[5]);
  125.     con_exit = (void stdcall(*)(int))load_library_procedure(lib, imports[6]);
  126.     con_get_flags = (dword stdcall(*)(void))load_library_procedure(lib, imports[7]);
  127.     con_set_flags = (dword stdcall(*)(dword))load_library_procedure(lib, imports[8]);
  128.     con_kbhit = (int stdcall(*)(void))load_library_procedure(lib, imports[9]);
  129.     con_getch = (int stdcall(*)(void))load_library_procedure(lib, imports[10]);
  130.     con_getch2 = (word stdcall(*)(void))load_library_procedure(lib, imports[11]);
  131.     con_gets = (char* stdcall(*)(char*,int))load_library_procedure(lib, imports[12]);
  132.     con_gets2 = (char* stdcall(*)(con_gets2_callback,char*,int))load_library_procedure(lib, imports[13]);
  133.     con_get_font_height = (int stdcall(*)(void))load_library_procedure(lib, imports[14]);
  134.     con_get_cursor_height = (int stdcall(*)(void))load_library_procedure(lib, imports[15]);
  135.     con_set_cursor_height = (int stdcall(*)(int))load_library_procedure(lib, imports[16]);
  136.     con_cls     = (void stdcall(*)(void))load_library_procedure(lib, imports[17]);
  137.     con_get_cursor_pos = (void stdcall(*)(int*,int*))load_library_procedure(lib, imports[18]);
  138.         con_set_cursor_pos = (void stdcall(*)(int,int))load_library_procedure(lib, imports[19]);
  139.         con_set_title = (void stdcall(*)(const char*))load_library_procedure(lib, imports[20]);
  140.  
  141. }
  142.  
  143. #ifdef __cplusplus
  144. }
  145. #endif
  146.  
  147. #endif
  148.