minecraft-pi-reborn/mods/src/chat/ui.c

78 lines
2.0 KiB
C
Raw Normal View History

2021-09-12 03:18:12 +00:00
#ifndef MCPI_SERVER_MODE
2021-02-16 17:26:40 +00:00
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <libreborn/libreborn.h>
#include "chat.h"
2021-07-04 23:02:45 +00:00
#include "../input/input.h"
2021-02-16 17:26:40 +00:00
// Run Command
2022-03-14 23:09:25 +00:00
static char *run_command_proper(const char *command[], int *return_code) {
2021-06-17 21:32:24 +00:00
// Prepare Environment
RESET_ENVIRONMENTAL_VARIABLE("LD_LIBRARY_PATH");
RESET_ENVIRONMENTAL_VARIABLE("LD_PRELOAD");
2021-02-16 17:26:40 +00:00
2022-03-14 23:09:25 +00:00
// Run
return run_command(command, return_code);
2021-02-16 17:26:40 +00:00
}
2021-02-16 22:08:43 +00:00
// Count Chat Windows
static pthread_mutex_t chat_counter_lock = PTHREAD_MUTEX_INITIALIZER;
static unsigned int chat_counter = 0;
unsigned int chat_get_counter() {
return chat_counter;
}
2021-02-16 17:26:40 +00:00
// Chat Thread
static void *chat_thread(__attribute__((unused)) void *nop) {
// Open
int return_code;
2022-03-14 23:09:25 +00:00
const char *command[] = {"zenity", "--title", "Chat", "--class", GUI_TITLE, "--entry", "--text", "Enter Chat Message:", NULL};
char *output = run_command_proper(command, &return_code);
2021-02-16 17:26:40 +00:00
// Handle Message
if (output != NULL) {
2021-02-17 16:31:01 +00:00
// Check Return Code
2021-02-16 17:26:40 +00:00
if (return_code == 0) {
// Remove Ending Newline
int length = strlen(output);
if (output[length - 1] == '\n') {
output[length - 1] = '\0';
}
length = strlen(output);
2021-02-17 16:31:01 +00:00
// Don't Allow Empty Strings
if (length > 0) {
// Submit
2021-06-17 21:32:24 +00:00
_chat_queue_message(output);
2021-02-17 16:31:01 +00:00
}
2021-02-16 17:26:40 +00:00
}
2021-02-17 16:31:01 +00:00
// Free Output
2021-02-16 17:26:40 +00:00
free(output);
}
2021-02-16 22:08:43 +00:00
// Update Counter
pthread_mutex_lock(&chat_counter_lock);
chat_counter--;
pthread_mutex_unlock(&chat_counter_lock);
2021-02-16 17:26:40 +00:00
// Return
return NULL;
}
// Create Chat Thead
void chat_open() {
2021-07-04 23:02:45 +00:00
if (_chat_enabled) {
// Release Mouse
input_set_mouse_grab_state(1);
// Update Counter
pthread_mutex_lock(&chat_counter_lock);
chat_counter++;
pthread_mutex_unlock(&chat_counter_lock);
// Start Thread
pthread_t thread;
pthread_create(&thread, NULL, chat_thread, NULL);
}
2021-06-17 21:32:24 +00:00
}
2022-03-14 23:09:25 +00:00
#endif