theme.c/h: consistent indentation

This commit is contained in:
wdlkmpx 2021-01-14 23:28:08 +08:00
parent a43f2eb39b
commit 373a362b01
2 changed files with 330 additions and 294 deletions

View File

@ -1,7 +1,7 @@
/* theme.c - functions related to theme handling /* theme.c - functions related to theme handling
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public * modif (y it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either * License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version. * version 2 of the License, or (at your option) any later version.
*/ */
@ -30,7 +30,9 @@ gchar *THEMEPREFIX="/.gtkballs/themes/";
#define DELTA(d, h) \ #define DELTA(d, h) \
d = h < 0 ? (d > -h ? d + h : 0) : (d + h < 255 ? d + h : 255) d = h < 0 ? (d > -h ? d + h : 0) : (d + h < 255 ? d + h : 255)
void hilight_pixbuff8(GdkPixbuf *pb, gint dr, gint dg, gint db) {
void hilight_pixbuff8(GdkPixbuf *pb, gint dr, gint dg, gint db)
{
/* pb created with 8b/rgb without alpha */ /* pb created with 8b/rgb without alpha */
gint i; gint i;
gint nc = gdk_pixbuf_get_n_channels(pb); gint nc = gdk_pixbuf_get_n_channels(pb);
@ -40,8 +42,8 @@ void hilight_pixbuff8(GdkPixbuf *pb, gint dr, gint dg, gint db) {
gint d = gdk_pixbuf_get_rowstride(pb) - w * nc; gint d = gdk_pixbuf_get_rowstride(pb) - w * nc;
guchar *data = gdk_pixbuf_get_pixels(pb); guchar *data = gdk_pixbuf_get_pixels(pb);
for(i = 0; i < l; i++) { for (i = 0; i < l; i++) {
if(i && (i%w == 0)) { if (i && (i%w == 0)) {
data += d; data += d;
} }
DELTA(*data, dr); DELTA(*data, dr);
@ -53,117 +55,129 @@ void hilight_pixbuff8(GdkPixbuf *pb, gint dr, gint dg, gint db) {
} }
} }
gchar *find_theme_path(gchar *themename) {
gchar *find_theme_path(gchar *themename)
{
gchar *homedir; gchar *homedir;
gchar *themepath; gchar *themepath;
struct stat buf; struct stat buf;
if((homedir = getenv("HOME")) && if ((homedir = getenv("HOME")) &&
(themepath = g_strconcat(homedir, THEMEPREFIX, themename, G_DIR_SEPARATOR_S, NULL))) { (themepath = g_strconcat(homedir, THEMEPREFIX, themename, G_DIR_SEPARATOR_S, NULL))) {
if(!stat(themepath, &buf) && S_ISDIR(buf.st_mode)) { if (!stat(themepath, &buf) && S_ISDIR(buf.st_mode)) {
return themepath; return themepath;
} }
g_free(themepath); g_free (themepath);
} }
if((themepath = g_strconcat(INSTALLPATH, themename, G_DIR_SEPARATOR_S, NULL))) { if ((themepath = g_strconcat(INSTALLPATH, themename, G_DIR_SEPARATOR_S, NULL))) {
if(!stat(themepath, &buf) && S_ISDIR(buf.st_mode)) { if (!stat(themepath, &buf) && S_ISDIR(buf.st_mode)) {
return themepath; return themepath;
} }
g_free(themepath); g_free (themepath);
} }
return NULL; return NULL;
} }
gint gtkb_load_pixmap(GtkbPixmap *pixmap, gchar *path, gchar *pixmapname) {
gint gtkb_load_pixmap(GtkbPixmap *pixmap, gchar *path, gchar *pixmapname)
{
gchar *fname; gchar *fname;
GError *error = NULL; GError *error = NULL;
gint rv = 1; gint rv = 1;
if(!(fname = g_strconcat(path, pixmapname, NULL))) { if (!(fname = g_strconcat(path, pixmapname, NULL))) {
return 0; return 0;
} }
if(pixmap->pixbuf) g_object_unref(pixmap->pixbuf); if (pixmap->pixbuf) {
g_object_unref(pixmap->pixbuf);
}
pixmap->pixbuf = gdk_pixbuf_new_from_file(fname, &error); pixmap->pixbuf = gdk_pixbuf_new_from_file(fname, &error);
if(!pixmap->pixbuf) { if (!pixmap->pixbuf) {
ut_simple_message_box(error->message); ut_simple_message_box(error->message);
g_error_free(error); g_error_free (error);
rv = 0; rv = 0;
} else { } else {
pixmap->xsize = gdk_pixbuf_get_width(pixmap->pixbuf); pixmap->xsize = gdk_pixbuf_get_width (pixmap->pixbuf);
pixmap->ysize = gdk_pixbuf_get_height(pixmap->pixbuf); pixmap->ysize = gdk_pixbuf_get_height (pixmap->pixbuf);
} }
g_free(fname); g_free (fname);
return rv; return rv;
} }
void gtkb_pixmap_free(GtkbPixmap pixmap) { void gtkb_pixmap_free(GtkbPixmap pixmap) {
if(pixmap.pixbuf) { if (pixmap.pixbuf) {
g_object_unref(pixmap.pixbuf); g_object_unref(pixmap.pixbuf);
} }
} }
void gtkb_theme_free(GtkbTheme *theme) {
void gtkb_theme_free(GtkbTheme *theme)
{
gint i, j; gint i, j;
if(!theme) { if (!theme) {
return; return;
} }
gtkb_pixmap_free(theme->emptycell); gtkb_pixmap_free(theme->emptycell);
gtkb_pixmap_free(theme->hemptycell); gtkb_pixmap_free(theme->hemptycell);
for(i = 0; i < 8; i++) { for (i = 0; i < 8; i++) {
gtkb_pixmap_free(theme->paws[i]); gtkb_pixmap_free(theme->paws[i]);
} }
if(theme->balls) { if (theme->balls) {
for(i = 0; i < theme->numballs; i++) { for (i = 0; i < theme->numballs; i++) {
gtkb_pixmap_free(theme->balls[i].ball); gtkb_pixmap_free(theme->balls[i].ball);
gtkb_pixmap_free(theme->balls[i].small); gtkb_pixmap_free(theme->balls[i].small);
if(theme->balls[i].jump) { if (theme->balls[i].jump) {
for(j = 0; j < theme->balls[i].jumpphases; j++) { for (j = 0; j < theme->balls[i].jumpphases; j++) {
gtkb_pixmap_free(theme->balls[i].jump[j]); gtkb_pixmap_free(theme->balls[i].jump[j]);
} }
g_free(theme->balls[i].jump); g_free (theme->balls[i].jump);
if(theme->balls[i].jumpdelays) { if (theme->balls[i].jumpdelays) {
g_free(theme->balls[i].jumpdelays); g_free (theme->balls[i].jumpdelays);
} }
} }
if(theme->balls[i].destroy) { if (theme->balls[i].destroy) {
for(j = 0; j < theme->balls[i].destroyphases; j++) { for (j = 0; j < theme->balls[i].destroyphases; j++) {
gtkb_pixmap_free(theme->balls[i].destroy[j]); gtkb_pixmap_free(theme->balls[i].destroy[j]);
} }
g_free(theme->balls[i].destroy); g_free (theme->balls[i].destroy);
if(theme->balls[i].destroydelays) { if (theme->balls[i].destroydelays) {
g_free(theme->balls[i].destroydelays); g_free (theme->balls[i].destroydelays);
} }
} }
} }
} }
g_free(theme->balls); g_free (theme->balls);
g_free(theme); g_free (theme);
} }
/* warning! tmpname will be free()'d! */ /* warning! tmpname will be free()'d! */
gint gtkb_load_pixmap_from(gchar **trc, gchar *themepath, gchar *tmpname, GtkbPixmap *pixmap) { gint gtkb_load_pixmap_from(gchar **trc, gchar *themepath, gchar *tmpname, GtkbPixmap *pixmap)
{
gchar *val; gchar *val;
gint ret; gint ret;
val = trc_get_str(trc, tmpname); val = trc_get_str(trc, tmpname);
g_free(tmpname); g_free (tmpname);
if(val == NULL) return 0; if (val == NULL) {
return 0;
}
ret = gtkb_load_pixmap(pixmap, themepath, val); ret = gtkb_load_pixmap(pixmap, themepath, val);
g_free(val); g_free (val);
return ret; return ret;
} }
#define CHECKRET(ret, cond) \ #define CHECKRET(ret, cond) \
if(ret == cond) { \ if (ret == cond) { \
gtkb_theme_free(theme); \ gtkb_theme_free(theme); \
trc_close(trc); \ trc_close(trc); \
return NULL; \ return NULL; \
} }
void gtkb_make_hl_pixmap(GtkbTheme *theme) { void gtkb_make_hl_pixmap(GtkbTheme *theme) {
if(theme->hemptycell.pixbuf) { if (theme->hemptycell.pixbuf) {
gtkb_pixmap_free(theme->hemptycell); gtkb_pixmap_free(theme->hemptycell);
} }
theme->hemptycell.pixbuf = gdk_pixbuf_copy(theme->emptycell.pixbuf); theme->hemptycell.pixbuf = gdk_pixbuf_copy(theme->emptycell.pixbuf);
@ -172,7 +186,9 @@ void gtkb_make_hl_pixmap(GtkbTheme *theme) {
hilight_pixbuff8(theme->hemptycell.pixbuf, prefs_get_hl_dr(), prefs_get_hl_dg(), prefs_get_hl_db()); hilight_pixbuff8(theme->hemptycell.pixbuf, prefs_get_hl_dr(), prefs_get_hl_dg(), prefs_get_hl_db());
} }
GtkbTheme *gtkb_load_theme(gchar *themepath) {
GtkbTheme *gtkb_load_theme(gchar *themepath)
{
gchar **trc, *opt; gchar **trc, *opt;
gchar *paws[] = {"down_up", "left_right", "up_down", "right_left", gchar *paws[] = {"down_up", "left_right", "up_down", "right_left",
"down_right", "down_left", "up_right", "up_left", NULL}; "down_right", "down_left", "up_right", "up_left", NULL};
@ -181,8 +197,8 @@ GtkbTheme *gtkb_load_theme(gchar *themepath) {
opt = g_strconcat(themepath, "themerc", NULL); opt = g_strconcat(themepath, "themerc", NULL);
trc = trc_open(opt); /* open theme description file */ trc = trc_open(opt); /* open theme description file */
g_free(opt); g_free (opt);
if(!trc) { if (!trc) {
return NULL; return NULL;
} }
theme = g_new0(GtkbTheme, 1); theme = g_new0(GtkbTheme, 1);
@ -191,7 +207,7 @@ GtkbTheme *gtkb_load_theme(gchar *themepath) {
opt = trc_get_str(trc, "cell"); opt = trc_get_str(trc, "cell");
CHECKRET(opt, NULL); CHECKRET(opt, NULL);
ret = gtkb_load_pixmap(&theme->emptycell, themepath, opt); ret = gtkb_load_pixmap(&theme->emptycell, themepath, opt);
g_free(opt); g_free (opt);
CHECKRET(ret, 0); CHECKRET(ret, 0);
/* /*
theme->hemptycell.pixbuf = gdk_pixbuf_copy(theme->emptycell.pixbuf); theme->hemptycell.pixbuf = gdk_pixbuf_copy(theme->emptycell.pixbuf);
@ -203,65 +219,65 @@ GtkbTheme *gtkb_load_theme(gchar *themepath) {
gtkb_make_hl_pixmap(theme); gtkb_make_hl_pixmap(theme);
/* find and load "footprints" pixmaps. */ /* find and load "footprints" pixmaps. */
for(i = 0; paws[i]; i++) { for (i = 0; paws[i]; i++) {
CHECKRET(gtkb_load_pixmap_from(trc, themepath, g_strconcat("paw.", paws[i], NULL), CHECKRET(gtkb_load_pixmap_from(trc, themepath, g_strconcat("paw.", paws[i], NULL),
&theme->paws[i]), &theme->paws[i]), 0);
0);
} }
/* query number of available balls in theme */ /* query number of available balls in theme */
theme->numballs = trc_get_uint(trc, "ball.numbers"); theme->numballs = trc_get_uint(trc, "ball.numbers");
CHECKRET(theme->numballs, -1); CHECKRET(theme->numballs, -1);
if(theme->numballs < rules_get_colors()) CHECKRET(0, 0); /* yes, i know. its ugly =) */ if (theme->numballs < rules_get_colors()) {
CHECKRET(0, 0); /* yes, i know. its ugly =) */
}
theme->balls = g_new0(GtkbBall, theme->numballs); theme->balls = g_new0(GtkbBall, theme->numballs);
/* find and load all balls data. */ /* find and load all balls data. */
for(i = 0; i < theme->numballs; i++) { for (i = 0; i < theme->numballs; i++) {
CHECKRET(gtkb_load_pixmap_from(trc, themepath, g_strdup_printf("ball.%d.still", i + 1), CHECKRET(gtkb_load_pixmap_from(trc, themepath, g_strdup_printf("ball.%d.still", i + 1),
&theme->balls[i].ball), &theme->balls[i].ball), 0);
0);
CHECKRET(gtkb_load_pixmap_from(trc, themepath, g_strdup_printf("ball.%d.small", i + 1), CHECKRET(gtkb_load_pixmap_from(trc, themepath, g_strdup_printf("ball.%d.small", i + 1),
&theme->balls[i].small), &theme->balls[i].small), 0);
0);
opt = g_strdup_printf("ball.%d.jump.numbers", i + 1); opt = g_strdup_printf("ball.%d.jump.numbers", i + 1);
theme->balls[i].jumpphases = trc_get_uint(trc, opt); theme->balls[i].jumpphases = trc_get_uint(trc, opt);
g_free(opt); g_free (opt);
CHECKRET(theme->balls[i].jumpphases, -1); CHECKRET(theme->balls[i].jumpphases, -1);
if(theme->balls[i].jumpphases < 2) CHECKRET(0, 0); /* yes, i know. its ugly =) */ if (theme->balls[i].jumpphases < 2) {
CHECKRET(0, 0); /* yes, i know. its ugly =) */
}
theme->balls[i].jump = g_new0(GtkbPixmap, theme->balls[i].jumpphases); theme->balls[i].jump = g_new0(GtkbPixmap, theme->balls[i].jumpphases);
theme->balls[i].jumpdelays = g_new0(gint, theme->balls[i].jumpphases); theme->balls[i].jumpdelays = g_new0(gint, theme->balls[i].jumpphases);
for(j = 0; j < theme->balls[i].jumpphases; j++) { for (j = 0; j < theme->balls[i].jumpphases; j++) {
CHECKRET(gtkb_load_pixmap_from(trc, themepath, CHECKRET(gtkb_load_pixmap_from(trc, themepath,
g_strdup_printf("ball.%d.jump.%d", i + 1, j + 1), g_strdup_printf("ball.%d.jump.%d", i + 1, j + 1),
&theme->balls[i].jump[j]), &theme->balls[i].jump[j]), 0);
0);
opt = g_strdup_printf("ball.%d.jump.%d.usec", i + 1, j + 1); opt = g_strdup_printf("ball.%d.jump.%d.usec", i + 1, j + 1);
theme->balls[i].jumpdelays[j] = trc_get_uint(trc, opt); theme->balls[i].jumpdelays[j] = trc_get_uint(trc, opt);
g_free(opt); g_free (opt);
CHECKRET(theme->balls[i].jumpdelays[j], -1); CHECKRET(theme->balls[i].jumpdelays[j], -1);
} }
opt = g_strdup_printf("ball.%d.destroy.numbers", i + 1); opt = g_strdup_printf("ball.%d.destroy.numbers", i + 1);
theme->balls[i].destroyphases = trc_get_uint(trc, opt); theme->balls[i].destroyphases = trc_get_uint(trc, opt);
g_free(opt); g_free (opt);
CHECKRET(theme->balls[i].destroyphases, -1); CHECKRET(theme->balls[i].destroyphases, -1);
if(theme->balls[i].destroyphases < 2) CHECKRET(0, 0); /* yes, i know. its ugly =) */ if (theme->balls[i].destroyphases < 2) {
if(theme->balls[i].destroyphases > theme->maxdestphases) { CHECKRET(0, 0); /* yes, i know. its ugly =) */
}
if (theme->balls[i].destroyphases > theme->maxdestphases) {
theme->maxdestphases = theme->balls[i].destroyphases; theme->maxdestphases = theme->balls[i].destroyphases;
} }
theme->balls[i].destroy = g_new0(GtkbPixmap, theme->balls[i].destroyphases); theme->balls[i].destroy = g_new0(GtkbPixmap, theme->balls[i].destroyphases);
theme->balls[i].destroydelays = g_new0(gint, theme->balls[i].destroyphases); theme->balls[i].destroydelays = g_new0(gint, theme->balls[i].destroyphases);
for (j = 0; j < theme->balls[i].destroyphases; j++) {
for(j = 0; j < theme->balls[i].destroyphases; j++) {
CHECKRET(gtkb_load_pixmap_from(trc, themepath, CHECKRET(gtkb_load_pixmap_from(trc, themepath,
g_strdup_printf("ball.%d.destroy.%d", i + 1, j + 1), g_strdup_printf("ball.%d.destroy.%d", i + 1, j + 1),
&theme->balls[i].destroy[j]), &theme->balls[i].destroy[j]), 0);
0);
opt = g_strdup_printf("ball.%d.destroy.%d.usec", i + 1, j + 1); opt = g_strdup_printf("ball.%d.destroy.%d.usec", i + 1, j + 1);
theme->balls[i].destroydelays[j] = trc_get_uint(trc, opt); theme->balls[i].destroydelays[j] = trc_get_uint(trc, opt);
g_free(opt); g_free (opt);
CHECKRET(theme->balls[i].destroydelays[j], -1); CHECKRET(theme->balls[i].destroydelays[j], -1);
} }
} }
@ -270,16 +286,22 @@ GtkbTheme *gtkb_load_theme(gchar *themepath) {
return theme; return theme;
} }
gint load_theme(gchar *themename) {
gint load_theme(gchar *themename)
{
gchar *themepath; gchar *themepath;
GtkbTheme *theme; GtkbTheme *theme;
if(!(themepath = find_theme_path(themename))) return 0; if (!(themepath = find_theme_path(themename))) {
return 0;
}
theme = gtkb_load_theme(themepath); theme = gtkb_load_theme(themepath);
g_free(themepath); g_free (themepath);
if(!theme) return 0; if (!theme) {
return 0;
}
gtkb_theme_free(gtkbTheme); gtkb_theme_free(gtkbTheme);
gtkbTheme = theme; gtkbTheme = theme;
@ -287,26 +309,34 @@ gint load_theme(gchar *themename) {
return 1; return 1;
} }
gint gtkb_theme_free_handler(GtkWidget *widget, gpointer data) {
gint gtkb_theme_free_handler(GtkWidget *widget, gpointer data)
{
gtkb_theme_free(gtkbTheme); gtkb_theme_free(gtkbTheme);
gtkbTheme = NULL; gtkbTheme = NULL;
return 0; return 0;
} }
gint gtkb_theme_get_balls_num(void) {
gint gtkb_theme_get_balls_num(void)
{
return gtkbTheme ? gtkbTheme->numballs : 0; return gtkbTheme ? gtkbTheme->numballs : 0;
} }
/* returns board coordinate of the pointer */ /* returns board coordinate of the pointer */
gint gtkb_theme_get_coord_at_x(gint x) { gint gtkb_theme_get_coord_at_x(gint x)
if(gtkbTheme) { {
if (gtkbTheme) {
return (x - 1) / gtkbTheme->emptycell.xsize; return (x - 1) / gtkbTheme->emptycell.xsize;
} }
return -1; return -1;
} }
gint gtkb_theme_get_coord_at_y(gint y) {
if(gtkbTheme) { gint gtkb_theme_get_coord_at_y(gint y)
{
if (gtkbTheme) {
return (y - 1) / gtkbTheme->emptycell.ysize; return (y - 1) / gtkbTheme->emptycell.ysize;
} }
return -1; return -1;
@ -320,8 +350,10 @@ gint theme_get_height(void) {
return gtkbTheme->emptycell.xsize; return gtkbTheme->emptycell.xsize;
} }
/* find all available themes. */ /* find all available themes. */
gchar **get_available_themes(void) { gchar **get_available_themes(void)
{
DIR *directory; DIR *directory;
struct dirent *dir_entry; struct dirent *dir_entry;
struct stat entry_stat; struct stat entry_stat;
@ -329,43 +361,47 @@ gchar **get_available_themes(void) {
gint i, j, num, flag; gint i, j, num, flag;
gchar **tlist = NULL; gchar **tlist = NULL;
if(getenv("HOME")) { if (getenv("HOME")) {
hdir = g_strconcat(getenv("HOME"), THEMEPREFIX, NULL); hdir = g_strconcat(getenv("HOME"), THEMEPREFIX, NULL);
} else { } else {
hdir = g_strdup("./"); /* FIXME: does it work on non unix os? */ hdir = g_strdup("./"); /* FIXME: does it work on non unix os? */
} }
for(j = 0, currdir = INSTALLPATH, num = 0; j < 2; j++, currdir = hdir) {
if(!(directory = opendir(currdir))) { for (j = 0, currdir = INSTALLPATH, num = 0; j < 2; j++, currdir = hdir)
{
if (!(directory = opendir(currdir))) {
continue; continue;
} }
while((dir_entry = readdir(directory))) { while((dir_entry = readdir(directory)))
if(!strncmp(dir_entry->d_name, ".", 2) || {
if (!strncmp(dir_entry->d_name, ".", 2) ||
!strncmp(dir_entry->d_name, "..", 3)) { !strncmp(dir_entry->d_name, "..", 3)) {
continue; continue;
} }
entry = g_strconcat(currdir, dir_entry->d_name, NULL); entry = g_strconcat(currdir, dir_entry->d_name, NULL);
if(!stat(entry, &entry_stat) && S_ISDIR(entry_stat.st_mode)) { if (!stat(entry, &entry_stat) && S_ISDIR(entry_stat.st_mode)) {
rcentry = g_strconcat(entry, G_DIR_SEPARATOR_S, "themerc", NULL); rcentry = g_strconcat(entry, G_DIR_SEPARATOR_S, "themerc", NULL);
if(!stat(rcentry, &entry_stat)) { if (!stat(rcentry, &entry_stat)) {
flag = 0; flag = 0;
for(i=0; i < num && !flag; i++) { for (i=0; i < num && !flag; i++) {
if(!strcmp(tlist[i], dir_entry->d_name)) { if (!strcmp(tlist[i], dir_entry->d_name)) {
flag++; flag++;
} }
} }
if(!flag) { if (!flag) {
num++; num++;
tlist = g_realloc(tlist, num * sizeof(gchar *)); tlist = g_realloc(tlist, num * sizeof(gchar *));
tlist[num-1] = g_strdup(dir_entry->d_name); tlist[num-1] = g_strdup(dir_entry->d_name);
} }
} }
g_free(rcentry); g_free (rcentry);
} }
g_free(entry); g_free (entry);
} }
closedir(directory); closedir(directory);
} }
g_free(hdir);
g_free (hdir);
tlist = g_realloc(tlist, (num + 1) * sizeof(gchar *)); tlist = g_realloc(tlist, (num + 1) * sizeof(gchar *));
tlist[num] = NULL; tlist[num] = NULL;
return tlist; return tlist;