| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include <gtk/gtk.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <sys/types.h> |
| 11 |
#include <dirent.h> |
| 12 |
#include <string.h> |
| 13 |
#include <sys/stat.h> |
| 14 |
#include <unistd.h> |
| 15 |
#include <stdio.h> |
| 16 |
|
| 17 |
#include "theme.h" |
| 18 |
|
| 19 |
#include "themerc.h" |
| 20 |
#include "game.h" |
| 21 |
#include "gtkutils.h" |
| 22 |
#include "prefs.h" |
| 23 |
|
| 24 |
GtkbTheme *gtkbTheme=NULL; |
| 25 |
|
| 26 |
|
| 27 |
gchar *INSTALLPATH=DATADIR "/gtkballs/themes/"; |
| 28 |
gchar *THEMEPREFIX="/.gtkballs/themes/"; |
| 29 |
|
| 30 |
#define DELTA(d, h) \ |
| 31 |
d = h < 0 ? (d > -h ? d + h : 0) : (d + h < 255 ? d + h : 255) |
| 32 |
|
| 33 |
|
| 34 |
void hilight_pixbuff8(GdkPixbuf *pb, gint dr, gint dg, gint db) |
| 35 |
{ |
| 36 |
|
| 37 |
gint i; |
| 38 |
gint nc = gdk_pixbuf_get_n_channels(pb); |
| 39 |
gint delta = nc - 3; |
| 40 |
gint w = gdk_pixbuf_get_width(pb); |
| 41 |
gint l = w * gdk_pixbuf_get_height(pb); |
| 42 |
gint d = gdk_pixbuf_get_rowstride(pb) - w * nc; |
| 43 |
guchar *data = gdk_pixbuf_get_pixels(pb); |
| 44 |
|
| 45 |
for (i = 0; i < l; i++) { |
| 46 |
if (i && (i%w == 0)) { |
| 47 |
data += d; |
| 48 |
} |
| 49 |
DELTA(*data, dr); |
| 50 |
data++; |
| 51 |
DELTA(*data, dg); |
| 52 |
data++; |
| 53 |
DELTA(*data, db); |
| 54 |
data += delta + 1; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
gchar *find_theme_path(gchar *themename) |
| 60 |
{ |
| 61 |
gchar *homedir; |
| 62 |
gchar *themepath; |
| 63 |
struct stat buf; |
| 64 |
|
| 65 |
if ((homedir = getenv("HOME")) && |
| 66 |
(themepath = g_strconcat(homedir, THEMEPREFIX, themename, G_DIR_SEPARATOR_S, NULL))) { |
| 67 |
if (!stat(themepath, &buf) && S_ISDIR(buf.st_mode)) { |
| 68 |
return themepath; |
| 69 |
} |
| 70 |
g_free (themepath); |
| 71 |
} |
| 72 |
if ((themepath = g_strconcat(INSTALLPATH, themename, G_DIR_SEPARATOR_S, NULL))) { |
| 73 |
if (!stat(themepath, &buf) && S_ISDIR(buf.st_mode)) { |
| 74 |
return themepath; |
| 75 |
} |
| 76 |
g_free (themepath); |
| 77 |
} |
| 78 |
return NULL; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
gint gtkb_load_pixmap(GtkbPixmap *pixmap, gchar *path, gchar *pixmapname) |
| 83 |
{ |
| 84 |
gchar *fname; |
| 85 |
GError *error = NULL; |
| 86 |
gint rv = 1; |
| 87 |
|
| 88 |
if (!(fname = g_strconcat(path, pixmapname, NULL))) { |
| 89 |
return 0; |
| 90 |
} |
| 91 |
if (pixmap->pixbuf) { |
| 92 |
g_object_unref(pixmap->pixbuf); |
| 93 |
} |
| 94 |
pixmap->pixbuf = gdk_pixbuf_new_from_file(fname, &error); |
| 95 |
if (!pixmap->pixbuf) { |
| 96 |
ut_simple_message_box(error->message); |
| 97 |
g_error_free (error); |
| 98 |
rv = 0; |
| 99 |
} else { |
| 100 |
pixmap->xsize = gdk_pixbuf_get_width (pixmap->pixbuf); |
| 101 |
pixmap->ysize = gdk_pixbuf_get_height (pixmap->pixbuf); |
| 102 |
} |
| 103 |
g_free (fname); |
| 104 |
return rv; |
| 105 |
} |
| 106 |
|
| 107 |
void gtkb_pixmap_free(GtkbPixmap pixmap) { |
| 108 |
if (pixmap.pixbuf) { |
| 109 |
g_object_unref(pixmap.pixbuf); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
void gtkb_theme_free(GtkbTheme *theme) |
| 115 |
{ |
| 116 |
gint i, j; |
| 117 |
|
| 118 |
if (!theme) { |
| 119 |
return; |
| 120 |
} |
| 121 |
gtkb_pixmap_free(theme->emptycell); |
| 122 |
gtkb_pixmap_free(theme->hemptycell); |
| 123 |
for (i = 0; i < 8; i++) { |
| 124 |
gtkb_pixmap_free(theme->paws[i]); |
| 125 |
} |
| 126 |
if (theme->balls) { |
| 127 |
for (i = 0; i < theme->numballs; i++) { |
| 128 |
gtkb_pixmap_free(theme->balls[i].ball); |
| 129 |
gtkb_pixmap_free(theme->balls[i].small); |
| 130 |
if (theme->balls[i].jump) { |
| 131 |
for (j = 0; j < theme->balls[i].jumpphases; j++) { |
| 132 |
gtkb_pixmap_free(theme->balls[i].jump[j]); |
| 133 |
} |
| 134 |
g_free (theme->balls[i].jump); |
| 135 |
if (theme->balls[i].jumpdelays) { |
| 136 |
g_free (theme->balls[i].jumpdelays); |
| 137 |
} |
| 138 |
} |
| 139 |
if (theme->balls[i].destroy) { |
| 140 |
for (j = 0; j < theme->balls[i].destroyphases; j++) { |
| 141 |
gtkb_pixmap_free(theme->balls[i].destroy[j]); |
| 142 |
} |
| 143 |
g_free (theme->balls[i].destroy); |
| 144 |
if (theme->balls[i].destroydelays) { |
| 145 |
g_free (theme->balls[i].destroydelays); |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
g_free (theme->balls); |
| 151 |
g_free (theme); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
gint gtkb_load_pixmap_from(gchar **trc, gchar *themepath, gchar *tmpname, GtkbPixmap *pixmap) |
| 157 |
{ |
| 158 |
gchar *val; |
| 159 |
gint ret; |
| 160 |
|
| 161 |
val = trc_get_str(trc, tmpname); |
| 162 |
g_free (tmpname); |
| 163 |
if (val == NULL) { |
| 164 |
return 0; |
| 165 |
} |
| 166 |
ret = gtkb_load_pixmap(pixmap, themepath, val); |
| 167 |
g_free (val); |
| 168 |
|
| 169 |
return ret; |
| 170 |
} |
| 171 |
|
| 172 |
#define CHECKRET(ret, cond) \ |
| 173 |
if (ret == cond) { \ |
| 174 |
gtkb_theme_free(theme); \ |
| 175 |
trc_close(trc); \ |
| 176 |
return NULL; \ |
| 177 |
} |
| 178 |
|
| 179 |
void gtkb_make_hl_pixmap(GtkbTheme *theme) { |
| 180 |
if (theme->hemptycell.pixbuf) { |
| 181 |
gtkb_pixmap_free(theme->hemptycell); |
| 182 |
} |
| 183 |
theme->hemptycell.pixbuf = gdk_pixbuf_copy(theme->emptycell.pixbuf); |
| 184 |
theme->hemptycell.xsize = theme->emptycell.xsize; |
| 185 |
theme->hemptycell.ysize = theme->emptycell.ysize; |
| 186 |
hilight_pixbuff8(theme->hemptycell.pixbuf, prefs_get_hl_dr(), prefs_get_hl_dg(), prefs_get_hl_db()); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
GtkbTheme *gtkb_load_theme(gchar *themepath) |
| 191 |
{ |
| 192 |
gchar **trc, *opt; |
| 193 |
gchar *paws[] = {"down_up", "left_right", "up_down", "right_left", |
| 194 |
"down_right", "down_left", "up_right", "up_left", NULL}; |
| 195 |
gint i, j, ret; |
| 196 |
GtkbTheme *theme; |
| 197 |
|
| 198 |
opt = g_strconcat(themepath, "themerc", NULL); |
| 199 |
trc = trc_open(opt); |
| 200 |
g_free (opt); |
| 201 |
if (!trc) { |
| 202 |
return NULL; |
| 203 |
} |
| 204 |
theme = g_new0(GtkbTheme, 1); |
| 205 |
|
| 206 |
|
| 207 |
opt = trc_get_str(trc, "cell"); |
| 208 |
CHECKRET(opt, NULL); |
| 209 |
ret = gtkb_load_pixmap(&theme->emptycell, themepath, opt); |
| 210 |
g_free (opt); |
| 211 |
CHECKRET(ret, 0); |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
gtkb_make_hl_pixmap(theme); |
| 220 |
|
| 221 |
|
| 222 |
for (i = 0; paws[i]; i++) { |
| 223 |
CHECKRET(gtkb_load_pixmap_from(trc, themepath, g_strconcat("paw.", paws[i], NULL), |
| 224 |
&theme->paws[i]), 0); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
theme->numballs = trc_get_uint(trc, "ball.numbers"); |
| 229 |
CHECKRET(theme->numballs, -1); |
| 230 |
if (theme->numballs < rules_get_colors()) { |
| 231 |
CHECKRET(0, 0); |
| 232 |
} |
| 233 |
theme->balls = g_new0(GtkbBall, theme->numballs); |
| 234 |
|
| 235 |
|
| 236 |
for (i = 0; i < theme->numballs; i++) { |
| 237 |
CHECKRET(gtkb_load_pixmap_from(trc, themepath, g_strdup_printf("ball.%d.still", i + 1), |
| 238 |
&theme->balls[i].ball), 0); |
| 239 |
CHECKRET(gtkb_load_pixmap_from(trc, themepath, g_strdup_printf("ball.%d.small", i + 1), |
| 240 |
&theme->balls[i].small), 0); |
| 241 |
|
| 242 |
opt = g_strdup_printf("ball.%d.jump.numbers", i + 1); |
| 243 |
theme->balls[i].jumpphases = trc_get_uint(trc, opt); |
| 244 |
g_free (opt); |
| 245 |
CHECKRET(theme->balls[i].jumpphases, -1); |
| 246 |
if (theme->balls[i].jumpphases < 2) { |
| 247 |
CHECKRET(0, 0); |
| 248 |
} |
| 249 |
theme->balls[i].jump = g_new0(GtkbPixmap, theme->balls[i].jumpphases); |
| 250 |
theme->balls[i].jumpdelays = g_new0(gint, theme->balls[i].jumpphases); |
| 251 |
|
| 252 |
for (j = 0; j < theme->balls[i].jumpphases; j++) { |
| 253 |
CHECKRET(gtkb_load_pixmap_from(trc, themepath, |
| 254 |
g_strdup_printf("ball.%d.jump.%d", i + 1, j + 1), |
| 255 |
&theme->balls[i].jump[j]), 0); |
| 256 |
opt = g_strdup_printf("ball.%d.jump.%d.usec", i + 1, j + 1); |
| 257 |
theme->balls[i].jumpdelays[j] = trc_get_uint(trc, opt); |
| 258 |
g_free (opt); |
| 259 |
CHECKRET(theme->balls[i].jumpdelays[j], -1); |
| 260 |
} |
| 261 |
|
| 262 |
opt = g_strdup_printf("ball.%d.destroy.numbers", i + 1); |
| 263 |
theme->balls[i].destroyphases = trc_get_uint(trc, opt); |
| 264 |
g_free (opt); |
| 265 |
CHECKRET(theme->balls[i].destroyphases, -1); |
| 266 |
if (theme->balls[i].destroyphases < 2) { |
| 267 |
CHECKRET(0, 0); |
| 268 |
} |
| 269 |
if (theme->balls[i].destroyphases > theme->maxdestphases) { |
| 270 |
theme->maxdestphases = theme->balls[i].destroyphases; |
| 271 |
} |
| 272 |
theme->balls[i].destroy = g_new0(GtkbPixmap, theme->balls[i].destroyphases); |
| 273 |
theme->balls[i].destroydelays = g_new0(gint, theme->balls[i].destroyphases); |
| 274 |
for (j = 0; j < theme->balls[i].destroyphases; j++) { |
| 275 |
CHECKRET(gtkb_load_pixmap_from(trc, themepath, |
| 276 |
g_strdup_printf("ball.%d.destroy.%d", i + 1, j + 1), |
| 277 |
&theme->balls[i].destroy[j]), 0); |
| 278 |
opt = g_strdup_printf("ball.%d.destroy.%d.usec", i + 1, j + 1); |
| 279 |
theme->balls[i].destroydelays[j] = trc_get_uint(trc, opt); |
| 280 |
g_free (opt); |
| 281 |
CHECKRET(theme->balls[i].destroydelays[j], -1); |
| 282 |
} |
| 283 |
} |
| 284 |
trc_close(trc); |
| 285 |
|
| 286 |
return theme; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
gint load_theme(gchar *themename) |
| 291 |
{ |
| 292 |
gchar *themepath; |
| 293 |
GtkbTheme *theme; |
| 294 |
|
| 295 |
if (!(themepath = find_theme_path(themename))) { |
| 296 |
return 0; |
| 297 |
} |
| 298 |
|
| 299 |
theme = gtkb_load_theme(themepath); |
| 300 |
g_free (themepath); |
| 301 |
|
| 302 |
if (!theme) { |
| 303 |
return 0; |
| 304 |
} |
| 305 |
|
| 306 |
gtkb_theme_free(gtkbTheme); |
| 307 |
gtkbTheme = theme; |
| 308 |
|
| 309 |
return 1; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
gint gtkb_theme_free_handler(GtkWidget *widget, gpointer data) |
| 314 |
{ |
| 315 |
gtkb_theme_free(gtkbTheme); |
| 316 |
gtkbTheme = NULL; |
| 317 |
return 0; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
gint gtkb_theme_get_balls_num(void) |
| 322 |
{ |
| 323 |
return gtkbTheme ? gtkbTheme->numballs : 0; |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
gint gtkb_theme_get_coord_at_x(gint x) |
| 329 |
{ |
| 330 |
if (gtkbTheme) { |
| 331 |
return (x - 1) / gtkbTheme->emptycell.xsize; |
| 332 |
} |
| 333 |
return -1; |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
gint gtkb_theme_get_coord_at_y(gint y) |
| 338 |
{ |
| 339 |
if (gtkbTheme) { |
| 340 |
return (y - 1) / gtkbTheme->emptycell.ysize; |
| 341 |
} |
| 342 |
return -1; |
| 343 |
} |
| 344 |
|
| 345 |
gint theme_get_width(void) { |
| 346 |
return gtkbTheme->emptycell.xsize; |
| 347 |
} |
| 348 |
|
| 349 |
gint theme_get_height(void) { |
| 350 |
return gtkbTheme->emptycell.xsize; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
gchar **get_available_themes(void) |
| 356 |
{ |
| 357 |
DIR *directory; |
| 358 |
struct dirent *dir_entry; |
| 359 |
struct stat entry_stat; |
| 360 |
gchar *entry, *currdir, *hdir, *rcentry; |
| 361 |
gint i, j, num, flag; |
| 362 |
gchar **tlist = NULL; |
| 363 |
|
| 364 |
if (getenv("HOME")) { |
| 365 |
hdir = g_strconcat(getenv("HOME"), THEMEPREFIX, NULL); |
| 366 |
} else { |
| 367 |
hdir = g_strdup("./"); |
| 368 |
} |
| 369 |
|
| 370 |
for (j = 0, currdir = INSTALLPATH, num = 0; j < 2; j++, currdir = hdir) |
| 371 |
{ |
| 372 |
if (!(directory = opendir(currdir))) { |
| 373 |
continue; |
| 374 |
} |
| 375 |
while((dir_entry = readdir(directory))) |
| 376 |
{ |
| 377 |
if (!strncmp(dir_entry->d_name, ".", 2) || |
| 378 |
!strncmp(dir_entry->d_name, "..", 3)) { |
| 379 |
continue; |
| 380 |
} |
| 381 |
entry = g_strconcat(currdir, dir_entry->d_name, NULL); |
| 382 |
if (!stat(entry, &entry_stat) && S_ISDIR(entry_stat.st_mode)) { |
| 383 |
rcentry = g_strconcat(entry, G_DIR_SEPARATOR_S, "themerc", NULL); |
| 384 |
if (!stat(rcentry, &entry_stat)) { |
| 385 |
flag = 0; |
| 386 |
for (i=0; i < num && !flag; i++) { |
| 387 |
if (!strcmp(tlist[i], dir_entry->d_name)) { |
| 388 |
flag++; |
| 389 |
} |
| 390 |
} |
| 391 |
if (!flag) { |
| 392 |
num++; |
| 393 |
tlist = g_realloc(tlist, num * sizeof(gchar *)); |
| 394 |
tlist[num-1] = g_strdup(dir_entry->d_name); |
| 395 |
} |
| 396 |
} |
| 397 |
g_free (rcentry); |
| 398 |
} |
| 399 |
g_free (entry); |
| 400 |
} |
| 401 |
closedir(directory); |
| 402 |
} |
| 403 |
|
| 404 |
g_free (hdir); |
| 405 |
tlist = g_realloc(tlist, (num + 1) * sizeof(gchar *)); |
| 406 |
tlist[num] = NULL; |
| 407 |
return tlist; |
| 408 |
} |