libacfutils
A general purpose library of utility functions designed to make it easier to develop addons for the X-Plane flight simulator.
Loading...
Searching...
No Matches
cursor-mac.m
1/*
2 * CDDL HEADER START
3 *
4 * This file and its contents are supplied under the terms of the
5 * Common Development and Distribution License ("CDDL"), version 1.0.
6 * You may only use this file in accordance with the terms of version
7 * 1.0 of the CDDL.
8 *
9 * A full copy of the text of the CDDL should have accompanied this
10 * source. A copy of the CDDL is also available via the Internet at
11 * http://www.illumos.org/license/CDDL.
12 *
13 * CDDL HEADER END
14*/
15/*
16 * Copyright 2020 Saso Kiselkov. All rights reserved.
17 */
18
19#include <errno.h>
20
21#import <AppKit/NSCursor.h>
22#import <AppKit/NSImage.h>
23
24#include "acfutils/cursor.h"
25#include "acfutils/helpers.h"
26#include "acfutils/safe_alloc.h"
27
28struct cursor_s {
29 NSCursor *crs;
30};
31
32cursor_t *
33cursor_read_from_file(const char *filename_png)
34{
35 cursor_t *cursor = safe_calloc(1, sizeof (*cursor));
36 NSString *path;
37 NSImage *img;
38 NSImageRep *rep;
39 NSPoint p;
40
41 ASSERT(filename_png != NULL);
42 path = [NSString stringWithUTF8String: filename_png];
43 img = [[NSImage alloc] initWithContentsOfFile: path];
44 if (img == nil) {
45 logMsg("Can't open image %s: %s", filename_png,
46 strerror(errno));
47 free(cursor);
48 return (NULL);
49 }
50 rep = [[img representations] objectAtIndex: 0];
51 ASSERT(rep != nil);
52 p = NSMakePoint([rep pixelsWide] / 2, [rep pixelsHigh] / 2);
53 cursor->crs = [[NSCursor alloc] initWithImage: img hotSpot: p];
54 ASSERT(cursor->crs != NULL);
55 [img release];
56
57 return (cursor);
58}
59
60void
61cursor_free(cursor_t *cursor)
62{
63 ASSERT(cursor != NULL);
64 ASSERT(cursor->crs != NULL);
65 [cursor->crs release];
66 free(cursor);
67}
68
69void
70cursor_make_current(cursor_t *cursor)
71{
72 ASSERT(cursor != NULL);
73 ASSERT(cursor->crs != NULL);
74 [cursor->crs set];
75}
#define ASSERT(x)
Definition assert.h:208
void cursor_make_current(cursor_t *cursor)
Definition cursor-lin.c:94
cursor_t * cursor_read_from_file(const char *filename_png)
Definition cursor-lin.c:39
void cursor_free(cursor_t *cursor)
Definition cursor-lin.c:76
#define logMsg(...)
Definition log.h:112
static void * safe_calloc(size_t nmemb, size_t size)
Definition safe_alloc.h:71