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);
@ -53,7 +55,9 @@ 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;
@ -74,7 +78,9 @@ gchar *find_theme_path(gchar *themename) {
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;
@ -82,7 +88,9 @@ gint gtkb_load_pixmap(GtkbPixmap *pixmap, gchar *path, gchar *pixmapname) {
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);
@ -102,7 +110,9 @@ void gtkb_pixmap_free(GtkbPixmap pixmap) {
} }
} }
void gtkb_theme_free(GtkbTheme *theme) {
void gtkb_theme_free(GtkbTheme *theme)
{
gint i, j; gint i, j;
if (!theme) { if (!theme) {
@ -141,14 +151,18 @@ void gtkb_theme_free(GtkbTheme *theme) {
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);
@ -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};
@ -205,38 +221,38 @@ GtkbTheme *gtkb_load_theme(gchar *themepath) {
/* 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);
@ -247,18 +263,18 @@ GtkbTheme *gtkb_load_theme(gchar *themepath) {
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) {
CHECKRET(0, 0); /* yes, i know. its ugly =) */
}
if (theme->balls[i].destroyphases > theme->maxdestphases) { 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);
@ -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,25 +309,33 @@ 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) {
gint gtkb_theme_get_coord_at_y(gint y)
{
if (gtkbTheme) { if (gtkbTheme) {
return (y - 1) / gtkbTheme->emptycell.ysize; return (y - 1) / gtkbTheme->emptycell.ysize;
} }
@ -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;
@ -334,11 +366,14 @@ gchar **get_available_themes(void) {
} 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) {
for (j = 0, currdir = INSTALLPATH, num = 0; j < 2; j++, currdir = hdir)
{
if (!(directory = opendir(currdir))) { 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;
@ -365,6 +400,7 @@ gchar **get_available_themes(void) {
} }
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;