diff options
author | James Booth <boothj5@gmail.com> | 2013-08-27 22:02:23 +0100 |
---|---|---|
committer | James Booth <boothj5@gmail.com> | 2013-08-27 22:02:23 +0100 |
commit | 42c3a1c1f7bce008c2e447fb130426264269cabb (patch) | |
tree | 402dcd0c52b89c782cd2bd7cf722df940a6f46b7 /src | |
parent | 77dcd04b103682ef85979cffeda24b0585a1b563 (diff) | |
download | profani-tty-42c3a1c1f7bce008c2e447fb130426264269cabb.tar.gz |
Added function to get next available window number, and tests
Diffstat (limited to 'src')
-rw-r--r-- | src/common.c | 64 | ||||
-rw-r--r-- | src/common.h | 3 | ||||
-rw-r--r-- | src/ui/windows.c | 1 |
3 files changed, 68 insertions, 0 deletions
diff --git a/src/common.c b/src/common.c index c7ec7d0b..74f35af6 100644 --- a/src/common.c +++ b/src/common.c @@ -399,6 +399,70 @@ get_unique_id(void) return result; } +int +cmp_win_num(gconstpointer a, gconstpointer b) +{ + int real_a = GPOINTER_TO_INT(a); + int real_b = GPOINTER_TO_INT(b); + + if (real_a == 0) { + real_a = 10; + } + + if (real_b == 0) { + real_b = 10; + } + + if (real_a < real_b) { + return -1; + } else if (real_a == real_b) { + return 0; + } else { + return 1; + } +} + +int +get_next_available_win_num(GSList *used) +{ + int result = 0; + used = g_slist_sort(used, cmp_win_num); + // only console used + if (g_slist_length(used) == 1) { + return 2; + } else { + int last_num = 1; + GSList *curr = used; + // skip console + curr = g_slist_next(curr); + while (curr != NULL) { + int curr_num = GPOINTER_TO_INT(curr->data); + if (((last_num != 9) && ((last_num + 1) != curr_num)) || + ((last_num == 9) && (curr_num != 0))) { + g_slist_free(curr); + result = last_num + 1; + if (result == 10) { + result = 0; + } + return (result); + } else { + last_num = curr_num; + if (last_num == 0) { + last_num = 10; + } + } + curr = g_slist_next(curr); + } + result = last_num + 1; + if (result == 10) { + result = 0; + } + + return result; + } +} + + static size_t _data_callback(void *ptr, size_t size, size_t nmemb, void *data) { diff --git a/src/common.h b/src/common.h index f2b59413..dd92e50b 100644 --- a/src/common.h +++ b/src/common.h @@ -91,4 +91,7 @@ contact_presence_t contact_presence_from_resource_presence(resource_presence_t r char * get_unique_id(void); +int cmp_win_num(gconstpointer a, gconstpointer b); +int get_next_available_win_num(GSList *used); + #endif diff --git a/src/ui/windows.c b/src/ui/windows.c index c55dad74..db75bc17 100644 --- a/src/ui/windows.c +++ b/src/ui/windows.c @@ -32,6 +32,7 @@ #include <ncurses.h> #endif +#include "common.h" #include "config/theme.h" #include "ui/ui.h" #include "ui/window.h" |