diff options
Diffstat (limited to 'src/client')
| -rw-r--r-- | src/client/chat.c | 98 | ||||
| -rw-r--r-- | src/client/keyboardBuffer.c | 105 |
2 files changed, 105 insertions, 98 deletions
diff --git a/src/client/chat.c b/src/client/chat.c index 2b2805c..eeca8d8 100644 --- a/src/client/chat.c +++ b/src/client/chat.c @@ -1,5 +1,6 @@ #include <stdlib.h> +#include <ctype.h> #include <assert.h> #include <SDL.h> @@ -64,8 +65,7 @@ void drawChat() char str[STR_SIZE]; int i; - if( chat_active == FALSE ) - { + if (chat_active == FALSE) { return; } @@ -73,32 +73,29 @@ void drawChat() CHAT_LOCATION_X, CHAT_LOCATION_Y, 0, 0, CHAT_SIZE_X, CHAT_SIZE_Y); - for( i = 0 ; i < listText->count ; i++ ) - { + for (i = 0; i < listText->count; i++) { char *s; s = (char *)listText->list[i]; drawFontMaxSize(s, - CHAT_LOCATION_X+5, CHAT_LOCATION_Y+5 + i*20, - CHAT_SIZE_X-10, 20, COLOR_WHITE); + CHAT_LOCATION_X + 5, CHAT_LOCATION_Y + 5 + i * 20, + CHAT_SIZE_X - 10, 20, COLOR_WHITE); } strcpy(str, line); - if( timeBlick > CHAT_TIME_BLICK_CURSOR/2 ) - { + if (timeBlick > CHAT_TIME_BLICK_CURSOR / 2) { strcat(str, "\f"); } timeBlick++; - if( timeBlick == CHAT_TIME_BLICK_CURSOR ) - { + if (timeBlick == CHAT_TIME_BLICK_CURSOR) { timeBlick = 0; } - drawFont(str, CHAT_LOCATION_X+5, CHAT_LOCATION_Y+5 + CHAT_MAX_LINES*20, COLOR_WHITE); + drawFont(str, CHAT_LOCATION_X + 5, (CHAT_LOCATION_Y + 5) + (CHAT_MAX_LINES * 20), COLOR_WHITE); } static void processMessageKey(SDL_keysym keysym) @@ -113,31 +110,35 @@ static void processMessageKey(SDL_keysym keysym) len = strlen(line); getTextSize(line, &w, &h); - if( w > CHAT_SIZE_X-40 ) - { + if (w > CHAT_SIZE_X - 40) { return; } - /* zpracovani backspace */ - if (keysym.sym == SDLK_BACKSPACE && len > 0){ - line[len-1]='\0'; + /* processing of backspace */ + if (keysym.sym == SDLK_BACKSPACE && len > 0) { + line[len - 1] = '\0'; return; } - /* zpracovani tisknutelnych paznaku */ - if ( (keysym.sym >= SDLK_SPACE && keysym.sym <= SDLK_AT) - || (keysym.sym >= SDLK_LEFTBRACKET && keysym.sym <= SDLK_BACKQUOTE) ){ - line[len] = keysym.sym; /* tady by bylo daleko lepsi dat strcat, - predpoklada se, ze line je vyplnen 0 az dokonce */ + /* processing of printable chars but not letters */ + if ((keysym.sym >= SDLK_SPACE && keysym.sym <= SDLK_AT) + || (keysym.sym >= SDLK_LEFTBRACKET && keysym.sym <= SDLK_BACKQUOTE)) { + /* here would be much better to put 'strcat' as 'line' is expected + * to be full of '0' up to its end + */ + line[len] = keysym.sym; } - /* zpracovani pismenek */ - if ( keysym.sym >= SDLK_a && keysym.sym <= SDLK_z){ + /* processing of letters */ + if (keysym.sym >= SDLK_a && keysym.sym <= SDLK_z) { char c = keysym.sym; - if (keysym.mod == KMOD_SHIFT) + if (keysym.mod == KMOD_SHIFT) { c = toupper(c); - line[len] = c; /* tady by bylo daleko lepsi dat strcat, - predpoklada se, ze line je vyplnen 0 az dokonce */ + } + /* here would be much better to put 'strcat' as 'line' is expected + * to be full of '0' up to its end + */ + line[len] = c; } return; @@ -145,13 +146,11 @@ static void processMessageKey(SDL_keysym keysym) static void sendNewMessage() { - if( getNetTypeGame() == NET_GAME_TYPE_CLIENT ) - { + if (getNetTypeGame() == NET_GAME_TYPE_CLIENT) { proto_send_chat_client(line); } - if( getNetTypeGame() == NET_GAME_TYPE_SERVER ) - { + if (getNetTypeGame() == NET_GAME_TYPE_SERVER) { char out[STR_PROTO_SIZE]; snprintf(out, STR_PROTO_SIZE, "chat %s:%s\n", @@ -175,28 +174,25 @@ static void eventChatDisable() static void eventChatEnable() { - /* Mame zobrazene okno chatu. Je potreba zpracovat vsechny klavesy v bufferu - * s tim, ze pri klavese RETURN provedeme odeslani radku chatu - * na server + /* The chat window is displayed. It is needed to process all keys in the buffer + * but when ENTER is pressed the chat row is sent to the server */ - while( isAnyKeyInKeyboardBuffer() == TRUE ) - { + while (isAnyKeyInKeyboardBuffer() == TRUE) { SDL_keysym key; key = popKeyFromKeyboardBuffer(); - if ( key.sym == SDLK_RETURN ) - { - if ( strcmp(line, "") != 0 ) - { /* mame napsany radek: je potreba jej odeslat */ + if (key.sym == SDLK_RETURN) { + if (strcmp(line, "") != 0) { + /* the chat row is already written - it is needed to send it */ sendNewMessage(); memset(line, '\0', STR_SIZE); - continue; /* pokracovat s dalsimi klavesami */ + continue; /* continue with other keys */ } - else - { /* radek je prazdny, je potreba vypnout okno chatu */ + else { + /* the chat row is empty - it is needed to turn off the chat window */ eventChatDisable(); - /* vypneme zachytavani klaves do bufferu a vycistime buffer */ + /* turn off key catching into the buffer and clear the buffer */ //disableKeyboardBuffer(); //clearKeyboardBuffer(); } @@ -207,12 +203,11 @@ static void eventChatEnable() } /** - * Zpracovani "udalosti" chatu? + * Processing of chat 'events' */ void eventChat() { - if( isChatActive() ) - { + if (isChatActive()) { eventChatEnable(); } } @@ -229,23 +224,22 @@ bool_t isRecivedNewMsg() void addToChat(char *s) { - addList(listText, strdup(s) ); + addList(listText, strdup(s)); - if( listText->count > CHAT_MAX_LINES ) - { + if (listText->count > CHAT_MAX_LINES) { delListItem(listText, 0, free); } - if( chat_active == FALSE ) - { + if (chat_active == FALSE) { receivedNewMsg = TRUE; } } void quitChat() { - assert( listText != NULL ); + assert(listText != NULL); unregisterHotKey(SDLK_RETURN); destroyListItem(listText, free); } + diff --git a/src/client/keyboardBuffer.c b/src/client/keyboardBuffer.c index 7dee362..246d262 100644 --- a/src/client/keyboardBuffer.c +++ b/src/client/keyboardBuffer.c @@ -1,64 +1,69 @@ /* * keyboardBuffer.c * - * Created on: 1.8.2008 + * Created on: 1. 8. 2008 * Author: Karel Podvolecky * - * Jednoducha fronta stisknutych klaves. + * Simple front of pressed keys. * */ #include <stdio.h> +#include <stdlib.h> +#include <string.h> #include <assert.h> #include <SDL.h> #include "keyboardBuffer.h" -static keyboardBuffer_t *keyboardBuffer=NULL; +static keyboardBuffer_t *keyboardBuffer = NULL; static SDL_keysym emptyKey; /* - * Inicializuje klavesovy buffer. Parametr udava pocet klaves. + * Key buffer initializes. The parameter sets the number of keys. */ -void initKeyboardBuffer(int size){ - assert(size>0); - assert(keyboardBuffer==NULL); +void initKeyboardBuffer(int size) +{ + assert(size > 0); + assert(keyboardBuffer == NULL); keyboardBuffer = malloc(sizeof(keyboardBuffer_t)); - assert(keyboardBuffer!=NULL); + assert(keyboardBuffer != NULL); memset(keyboardBuffer, 0, sizeof(keyboardBuffer_t)); keyboardBuffer->buff = malloc(size * sizeof(SDL_keysym)); - assert(keyboardBuffer->buff!=NULL); + assert(keyboardBuffer->buff != NULL); memset(keyboardBuffer->buff, 0, size * sizeof(SDL_keysym)); - keyboardBuffer->begin=0; - keyboardBuffer->end=0; - keyboardBuffer->count=0; - keyboardBuffer->size=size; + keyboardBuffer->begin = 0; + keyboardBuffer->end = 0; + keyboardBuffer->count = 0; + keyboardBuffer->size = size; emptyKey.sym = SDLK_UNKNOWN; } /* - * Vyprazdni buffer. + * Empty the buffer */ -void clearKeyboardBuffer(){ - assert(keyboardBuffer!=NULL); +void clearKeyboardBuffer() +{ + assert(keyboardBuffer != NULL); - keyboardBuffer->begin=0; - keyboardBuffer->end=0; - keyboardBuffer->count=0; + keyboardBuffer->begin = 0; + keyboardBuffer->end = 0; + keyboardBuffer->count = 0; memset(keyboardBuffer->buff, 0, keyboardBuffer->size * sizeof(SDL_keysym)); } /* - * Prida klavesu na konec bufferu. Pokud je buffer preplnen, - * vypise chybu na chybovy vystup a klavesu zahodi. + * Adds key to the end of the buffer. If the buffer is overrun, + * an error is printed to stderr and the key is dropped. */ -bool_t pushKeyToKeyboardBuffer(SDL_keysym key){ - assert(keyboardBuffer!=NULL); +bool_t pushKeyToKeyboardBuffer(SDL_keysym key) +{ + assert(keyboardBuffer != NULL); if (keyboardBuffer->count >= keyboardBuffer->size){ fprintf(stderr, _("Keyboard Buffer overrun! Dropping key: %02x\n"), key.sym); @@ -68,8 +73,9 @@ bool_t pushKeyToKeyboardBuffer(SDL_keysym key){ keyboardBuffer->buff[keyboardBuffer->end] = key; keyboardBuffer->end++; - if (keyboardBuffer->end>=keyboardBuffer->size) - keyboardBuffer->end=0; + if (keyboardBuffer->end >= keyboardBuffer->size) { + keyboardBuffer->end = 0; + } keyboardBuffer->count++; @@ -77,11 +83,12 @@ bool_t pushKeyToKeyboardBuffer(SDL_keysym key){ } /* - * Vyjme prvni klavesu z bufferu a vrati ji. Pokud je buffer prazdny, - * vypise chybu na chybovy vystup a vrati SDLK_UNKNOWN + * Takes out first key from the buffer and returns it. If the buffer is empty, + * an error is printed to stderr and SDLK_UNKNOWN is returned. */ -SDL_keysym popKeyFromKeyboardBuffer(){ - assert(keyboardBuffer!=NULL); +SDL_keysym popKeyFromKeyboardBuffer() +{ + assert(keyboardBuffer != NULL); if (keyboardBuffer->count <= 0){ fprintf(stderr, _("Keyboard Buffer underrun!\n")); @@ -89,11 +96,12 @@ SDL_keysym popKeyFromKeyboardBuffer(){ } SDL_keysym key = keyboardBuffer->buff[keyboardBuffer->begin]; - keyboardBuffer->buff[keyboardBuffer->begin]=emptyKey; + keyboardBuffer->buff[keyboardBuffer->begin] = emptyKey; keyboardBuffer->begin++; - if (keyboardBuffer->begin>=keyboardBuffer->size) - keyboardBuffer->begin=0; + if (keyboardBuffer->begin >= keyboardBuffer->size) { + keyboardBuffer->begin = 0; + } keyboardBuffer->count--; @@ -101,38 +109,43 @@ SDL_keysym popKeyFromKeyboardBuffer(){ } /* - * Velikost bufferu. + * The buffer size */ -int getKeyboardBufferSize(){ - assert(keyboardBuffer!=NULL); +int getKeyboardBufferSize() +{ + assert(keyboardBuffer != NULL); return keyboardBuffer->size; } /* - * Pocet klaves v bufferu. + * Number of keys in the buffer */ -int KeyboardBufferCount(){ - assert(keyboardBuffer!=NULL); +int KeyboardBufferCount() +{ + assert(keyboardBuffer != NULL); return keyboardBuffer->count; } -bool_t isAnyKeyInKeyboardBuffer(){ - return KeyboardBufferCount()>0 ? TRUE : FALSE; +bool_t isAnyKeyInKeyboardBuffer() +{ + return KeyboardBufferCount() > 0 ? TRUE : FALSE; } /* - * Uvolni buffer. Pokud neni prazdny, vypise predtim - * info o zaplneni na chybovy vystup. + * Frees the buffer. If it is not empty, information + * about filling is prined to stderr first. */ -void quitKeyboardBuffer(){ - assert(keyboardBuffer!=NULL); +void quitKeyboardBuffer() +{ + assert(keyboardBuffer != NULL); - if (keyboardBuffer->count>0){ + if (keyboardBuffer->count > 0){ fprintf(stderr, _("Keyboard buffer is not empty!\n")); } free(keyboardBuffer->buff); free(keyboardBuffer); - keyboardBuffer=NULL; + keyboardBuffer = NULL; } + |