Debian Codegraph @

helpers/g15helper/g15helper_macx.c

raw symbols SCIP
1 // Copyright 2022-2023 The Mumble Developers. All rights reserved.
2 // Use of this source code is governed by a BSD-style license
3 // that can be found in the LICENSE file at the root of the
4 // Mumble source tree or at <https://www.mumble.info/LICENSE>.
5
6 /*
7 * G15 Helper Daemon for Mac OS X.
8 */
9
10 #include <CoreFoundation/CoreFoundation.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <time.h>
14
15 #include "g15helper.h"
16 #include "lglcd.h"
17
18 #define ERROR_SUCCESS 0
19 #define BOOL unsigned char
20 #define BYTE unsigned char
21 #define TRUE 1
22 #define FALSE 0
23
24 static void ods(const char *fmt, va_list args) {
25 vfprintf(stderr, fmt, args);
26 }
27
28 static void warn(const char *fmt, ...) {
29 va_list args;
30 va_start(args, fmt);
31 ods(fmt, args);
32 va_end(args);
33 }
34
35 static void die(int err, const char *fmt, ...) {
36 va_list args;
37 va_start(args, fmt);
38 ods(fmt, args);
39 va_end(args);
40 exit(err);
41 }
42
43 int main(int argc, char *argv[]) {
44 int dwErr;
45 BOOL bDetect = FALSE;
46 int i;
47 lgLcdConnectContextEx conn;
48 lgLcdOpenByTypeContext ctx;
49 lgLcdBitmap160x43x1 bitmap;
50
51 if (argc > 1 && (strcmp(argv[1], "/detect") == 0)) {
52 warn("Detect mode!");
53 bDetect = TRUE;
54 } else if (!(argc > 1) || (strcmp(argv[1], "/mumble") != 0)) {
55 CFUserNotificationDisplayAlert(0, 0, NULL, NULL, NULL, CFSTR("Nothing to see here"),
56 CFSTR("This program is run by Mumble, and should not be started separately."),
57 CFSTR("OK"), NULL, NULL, NULL);
58 return 0;
59 }
60
61 /*
62 * Clear and set up initial structures.
63 */
64 memset(&conn, 0, sizeof(conn));
65 memset(&ctx, 0, sizeof(ctx));
66 memset(&bitmap, 0, sizeof(bitmap));
67
68 conn.appFriendlyName = G15_WIDGET_NAME;
69 conn.isAutostartable = FALSE;
70 conn.isPersistent = FALSE;
71 conn.dwAppletCapabilitiesSupported = LGLCD_APPLET_CAP_BASIC | LGLCD_APPLET_CAP_BW;
72 conn.connection = LGLCD_INVALID_CONNECTION;
73
74 /*
75 * Initialize and connect.
76 */
77 dwErr = lgLcdInit();
78 if (dwErr != ERROR_SUCCESS)
79 die(G15_ERR_INIT, "Unable to initialize Logitech LCD library. (Error: %i)", dwErr);
80
81 dwErr = lgLcdConnectEx(&conn);
82 if (dwErr != ERROR_SUCCESS)
83 die(G15_ERR_CONNECT, "Unable to connect to Logitech LCD manager. (Error: %i)", dwErr);
84
85 ctx.connection = conn.connection;
86 ctx.device = LGLCD_INVALID_DEVICE;
87 ctx.deviceType = LGLCD_DEVICE_BW;
88
89 dwErr = lgLcdOpenByType(&ctx);
90
91 warn("That returned %d %d", dwErr, ERROR_SUCCESS);
92
93 if (bDetect)
94 return (dwErr != ERROR_SUCCESS);
95 else if (dwErr != ERROR_SUCCESS)
96 die(G15_ERR_OPEN, "Unable to open device. (Error: %i)", dwErr);
97
98 /*
99 * Diplay buffer format.
100 */
101 bitmap.hdr.Format = LGLCD_BMP_FORMAT_160x43x1;
102
103 /*
104 * Main drawing loop.
105 */
106 while (1) {
107 int ret;
108 int remain = 0;
109 BYTE bPriority;
110
111 ret = read(0, &bPriority, 1);
112 if (ret == -1 || ret != 1)
113 die(G15_ERR_READFILE, "Error while reading priority.");
114
115 do {
116 ret = read(0, bitmap.pixels + remain, G15_MAX_FBMEM - remain);
117 if (ret < 1)
118 die(G15_ERR_READFILE, "Error while reading framebuffer. %d (%s)", ret, strerror(errno));
119 remain += ret;
120 } while (remain < G15_MAX_FBMEM);
121
122 dwErr = lgLcdUpdateBitmap(ctx.device, (const lgLcdBitmapHeader *) &bitmap,
123 bPriority ? LGLCD_SYNC_UPDATE(LGLCD_PRIORITY_ALERT)
124 : LGLCD_SYNC_UPDATE(LGLCD_PRIORITY_NORMAL));
125 if (dwErr != ERROR_SUCCESS)
126 warn("Unable to update bitmap for device #%i successfully. (Error: %i)", i, dwErr);
127 }
128
129 /*
130 * Close device connections.
131 */
132 dwErr = lgLcdClose(ctx.device);
133 if (dwErr != ERROR_SUCCESS)
134 die(G15_ERR_CLOSE, "Unable to close LCD device. (Error: %i)", dwErr);
135
136 /*
137 * Disconnect from LCD monitor.
138 */
139 dwErr = lgLcdDisconnect(conn.connection);
140 if (dwErr != ERROR_SUCCESS)
141 die(G15_ERR_DISCONNECT, "Unable to disconnect from LCD manager. (Error: %i)", dwErr);
142
143 /*
144 * Deinitialize G15 library.
145 */
146 dwErr = lgLcdDeInit();
147 if (dwErr != ERROR_SUCCESS)
148 die(G15_ERR_DEINIT, "Unable to deinitialize LCD library. (Error: %i)", dwErr);
149
150 warn("Terminated successfully.");
151
152 return 0;
153 }