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-win.c
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 <windows.h>
20
21#include "acfutils/cursor.h"
22#include "acfutils/helpers.h"
23#include "acfutils/safe_alloc.h"
24
25struct cursor_s {
26 HCURSOR crs;
27};
28
29cursor_t *
30cursor_read_from_file(const char *filename_png)
31{
32 cursor_t *cursor = safe_calloc(1, sizeof (*cursor));
33 char *filename_cur, *extension;
34
35 ASSERT(filename_png != NULL);
36
37 /*
38 * On Windows, we need to grab a cursor file (.cur), so substitute
39 * the path extension in the filename.
40 */
41 filename_cur = safe_calloc(1, strlen(filename_png) + 8);
42 strlcpy(filename_cur, filename_png, strlen(filename_png) + 8);
43 extension = strrchr(filename_cur, '.');
44 if (extension != NULL)
45 strlcpy(extension, ".cur", 8);
46 else
47 strlcpy(&filename_cur[strlen(filename_cur)], ".cur", 8);
48 cursor->crs = LoadCursorFromFileA(filename_cur);
49 if (cursor->crs == NULL) {
50 win_perror(GetLastError(), "Error loading cursor file %s",
51 filename_cur);
52 free(cursor);
53 free(filename_cur);
54 return (NULL);
55 }
56 free(filename_cur);
57
58 return (cursor);
59}
60
61void
62cursor_free(cursor_t *cursor)
63{
64 if (cursor == NULL)
65 return;
66 ASSERT(cursor->crs != NULL);
67 DestroyCursor(cursor->crs);
68 free(cursor);
69}
70
71void
72cursor_make_current(cursor_t *cursor)
73{
74 ASSERT(cursor != NULL);
75 ASSERT(cursor->crs != NULL);
76 SetCursor(cursor->crs);
77}
#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
static void * safe_calloc(size_t nmemb, size_t size)
Definition safe_alloc.h:71