wmail: added new ChangeLog.
[dockapps.git] / wmfortune / wmfortune.c
blob80e071123c70d36c81d266588c1011941ffea8b7
1 /*
2 * $Id: wmfortune.c,v 0.24 2000/03/28 01:49:58 sugano Exp $
3 * wmfortune Copyright (c) 2000 Makoto Sugano
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #define BUFF_SIZE 1024
24 #define SCROLL_DEFAULT_SPEED 1000
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <libdockapp/dockapp.h>
30 #include "XPM/panel.xpm"
31 #include "XPM/panel_button_pressed.xpm"
32 #include "XPM/panel_window.xpm"
34 /* WINDOW: window for showing the messages */
35 #define WINDOW_X 4
36 #define WINDOW_Y 4
37 #define WINDOW_WIDTH 56
38 #define WINDOW_HEIGHT 22
40 /* BUTTON: button for the new fortune message */
41 #define BUTTON_X 6
42 #define BUTTON_Y 32
43 #define BUTTON_WIDTH 52
44 #define BUTTON_HEIGHT 28
46 /* 6 pixels are used to draw a letter */
47 #define PIXELS_PER_LETTER 6
49 #define STRING_Y 18
52 * pixel lag between the last letter of the previous message
53 * and the first letter of the next message.
54 * if set to 0, the messages appear quickly after
55 * the previous messages ends.
57 #define MESSAGE_LAG 5
59 char *displayName = "";
60 int speed = SCROLL_DEFAULT_SPEED;
62 /* global variables for main() and callbacks */
63 FILE *fortune;
64 Pixmap pixmap;
65 Pixmap panel_button_pressed_pixmap;
66 Pixmap panel_pixmap, panel_mask;
67 GC gc;
68 int c;
69 char buff[BUFF_SIZE];
72 * i: used to count the number of letters in fill_buff().
73 * j: the number that points the message starting pixel.
75 int i, j;
77 /* fill the buffer with the fortune message. */
78 static void
79 fill_buff (void)
81 i = 0;
83 memset(buff, '\0', BUFF_SIZE);
84 if (!(fortune = popen("fortune -s", "r")))
86 perror("fortune");
87 exit(0);
90 while (( c = fgetc(fortune)) != EOF)
92 if (c == '\n')
94 buff[i++] = ' ';
96 else if (c == '\t')
98 buff[i++] = ' ';
99 buff[i++] = ' ';
100 buff[i++] = ' ';
102 else
104 buff[i++] = c;
108 j = i + MESSAGE_LAG;
109 pclose(fortune);
112 static DAProgramOption options[] = {
113 {"-d", "--displayname", "display to use.", DOString, False, {&displayName}},
114 {"-s", "--speed", "scrolling speed. (default 1000 dot/ms)", DOInteger, False, {&speed}},
117 static void
118 buttonReleaseCallback (int button, int state, int x, int y)
120 if (button == 1 && BUTTON_X < x &&
121 x < (BUTTON_X + BUTTON_WIDTH) &&
122 BUTTON_Y < y && y < (BUTTON_Y + BUTTON_HEIGHT))
124 fill_buff();
126 XCopyArea(DADisplay, panel_pixmap, pixmap, gc,
127 BUTTON_X, BUTTON_Y, BUTTON_WIDTH, BUTTON_HEIGHT,
128 BUTTON_X, BUTTON_Y);
131 static void
132 buttonPressCallback (int button, int state, int x, int y)
134 if (button == 1 && BUTTON_X < x &&
135 x < (BUTTON_X + BUTTON_WIDTH) &&
136 BUTTON_Y < y && y < (BUTTON_Y + BUTTON_HEIGHT))
138 XCopyArea(DADisplay, panel_button_pressed_pixmap, pixmap, gc,
139 BUTTON_X, BUTTON_Y, BUTTON_WIDTH, BUTTON_HEIGHT,
140 BUTTON_X, BUTTON_Y);
145 main(int argc, char **argv)
147 Pixmap panel_window_pixmap;
149 short unsigned int w = 64, h = 64;
151 DACallbacks callbacks = {NULL,buttonPressCallback
152 ,buttonReleaseCallback,NULL,NULL,NULL,NULL};
154 DAParseArguments(argc, argv, options,
155 sizeof(options)/sizeof(DAProgramOption),
156 "dockapp that shows the messages from fortune command.", "$Id: wmfortune.c,v 0.24 2000/03/28 01:49:58 sugano Exp $");
158 fill_buff();
160 DAInitialize(displayName, "wmfortune", 64, 64, argc, argv);
161 pixmap = DAMakePixmap();
163 /* making pixmap for the panel */
164 DAMakePixmapFromData(panel_xpm, &panel_pixmap,
165 &panel_mask, &w, &h);
166 DAMakePixmapFromData(panel_button_pressed_xpm,
167 &panel_button_pressed_pixmap, NULL, &w, &h);
168 DAMakePixmapFromData(panel_window_xpm,
169 &panel_window_pixmap, NULL, &w, &h);
171 /* setting up the mask for the panel */
172 DASetShape(panel_mask);
173 DASetPixmap(panel_pixmap);
175 /* setting up the graphic context */
176 gc = DefaultGC(DADisplay, DefaultScreen(DADisplay));
178 DASetCallbacks(&callbacks);
179 DAShow();
181 /* drawing the button */
182 XCopyArea(DADisplay, panel_pixmap, pixmap, gc,
183 BUTTON_X, BUTTON_Y, BUTTON_WIDTH, BUTTON_HEIGHT,
184 BUTTON_X, BUTTON_Y);
186 while (1)
188 XEvent ev;
190 /* sets the foreground color green */
191 XSetForeground(DADisplay, gc, DAGetColor("green"));
192 XCopyArea(DADisplay, panel_window_pixmap, pixmap, gc,
193 WINDOW_X, WINDOW_Y, WINDOW_WIDTH, WINDOW_HEIGHT,
194 WINDOW_X, WINDOW_Y);
195 XDrawString(DADisplay, pixmap, gc, j, STRING_Y, buff, strlen(buff));
197 DASetPixmap(pixmap);
199 /* scroll the message by a pixel to left */
200 i--;
201 j = i + MESSAGE_LAG;
204 * starts scrolling the messages from the beginning
205 * if the message ends.
207 if (j == - (PIXELS_PER_LETTER) * strlen(buff))
209 /* if not 64, message suddenly appears in the window */
210 i = 64;
211 j = i + MESSAGE_LAG;
214 /* handle all pending X events */
215 while (XPending(DADisplay))
217 XNextEvent(DADisplay, &ev);
218 DAProcessEvent(&ev);
220 usleep(speed);
222 return 0;