Subversion Repositories Kolibri OS

Rev

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