Subversion Repositories Kolibri OS

Rev

Rev 8283 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #ifndef __KOLIBRI_H_INCLUDED_
  2. #define __KOLIBRI_H_INCLUDED_
  3.  
  4. #include "kos_lib.h"
  5.  
  6. // Kolibri interface.
  7.  
  8. namespace Kolibri   // All kolibri functions, types and data are nested in the (Kolibri) namespace.
  9. {
  10.         const char *DebugPrefix = "User program: ";
  11.         char CommandLine[4096];
  12.  
  13.         struct TWindowData   // Data for drawing a window.
  14.         {
  15.                 unsigned short WindowType, HeaderType;
  16.                 unsigned long WindowColor, HeaderColor, BorderColor, TitleColor;
  17.                 const char *Title;
  18.         };
  19.  
  20.         struct TStartData   // This structure is used only for KolibriOnStart function.
  21.         {
  22.                 unsigned short Left, Width, Top, Height; // Initial window rectangle.
  23.                 TWindowData WinData;
  24.         };
  25.  
  26.         typedef void **TThreadData;   // Thread data are the fast identifier of thread, contains user dword in
  27.                         //_ the zero element and stack beginning (or zero if it is unknown) in the first element.
  28.                         //_ The stack will be deleted from dynamic memory at the finish of the thread if stack beginning is not zero.
  29.  
  30.         struct TMutex;   // Simple mutex can be locked only once at a time.
  31. #define KOLIBRI_MUTEX_INIT {}   // Simple mutex initializer, cat be redefined in a realization of the library
  32.  
  33.         struct TRecMutex;   // Recursive mutex can be locked many times by a single thread at a time.
  34. #define KOLIBRI_REC_MUTEX_INIT {}   // Recursive mutex initializer, cat be redefined in a realization of the library
  35.  
  36.         // Some functions have two forms: the fast form with (thread_data) parameter and the form without it.
  37.         // Note: pass only thread data of current thread as (thread_data) parameter to these functions.
  38.  
  39.         void Main();   // Main function is called at program startup.
  40.         void DrawButton(long id, long color, long x, long y, long c_x, long c_y);   // Draw Standard button
  41.         void* ThreadMain(void *user = 0, void *stack_begin = 0);
  42.                         // Called at thread startup, (user) is placed in thread data as a user dword,
  43.                         //_ (stack_begin) is placed in thread data as a stack beginning.
  44.                         //_ Return new value of stack beginning that can be changed in the thread data.
  45.         void GetWindowData(TWindowData &win_data);   // Write current window data to (win_data).
  46.         void GetWindowData(TWindowData &win_data, TThreadData thread_data);
  47.         void SetWindowData(const TWindowData &win_data);   // Replace current window data by (win_data).
  48.         void SetWindowData(const TWindowData &win_data, TThreadData thread_data);
  49.         void CloseWindow();   // Close current window when returning to message loop.
  50.         void CloseWindow(TThreadData thread_data);
  51.         void Redraw(int frame = 0);   // Redraw current window immediately, if (frame) is positive redraw the frame too,
  52.         void Redraw(int frame, TThreadData thread_data);   //_ if (frame) is negative redraw only invalideted window.
  53.         void Invalidate(int frame = 0);   // Redraw current window when no message will be is the queue,
  54.         void Invalidate(int frame, TThreadData thread_data);   //_ if (frame) is positive redraw the frame too,
  55.                                                                                                                    //_ if (frame) is negative do nothing.
  56.         void MoveWindow(const int window_rect[/* 4 */]);   // Move and resize current window.
  57.  
  58.         void ExitDebug();    // Abnormally terminate a program.
  59.         void ExitProcess();  // Exit from the process, don't call any destructors of global varyables
  60.         void ExitThread();   // Exit from the current thread
  61.         void ExitThread(TThreadData thread_data);
  62.         void ReturnMessageLoop();   // Return to the message loop of the thread. Exit from the thread
  63.         void ReturnMessageLoop(TThreadData thread_data);   //_ if it is called from (KolibriOnStart).
  64.  
  65.         void Delay(unsigned int time);   // Delay the execution of the program during (time) hundredth seconds.
  66.         unsigned int Clock();   // Return the time from starting of the system to this moment in hundredth of seconds.
  67.         int GetPackedTime();   // Return the current time of day in binary-decimal format 0x00SSMMHH.
  68.         void GetTime(int t[/* 3 */]);   // Write the current time to (t): t[0] = second, t[1] = minute, t[2] = hour.
  69.         int GetPackedDate();   // Return the current date in binary-decimal format 0x00YYDDMM.
  70.         void GetDate(int d[/* 3 */]);   // Write the current date to (d): d[0] = day, d[1] = month, d[2] = year.
  71.         void GetTimeDate(int t[/* 6 */]);   // Write the current time and date to (t): t[0] = second,
  72.                                                                                 //_ t[1] = minute, t[2] = hour, t[3] = day, t[4] = month, t[5] = year.
  73.         void ReadCommonColors(unsigned int colors[/* 10 */]);   // Writes standart window colors to (colors).
  74.         unsigned int GetProcessInfo(unsigned int *use_cpu, char process_name[/* 13 */], unsigned int *use_memory,
  75.                                                                 unsigned int *pid, int window_rect[/* 4 */], unsigned int pid_for = -1);
  76.                                                                 // Write (pid_for) process information to variables parameters points, return
  77.                                                                 //_ the number of processes. (pid_for) equal to (-1) means current process.
  78.         unsigned int GetPid();   // Return the current thread identifier (pid).
  79.         unsigned int GetPid(TThreadData thread_data);
  80.         TThreadData GetThreadData();   // Return the thread data of the current thread.
  81.         TThreadData GetThreadData(unsigned int pid);   // Return the thread data of the thread with the given pid.
  82.  
  83.         void* GetPicture(unsigned short &width, unsigned short &height);
  84.         void* GetPicture(unsigned short &width, unsigned short &height, TThreadData thread_data);
  85.                         // Return the picture on the window and write its width and height to (width) and (height).
  86.         void SetPicture(void *picture, unsigned short width, unsigned short height);
  87.         void SetPicture(void *picture, unsigned short width, unsigned short height, TThreadData thread_data);
  88.                         // Replace the picture on the window by the given picture with the given width and height.
  89.         void GetBorderHeader(unsigned short &border_size, unsigned short &header_size);
  90.         void GetBorderHeader(unsigned short &border_size, unsigned short &header_size, TThreadData thread_data);
  91.                         // Write the border thickness to (border_size) and header height to (header_size).
  92.         void GetClientSize(unsigned short &width, unsigned short &height);
  93.         void GetClientSize(unsigned short &width, unsigned short &height, TThreadData thread_data);
  94.                         // Write the client area width and height to (width) and (height) parameters.
  95.         void GetClientSize(unsigned short &width, unsigned short &height, int win_width, int win_height);
  96.         void GetClientSize(unsigned short &width, unsigned short &height, int win_width, int win_height, TThreadData thread_data);
  97.                         // Write the client area size of window with the width (win_width)
  98.                         //_ and height (win_height) to (width) and (height) parameters.
  99.         void GetScreenSize(unsigned short &width, unsigned short &height);
  100.                         // Write the screen width and height to (width) and (height) parameters.
  101.  
  102.         void InitMutex(TMutex *mutex);   // Initialize the simple mutex.
  103.         void InitRecMutex(TRecMutex *mutex);   // Initialize the recursive mutex.
  104.         bool TryLock(TMutex *mutex);   // Try to lock the mutex without waitting, return true if lock.
  105.         bool TryLock(TRecMutex *mutex);
  106.         bool TryLock(TRecMutex *mutex, TThreadData thread_data);
  107.         bool TryLock(TRecMutex *mutex, unsigned int pid);
  108.         void Lock(TMutex *mutex);   // Lock mutex and wait for it if this necessary.
  109.         void Lock(TRecMutex *mutex);
  110.         void Lock(TRecMutex *mutex, TThreadData thread_data);
  111.         void Lock(TRecMutex *mutex, unsigned int pid);
  112.         bool LockTime(TMutex *mutex, int time);
  113.         bool LockTime(TRecMutex *mutex, int time);   // Lock mutex and wait for it during (time) hundredth seconds.
  114.         bool LockTime(TRecMutex *mutex, int time, TThreadData thread_data);
  115.         bool LockTime(TRecMutex *mutex, int time, unsigned int pid);
  116.         void UnLock(TMutex *mutex);   // Unlock mutex
  117.         void UnLock(TRecMutex *mutex);
  118.         void UnLock(TRecMutex *mutex, TThreadData thread_data);
  119.         void UnLock(TRecMutex *mutex, unsigned int pid);
  120.  
  121.         void DebugPutChar(char c);   // Put the character to the debug board.
  122.         void DebugPutString(const char *s);   // Put the string to the debug board.
  123.         int GetKey();   // Return key pressed by user or -1 if no key was pressed.
  124.         void GetMousePosition(short &x, short &y, bool absolute = false);
  125.                         // Write mouse position to (x) and (y): absolute if (absolute) is true and relative the window otherwise.
  126.         void GetMousePosPicture(short &x, short &y);
  127.         int GetMouseButton();   // Return buttons pressed: bit 0 - left button, bit 1 - right button...
  128.         void GetMouseScrollData(short &x, short &y);
  129.  
  130.         int GetThreadNumber();   // Return the number of threads currently executing.
  131.         bool WasThreadCreated();   // Return true if there was created at least one thread except the main thred.
  132.         unsigned int CreateThread(void *user = 0, unsigned int stack_size = 0, void *stack_end = 0);
  133.                         // Create a new thread with the user dword (user) and stack pointer (stack_end).
  134.                         //_ If (stack_end) is zero, create stack in dynamic memory of size (stack_size) or
  135.                         //_ the same size as the main thread if (stack_size) less that 4096. Set the beginning
  136.                         //_ of the stack if (stack_end) is zero or (stack_size) is not zero, in this case stack
  137.                         //_ will be deleted automaticaly from dynamic memory at the finish of the thread.
  138.         void DrawText(short x, short y, int color, const char* string);
  139.         void DrawRect(long l, long t, long w, long h, long color);
  140.         void PutImage(unsigned char* img_data, long img_l, long img_t, long img_w, long img_h);
  141.         void SetWindowCaption(const char* caption);
  142. }
  143.  
  144. // Function, defined outside.
  145.  
  146. bool KolibriOnStart(Kolibri::TStartData &me_start, Kolibri::TThreadData thread_data);
  147.                         // Window will be created iff return value is true.
  148. bool KolibriOnClose(Kolibri::TThreadData thread_data);     // Window will be closed iff return value is true.
  149. int KolibriOnIdle(Kolibri::TThreadData thread_data);       // Return the time to wait next message.
  150. void KolibriOnSize(int window_rect[/* 4 */], Kolibri::TThreadData thread_data);  // When the window is resized.
  151. void KolibriOnKeyPress(Kolibri::TThreadData thread_data);  // When user press a key.
  152. void KolibriOnMouse(Kolibri::TThreadData thread_data);     // When user move a mouse.
  153. void KolibriOnButton(long id, Kolibri::TThreadData th);
  154.  
  155. #ifdef __KOLIBRI__
  156.  
  157. namespace Kolibri
  158. {
  159. // Structures.
  160.  
  161.         struct TMutex   // Simple mutex can be locked only once at a time.
  162.         {
  163.                 unsigned int mut;
  164.         };
  165. #undef  KOLIBRI_MUTEX_INIT
  166. #define KOLIBRI_MUTEX_INIT {0x40}   // Simple mutex initializer, cat be redefined in a realization of the library
  167.  
  168.         struct TRecMutex   // Recursive mutex can be locked many times by a single thread at a time.
  169.         {
  170.                 unsigned int mut, pid;
  171.         };
  172. #undef  KOLIBRI_REC_MUTEX_INIT
  173. #define KOLIBRI_REC_MUTEX_INIT {0x20,-1}   // Recursive mutex initializer, cat be redefined in a realization of the library
  174.  
  175. // Global variables.
  176.  
  177.         volatile TThreadData _ThreadTable[256];
  178.         volatile unsigned int _ThreadScanCount[2] = {0, 0};
  179.         volatile int _ThreadNumber = 1;
  180.         volatile int _ExitProcessNow = 0;
  181.         TMutex _ThreadMutex = KOLIBRI_MUTEX_INIT;
  182.         unsigned int _ThreadSavedBegProc[4];
  183.  
  184. // Inline functions.
  185.  
  186.         inline void GetWindowData(TWindowData &win_data) {GetWindowData(win_data, GetThreadData());}
  187.  
  188.         inline void SetWindowData(const TWindowData &win_data) {SetWindowData(win_data, GetThreadData());}
  189.  
  190.         inline void CloseWindow() {CloseWindow(GetThreadData());}
  191.  
  192.         inline void Redraw(int frame) {Redraw(frame, GetThreadData());}
  193.  
  194.         inline void Invalidate(int frame) {Invalidate(frame, GetThreadData());}
  195.  
  196.         inline void* GetPicture(unsigned short &width, unsigned short &height)
  197.         {
  198.                 return GetPicture(width, height, GetThreadData());
  199.         }
  200.  
  201.         inline void SetPicture(void *picture, unsigned short width, unsigned short height)
  202.         {
  203.                 SetPicture(picture, width, height, GetThreadData());
  204.         }
  205.  
  206.         inline void GetBorderHeader(unsigned short &border_size, unsigned short &header_size)
  207.         {
  208.                 GetBorderHeader(border_size, header_size, GetThreadData());
  209.         }
  210.  
  211.         inline void GetClientSize(unsigned short &width, unsigned short &height)
  212.         {
  213.                 unsigned int pid;
  214.                 int rect[4];
  215.                 GetProcessInfo(0, 0, 0, &pid, rect);
  216.                 GetClientSize(width, height, rect[2], rect[3], GetThreadData(pid));
  217.         }
  218.  
  219.         inline void GetClientSize(unsigned short &width, unsigned short &height, TThreadData thread_data)
  220.         {
  221.                 int rect[4];
  222.                 GetProcessInfo(0, 0, 0, 0, rect);
  223.                 GetClientSize(width, height, rect[2], rect[3], thread_data);
  224.         }
  225.  
  226.         inline void GetClientSize(unsigned short &width, unsigned short &height, int win_width, int win_height)
  227.         {
  228.                 GetClientSize(width, height, win_width, win_height, GetThreadData());
  229.         }
  230.  
  231.         inline void GetTimeDate(int t[/* 6 */]) {GetTime(t); GetDate(t + 3);}
  232.  
  233.         inline void InitMutex(TMutex *mutex) {mutex->mut = 0;}
  234.  
  235.         inline void InitRecMutex(TRecMutex *mutex) {mutex->mut = 0; mutex->pid = -1;}
  236.  
  237.         inline bool TryLock(TRecMutex *mutex) {return TryLock(mutex, GetPid());}
  238.  
  239.         inline bool TryLock(TRecMutex *mutex, TThreadData thread_data) {return TryLock(mutex, GetPid(thread_data));}
  240.  
  241.         inline void Lock(TRecMutex *mutex) {Lock(mutex, GetPid());}
  242.  
  243.         inline void Lock(TRecMutex *mutex, TThreadData thread_data) {Lock(mutex, GetPid(thread_data));}
  244.  
  245.         inline bool LockTime(TRecMutex *mutex, int time) {return LockTime(mutex, time, GetPid());}
  246.  
  247.         inline bool LockTime(TRecMutex *mutex, int time, TThreadData thread_data)
  248.                                 {return LockTime(mutex, time, GetPid(thread_data));}
  249.  
  250.         inline void UnLock(TRecMutex *mutex) {UnLock(mutex, GetPid());}
  251.  
  252.         inline void UnLock(TRecMutex *mutex, TThreadData thread_data) {UnLock(mutex, GetPid(thread_data));}
  253.  
  254.         inline int GetThreadNumber() {return _ThreadNumber;}
  255.  
  256. // Constants from fasm.
  257.  
  258. #include "kos_func.inc"
  259.  
  260. // Functions.
  261.  
  262.         unsigned char _HashByte(unsigned int value);
  263.         unsigned short _HashWord(unsigned int value);
  264.         unsigned int _HashDword(unsigned int value);
  265.  
  266.         void _GetStartData(TStartData &start_data, TThreadData thread_data)
  267.         {
  268.                 start_data.Left = (unsigned short)((unsigned long)thread_data[KOLIBRI_THREAD_DATA_X] >> 16);
  269.                 start_data.Width = (unsigned short)((unsigned long)thread_data[KOLIBRI_THREAD_DATA_X]);
  270.                 start_data.Top = (unsigned short)((unsigned long)thread_data[KOLIBRI_THREAD_DATA_Y] >> 16);
  271.                 start_data.Height = (unsigned short)((unsigned long)thread_data[KOLIBRI_THREAD_DATA_Y]);
  272.                 GetWindowData(start_data.WinData, thread_data);
  273.         }
  274.  
  275.         void _SetStartData(const TStartData &start_data, TThreadData thread_data)
  276.         {
  277.                 (unsigned long&)thread_data[KOLIBRI_THREAD_DATA_X] =
  278.                                         ((unsigned int)start_data.Left << 16) | start_data.Width;
  279.                 (unsigned long&)thread_data[KOLIBRI_THREAD_DATA_Y] =
  280.                                         ((unsigned int)start_data.Top << 16) | start_data.Height;
  281.                 SetWindowData(start_data.WinData, thread_data);
  282.         }
  283.  
  284.         void _ApplyCommonColors(TWindowData &win_data)
  285.         {
  286.                 unsigned int colors[10];
  287.                 ReadCommonColors(colors);
  288.                 win_data.WindowColor = colors[5];
  289.                 win_data.HeaderColor = colors[1];
  290.                 win_data.BorderColor = colors[0];
  291.                 win_data.TitleColor = colors[4];
  292.         }
  293.  
  294.         void _SetValueFunctionPriority(void *beg, int n)
  295.         {
  296.                 int k, i;
  297.                 unsigned char num[256];
  298.                 for (i = 0; i < 256; i++) num[i] = 0;
  299.                 for (k = 0; k < n; k++)
  300.                 {
  301.                         i = ((unsigned char*)beg + 6*k)[1];
  302.                         ((unsigned char*)beg + 6*k)[0] = num[i];
  303.                         if (num[i] != 255) num[i]++;
  304.                 }
  305.         }
  306.  
  307.         void _CallFunctionPriority(void *beg, void *end, bool reverse = false)
  308.         {
  309.                 struct _Local
  310.                 {
  311.                         static int cmp(void *beg, int i, int j)
  312.                         {
  313.                                 unsigned char *x = (unsigned char*)beg + 6*i;
  314.                                 unsigned char *y = (unsigned char*)beg + 6*j;
  315.                                 if (*(unsigned short*)x < *(unsigned short*)y) return -1;
  316.                                 if (*(unsigned short*)x > *(unsigned short*)y) return 1;
  317.                                 return 0;
  318.                         }
  319.  
  320.                         static void swap(void *beg, int i, int j)
  321.                         {
  322.                                 unsigned char *x = (unsigned char*)beg + 6*i;
  323.                                 unsigned char *y = (unsigned char*)beg + 6*j;
  324.                                 short s;
  325.                                 int t;
  326.                                 s = *(short*)x; *(short*)x = *(short*)y; *(short*)y = s;
  327.                                 x += 2; y += 2;
  328.                                 t = *(int*)x; *(int*)x = *(int*)y; *(int*)y = t;
  329.                         }
  330.  
  331.                         static void call(void *beg, int i)
  332.                         {
  333.                                 unsigned char *x = (unsigned char*)beg + 6*i;
  334.                                 (*(void(**)())(x+2))();
  335.                         }
  336.                 };
  337.  
  338.                 if (!beg || !end || end <= beg) return;
  339.                 int i, j, k, m, n;
  340.                 n = ((unsigned char*)end - (unsigned char*)beg) / 6;
  341.                 if (n <= 0) return;
  342.                 _SetValueFunctionPriority(beg, n);
  343.                 m = n; k = n;
  344.                 while (m > 1)
  345.                 {
  346.                         if (k > 0) k--;
  347.                         else _Local::swap(beg, 0, --m);
  348.                         j = k;
  349.                         for (;;)
  350.                         {
  351.                                  i = j;
  352.                                  if (2*i + 1 >= m) break;
  353.                                  if (_Local::cmp(beg, 2*i + 1, j) > 0) j = 2*i + 1;
  354.                                  if (2*i + 2 < m && _Local::cmp(beg, 2*i + 2, j) > 0) j = 2*i + 2;
  355.                                  if (i == j) break;
  356.                                  _Local::swap(beg, i, j);
  357.                         }
  358.                 }
  359.                 if (!reverse)
  360.                 {
  361.                         for (k = 0; k < n; k++) _Local::call(beg, k);
  362.                 }
  363.                 else
  364.                 {
  365.                         for (k = n-1; k >= 0; k--) _Local::call(beg, k);
  366.                 }
  367.         }
  368.  
  369.         bool _CallStart(TThreadData thread_data, void *init = 0, void *init_end = 0)
  370.         {
  371.                 struct _TThreadDataTemplate
  372.                 {
  373.                         unsigned int data[12];
  374.                 };
  375.                 static const _TThreadDataTemplate _ThreadDataTemplate =
  376.                         {{3, 0x00320100, 0x00320100, 0x33FFFFFF, 0x806060FF, 0x00000000, 0x00FFFF40, 0, 0, 0, -1, -1}};
  377.  
  378.                 unsigned int pid = GetPid();
  379.                 volatile TThreadData *thread_table_item;
  380.                 Lock(&_ThreadMutex);
  381.                 if (_ExitProcessNow) ExitProcess();
  382.                 thread_table_item = &_ThreadTable[_HashByte(pid)];
  383.                 thread_data[KOLIBRI_THREAD_DATA_NEXT] = (void*)*thread_table_item;
  384.                 (unsigned int&)thread_data[KOLIBRI_THREAD_DATA_PID] = pid;
  385.                 *(_TThreadDataTemplate*)(thread_data + KOLIBRI_THREAD_DATA_FLAG) = _ThreadDataTemplate;
  386.                 *thread_table_item = thread_data;
  387.                 UnLock(&_ThreadMutex);
  388.                 if (_ExitProcessNow) ExitProcess();
  389.                 _CallFunctionPriority(init, init_end, false);
  390.                 TStartData start_data;
  391.                 _GetStartData(start_data, thread_data);
  392.                 _ApplyCommonColors(start_data.WinData);
  393.                 (unsigned int&)thread_data[KOLIBRI_THREAD_DATA_FLAG] |= 0x40000000;
  394.                 thread_data[KOLIBRI_THREAD_DATA_TITLE] = (void*)(&start_data);
  395.                 if (!KolibriOnStart(start_data, thread_data)) return false;
  396.                 (unsigned int&)thread_data[KOLIBRI_THREAD_DATA_FLAG] &= ~0x40000000;
  397.                 _SetStartData(start_data, thread_data);
  398.                 return true;
  399.         }
  400.  
  401.         void _RemoveThreadData(TThreadData thread_data, void *exit = 0, void *exit_end = 0)
  402.         {
  403.                 _CallFunctionPriority(exit, exit_end, true);
  404.                 volatile TThreadData *thread_table_item;
  405.                 Lock(&_ThreadMutex);
  406.                 if (_ExitProcessNow) ExitProcess();
  407.                 thread_table_item = &_ThreadTable[_HashByte(GetPid(thread_data))];
  408.                 while (*thread_table_item)
  409.                 {
  410.                         if (*thread_table_item == thread_data)
  411.                         {
  412.                                 *thread_table_item = (TThreadData)thread_data[KOLIBRI_THREAD_DATA_NEXT];
  413.                                 break;
  414.                         }
  415.                         thread_table_item = (TThreadData*)(*thread_table_item + KOLIBRI_THREAD_DATA_NEXT);
  416.                 }
  417.                 UnLock(&_ThreadMutex);
  418.                 if (_ExitProcessNow) ExitProcess();
  419.         }
  420.  
  421.         void GetWindowData(TWindowData &win_data, TThreadData thread_data)
  422.         {
  423.                 if ((unsigned int)thread_data[KOLIBRI_THREAD_DATA_FLAG] & 0x40000000)
  424.                 {
  425.                         win_data = ((TStartData*)thread_data[KOLIBRI_THREAD_DATA_TITLE])->WinData;
  426.                         return;
  427.                 }
  428.                 win_data.WindowType = (unsigned short)((unsigned int)thread_data[KOLIBRI_THREAD_DATA_C_WINDOW] >> 24);
  429.                 win_data.HeaderType = (unsigned short)((unsigned int)thread_data[KOLIBRI_THREAD_DATA_C_HEADER] >> 24);
  430.                 win_data.WindowColor = (unsigned int)thread_data[KOLIBRI_THREAD_DATA_C_WINDOW] & 0xFFFFFF;
  431.                 win_data.HeaderColor = (unsigned int)thread_data[KOLIBRI_THREAD_DATA_C_HEADER] & 0xFFFFFF;
  432.                 win_data.BorderColor = (unsigned int)thread_data[KOLIBRI_THREAD_DATA_C_BORDER] & 0xFFFFFF;
  433.                 win_data.TitleColor = (unsigned int)thread_data[KOLIBRI_THREAD_DATA_C_TITLE] & 0xFFFFFF;
  434.                 win_data.Title = (char*)thread_data[KOLIBRI_THREAD_DATA_TITLE];
  435.         }
  436.  
  437.         void SetWindowData(const TWindowData &win_data, TThreadData thread_data)
  438.         {
  439.                 if ((unsigned int)thread_data[KOLIBRI_THREAD_DATA_FLAG] & 0x40000000)
  440.                 {
  441.                         ((TStartData*)thread_data[KOLIBRI_THREAD_DATA_TITLE])->WinData = win_data;
  442.                         return;
  443.                 }
  444.                 (unsigned int&)thread_data[KOLIBRI_THREAD_DATA_C_WINDOW] =
  445.                                         ((unsigned int)win_data.WindowType << 24) | (win_data.WindowColor & 0xFFFFFF);
  446.                 (unsigned int&)thread_data[KOLIBRI_THREAD_DATA_C_HEADER] =
  447.                                         ((unsigned int)win_data.HeaderType << 24) | (win_data.HeaderColor & 0xFFFFFF);
  448.                 (unsigned int&)thread_data[KOLIBRI_THREAD_DATA_C_BORDER] = win_data.BorderColor & 0xFFFFFF;
  449.                 (unsigned int&)thread_data[KOLIBRI_THREAD_DATA_C_TITLE] = win_data.TitleColor & 0xFFFFFF;
  450.                 thread_data[KOLIBRI_THREAD_DATA_TITLE] = (void*)win_data.Title;
  451.                 Invalidate(1, thread_data);
  452.         }
  453.  
  454.         void CloseWindow(TThreadData thread_data)
  455.         {
  456.                 (unsigned int&)thread_data[KOLIBRI_THREAD_DATA_FLAG] |= 0x80000000;
  457.         }
  458.  
  459.         void Invalidate(int frame, TThreadData thread_data)
  460.         {
  461.                 if (frame < 0) return;
  462.                 (unsigned int&)thread_data[KOLIBRI_THREAD_DATA_FLAG] |= (frame ? 3 : 1);
  463.         }
  464.  
  465.         void* GetPicture(unsigned short &width, unsigned short &height, TThreadData thread_data)
  466.         {
  467.                 width = (unsigned short)((unsigned int)thread_data[KOLIBRI_THREAD_DATA_SZ_PICT] >> 16);
  468.                 height = (unsigned short)((unsigned int)thread_data[KOLIBRI_THREAD_DATA_SZ_PICT]);
  469.                 return (void*)thread_data[KOLIBRI_THREAD_DATA_PICTURE];
  470.         }
  471.  
  472.         void SetPicture(void *picture, unsigned short width, unsigned short height, TThreadData thread_data)
  473.         {
  474.                 thread_data[KOLIBRI_THREAD_DATA_PICTURE] = (void*)picture;
  475.                 (unsigned int&)thread_data[KOLIBRI_THREAD_DATA_SZ_PICT] =
  476.                                         (width == 0 || height == 0) ? 0 : (((unsigned int)width << 16) | height);
  477.                 Invalidate(0, thread_data);
  478.         }
  479.  
  480.         int _GetSkinHeader();
  481.  
  482.         void GetBorderHeader(unsigned short &border_size, unsigned short &header_size, TThreadData thread_data)
  483.         {
  484.                 int win_type = ((unsigned int)thread_data[KOLIBRI_THREAD_DATA_FLAG] & 0x40000000) ?
  485.                         ((TStartData*)thread_data[KOLIBRI_THREAD_DATA_TITLE])->WinData.WindowType :
  486.                    ((unsigned int)thread_data[KOLIBRI_THREAD_DATA_C_WINDOW] >> 24);
  487.                 border_size = KOLIBRI_BORDER_SIZE;
  488.                 header_size = short(((win_type & 15) == 3) ? _GetSkinHeader() : KOLIBRI_HEADER_SIZE);
  489.         }
  490.  
  491.         void GetClientSize(unsigned short &width, unsigned short &height,
  492.                                                 int win_width, int win_height, TThreadData thread_data)
  493.         {
  494.                 const int MAX_SIZE = 32767;
  495.                 unsigned short border_size, header_size;
  496.                 GetBorderHeader(border_size, header_size, thread_data);
  497.                 win_width -= 2 * border_size;
  498.                 win_height -= border_size + header_size;
  499.                 if (win_width < 0) win_width = 0;
  500.                 else if (win_width > MAX_SIZE) win_width = MAX_SIZE;
  501.                 if (win_height < 0) win_height = 0;
  502.                 else if (win_height > MAX_SIZE) win_height = MAX_SIZE;
  503.                 width = (unsigned short)win_width;
  504.                 height = (unsigned short)win_height;
  505.         }
  506.  
  507.         void GetMousePosPicture(short &x, short &y)
  508.         {
  509.                 unsigned short dx, dy;
  510.                 GetMousePosition(x, y);
  511.                 GetBorderHeader(dx, dy);
  512.                 x -= dx; y -= dy;
  513.         }
  514. }
  515.  
  516. #else   // def __KOLIBRI__
  517.  
  518. namespace Kolibri
  519. {
  520.         struct TMutex
  521.         {
  522.                 unsigned int mut;
  523.  
  524.                 TMutex();
  525.                 ~TMutex();
  526.         };
  527. #undef  KOLIBRI_MUTEX_INIT
  528. #define KOLIBRI_MUTEX_INIT  TMutex()
  529.  
  530.         struct TRecMutex
  531.         {
  532.                 unsigned int mut;
  533.  
  534.                 TRecMutex();
  535.                 ~TRecMutex();
  536.         };
  537. #undef  KOLIBRI_REC_MUTEX_INIT
  538. #define KOLIBRI_REC_MUTEX_INIT  TRecMutex()
  539. }
  540.  
  541. #endif  // else: def __KOLIBRI__
  542.  
  543. #endif  // ndef __KOLIBRI_H_INCLUDED_
  544.  
  545.