diff --git a/dgamelaunch.c b/dgamelaunch.c index 781c467..b2dc69e 100644 --- a/dgamelaunch.c +++ b/dgamelaunch.c @@ -440,16 +440,20 @@ inprogressmenu (int gameid) if (len > 0) { mvaddstr (3, 1, "The following games are in progress:"); +#define dgl_sortprintw(mode, x, str) \ + if (sortmode == mode) attron(title_attr); \ + mvprintw(top_banner_hei,x,str); \ + if (sortmode == mode) attroff(title_attr); + mvprintw(top_banner_hei,1," "); - if (sortmode == SORTMODE_USERNAME) attron(title_attr); - mvprintw(top_banner_hei,4,"Username"); - if (sortmode == SORTMODE_USERNAME) attroff(title_attr); - mvprintw(top_banner_hei,21,"Game"); - mvprintw(top_banner_hei,29,"Size"); - mvprintw(top_banner_hei,37,"Start date & time"); - if (sortmode == SORTMODE_IDLETIME) attron(title_attr); - mvprintw(top_banner_hei,58,"Idle time"); - if (sortmode == SORTMODE_IDLETIME) attroff(title_attr); + + dgl_sortprintw(SORTMODE_USERNAME, 4, "Username") + dgl_sortprintw(SORTMODE_GAMENUM, 21, "Game") + dgl_sortprintw(SORTMODE_WINDOWSIZE, 29, "Size") + dgl_sortprintw(SORTMODE_STARTTIME, 37, "Start date & time") + dgl_sortprintw(SORTMODE_IDLETIME, 58, "Idle time") + +#undef dgl_sortprintw } for (i = 0; i < max_height; i++) diff --git a/dgamelaunch.h b/dgamelaunch.h index 4d4a793..738a8d0 100644 --- a/dgamelaunch.h +++ b/dgamelaunch.h @@ -13,6 +13,7 @@ # define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) #endif +#define dglsign(x) (x < 0 ? -1 : (x > 0 ? 1 : 0)) typedef enum { @@ -146,6 +147,9 @@ typedef enum { SORTMODE_NONE = 0, SORTMODE_USERNAME, + SORTMODE_GAMENUM, + SORTMODE_WINDOWSIZE, + SORTMODE_STARTTIME, SORTMODE_IDLETIME, NUM_SORTMODES } dg_sortmode; @@ -153,6 +157,9 @@ typedef enum static const char *SORTMODE_NAME[NUM_SORTMODES] = { "Unsorted", "Username", + "Game", + "Windowsize", + "Starttime", "Idletime" }; diff --git a/dgl-common.c b/dgl-common.c index cffd9e2..7a4e517 100644 --- a/dgl-common.c +++ b/dgl-common.c @@ -379,12 +379,51 @@ sort_game_idletime(const void *g1, const void *g2) return strcasecmp(game1->name, game2->name); } +static int +sort_game_gamenum(const void *g1, const void *g2) +{ + const struct dg_game *game1 = *(const struct dg_game **)g1; + const struct dg_game *game2 = *(const struct dg_game **)g2; + if (game2->gamenum != game1->gamenum) + return dglsign(game2->gamenum - game1->gamenum); + else + return strcasecmp(game1->name, game2->name); +} + +static int +sort_game_windowsize(const void *g1, const void *g2) +{ + const struct dg_game *game1 = *(const struct dg_game **)g1; + const struct dg_game *game2 = *(const struct dg_game **)g2; + if (game2->ws_col != game1->ws_col) + return dglsign(game1->ws_col - game2->ws_col); + if (game2->ws_row != game1->ws_row) + return dglsign(game1->ws_row - game2->ws_row); + return strcasecmp(game1->name, game2->name); +} + +static int +sort_game_starttime(const void *g1, const void *g2) +{ + const struct dg_game *game1 = *(const struct dg_game **)g1; + const struct dg_game *game2 = *(const struct dg_game **)g2; + int i = strcmp(game1->date, game2->date); + if (!i) + i = strcmp(game1->time, game2->time); + if (!i) + return strcasecmp(game1->name, game2->name); + return i; +} + struct dg_game ** sort_games (struct dg_game **games, int len, dg_sortmode sortmode) { switch (sortmode) { case SORTMODE_USERNAME: qsort(games, len, sizeof(struct dg_game *), sort_game_username); break; + case SORTMODE_GAMENUM: qsort(games, len, sizeof(struct dg_game *), sort_game_gamenum); break; + case SORTMODE_WINDOWSIZE: qsort(games, len, sizeof(struct dg_game *), sort_game_windowsize); break; case SORTMODE_IDLETIME: qsort(games, len, sizeof(struct dg_game *), sort_game_idletime); break; + case SORTMODE_STARTTIME: qsort(games, len, sizeof(struct dg_game *), sort_game_starttime); break; default: ; } return games; diff --git a/examples/dgamelaunch.conf b/examples/dgamelaunch.conf index 0c203e5..95c31a2 100644 --- a/examples/dgamelaunch.conf +++ b/examples/dgamelaunch.conf @@ -20,7 +20,8 @@ allow_new_nicks = yes maxnicklen = 10 # Set the default watching-screen sorting mode. Can be one of -# "unsorted", "username" or "idletime". Unsorted is the default. +# "unsorted", "username", "game", "windowsize", "starttime" or "idletime". +# Unsorted is the default. #sortmode = "username" # Path to a prepared chroot jail.