Subversion Repositories Kolibri OS

Rev

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