wmcore: set DESTDIR so binary installs correctly.
[dockapps.git] / buttonmaker / src / main.c
blob38ad33cada660eaa5547c455cbbf13302a7a32b1
1 /*
2 * main.c
3 * Copyright (C) Renan Vedovato Traba 2012 <rvt10@inf.ufpr.br>
5 ButtonMaker is free software: you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 ButtonMaker is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program. If not, see <http://www.gnu.org/licenses/>.
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/timeb.h>
27 #include <X11/X.h>
28 #include <X11/xpm.h>
29 #include <X11/Xlib.h>
30 #include <Imlib2.h>
32 #include "wmgeneral.h"
33 #include "pixmaps.h"
35 char *cmd, *fname;
36 int isize=50;
38 /* Prototypes */
39 static void print_usage(char *);
40 static void ParseCMDLine(int argc, char *argv[]);
42 static void print_usage(char * pname)
44 printf("%s version: %s\n", pname, VERSION);
45 printf("By Renan Vedovato Traba <rvt10@inf.ufpr.br>\n\n");
46 printf("\t-c, --command [CMD]\t which command will be called \n");
47 printf("\t-i, --icon [ICON]\t icon displayed\n");
48 printf("\t-s, --size [SIZE]\t size of the icon\n");
49 printf("\tdouble click to run CMD\n");
50 printf("\tright click to exit\n");
51 printf("\neg:\n%s -c sudo apt-get update -i TerminalGNUstep.tiff -s 32\n", pname);
54 static void ParseCMDLine(int argc, char *argv[])
56 int i, j, k, size=2;
57 for (i=1; i < argc; i++) {
58 if (!strcmp(argv[i], "-display")) {
59 i++;
60 } else if ( ((!strcmp(argv[i], "--icon")) || (!strcmp(argv[i], "-i"))) && (i+1 < argc) ) {
61 fname = argv[++i];
62 } else if ( ((!strcmp(argv[i], "--size")) || (!strcmp(argv[i], "-s"))) && (i+1 < argc) ) {
63 isize = atoi(argv[++i]);
64 } else if ( ((!strcmp(argv[i], "--command")) || (!strcmp(argv[i], "-c"))) && (i+1 < argc) ) {
65 for (j=i+1; j < argc; j++) {
66 if (argv[j][0] == '-') {
67 break;
68 } else {
69 size+=1+strlen(argv[j]);
72 cmd = (char *) malloc((size)*sizeof(char));
73 for (k=i+1; k < j; k++) {
74 strcat(cmd, argv[k]);
75 strcat(cmd, " ");
77 strcat(cmd, "&");
78 i = j-1;
79 } else {
80 print_usage(argv[0]);
81 exit(1);
84 if ((argc < 3) || (!cmd)) {
85 print_usage(argv[0]);
86 exit(1);
90 int main(int argc, char *argv[])
92 int b;
94 extern int d_depth;
95 extern Window iconwin, win;
96 Visual *visual;
97 XEvent event;
98 Pixmap xpm;
99 Time lastTime=0;
101 Imlib_Image image;
103 ParseCMDLine(argc, argv);
104 openXwindow(argc, argv, xpm_master, xpm_mask_bits, xpm_mask_width, xpm_mask_height);
106 xpm = XCreatePixmap(display, win, 64, 64, d_depth);
107 XFillRectangle(display, xpm, NormalGC, 0, 0, 64, 64);
109 if (fname) {
110 visual = DefaultVisual(display, DefaultScreen(display));
112 imlib_context_set_dither(1);
113 imlib_context_set_display(display);
114 imlib_context_set_visual(visual);
116 image = imlib_load_image(fname);
117 imlib_context_set_image(image);
118 imlib_context_set_drawable(xpm);
119 imlib_render_image_on_drawable_at_size(0, 0, isize, isize);
121 b = (64-isize)/2;
122 /* Loop Forever */
123 while (1) {
124 /* Process any pending X events. */
125 while (XPending(display)) {
126 XNextEvent(display, &event);
127 switch (event.type) {
128 case Expose:
129 RedrawWindow();
130 XCopyArea(display, xpm, iconwin, NormalGC, 0, 0, isize, isize, b, b);
131 break;
132 case MotionNotify:
133 break;
134 case ButtonPress:
135 /*printf("ButtonPress\n");*/
136 break;
137 case ButtonRelease:
138 if (event.xbutton.button == Button1) {
139 if (event.xbutton.time - lastTime < 250) {
140 if (system(cmd) == -1) {
141 fprintf(stdout, "Failed to run command:%s\n", cmd);
142 exit(0);
144 } else {
145 lastTime = event.xbutton.time;
147 } else if (event.xbutton.button == Button3) {
148 exit(0);
150 /*printf("ButtonRelease\n");*/
151 break;
154 usleep(10000);
156 /* we should never get here */
157 return (0);