fdtuicfg.c 15.5 KB
Newer Older
Ercan Ersoy's avatar
Ercan Ersoy committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
/*
 FDTUICFG.C

 License CC0 PUBLIC DOMAIN

 To the extent possible under law, Mark J. Olesen has waived all copyright 
 and related or neighboring rights to FDOSTUI Library. This work is published 
 from: United States.
*/
#include "fdtuicfg.h"
#include "ini.h"
#include "skin.h"
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#if defined(__DOS__) || defined(__NT__) && !defined(__NTCRUSTY__)
#include <sys\types.h>
#include <sys\stat.h>
#include <direct.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <limits.h>
#endif

#if !defined(PATH_MAX)
#   define PATH_MAX                     4096
#endif

static enum background
strtobcolor(
    char const *                        i_color,
    enum background const               i_default)
{
    enum background                     l_color;
    int                                 l_rc;

    l_color= i_default;

    do
    {

        l_rc= strcmp(i_color, "black");
        if (0 == l_rc)
        {
            l_color= bcolor_black;
            break;
        }

        l_rc= strcmp(i_color, "blue");
        if (0 == l_rc)
        {
            l_color= bcolor_blue;
            break;
        }

        l_rc= strcmp(i_color, "green");
        if (0 == l_rc)
        {
            l_color= bcolor_green;
            break;
        }

        l_rc= strcmp(i_color, "cyan");
        if (0 == l_rc)
        {
            l_color= bcolor_cyan;
            break;
        }

        l_rc= strcmp(i_color, "red");
        if (0 == l_rc)
        {
            l_color= bcolor_red;
            break;
        }

        l_rc= strcmp(i_color, "magenta");
        if (0 == l_rc)
        {
            l_color= bcolor_magenta;
            break;
        }

        l_rc= strcmp(i_color, "grey");
        if (0 == l_rc)
        {
            l_color= bcolor_light_gray;
            break;
        }

    }while(0);

    return l_color;
}

static enum foreground
strtofcolor(
    char const *                        i_color,
    enum foreground const               i_default)
{
    enum foreground                     l_color;
    int                                 l_rc;

    l_color= i_default;

    do
    {

        l_rc= strcmp(i_color, "black");
        if (0 == l_rc)
        {
            l_color= fcolor_black;
            break;
        }

        l_rc= strcmp(i_color, "blue");
        if (0 == l_rc)
        {
            l_color= fcolor_blue;
            break;
        }

        l_rc= strcmp(i_color, "green");
        if (0 == l_rc)
        {
            l_color= fcolor_green;
            break;
        }

        l_rc= strcmp(i_color, "cyan");
        if (0 == l_rc)
        {
            l_color= fcolor_cyan;
            break;
        }

        l_rc= strcmp(i_color, "red");
        if (0 == l_rc)
        {
            l_color= bcolor_red;
            break;
        }

        l_rc= strcmp(i_color, "magenta");
        if (0 == l_rc)
        {
            l_color= fcolor_magenta;
            break;
        }

        l_rc= strcmp(i_color, "yellow");
        if (0 == l_rc)
        {
            l_color= fcolor_yellow;
            break;
        }

        l_rc= strcmp(i_color, "white");
        if (0 == l_rc)
        {
            l_color= fcolor_white;
            break;
        }

    }while(0);

    return l_color;
}

static void
desktop_load(
    struct skin_desktop*const           io_skin,
    struct ini *const                   i_ini,
    char const*                         i_section)
{
    int                                 l_found;
    char const *                        l_value;

    l_value= ini_lookup(&l_found, i_ini, i_section, "fill");
    if (l_value) 
    {
        (*io_skin).m_fillc= *l_value;
    }

    l_value= ini_lookup(&l_found, i_ini, i_section, "foreground");
    if (l_value)
    {
        (*io_skin).m_fcolor= strtofcolor(l_value, (*io_skin).m_fcolor);
    }

    l_value= ini_lookup(&l_found, i_ini, i_section, "background");
    if (l_value)
    {
        (*io_skin).m_bcolor= strtobcolor(l_value, (*io_skin).m_bcolor);
    }

    return;
}

static void
window_load(
    struct skin_window*const            io_skin,
    struct ini *const                   i_ini,
    char const*                         i_section)
{
    int                                 l_found;
    char const *                        l_value;

    l_value= ini_lookup(&l_found, i_ini, i_section, "fill");
    if (l_value) 
    {
        (*io_skin).m_fillc= *l_value;
    }

    l_value= ini_lookup(&l_found, i_ini, i_section, "foreground");
    if (l_value)
    {
        (*io_skin).m_fcolor= strtofcolor(l_value, (*io_skin).m_fcolor);
    }

    l_value= ini_lookup(&l_found, i_ini, i_section, "background");
    if (l_value)
    {
        (*io_skin).m_bcolor= strtobcolor(l_value, (*io_skin).m_bcolor);
    }

    l_value= ini_lookup(&l_found, i_ini, i_section, "text.foreground");
    if (l_value)
    {
        (*io_skin).m_text_fcolor= strtofcolor(l_value, (*io_skin).m_text_fcolor);
    }

    l_value= ini_lookup(&l_found, i_ini, i_section, "text.background");
    if (l_value)
    {
        (*io_skin).m_text_bcolor= strtobcolor(l_value, (*io_skin).m_text_bcolor);
    }

    return;
}

static void
menu_load(
    struct skin_menu*const              io_skin,
    struct ini *const                   i_ini,
    char const*                         i_section)
{
    int                                 l_found;
    char const *                        l_value;

    window_load(&(*io_skin).m_window, i_ini, i_section);

    l_value= ini_lookup(&l_found, i_ini, i_section, "selected.foreground");
    if (l_value)
    {
        (*io_skin).m_selected_fcolor= strtofcolor(l_value, (*io_skin).m_selected_fcolor);
    }

    l_value= ini_lookup(&l_found, i_ini, i_section, "selected.background");
    if (l_value)
    {
        (*io_skin).m_selected_bcolor= strtobcolor(l_value, (*io_skin).m_selected_bcolor);
    }

    l_value= ini_lookup(&l_found, i_ini, i_section, "disabled.foreground");
    if (l_value)
    {
        (*io_skin).m_disabled_fcolor= strtofcolor(l_value, (*io_skin).m_disabled_fcolor);
    }

    l_value= ini_lookup(&l_found, i_ini, i_section, "disabled.background");
    if (l_value)
    {
        (*io_skin).m_disabled_bcolor= strtobcolor(l_value, (*io_skin).m_disabled_bcolor);
    }

    l_value= ini_lookup(&l_found, i_ini, i_section, "hotkey.foreground");
    if (l_value)
    {
        (*io_skin).m_hotkey_fcolor= strtofcolor(l_value, (*io_skin).m_hotkey_fcolor);
    }

    l_value= ini_lookup(&l_found, i_ini, i_section, "hotkey.background");
    if (l_value)
    {
        (*io_skin).m_hotkey_bcolor= strtobcolor(l_value, (*io_skin).m_hotkey_bcolor);
    }

    return;
}

static void
button_load(
    struct skin_button*const            io_skin,
    struct ini *const                   i_ini,
    char const*                         i_section)
{
    int                                 l_found;
    char const *                        l_value;

    l_value= ini_lookup(&l_found, i_ini, i_section, "foreground");
    if (l_value)
    {
        (*io_skin).m_fcolor= strtofcolor(l_value, (*io_skin).m_fcolor);
    }

    l_value= ini_lookup(&l_found, i_ini, i_section, "background");
    if (l_value)
    {
        (*io_skin).m_bcolor= strtobcolor(l_value, (*io_skin).m_bcolor);
    }

    l_value= ini_lookup(&l_found, i_ini, i_section, "focused.foreground");
    if (l_value)
    {
        (*io_skin).m_focused= strtofcolor(l_value, (*io_skin).m_focused);
    }

    l_value= ini_lookup(&l_found, i_ini, i_section, "shadow.background");
    if (l_value)
    {
        (*io_skin).m_shadow= strtobcolor(l_value, (*io_skin).m_shadow);
    }

    return;
}

static void
label_load(
    struct skin_label*const             io_skin,
    struct ini *const                   i_ini,
    char const*                         i_section)
{
    int                                 l_found;
    char const *                        l_value;

    l_value= ini_lookup(&l_found, i_ini, i_section, "foreground");
    if (l_value)
    {
        (*io_skin).m_fcolor= strtofcolor(l_value, (*io_skin).m_fcolor);
    }

    l_value= ini_lookup(&l_found, i_ini, i_section, "background");
    if (l_value)
    {
        (*io_skin).m_bcolor= strtobcolor(l_value, (*io_skin).m_bcolor);
    }

    return;
}

static void
slider_load(
    struct skin_slider*const            io_skin,
    struct ini *const                   i_ini,
    char const*                         i_section)
{
    int                                 l_found;
    char const *                        l_value;

    l_value= ini_lookup(&l_found, i_ini, i_section, "fill");
    if (l_value) 
    {
        (*io_skin).m_fillc= *l_value;
    }

    l_value= ini_lookup(&l_found, i_ini, i_section, "foreground");
    if (l_value)
    {
        (*io_skin).m_fcolor= strtofcolor(l_value, (*io_skin).m_fcolor);
    }

    l_value= ini_lookup(&l_found, i_ini, i_section, "background");
    if (l_value)
    {
        (*io_skin).m_bcolor= strtobcolor(l_value, (*io_skin).m_bcolor);
    }

    return;
}

static void
listbox_load(
    struct skin_listbox*const           io_skin,
    struct ini *const                   i_ini,
    char const*                         i_section)
{
    int                                 l_found;
    char const *                        l_value;

    l_value= ini_lookup(&l_found, i_ini, i_section, "foreground");
    if (l_value)
    {
        (*io_skin).m_fcolor= strtofcolor(l_value, (*io_skin).m_fcolor);
    }

    l_value= ini_lookup(&l_found, i_ini, i_section, "background");
    if (l_value)
    {
        (*io_skin).m_bcolor= strtobcolor(l_value, (*io_skin).m_bcolor);
    }

    l_value= ini_lookup(&l_found, i_ini, i_section, "highlight.foreground");
    if (l_value)
    {
        (*io_skin).m_highlight_fcolor= strtofcolor(l_value, (*io_skin).m_highlight_fcolor);
    }

    l_value= ini_lookup(&l_found, i_ini, i_section, "highlight.background");
    if (l_value)
    {
        (*io_skin).m_highlight_bcolor= strtobcolor(l_value, (*io_skin).m_highlight_bcolor);
    }

    l_value= ini_lookup(&l_found, i_ini, i_section, "highlight.foreground");
    if (l_value)
    {
        (*io_skin).m_selected_fcolor= strtofcolor(l_value, (*io_skin).m_selected_fcolor);
    }

    l_value= ini_lookup(&l_found, i_ini, i_section, "selected.foreground");
    if (l_value)
    {
        (*io_skin).m_selected_fcolor= strtofcolor(l_value, (*io_skin).m_selected_fcolor);
    }

    l_value= ini_lookup(&l_found, i_ini, i_section, "selected.background");
    if (l_value)
    {
        (*io_skin).m_selected_bcolor= strtobcolor(l_value, (*io_skin).m_selected_bcolor);
    }

    return;
}

static void
edit_load(
    struct skin_edit*const              io_skin,
    struct ini *const                   i_ini,
    char const*                         i_section)
{
    int                                 l_found;
    char const *                        l_value;

    l_value= ini_lookup(&l_found, i_ini, i_section, "foreground");
    if (l_value)
    {
        (*io_skin).m_fcolor= strtofcolor(l_value, (*io_skin).m_fcolor);
    }

    l_value= ini_lookup(&l_found, i_ini, i_section, "background");
    if (l_value)
    {
        (*io_skin).m_bcolor= strtobcolor(l_value, (*io_skin).m_bcolor);
    }

    return;
}

static void
tree_load(
    struct skin_tree*const              io_skin,
    struct ini *const                   i_ini,
    char const*                         i_section)
{
    int                                 l_found;
    char const *                        l_value;

    l_value= ini_lookup(&l_found, i_ini, i_section, "foreground");
    if (l_value)
    {
        (*io_skin).m_fcolor= strtofcolor(l_value, (*io_skin).m_fcolor);
    }

    l_value= ini_lookup(&l_found, i_ini, i_section, "background");
    if (l_value)
    {
        (*io_skin).m_bcolor= strtobcolor(l_value, (*io_skin).m_bcolor);
    }

    l_value= ini_lookup(&l_found, i_ini, i_section, "selected.foreground");
    if (l_value)
    {
        (*io_skin).m_selected_fcolor= strtofcolor(l_value, (*io_skin).m_selected_fcolor);
    }

    l_value= ini_lookup(&l_found, i_ini, i_section, "selected.background");
    if (l_value)
    {
        (*io_skin).m_selected_bcolor= strtobcolor(l_value, (*io_skin).m_selected_bcolor);
    }

    return;
}

extern void
fdtuicfg_load_path(
    char const*                         i_path)
{
    int                                 l_exit;
    struct ini                          l_ini;

    do
    {

        l_exit= ini_assign(&l_ini);

        if (l_exit)
        {
            break;
        }

        l_exit= ini_parse(&l_ini, i_path, INI_OPTION_NONE);            

        if (l_exit)
        {
            break;
        }

        desktop_load(&_skin_desktop, &l_ini, "desktop");
        window_load(&_skin_window, &l_ini, "window");
        menu_load(&_skin_menu, &l_ini, "menu");
        button_load(&_skin_button, &l_ini, "button");
        label_load(&_skin_label, &l_ini, "label");
        slider_load(&_skin_slider, &l_ini, "slider");
        listbox_load(&_skin_listbox, &l_ini, "listbox");
        edit_load(&_skin_edit, &l_ini, "edit");
        tree_load(&_skin_tree, &l_ini, "tree");

    }while(0);

    ini_discharge(&l_ini);

    return;
}

extern void
fdtuicfg_load()
{
#if defined(__DOS__) || defined(__NT__)
    size_t                              l_length;
    char*                               l_ptr;
#endif
    char*                               l_path;
    int                                 l_rc;
    struct stat                         l_stat;

    l_path= 0;

    do
    {

        l_rc= stat("fdostui.ini", &l_stat);
        if (0 == l_rc)
        {
            fdtuicfg_load_path("fdostui.ini");
            break;
        }

        l_path= calloc(1, 1+PATH_MAX);

        if (0 == l_path)
        {
            break;
        }

#if defined(__NT__)
       GetModuleFileNameA(0, l_path, PATH_MAX);
#define TEST_MODULE
#elif defined(__DOS__)
       strlcpy(l_path, __argv[0], PATH_MAX);
#define TEST_MODULE
#endif

#if defined(TEST_MODULE)
        l_length= strlen(l_path);
        l_ptr= &l_path[l_length];
        while(l_length)
        {
            if ('\\' == (*l_ptr))
            {
                break;
            }
            if (':' == (*l_ptr))
            {
                l_ptr++;
                break;
            }
            l_ptr--;
            l_length--;
        }
        strcpy(l_ptr, "\\fdostui.ini");

        l_rc= stat(l_path, &l_stat);
        if (0 == l_rc)
        {
            fdtuicfg_load_path(l_path);
            break;
        }
#endif

#if defined(__NCURSES__)
        l_ptr= getenv("XDG_CONFIG_HOME");
        if (l_ptr)
        {
            snprintf(l_path, PATH_MAX, "%s/fdostui.ini", l_ptr);
        }
        else
        {
            l_ptr= getenv("HOME");
            if (0 == l_ptr)
            {
                break;
            }
            snprintf(l_path, PATH_MAX, "%s/.config/fdostui.ini", l_ptr);
        }

        l_rc= stat(l_path, &l_stat);
        if (0 == l_rc)
        {
            fdtuicfg_load_path(l_path);
            break;
        }
#endif

    }while(0);

    free(l_path);

    return;
}