Subversion Repositories Kolibri OS

Rev

Rev 109 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 109 Rev 485
1
;
1
;
2
;   Example for Inter Process Communication
2
;   Example for Inter Process Communication
3
;
3
;
4
;   Compile with FASM for Menuet
4
;   Compile with FASM for Menuet
5
;
5
;
6
include 'lang.inc'
6
include 'lang.inc'
7
include 'macros.inc'
7
include '..\..\..\macros.inc'
8
 
8
 
9
  use32
9
  use32
10
  org    0x0
10
  org    0x0
11
 
11
 
12
  db     'MENUET01'             ; 8 byte id
12
  db     'MENUET01'             ; 8 byte id
13
  dd     0x01                   ; header version
13
  dd     0x01                   ; header version
14
  dd     START                  ; start of code
14
  dd     START                  ; start of code
15
  dd     I_END                  ; size of image
15
  dd     I_END                  ; size of image
16
  dd     0x60000                ; memory for app
16
  dd     0x60000                ; memory for app
17
  dd     0x60000                ; esp
17
  dd     0x60000                ; esp
18
  dd     0x0 , 0x0              ; I_Param , I_Icon
18
  dd     0x0 , 0x0              ; I_Param , I_Icon
19
 
19
 
20
START:                          ; start of execution
20
START:                          ; start of execution
21
 
21
 
22
 
22
 
23
    mov  eax,60                 ; IPC
23
    mov  eax,60                 ; IPC
24
    mov  ebx,1                  ; define receive area
24
    mov  ebx,1                  ; define receive area
25
    mov  ecx,received_messages  ; pointer to start
25
    mov  ecx,received_messages  ; pointer to start
26
    mov  edx,1000               ; size of area
26
    mov  edx,1000               ; size of area
27
    int  0x40
27
    mcall
28
 
28
 
29
    mov  eax,40                 ; WANTED EVENTS
29
    mov  eax,40                 ; WANTED EVENTS
30
    mov  ebx,01000111b          ; IPC 7 + defaults
30
    mov  ebx,01000111b          ; IPC 7 + defaults
31
    int  0x40
31
    mcall
32
 
32
 
33
    mov  [received_messages+8],dword 0*256+0
33
    mov  [received_messages+8],dword 0*256+0
34
    mov  [received_messages+12],dword 0
34
    mov  [received_messages+12],dword 0
-
 
35
 
35
 
36
  red:
36
    call draw_window            ; at first, draw the window
37
    call draw_window            ; at first, draw the window
37
 
38
 
38
still:
39
still:
39
 
40
 
40
    mov  eax,23                 ; wait here for event
41
    mov  eax,23                 ; wait here for event
41
    mov  ebx,50
42
    mov  ebx,50
42
    int  0x40
43
    mcall
43
 
44
 
44
    cmp  eax,1                  ; redraw request ?
45
    cmp  eax,1                  ; redraw request ?
45
    je   red
46
    je   red
46
    cmp  eax,2                  ; key in buffer ?
47
    cmp  eax,2                  ; key in buffer ?
47
    je   key
48
    je   key
48
    cmp  eax,3                  ; button in buffer ?
49
    cmp  eax,3                  ; button in buffer ?
49
    je   button
50
    je   button
50
 
51
 
51
    cmp  eax,7                  ; IPC ?
52
    cmp  eax,7                  ; IPC ?
52
    jne  no_ipc
53
    jne  no_ipc
53
    call display_ipc_messages
54
    call display_ipc_messages
54
    jmp  still
55
    jmp  still
55
  no_ipc:
56
  no_ipc:
56
 
57
 
57
    jmp  still
58
    jmp  still
58
 
-
 
59
  red:                          ; redraw
-
 
60
    call draw_window
-
 
61
    jmp  still
-
 
62
 
59
 
63
  key:                          ; key
60
  key:                          ; key
64
    mov  eax,2                  ; just read it and ignore
61
    mov  eax,2                  ; just read it and ignore
65
    int  0x40
62
    mcall
66
    jmp  still
63
    jmp  still
67
 
64
 
68
  button:                       ; button
65
  button:                       ; button
69
    mov  eax,17                 ; get id
66
    mov  eax,17                 ; get id
70
    int  0x40
67
    mcall
71
 
68
 
72
    cmp  ah,1                   ; button id=1 ?
69
    cmp  ah,1                   ; button id=1 ?
73
    jne  noclose
70
    jne  noclose
74
    mov  eax,-1                 ; close this program
71
    mov  eax,-1                 ; close this program
75
    int  0x40
72
    mcall
76
  noclose:
73
  noclose:
77
 
74
 
78
    cmp  ah,2
75
    cmp  ah,2
79
    jne  no_read
76
    jne  no_read
80
    call read_string
77
    call read_string
81
 
78
 
82
    movzx eax,byte [message]
79
    movzx eax,byte [message]
83
    sub   eax,48
80
    sub   eax,48
84
    imul  eax,10
81
    imul  eax,10
85
    movzx ebx,byte [message+1]
82
    movzx ebx,byte [message+1]
86
    add   eax,ebx
83
    add   eax,ebx
87
    sub   eax,48
84
    sub   eax,48
88
    imul  eax,10
85
    imul  eax,10
89
    movzx ebx,byte [message+2]
86
    movzx ebx,byte [message+2]
90
    add   eax,ebx
87
    add   eax,ebx
91
    sub   eax,48
88
    sub   eax,48
92
    imul  eax,10
89
    imul  eax,10
93
    movzx ebx,byte [message+3]
90
    movzx ebx,byte [message+3]
94
    add   eax,ebx
91
    add   eax,ebx
95
    sub   eax,48
92
    sub   eax,48
96
 
93
 
97
    mov   [PID],eax
94
    mov   [PID],eax
98
 
95
 
99
    mov  eax,60                 ; IPC
96
    mov  eax,60                 ; IPC
100
    mov  ebx,2                  ; send message
97
    mov  ebx,2                  ; send message
101
    mov  ecx,[PID]
98
    mov  ecx,[PID]
102
    mov  edx,message+4
99
    mov  edx,message+4
103
    mov  esi,20;[message_size]
100
    mov  esi,20;[message_size]
104
    int  0x40
101
    mcall
105
 
102
 
106
    jmp  still
103
    jmp  still
107
  no_read:
104
  no_read:
108
 
105
 
109
 
106
 
110
    cmp  ah,3
107
    cmp  ah,3
111
    jne  no_messages_pop        ; pop the first out
108
    jne  no_messages_pop        ; pop the first out
112
    call ipc_message_pop
109
    call ipc_message_pop
113
    jmp  still
110
    jmp  still
114
  no_messages_pop:
111
  no_messages_pop:
115
 
112
 
116
    jmp  still
113
    jmp  still
117
 
114
 
118
 
115
 
119
ipc_message_pop:
116
ipc_message_pop:
120
 
117
 
121
    pusha
118
    pusha
122
 
119
 
123
    cmp  [received_messages+4],dword 8
120
    cmp  [received_messages+4],dword 8
124
    je   already_empty
121
    je   already_empty
125
 
122
 
126
    mov  [received_messages],byte 1  ; lock the area
123
    mov  [received_messages],byte 1  ; lock the area
127
 
124
 
128
    push dword [received_messages+4]
125
    push dword [received_messages+4]
129
 
126
 
130
    mov  ecx,[received_messages+12]
127
    mov  ecx,[received_messages+12]
131
 
128
 
132
    sub  [received_messages+4],ecx
129
    sub  [received_messages+4],ecx
133
    sub  [received_messages+4],dword 8
130
    sub  [received_messages+4],dword 8
134
 
131
 
135
    mov  edi,received_messages+8
132
    mov  edi,received_messages+8
136
    mov  esi,edi
133
    mov  esi,edi
137
    add  esi,ecx
134
    add  esi,ecx
138
    add  esi,8
135
    add  esi,8
139
 
136
 
140
    pop  ecx
137
    pop  ecx
141
 
138
 
142
    cld
139
    cld
143
    rep  movsb
140
    rep  movsb
144
 
141
 
145
    call display_ipc_messages
142
    call display_ipc_messages
146
 
143
 
147
    mov  [received_messages],byte 0  ; free the area
144
    mov  [received_messages],byte 0  ; free the area
148
 
145
 
149
  already_empty:
146
  already_empty:
150
 
147
 
151
    popa
148
    popa
152
    ret
149
    ret
153
 
150
 
154
 
151
 
155
 
152
 
156
 display_ipc_messages:
153
 display_ipc_messages:
157
 
154
 
158
    pusha
155
    pusha
159
 
156
 
160
    mov  eax,13
157
    mov  eax,13
161
    mov  ebx,25*65536+245
158
    mov  ebx,25*65536+245
162
    mov  ecx,105*65536+90
159
    mov  ecx,105*65536+90
163
    mov  edx,0xdddddd
160
    mov  edx,0xdddddd
164
    int  0x40
161
    mcall
165
 
162
 
166
    cmp  [received_messages+4],dword 8  ; empty list
163
    cmp  [received_messages+4],dword 8  ; empty list
167
    je   ipma1
164
    je   ipma1
168
 
165
 
169
    mov  ebx,25*65536+105           ; draw info text with function 4
166
    mov  ebx,25*65536+105           ; draw info text with function 4
170
    mov  ecx,0x224466
167
    mov  ecx,0x224466
171
    mov  edx,received_messages+8
168
    mov  edx,received_messages+8
172
    mov  esi,40
169
    mov  esi,40
173
    mov  [counter],0
170
    mov  [counter],0
174
  newline2:
171
  newline2:
175
    pusha
172
    pusha
176
    mov  ecx,[edx]
173
    mov  ecx,[edx]
177
    and  ecx,0xfff
174
    and  ecx,0xfff
178
    mov  edx,ebx
175
    mov  edx,ebx
179
    mov  eax,47
176
    mov  eax,47
180
    mov  ebx,4*65536
177
    mov  ebx,4*65536
181
    mov  esi,0xff0000
178
    mov  esi,0xff0000
182
    int  0x40
179
    mcall
183
    popa
180
    popa
184
    pusha
181
    pusha
185
    mov  esi,20
182
    mov  esi,20
186
    add  edx,8
183
    add  edx,8
187
    add  ebx,30*65536
184
    add  ebx,30*65536
188
    mov  eax,4
185
    mov  eax,4
189
    int  0x40
186
    mcall
190
    popa
187
    popa
191
 
188
 
192
    add  ebx,10
189
    add  ebx,10
193
    mov  edi,[edx+4]
190
    mov  edi,[edx+4]
194
    add  edi,8
191
    add  edi,8
195
    and  edi,0xfff
192
    and  edi,0xfff
196
    add  edx,edi
193
    add  edx,edi
197
 
194
 
198
    mov  edi,[received_messages+4]
195
    mov  edi,[received_messages+4]
199
    add  edi,received_messages
196
    add  edi,received_messages
200
    cmp  edx,edi
197
    cmp  edx,edi
201
    jge  ipma1
198
    jge  ipma1
202
 
199
 
203
    inc  [counter]
200
    inc  [counter]
204
    cmp  [counter],8
201
    cmp  [counter],8
205
    jbe  newline2
202
    jbe  newline2
206
 
203
 
207
   ipma1:
204
   ipma1:
208
 
205
 
209
    popa
206
    popa
210
    ret
207
    ret
211
 
208
 
212
 
209
 
213
counter   dd  0x0
210
counter   dd  0x0
214
 
211
 
215
 
212
 
216
;   *********************************************
213
;   *********************************************
217
;   *******  WINDOW DEFINITIONS AND DRAW ********
214
;   *******  WINDOW DEFINITIONS AND DRAW ********
218
;   *********************************************
215
;   *********************************************
219
 
216
 
220
 
217
 
221
draw_window:
218
draw_window:
222
 
219
 
223
    mov  eax,12                    ; function 12:tell os about windowdraw
220
    mov  eax,12                    ; function 12:tell os about windowdraw
224
    mov  ebx,1                     ; 1, start of draw
221
    mov  ebx,1                     ; 1, start of draw
225
    int  0x40
222
    mcall
226
 
223
 
227
                                   ; DRAW WINDOW
224
                                   ; DRAW WINDOW
228
    mov  eax,0                     ; function 0 : define and draw window
225
    mov  eax,0                     ; function 0 : define and draw window
229
    mov  ebx,100*65536+290         ; [x start] *65536 + [x size]
226
    mov  ebx,100*65536+290         ; [x start] *65536 + [x size]
230
    mov  ecx,100*65536+220         ; [y start] *65536 + [y size]
227
    mov  ecx,100*65536+220         ; [y start] *65536 + [y size]
231
    mov  edx,0x03ffffff            ; color of work area RRGGBB,8->color gl
228
    mov  edx,0x13ffffff            ; color of work area RRGGBB,8->color gl
-
 
229
    mov  edi,title                 ; WINDOW LABEL
232
    int  0x40
230
    mcall
233
 
-
 
234
                                   ; WINDOW LABEL
-
 
235
    mov  eax,4                     ; function 4 : write text to window
-
 
236
    mov  ebx,8*65536+8             ; [x start] *65536 + [y start]
-
 
237
    mov  ecx,0x10ffffff            ; color of text RRGGBB
-
 
238
    mov  edx,labelt                ; pointer to text beginning
-
 
239
    mov  esi,labellen-labelt       ; text length
-
 
240
    int  0x40
231
 
241
 
232
                                   
242
    mov  eax,9
233
    mov  eax,9
243
    mov  ebx,process_info
234
    mov  ebx,process_info
244
    mov  ecx,-1
235
    mov  ecx,-1
245
    int  0x40
236
    mcall
246
 
237
 
247
    mov  eax,47
238
    mov  eax,47
248
    mov  ebx,4*65536
239
    mov  ebx,4*65536
249
    mov  ecx,[process_info+30]
240
    mov  ecx,[process_info+30]
250
    mov  edx,180*65536+35
241
    mov  edx,180*65536+35
251
    mov  esi,0x000000
242
    mov  esi,0x000000
252
    int  0x40
243
    mcall
253
 
244
 
254
    mov  eax,8                     ; MESSAGE
245
    mov  eax,8                     ; MESSAGE
255
    mov  ebx,25*65536+87
246
    mov  ebx,25*65536+87
256
    mov  ecx,50*65536+16
247
    mov  ecx,50*65536+16
257
    mov  edx,2
248
    mov  edx,2
258
    mov  esi,0x5588dd
249
    mov  esi,0x5588dd
259
    int  0x40
250
    mcall
260
 
251
 
261
    mov  eax,8                     ; POP
252
    ;mov  eax,8                     ; POP
262
    mov  ebx,216*65536+53
253
    mov  ebx,216*65536+53
263
    mov  ecx,80*65536+16
254
    mov  ecx,80*65536+16
264
    mov  edx,3
255
    mov  edx,3
265
    mov  esi,0x5588dd
-
 
266
    int  0x40
256
    mcall
-
 
257
 
267
 
258
    mov  eax,4
268
    mov  ebx,25*65536+35           ; draw info text with function 4
259
    mov  ebx,25*65536+35           ; draw info text with function 4
269
    mov  ecx,0x224466
260
    mov  ecx,0x224466
270
    mov  edx,text
261
    mov  edx,text
271
    mov  esi,40
262
    mov  esi,40
272
  newline:
263
  newline:
273
    mov  eax,4
264
    mcall
274
    int  0x40
-
 
275
    add  ebx,10
265
    add  ebx,10
276
    add  edx,40
266
    add  edx,40
277
    cmp  [edx],byte 'x'
267
    cmp  [edx],byte 'x'
278
    jne  newline
268
    jne  newline
279
 
269
 
280
    call display_ipc_messages
270
    call display_ipc_messages
281
 
271
 
282
    mov  eax,12                    ; function 12:tell os about windowdraw
272
    mov  eax,12                    ; function 12:tell os about windowdraw
283
    mov  ebx,2                     ; 2, end of draw
273
    mov  ebx,2                     ; 2, end of draw
284
    int  0x40
274
    mcall
285
 
275
 
286
    ret
276
    ret
287
 
277
 
288
 
278
 
289
 
279
 
290
 
280
 
291
read_string:
281
read_string:
292
 
282
 
293
    pusha
283
    pusha
294
 
284
 
295
    mov  [addr],dword message
285
    mov  [addr],dword message
296
    mov  [ya],55
286
    mov  [ya],55
297
    mov  [xa],120
287
    mov  [xa],120
298
 
288
 
299
    mov  ecx,20
289
    mov  ecx,20
300
    mov  edi,[addr]
290
    mov  edi,[addr]
301
    mov  al,' '
291
    mov  al,' '
302
    cld
292
    cld
303
    rep  stosb
293
    rep  stosb
304
 
294
 
305
    call print_text
295
    call print_text
306
 
296
 
307
    mov  edi,[addr]
297
    mov  edi,[addr]
308
 
298
 
309
  f11:
299
  f11:
310
    mov  eax,10
300
    mov  eax,10
311
    int  0x40
301
    mcall
312
    cmp  eax,2
302
    cmp  eax,2
313
    jz   fbu
303
    jz   fbu
314
 
304
 
315
  exit_readkey:
305
  exit_readkey:
316
 
306
 
317
    popa
307
    popa
318
    ret
308
    ret
319
 
309
 
320
  fbu:
310
  fbu:
321
    mov  eax,2
311
    mov  eax,2
322
    int  0x40  ; get key
312
    mcall  ; get key
323
    shr  eax,8
313
    shr  eax,8
324
 
314
 
325
    cmp  eax,13
315
    cmp  eax,13
326
    je   exit_readkey
316
    je   exit_readkey
327
 
317
 
328
    cmp  eax,8
318
    cmp  eax,8
329
    jnz  nobs
319
    jnz  nobs
330
    cmp  edi,[addr]
320
    cmp  edi,[addr]
331
    jz   f11
321
    jz   f11
332
    dec  edi
322
    dec  edi
333
    mov  [edi],byte ' '
323
    mov  [edi],byte ' '
334
    call print_text
324
    call print_text
335
    jmp  f11
325
    jmp  f11
336
  nobs:
326
  nobs:
337
 
327
 
338
    cmp  eax,31
328
    cmp  eax,31
339
    jbe  f11
329
    jbe  f11
340
    cmp  eax,95
330
    cmp  eax,95
341
    jb   keyok
331
    jb   keyok
342
    sub  eax,32
332
    sub  eax,32
343
  keyok:
333
  keyok:
344
    mov  [edi],al
334
    mov  [edi],al
345
 
335
 
346
    call print_text
336
    call print_text
347
 
337
 
348
    inc  edi
338
    inc  edi
349
    mov  esi,[addr]
339
    mov  esi,[addr]
350
    add  esi,20
340
    add  esi,20
351
    cmp  esi,edi
341
    cmp  esi,edi
352
    jnz  f11
342
    jnz  f11
353
 
343
 
354
    popa
344
    popa
355
    ret
345
    ret
356
 
346
 
357
 
347
 
358
 
348
 
359
print_text:
349
print_text:
360
 
350
 
361
    mov  eax,13
351
    mov  eax,13
362
    mov  ebx,[xa]
352
    mov  ebx,[xa]
363
    shl  ebx,16
353
    shl  ebx,16
364
    add  ebx,25*6
354
    add  ebx,25*6
365
    mov  ecx,[ya]
355
    mov  ecx,[ya]
366
    shl  ecx,16
356
    shl  ecx,16
367
    mov  cx,8
357
    mov  cx,8
368
    mov  edx,0xffffff
358
    mov  edx,0xffffff
369
    int  0x40
359
    mcall
370
 
360
 
371
    mov  eax,4
361
    mov  eax,4
372
    mov  ebx,[xa]
362
    mov  ebx,[xa]
373
    shl  ebx,16
363
    shl  ebx,16
374
    add  ebx,[ya]
364
    add  ebx,[ya]
375
    mov  ecx,0x000000
365
    mov  ecx,0x000000
376
    mov  edx,[addr]
366
    mov  edx,[addr]
377
    mov  esi,25
367
    mov  esi,25
378
    int  0x40
368
    mcall
379
 
369
 
380
    ret
370
    ret
381
 
371
 
382
 
372
 
383
 
373
 
384
 
374
 
385
 
375
 
386
 
376
 
387
; DATA AREA
377
; DATA AREA
388
 
378
 
389
ya   dd  0x0
379
ya   dd  0x0
390
xa   dd  0x0
380
xa   dd  0x0
391
addr dd  0x0
381
addr dd  0x0
392
 
382
 
393
text:
383
text:
394
    db 'PROCESS ID FOR THIS APP :               '
384
    db 'PROCESS ID FOR THIS APP :               '
395
    db '                                        '
385
    db '                                        '
396
    db '  PID:MESSAGE   0130 EXAMPLE MESSAGE    '
386
    db '  PID:MESSAGE   0130 EXAMPLE MESSAGE    '
397
    db '                                        '
387
    db '                                        '
398
    db '                                        '
388
    db '                                        '
399
    db 'RECEIVED:                          POP  '
389
    db 'RECEIVED:                          POP  '
400
    db 'x' ; <- END MARKER, DONT DELETE
390
    db 'x' ; <- END MARKER, DONT DELETE
401
 
391
 
402
 
-
 
403
labelt:
392
 
404
       db   'IPC - START AT LEAST 2'
-
 
405
labellen:
393
title      db   'IPC - START AT LEAST 2',0
406
 
394
 
407
I_END:
395
I_END:
408
 
396
 
409
PID:                 dd  0x0
397
PID:                 dd  0x0
410
message_size:        dd  20
398
message_size:        dd  20
411
 
399
 
412
received_messages:
400
received_messages:
413
 
401
 
414
      db  0      ; lock byte
402
      db  0      ; lock byte
415
      db  0,0,0  ; reserved
403
      db  0,0,0  ; reserved
416
      dd  8      ; pointer to free msg position from received_messages
404
      dd  8      ; pointer to free msg position from received_messages
417
 
405
 
418
; Sender PID
406
; Sender PID
419
; Msg length
407
; Msg length
420
; Msg data
408
; Msg data
421
 
409
 
422
rb 0x1000
410
rb 0x1000
423
message:       times 70  db  ?
411
message:       times 70  db  ?
424
process_info:  times 256 dd ?
412
process_info:  times 256 dd ?