summaryrefslogtreecommitdiff
path: root/util.c
blob: 7110232abfaa7321ec52b49cd877b7fa7d85d950 (plain)
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
/**
 *
 * Compiz Nintendo(R) Wii(TM) Remote Interface Plugin
 *
 * Copyright : (C) 2008 by Sam Spilsbury
 * E-mail    : smspillaz@gmail.com
 *
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include "compiz-wiimote.h"


/* Utility Functions --------------------------------------------------- */

/* Error Handling */
void err(cwiid_wiimote_t *wiimote, const char *s, va_list ap)
{
	if (wiimote) printf("%d:", cwiid_get_id(wiimote));
	else printf("-1:");
	vprintf(s, ap);
	printf("\n");
}

/* Set LED Wrapper */
void set_led_state(cwiid_wiimote_t *wiimote, unsigned char led_state)
{
	if (cwiid_set_led(wiimote, led_state)) {
		fprintf(stderr, "Error setting LEDs \n");
	}
}

/* Set Reporting Mode Wrapper*/
void set_rpt_mode(cwiid_wiimote_t *wiimote, unsigned char rpt_mode)
{
	if (cwiid_set_rpt_mode(wiimote, rpt_mode)) {
		fprintf(stderr, "Error setting report mode\n");
	}
}

/* Wii Remote Light Show */

Bool wiimoteChangeLights(void *closure)
{
    CompWiimote *cwiimote = (CompWiimote *) closure;
    cwiid_wiimote_t *wiimote = cwiimote->wiimote;
    int led_state = 0;
    cwiimote->count++;
    switch (cwiimote->count)
    {
        case 1:
            {
                toggle_bit(led_state, CWIID_LED1_ON);
	            set_led_state(wiimote, led_state);
	        }
	        break;
	    case 2:
            {
                toggle_bit(led_state, CWIID_LED2_ON);
	            set_led_state(wiimote, led_state);
	        }
	        break;
	    case 3:
            {
                toggle_bit(led_state, CWIID_LED3_ON);
	            set_led_state(wiimote, led_state);
	        }
	        break;
	    case 4:
            {
                toggle_bit(led_state, CWIID_LED4_ON);
	            set_led_state(wiimote, led_state);
	        }
	        break;
	    case 5:
            {
                toggle_bit(led_state, CWIID_LED3_ON);
	            set_led_state(wiimote, led_state);
	        }
	        break;
	    case 6:
            {
                toggle_bit(led_state, CWIID_LED2_ON);
	            set_led_state(wiimote, led_state);
	        }
	        break;
	    case 7:
            {
                toggle_bit(led_state, CWIID_LED1_ON);
	            set_led_state(wiimote, led_state);
	            cwiimote->count = 1;
	        }
	        break;
	}

		
	return TRUE;
}

/* Utility */
int findMinIR (CompDisplay *d, CompWiimote *wiimote, int ir1, int ir2, int ir3, int ir4)
{
    
    int min = ir1;
    
    if ((ir2 < min) && wiimote->ir[1].valid)
        min = ir2;
    if ((ir3 < min) && wiimote->ir[2].valid)
        min = ir3;
    if ((ir4 < min) && wiimote->ir[3].valid)
        min = ir4;
        
    return min;
}

int findMaxIR (CompDisplay *d, CompWiimote *wiimote, int ir1, int ir2, int ir3, int ir4)
{
    
    int max = ir1;
    
    if ((ir2 > max) && wiimote->ir[1].valid)
        max = ir2;
    if ((ir3 > max) && wiimote->ir[2].valid)
        max = ir3;
    if ((ir4 > max) && wiimote->ir[3].valid)
        max = ir4;
        
    return max;
}