cat_engine_basement 0.0.0-alpha7

The CatEnigne's basement
Documentation
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
use crate::windows::{
    WinCore,
    core::InstanceHandle,
    core::cursor::{
        CursorInfo,
        CursorHandle,
        SystemCursor,
        SystemCursorId,
    },
};

use super::error::Error;

use core::{
    mem::{
        transmute,
        transmute_copy,
        size_of,
    },
    ptr::{
        null_mut,
        NonNull
    },
};


/// Before closing, an application must call the `Cursor::destroy` function
/// to free any system resources associated with the cursor.
#[repr(transparent)]
pub struct Cursor{
    handle:CursorHandle,
}

impl Cursor{
    /// Creates a cursor having the specified size, bit patterns, and hot spot.
    /// 
    /// `and_plane` - An array of bytes that contains the bit values for the AND mask of the cursor,
    /// as in a device-dependent monochrome bitmap.
    /// 
    /// `xor_plane` - An array of bytes that contains the bit values for the XOR mask of the cursor,
    /// as in a device-dependent monochrome bitmap.
    /// 
    /// The `width` and `height` parameters must specify a width and height
    /// that are supported by the current display driver,
    /// because the system cannot create cursors of other sizes.
    /// To determine the width and height supported by the display driver,
    /// use the `GetSystemMetrics` function, specifying the `SM_CXCURSOR` or `SM_CYCURSOR` value.
    /// 
    /// This API does not participate in DPI virtualization.
    /// The output returned is in terms of physical coordinates,
    /// and is not affected by the DPI of the calling thread.
    /// Note that the cursor created may still be scaled
    /// to match the DPI of any given window it is drawn into.
    #[inline(always)]
    pub fn create(
        &self,
        instance:Option<InstanceHandle>,
        [x,y,width,height]:[i32;4],
        and_plane:*const u8,
        xor_plane:*const u8,
    )->Result<Cursor,Error>{
        unsafe{
            if let Some(cursor)=WinCore.cursor.create(
                instance,
                [x,y,width,height],
                and_plane,
                xor_plane
            ){
                Ok(
                    Self{
                        handle:cursor
                    }
                )
            }
            else{
                Err(Error::get_last_error())
            }
        }
    }

    /// Copies the specified cursor.
    /// 
    /// `cursor` - A handle to the cursor to be copied.
    /// 
    /// Enables an application or DLL to obtain the handle to a cursor shape owned by another module.
    /// Then if the other module is freed, the application is still able to use the cursor shape.
    /// 
    /// Do not use the `Cursor::copy` function for animated cursors.
    /// Instead, use the `CopyImage` function.
    /// 
    /// `Cursor::copy` is implemented as a call to the `Icon::copy` function.
    pub fn copy(&self)->Result<Cursor,Error>{
        unsafe{
            if let Some(cursor)=WinCore.cursor.copy(self.handle){
                Ok(
                    Self{
                        handle:cursor
                    }
                )
            }
            else{
                Err(Error::get_last_error())
            }
        }
    }

    /// Destroys a cursor and frees any memory the cursor occupied.
    /// Do not use this function to destroy a shared cursor.
    /// 
    /// The cursor must not be in use.
    /// 
    /// The `Cursor::destroy` function destroys a nonshared cursor.
    /// Do not use this function to destroy a shared cursor.
    /// A shared cursor is valid as long as the module from which it was loaded remains in memory.
    /// The following functions obtain a shared cursor:
    /// - `Cursor::load`
    /// - `Cursor::load_from_file`
    /// - `LoadImage` (if you use the `LR_SHARED` flag)
    /// - `CopyImage` (if you use the `LR_COPYRETURNORG` flag and the `hImage` parameter is a shared cursor)
    #[inline(always)]
    pub fn destroy(&self)->Result<(),Error>{
        unsafe{
            if WinCore.cursor.destroy(self.handle){
                Ok(())
            }
            else{
                Err(Error::get_last_error())
            }
        }
    }

    pub const fn handle(&self)->CursorHandle{
        self.handle
    }
}

impl Cursor{
    /// Loads the specified cursor resource from the executable (.EXE) file associated with an application instance.
    /// 
    /// `instance` - A handle to an instance of the module
    /// whose executable file contains the cursor to be loaded.
    /// 
    /// `name` - The name of the cursor resource to be loaded.
    /// 
    /// The `Cursor::load` function loads the cursor resource
    /// only if it has not been loaded; otherwise,
    /// it retrieves the handle to the existing resource.
    /// This function returns a valid cursor handle
    /// only if the `name` parameter is a pointer to a cursor resource.
    /// If `name` is a pointer to any type of resource other than a cursor (such as an icon),
    /// the return value is not `None`, even though it is not a valid cursor handle.
    /// 
    /// The `Cursor::load` function searches the cursor resource most appropriate for the cursor for the current display device.
    /// The cursor resource can be a color or monochrome bitmap.
    /// 
    /// This API does not participate in DPI virtualization.
    /// The output returned is not affected by the DPI of the calling thread.
    /// 
    /// This function has been superseded by the `LoadImage` function.
    #[inline(always)]
    pub fn load(instance:Option<InstanceHandle>,name:*const u16)->Result<Cursor,Error>{
        unsafe{
            if let Some(cursor)=WinCore.cursor.load(instance,name){
                Ok(
                    Self{
                        handle:cursor
                    }
                )
            }
            else{
                Err(Error::get_last_error())
            }
        }
    }

    /// Same as `Cursor::load` but for the ANSI encoding.
    #[inline(always)]
    pub fn load_ansi(instance:Option<InstanceHandle>,name:&str)->Result<Cursor,Error>{
        unsafe{
            if let Some(cursor)=WinCore.cursor.load_ansi(instance,name){
                Ok(
                    Self{
                        handle:cursor
                    }
                )
            }
            else{
                Err(Error::get_last_error())
            }
        }
    }

    /// Same as `Cursor::load` but for the system cursors.
    #[inline(always)]
    pub fn load_system_cursor(name:SystemCursor)->Result<Cursor,Error>{
        unsafe{
            if let Some(cursor)=WinCore.cursor.load_system_cursor(name){
                Ok(
                    Self{
                        handle:cursor
                    }
                )
            }
            else{
                Err(Error::get_last_error())
            }
        }
    }

    /// Creates a cursor based on data contained in a file.
    /// 
    /// `name` - The source of the file data to be used to create the cursor.
    /// The data in the file must be in either .CUR or .ANI format.
    /// If the high-order word of `name` is nonzero,
    /// it is a pointer to a string that is a fully qualified name of a file containing cursor data.
    /// 
    /// This API does not participate in DPI virtualization.
    /// The output returned is not affected by the DPI of the calling thread.
    #[inline(always)]
    pub fn load_from_file(name:*const u16)->Result<Cursor,Error>{
        unsafe{
            if let Some(cursor)=WinCore.cursor.load_from_file(name){
                Ok(
                    Self{
                        handle:cursor
                    }
                )
            }
            else{
                Err(Error::get_last_error())
            }
        }
    }

    /// Same as `Cursor::load_from_file` but for the ANSI encoding.
    #[inline(always)]
    pub fn load_from_file_ansi(name:*const u8)->Result<Cursor,Error>{
        unsafe{
            if let Some(cursor)=WinCore.cursor.load_from_file_ansi(name){
                Ok(
                    Self{
                        handle:cursor
                    }
                )
            }
            else{
                Err(Error::get_last_error())
            }
        }
    }
}

impl Cursor{
    /// Sets the cursor shape.
    /// 
    /// `cursor` - A handle to the cursor.
    /// The cursor must have been created by the `Cursor::create` function
    /// or loaded by the `Cursor::load` or `LoadImage` function.
    /// If this parameter is `None`, the cursor is removed from the screen.
    /// 
    /// The return value is the handle to the previous cursor, if there was one.
    /// If there was no previous cursor, the return value is `None`.
    /// 
    /// The cursor is set only if the new cursor is different from the previous cursor;
    /// otherwise, the function returns immediately.
    /// 
    /// The cursor is a shared resource.
    /// A window should set the cursor shape only
    /// when the cursor is in its client area
    /// or when the window is capturing mouse input.
    /// In systems without a mouse, the window should restore the previous cursor
    /// before the cursor leaves the client area
    /// or before it relinquishes control to another window.
    /// 
    /// If your application must set the cursor while it is in a window,
    /// make sure the class cursor for the specified window's class is set to `None`.
    /// If the class cursor is not `None`,
    /// the system restores the class cursor each time the mouse is moved.
    /// 
    /// The cursor is not shown on the screen
    /// if the internal cursor display count is less than zero.
    /// This occurs if the application uses the `Cursor::show` function
    /// to hide the cursor more timesthan to show the cursor.
    #[inline(always)]
    pub fn set(cursor:Option<CursorHandle>)->Result<Cursor,Error>{
        unsafe{
            if let Some(cursor)=WinCore.cursor.set(cursor){
                Ok(
                    Self{
                        handle:cursor
                    }
                )
            }
            else{
                Err(Error::get_last_error())
            }
        }
    }

    /// Enables an application to customize the system cursors.
    /// It replaces the contents of the system cursor specified by the id parameter
    /// with the contents of the cursor specified by the `cursor` parameter and then destroys `cursor`.
    /// 
    /// `cursor` - A handle to the cursor.
    /// The function replaces the contents of the system cursor specified
    /// by id with the contents of the cursor handled by `cursor`.
    /// The system destroys `cursor` by calling the `Cursor::destroy` function.
    /// Therefore, `cursor` cannot be a cursor loaded using the `Cursor::load` function.
    /// To specify a cursor loaded from a resource, copy the cursor using the `Cursor::copy` function,
    /// then pass the copy to `Cursor::set_system`.
    /// 
    /// `id` - The system cursor to replace with the contents of `cursor`.
    /// This parameter can be one of the following values.
    #[inline(always)]
    pub fn set_system(cursor:Option<CursorHandle>,id:SystemCursorId)->Result<(),Error>{
        unsafe{
            if WinCore.cursor.set_system(cursor,id){
                Ok(())
            }
            else{
                Err(Error::get_last_error())
            }
        }
    }

    /// Retrieves a handle to the current cursor.
    /// 
    /// To get information on the global cursor,
    /// even if it is not owned by the current thread, use `Cursor::get_info`.
    #[inline(always)]
    pub fn get()->Result<Cursor,Error>{
        unsafe{
            if let Some(cursor)=WinCore.cursor.get(){
                Ok(
                    Self{
                        handle:cursor
                    }
                )
            }
            else{
                Err(Error::get_last_error())
            }
        }
    }

    /// Retrieves information about the global cursor.
    /// 
    /// If the function succeeds, the return value is `true`.
    #[inline(always)]
    pub fn get_info(info:&mut CursorInfo)->Result<(),Error>{
        unsafe{
            if WinCore.cursor.get_info(info){
                Ok(())
            }
            else{
                Err(Error::get_last_error())
            }
        }
    }

    /// Displays or hides the cursor.
    /// 
    /// This function sets an internal display counter that determines
    /// whether the cursor should be displayed.
    /// The cursor is displayed only if the display count is greater than or equal to 0.
    /// 
    /// If a mouse is installed, the initial display count is 0.
    /// If no mouse is installed, the display count is –1.
    #[inline(always)]
    pub fn show(show:bool)->i32{
        unsafe{
            WinCore.cursor.show(show)
        }
    }
}

impl Cursor{
    /// Confines the cursor to a rectangular area on the screen.
    /// If a subsequent cursor position (set by the `Cursor::set_position` function or the mouse) lies outside the rectangle,
    /// the system automatically adjusts the position to keep the cursor inside the rectangular area.
    /// 
    /// `rect` - A reference to the structure
    /// that contains the screen coordinates of the upper-left and lower-right corners of the confining rectangle.
    /// If this parameter is `None`, the cursor is free to move anywhere on the screen.
    /// 
    /// The cursor is a shared resource.
    /// If an application confines the cursor,
    /// it must release the cursor by using `Cursor::clip` before relinquishing control to another application.
    /// 
    /// The calling process must have `WINSTA_WRITEATTRIBUTES` access to the window station.
    #[inline(always)]
    pub fn clip(rect:Option<&[i32;4]>)->Result<(),Error>{
        unsafe{
            if WinCore.cursor.clip(rect){
                Ok(())
            }
            else{
                Err(Error::get_last_error())
            }
        }
    }

    /// Retrieves the screen coordinates of the rectangular area to which the cursor is confined.
    /// 
    /// `rect` receives the screen coordinates of the confining rectangle,
    /// receives the dimensions of the screen if the cursor is not confined to a rectangle.
    /// 
    /// If the function succeeds, the return value is `true`.
    /// 
    /// The cursor is a shared resource.
    /// If an application confines the cursor with the `Cursor::clip` function,
    /// it must later release the cursor by using `Cursor::clip`
    /// before relinquishing control to another application.
    /// 
    /// The calling process must have `WINSTA_READATTRIBUTES` access to the window station.
    #[inline(always)]
    pub fn get_clip(rect:&mut [i32;4])->Result<(),Error>{
        unsafe{
            if WinCore.cursor.get_clip(rect){
                Ok(())
            }
            else{
                Err(Error::get_last_error())
            }
        }
    }
}


impl Cursor{
    /// Retrieves the position of the mouse cursor, in screen coordinates
    /// and writes it to `point`.
    /// 
    /// The cursor position is always specified in screen coordinates
    /// and is not affected by the mapping mode of the window that contains the cursor.
    /// 
    /// The calling process must have `WINSTA_READATTRIBUTES` access to the window station.
    /// 
    /// The input desktop must be the current desktop when you call `Cursor::get_position`.
    /// Call `OpenInputDesktop` to determine whether the current desktop is the input desktop.
    /// If it is not, call `SetThreadDesktop` with the `HDESK`
    /// returned by `OpenInputDesktop` to switch to that desktop.
    #[inline(always)]
    pub fn get_position(point:&mut [i32;2])->Result<(),Error>{
        unsafe{
            if WinCore.cursor.get_position(point){
                Ok(())
            }
            else{
                Err(Error::get_last_error())
            }
        }
    }

    /// Moves the cursor to the specified screen coordinates.
    /// If the new coordinates are not within the screen rectangle set
    /// by the most recent `Cursor::clip` function call,
    /// the system automatically adjusts the coordinates so that the cursor stays within the rectangle.
    /// 
    /// The cursor is a shared resource.
    /// A window should move the cursor only when the cursor is in the window's client area.
    /// 
    /// The calling process must have `WINSTA_WRITEATTRIBUTES` access to the window station.
    /// 
    /// The input desktop must be the current desktop when you call `Cursor::set_position`.
    /// Call `OpenInputDesktop` to determine whether the current desktop is the input desktop.
    /// If it is not, call `SetThreadDesktop` with the `HDESK`
    /// returned by `OpenInputDesktop` to switch to that desktop.
    #[inline(always)]
    pub fn set_position([x,y]:[i32;2])->Result<(),Error>{
        unsafe{
            if WinCore.cursor.set_position([x,y]){
                Ok(())
            }
            else{
                Err(Error::get_last_error())
            }
        }
    }

    /// Sets the position of the cursor in physical coordinates.
    /// 
    /// For a description of the difference
    /// between logical coordinates and physical coordinates, see `PhysicalToLogicalPoint`.
    #[inline(always)]
    pub fn set_physical_position([x,y]:[i32;2])->Result<(),Error>{
        unsafe{
            if WinCore.cursor.set_physical_position([x,y]){
                Ok(())
            }
            else{
                Err(Error::get_last_error())
            }
        }
    }

    /// Retrieves the position of the cursor in physical coordinates.
    /// 
    /// For a description of the difference
    /// between logical coordinates and physical coordinates, see `PhysicalToLogicalPoint`.
    #[inline(always)]
    pub fn get_physical_position(point:&mut [i32;2])->Result<(),Error>{
        unsafe{
            if WinCore.cursor.get_physical_position(point){
                Ok(())
            }
            else{
                Err(Error::get_last_error())
            }
        }
    }
}