Subversion Repositories Kolibri OS

Rev

Rev 969 | Rev 1143 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
968 leency 1
#include "kosSyst.h"
2
#include 
3
 
4
#define atexitBufferSize	32
5
 
6
 
7
char pureCallMessage[] = "PURE function call!";
8
 
9
char *kosExePath = NULL;
10
 
11
//
12
void (__cdecl *atExitList[atexitBufferSize])();
13
int atExitFnNum = 0;
14
//
15
int __cdecl atexit( void (__cdecl *func )( void ))
16
{
17
	//
18
	if ( atExitFnNum < atexitBufferSize )
19
	{
20
		//
21
		atExitList[atExitFnNum++] = func;
22
		return 0;
23
	}
24
	else
25
	{
26
		return 1;
27
	}
28
}
29
 
30
 
31
//
32
Dword RandomSeed = 1;
33
//
34
void rtlSrand( Dword seed )
35
{
36
	RandomSeed = seed;
37
}
38
//
39
Dword rtlRand( void )
40
{
41
  //маска 0x80000776
42
 
43
  Dword dwi, i;
44
 
45
  for ( i = 0; i < 32; i++ )
46
  {
47
 
48
    dwi = RandomSeed & 0x80000776;
49
 
50
      __asm{
51
            mov   eax, dwi
52
            mov   edx, eax
53
            bswap eax
54
            xor   eax, edx
55
            xor   al, ah
56
            setpo al
57
            movzx eax, al
58
            mov   dwi, eax
59
    }
60
 
61
    RandomSeed = ( RandomSeed << 1 ) | ( dwi & 1 );
62
  }
63
 
64
 return RandomSeed;
65
}
66
 
67
#if _MSC_VER >= 1400
68
//
69
void * __cdecl memcpy( void *dst, const void *src, size_t bytesCount )
70
{
71
	__asm{
72
		mov edi, dst
73
		mov eax, dst
74
		mov esi, src
75
		mov ecx, bytesCount
76
		rep movsb
77
	}
78
}
79
 
80
//
81
void memset( Byte *dst, Byte filler, Dword count )
82
{
83
	//
84
	__asm{
85
		mov edi, dst
86
		mov al, filler
87
		mov ecx, count
88
		rep stosb
89
	}
90
}
91
#endif
92
 
93
//
94
Dword rtlInterlockedExchange( Dword *target, Dword value )
95
{
96
//	Dword result;
97
 
98
	//
99
	__asm{
100
		mov eax, value
101
		mov ebx, target
102
		xchg eax, [ebx]
103
//		mov result, eax
104
	}
105
	//
106
//	return result;
107
}
108
 
109
 
110
//////////////////////////////////////////////////////////////////////
111
//
112
// копирование строки
113
//
114
 
115
char * __cdecl strcpy( char *target, const char *source )
116
{
117
	char *result = target;
118
 
119
	while( target[0] = source[0] )
120
	{
121
		target++;
122
		source++;
123
	}
124
 
125
	return result;
126
}
127
 
128
 
129
//////////////////////////////////////////////////////////////////////
130
//
131
// реверсивный поиск символа
132
//
133
 
134
char * __cdecl strrchr( const char * string, int c )
135
{
136
	char *cPtr;
137
 
138
	//
139
	for ( cPtr = (char *)string + strlen( string ); cPtr >= string; cPtr-- )
140
	{
141
		//
142
		if ( *cPtr == c ) return cPtr;
143
	}
144
	//
145
	return NULL;
146
}
147
 
148
 
149
//////////////////////////////////////////////////////////////////////
150
//
151
// определение длины строки
152
//
153
 
154
int __cdecl strlen( const char *line )
155
{
156
  int i;
157
 
158
  for( i=0; line[i] != 0; i++ );
159
  return i;
160
}
161
 
162
 
163
 
164
//////////////////////////////////////////////////////////////////////
165
//
166
// перевод шестнадцатиричного числа в символ
167
//
168
 
169
unsigned int num2hex( unsigned int num )
170
{
171
  if( num < 10 )
172
    return num + '0';
173
  return num - 10 + 'A';
174
}
175
 
176
 
177
//////////////////////////////////////////////////////////////////////
178
//
179
// вывод строки на печать
180
//
181
 
182
Dword dectab[] = { 1000000000, 100000000, 10000000, 1000000, 100000,
183
                   10000, 1000, 100, 10, 0 };
184
 
185
//
186
void sprintf( char *Str, char* Format, ... )
187
{
188
	int i, fmtlinesize, j, k, flag;
189
	Dword head, tail;
190
	char c;
191
	va_list arglist;
192
	//
193
	va_start(arglist, Format);
194
 
195
	//
196
	fmtlinesize = strlen( Format );
197
	//
198
	if( fmtlinesize == 0 ) return;
199
 
200
	//
201
	for( i = 0, j = 0; i < fmtlinesize; i++ )
202
	{
203
		//
204
		c = Format[i];
205
		//
206
		if( c != '%' )
207
		{
208
			Str[j++] = c;
209
			continue;
210
		}
211
		//
212
		i++;
213
		//
214
		if( i >= fmtlinesize ) break;
215
 
216
		//
217
		flag = 0;
218
		//
219
		c = Format[i];
220
		//
221
		switch( c )
222
		{
223
		//
224
		case '%':
225
			Str[j++] = c;
226
			break;
227
		// вывод строки
228
		case 'S':
229
			Byte* str;
230
			str = va_arg(arglist, Byte*);
231
			for( k = 0; ( c = str[k] ) != 0; k++ )
232
			{
233
				Str[j++] = c;
234
			}
235
			break;
236
		// вывод байта
237
		case 'B':
238
			k = va_arg(arglist, int) & 0xFF;
239
			Str[j++] = num2hex( ( k >> 4 ) & 0xF );
240
			Str[j++] = num2hex( k & 0xF );
241
			break;
242
		// вывод символа
243
		case 'C':
244
			Str[j++] = va_arg(arglist, int) & 0xFF;
245
			break;
246
		// вывод двойного слова в шестнадцатиричном виде
247
		case 'X':
248
			Dword val;
249
			val = va_arg(arglist, Dword);
250
			for( k = 7; k >= 0; k-- )
251
			{
252
				//
253
				c = num2hex ( ( val >> (k * 4) ) & 0xF );
254
				//
255
				if( c == '0' )
256
				{
257
					if( flag ) Str[j++] = c;
258
				}
259
				else
260
				{
261
					flag++;
262
					Str[j++] = c;
263
				}
264
			}
265
			//
266
			if( flag == 0 ) Str[j++] = '0';
267
			break;
268
		// вывод двойного слова в десятичном виде
269
		case 'U':
270
			head = va_arg(arglist, Dword);
271
			tail = 0;
272
			for( k = 0; dectab[k] != 0; k++ )
273
			{
274
				tail = head % dectab[k];
275
				head /= dectab[k];
276
				c = head + '0';
277
				if( c == '0' )
278
				{
279
					if( flag ) Str[j++] = c;
280
				}
281
				else
282
				{
283
					flag++;
284
					Str[j++] = c;
285
				}
286
				//
287
				head = tail;
288
			}
289
			//
290
			c = head + '0';
291
			Str[j++] = c;
292
			break;
293
		// вывод 64-битного слова в шестнадцатиричном виде
294
		case 'Q':
295
			unsigned int low_dword, high_dword;
296
			low_dword = va_arg(arglist, unsigned int);
297
			high_dword = va_arg(arglist, unsigned int);
298
			for( k = 7; k >= 0; k-- )
299
			{
300
				//
301
				c = num2hex ( ( ( high_dword + 1) >> (k * 4) ) & 0xF );
302
				//
303
				if( c == '0' )
304
				{
305
					if( flag ) Str[j++] = c;
306
				}
307
				else
308
				{
309
					flag++;
310
					Str[j++] = c;
311
				}
312
			}
313
			//
314
			for( k=7; k >= 0; k-- )
315
			{
316
				//
317
				c = num2hex ( ( low_dword >> (k * 4) ) & 0xF );
318
				//
319
				if( c == '0' )
320
				{
321
					if( flag ) Str[j++] = c;
322
				}
323
				else
324
				{
325
					flag++;
326
					Str[j++] = c;
327
				}
328
			}
329
			//
330
			if( flag == 0 ) Str[j++] = '0';
331
			//
332
			break;
333
		//
334
		default:
335
			break;
336
		}
337
	}
338
	//
339
	Str[j] = 0;
340
}
341
 
342
 
343
// функция -1 завершения процесса
344
void kos_ExitApp()
345
{
346
	int i;
347
 
348
	//
349
	for ( i = atExitFnNum - 1; i >= 0; i-- )
350
	{
351
		//
352
		atExitList[i]();
353
	}
354
	//
355
	__asm{
356
		mov eax, -1
357
		int 0x40
358
	}
359
}
360
 
361
 
362
// функция 0
363
void kos_DefineAndDrawWindow(
364
	Word x, Word y,
365
	Word sizeX, Word sizeY,
366
	Byte mainAreaType,
367
	Dword mainAreaColour,
368
	Byte headerType,
369
	Dword headerColour,
370
	Dword borderColour
371
	)
372
{
373
	Dword arg1, arg2, arg3, arg4;
374
 
375
	//
376
	arg1 = ( x << 16 ) + sizeX;
377
	arg2 = ( y << 16 ) + sizeY;
378
	arg3 = ( mainAreaType << 24 ) | mainAreaColour;
379
	arg4 = ( headerType << 24 ) | headerColour;
380
	//
381
	__asm{
382
		mov eax, 0
383
		mov ebx, arg1
384
		mov ecx, arg2
385
		mov edx, arg3
386
		mov esi, arg4
387
		mov edi, borderColour
388
		int 0x40
389
	}
390
}
391
 
392
 
393
// функция 1 поставить точку
394
void kos_PutPixel( Dword x, Dword y, Dword colour )
395
{
396
	//
397
	__asm{
398
		mov eax, 1
399
		mov ebx, x
400
		mov ecx, y
401
		mov edx, colour
402
		int 0x40
403
	}
404
}
405
 
406
 
407
// функция 2 получить код нажатой клавиши
408
bool kos_GetKey( Byte &keyCode )
409
{
410
	Dword result;
411
 
412
	//
1012 leency 413
	__asm
414
	{
415
		push edx
416
getkey:
417
		mov  eax,2 ; Gluk
418
		int  0x40
419
		cmp eax,1
420
		jne getkeyi
421
		mov ah,dh
422
		jmp getkeyii
423
getkeyi:
424
		mov dh,ah ; Gluk
425
		jmp getkey
426
getkeyii:
427
		pop edx
968 leency 428
		mov result, eax
429
	}
430
	//
431
	keyCode = result >> 8;
432
	//
433
	return ( result & 0xFF ) == 0;
434
}
435
 
436
 
437
// функция 3 получить время
438
Dword kos_GetSystemClock()
439
{
440
//	Dword result;
441
 
442
	//
443
	__asm{
444
		mov eax, 3
445
		int 0x40
446
//		mov result, eax
447
	}
448
	//
449
//	return result;
450
}
451
 
452
 
453
// функция 4
454
void kos_WriteTextToWindow(
455
	Word x,
456
	Word y,
457
	Byte fontType,
458
	Dword textColour,
459
	char *textPtr,
460
	Dword textLen
461
	)
462
{
463
	Dword arg1, arg2;
464
 
465
	//
466
	arg1 = ( x << 16 ) | y;
467
	arg2 = ( fontType << 24 ) | textColour;
468
	//
469
	__asm{
470
		mov eax, 4
471
		mov ebx, arg1
472
		mov ecx, arg2
473
		mov edx, textPtr
474
		mov esi, textLen
475
		int 0x40
476
	}
477
}
478
 
479
 
480
// функция 5 пауза, в сотых долях секунды
481
void kos_Pause( Dword value )
482
{
483
	//
484
	__asm{
485
		mov eax, 5
486
		mov ebx, value
487
		int 0x40
488
	}
489
}
490
 
491
 
492
// функция 7 нарисовать изображение
493
void kos_PutImage( RGB * imagePtr, Word sizeX, Word sizeY, Word x, Word y )
494
{
495
	Dword arg1, arg2;
496
 
497
	//
498
	arg1 = ( sizeX << 16 ) | sizeY;
499
	arg2 = ( x << 16 ) | y;
500
	//
501
	__asm{
502
		mov eax, 7
503
		mov ebx, imagePtr
504
		mov ecx, arg1
505
		mov edx, arg2
506
		int 0x40
507
	}
508
}
509
 
510
 
511
 
512
// функция 8 определить кнопку
513
void kos_DefineButton( Word x, Word y, Word sizeX, Word sizeY, Dword buttonID, Dword colour )
514
{
515
	Dword arg1, arg2;
516
 
517
	//
518
	arg1 = ( x << 16 ) | sizeX;
519
	arg2 = ( y << 16 ) | sizeY;
520
	//
521
	__asm{
522
		mov eax, 8
523
		mov ebx, arg1
524
		mov ecx, arg2
525
		mov edx, buttonID
526
		mov esi, colour
527
		int 0x40
528
	}
529
}
530
 
531
 
532
// функция 9 - информация о процессе
533
Dword kos_ProcessInfo( sProcessInfo *targetPtr, Dword processID )
534
{
535
//	Dword result;
536
 
537
	//
538
	__asm{
539
		mov eax, 9
540
		mov ebx, targetPtr
541
		mov ecx, processID
542
		int 0x40
543
//		mov result, eax
544
	}
545
	//
546
//	return result;
547
}
548
 
549
 
550
// функция 10
551
Dword kos_WaitForEvent()
552
{
553
//	Dword result;
554
 
555
	__asm{
556
		mov eax, 10
557
		int 0x40
558
//		mov result, eax
559
	}
560
 
561
//	return result;
562
}
563
 
564
 
565
// функция 11
566
Dword kos_CheckForEvent()
567
{
568
//	Dword result;
569
 
570
	__asm{
571
		mov eax, 11
572
		int 0x40
573
//		mov result, eax
574
	}
575
 
576
//	return result;
577
}
578
 
579
 
580
// функция 12
581
void kos_WindowRedrawStatus( Dword status )
582
{
583
	__asm{
584
		mov eax, 12
585
		mov ebx, status
586
		int 0x40
587
	}
588
}
589
 
590
 
591
// функция 13 нарисовать полосу
592
void kos_DrawBar( Word x, Word y, Word sizeX, Word sizeY, Dword colour )
593
{
594
	Dword arg1, arg2;
595
 
596
	//
597
	arg1 = ( x << 16 ) | sizeX;
598
	arg2 = ( y << 16 ) | sizeY;
599
	//
600
	__asm{
601
		mov eax, 13
602
		mov ebx, arg1
603
		mov ecx, arg2
604
		mov edx, colour
605
		int 0x40
606
	}
607
}
608
 
609
 
610
// функция 17
611
bool kos_GetButtonID( Dword &buttonID )
612
{
613
	Dword result;
614
 
615
	//
616
	__asm{
617
		mov eax, 17
618
		int 0x40
619
		mov result, eax
620
	}
621
	//
622
	buttonID = result >> 8;
623
	//
624
	return (result & 0xFF) == 0;
625
}
626
 
627
 
628
// функция 23
629
Dword kos_WaitForEvent( Dword timeOut )
630
{
631
//	Dword result;
632
 
633
	__asm{
634
		mov eax, 23
635
		mov ebx, timeOut
636
		int 0x40
637
//		mov result, eax
638
	}
639
 
640
//	return result;
641
}
642
 
643
 
644
// получение информации о состоянии "мыши" функция 37
645
void kos_GetMouseState( Dword & buttons, int & cursorX, int & cursorY )
646
{
647
	Dword mB;
648
	Word curX;
649
	Word curY;
650
	sProcessInfo sPI;
651
 
652
	//
653
	__asm{
654
		mov		eax, 37
655
		mov		ebx, 0
656
		int		0x40
657
		mov		curY, ax
658
		shr		eax, 16
659
		mov		curX, ax
660
		mov		eax, 37
661
		mov		ebx, 2
662
		int		0x40
663
		mov		mB, eax
664
	}
665
	//
666
	kos_ProcessInfo( &sPI );
667
	//
668
	buttons = mB;
669
	cursorX = curX - sPI.processInfo.x_start;
670
	cursorY = curY - sPI.processInfo.y_start;
671
}
672
 
673
 
674
// функция 40 установить маску событий
675
void kos_SetMaskForEvents( Dword mask )
676
{
677
	//
678
	__asm{
679
		mov eax, 40
680
		mov ebx, mask
681
		int 0x40
682
	}
683
}
684
 
685
 
686
// функция 47 вывести в окно приложения число
687
void kos_DisplayNumberToWindow(
688
   Dword value,
689
   Dword digitsNum,
690
   Word x,
691
   Word y,
692
   Dword colour,
693
   eNumberBase nBase,
694
   bool valueIsPointer
695
   )
696
{
697
	Dword arg1, arg2;
698
 
699
	//
700
	arg1 = ( valueIsPointer ? 1 : 0 ) |
701
		( ((Byte)nBase) << 8 ) |
702
		( ( digitsNum & 0x1F ) << 16 );
703
	arg2 = ( x << 16 ) | y;
704
	//
705
	__asm{
706
		mov eax, 47
707
		mov ebx, arg1
708
		mov ecx, value
709
		mov edx, arg2
710
		mov esi, colour
711
		int 0x40
712
	}
713
}
714
 
969 leency 715
 
716
Dword kos_GetSkinWidth()
717
{
718
	__asm{
719
		mov eax, 48
720
		mov ebx, 4
721
		int 0x40
722
	}
723
}
724
 
725
 
968 leency 726
// функция 70 доступ к файловой системе
727
Dword kos_FileSystemAccess( kosFileInfo *fileInfo )
728
{
729
//	Dword result;
730
 
731
	//
732
	__asm{
733
		mov eax, 70
734
		mov ebx, fileInfo
735
		int 0x40
736
//		mov result, eax
737
	}
738
	//
739
//	return result;
740
}
741
 
742
 
743
// функция 63 вывод символя в окно отладки
744
void kos_DebugOutChar( char ccc )
745
{
746
	//
747
	__asm{
748
		mov eax, 63
749
		mov ebx, 1
750
		mov cl, ccc
751
		int 0x40
752
	}
753
}
754
 
755
 
756
// функция 66 режим получения данных от клавиатуры
757
void kos_SetKeyboardDataMode( Dword mode )
758
{
759
	//
760
	__asm{
761
		mov eax, 66
762
		mov ebx, 1
763
		mov ecx, mode
764
		int 0x40
765
	}
766
}
767
 
768
 
769
// вывод строки в окно отладки
770
void rtlDebugOutString( char *str )
771
{
772
	//
773
	for ( ; str[0] != 0; str++ )
774
	{
775
		kos_DebugOutChar( str[0] );
776
	}
777
	//
778
	kos_DebugOutChar( 13 );
779
	kos_DebugOutChar( 10 );
780
}
781
 
782
 
783
// функция 64 изменение количества памяти, выделенной для программы
784
bool kos_ApplicationMemoryResize( Dword targetSize )
785
{
786
	Dword result;
787
 
788
	//
789
	__asm{
790
		mov eax, 64
791
		mov ebx, 1
792
		mov ecx, targetSize
793
		int 0x40
794
		mov result, eax
795
	}
796
	//
797
	return result == 0;
798
}
799
 
800
 
801
// функция 67 изменить параметры окна, параметр == -1 не меняется
802
void kos_ChangeWindow( Dword x, Dword y, Dword sizeX, Dword sizeY )
803
{
804
	//
805
	__asm{
806
		mov eax, 67
807
		mov ebx, x
808
		mov ecx, y
809
		mov edx, sizeX
810
		mov esi, sizeY
811
		int 0x40
812
	}
813
}
814
 
815
 
816
 
817
// вызов абстрактного метода
818
int __cdecl _purecall()
819
{
820
	rtlDebugOutString( pureCallMessage );
821
	kos_ExitApp();
822
	return 0;
823
}
824
 
825
 
826
// вызов статических инициализаторов
827
// заодно инициализация генератора случайных чисел
828
//#pragma section(".CRT$XCA",long,read,write)
829
//#pragma section(".CRT$XCZ",long,read,write)
830
#pragma data_seg(".CRT$XCA")
831
#pragma data_seg(".CRT$XCZ")
832
typedef void (__cdecl *_PVFV)(void);
833
__declspec(allocate(".CRT$XCA"))  _PVFV __xc_a[1] = { NULL };
834
__declspec(allocate(".CRT$XCZ"))  _PVFV __xc_z[1] = { NULL };
835
//
836
#pragma comment(linker, "/merge:.CRT=.rdata")
837
//
838
void crtStartUp()
839
{
840
	// вызываем инициализаторы по списку, NULL'ы игнорируем
841
	for ( _PVFV *pbegin = __xc_a; pbegin < __xc_z; pbegin++ )
842
	{
843
		//
844
		if ( *pbegin != NULL )
845
			(**pbegin)();
846
	}
847
	// инициализируем генератор случайных чисел
848
	rtlSrand( kos_GetSystemClock() );
849
	// путь к файлу процесса
850
	kosExePath = *((char **)0x20);
851
	// вызов главной функции приложения
852
	kos_Main();
853
	// выход
854
	kos_ExitApp();
855
}
856