| 1 | #include <stdlib.h> |
| 2 | #include <stdio.h> |
| 3 | #include <getopt.h> |
| 4 | #include <gtk/gtk.h> |
| 5 | |
| 6 | #define ENV_PREFIX "XSOCK_" |
| 7 | |
| 8 | int return_true(GtkWidget *widget, gpointer data) { return 1; } |
| 9 | |
| 10 | int switch_sockets(GtkWidget *widget, gpointer data) { |
| 11 | fprintf(stderr,"SWITCH: %d\n", (int)data); |
| 12 | return 1; |
| 13 | } |
| 14 | |
| 15 | void print_help(char ** argv) { |
| 16 | printf( |
| 17 | "Usage: %s [args]\n" |
| 18 | "Args:\n" |
| 19 | "\t -c color\n" |
| 20 | "\t -w width\n" |
| 21 | "\t -h height\n" |
| 22 | //"\t -r reuse " ENV_PREFIX "* from environment\n" |
| 23 | "\t -? (display help)" |
| 24 | "\nExample: %s -c black -w 640 -h 480\n\n", |
| 25 | argv[0], argv[0]); |
| 26 | } |
| 27 | |
| 28 | gint main(gint argc, gchar ** argv) |
| 29 | { |
| 30 | gchar *colorstr = "black"; |
| 31 | int width = 640; |
| 32 | int height = 480; |
| 33 | |
| 34 | int opt; |
| 35 | while ((opt = getopt(argc, argv, "?c:w:h:")) != -1) { |
| 36 | switch (opt) { |
| 37 | case 'c': |
| 38 | colorstr = optarg; |
| 39 | break; |
| 40 | case 'w': |
| 41 | width = atoi(optarg); |
| 42 | break; |
| 43 | case 'h': |
| 44 | height = atoi(optarg); |
| 45 | break; |
| 46 | case 'r': //Not yet |
| 47 | if(getenv(ENV_PREFIX"PID") |
| 48 | && getenv(ENV_PREFIX"WID") |
| 49 | && kill(atoi(getenv(ENV_PREFIX"PID")), 0) |
| 50 | ) { |
| 51 | puts("REUSABLE"); |
| 52 | return 0; |
| 53 | } |
| 54 | break; |
| 55 | case '?': |
| 56 | print_help(argv); |
| 57 | return 0; |
| 58 | break; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | //printf("%s\n", colorstr); return 0; |
| 63 | |
| 64 | gtk_init(&argc, &argv); |
| 65 | |
| 66 | //Colors |
| 67 | GdkColor color; |
| 68 | gdk_color_parse(colorstr, &color); |
| 69 | |
| 70 | //Create window |
| 71 | GtkWidget *win = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
| 72 | GtkWidget *sock[1]; |
| 73 | sock[0] = gtk_socket_new(); |
| 74 | g_signal_connect(sock[0], "plug-added", G_CALLBACK(switch_sockets), (gpointer)23); |
| 75 | g_signal_connect(sock[0], "plug-removed", G_CALLBACK(return_true), NULL); |
| 76 | g_signal_connect(win, "delete-event", gtk_main_quit, NULL); |
| 77 | gtk_widget_set_size_request(sock[0], width, height); |
| 78 | gtk_widget_modify_bg(sock[0], GTK_STATE_NORMAL, &color); |
| 79 | gtk_container_add(GTK_CONTAINER(win), sock[0]); |
| 80 | gtk_widget_show_all(win); |
| 81 | |
| 82 | //Print id |
| 83 | GdkNativeWindow wid = gtk_socket_get_id(GTK_SOCKET(sock[0])); |
| 84 | int pid; |
| 85 | if ((pid = fork())) { |
| 86 | printf(ENV_PREFIX"WID=%d\n"ENV_PREFIX"PID=%d\n", wid, pid); |
| 87 | return 0; |
| 88 | } |
| 89 | fclose(stdout); |
| 90 | |
| 91 | //Run |
| 92 | gtk_main(); |
| 93 | return 0; |
| 94 | } |