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 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
|
/*
* $Id$
*/
#include "x_picture.h"
#include <kiklib/kik_debug.h>
#include <kiklib/kik_str.h> /* strdup */
#include <kiklib/kik_mem.h> /* malloc */
#include <kiklib/kik_util.h> /* K_MIN */
#include "x_imagelib.h"
#if 0
#define __DEBUG
#endif
/* --- static varaibles --- */
static x_picture_t ** pics ;
static u_int num_of_pics ;
static x_icon_picture_t ** icon_pics ;
static u_int num_of_icon_pics ;
/* --- static functions --- */
static x_picture_t *
create_picture_intern(
Display * display ,
x_picture_modifier_t * mod ,
char * file_path ,
u_int width ,
u_int height
)
{
x_picture_t * pic ;
if( ( pic = malloc( sizeof( x_picture_t))) == NULL)
{
return NULL ;
}
if( mod)
{
if( ( pic->mod = malloc( sizeof( x_picture_modifier_t))) == NULL)
{
goto error1 ;
}
*pic->mod = *mod ;
}
else
{
pic->mod = NULL ;
}
if( ( pic->file_path = strdup( file_path)) == NULL)
{
goto error2 ;
}
pic->display = display ;
pic->width = width ;
pic->height = height ;
return pic ;
error2:
free( pic->mod) ;
error1:
free( pic) ;
return NULL ;
}
static int
delete_picture_intern(
x_picture_t * pic
)
{
free( pic->file_path) ;
free( pic->mod) ;
free( pic) ;
return 1 ;
}
static x_picture_t *
create_bg_picture(
x_window_t * win ,
x_picture_modifier_t * mod ,
char * file_path
)
{
x_picture_t * pic ;
if( ! ( pic = create_picture_intern( win->disp->display , mod , file_path ,
ACTUAL_WIDTH(win) , ACTUAL_HEIGHT(win))))
{
return NULL ;
}
if( strcmp( file_path , "root") == 0)
{
pic->pixmap = x_imagelib_get_transparent_background( win , mod) ;
}
else
{
pic->pixmap = x_imagelib_load_file_for_background( win , file_path , mod) ;
}
if( pic->pixmap == None)
{
delete_picture_intern( pic) ;
return NULL ;
}
#ifdef DEBUG
kik_debug_printf( KIK_DEBUG_TAG " New pixmap %ul is created.\n" , pic->pixmap) ;
#endif
pic->ref_count = 1 ;
return pic ;
}
#ifdef ENABLE_SIXEL
static x_picture_t *
create_picture(
x_display_t * disp ,
x_picture_modifier_t * mod ,
char * file_path ,
u_int width ,
u_int height
)
{
x_picture_t * pic ;
if( ! ( pic = create_picture_intern( disp->display , mod , file_path , width , height)))
{
return NULL ;
}
if( ! x_imagelib_load_file( disp , file_path ,
NULL , &(pic->pixmap) , NULL , &(pic->width) , &(pic->height)))
{
delete_picture_intern( pic) ;
return NULL ;
}
pic->ref_count = 1 ;
#ifdef DEBUG
kik_debug_printf( KIK_DEBUG_TAG " New pixmap %ul is created.\n" , pic->pixmap) ;
#endif
return pic ;
}
#endif /* ENABLE_SIXEL */
static x_picture_t *
acquire_picture(
x_window_t * win , /* acquire bg picture => not-NULL */
x_display_t * disp ,
x_picture_modifier_t * mod ,
char * file_path ,
u_int width ,
u_int height
)
{
u_int count ;
x_picture_t ** p ;
if( ! win || strcmp( file_path , "root") != 0) /* Transparent background is not cached. */
{
for( count = 0 ; count < num_of_pics ; count++)
{
if( strcmp( file_path , pics[count]->file_path) == 0 &&
disp->display == pics[count]->display &&
x_picture_modifiers_equal( mod , pics[count]->mod) &&
width == pics[count]->width &&
height == pics[count]->height)
{
#ifdef DEBUG
kik_debug_printf( KIK_DEBUG_TAG " Use cached picture(%s).\n" ,
file_path) ;
#endif
pics[count]->ref_count ++ ;
return pics[count] ;
}
}
}
if( ( p = realloc( pics , ( num_of_pics + 1) * sizeof( *pics))) == NULL)
{
return NULL ;
}
pics = p ;
if( win)
{
pics[num_of_pics] = create_bg_picture( win , mod , file_path) ;
}
#ifdef ENABLE_SIXEL
else
{
pics[num_of_pics] = create_picture( disp , NULL , file_path , width , height) ;
}
#endif /* ENABLE_SIXEL */
if( ! pics[num_of_pics])
{
if( num_of_pics == 0 /* pics == NULL */)
{
free( pics) ;
pics = NULL ;
}
return NULL ;
}
return pics[num_of_pics++] ;
}
static int
delete_picture(
x_picture_t * pic
)
{
/* XXX Pixmap of "pixmap:<ID>" is managed by others, so don't free here. */
if( strncmp( pic->file_path , "pixmap:" , 7) != 0)
{
x_delete_image( pic->display , pic->pixmap) ;
}
delete_picture_intern( pic) ;
#ifdef DEBUG
kik_debug_printf( KIK_DEBUG_TAG " pixmap is deleted.\n") ;
#endif
return 1 ;
}
static x_icon_picture_t *
create_icon_picture(
x_display_t * disp ,
char * file_path /* Don't specify NULL. */
)
{
int icon_size = 48 ;
x_icon_picture_t * pic ;
if( ( pic = malloc( sizeof( x_icon_picture_t))) == NULL)
{
return NULL ;
}
if( ( pic->file_path = strdup( file_path)) == NULL)
{
free( pic->file_path) ;
return NULL ;
}
if( ! x_imagelib_load_file( disp , file_path ,
&(pic->cardinal) , &(pic->pixmap) , &(pic->mask) , &icon_size , &icon_size))
{
free( pic->file_path) ;
free( pic) ;
#ifdef DEBUG
kik_debug_printf( KIK_DEBUG_TAG " Failed to load icon file(%s).\n" , file_path) ;
#endif
return NULL ;
}
pic->disp = disp ;
pic->ref_count = 1 ;
#if 0
kik_debug_printf( KIK_DEBUG_TAG " Successfully loaded icon file %s.\n" , file_path) ;
#endif
return pic ;
}
static int
delete_icon_picture(
x_icon_picture_t * pic
)
{
#ifdef __DEBUG
kik_debug_printf( KIK_DEBUG_TAG " %s icon will be deleted.\n" , pic->file_path) ;
#endif
x_delete_image( pic->disp->display , pic->pixmap) ;
free( pic->cardinal) ;
free( pic->file_path) ;
free( pic) ;
return 1 ;
}
/* --- global functions --- */
int
x_picture_display_opened(
Display * display
)
{
if( ! x_imagelib_display_opened( display))
{
return 0 ;
}
return 1 ;
}
int
x_picture_display_closed(
Display * display
)
{
if( num_of_icon_pics > 0)
{
int count ;
for( count = num_of_icon_pics - 1 ; count >= 0 ; count--)
{
if( icon_pics[count]->disp->display == display)
{
delete_icon_picture( icon_pics[count]) ;
icon_pics[count] = icon_pics[--num_of_icon_pics] ;
}
}
if( num_of_icon_pics == 0)
{
#ifdef DEBUG
kik_debug_printf( KIK_DEBUG_TAG " All cached icons were free'ed\n") ;
#endif
free( icon_pics) ;
icon_pics = NULL ;
}
}
return x_imagelib_display_closed( display) ;
}
/*
* Judge whether pic_mods are equal or not.
* \param a,b picture modifier
* \return 1 when they are same. 0 when not.
*/
int
x_picture_modifiers_equal(
x_picture_modifier_t * a , /* Can be NULL (which means normal pic_mod) */
x_picture_modifier_t * b /* Can be NULL (which means normal pic_mod) */
)
{
if( a == b)
{
/* If a==NULL and b==NULL, return 1 */
return 1 ;
}
if( a == NULL)
{
a = b ;
b = NULL ;
}
if( b == NULL)
{
/* Check if 'a' is normal or not. */
if( (a->brightness == 100) &&
(a->contrast == 100) &&
(a->gamma == 100) &&
(a->alpha == 0))
{
return 1 ;
}
}
else
{
if( (a->brightness == b->brightness) &&
(a->contrast == b->contrast) &&
(a->gamma == b->gamma) &&
(a->alpha == b->alpha) &&
(a->blend_red == b->blend_red) &&
(a->blend_green == b->blend_green) &&
(a->blend_blue == b->blend_blue))
{
return 1 ;
}
}
return 0 ;
}
x_picture_t *
x_acquire_bg_picture(
x_window_t * win ,
x_picture_modifier_t * mod ,
char * file_path /* "root" means transparency. */
)
{
return acquire_picture( win , win->disp , mod , file_path , 0 , 0) ;
}
#ifdef ENABLE_SIXEL
x_picture_t *
x_acquire_picture(
x_display_t * disp ,
char * file_path ,
u_int width ,
u_int height
)
{
return acquire_picture( NULL , disp , NULL , file_path , width , height) ;
}
#endif /* ENABLE_SIXEL */
int
x_release_picture(
x_picture_t * pic
)
{
u_int count ;
for( count = 0 ; count < num_of_pics ; count++)
{
if( pic == pics[count])
{
if( -- (pic->ref_count) == 0)
{
delete_picture( pic) ;
if( --num_of_pics == 0)
{
#ifdef DEBUG
kik_debug_printf( KIK_DEBUG_TAG
" All cached bg pictures were free'ed\n") ;
#endif
free( pics) ;
pics = NULL ;
}
else
{
pics[count] = pics[num_of_pics] ;
}
}
return 1 ;
}
}
return 0 ;
}
x_icon_picture_t *
x_acquire_icon_picture(
x_display_t * disp ,
char * file_path /* Don't specify NULL. */
)
{
u_int count ;
x_icon_picture_t ** p ;
for( count = 0 ; count < num_of_icon_pics ; count++)
{
if( strcmp( file_path , icon_pics[count]->file_path) == 0 &&
disp == icon_pics[count]->disp)
{
#ifdef __DEBUG
kik_debug_printf( KIK_DEBUG_TAG " Use cached icon(%s).\n" , file_path) ;
#endif
icon_pics[count]->ref_count ++ ;
return icon_pics[count] ;
}
}
if( ( p = realloc( icon_pics , ( num_of_icon_pics + 1) * sizeof( *icon_pics))) == NULL)
{
return NULL ;
}
icon_pics = p ;
if( ( icon_pics[num_of_icon_pics] = create_icon_picture( disp , file_path)) == NULL)
{
if( num_of_icon_pics == 0 /* icon_pics == NULL */)
{
free( icon_pics) ;
icon_pics = NULL ;
}
return NULL ;
}
return icon_pics[num_of_icon_pics++] ;
}
int
x_release_icon_picture(
x_icon_picture_t * pic
)
{
u_int count ;
for( count = 0 ; count < num_of_icon_pics ; count++)
{
if( pic == icon_pics[count])
{
if( -- (pic->ref_count) == 0)
{
delete_icon_picture( pic) ;
if( --num_of_icon_pics == 0)
{
#ifdef DEBUG
kik_debug_printf( KIK_DEBUG_TAG
" All cached icons were free'ed\n") ;
#endif
free( icon_pics) ;
icon_pics = NULL ;
}
else
{
icon_pics[count] = icon_pics[num_of_icon_pics] ;
}
}
return 1 ;
}
}
return 0 ;
}
#ifdef ENABLE_SIXEL
x_picture_manager_t *
x_picture_manager_new(void)
{
return calloc( 1 , sizeof( x_picture_manager_t)) ;
}
int
x_picture_manager_delete(
x_picture_manager_t * pic_man
)
{
u_int count ;
for( count = 0 ; count < pic_man->num_of_pics ; count++)
{
x_release_picture( pic_man->pics[count].pic) ;
}
free( pic_man->pics) ;
free( pic_man) ;
return 1 ;
}
int
x_picture_manager_add(
x_picture_manager_t * pic_man ,
x_picture_t * pic ,
int x ,
int y
)
{
void * p ;
if( ! ( p = realloc( pic_man->pics ,
sizeof(*pic_man->pics) * (pic_man->num_of_pics + 1))))
{
return 0 ;
}
pic_man->pics = p ;
pic_man->pics[pic_man->num_of_pics].x = x ;
pic_man->pics[pic_man->num_of_pics].y = y ;
pic_man->pics[pic_man->num_of_pics].pic = pic ;
pic_man->num_of_pics ++ ;
return 1 ;
}
int
x_picture_manager_remove(
x_picture_manager_t * pic_man ,
int x ,
int y ,
int width ,
int height
)
{
int count ;
for( count = pic_man->num_of_pics - 1 ; count >= 0 ; count--)
{
if( x <= pic_man->pics[count].x + pic_man->pics[count].pic->width &&
pic_man->pics[count].x <= x + width &&
y <= pic_man->pics[count].y + pic_man->pics[count].pic->height &&
pic_man->pics[count].y <= y + height)
{
x_release_picture( pic_man->pics[count].pic) ;
if( -- pic_man->num_of_pics == 0)
{
return 1 ;
}
pic_man->pics[count] = pic_man->pics[pic_man->num_of_pics] ;
}
}
return 1 ;
}
int
x_picture_manager_redraw(
x_picture_manager_t * pic_man ,
x_window_t * win ,
int x ,
int y ,
u_int width ,
u_int height
)
{
u_int count ;
for( count = 0 ; count < pic_man->num_of_pics ; count++)
{
int dst_x ;
int dst_y ;
int src_x ;
int src_y ;
u_int src_width ;
u_int src_height ;
if( x > pic_man->pics[count].x)
{
dst_x = x ;
src_x = x - pic_man->pics[count].x ;
if( src_x >= pic_man->pics[count].pic->width)
{
continue ;
}
src_width = K_MIN(width,pic_man->pics[count].pic->width - src_x) ;
}
else
{
dst_x = pic_man->pics[count].x ;
src_x = 0 ;
src_width = K_MIN(width + x - dst_x,pic_man->pics[count].pic->width) ;
}
if( y > pic_man->pics[count].y)
{
dst_y = y ;
src_y = y - pic_man->pics[count].y ;
if( src_y >= pic_man->pics[count].pic->height)
{
continue ;
}
src_height = K_MIN(height,pic_man->pics[count].pic->height - src_y) ;
}
else
{
dst_y = pic_man->pics[count].y ;
src_y = 0 ;
src_height = K_MIN(height + y - dst_y,pic_man->pics[count].pic->height) ;
}
x_window_copy_area( win , pic_man->pics[count].pic->pixmap ,
src_x , src_y , src_width , src_height , dst_x , dst_y) ;
}
return 1 ;
}
int
x_picture_manager_scroll(
x_picture_manager_t * pic_man ,
int beg_y ,
int end_y ,
int height /* < 0 => upward, > 0 => downward */
)
{
int count ;
if( pic_man->num_of_pics == 0)
{
return 1 ;
}
for( count = pic_man->num_of_pics - 1 ; count >= 0 ; count--)
{
if( beg_y < pic_man->pics[count].y + pic_man->pics[count].pic->height &&
pic_man->pics[count].y < end_y)
{
pic_man->pics[count].y += height ;
if( pic_man->pics[count].y + (int)pic_man->pics[count].pic->height <= 0)
{
x_release_picture( pic_man->pics[count].pic) ;
if( -- pic_man->num_of_pics == 0)
{
return 1 ;
}
pic_man->pics[count] = pic_man->pics[pic_man->num_of_pics] ;
}
}
}
return 1 ;
}
#endif /* ENABLE_SIXEL */
|