noirchat/x11/x11.c

452 lines
13 KiB
C

#include "x11.h"
void testFunc(){}
void drawLogo() {
// char tmp[] = "#008800";
// XParseColor(display, colormap, tmp, &tmp_color);
// XAllocColor(display, colormap, &tmp_color);
// XSetForeground(display, gc, tmp_color.pixel);
//XSetForeground(display, gc, 12812818);
srand(time(NULL));
XSetForeground(display, gc, rand());
// XDrawArc(display, window, gc, -26, -24, 72, 48, (260*64), (90*64));
// XDrawArc(display, window, gc, -28, -26, 76, 52, (260*64), (90*64));
// XDrawArc(display, window, gc, -32, -30, 80, 56, (260*64), (90*64));
XDrawRectangle(display, window, gc, 1, 1, (7*8)-3, 15);
XDrawRectangle(display, window, gc, 1, 1, (7*8)-1, 17);
XDrawString(display, window, gc, 4, 13, "noirchat", 8);
XDrawString(display, window, gc, 3, 13, "noirchat", 8);
}
int initInterface() {
x_logo.chars = START_MESSAGE;
x_logo.nchars = 60;
x_logo.delta = 10;
display = XOpenDisplay(NULL);
screen = DefaultScreen(display);
int black = BlackPixel(display, screen);
int white = WhitePixel(display, screen);
window = XCreateSimpleWindow(display, RootWindow(display, 0), 1, 1,
240, 320, 0, white, black);
//window = XCreateSimpleWindow(display, RootWindow(display, 0), 1, 1, 256, 256, 0, BlackPixel (display, 0), BlackPixel(display, 0));
XSetStandardProperties(display, window, "noirchat", "noirchat", None, NULL, 0, NULL);
XMapWindow(display, window);
gc = XCreateGC(display, window, 0, 0);
XSetForeground(display, gc, WhitePixel(display, screen));
XSetBackground(display, gc, BlackPixel(display, screen));
XSelectInput(display, window,
ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask
| ButtonReleaseMask | StructureNotifyMask
);
/* set our colormap */
// int i;
// XColor tmp[255];
// for (i=0;i<255;i++) {
// tmp[i].pixel = i;
// tmp[i].flags = DoRed|DoGreen|DoBlue;
// tmp[i].red = i*256;
// tmp[i].green = i*256;
// tmp[i].blue = i*256;
// }
// colormap = XCreateColormap(display, RootWindow(display, screen),
// DefaultVisual(display, screen), AllocAll);
// XStoreColors(display, colormap, tmp, 255);
// XSetWindowColormap(display, window, colormap);
// XSetForeground(display, gc, tmp[16].pixel);
colormap = DefaultColormap(display, 0);
green_gc = XCreateGC(display, window, 0, 0);
char green[] = "#008800";
XParseColor(display, colormap, green, &green_color);
XAllocColor(display, colormap, &green_color);
XSetForeground(display, green_gc, green_color.pixel);
string_gc = XCreateGC(display, window, 0, 0);
XSetForeground(display, string_gc, BlackPixel(display, screen));
XSetBackground(display, gc, WhitePixel(display, screen));
int offset_left = 240;
address_input = newTextInput(offset_left, 6, 20, setAddress);
offset_left = address_input.x = offset_left-(address_input.width+6);
int ip_hash = generateHash("ip", CONFIG_HASH_SIZE);
printf("IP: %s\n", config[ip_hash]);
printf("%d\n", strlen(config[ip_hash]));
strncpy(address_input.data, config[ip_hash], strlen(config[ip_hash]));
address_input.position = strlen(config[ip_hash]);
//address_input.data = (void *)config[ip_hash];
//printf("send: %s -- %d\n", (char *)text_input.data, text_input.size);
addInput(&address_input);
offset_left = 240;
quit_button = newButton(offset_left, 26, QUIT_COMMAND, strlen(QUIT_COMMAND), quitProgram);
offset_left = quit_button.x = 240-(quit_button.width+6);
addInput(&quit_button);
stop_button = newButton(offset_left, 26, STOP_COMMAND, strlen(STOP_COMMAND), stopSockets);
offset_left = stop_button.x = offset_left-(stop_button.width+6);
addInput(&stop_button);
start_button = newButton(offset_left, 26, START_COMMAND, strlen(START_COMMAND), startSockets);
offset_left = start_button.x = offset_left-(start_button.width+6);
addInput(&start_button);
restart_button = newButton(offset_left, 26, RESTART_COMMAND, strlen(RESTART_COMMAND), startSockets);
offset_left = restart_button.x = offset_left-(restart_button.width+6);
addInput(&restart_button);
offset_left = 240;
send_button = newButton(offset_left, 300, "send", 4, sendMessage);
offset_left = send_button.x = offset_left-(send_button.width+6);
addInput(&send_button);
text_input = newTextInput(offset_left, 300, 27, sendMessage);
offset_left = text_input.x = offset_left-(text_input.width+6);
addInput(&text_input);
offset_left = 8;
list_button = newButton(offset_left, 275, LIST_COMMAND, strlen(LIST_COMMAND), showVariables);
offset_left = offset_left+(list_button.width+6);
addInput(&list_button);
redrawInterface();
XFlush(display);
interface_fd = ConnectionNumber(display);
FD_SET(interface_fd, &master_fds);
max_fd = interface_fd;
return 0;
}
Input newButton(int x, int y, const void* data, size_t size, void(*callback)) {
Input new_input;
new_input.type = INPUT_BUTTON;
new_input.position = 0;
new_input.x = x;
new_input.y = y;
new_input.state = STATE_INACTIVE;
new_input.data = malloc(size-1);
//strcpy(new_input.data, data);
memcpy(new_input.data, data, size);
new_input.size = size;
new_input.width = new_input.size*7;
new_input.height = 13;
new_input.callback = callback;
new_input.previous = NULL;
new_input.next = NULL;
return(new_input);
}
Input newTextInput(int x, int y, size_t size, void(*callback)) {
Input new_input;
new_input.type = INPUT_TEXT;
new_input.position = 0;
new_input.x = x;
new_input.y = y;
new_input.state = STATE_INACTIVE;
char string[size];
int i;
for (i=0;i<size;i++) {
string[i] = ' ';
}
string[size] = '\0';
new_input.data = malloc(size);
memcpy(new_input.data, string, size);
//strcpy(new_input.data, string);
new_input.size = size;
new_input.width = new_input.size*7;
new_input.height = 13;
new_input.callback = callback;
new_input.previous = NULL;
new_input.next = NULL;
return(new_input);
}
Input newInput(int x, int y, const void* data, size_t size, void(*callback)) {
Input new_input;
new_input.position = 0;
new_input.x = x;
new_input.y = y;
new_input.state = STATE_INACTIVE;
new_input.data = malloc(size-1);
//strcpy(new_input.data, data);
memcpy(new_input.data, data, size);
new_input.size = size;
new_input.width = new_input.size*7;
new_input.height = 13;
new_input.callback = callback;
new_input.previous = NULL;
new_input.next = NULL;
return(new_input);
}
void freeInput(Input input) {
free(input.data);
}
int addInput(Input *input) {
if (!last_input) {
last_input = input;
} else {
last_input->next = input;
input->previous = last_input;
last_input = input;
}
inputs[input_count] = input;
input_count++;
return 0;
}
int drawInput(const Input *input) {
if (input->type == INPUT_BUTTON) {
if (input->state == STATE_INACTIVE) {
XFillRectangle(display, window, string_gc, input->x-1, input->y-1, input->size*7, 13);
XDrawRectangle(display, window, gc, input->x-2, input->y-2, input->size*7, 13);
XFillRectangle(display, window, gc, input->x, input->y, input->size*7, 13);
XDrawRectangle(display, window, gc, input->x, input->y, input->size*7, 13);
XDrawString(display, window, string_gc, input->x+(input->size/2)+1, input->y+(11), (char *)input->data, input->size);
} else if (input->state == STATE_ACTIVE) {
//XDrawRectangle(display, window, gc, input->x-2, input->y-2, input->size*7, 13);
XFillRectangle(display, window, string_gc, input->x, input->y, input->size*7, 13); // optional
XFillRectangle(display, window, gc, input->x-1, input->y-1, input->size*7, 13);
XDrawRectangle(display, window, gc, input->x-1, input->y-1, input->size*7, 13);
XDrawString(display, window, string_gc, input->x-1+(input->size/2)+1, input->y-1+(11), (char *)input->data, input->size);
}
} else if (input->type == INPUT_TEXT) {
drawInputText(input);
}
return(0);
}
int drawInputText(const Input *input) {
if (input->state == STATE_INACTIVE) {
XFillRectangle(display, window, gc, input->x-1, input->y-1, input->size*7, 13);
XDrawRectangle(display, window, string_gc, input->x-2, input->y-2, input->size*7, 13);
XFillRectangle(display, window, string_gc, input->x, input->y, input->size*7, 13);
XDrawRectangle(display, window, gc, input->x, input->y, input->size*7, 13);
XDrawString(display, window, gc, input->x+(input->size/2)+1, input->y+(11), (char *)input->data, input->size);
} else if (input->state == STATE_ACTIVE) {
//XDrawRectangle(display, window, gc, input->x-2, input->y-2, input->size*7, 13);
XFillRectangle(display, window, string_gc, input->x, input->y, input->size*7, 13); // optional
XFillRectangle(display, window, gc, input->x-1, input->y-1, input->size*7, 13);
XDrawRectangle(display, window, gc, input->x-1, input->y-1, input->size*7, 13);
XDrawString(display, window, string_gc, input->x-1+(input->size/2)+1, input->y-1+(11), (char *)input->data, input->size);
}
return 0;
}
int closeInterface() {
freeInput(quit_button);
FD_CLR(interface_fd, &master_fds);
XFreeGC(display, gc);
XFreeGC(display, string_gc);
XDestroyWindow(display, window);
XCloseDisplay(display);
return 0;
}
int redrawInterface() {
//XClearWindow(display, window);
XDrawRectangle(display, window, gc, 1, 1, 238, 318);
Input *current_input;
int i;
for (i=0;i < input_count; i++) {
current_input = inputs[i];
if (current_input) {
drawInput(current_input);
} else {
break;
}
}
// @@ TODO: implement iterator
/* while (iterateInput(current_input)) {
drawInput(current_input);
}*/
drawLogo();
return 0;
}
int iterateInput(Input *input) {
if (input->previous == NULL) {
printf("lol");
return 0;
} else {
printf("real lol\n");
input = (Input*)input->previous;
return 1;
}
}
int clearInterface() {
XClearWindow(display, window);
return 0;
}
void setAddress(char *str, size_t string_length) {
setConfig("ip", str);
}
void sendMessage(char *str, size_t string_length) {
//printf("send: %s -- %d\n", (char *)text_input.data, text_input.size);
int i = 0;
int done = 0;
while (!done) {
if (((char *)text_input.data)[i++] == '\n')
done = 1;
}
char message[i-1];
strncpy(message, ((char *)text_input.data), i-1); // cut the \n
message[i-1] = '\0';
sendOpen(message);
// sendMulticast(send_socket, &group_socket, text_input.data, text_input.size);
}
int handleInterface() {
while (XPending(display)) {
XNextEvent(display, &event);
if (event.type == Expose && event.xexpose.count == 0) {
redrawInterface();
}
if (event.type == KeyPress && XLookupString(&event.xkey, key_text, 255, &key, 0) == 1) {
/* if (key_text[0] == 'q') {
quitProgram();
printf("%s\n", QUIT_MESSAGE);
}*/
if (active_input != NULL) {
textInput(active_input, key_text[0]);
}
redrawInterface(); // lol
}
if (event.type == ButtonPress) {
Input *current_input;
int i;
if (active_input != NULL) {
active_input->state = STATE_INACTIVE;
active_input = NULL;
}
for (i=0;i < input_count; i++) {
current_input = inputs[i];
if (current_input) {
if (event.xbutton.x < current_input->x+current_input->width && event.xbutton.x > current_input->x
&& event.xbutton.y < current_input->y+current_input->height && event.xbutton.y > current_input->y) {
if (current_input->type == INPUT_TEXT) {
active_input = current_input;
}
current_input->state = STATE_ACTIVE;
active_list[active_count] = current_input;
active_count++;
}
} else {
break;
}
}
redrawInterface();
} else if (event.type == ButtonRelease) {
Input *current_input;
int i;
for (i=0;i < input_count; i++) {
current_input = inputs[i];
if (current_input) {
if (current_input->state == STATE_ACTIVE) {
if (current_input->type == INPUT_TEXT) {
//(*current_input->callback)(NULL);
} else {
(*current_input->callback)(NULL);
current_input->state = STATE_INACTIVE;
}
}
} else {
break;
}
}
/*
for (i=0;i<active_count;i++) {
if (active_list[i]) {
if (active_list[i].type == INPUT_TEXT) {
(*active_list[i]->callback)(NULL);
} else {
(*active_list[i]->callback)(NULL);
active_list[i]->state = STATE_INACTIVE;
}
} else {
break;
}
}
active_count = 0;*/
}
redrawInterface(); // lol
}
return 0;
}
void textInput(Input *input, const char character) {
printf("%d hmm\n", character);
char string[input->size];
memcpy(string, input->data, input->size);
if ((int)character == 8) {// backspace
input->position--;
string[input->position] = ' ';
//string[input->position] = ' ';
} else if ((int)character == 13) { // return
(*input->callback)(&string, input->size);
} else {
string[input->position] = character;
input->position++;
}
memcpy(input->data, string, input->size);
//input->data[1] = character;
printf("%c augh\n", character);
}
int handleInput(const char *data_buffer) {
return 0;
}
void messageOpen(Message message) {
}
void showHelp(const char *value) {
}
void showVariables() {
}
void setAttribute(const int attribute) {
}
void clearAttributes() {
}
// remove ?
void showPrompt() {
}
// implement!
void showUsers() {
}