Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  1 
  2 /* shameless stolen from netscape :-) */
  3 
  4 #include <stdio.h>
  5 #include <stdlib.h>
  6 #include <unistd.h>
  7 #include <string.h>
  8 
  9 #include <X11/Xlib.h>
 10 #include <X11/Xatom.h>
 11 #include <X11/Xmu/WinUtil.h>    /* for XmuClientWindow() */
 12 
 13 unsigned int debug=0;
 14 
 15 static int
 16 x11_error_dev_null(Display * dpy, XErrorEvent * event)
 17 {
 18     fprintf(stderr,"x11-error\n");
 19     return 0;
 20 }
 21 
 22 static Window
 23 find_window(Display * dpy, Atom atom)
 24 {
 25     int             n;
 26     unsigned int    i;
 27     Window          root = RootWindowOfScreen(DefaultScreenOfDisplay(dpy));
 28     Window          root2, parent, *kids;
 29     unsigned int    nkids;
 30     Window          result = 0;
 31 
 32     if (!XQueryTree(dpy, root, &root2, &parent, &kids, &nkids)) {
 33         fprintf(stderr, "XQueryTree failed on display %s\n",
 34                 DisplayString(dpy));
 35         exit(2);
 36     }
 37 
 38     if (!(kids && nkids)) {
 39         fprintf(stderr, "root window has no children on display %s\n",
 40                 DisplayString(dpy));
 41         exit(2);
 42     }
 43     for (n = nkids - 1; n >= 0; n--) {
 44         Atom            type;
 45         int             format;
 46         unsigned long   nitems, bytesafter;
 47         unsigned char  *args = NULL;
 48 
 49         Window          w = XmuClientWindow(dpy, kids[n]);
 50 
 51         XGetWindowProperty(dpy, w, atom,
 52                            0, (65536 / sizeof(long)),
 53                            False, XA_STRING,
 54                            &type, &format, &nitems, &bytesafter,
 55                            &args);
 56 
 57         if (!args)
 58             continue;
 59         if (debug) {
 60             printf("query 0x%08lx: ",w);
 61             for (i = 0; i < nitems; i += strlen(args + i) + 1)
 62                 printf("%s ", args + i);
 63             printf("\n");
 64         }
 65         XFree(args);
 66 
 67         result = w;
 68 #if 0 /* there might be more than window */
 69         break;
 70 #endif
 71     }
 72     return result;
 73 }
 74 
 75 static void
 76 pass_cmd(Display *dpy, Atom atom, Window win, int argc, char **argv)
 77 {
 78     int             i, len;
 79     char           *pass;
 80 
 81     if (debug)
 82         printf("ctrl  0x%08lx: ",win);
 83     for (len = 0, i = 0; i < argc; i++) {
 84         if (debug)
 85             printf("%s ",argv[i]);
 86         len += strlen(argv[i]) + 1;
 87     }
 88     if (debug)
 89         printf("\n");
 90     pass = malloc(len);
 91     pass[0] = 0;
 92     for (len = 0, i = 0; i < argc; i++)
 93         strcpy(pass + len, argv[i]),
 94             len += strlen(argv[i]) + 1;
 95     XChangeProperty(dpy, win,
 96                     atom, XA_STRING,
 97                     8, PropModeReplace,
 98                     pass, len);
 99     free(pass);
100 }
101 
102 static void
103 usage(char *argv0)
104 {
105     char *prog;
106 
107     if (NULL != (prog = strrchr(argv0,'/')))
108         prog++;
109     else
110         prog = argv0;
111 
112     fprintf(stderr,
113 "This is a \"remote control\" for xawtv\n"
114 "usage: %s [ options ] [ command ]\n"
115 "\n"
116 "available options:\n"
117 "    -d display\n"
118 "        set X11 display\n"
119 "    -i window ID\n"
120 "        select xawtv window\n"
121 "    -v n\n"
122 "        Set debug level to n, default 0."
123 "\n"
124 "available commands (most frequently used ones):\n"
125 "    setstation <name> | <nr> | next | prev\n"
126 "    setchannel <name> | next | prev\n"
127 "    capture on | off\n"
128 "    volume mute | dec | inc | <number>\n"
129 "    snap [ <format> [ <size> [ <filename> ]]]\n"
130 "\n"
131 "Check the man-page for a full list and detailed descriptions.\n"
132 "\n"
133             ,prog);
134 }
135 
136 int
137 main(int argc, char *argv[])
138 {
139     Display  *dpy;
140     char     *dpyname = NULL;
141     Window   win,id = 0;
142     Atom     station,remote;
143     int      c;
144 
145     for (;;) {
146         c = getopt(argc, argv, "hd:i:v:");
147         if (c == -1)
148             break;
149         switch (c) {
150         case 'd':
151             dpyname = optarg;
152             break;
153         case 'i':
154             sscanf(optarg,"%li",&id);
155             break;
156         case 'v':
157             debug = atoi(optarg);
158             break;
159         case 'h':
160         default:
161             usage(argv[0]);
162             exit(1);
163         }
164     }
165 
166     if (NULL == (dpy = XOpenDisplay(dpyname))) {
167         fprintf(stderr,"can't open display %s\n", dpyname?dpyname:"");
168         exit(1);
169     }
170     XSetErrorHandler(x11_error_dev_null);
171     station = XInternAtom(dpy, "_XAWTV_STATION", False);
172     remote =  XInternAtom(dpy, "_XAWTV_REMOTE",  False);
173     
174     if (0 == (win = find_window(dpy,station)) &&
175         0 == id) {
176         fprintf(stderr,"xawtv not running\n");
177         exit(2);
178     }
179     if (argc > optind)
180         pass_cmd(dpy, remote, (id != 0) ? id : win, argc-optind, argv+optind);
181 
182     XCloseDisplay(dpy);
183     return 0;
184 }
185 
  This page was automatically generated by the LXR engine.