version 0.3.2
[expresskeys.git] / src-expresskeys / exec_shell.c
1 /*
2  exec_shell.c -- Support ExpressKeys & Touch Strips on a Wacom Intuos3 tablet.
3
4  Copyright (C) 2005-2006 - Mats Johannesson
5
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
19 */
20
21 #include "globals.h"
22
23 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24  Function uses the simple "system" command to execute another program in
25  a shell, waiting for it to return. Here it calls the "xsetwacom" from
26  the linuxwacom.sourceforge.net driver package. Observe that it doesn't
27  check whether the program gets executed or not, just that the shell gets
28  forked without an error.
29
30  This sample utilization of xsetwacom alters the pen sensitivity to pressure
31  just like setting the Feel -> Sensitivity in wacomcpl (the tcl utility
32  program) between Soft (lower) and Firm (higher) values (1 to 7, 4 is
33  default). Both STYLUS1_CURVE_DOWNWARD and STYLUS1_CURVE_UPWARD flip over in a
34  carousel fashion at the top and bottom values.
35  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
36
37 int call_xsetwacom(int num)
38 {
39         char buffer [MAXBUFFER];
40         char curve [12];
41
42         if ((num >= STYLUS1_CURVE_DOWNWARD) &&
43                 (num <= STYLUS1_CURVE_UPWARD)) {
44                 if (stylus1_info) {
45                         if (num == STYLUS1_CURVE_DOWNWARD) {
46                                 if (padstylus1_presscurve > 1) {
47                                         padstylus1_presscurve--;
48                                 } else {
49                                         padstylus1_presscurve = 7;
50                                 }
51                         }
52                         if (num == STYLUS1_CURVE_DEFAULT) {
53                                 padstylus1_presscurve = 4;
54                         }
55                         if (num == STYLUS1_CURVE_UPWARD) {
56                                 if (padstylus1_presscurve < 7) {
57                                         padstylus1_presscurve++;
58                                 } else {
59                                         padstylus1_presscurve = 1;
60                                 }
61                         }
62                         switch (padstylus1_presscurve) {
63
64                                 case 1:
65                                 sprintf(curve, "0 75 25 100");
66                                 break;
67
68                                 case 2:
69                                 sprintf(curve, "0 50 50 100");
70                                 break;
71
72                                 case 3:
73                                 sprintf(curve, "0 25 75 100");
74                                 break;
75
76                                 case 4:
77                                 sprintf(curve, "0 0 100 100");
78                                 break;
79
80                                 case 5:
81                                 sprintf(curve, "25 0 100 75");
82                                 break;
83
84                                 case 6:
85                                 sprintf(curve, "50 0 100 50");
86                                 break;
87
88                                 case 7:
89                                 sprintf(curve, "75 0 100 25");
90                                 break;
91
92                                 default:
93                                 sprintf(curve, "0 0 100 100");
94                                 break;
95
96                         }
97                         snprintf(buffer, MAXBUFFER, "xsetwacom set %s PressCurve %s", stylus1_name, curve);
98                         if ((system(buffer)) == NON_VALID) {
99                                 fprintf(stderr, "Failed to fork a shell for xsetwacom!\n");
100                         } else {
101                                 if (be_verbose) {
102                                         fprintf(stderr, "%s Sensitivity = %d PressCurve = %s\n", stylus1_name, padstylus1_presscurve, curve);
103                                 }
104                         }
105                 }
106         }
107         return 0;
108 }
109
110 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
111  Function uses the "popen" command which creates a pipe, forks and invokes
112  a shell where xsetwacom can be run. First action is to ensure that version
113  0.0.7 or greater of xsetwacom is present (ie linuxwacom-0.7.5-2 where the
114  option GetTabletID was introduced). Thereafter we match the tablet decimal
115  number string against known Graphire4 numbers. A full table can be found in
116  src/xdrv/wcmUSB.c of the linuxwacom sources (Hex numbers).
117 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
118
119 int identify_device(char *name)
120 {
121         const char *graphire4_4x5 = "21"; /* 0x15 */
122         const char *graphire4_6x8 = "22"; /* 0x16 */
123         const char *graphire4_6x8_BT = "129"; /* 0x81 */
124         const int min_xsetwacom = 7; /* Minimum xsetwacom version we can use is 0.0.7 */
125
126         char read_buffer [MAXBUFFER];
127         char write_buffer [MAXBUFFER];
128
129         int len = 0;
130         int i, j = 0;
131         int ok_xsetwacom = 0;
132
133         FILE *execfp = NULL;
134
135         if ((execfp = popen("xsetwacom -V", "r")) != NULL) {
136                 fgets(read_buffer, MAXBUFFER, execfp);
137                 if ((pclose(execfp)) == 0) {
138                         len = strcspn(read_buffer, " \t\n");
139                         for (i = 0; i < len; i++) {
140                                 if (isdigit(read_buffer[i])) {
141                                         write_buffer[j] = read_buffer[i];
142                                         j++;
143                                 }
144                         }
145                         write_buffer[j] = '\0';
146                         if ((atoi(write_buffer)) >= min_xsetwacom) {
147                                 ok_xsetwacom = 1;
148                         }
149                 }
150         }
151
152         if (ok_xsetwacom) {
153                 snprintf(write_buffer, MAXBUFFER, "xsetwacom get %s GetTabletID", name);
154                 if ((execfp = popen(write_buffer, "r")) != NULL) {
155                         fgets(read_buffer, MAXBUFFER, execfp);
156                         if ((pclose(execfp)) == 0) {
157                                 len = strcspn(read_buffer, " \t\n");
158                                 if (((strncmp(read_buffer, graphire4_4x5, len)) == 0)
159                                         || ((strncmp(read_buffer, graphire4_6x8, len)) == 0)
160                                         || ((strncmp(read_buffer, graphire4_6x8_BT, len)) == 0)) {
161                                         is_graphire4 = 1;
162                                 }
163                         }
164                 }
165         }
166         return 0;
167 }
168
169 /* End Code */
170