Debian Codegraph @

helpers/g15helper/g15helper.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 Win32.
8 */
9
10 #include <windows.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <time.h>
14 #include <wchar.h>
15
16 #include "g15helper.h"
17 #include "lglcd.h"
18
19 static void __cdecl ods(const char *fmt, va_list args) {
20 char buffer[2048];
21 _vsnprintf_s(buffer, 2048, _TRUNCATE, fmt, args);
22 OutputDebugStringA(buffer);
23 }
24
25 static void __cdecl warn(const char *fmt, ...) {
26 va_list args;
27 va_start(args, fmt);
28 ods(fmt, args);
29 va_end(args);
30 }
31
32 static void __cdecl die(int err, const char *fmt, ...) {
33 va_list args;
34 va_start(args, fmt);
35 ods(fmt, args);
36 va_end(args);
37 exit(err);
38 }
39
40 int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
41 DWORD dwErr;
42 BOOL bErr;
43 BOOL bDetect = FALSE;
44 HANDLE hStdin, hStdout;
45 lgLcdConnectContextEx conn;
46 lgLcdOpenByTypeContext ctx;
47 lgLcdBitmap160x43x1 bitmap;
48
49 warn("Args: %s", lpCmdLine);
50
51 if (lpCmdLine && (strcmp(lpCmdLine, "/detect") == 0)) {
52 warn("Detect mode!");
53 bDetect = TRUE;
54 } else if (!lpCmdLine || (strcmp(lpCmdLine, "/mumble") != 0)) {
55 MessageBox(NULL, L"This program is run by Mumble, and should not be started separately.",
56 L"Nothing to see here, move along", MB_OK | MB_ICONERROR);
57 return 0;
58 }
59
60 hStdin = GetStdHandle(STD_INPUT_HANDLE);
61 if (hStdin == INVALID_HANDLE_VALUE)
62 die(G15_ERR_INIT, "Failed to get standard input");
63
64 hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
65 if (hStdout == INVALID_HANDLE_VALUE)
66 die(G15_ERR_INIT, "Failed to get standard output");
67
68 /*
69 * Clear and set up initial structures.
70 */
71 ZeroMemory(&conn, sizeof(conn));
72 ZeroMemory(&ctx, sizeof(ctx));
73 ZeroMemory(&bitmap, sizeof(bitmap));
74
75 conn.appFriendlyName = G15_WIDGET_NAME;
76 conn.isAutostartable = FALSE;
77 conn.isPersistent = FALSE;
78 conn.dwAppletCapabilitiesSupported = LGLCD_APPLET_CAP_BASIC | LGLCD_APPLET_CAP_BW;
79 conn.connection = LGLCD_INVALID_CONNECTION;
80
81 /*
82 * Initialize and connect.
83 */
84 dwErr = lgLcdInit();
85 if (dwErr != ERROR_SUCCESS)
86 die(G15_ERR_INIT, "Unable to initialize Logitech LCD library. (Error: %i)", dwErr);
87
88 dwErr = lgLcdConnectEx(&conn);
89 if (dwErr != ERROR_SUCCESS)
90 die(G15_ERR_CONNECT, "Unable to connect to Logitech LCD manager. (Error: %i)", dwErr);
91
92 ctx.connection = conn.connection;
93 ctx.device = LGLCD_INVALID_DEVICE;
94 ctx.deviceType = LGLCD_DEVICE_BW;
95
96 dwErr = lgLcdOpenByType(&ctx);
97
98 warn("That returned %d %d", dwErr, ERROR_SUCCESS);
99
100 if (bDetect)
101 return (dwErr != ERROR_SUCCESS);
102 else if (dwErr != ERROR_SUCCESS)
103 die(G15_ERR_OPEN, "Unable to open device. (Error: %i)", dwErr);
104
105 /*
106 * Diplay buffer format.
107 */
108 bitmap.hdr.Format = LGLCD_BMP_FORMAT_160x43x1;
109
110 /*
111 * Main drawing loop.
112 */
113 while (1) {
114 DWORD dwRead;
115 DWORD dwTotRead = 0;
116 BYTE bPriority;
117
118 dwErr = WaitForSingleObject(hStdin, 2000);
119 if (dwErr != WAIT_OBJECT_0) {
120 die(0, "Unable to wait for object.");
121 }
122
123 bErr = ReadFile(hStdin, &bPriority, 1, &dwRead, NULL);
124 if ((bErr == FALSE) || (dwRead != 1))
125 die(G15_ERR_READFILE, "Error while reading priority.");
126
127 do {
128 dwErr = WaitForSingleObject(hStdin, 2000);
129 if (dwErr != WAIT_OBJECT_0) {
130 die(0, "Unable to wait for object.");
131 }
132 bErr = ReadFile(hStdin, bitmap.pixels + dwTotRead, G15_MAX_FBMEM - dwTotRead, &dwRead, NULL);
133 if (bErr == FALSE || dwRead == 0)
134 die(G15_ERR_READFILE, "Error while reading framebuffer. %d %x", dwRead, GetLastError());
135 dwTotRead += dwRead;
136 } while (dwTotRead < G15_MAX_FBMEM);
137
138 dwErr = lgLcdUpdateBitmap(ctx.device, (const lgLcdBitmapHeader *) &bitmap,
139 bPriority ? LGLCD_SYNC_UPDATE(LGLCD_PRIORITY_ALERT)
140 : LGLCD_SYNC_UPDATE(LGLCD_PRIORITY_NORMAL));
141 if (dwErr != ERROR_SUCCESS)
142 warn("Unable to update bitmap for device. (Error: %i)", dwErr);
143 }
144
145 /*
146 * Close device connections.
147 */
148 dwErr = lgLcdClose(ctx.device);
149 if (dwErr != ERROR_SUCCESS)
150 die(G15_ERR_CLOSE, "Unable to close LCD device. (Error: %i)", dwErr);
151
152 /*
153 * Disconnect from LCD monitor.
154 */
155 dwErr = lgLcdDisconnect(conn.connection);
156 if (dwErr != ERROR_SUCCESS)
157 die(G15_ERR_DISCONNECT, "Unable to disconnect from LCD manager. (Error: %i)", dwErr);
158
159 /*
160 * Deinitialize G15 library.
161 */
162 dwErr = lgLcdDeInit();
163 if (dwErr != ERROR_SUCCESS)
164 die(G15_ERR_DEINIT, "Unable to deinitialize LCD library. (Error: %i)", dwErr);
165
166 warn("Terminated successfully.");
167
168 return 0;
169 }