wmtime - update localization
[dockapps.git] / wmkeys / wmkeys.c
blob2d4859b4b60c60a63d8d027c02eb069a85f1c142
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 "wmgeneral/wmgeneral.h"
60 #include "wmgeneral/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 void 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);
111 * wmkeys_routine
114 void wmkeys_routine(int argc, char **argv)
116 int i;
117 XEvent Event;
118 int but_stat = -1;
120 openXwindow(argc, argv, wmkeys_master_xpm, wmkeys_mask_bits, 64, 64);
121 enable_configuration(0);
123 /* add mouse region */
124 AddMouseRegion(0, 5, 5, 58, 122);
126 while (1) {
127 waitpid(0, NULL, WNOHANG);
128 RedrawWindow();
130 while (XPending(display)) {
131 XNextEvent(display, &Event);
132 switch (Event.type) {
133 case Expose:
134 RedrawWindow();
135 break;
136 case DestroyNotify:
137 XCloseDisplay(display);
138 exit(0);
139 break;
140 case ButtonPress:
141 but_stat = CheckMouseRegion(Event.xbutton.x, Event.xbutton.y);
142 break;
143 case ButtonRelease:
144 i = CheckMouseRegion(Event.xbutton.x, Event.xbutton.y);
145 if (but_stat == i && but_stat >= 0) {
146 switch (but_stat) {
147 case 0:
148 current_config++;
149 if(current_config == num_configs)
150 current_config = 0;
151 enable_configuration(current_config);
152 break;
155 break;
159 /* Sleep 0.3 seconds */
160 usleep(300000L);
165 * draw_string()
167 * Draws string s in the LCD display portion of the window.
170 void draw_string(char* s)
172 int i;
173 for(i=0; i<strlen(s) && i<8; i++) {
174 copyXPMArea((toupper(s[i]) - 'A')*6, 74, 6, 9, 5+(i*6), 49);
179 * getline()
182 int getline(FILE* pfile, char* s, int lim)
184 int c = 0, i;
185 for(i=0; i<lim-1 && (c=fgetc(pfile)) != EOF && c!='\n'; ++i) {
186 s[i] = c;
188 if(c == '\n') {
189 s[i] = c;
190 ++i;
192 s[i] = '\0';
193 return i;
197 * read_config()
199 * Reads the appropriate configuration file from ${HOME}/.wmkeysrc
200 * or from /etc/wmkeysrc, in that order.
203 void read_config()
205 char* rcfilename;
206 char* home_var;
207 FILE* pfile;
208 char key[256], value[256];
210 home_var = getenv("HOME");
212 rcfilename = malloc(sizeof(char) * strlen(home_var) + 11 /* / .wmkeysrc + NULL*/);
213 strcpy(rcfilename, home_var);
214 strcat(rcfilename, "/.wmkeysrc");
216 pfile = fopen(rcfilename, "r");
217 if(pfile == NULL) {
218 /* try to open system-wide configuration */
219 strcpy(rcfilename, "/etc/wmkeysrc");
220 pfile = fopen(rcfilename, "r");
222 if(!pfile) {
223 fprintf(stderr, "Error: cannot open ${HOME}/.wmkeysrc or /etc/wmkeysrc\n");
224 exit(1);
228 while(!feof(pfile)) {
229 getline(pfile, key, 256);
231 if(!feof(pfile)) {
232 getline(pfile, value, 256);
234 configs[num_configs].name = malloc(sizeof(char)*strlen(key)+1);
235 strcpy(configs[num_configs].name, key);
236 configs[num_configs].filename = malloc(sizeof(char)*strlen(value)+1);
237 strcpy(configs[num_configs].filename, value);
238 num_configs++;
241 if(num_configs == 0) {
242 fprintf(stderr, "Error: no configurations, exiting.\n");
243 exit(1);
248 * enable_configuration()
250 * Enables configuration number n.
253 void enable_configuration(int n)
255 char syscmd[256];
256 draw_string(configs[n].name);
257 strcpy(syscmd, "xmodmap ");
258 strcat(syscmd, configs[n].filename);
259 system(syscmd);
260 RedrawWindow();