Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
8073 superturbo 1
#ifndef __KOS_32_SYS_H__
2
#define __KOS_32_SYS_H__
3
 
4
// file header taken from newlib
5
// added many sys functions, compatible with tcc
6
 
7
//#include 
8
//#include 
9
#include 
10
#include 
11
typedef unsigned int uint32_t;
12
typedef int int32_t;
13
typedef unsigned char uint8_t;
14
typedef unsigned short int uint16_t;
15
typedef unsigned long long uint64_t;
16
 
17
#ifdef __cplusplus
18
extern "C" {
19
#endif
20
 
21
//#ifdef CONFIG_DEBUF
22
//  #define DBG(format,...) printf(format,##__VA_ARGS__)
23
//#else
24
//  #define DBG(format,...)
25
//#endif
26
 
27
#define TYPE_3_BORDER_WIDTH  5
28
#define WIN_STATE_MINIMIZED  0x02
29
#define WIN_STATE_ROLLED     0x04
30
#define POS_SCREEN 0
31
#define POS_WINDOW 1
32
 
33
#define IPC_NOBUFFER 1
34
#define IPC_LOCKED 2
35
#define IPC_OVERFLOW 3
36
#define IPC_NOPID 4
37
 
38
#define SHM_OPEN        0x00
39
#define SHM_OPEN_ALWAYS 0x04
40
#define SHM_CREATE      0x08
41
#define SHM_READ        0x00
42
#define SHM_WRITE       0x01
43
 
44
 
45
 
46
typedef  unsigned int color_t;
47
 
48
 
49
typedef union __attribute__((packed)) pos_t
50
{
51
    uint32_t val;
52
    struct
53
    {
54
        short  x;
55
        short  y;
56
    };
57
} pos_t;
58
 
59
 
60
typedef union __attribute__((packed)) oskey_t
61
{
62
    uint32_t val;
63
    struct
64
    {
65
        uint8_t   state;
66
        uint8_t   code;
67
        uint16_t  ctrl_key;
68
    };
69
} oskey_t;
70
 
71
typedef struct
72
{
73
  unsigned      handle;
74
  unsigned      io_code;
75
  void          *input;
76
  int           inp_size;
77
  void          *output;
78
  int           out_size;
79
}ioctl_t;
80
 
81
typedef union
82
{
83
    struct
84
    {
85
        void   *data;
86
        size_t  size;
87
    } x;
88
    unsigned long long raw;
89
}ufile_t;
90
 
91
struct kolibri_system_colors {
92
  color_t frame_area;
93
  color_t grab_bar;
94
  color_t grab_bar_button;
95
  color_t grab_button_text;
96
  color_t grab_text;
97
  color_t work_area;
98
  color_t work_button;
99
  color_t work_button_text;
100
  color_t work_text;
101
  color_t work_graph;
102
};
103
 
104
 
105
struct blit_call
106
{
107
    int dstx;
108
    int dsty;
109
    int w;
110
    int h;
111
 
112
    int srcx;
113
    int srcy;
114
    int srcw;
115
    int srch;
116
 
117
    void *bitmap;
118
    int   stride;
119
};
120
 
121
struct ipc_message
122
{
123
    uint32_t    pid;        // PID of sending thread
124
    uint32_t    datalen;    // data bytes
125
    char        data[0];    // data begin
126
};
127
 
128
struct ipc_buffer
129
{
130
    uint32_t    lock;   // nonzero is locked
131
    uint32_t    used;   // used bytes in buffer
132
    struct ipc_message  data[0];    // data begin
133
};
134
 
135
static inline void begin_draw(void)
136
{
137
    __asm__ __volatile__(
138
    "int $0x40" ::"a"(12),"b"(1));
139
};
140
 
141
static inline
142
void end_draw(void)
143
{
144
    __asm__ __volatile__(
145
    "int $0x40" ::"a"(12),"b"(2));
146
};
147
 
148
static inline
149
void sys_create_window(int x, int y, int w, int h, const char *name,
150
                       color_t workcolor, uint32_t style)
151
{
152
    __asm__ __volatile__(
153
    "int $0x40"
154
    ::"a"(0),
155
     "b"((x << 16) | ((w-1) & 0xFFFF)),
156
     "c"((y << 16) | ((h-1) & 0xFFFF)),
157
     "d"((style << 24) | (workcolor & 0xFFFFFF)),
158
     "D"(name),
159
     "S"(0) : "memory");
160
};
161
 
162
static inline
163
void define_button(uint32_t x_w, uint32_t y_h, uint32_t id, uint32_t color)
164
{
165
    __asm__ __volatile__(
166
    "int $0x40"
167
    ::"a"(8),
168
      "b"(x_w),
169
      "c"(y_h),
170
      "d"(id),
171
      "S"(color));
172
};
173
 
174
static inline
175
void draw_line(int xs, int ys, int xe, int ye, color_t color)
176
{
177
    __asm__ __volatile__(
178
    "int $0x40"
179
    ::"a"(38), "d"(color),
180
      "b"((xs << 16) | xe),
181
      "c"((ys << 16) | ye));
182
}
183
 
184
static inline
185
void draw_bar(int x, int y, int w, int h, color_t color)
186
{
187
    __asm__ __volatile__(
188
    "int $0x40"
189
    ::"a"(13), "d"(color),
190
      "b"((x << 16) | w),
191
      "c"((y << 16) | h));
192
}
193
 
194
static inline
195
void draw_bitmap(void *bitmap, int x, int y, int w, int h)
196
{
197
    __asm__ __volatile__(
198
    "int $0x40"
199
    ::"a"(7), "b"(bitmap),
200
      "c"((w << 16) | h),
201
      "d"((x << 16) | y));
202
}
203
 
204
static inline
205
void draw_text_sys(const char *text, int x, int y, int len, color_t color)
206
{
207
    __asm__ __volatile__(
208
    "int $0x40"
209
    ::"a"(4),"d"(text),
210
      "b"((x << 16) | y),
211
      "S"(len),"c"(color)
212
     :"memory");
213
}
214
 
215
static inline
216
uint32_t get_skin_height(void)
217
{
218
    uint32_t height;
219
 
220
    __asm__ __volatile__(
221
    "int $0x40 \n\t"
222
    :"=a"(height)
223
    :"a"(48),"b"(4));
224
    return height;
225
};
226
 
227
static inline
228
pos_t get_mouse_pos(int origin)
229
{
230
    pos_t val;
231
 
232
    __asm__ __volatile__(
233
    "int $0x40 \n\t"
234
    "rol $16, %%eax"
235
    :"=a"(val)
236
    :"a"(37),"b"(origin));
237
    return val;
238
}
239
 
240
static inline
241
uint32_t get_mouse_buttons(void)
242
{
243
    uint32_t val;
244
 
245
    __asm__ __volatile__(
246
    "int $0x40"
247
    :"=a"(val)
248
    :"a"(37),"b"(2));
249
    return val;
250
};
251
 
252
static inline
253
uint32_t get_mouse_wheels(void)
254
{
255
    uint32_t val;
256
 
257
    __asm__ __volatile__(
258
    "int $0x40 \n\t"
259
    :"=a"(val)
260
    :"a"(37),"b"(7));
261
    return val;
262
};
263
 
264
static inline uint32_t load_cursor(void *path, uint32_t flags)
265
{
266
    uint32_t  val;
267
    __asm__ __volatile__(
268
    "int $0x40"
269
    :"=a"(val)
270
    :"a"(37), "b"(4), "c"(path), "d"(flags));
271
    return val;
272
}
273
 
274
static inline uint32_t  set_cursor(uint32_t  cursor)
275
{
276
    uint32_t  old;
277
    __asm__ __volatile__(
278
    "int $0x40"
279
    :"=a"(old)
280
    :"a"(37), "b"(5), "c"(cursor));
281
    return old;
282
};
283
 
284
static inline int destroy_cursor(uint32_t cursor)
285
{
286
    int ret;
287
    __asm__ __volatile__(
288
    "int $0x40"
289
    :"=a"(ret)
290
    :"a"(37), "b"(6), "c"(cursor)
291
    :"memory");
292
    return ret;
293
};
294
 
295
 
296
static inline
297
uint32_t wait_for_event(uint32_t time)
298
{
299
    uint32_t val;
300
    __asm__ __volatile__(
301
    "int $0x40"
302
    :"=a"(val)
303
    :"a"(23), "b"(time));
304
    return val;
305
};
306
 
307
static inline uint32_t check_os_event()
308
{
309
    uint32_t val;
310
    __asm__ __volatile__(
311
    "int $0x40"
312
    :"=a"(val)
313
    :"a"(11));
314
    return val;
315
};
316
 
317
static inline uint32_t get_os_event()
318
{
319
    uint32_t val;
320
    __asm__ __volatile__(
321
    "int $0x40"
322
    :"=a"(val)
323
    :"a"(10));
324
    return val;
325
};
326
 
327
static inline
328
uint32_t get_tick_count(void)
329
{
330
    uint32_t val;
331
    __asm__ __volatile__(
332
    "int $0x40"
333
    :"=a"(val)
334
    :"a"(26),"b"(9));
335
    return val;
336
};
337
 
338
static inline
339
uint64_t get_ns_count(void)
340
{
341
    uint64_t val;
342
    __asm__ __volatile__(
343
    "int $0x40"
344
    :"=A"(val)
345
    :"a"(26), "b"(10));
346
    return val;
347
};
348
 
349
static inline
350
oskey_t get_key(void)
351
{
352
    oskey_t val;
353
    __asm__ __volatile__(
354
    "int $0x40"
355
    :"=a"(val)
356
    :"a"(2));
357
    return val;
358
}
359
 
360
static inline
361
uint32_t get_os_button()
362
{
363
    uint32_t val;
364
    __asm__ __volatile__(
365
    "int $0x40"
366
    :"=a"(val)
367
    :"a"(17));
368
    return val>>8;
369
};
370
 
371
static inline uint32_t get_service(char *name)
372
{
373
    uint32_t retval = 0;
374
    __asm__ __volatile__(
375
    "int $0x40"
376
    :"=a"(retval)
377
    :"a"(68),"b"(16),"c"(name)
378
    :"memory");
379
 
380
    return retval;
381
};
382
 
383
static inline int call_service(ioctl_t *io)
384
{
385
    int retval;
386
 
387
    __asm__ __volatile__(
388
    "int $0x40"
389
    :"=a"(retval)
390
    :"a"(68),"b"(17),"c"(io)
391
    :"memory","cc");
392
 
393
    return retval;
394
};
395
 
396
 
397
static inline void yield(void)
398
{
399
    __asm__ __volatile__(
400
    "int $0x40"
401
    ::"a"(68), "b"(1));
402
};
403
 
404
static inline void delay(uint32_t time)
405
{
406
    __asm__ __volatile__(
407
    "int $0x40"
408
    ::"a"(5), "b"(time)
409
    :"memory");
410
};
411
 
412
static inline
413
void *user_alloc(size_t size)
414
{
415
    void  *val;
416
    __asm__ __volatile__(
417
    "int $0x40"
418
    :"=a"(val)
419
    :"a"(68),"b"(12),"c"(size));
420
    return val;
421
}
422
 
423
static inline
424
int user_free(void *mem)
425
{
426
    int  val;
427
    __asm__ __volatile__(
428
    "int $0x40"
429
    :"=a"(val)
430
    :"a"(68),"b"(13),"c"(mem));
431
    return val;
432
}
433
 
434
static inline
435
void* user_realloc(void *mem, size_t size)
436
{
437
    void *val;
438
    __asm__ __volatile__(
439
    "int $0x40"
440
    :"=a"(val)
441
    :"a"(68),"b"(20),"c"(size),"d"(mem)
442
    :"memory");
443
 
444
    return val;
445
};
446
 
447
static inline
448
int *user_unmap(void *base, size_t offset, size_t size)
449
{
450
    int  *val;
451
    __asm__ __volatile__(
452
    "int $0x40"
453
    :"=a"(val)
454
    :"a"(68),"b"(26),"c"(base),"d"(offset),"S"(size));
455
    return val;
456
};
457
 
458
static inline ufile_t load_file(const char *path)
459
{
460
    ufile_t uf;
461
 
462
    __asm__ __volatile__ (
463
    "int $0x40"
464
    :"=A"(uf.raw)
465
    :"a" (68), "b"(27),"c"(path));
466
 
467
    return uf;
468
};
469
 
470
static inline int GetScreenSize()
471
{
472
    int retval;
473
 
474
    __asm__ __volatile__(
475
    "int $0x40"
476
    :"=a"(retval)
477
    :"a"(61), "b"(1));
478
    return retval;
479
}
480
 
481
 
482
static inline void get_proc_info(char *info)
483
{
484
    __asm__ __volatile__(
485
    "int $0x40"
486
    :
487
    :"a"(9), "b"(info), "c"(-1)
488
    :"memory");
489
};
490
 
491
static inline void Blit(void *bitmap, int dst_x, int dst_y,
492
                        int src_x, int src_y, int w, int h,
493
                        int src_w, int src_h, int stride)
494
{
495
    volatile struct blit_call bc;
496
 
497
    bc.dstx = dst_x;
498
    bc.dsty = dst_y;
499
    bc.w    = w;
500
    bc.h    = h;
501
    bc.srcx = src_x;
502
    bc.srcy = src_y;
503
    bc.srcw = src_w;
504
    bc.srch = src_h;
505
    bc.stride = stride;
506
    bc.bitmap = bitmap;
507
 
508
    __asm__ __volatile__(
509
    "int $0x40"
510
    ::"a"(73),"b"(0),"c"(&bc.dstx));
511
};
512
 
513
 
514
// newlib exclusive
515
#ifndef __TINYC__
516
int create_thread(int (*proc)(void *param), void *param, int stack_size);
517
 
518
void* load_library(const char *name);
519
 
520
void* get_proc_address(void *handle, const char *proc_name);
521
 
522
void enumerate_libraries(int (*callback)(void *handle, const char* name,
523
                                         uint32_t base, uint32_t size, void *user_data),
524
                         void *user_data);
525
#endif
526
 
527
// May be next section need to be added in newlibc
528
 
529
enum KOLIBRI_GUI_EVENTS {
530
    KOLIBRI_EVENT_NONE = 0,     /* Event queue is empty */
531
    KOLIBRI_EVENT_REDRAW = 1,   /* Window and window elements should be redrawn */
532
    KOLIBRI_EVENT_KEY = 2,      /* A key on the keyboard was pressed */
533
    KOLIBRI_EVENT_BUTTON = 3,   /* A button was clicked with the mouse */
534
    KOLIBRI_EVENT_DESKTOP = 5,  /* Desktop redraw finished */
535
    KOLIBRI_EVENT_MOUSE = 6,    /* Mouse activity (movement, button press) was detected */
536
    KOLIBRI_EVENT_IPC = 7,      /* Interprocess communication notify */
537
    KOLIBRI_EVENT_NETWORK = 8,  /* Network event */
538
    KOLIBRI_EVENT_DEBUG = 9,    /* Debug subsystem event */
539
    KOLIBRI_EVENT_IRQBEGIN = 16 /* 16..31 IRQ0..IRQ15 interrupt =IRQBEGIN+IRQn */
540
};
541
 
542
 
543
// copied from /programs/system/shell/system/kolibri.c
544
// fn's returned -1 as syserror, 1 as error, 0 as OK
545
static inline
546
int kol_clip_num()
547
{
548
    register uint32_t val;
549
    asm volatile ("int $0x40":"=a"(val):"a"(54), "b"(0));
550
    return val;
551
}
552
 
553
static inline
554
char* kol_clip_get(int n)
555
// returned buffer must be freed by user_free()
556
{
557
    register char* val;
558
    asm volatile ("int $0x40":"=a"(val):"a"(54), "b"(1), "c"(n));
559
    return val;
560
}
561
 
562
static inline
563
int kol_clip_set(int n, char buffer[])
564
{
565
    register uint32_t val;
566
    asm volatile ("int $0x40":"=a"(val):"a"(54), "b"(2), "c"(n), "d"(buffer));
567
    return val;
568
}
569
 
570
static inline
571
int kol_clip_pop()
572
{
573
    register uint32_t val;
574
    asm volatile ("int $0x40":"=a"(val):"a"(54), "b"(3));
575
    return val;
576
}
577
 
578
static inline
579
int kol_clip_unlock()
580
{
581
    register uint32_t val;
582
    asm volatile ("int $0x40":"=a"(val):"a"(54), "b"(4));
583
    return val;
584
}
585
 
586
static inline void get_system_colors(struct kolibri_system_colors *color_table)
587
{
588
  __asm__ volatile ("int $0x40"
589
		    :
590
		    :"a"(48),"b"(3),"c"(color_table),"d"(40)
591
		    );
592
 
593
  /* color_table should point to the system color table */
594
}
595
 
596
static inline void debug_board_write_byte(const char ch){
597
    __asm__ __volatile__(
598
    "int $0x40"
599
    :
600
    :"a"(63), "b"(1), "c"(ch));
601
}
602
 
603
 
604
static inline void draw_number_sys(int32_t number, int x, int y, int len, color_t color){
605
    register uint32_t fmt;
606
    fmt = len << 16 | 0x80000000; // no leading zeros + width
607
//    fmt = len << 16 | 0x00000000; //  leading zeros + width
608
    __asm__ __volatile__(
609
    "int $0x40"
610
    :
611
    :"a"(47), "b"(fmt), "c"(number), "d"((x << 16) | y), "S"(color));
612
}
613
 
614
static inline
615
uint32_t get_mouse_eventstate(void)
616
{
617
    uint32_t val;
618
 
619
    __asm__ __volatile__(
620
    "int $0x40"
621
    :"=a"(val)
622
    :"a"(37),"b"(3));
623
    return val;
624
};
625
 
626
static inline
627
uint32_t set_event_mask(uint32_t mask)
628
{
629
    register uint32_t val;
630
    asm volatile ("int $0x40":"=a"(val):"a"(40), "b"(mask));
631
    return val;
632
}
633
 
634
typedef void (*thread_proc)(void*);
635
 
636
static inline
637
int start_thread(thread_proc proc, char* stack_top)
638
{
639
    register int val;
640
    asm volatile ("int $0x40":"=a"(val):"a"(51), "b"(1), "c"(proc), "d"(stack_top));
641
    return val;
642
}
643
 
644
static inline
645
void kos_exit()
646
{
647
    asm volatile ("int $0x40"::"a"(-1));
648
}
649
 
650
static inline void focus_window(int slot){
651
    asm volatile ("int $0x40"::"a"(18), "b"(3), "c"(slot));
652
}
653
 
654
static inline int get_thread_slot(int tid){
655
    register int val;
656
    asm volatile ("int $0x40":"=a"(val):"a"(18), "b"(21), "c"(tid));
657
    return val;
658
}
659
 
660
static inline void set_current_folder(char* dir){
661
    asm volatile ("int $0x40"::"a"(30), "b"(1), "c"(dir));
662
}
663
 
664
static inline int get_current_folder(char* buf, int bufsize){
665
    register int val;
666
    asm volatile ("int $0x40":"=a"(val):"a"(30), "b"(2), "c"(buf), "d"(bufsize));
667
    return val;
668
}
669
 
670
static inline
671
void ipc_set_area(void* buf, int bufsize){
672
    asm volatile ("int $0x40"::"a"(60), "b"(1), "c"(buf), "d"(bufsize));
673
}
674
 
675
static inline
676
int ipc_send_message(int pid_reciever, void *data, int datalen) {
677
    register int val;
678
    asm volatile ("int $0x40":"=a"(val):"a"(60), "b"(2), "c"(pid_reciever), "d"(data), "S"(datalen));
679
    return val;
680
}
681
 
682
static inline
683
void* shm_open(char *shm_name, int msize, int flags, int *retsz){
684
    register int val, cod;
685
    asm volatile ("int $0x40":"=a"(val),"=d"(cod):"a"(68), "b"(22), "c"(shm_name), "d"(msize), "S"(flags));
686
 
687
    if(retsz) *retsz = cod;  // errcode if NULL or memsize when open
688
    return (void*)val;
689
}
690
 
691
static inline
692
void shm_close(char *shm_name){
693
    asm volatile ("int $0x40"::"a"(68), "b"(23), "c"(shm_name));
694
}
695
 
696
static inline
697
int start_app(char *app_name, char *args){
698
    #pragma pack(push, 1)
699
    struct file_op_t
700
    {
701
        uint32_t    fn;
702
        uint32_t    flags;
703
        char*       args;
704
        uint32_t    res1, res2;
705
        char        zero;
706
        char*       app_name  __attribute__((packed));
707
    } file_op;
708
    #pragma pack(pop)
709
    memset(&file_op, 0, sizeof(file_op));
710
    file_op.fn = 7;
711
    file_op.args = args;
712
    file_op.app_name = app_name;
713
 
714
    register int val;
715
    asm volatile ("int $0x40":"=a"(val):"a"(70), "b"(&file_op));
716
 
717
    return val;
718
}
719
 
720
 
721
/*
722
static inline char *getcwd(char *buf, size_t size)
723
{
724
	int rc = get_current_folder(buf, size);
725
	if (rc > size)
726
	{
727
		errno = ERANGE;
728
		return 0;
729
	}
730
	else
731
		return buf;
732
}
733
*/
734
// end section
735
 
736
 
737
 
738
//added nonstatic inline because incomfortabre stepping in in debugger
739
void __attribute__ ((noinline)) debug_board_write_str(const char* str);
740
void __attribute__ ((noinline)) debug_board_printf(const char *format,...);
741
 
742
/* copy body to only one project file
743
void __attribute__ ((noinline)) debug_board_write_str(const char* str){
744
  while(*str)
745
    debug_board_write_byte(*str++);
746
}
747
 
748
void __attribute__ ((noinline)) debug_board_printf(const char *format,...)
749
{
750
        va_list ap;
751
        char log_board[300];
752
 
753
        va_start (ap, format);
754
        vsnprintf(log_board, sizeof log_board, format, ap);
755
        va_end(ap);
756
        debug_board_write_str(log_board);
757
}
758
*/
759
 
760
 
761
 
762
// TinyC don't support aliasing of static inline funcs
763
#ifndef __TINYC__
764
static inline void BeginDraw(void) __attribute__ ((alias ("begin_draw")));
765
static inline void EndDraw(void) __attribute__ ((alias ("end_draw")));
766
static inline void DrawWindow(int x, int y, int w, int h, const char *name,
767
                              color_t workcolor, uint32_t style)
768
                              __attribute__ ((alias ("sys_create_window")));
769
static inline void DefineButton(void) __attribute__ ((alias ("define_button")));
770
static inline void DrawLine(int xs, int ys, int xe, int ye, color_t color)
771
                            __attribute__ ((alias ("draw_line")));
772
static inline void DrawBar(int x, int y, int w, int h, color_t color)
773
                           __attribute__ ((alias ("draw_bar")));
774
static inline void DrawBitmap(void *bitmap, int x, int y, int w, int h)
775
                              __attribute__ ((alias ("draw_bitmap")));
776
static inline uint32_t GetSkinHeight(void) __attribute__ ((alias ("get_skin_height")));
777
static inline pos_t GetMousePos(int origin) __attribute__ ((alias ("get_mouse_pos")));
778
static inline uint32_t GetMouseButtons(void) __attribute__ ((alias ("get_mouse_buttons")));
779
static inline uint32_t GetMouseWheels(void) __attribute__ ((alias ("get_mouse_wheels")));
780
static inline uint32_t  LoadCursor(void *path, uint32_t flags) __attribute__ ((alias ("load_cursor")));
781
static inline uint32_t SetCursor(uint32_t  cursor) __attribute__ ((alias ("set_cursor")));
782
static inline int DestroyCursor(uint32_t cursor) __attribute__ ((alias ("destroy_cursor")));
783
static inline uint32_t GetOsEvent(void) __attribute__ ((alias ("get_os_event")));
784
static inline void *UserAlloc(size_t size) __attribute__ ((alias ("user_alloc")));
785
static inline int UserFree(void *mem) __attribute__ ((alias ("user_free")));
786
static inline void* UserRealloc(void *mem, size_t size) __attribute__ ((alias ("user_realloc")));
787
static inline int *UserUnmap(void *base, size_t offset, size_t size) __attribute__ ((alias ("user_unmap")));
788
static inline ufile_t LoadFile(const char *path) __attribute__ ((alias ("load_file")));
789
static inline void GetProcInfo(char *info) __attribute__ ((alias ("get_proc_info")));
790
#endif
791
 
792
#ifdef __cplusplus
793
}
794
#endif
795
 
796
 
797
#endif
798