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 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
|
#include <stdio.h>
#include <gtk/gtk.h>
#include "jc.h"
static js_axis_struct *JCGetAxis(js_data_struct *jsd, gint i);
static void JCDrawAxisesRepresentative(jc_struct *jc);
static void JCDrawAxisesLogical(jc_struct *jc);
void JCDrawAxises(jc_struct *jc);
void JCDrawButtons(jc_struct *jc);
void JCDrawSignalLevel(jc_struct *jc);
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
/*
* Returns the axis structure i from the jsd structure.
*
* Can return NULL if axis number i is invalid or on error.
*/
static js_axis_struct *JCGetAxis(js_data_struct *jsd, gint i)
{
if(jsd == NULL)
return(NULL);
else if((i < 0) || (i >= jsd->total_axises))
return(NULL);
else
return(jsd->axis[i]);
}
/*
* Redraws the axises layout on the given jc
* for the representative layout. The given inputs are
* assumed valid and the layout is assumed to be representative.
*/
static void JCDrawAxisesRepresentative(jc_struct *jc)
{
gint x, y, width, height;
GtkWidget *w;
GdkPixmap *pixmap;
GdkGC *gc;
layout_rep_struct *lr;
js_axis_struct *axis_ptr1, *axis_ptr2;
gfloat axis0_value, axis1_value, axis2_value, axis3_value,
axis4_value, axis5_value;
int total_axises;
lr = &jc->layout_rep;
/* Not initialized? */
if(lr->toplevel == NULL)
return;
gc = jc->gc;
if(gc == NULL)
return;
/* Get first four axis values (set value 0 if they don't
* exist.
*/
total_axises = jc->jsd.total_axises;
axis0_value = JSGetAxisCoeff(&jc->jsd, 0);
axis1_value = JSGetAxisCoeff(&jc->jsd, 1);
axis2_value = JSGetAxisCoeff(&jc->jsd, 2); /* Rotate. */
axis3_value = JSGetAxisCoeff(&jc->jsd, 3); /* Throttle. */
axis4_value = JSGetAxisCoeff(&jc->jsd, 4);
axis5_value = JSGetAxisCoeff(&jc->jsd, 5);
/* Hat. */
w = lr->hat_da;
pixmap = lr->hat_buf;
gdk_window_get_size(pixmap, &width, &height);
/* Draw solid background, this `clearing' the pixmap. */
gdk_gc_set_foreground(gc, &lr->c_axis_bg);
gdk_draw_rectangle(
pixmap, gc,
TRUE,
0, 0,
width, height
);
/* Draw grids. */
x = (width / 2);
y = (height / 2);
gdk_gc_set_foreground(gc, &lr->c_axis_grid);
gdk_draw_line(
pixmap, gc,
x, 0,
x, height - 1
);
gdk_draw_line(
pixmap, gc,
0, y,
width - 1, y
);
/* Draw hat, determine the hat axis value by the total number
* of axises.
*/
if(total_axises >= 6)
{
x = (axis4_value * (width / 4)) + (width / 2);
y = (gfloat)height - (
(axis5_value * (height / 4)) + (height / 2)
);
}
else if(total_axises == 5)
{
x = (axis3_value * (width / 4)) + (width / 2);
y = (gfloat)height - (
(axis4_value * (height / 4)) + (height / 2)
);
}
else
{
x = (width / 2);
y = height - (height / 2);
}
/* Draw a solid circle, representing the hat. */
gdk_gc_set_foreground(gc, &lr->c_axis_hat);
gdk_draw_arc(
pixmap, gc,
TRUE,
x - 10, y - 10,
20, 20,
0 * 64, 360 * 64
);
/* Put the entire drawn pixmap to the window. */
gdk_draw_pixmap(
w->window, gc, pixmap,
0, 0,
0, 0,
width, height
);
/* First two axises. */
w = lr->axis_da;
pixmap = lr->axis_buf;
gdk_window_get_size(pixmap, &width, &height);
/* Draw solid background, this `clearing' the pixmap. */
gdk_gc_set_foreground(gc, &lr->c_axis_bg);
gdk_draw_rectangle(
pixmap, gc,
TRUE,
0, 0,
width, height
);
/* Null zone circle and grids. */
x = (width / 2);
y = (height / 2);
gdk_gc_set_foreground(gc, &lr->c_axis_grid);
axis_ptr1 = JCGetAxis(&jc->jsd, 0);
axis_ptr2 = JCGetAxis(&jc->jsd, 1);
if((axis_ptr1 != NULL) &&
(axis_ptr2 != NULL)
)
{
gint axis_len1, win_len1;
gint axis_len2, win_len2;
axis_len1 = MAX(axis_ptr1->max - axis_ptr1->min, 1);
axis_len2 = MAX(axis_ptr2->max - axis_ptr2->min, 1);
win_len1 = (gfloat)width * (
(gfloat)(2 * axis_ptr1->nz) / (gfloat)axis_len1
);
win_len2 = (gfloat)height * (
(gfloat)(2 * axis_ptr2->nz) / (gfloat)axis_len2
);
gdk_draw_arc(
pixmap, gc,
FALSE,
x - (win_len1 / 2), y - (win_len2 / 2),
win_len1, win_len2,
0 * 64, 360 * 64
);
}
gdk_draw_line(
pixmap, gc,
x, 0,
x, height - 1
);
gdk_draw_line(
pixmap, gc,
0, y,
width - 1, y
);
/* Cross hairs. */
x = (axis0_value * (gfloat)width / 2) + ((gfloat)width / 2);
y = (gfloat)height - (gfloat)(
(axis1_value * (gfloat)height / 2) + ((gfloat)height / 2)
);
gdk_gc_set_foreground(gc, &lr->c_axis_fg);
gdk_draw_line(
pixmap, gc,
x - 5, y, x + 6, y
);
gdk_draw_line(
pixmap, gc,
x, y - 5, x, y + 6
);
/* Put the entire drawn pixmap to the window. */
gdk_draw_pixmap(
w->window,
gc,
pixmap,
0, 0,
0, 0,
width, height
);
/* Throttle. */
w = lr->throttle_da;
pixmap = lr->throttle_buf;
gdk_window_get_size(pixmap, &width, &height);
/* Draw solid background, this `clearing' the pixmap. */
gdk_gc_set_foreground(gc, &lr->c_axis_bg);
gdk_draw_rectangle(
pixmap, gc,
TRUE,
0, 0,
width, height
);
/* Get throttle axis number based on total axises. */
if((total_axises >= 6) || (total_axises == 4))
{
y = height - (
(axis3_value * height / 2) + (height / 2)
);
}
else if((total_axises == 5) || (total_axises == 3))
{
y = height - (
(axis2_value * height / 2) + (height / 2)
);
}
else
{
y = height - 0;
}
/* Draw throttle. */
gdk_gc_set_foreground(gc, &lr->c_throttle_fg);
gdk_draw_rectangle(
pixmap, gc,
TRUE,
0, y,
width, height - y
);
/* Put the entire drawn pixmap to the window. */
gdk_draw_pixmap(
w->window, gc, pixmap,
0, 0,
0, 0,
width, height
);
/* Rotate. */
w = lr->rotate_da;
pixmap = lr->rotate_buf;
gdk_window_get_size(pixmap, &width, &height);
/* Draw solid background, this `clearing' the pixmap. */
gdk_gc_set_foreground(gc, &lr->c_axis_bg);
gdk_draw_rectangle(
pixmap, gc,
TRUE,
0, 0,
width, height
);
/* Draw grids and null zone bounds. */
gdk_gc_set_foreground(gc, &lr->c_axis_grid);
x = width / 2;
axis_ptr1 = JCGetAxis(&jc->jsd, 2);
if(axis_ptr1 != NULL)
{
gint axis_len1, win_len1;
axis_len1 = MAX(axis_ptr1->max - axis_ptr1->min, 1);
win_len1 = (gfloat)width * (
(gfloat)(2 * axis_ptr1->nz) / (gfloat)axis_len1
);
gdk_draw_line(
pixmap, gc,
x - (win_len1 / 2), 1,
x - (win_len1 / 2), height - 1
);
gdk_draw_line(
pixmap, gc,
x + (win_len1 / 2), 1,
x + (win_len1 / 2), height - 1
);
}
gdk_draw_line(
pixmap, gc,
x, 1,
x, height - 1
);
/* Get rotate axis value by the total number of axises. */
if((total_axises >= 6) || (total_axises == 4))
{
x = (axis2_value * width / 2) + (width / 2);
}
else
{
x = width / 2;
}
gdk_gc_set_foreground(gc, &lr->c_axis_fg);
gdk_draw_line(
pixmap, gc,
x, 1,
x, height - 1
);
/* Put the entire drawn pixmap to the window. */
gdk_draw_pixmap(
w->window, gc, pixmap,
0, 0,
0, 0,
width, height
);
}
/*
* Redraws the axises layout on the given jc
* for the logical layout. The given inputs are
* assumed valid and the layout is assumed to be logical.
*/
static void JCDrawAxisesLogical(jc_struct *jc)
{
gint i, x, width, height, center_w;
gint border_minor = 2;
gfloat rtow_coeff, axis_coeff_value;
GtkWidget *w;
GdkPixmap *pixmap;
GdkGC *gc;
GdkFont *font;
layout_logical_struct *ll;
layout_logical_axis_struct *ll_axis_ptr;
js_axis_struct *axis_ptr;
ll = &jc->layout_logical;
/* Not initialized? */
if(ll->toplevel == NULL)
return;
gc = jc->gc;
if(gc == NULL)
return;
/* Itterate through each axis. */
for(i = 0; i < ll->total_axises; i++)
{
ll_axis_ptr = ll->axis[i];
if(ll_axis_ptr == NULL)
continue;
/* Skip uninitialized ones. */
if(!ll_axis_ptr->initialized)
continue;
if((ll_axis_ptr->position_buf == NULL) ||
(ll_axis_ptr->position_da == NULL)
)
continue;
/* Get axis value coefficient. */
axis_coeff_value = JSGetAxisCoeff(&jc->jsd, i);
/* Get drawing area widget and pixmap buffer. */
w = ll_axis_ptr->position_da;
pixmap = ll_axis_ptr->position_buf;
/* Get size of pixmap buffer. */
gdk_window_get_size(pixmap, &width, &height);
font = ll_axis_ptr->font;
/* Begin drawing on the pixmap buffer. */
/* Draw a solid rectangle to `clear' the pixmap buffer. */
gdk_gc_set_foreground(gc, &ll_axis_ptr->c_axis_bg);
gdk_draw_rectangle(
pixmap, gc,
TRUE,
0, 0,
width, height
);
/* Calculate center position in window coordinates. */
center_w = width / 2;
/* Get pointer to axis. */
axis_ptr = JCGetAxis(&jc->jsd, i);
if(axis_ptr != NULL)
{
gbool flip;
gint n, axis_range, axis_range_w, nz_len_w;
GdkPoint p[3];
/* Get flip flag. */
flip = (axis_ptr->flags & JSAxisFlagFlipped) ? TRUE : FALSE;
/* Get full range of axis in raw units, this value
* is gauranteed to be positive.
*/
axis_range = MAX(axis_ptr->max - axis_ptr->min, 1);
/* Calculate axis range in window coordinates, this
* will be the width minus margins on both sides.
*/
axis_range_w = width - (2 * border_minor);
/* Calculate horizontal raw units to window units
* coefficient relative to axis_range_w, not width.
*/
rtow_coeff = (gfloat)axis_range_w / (gfloat)axis_range;
/* Get length of null zone in window units. */
nz_len_w = (2 * axis_ptr->nz) * rtow_coeff;
/* Draw null zone region. */
gdk_gc_set_foreground(gc, &ll_axis_ptr->c_axis_nz);
gdk_draw_rectangle(
pixmap, gc,
TRUE,
center_w - (nz_len_w / 2), 0,
nz_len_w, height
);
/* Begin drawing grid and ticks. */
gdk_gc_set_foreground(gc, &ll_axis_ptr->c_axis_grid);
/* Vertical center line. */
gdk_draw_line(
pixmap, gc,
center_w, height - 5 - border_minor,
center_w, height - 1 - border_minor
);
/* Vertical ticks at every 25% to the left. */
for(n = 0; n < 3; n++)
{
x = center_w - (axis_range_w / 8) - (n * axis_range_w / 8);
gdk_draw_line(
pixmap, gc,
x, height - 5 - border_minor,
x, height - 1 - border_minor
);
}
/* Left most vertical tick. */
x = border_minor;
gdk_draw_line(
pixmap, gc,
x, height - 5 - border_minor,
x, height - 1 - border_minor
);
/* Vertical ticks at every 25% to the right. */
for(n = 0; n < 3; n++)
{
x = center_w + (axis_range_w / 8) + (n * axis_range_w / 8);
gdk_draw_line(
pixmap, gc,
x, height - 5 - border_minor,
x, height - 1 - border_minor
);
}
/* Right most vertical tick. */
x = width - 1 - border_minor;
gdk_draw_line(
pixmap, gc,
x, height - 5 - border_minor,
x, height - 1 - border_minor
);
/* Long horizontal grid. */
gdk_draw_line(
pixmap, gc,
border_minor, height - 1 - border_minor,
width - border_minor, height - 1 - border_minor
);
/* Draw dead zone bars, only if correction level is above
* level 0.
*/
if(axis_ptr->correction_level > 0)
{
int dz_min = MIN(axis_ptr->dz_min - axis_ptr->cen, 0),
dz_max = MAX(axis_ptr->dz_max - axis_ptr->cen, 0);
int dz_min_w = dz_min * rtow_coeff,
dz_max_w = dz_max * rtow_coeff;
/* Draw dead zone edge lines. */
gdk_gc_set_foreground(gc, &ll_axis_ptr->c_axis_fg);
x = center_w + (flip ? -dz_min_w : dz_min_w);
if(dz_min < 0)
{
gdk_draw_line(
pixmap, gc,
x, height - 5 - border_minor,
x, height - 1 - border_minor
);
gdk_draw_line(
pixmap, gc,
x, height - 2 - border_minor,
center_w, height - 2 - border_minor
);
}
x = center_w + (flip ? -dz_max_w : dz_max_w);
if(dz_max > 0)
{
gdk_draw_line(
pixmap, gc,
x, height - 5 - border_minor,
x, height - 1 - border_minor
);
gdk_draw_line(
pixmap, gc,
center_w, height - 2 - border_minor,
x, height - 2 - border_minor
);
}
}
/* Calculate current axis position in window coordinates
* and set up to draw current axis position arrow.
*/
x = (axis_coeff_value * (gfloat)axis_range_w / 2) +
center_w;
/* Calculate arrow points, check if current axis
* position x in window coordinates is in bounds or
* not.
*/
if(x < border_minor)
{
/* At or beyond the left edge, draw an arrow pointing
* to the left.
*/
x = 0;
p[0].x = x + 4;
p[0].y = height - 11 - border_minor;
p[1].x = x;
p[1].y = height - 8 - border_minor;
p[2].x = x + 4;
p[2].y = height - 5 - border_minor;
}
else if(x > (width - 1 - border_minor))
{
/* At or beyond the right edge, draw an arrow pointing
* to the right.
*/
x = width - 1;
p[0].x = x - 4;
p[0].y = height - 11 - border_minor;
p[1].x = x;
p[1].y = height - 8 - border_minor;
p[2].x = x - 4;
p[2].y = height - 5 - border_minor;
}
else
{
/* In bounds, draw arrow pointing down. */
p[0].x = x - 3;
p[0].y = height - 9 - border_minor;
p[1].x = x + 4;
p[1].y = height - 9 - border_minor;
p[2].x = x;
p[2].y = height - 5 - border_minor;
}
/* Draw the arrow as a polygon. */
gdk_gc_set_foreground(gc, &ll_axis_ptr->c_axis_fg);
gdk_draw_polygon(
pixmap, gc,
TRUE,
p, 3
);
/* Draw text. */
if((axis_ptr != NULL) && (font != NULL))
{
gint f_lbearing, f_rbearing;
gint f_width, f_ascent, f_descent;
gchar text[256];
sprintf(text, "%i", axis_ptr->cur);
gdk_string_extents(
font, text,
&f_lbearing, &f_rbearing,
&f_width, &f_ascent, &f_descent
);
gdk_draw_string(
pixmap, font, gc,
(axis_coeff_value > -0.25) ?
border_minor + f_lbearing :
width - f_width - border_minor + f_lbearing,
border_minor + f_ascent,
text
);
}
}
/* Put the entire drawn pixmap to the window. */
gdk_draw_pixmap(
w->window, gc, pixmap,
0, 0,
0, 0,
width, height
);
}
}
/*
* Redraws the axises layout on the given jc depending on its current
* layout style.
*/
void JCDrawAxises(jc_struct *jc)
{
if(jc == NULL)
return;
if(jc->toplevel == NULL)
return;
/* Handle by currently selected axis layout page. */
switch(jc->layout_state)
{
case JC_LAYOUT_REPRESENTATIVE:
JCDrawAxisesRepresentative(jc);
break;
case JC_LAYOUT_LOGICAL:
JCDrawAxisesLogical(jc);
break;
}
}
/*
* Redraw button labels.
*/
void JCDrawButtons(jc_struct *jc)
{
gint i, total;
buttons_list_struct *bl;
js_button_struct *button_ptr;
GtkWidget *w;
if(jc == NULL)
return;
/* Get buttons list from jc structure. */
bl = &jc->buttons_list;
/* Get total button labels. */
total = bl->total_buttons;
/* Make sure that total for button labels does not exceed the
* total number of buttons on the joystick data structure.
*/
if(total > jc->jsd.total_buttons)
total = jc->jsd.total_buttons;
/* Itterate through each button. */
for(i = 0; i < total; i++)
{
w = bl->button[i];
if(w == NULL)
continue;
button_ptr = jc->jsd.button[i];
if(button_ptr == NULL)
continue;
/* Set new button state, thus `redrawing it'. */
GTK_BUTTON(w)->in_button = (button_ptr->state == JSButtonStateOn) ?
1 : 0;
gtk_signal_emit_by_name(
GTK_OBJECT(w),
(button_ptr->state == JSButtonStateOn) ?
"pressed" : "released"
);
}
}
/*
* Redraw signal level.
*/
void JCDrawSignalLevel(jc_struct *jc)
{
gint i, width, height;
u_int16_t *history;
GtkWidget *w;
GdkPixmap *pixmap;
GdkGC *gc;
if(jc == NULL)
return;
/* Get references to the graphics context, drawing area and
* GdkPixmap buffer.
*/
gc = jc->gc;
w = jc->signal_level_da;
pixmap = jc->signal_level_pixmap;
if((gc == NULL) || (w == NULL) || (pixmap == NULL))
return;
/* Get size of the GdkPixmap buffer. */
gdk_window_get_size(pixmap, &width, &height);
if((width < 1) || (height < 1))
return;
/* Draw background, thus `clearing' the pixmap. */
gdk_gc_set_foreground(gc, &jc->c_signal_level_bg);
gdk_draw_rectangle(
pixmap, gc,
TRUE,
0, 0,
width, height
);
/* Begin drawing each vertical bar representing the signal value. */
gdk_gc_set_foreground(gc, &jc->c_signal_level_fg);
history = jc->signal_history;
for(i = 0; i < JC_SIGNAL_HISTORY_MAX; i++)
{
gdk_draw_line(
pixmap, gc,
i,
height - (
height * ((gfloat)history[i] /
(gfloat)((u_int16_t)-1)
)
) - 1,
i,
height - 1
);
}
/* Put entire drawn pixmap to window. */
gdk_draw_pixmap(
w->window, gc,
pixmap,
0, 0,
0, 0,
width, height
);
}
|