summaryrefslogtreecommitdiff
path: root/src/wayland-client.adb
blob: 21d5cc2fed8d7a7a9934e800cd7a37dd5f5a6527 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544


with

    Interfaces.C.Strings;


package body Wayland.Client is


    --  C constructors

    function wl_proxy_create
           (P, I : in System.Address)
        return System.Address
        with Convention => C,
             Import => True;

    function wl_display_connect
           (N : in Interfaces.C.Strings.chars_ptr)
        return System.Address
        with Convention => C,
             Import => True;

    function wl_display_connect_to_fd
           (FD : in Interfaces.C.int)
        return System.Address
        with Convention => C,
             Import => True;

    function wl_display_create_queue
           (D : in System.Address)
        return System.Address
        with Convention => C,
             Import => True;





    --  C destructors

    procedure wl_proxy_destroy
           (P : in System.Address)
        with Convention => C,
             Import => True;

    procedure wl_display_disconnect
           (D : in System.Address)
        with Convention => C,
             Import => True;

    procedure wl_event_queue_destroy
           (E : in System.Address)
        with Convention => C,
             Import => True;





    --  C proxy functions

    function wl_proxy_get_version
           (P : in System.Address)
        return Interfaces.C.Unsigned
        with Convention => C,
             Import => True;

    function wl_proxy_get_version
           (P : in System.Address)
        return Interfaces.C.Unsigned
        with Convention => C,
             Import => True;

    function wl_proxy_get_class
           (P : in System.Address)
        return Interfaces.C.Strings.chars_ptr
        with Convention => C,
             Import => True;

    procedure wl_proxy_set_queue
           (P, E : in System.Address)
        with Convention => C,
             Import => True;





    --  C display functions

    function wl_display_get_fd
           (D : in System.Address)
        return Interfaces.C.int
        with Convention => C,
             Import => True;

    function wl_display_dispatch
           (D : in System.Address)
        return Interfaces.C.int
        with Convention => C,
             Import => True;

    function wl_display_dispatch_pending
           (D : in System.Address)
        return Interfaces.C.int
        with Convention => C,
             Import => True;

    function wl_display_dispatch_queue
           (D, Q : in System.Address)
        return Interfaces.C.int
        with Convention => C,
             Import => True;

    function wl_display_dispatch_queue_pending
           (D, Q : in System.Address)
        return Interfaces.C.int
        with Convention => C,
             Import => True;

    function wl_display_get_error
           (D : in System.Address)
        return Interfaces.C.int
        with Convention => C,
             Import => True;

    function wl_display_get_protocol_error
           (D, P : in System.Address;
            ID   : in Interfaces.C.unsigned)
        return Interfaces.C.unsigned
        with Convention => C,
             Import => True;

    function wl_display_flush
           (D : in System.Address)
        return Interfaces.C.int
        with Convention => C,
             Import => True;

    function wl_display_roundtrip_queue
           (D, Q : in System.Address)
        return Interfaces.C.int
        with Convention => C,
             Import => True;

    function wl_display_roundtrip
           (D : in System.Address)
        return Interfaces.C.int
        with Convention => C,
             Import => True;

    function wl_display_prepare_read
           (D : in System.Address)
        return Interfaces.C.int
        with Convention => C,
             Import => True;

    function wl_display_prepare_read_queue
           (D, Q : in System.Address)
        return Interfaces.C.int
        with Convention => C,
             Import => True;

    function wl_display_read_events
           (D : in System.Address)
        return Interfaces.C.int
        with Convention => C,
             Import => True;





    --  C log handling

    procedure wayland_client_set_log_hook
           (F : in System.Address)
        with Convention => C,
             Import => True;





    --  Ada Destructors

    procedure Finalize
           (This : in out Proxy) is
    begin
        if This.Void_Ptr /= System.Null_Address then
            wl_proxy_destroy (This.Void_Ptr);
            This.Void_Ptr := System.Null_Address;
        end if;
    end Finalize;


    procedure Finalize
           (This : in out Display) is
    begin
        if This.Void_Ptr /= System.Null_Address then
            wl_display_disconnect (This.Void_Ptr);
            This.Void_Ptr := System.Null_Address;
        end if;
    end Finalize;


    procedure Finalize
           (This : in out Event_Queue) is
    begin
        if This.Void_Ptr /= System.Null_Address then
            wl_event_queue_destroy (This.Void_Ptr);
            This.Void_Ptr := System.Null_Address;
        end if;
    end Finalize;





    --  Internal callback hooks

    function Dispatcher_Hook
           (P, V : in System.Address;
            Op   : in Opcode;
            M    : in Message_Signature;
            Args : in Argument_Array)
    is
        This_Proxy : access Proxy'Class :=
            Proxy_Convert.To_Pointer (P);
    begin
        
    end Dispatcher_Hook;


    Current_Log_Function : Log_Function := null;

    procedure Log_Function_Hook
           (Msg  : in Interfaces.C.Strings.chars_ptr;
            Fail : in Interfaces.C.int)
        with Convention => C,
             Export => True
    is
        Text : String := Interfaces.C.Strings.Value (Msg);
    begin
        if Fail /= 0 then
            raise Storage_Error;
        end if;

        if Current_Log_Function /= null then
            Current_Log_Function.all (Text);
        end if;
    end Log_Function_Hook;





    --  Proxy

    function Create
           (Factory      : in Proxy;
            My_Interface : in Protocol_Interface)
        return Proxy
    is
        Ptr : System.Address :=
            wl_proxy_create (Factor.Void_Ptr, My_Interface.Void_Ptr);
    begin
        if Ptr = System.Null_Address then
            raise Storage_Error;
        end if;

        return This : Proxy do
            This.Void_Ptr := Ptr;
            This.Current_Dispatcher := null;
            
        end return;
    end Create;


    procedure Marshal
           (This : in out Proxy;
            Op   : in     Opcode;
            Args : in     Argument_Array);

    function Marshal_Constructor
           (This         : in out Proxy;
            My_Interface : in     Protocol_Interface;
            Op           : in     Opcode;
            Args         : in     Argument_Array)
        return Proxy;

    function Marshal_Constructor
           (This         : in out Proxy;
            My_Interface : in     Protocol_Interface;
            Op           : in     Opcode;
            Args         : in     Argument_Array;
            Version      : in     Version_Number)
        return Proxy;


    procedure Set_Dispatcher
           (This : in out Proxy;
            Func : in     Dispatcher_Function) is
    begin
        This.Current_Dispatcher := Func;
    end Set_Dispatcher;


    function Get_Version
           (This : in Proxy)
        return Version_Number is
    begin
        return Version_Number (wl_proxy_get_version (This.Void_Ptr));
    end Get_Version;


    function Get_ID
           (This : in Proxy)
        return ID_Number is
    begin
        return ID_Number (wl_proxy_get_id (This.Void_Ptr));
    end Get_ID;


    function Get_Interface_Name
           (This : in Proxy)
        return String is
    begin
        return Interfaces.C.Strings.Value (wl_proxy_get_class (This.Void_Ptr));
    end Get_Interfaces_Name;


    procedure Set_Queue
           (This  : in out Proxy;
            Queue : in     Event_Queue_Access) is
    begin
        if Queue = null then
            wl_proxy_set_queue (This.Void_Ptr, System.Null_Address);
        else
            wl_proxy_set_queue (This.Void_Ptr, Queue.Void_Ptr);
        end if;
    end Set_Queue;





    --  Display

    function Connect
           (Name : in String)
        return Display
    is
        Ptr : System.Address :=
            wl_display_connect (Interfaces.C.To_C (Name));
    begin
        if Ptr = System.Null_Address then
            raise Storage_Error;
        end if;

        return This : Display do
            This.Void_Ptr := Ptr;
            This.Current_Dispatcher := null;
        end return;
    end Connect;


    function Connect_To_FD
           (FD : in File_Descriptor)
        return Display
    is
        Ptr : System.Address :=
            wl_display_connect_to_fd (Interfaces.C.int (FD));
    begin
        if Ptr = System.Null_Address then
            raise Storage_Error;
        end if;

        return This : Display do
            This.Void_Ptr := Ptr;
            This.Current_Dispatcher := null;
        end return;
    end Connect_To_FD;


    function Get_FD
           (This : in Display)
        return File_Descriptor is
    begin
        return File_Descriptor (wl_display_get_fd (This.Void_Ptr));
    end Get_FD;


    function Dispatch
           (This : in out Display)
        return Num_Events_Dispatched is
    begin
        return Num_Events_Dispatched (wl_display_dispatch (This.Void_Ptr));
    end Dispatch;


    function Dispatch_Pending
           (This : in out Display)
        return Num_Events_Dispatched is
    begin
        return Num_Events_Dispatched (wl_display_dispatch_pending (This.Void_Ptr));
    end Dispatch_Pending;


    function Dispatch_Queue
           (This  : in out Display;
            Queue : in out Event_Queue)
        return Num_Events_Dispatched is
    begin
        return Num_Events_Dispatched
            (wl_display_dispatch_queue (This.Void_Ptr, Queue.Void_Ptr));
    end Dispatch_Queue;


    function Dispatch_Queue_Pending
           (This  : in out Display;
            Queue : in out Event_Queue)
        return Num_Events_Dispatched is
    begin
        return Num_Events_Dispatched
            (wl_display_dispatch_queue_pending (This.Void_Ptr, Queue.Void_Ptr));
    end Dispatch_Queue_Pending;


    function Get_Error
           (This : in out Display)
        return Error_Value is
    begin
        return Error_Value (wl_display_get_error (This.Void_Ptr));
    end Get_Error;


    function Get_Protocol_Error
           (This         : in Display;
            My_Interface : in Protocol_Interface_Access;
            ID           : in ID_Number)
        return Error_Value is
    begin
        return Error_Value
            (wl_display_get_protocol_error
                (This.Void_Ptr, My_Interfaces.Void_Ptr, Interfaces.C.unsigned (ID)));
    end Get_Protocol_Error;


    function Flush
           (This : in out Display)
        return Num_Bytes_Sent is
    begin
        return Num_Bytes_Sent (wl_display_flush (This.Void_Ptr));
    end Flush;


    function Roundtrip_Queue
           (This  : in out Display;
            Queue : in out Event_Queue)
        return Num_Events_Dispatched is
    begin
        return Num_Events_Dispatched
            (wl_display_roundtrip_queue (This.Void_Ptr, Queue.Void_Ptr));
    end Roundtrip_Queue;


    function Roundtrip
           (This : in out Display)
        return Num_Events_Dispatched is
    begin
        return Num_Events_Dispatched (wl_display_roundtrip (This.Void_Ptr));
    end Roundtrip;


    procedure Read_Events
           (This  : in out Display) is
    begin
        if  wl_display_prepare_read (This.Void_Ptr) /= 0 or else
            wl_display_read_events (This.Void_Ptr) /= 0
        then
            raise Wayland_Error;
        end if;
    end Read_Events;


    procedure Read_Events
           (This  : in out Display;
            Queue : in out Event_Queue) is
    begin
        if  wl_display_prepare_read_queue (This.Void_Ptr, Queue.Void_Ptr) /= 0 or else
            wl_display_read_events (This.Void_Ptr) /= 0
        then
            raise Wayland_Error;
        end if;
    end Read_Events;





    --  Event_Queue

    function Create_Queue
           (This : in out Display)
        return Event_Queue
    is
        Ptr : System.Address :=
            wl_display_create_queue (This.Void_Ptr);
    begin
        if Ptr = System.Null_Address then
            raise Storage_Error;
        end if;

        return Queue : Event_Queue do
            Queue.Void_Ptr := Ptr;
        end return;
    end Create_Queue;





    --  Log

    procedure Set_Log_Handler
           (Handler : in Log_Function) is
    begin
        Current_Log_Function := Handler;
    end Set_Log_Handler;




begin


    wayland_client_set_log_hook (Log_Function_Hook'Access);


end Wayland.Client;