source: trunk/third/startup-notification/test/test-launcher.c @ 20837

Revision 20837, 4.3 KB checked in by ghudson, 20 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r20836, which included commits to RCS files with non-trunk default branches.
Line 
1/*
2 * Copyright (C) 2002 Red Hat, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25#include <config.h>
26#include <libsn/sn.h>
27#include <assert.h>
28
29#include "test-boilerplate.h"
30
31static pid_t child_pid = 0;
32
33/* This is a poor way to obtain a timestamp normally (one should be available
34 * to the app from the user clicking on a button or something), but such a
35 * method is not available for this simple test application.
36 */
37Time
38slowly_obtain_timestamp (SnDisplay *display)
39{
40  Window xwindow;
41  Display *xdisplay;
42  XEvent event;
43
44  xdisplay = sn_display_get_x_display (display);
45
46  {
47    XSetWindowAttributes attrs;
48    Atom atom_name;
49    Atom atom_type;
50    char* name;
51
52    attrs.override_redirect = True;
53    attrs.event_mask = PropertyChangeMask | StructureNotifyMask;
54
55    xwindow =
56      XCreateWindow (xdisplay,
57                     RootWindow (xdisplay, 0),
58                     -100, -100, 1, 1,
59                     0,
60                     CopyFromParent,
61                     CopyFromParent,
62                     CopyFromParent,
63                     CWOverrideRedirect | CWEventMask,
64                     &attrs);
65
66    atom_name = XInternAtom (xdisplay, "WM_NAME", TRUE);
67    assert (atom_name != None);
68    atom_type = XInternAtom (xdisplay, "STRING", TRUE);
69    assert (atom_type != None);
70
71    name = "Fake Window";
72    XChangeProperty (xdisplay,
73                     xwindow, atom_name,
74                     atom_type,
75                     8, PropModeReplace, name, strlen (name));
76  }
77 
78  XWindowEvent (xdisplay,
79                xwindow,
80                PropertyChangeMask,
81                &event);
82
83  XDestroyWindow (xdisplay, xwindow);
84
85  return event.xproperty.time;
86}
87
88int
89main (int argc, char **argv)
90{
91  Display *xdisplay;
92  SnDisplay *display;
93  SnLauncherContext *context;
94  Time timestamp;
95
96  if (argc < 2)
97    {
98      fprintf (stderr, "must specify command line to launch\n");
99      exit (1);
100    }
101 
102  xdisplay = XOpenDisplay (NULL);
103  if (xdisplay == NULL)
104    {
105      fprintf (stderr, "Could not open display\n");
106      return 1;
107    }
108
109  if (getenv ("LIBSN_SYNC") != NULL)
110    XSynchronize (xdisplay, True);
111 
112  XSetErrorHandler (x_error_handler);
113 
114  display = sn_display_new (xdisplay,
115                            error_trap_push,
116                            error_trap_pop);
117
118  context = sn_launcher_context_new (display, DefaultScreen (xdisplay));
119
120  sn_launcher_context_set_name (context, "Test Launch");
121  sn_launcher_context_set_description (context, "Launching a test program for libsn");
122  sn_launcher_context_set_binary_name (context, argv[1]);
123
124  timestamp = slowly_obtain_timestamp (display);
125  sn_launcher_context_initiate (context,
126                                "test-launcher",
127                                argv[1],
128                                timestamp);
129
130  switch ((child_pid = fork ()))
131    {
132    case -1:
133      fprintf (stderr, "Fork failed: %s\n", strerror (errno));
134      break;
135    case 0:
136      sn_launcher_context_setup_child_process (context);
137      execv (argv[1], argv + 1);
138      fprintf (stderr, "Failed to exec %s: %s\n", argv[1], strerror (errno));
139      _exit (1);
140      break;
141    }
142 
143  while (TRUE)
144    {
145      XEvent xevent;
146
147      XNextEvent (xdisplay, &xevent);
148
149      sn_display_process_event (display, &xevent);
150    }
151
152  sn_launcher_context_unref (context);
153 
154  return 0;
155}
Note: See TracBrowser for help on using the repository browser.