-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathklicknmenu.c
413 lines (363 loc) · 10.6 KB
/
klicknmenu.c
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
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <assert.h>
#include "types.h"
#include "klicknmenu.h"
#include "draw.h"
int menu_width;
int menu_height;
int menu_padding;
int border_width;
char* border_color;
int entry_height;
char* bgColor;
char* fgColor;
char* focus_bgColor;
char* focus_fgColor;
char* fontname;
/* location values of mouse */
int pointer_x;
int pointer_y;
ALIGN align;
ENTRY* lastentry;
ENTRY* menu;
ENTRY* current_entry;
/* initmenu: with $HOME environment value, make the configuration file path
and read the file, starting to make entry nodes. */
extern void
initmenu(void)
{
char* homedir;
char* configPath;
FILE* fp;
/* initialize menu value as NULL */
menu = NULL;
homedir = getenv("HOME");
if( homedir == NULL ) {
fprintf(stderr, "$HOME environment value is not set.\n");
exit(1);
}
/* make the config path */
configPath = (char *) malloc( sizeof(char) * ( strlen(homedir) + strlen("/.klicknmenu")) + 1);
strncpy( configPath, homedir, strlen( homedir ) );
strncat( configPath, "/.klicknmenu", strlen("/.klicknmenu") );
if ( (fp = fopen( configPath, "r" )) == NULL )
{
fprintf(stderr, "configuration file isn't exist: %s\n", configPath );
exit(2);
}
else
{
/* if the file exist, make the entry nodes. */
initConfig(fp);
}
fclose(fp);
}
/* checkValue: if configuration file has not proper values, alternatively, set the default value. */
extern void
checkValues(void)
{
if( menu_width < 0 ) menu_width = DEFAULT_MENUWIDTH;
if( menu_height < 0 ) menu_height = DEFAULT_MENUHEIGHT;
if( menu_padding < 0 ) menu_padding = DEFAULT_MENUPADDING;
if( border_width < 0 ) border_width = DEFAULT_BORDERWIDTH;
if( entry_height < 0 ) entry_height = DEFAULT_ENTRYHEIGHT;
if( border_color == NULL ) {
char* tmp = (char*)malloc( sizeof( DEFAULT_BORDERCOLOR ) );
strncpy( tmp, DEFAULT_BORDERCOLOR, strlen( DEFAULT_BORDERCOLOR ) );
border_color = tmp;
}
if( bgColor == NULL ) {
char* tmp = (char*)malloc( sizeof( DEFAULT_BORDERCOLOR ) );
strncpy( tmp, DEFAULT_BACKGROUND, strlen( DEFAULT_BACKGROUND ) );
bgColor = tmp;
}
if( fgColor == NULL ) {
char* tmp = (char*)malloc( sizeof( DEFAULT_BORDERCOLOR ) );
strncpy( tmp, DEFAULT_FOREGROUND, strlen( DEFAULT_FOREGROUND ) );
fgColor = tmp;
}
if( fontname == NULL ) {
char* tmp = (char*)malloc( sizeof( DEFAULT_FONTNAME ) );
strncpy( tmp, DEFAULT_FONTNAME, strlen( DEFAULT_FONTNAME ) );
fontname = tmp;
}
if( focus_bgColor == NULL ) {
char* tmp = (char*)malloc( sizeof( DEFAULT_FOCUSBACKGROUND ) );
strncpy( tmp, DEFAULT_FOCUSBACKGROUND, strlen( DEFAULT_FOCUSBACKGROUND ) );
focus_bgColor = tmp;
}
if( focus_fgColor == NULL ) {
char* tmp = (char*)malloc( sizeof( DEFAULT_FOCUSFOREGROUND ) );
strncpy( tmp, DEFAULT_FOCUSFOREGROUND, strlen( DEFAULT_FOCUSFOREGROUND ) );
focus_fgColor = tmp;
}
}
/* getLevel: "tab character" decide level of menu - please look a configuration example file. */
extern int
getLevel(char* str)
{
int rValue = 0;
while( !isalpha(*str) )
{
if( *str == '\t' ) rValue++;
str++;
}
return rValue;
}
/* numberOfMenu: return the number of menu, which contains sub entries. */
extern int
numberOfMenu(int level)
{
ENTRY* cursor;
int total = 0, \
index;
cursor = menu;
for( index = 0; index < level; index++ )
{
cursor = menu->child;
}
if( cursor == NULL )
return 0;
while( cursor != NULL )
{
cursor = cursor->next;
total++;
}
return total;
}
extern ENTRY*
makeNewEntry(char* str)
{
char* ptr;
ENTRY* newentry;
/* firstly, initialize entry title as str */
newentry = (ENTRY*)malloc(sizeof(ENTRY));
ptr = strtok( str, "," );
newentry->name = trimTabspace(ptr); /* name */
newentry->command = NULL;
newentry->child = NULL;
newentry->parent = NULL;
newentry->xpmPath = NULL;
newentry->prev = NULL;
newentry->next = NULL;
newentry->level = getLevel(ptr);
if( (ptr = strtok( NULL, "," )) != NULL ) {
/* if str has a command */
newentry->command = trimTabspace(ptr);
/* set child pointers(ptr for sub directory) as null */
newentry->child = NULL;
}
if( menu == NULL ) {
/* first entry location will be saved in menu ptr(global value) */
menu = newentry;
lastentry = newentry;
}
else {
if( newentry->level == lastentry->level ) { /* new entry in same level */
lastentry->next = newentry;
newentry->parent = lastentry->parent;
newentry->prev = lastentry;
}
else if( newentry->level > lastentry->level ) { /* new entry in sub level */
lastentry->child = newentry;
newentry->parent = lastentry;
}
else if( newentry->level < lastentry->level ) { /* new entry in parent level */
{
/* move to upper level */
int i;
int max = lastentry->level - newentry->level;
for(i = 0; i < max; i++ )
lastentry = lastentry->parent;
}
newentry->parent = lastentry->parent;
lastentry->next = newentry;
}
}
lastentry = newentry;
return newentry;
}
/* Read configuration file and init menu entries. */
extern void
initConfig(FILE* fp)
{
char buf[BUFSIZE];
char* pch;
bool init_menu = false; /* flag for representing to read entry nodes( name, command, xpm path ) */
/* initialize global value as minus, NULL */
menu_width = -1;
menu_height = -1;
menu_padding = -1;
border_width = -1;
entry_height = -1;
border_color = NULL;
bgColor = NULL;
fgColor = NULL;
fontname = NULL;
focus_bgColor= NULL;
focus_fgColor= NULL;
/* setting global variation from configuration file */
while( fgets(buf, BUFSIZE, fp) > 0 ) {
/* start to tokenize the string */
pch = strtok( buf, TOKEN_VALUES );
while ( pch != NULL ) {
/* make string from configuration file be lower */
tolowerString(pch);
if( ISOPTION(OPTION_MENUWIDTH) ) {
pch = strtok( NULL, TOKEN_VALUES );
COPY_INTVALUE(menu_width)
}
else if( ISOPTION(OPTION_MENUHEIGHT)) {
pch = strtok( NULL, TOKEN_VALUES );
COPY_INTVALUE(menu_height)
}
else if( ISOPTION(OPTION_MENUPADDING)) {
pch = strtok( NULL, TOKEN_VALUES );
COPY_INTVALUE(menu_padding)
}
else if( ISOPTION(OPTION_BORDERWIDTH)) {
pch = strtok( NULL, TOKEN_VALUES );
COPY_INTVALUE(border_width)
}
else if( ISOPTION(OPTION_BORDERCOLOR) ){
pch = strtok( NULL, TOKEN_VALUES );
COPY_STRVALUE(border_color)
}
else if( ISOPTION(OPTION_ENTRYHEIGHT) ) {
pch = strtok( NULL, TOKEN_VALUES );
COPY_INTVALUE(entry_height);
}
else if( ISOPTION(OPTION_BACKGROUND) ) {
pch = strtok( NULL, TOKEN_VALUES );
COPY_STRVALUE(bgColor)
}
else if( ISOPTION(OPTION_FOREGROUND)) {
pch = strtok( NULL, TOKEN_VALUES );
COPY_STRVALUE(fgColor)
}
else if( ISOPTION(OPTION_FONTNAME)) {
pch = strtok( NULL, TOKEN_FONTNAME );
COPY_STRVALUE(fontname);
}
else if( ISOPTION(OPTION_FOCUSBACKGROUND)) {
pch = strtok( NULL, TOKEN_VALUES );
COPY_STRVALUE(focus_bgColor);
}
else if( ISOPTION(OPTION_FOCUSFOREGROUND)) {
pch = strtok( NULL, TOKEN_VALUES );
COPY_STRVALUE(focus_fgColor);
}
else if( ISOPTION(OPTION_TEXTALIGN) ) {
pch = strtok( NULL, TOKEN_VALUES );
if( strncmp(pch, "left", 4) == 0 ) align = LEFT;
else if( strncmp(pch, "right", 4) == 0 ) align = RIGHT;
else if( strncmp(pch, "center", 4) == 0 ) align = CENTER;
else align = DEFAULT_ALIGN; /* DEFAULT_ALIGN = LEFT */
}
else if( !init_menu && ISOPTION(OPTION_MENU) ) {
/* start parsing all entries in configuration file */
fgets(buf, BUFSIZE, fp); /* drop the "menu" */
tolowerString(buf);
while ( !ISENDMENU(buf) ){
/********************** make entry nodes ***********************/
#ifdef _DEBUG_ON_
ENTRY* newentry = makeNewEntry(buf);
showEntry(newentry);
#else
makeNewEntry(buf);
#endif
/***************************************************************/
fgets(buf, BUFSIZE, fp);
tolowerString(buf);
}
}
else {
pch = strtok( NULL, TOKEN_VALUES );
}
}
}
}
extern char*
trimTabspace(char* str)
{
int index, count=0;
char* copied;
for( index = 0; index < strlen(str); index++ ) {
if( !isspace(str[index]) ) break;
else if( str[index] == '\t' || str[index] == '\n' || str[index] == '\'' || str[index] == '\"' ) count++;
}
copied = (char*)malloc(sizeof(char)*(strlen(str) - count + 1));
for( index = 0, count = 0; index < strlen(str); index++ ) {
if( str[index] != '\t' && str[index] != '\n' && str[index] != '\'' && str[index] != '\"' ) {
copied[count] = str[index];
count++;
}
}
copied[count] = '\0';
return copied;
}
extern void
tolowerString(char* str)
{
int index = 0;
int doublequote = 0;
for( ; index < strlen(str); index++ ) {
if( doublequote >= 2 ) break;
else if( str[index] == '\"' || str[index] == '\'' ) doublequote++;
str[index] = tolower(str[index]);
}
}
int main(void)
{
initmenu();
#ifdef _DEBUG_ON_
showValues();
#endif
checkValues();
return draw();
}
#ifdef _DEBUG_ON_
void
showMenu(ENTRY* ent)
{
ENTRY* cursor = ent;
if( cursor == NULL ) return;
else {
showEntry(cursor);
if( cursor->child != NULL ) showMenu(cursor->child);
if( cursor->next != NULL ) showMenu(cursor->next);
}
}
void
showValues(void)
{
printf("=======================================================\n");
printf("menu_width\t%d\n", menu_width);
printf("menu_height\t%d\n", menu_height);
printf("border_width\t%d\n", border_width);
printf("border_color\t%s\n", border_color);
printf("entry_height\t%d\n", entry_height);
printf("background\t%s\n", bgColor);
printf("foreground\t%s\n", fgColor);
printf("text_align\t%d\n", align);
printf("fontname\t%s\n", fontname);
printf("=======================================================\n");
}
void
showEntry(ENTRY* target)
{
fprintf(stdout, "name : %s\n", target->name);
fprintf(stdout, "command : %s\n", target->command);
fprintf(stdout, "child : %p\n", target->child);
fprintf(stdout, "parent : %p\n", target->parent);
fprintf(stdout, "prev : %p\n", target->prev);
fprintf(stdout, "next : %p\n", target->next);
fprintf(stdout, "level : %d\n", target->level);
}
#endif