-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
139 lines (120 loc) · 4.08 KB
/
main.c
File metadata and controls
139 lines (120 loc) · 4.08 KB
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
#include "common.h"
#include "logging.h"
#include "network.h"
#include "ui.h"
#include "utils.h"
#include <stdlib.h>
#include <string.h>
#include <strings.h>
// no global variables needed, we will initialize in main.
int main(void) {
Logger_Init("steganet.log");
SetTraceLogLevel(LOG_WARNING);
AppState appState = {0};
AppState *state = &appState;
// Set some defaults
strcpy(state->serverIPBuffer, "127.0.0.1");
strcpy(state->portBuffer, "8888");
state->showConnectionDialog = true;
strcpy(state->ytUrlBuffer, "https://www.youtube.com/");
state->messageMutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
SetConfigFlags(FLAG_MSAA_4X_HINT | FLAG_VSYNC_HINT);
InitWindow(800, 600, "StegaChat - Secure Messaging with Steganography");
if (!IsAudioDeviceReady()) {
InitAudioDevice();
}
// Generate short notification beep
Wave beepWave = {0};
beepWave.frameCount = 44100 / 10;
beepWave.sampleRate = 44100;
beepWave.sampleSize = 16;
beepWave.channels = 1;
short *beepData = (short *)malloc(beepWave.frameCount * sizeof(short));
for (unsigned int i = 0; i < beepWave.frameCount; i++) {
beepData[i] =
(short)(8000.0f * ((i % 100) < 50 ? 1 : -1)); // simple square wave
}
beepWave.data = beepData;
state->alertSound = LoadSoundFromWave(beepWave);
UnloadWave(beepWave);
SetTargetFPS(60);
while (!WindowShouldClose() && !state->shouldExit) {
if (state->statusTimer > 0)
state->statusTimer -= GetFrameTime();
if (state->connection.isConnected) {
if (GetTime() - state->connection.lastPingSent > 2.0f) {
SendPing(state);
}
}
bool shouldSend = false;
if (IsKeyPressed(KEY_ENTER)) {
if (!state->inputEditMode)
shouldSend = true;
else if (IsKeyDown(KEY_LEFT_CONTROL) || IsKeyDown(KEY_RIGHT_CONTROL))
shouldSend = true;
}
if (shouldSend && !state->showConnectionDialog &&
state->connection.isConnected) {
if (state->selectedMessageType == MSG_TEXT &&
strlen(state->inputBuffer) > 0) {
SendMessage(state, state->inputBuffer, MSG_TEXT);
state->inputBuffer[0] = '\0';
state->inputEditMode = false;
}
}
if (IsFileDropped()) {
FilePathList droppedFiles = LoadDroppedFiles();
if (droppedFiles.count > 0) {
strncpy(state->selectedFilePath, droppedFiles.paths[0],
sizeof(state->selectedFilePath) - 1);
state->selectedFilePath[sizeof(state->selectedFilePath) - 1] = '\0';
char *ext = strrchr(droppedFiles.paths[0], '.');
if (ext) {
if (strcasecmp(ext, ".png") == 0 || strcasecmp(ext, ".jpg") == 0 ||
strcasecmp(ext, ".jpeg") == 0)
state->selectedMessageType = MSG_IMAGE;
else if (strcasecmp(ext, ".wav") == 0 ||
strcasecmp(ext, ".mp3") == 0 || strcasecmp(ext, ".ogg") == 0)
state->selectedMessageType = MSG_AUDIO;
else
state->selectedMessageType = MSG_FILE_CHUNK;
}
}
UnloadDroppedFiles(droppedFiles);
}
if (IsKeyPressed(KEY_ESCAPE)) {
if (state->showEncodingOptions) {
state->showEncodingOptions = false;
} else if (!state->showConnectionDialog &&
state->connection.isConnected) {
CloseConnection(state);
state->showConnectionDialog = true;
}
}
BeginDrawing();
ClearBackground(RAYWHITE);
if (state->showConnectionDialog) {
DrawConnectionDialog(state);
} else if (state->connection.isConnected || state->messageCount > 0) {
DrawChat(state);
} else {
state->showConnectionDialog = true;
}
EndDrawing();
}
for (int i = 0; i < MAX_MESSAGES; i++) {
if (state->messageImageLoaded[i])
UnloadTexture(state->messageImages[i]);
if (state->messageAudioLoaded[i])
UnloadSound(state->messageAudioSounds[i]);
}
CloseConnection(state);
pthread_mutex_destroy(&state->messageMutex);
if (state->imageLoaded)
UnloadTexture(state->currentImageTexture);
if (IsAudioDeviceReady())
CloseAudioDevice();
CloseWindow();
Logger_Close();
return 0;
}