File: xteddy.c

package info (click to toggle)
xteddy 2.0.1-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 604 kB
  • ctags: 35
  • sloc: sh: 380; ansic: 261; makefile: 82
file content (364 lines) | stat: -rw-r--r-- 10,238 bytes parent folder | download | duplicates (2)
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
/* Xteddy - a cuddly bear to place on your desktop. */
/* Author: Stefan Gustavson, ISY-LiTH, 1994         */
/* Internet email address: stegu@itn.liu.se         */
/* This software is distributed under the GNU       */
/* Public Licence (GPL).                            */
/* Also, if you modify this program or include it   */
/* in some kind of official distribution, I would   */
/* like to know about it.                           */

/* Xpm pixmap manipulation routines for color       */
/* and grayscale teddies are from the Xpm library   */
/* by Arnaud Le Hors, lehors@sophia.inria.fr,       */
/* Copyright 1990-93 GROUPE BULL                    */

/* This is Xteddy version 1.9 as of 1998-12-22.     */
/* from Andreas Tille <tille@physik.uni-halle.de>   */
/* Changes: use Imlib instead of libxpm and load    *
 *          different image formats                 */

#define DEFAULT_IMAGE_DIR "/usr/share/xteddy"

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
#include <X11/extensions/shape.h>
#include <X11/cursorfont.h>

#include <Imlib.h>

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/param.h>

#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif

static char *progname;
Display *display;
int screen_num;

char *Extensions[] = {
  "xpm",
  "png",
  "tif",
  "jpg",
  "jpeg",
  "gif",
  "pnm",
  "XPM",
  "PNG",
  "TIF",
  "JPG",
  "JPEG",
  "GIF",
  "PNG",
  NULL
};

char *InitTeddy(char *teddy)
/* Initializing filename */
{
  char     fbuf[MAXPATHLEN], sbuf[MAXPATHLEN], *name, *ext, *sext;
  struct   stat stat_buf;
  char   **ptr;

  if ( !teddy || !*(teddy) ) return NULL;

  if ( !stat(teddy, &stat_buf) ) return strdup(teddy);
  /* Test, some image formats PIXMAP_PATH */
  strcat(strcpy(fbuf, PIXMAP_PATH), "/");
  strcat(strcat(strcpy(sbuf, DEFAULT_IMAGE_DIR), "/"), teddy);
  name = fbuf + strlen(fbuf);
  strcpy(name, teddy);
  if ( !stat(fbuf, &stat_buf) ) return strdup(fbuf);
  if ( !stat(sbuf, &stat_buf) ) return strdup(sbuf);
  strcat(name, ".");
  ext  = name + strlen(name);
  sext = sbuf + strlen(sbuf);
  for ( ptr = Extensions; *ptr; ptr++ ) {
    strcat(name, *ptr);
    if ( !stat(name, &stat_buf) ) return strdup(name);
    if ( !stat(fbuf, &stat_buf) ) return strdup(fbuf);
    strcat(sbuf, *ptr);
    if ( !stat(sbuf, &stat_buf) ) return strdup(sbuf);
    *ext = *sext = 0;
  }
  return NULL;
}

int main(int argc, char **argv)
{
  /* Display, window and gc manipulation variables */
  Window               win;
  XSetWindowAttributes setwinattr;
  unsigned long        valuemask, inputmask;
  int                  x, y, geomflags, xw, xh;
  unsigned int         border_width = 0;
  unsigned int         display_width, display_height;
  XSizeHints           size_hints;
  XWMHints             wm_hints;
  XClassHint           class_hints;
  XTextProperty        windowName, iconName;
  int                  argnum;
  int                  use_wm, float_up, allow_quit;
  XEvent               report;
  char                *display_name = NULL;
  char                 buffer[20];
  int                  bufsize = 20;
  KeySym               keysym;
  XComposeStatus       compose;
  int                  charcount;
  Cursor               cursor;
  char                *teddy;
  char                *file;

  /* Window movement variables */
  XWindowChanges       winchanges;
  Window               root, child, basewin;
  int                  offs_x, offs_y, new_x, new_y, tmp_x, tmp_y;
  unsigned int         tmp_mask;

  ImlibData           *id;
  ImlibImage          *im;

  /* Determine program name */
  if ((progname = strrchr(argv[0],'/')) == NULL)
    progname = argv[0];
  else
    progname++;
  teddy = progname; 

  /* Option handling: "-wm", "-float", "-noquit", "-geometry" */
  /* and "-display" are recognized. See manual page for details. */
  /* -F<name> ... Other pixmap name */
  use_wm = FALSE;
  float_up = FALSE;
  allow_quit = TRUE;
  x = y = 0;
  geomflags = 0;
  for ( argnum = 1; argnum < argc; argnum++ ) {
    if (!strcmp(argv[argnum],"-wm"))
      use_wm = TRUE;
    if (!strcmp(argv[argnum],"-float"))
      float_up = TRUE;
    if (!strcmp(argv[argnum],"-noquit"))
      allow_quit = FALSE;
    if (!strcmp(argv[argnum],"-geometry")) 
      geomflags = XParseGeometry(argv[++argnum], &x, &y, &xw, &xh);
    if (!strcmp(argv[argnum],"-display"))
      display_name = argv[++argnum];
    if (!strncmp(argv[argnum],"-F", 2)) 
      teddy = argv[argnum] + 2;
  }
  /* Connect to X server */
  if ( (display = XOpenDisplay(display_name)) == NULL )
    {
      (void) fprintf(stderr, "%s: Cannot connect to X server %s\n",
		     progname, XDisplayName(display_name));
      exit(-1);
    }
  /* Get screen size and depth */
  display_width  = DisplayWidth(display, screen_num);
  display_height = DisplayHeight(display, screen_num);

  if ( !(id=Imlib_init(display)) ) return -1;
  if ( !(file = InitTeddy(teddy)) ) {
    fprintf(stderr, "Can not find any image with name %s.\n", teddy);
    return -1;
  }
  if ( !(im=Imlib_load_image(id, file)) ) return -1;

  /* Set the window position according to user preferences */
  if (geomflags & XNegative)
    x = display_width - im->rgb_width + x;
  if (geomflags & YNegative)
    y = display_height - im->rgb_height + y;
  /* Clip against bounds to stay on the screen */
  if (x<0) x=0;
  if (x > display_width - im->rgb_width) x = display_width - im->rgb_width;
  if (y<0) y=0;
  if (y > display_height - im->rgb_height) y = display_height - im->rgb_height;

  /* Create the main window */
  win = XCreateSimpleWindow(display, DefaultRootWindow(display),
			    x,y,im->rgb_width,im->rgb_height,border_width,
			    BlackPixel(display,screen_num),
			    WhitePixel(display,screen_num));
  XSelectInput(display,win,StructureNotifyMask);
  Imlib_apply_image(id,im,win);

  basewin = win;

  if (use_wm)
    setwinattr.override_redirect = FALSE;
  else
    setwinattr.override_redirect = TRUE;
  cursor = XCreateFontCursor(display, XC_heart);
  setwinattr.cursor = cursor;
  valuemask = CWOverrideRedirect | CWCursor;
  XChangeWindowAttributes(display, win, valuemask, &setwinattr);

  /* Report size hints and other stuff to the window manager */
  size_hints.min_width  = im->rgb_width;    /* Don't allow any resizing */
  size_hints.min_height = im->rgb_height;
  size_hints.max_width  = im->rgb_width;
  size_hints.max_height = im->rgb_height;
  size_hints.flags = PPosition | PSize | PMinSize | PMaxSize;
  if (XStringListToTextProperty(&(teddy), 1, &windowName) == 0)
    {
      (void) fprintf(stderr,
		     "%s: structure allocation for windowName failed.\n",
		     progname);
      Imlib_kill_image(id, im);
      return -1;
    }
  wm_hints.initial_state = NormalState;
  wm_hints.input = TRUE;
  wm_hints.flags = StateHint | IconPixmapHint | InputHint;
  
  class_hints.res_name = progname;
  class_hints.res_class = "Xteddy";
  
  XSetWMProperties(display, win, &windowName, &iconName,
		   argv, argc, &size_hints, &wm_hints, &class_hints);

  /* Select event types wanted */
  inputmask = ExposureMask | KeyPressMask | ButtonPressMask | 
    ButtonReleaseMask | StructureNotifyMask | ButtonMotionMask | 
      PointerMotionHintMask | EnterWindowMask | LeaveWindowMask;
  if (float_up) inputmask |= VisibilityChangeMask;
  XSelectInput(display, win, inputmask);

  /* Display window */
  XMapWindow(display,win);

  /* Get and process the events */
  while (1)
    {
      XNextEvent(display, &report);
      switch(report.type)
	{
	case Expose:
	  if (report.xexpose.count != 0)
	    break;
	  else
	    {
	      /* No drawing needed - the background pixmap */
	      /* is handled automatically by the X server  */
	    }
	  break;
	case ConfigureNotify:
	  /* Window has been resized */
          Imlib_apply_image(id,im,win);
          XSync(display,False);
	  break;
	case ReparentNotify:
	  /* Window was reparented by the window manager */
	  if (!use_wm)
	    (void) fprintf(stderr,
			   "%s: Window manager wouldn't leave the window alone!\n",
			   progname);
	  basewin = report.xreparent.parent;
	  break;
	case EnterNotify:
	  /* Grab the keyboard while the pointer is in the window */
	  XGrabKeyboard(display, win, FALSE, GrabModeAsync, GrabModeAsync,
			CurrentTime);
	  break;
	case LeaveNotify:
	  /* Release the keyboard when the pointer leaves the window */
	  XUngrabKeyboard(display, CurrentTime);
	  break;
	case ButtonPress:
	  /* Raise xteddy above sibling windows  */
	  XRaiseWindow(display, win);
	  /* Remember where the mouse went down */
	  XQueryPointer(display, basewin, &root, &child, &tmp_x, &tmp_y,
			&offs_x, &offs_y, &tmp_mask);
	  break;
	case ButtonRelease:
	  /* Place xteddy at the new position */
	  XQueryPointer(display, basewin, &root, &child, &new_x, &new_y,
		        &tmp_x, &tmp_y, &tmp_mask);
	  winchanges.x = new_x - offs_x;
	  winchanges.y = new_y - offs_y;
	  XReconfigureWMWindow(display, basewin, screen_num,
			       CWX | CWY, &winchanges);
	  break;
	case MotionNotify:
	  /* Move xteddy around with the mouse */
	  while (XCheckMaskEvent(display, ButtonMotionMask, &report));
	  if (!XQueryPointer(display, report.xmotion.window, &root, &child,
			    &new_x, &new_y, &tmp_x, &tmp_y, &tmp_mask))
	    break;
	  winchanges.x = new_x - offs_x;
	  winchanges.y = new_y - offs_y;
	  XReconfigureWMWindow(display, win, screen_num,
			       CWX | CWY, &winchanges);
	  break;
	case VisibilityNotify:
	  /* Put xteddy on top of overlapping windows */
	  if (float_up)
	    if ((report.xvisibility.state == VisibilityFullyObscured)
		|| (report.xvisibility.state == VisibilityPartiallyObscured))
	      XRaiseWindow(display,win);
	  break;
	case KeyPress:
	  /* Exit on "q" or "Q" */
	  charcount = XLookupString(&report.xkey, buffer, bufsize,
				    &keysym, &compose);
	  if((keysym == XK_Q) || (keysym == XK_q))
	    {
              if (allow_quit)
		{
                  Imlib_kill_image(id, im);
		  XCloseDisplay(display);
		  return 0;
		}
	    }
	  break;
	default:
	  /* Throw away all other events */
	  break;
	} /* end switch */
    } /* end while */
}