wmbiff: use temporary variables and remove casts.
[dockapps.git] / wmkeys / wmkeys.c
blob355cf0a1bc68eabcb2e6518164efedcb0b344968
1 /* $Id$ -*- C -*-
3 * wmkeys.c
4 * Version: 0.1
5 * A Window Maker/AfterStep dock application for switching X key sets
6 * on the fly.
8 * Copyright (C) 1999 Eric Crampton <EricCrampton@worldnet.att.net>
9 * All rights reserved.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this software; see the file COPYING. If not, write to
23 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 * Boston, MA 02110-1301 USA
26 * Reference source: wmtime dock app
31 * Standard C includes
34 #include <ctype.h>
35 #include <fcntl.h>
36 #include <math.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <time.h>
41 #include <unistd.h>
43 #include <sys/wait.h>
44 #include <sys/param.h>
45 #include <sys/types.h>
48 * X11 includes
51 #include <X11/Xlib.h>
52 #include <X11/xpm.h>
53 #include <X11/extensions/shape.h>
56 * Window Maker includes
59 #include "libdockapp/wmgeneral.h"
60 #include "libdockapp/misc.h"
63 * Pixmap, bitmap includes
66 #include "wmkeys-master.xpm"
67 #include "wmkeys-mask.xbm"
70 * Global variables
73 char *ProgName;
75 typedef struct {
76 char* name;
77 char* filename;
78 } keysym_config;
80 keysym_config configs[10];
81 int num_configs;
82 int current_config;
85 * Function prototypes
88 void wmkeys_routine(int, char **);
89 void read_config();
90 void draw_string(char* s);
91 void enable_configuration(int n);
94 * Main
97 int main(int argc, char *argv[])
99 num_configs = 0;
100 current_config = 0;
102 ProgName = argv[0];
103 if (strlen(ProgName) >= 6)
104 ProgName += (strlen(ProgName) - 6);
106 read_config();
107 wmkeys_routine(argc, argv);
109 return 0;
113 * wmkeys_routine
116 void wmkeys_routine(int argc, char **argv)
118 int i;
119 XEvent Event;
120 int but_stat = -1;
122 openXwindow(argc, argv, wmkeys_master_xpm, wmkeys_mask_bits, 64, 64);
123 enable_configuration(0);
125 /* add mouse region */
126 AddMouseRegion(0, 5, 5, 58, 122);
128 while (1) {
129 waitpid(0, NULL, WNOHANG);
130 RedrawWindow();
132 while (XPending(display)) {
133 XNextEvent(display, &Event);
134 switch (Event.type) {
135 case Expose:
136 RedrawWindow();
137 break;
138 case DestroyNotify:
139 XCloseDisplay(display);
140 exit(0);
141 break;
142 case ButtonPress:
143 but_stat = CheckMouseRegion(Event.xbutton.x, Event.xbutton.y);
144 break;
145 case ButtonRelease:
146 i = CheckMouseRegion(Event.xbutton.x, Event.xbutton.y);
147 if (but_stat == i && but_stat >= 0) {
148 switch (but_stat) {
149 case 0:
150 current_config++;
151 if(current_config == num_configs)
152 current_config = 0;
153 enable_configuration(current_config);
154 break;
157 break;
161 /* Sleep 0.3 seconds */
162 usleep(300000L);
167 * draw_string()
169 * Draws string s in the LCD display portion of the window.
172 void draw_string(char* s)
174 int i;
175 for(i=0; i<strlen(s) && i<8; i++) {
176 copyXPMArea((toupper(s[i]) - 'A')*6, 74, 6, 9, 5+(i*6), 49);
181 * getline()
184 int getline_wmkeys(FILE* pfile, char* s, int lim)
186 int c = 0, i;
187 for(i=0; i<lim-1 && (c=fgetc(pfile)) != EOF && c!='\n'; ++i) {
188 s[i] = c;
190 if(c == '\n') {
191 s[i] = c;
192 ++i;
194 s[i] = '\0';
195 return i;
199 * read_config()
201 * Reads the appropriate configuration file from ${HOME}/.wmkeysrc
202 * or from /etc/wmkeysrc, in that order.
205 void read_config()
207 char* rcfilename;
208 char* home_var;
209 FILE* pfile;
210 char key[256], value[256];
212 home_var = getenv("HOME");
214 rcfilename = malloc(sizeof(char) * strlen(home_var) + 11 /* / .wmkeysrc + NULL*/);
215 strcpy(rcfilename, home_var);
216 strcat(rcfilename, "/.wmkeysrc");
218 pfile = fopen(rcfilename, "r");
219 if(pfile == NULL) {
220 /* try to open system-wide configuration */
221 strcpy(rcfilename, "/etc/wmkeysrc");
222 pfile = fopen(rcfilename, "r");
224 if(!pfile) {
225 fprintf(stderr, "Error: cannot open ${HOME}/.wmkeysrc or /etc/wmkeysrc\n");
226 exit(1);
230 while(!feof(pfile)) {
231 getline_wmkeys(pfile, key, 256);
233 if(!feof(pfile)) {
234 getline_wmkeys(pfile, value, 256);
236 configs[num_configs].name = malloc(sizeof(char)*strlen(key)+1);
237 strcpy(configs[num_configs].name, key);
238 configs[num_configs].filename = malloc(sizeof(char)*strlen(value)+1);
239 strcpy(configs[num_configs].filename, value);
240 num_configs++;
243 if(num_configs == 0) {
244 fprintf(stderr, "Error: no configurations, exiting.\n");
245 exit(1);
250 * enable_configuration()
252 * Enables configuration number n.
255 void enable_configuration(int n)
257 char syscmd[256];
258 draw_string(configs[n].name);
259 strcpy(syscmd, "xmodmap ");
260 strcat(syscmd, configs[n].filename);
261 system(syscmd);
262 RedrawWindow();