Subversion Repositories Kolibri OS

Rev

Rev 7873 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1.  
  2. char* con_caption = "Console app";
  3. //extern int __argc;
  4. //extern char** __argv;
  5. //extern char* __path;
  6. uint32_t   *con_dll_ver;
  7. int   __console_initdll_status = 0;
  8.  
  9. char* con_dllname = "/sys/lib/console.obj";
  10.  
  11. typedef int (stdcall * con_gets2_callback)(int keycode, char** pstr, int* pn, int* ppos);
  12.  
  13.  
  14. void stdcall (*con_init)(uint32_t wnd_width, uint32_t wnd_height, uint32_t scr_width, uint32_t scr_height, const char* title) = 0;
  15. void stdcall (*con_exit)(int bCloseWindow) = 0;
  16. void stdcall (*con_set_title)(const char* title) = 0;
  17. void stdcall (*con_write_asciiz)(const char* str) = 0;
  18. void stdcall (*con_write_string)(const char* str, uint32_t length) = 0;
  19. int cdecl (*con_printf)(const char* format, ...) = 0;
  20. uint32_t stdcall (*con_get_flags)(void) = 0;
  21. uint32_t stdcall (*con_set_flags)(uint32_t new_flags) = 0;
  22. int stdcall (*con_get_font_height)(void) = 0;
  23. int stdcall (*con_get_cursor_height)(void) = 0;
  24. int stdcall (*con_set_cursor_height)(int new_height) = 0;
  25. int stdcall (*con_getch)(void) = 0;
  26. uint16_t stdcall (*con_getch2)(void) = 0;
  27. int stdcall (*con_kbhit)(void) = 0;
  28. char* stdcall (*con_gets)(char* str, int n) = 0;
  29. char* stdcall (*con_gets2)(con_gets2_callback callback, char* str, int n) = 0;
  30. void stdcall (*con_cls)() = 0;
  31. void stdcall (*con_get_cursor_pos)(int* px, int* py) = 0;
  32. void stdcall (*con_set_cursor_pos)(int x, int y) = 0;
  33.  
  34.  
  35.  
  36. void *load_library(char *name) {
  37.         void *exports;
  38.     asm volatile ("int $0x40":"=a"(exports):"a"(68), "b"(19), "c"(name));
  39.     return exports;
  40. }
  41.  
  42. void *getprocaddress(void *exports, char *name)
  43. {
  44.         if (exports == NULL) { return 0; }
  45.         while (*(uint32_t*)exports != 0)
  46.         {
  47.                 char *str1 = (char*)(*(uint32_t*)exports);
  48.                 if (strcmp(str1, name) == 0)
  49.                 {
  50.             void *ptr = (void*)*(uint32_t*)(exports + 4);
  51.  
  52.             // important for debug
  53.             /*debug_board_write_string(name);
  54.             char otv[16];
  55.             itoa(ptr, otv);
  56.             debug_board_write_string(otv);
  57.             debug_board_write_byte('\n');*/
  58.  
  59.                         return ptr;
  60.                 }
  61.                 exports += 8;
  62.         }
  63.         return 0;
  64. }
  65.  
  66.  
  67. // don't change order in this! linked by index
  68. char* con_imports[] = {
  69.     "START", "version", "con_init", "con_write_asciiz", "con_write_string",
  70.     "con_printf", "con_exit", "con_get_flags", "con_set_flags", "con_kbhit",
  71.     "con_getch", "con_getch2", "con_gets", "con_gets2", "con_get_font_height",
  72.     "con_get_cursor_height", "con_set_cursor_height",  "con_cls",
  73.     "con_get_cursor_pos", "con_set_cursor_pos", "con_set_title",
  74.     (char*)0
  75. };
  76.  
  77. void con_lib_link(void *exp, char** imports)
  78. {
  79.     con_dll_ver             = getprocaddress(exp, imports[1]);
  80.     con_init                = getprocaddress(exp, imports[2]);
  81.     con_write_asciiz        = getprocaddress(exp, imports[3]);
  82.     con_write_string        = getprocaddress(exp, imports[4]);
  83.     con_printf              = getprocaddress(exp, imports[5]);
  84.     con_exit                = getprocaddress(exp, imports[6]);
  85.     con_get_flags           = getprocaddress(exp, imports[7]);
  86.     con_set_flags           = getprocaddress(exp, imports[8]);
  87.     con_kbhit               = getprocaddress(exp, imports[9]);
  88.     con_getch               = getprocaddress(exp, imports[10]);
  89.     con_getch2              = getprocaddress(exp, imports[11]);
  90.     con_gets                = getprocaddress(exp, imports[12]);
  91.     con_gets2               = getprocaddress(exp, imports[13]);
  92.     con_get_font_height     = getprocaddress(exp, imports[14]);
  93.     con_get_cursor_height   = getprocaddress(exp, imports[15]);
  94.     con_set_cursor_height   = getprocaddress(exp, imports[16]);
  95.     con_cls                 = getprocaddress(exp, imports[17]);
  96.     con_get_cursor_pos      = getprocaddress(exp, imports[18]);
  97.     con_set_cursor_pos      = getprocaddress(exp, imports[19]);
  98.     con_set_title           = getprocaddress(exp, imports[20]);
  99. }
  100.  
  101.  
  102. int con_init_console_dll_param(uint32_t wnd_width, uint32_t wnd_height, uint32_t scr_width, uint32_t scr_height, const char* title)
  103. /*works as con_init_console_dll, but call con_init with params*/
  104. {
  105.     void *hDll;
  106.     if (__console_initdll_status == 1) return 0;
  107.     if((hDll = load_library(con_dllname)) == 0)
  108.     {
  109.             debug_out_str("can't load lib\n");
  110.             return 1;
  111.     }
  112.     //debug_board_write_byte('I');
  113.     con_lib_link(hDll, con_imports);
  114.     /*if (con_dll_ver != (uint32_t*)0x00020008)
  115.     {
  116.         //debug_board_write_byte(48 + sizeof(KosExp));
  117.         //char otv[16];
  118.         //itoa(con_init, otv);
  119.         //debug_board_write_string(otv);
  120.  
  121.         debug_board_write_string("con_dll_ver=");
  122.         char otv[16];
  123.         itoa(con_dll_ver, otv);
  124.         debug_board_write_string(otv);
  125.         debug_board_write_byte('\n');
  126.  
  127.         debug_board_write_string("con_init=");
  128.         //char otv[16];
  129.         itoa(con_init, otv);
  130.         debug_board_write_string(otv);
  131.         debug_board_write_byte('\n');
  132.  
  133.         debug_board_write_string("(wtf)");
  134.     }*/
  135.     con_init(wnd_width, wnd_height, scr_width, scr_height, title);
  136.     __console_initdll_status = 1;
  137.     return 0;
  138. }
  139.  
  140.  
  141. int con_init_console_dll(void)
  142. {
  143.     return con_init_console_dll_param(-1, -1, -1, -1, con_caption);
  144. }
  145.  
  146.  
  147. // --------------------------------------------------------------------
  148.  
  149. int _getch()
  150. {
  151.         con_init_console_dll();
  152.         return con_getch();
  153. }
  154.  
  155. int _kbhit()
  156. {
  157.         con_init_console_dll();
  158.         return con_kbhit();
  159. }