static Window action_window_str (Display *disp, gchar *class_name, gchar *title_name) {/*{{{*/ Window activate = 0; Window *client_list; unsigned long client_list_size; int i; if ((client_list = get_client_list(disp, &client_list_size)) == NULL) { return EXIT_FAILURE; } for (i = 0; i < client_list_size / sizeof(Window); i++) { gchar *r_class_name = get_window_class(disp, client_list[i]); gchar *r_title_name = get_window_title(disp, client_list[i]); if(strcmp(r_class_name, class_name) == 0 && strcmp(r_title_name, title_name) == 0) { activate = client_list[i]; g_free(r_class_name); g_free(r_title_name); break; } } g_free(client_list); if (activate) { return activate; //action_window(disp, activate); } else { return EXIT_FAILURE; } }/*}}}*/
int is_client_available(int client_id) { int res = -1; client_node_t* client_list = get_client_list(); if(client_list[client_id].id != -1) { res = 1; } return res; }
int check_name_exist(char* name) { int c; client_node_t* client_list = get_client_list(); for(c=1; c<=30; c++) { if(client_list[c].id != -1 && strcmp(client_list[c].name, name) == 0) { return 1; } } return 0; }
void get_mbox_info(int client_id, int* head, int* tail) { client_node_t* client_list = get_client_list(); if(client_list[client_id].id == -1) { *head = -1; *tail = -1; } else { *head = client_list[client_id].mbox_head; *tail = client_list[client_id].mbox_tail; } }
void broad_cast(client_node_t* current_client, char* message) { client_node_t* client_list = get_client_list(); int c; for(c=1; c<=30; c++) { if(client_list[c].id != -1) { mq_send_msg(TELL_MSG, current_client->id, c, message); kill(client_list[c].pid, SIGUSR1); client_wait(); } } }
int insert_to_client_list(client_node_t* new_client_node) { client_node_t* client_list = get_client_list(); int c; for(c=1; c<=30; c++) { if(client_list[c].id == -1) { new_client_node->id = c; client_list[c] = *new_client_node; return c; } } return -1; }
int tell(client_node_t* current_client, char* client_id_str, char* message) { client_node_t* client_list = get_client_list(); int client_id = atoi(client_id_str); if(client_list[client_id].id == -1 || client_id < 1 || client_id > 30) { return -1; } else { char msg_to_send[2048]; sprintf(msg_to_send, "*** %s told you ***: %s\n", current_client->name, message); mq_send_msg(TELL_MSG, current_client->id, client_id, msg_to_send); kill(client_list[client_id].pid, SIGUSR1); client_wait(); } return 0; }
int close_win_by_title(gchar *title) { Window *client_list; unsigned long client_list_size; int count; int i; Display *disp; setlocale(LC_ALL, ""); init_charset();/*lkadd*/ if (! (disp = XOpenDisplay(NULL))) { fputs("Cannot open display.\n", stderr); return 1; }/*打开显示*/ if((client_list = get_client_list(disp, &client_list_size)) == NULL) { return EXIT_FAILURE; } count = client_list_size / sizeof(Window); for (i = 0; i < count; i++) { gchar *title_utf8 = get_window_title(disp, client_list[i]); /* UTF8 */ gchar *title_out = get_output_str(title_utf8, TRUE); if(g_strcasecmp(title, title_out) == 0) { close_window(disp, client_list[i]); break; } g_free(title_utf8); g_free(title_out); } g_free(client_list); XCloseDisplay(disp); if(i >= count) { return EXIT_SUCCESS; } else { #if MY_DEBUG_OUTPUT == 1 g_print("cann't find the window with title:%s \n", title); #endif return 1; } }
Window *get_window_list(Display *disp, int *count) { Window *client_list; unsigned long client_list_size; setlocale(LC_ALL, ""); init_charset();/*lkadd*/ if((client_list = get_client_list(disp, &client_list_size)) == NULL) { return NULL; } if(count != NULL) { *count = client_list_size / sizeof(Window); } return client_list; }
int who(client_node_t* current_client) { int c; client_node_t* client_list = get_client_list(); printf("<ID>\t<nickname>\t<IP/port>\t<indicate me>\n"); for(c=1; c<=30; c++) { client_node_t t_client = client_list[c]; if(t_client.id != -1) { printf("%d\t%s\t%s/%d", c, t_client.name, t_client.ip, t_client.port); if(c == current_client->id) { printf("\t<-me"); } printf("\n"); fflush(stdout); } } return 0; }
void remove_client_node(client_node_t* client) { client_node_t* client_list = get_client_list(); client_list[client->id].id = -1; }
void set_client_name(int client_id, char* name) { client_node_t* client_list = get_client_list(); strcpy(client_list[client_id].name, name); }
void set_mbox_info(int client_id, int head, int tail) { client_node_t* client_list = get_client_list(); client_list[client_id].mbox_head = head; client_list[client_id].mbox_tail = tail; }
int main(int argc, char **argv) { current_only = False; do_check_desktop = False; given_groupnumber = False; autodestroy_on_lost_focus = True; boldface = False; piped = False; double_clutch = True; add_timeout = False; move_window = False; verbose = False; int groupnumber = 0; int loc_x = 0; int loc_y = 0; int timeout_sec; /* scanning the command line : */ int ia; for (ia = 0; ia < argc; ia++) { if ((strcmp(argv[ia],"-c") == 0) || (strcmp(argv[ia],"--current") == 0)) { current_only=True; } else if ((strcmp(argv[ia],"-r") == 0) || (strcmp(argv[ia],"--reverse") == 0)) { reverse_list=True; } else if ((strcmp(argv[ia],"-p") == 0) || (strcmp(argv[ia],"--persist") == 0)) { autodestroy_on_lost_focus = False; } else if (strcmp(argv[ia],"--rich") == 0) { boldface=True; } else if (strcmp(argv[ia],"--pipe") == 0) { piped=True; } else if (strcmp(argv[ia],"--easy") == 0) { double_clutch=False; } else if (sscanf(argv[ia],"--timeout=%d", &timeout_sec) == 1) { add_timeout=True; } else if ((strcmp(argv[ia],"-v") == 0) || (strcmp(argv[ia],"--verbose") == 0)) { verbose = True; } else if (sscanf(argv[ia],"--location=%dx%d", &loc_x, &loc_y) == 2) { move_window=True; } if (strcmp(argv[ia],"-g") == 0) { ia++; if (sscanf(argv[ia],"%d",&groupnumber) == 1) {given_groupnumber = True;} else { printf("\n**** parsing error of -g ***\n"); }; } else if (strncmp(argv[ia],"-g",2) == 0) { if (sscanf(argv[ia],"-g%d",&groupnumber) == 1) {given_groupnumber = True;} } } if ((given_groupnumber) && (verbose)) { printf("\nwmjump invoked with -g %d => assuming we are on workspace No. %d ...\n\n", groupnumber, groupnumber); } lttr = g_strsplit(lttrs, "," , 0); wait_time_short.tv_sec=0; wait_time_short.tv_nsec=TIME_SHORT; wait_time_middle.tv_sec=0; wait_time_middle.tv_nsec=TIME_SHORT; wait_time_long.tv_sec=0; wait_time_long.tv_nsec=TIME_LONG; setlocale(LC_ALL, ""); init_charset(); home = getenv("HOME"); /* Checking if ~/.wmjump exists, and if not creating it: */ gchar *wmjump_dirname = g_strconcat(home,"/.wmjump/",NULL); if (!g_file_test(wmjump_dirname, G_FILE_TEST_EXISTS)) { g_print ("Creating %s directory. \n", wmjump_dirname); if (g_mkdir(wmjump_dirname, 0700)==-1) { fputs("ERROR: Cannot create directory ~/.wmjump \n", stderr); return EXIT_FAILURE; } } /* Checking the pipe: */ if (piped) { remove(g_strconcat(home,"/.wmjump/",PIPEFILE,NULL)); mknod( g_strconcat(home,"/.wmjump/",PIPEFILE,NULL), S_IFIFO|0666, 0); } /* ---------------- MAIN LOOP --------------- */ /* The loop is needed only in piped mode, in normal mode no need to loop */ gboolean not_enough = True; while (not_enough) { if (verbose) printf("wmjump is entering main loop\n"); /* Pipework: */ int pipe_descr[2]; if (pipe(pipe_descr) != 0) {printf("wmjump: --- could not open pipe ---\n"); return EXIT_FAILURE; } if (fork()) { /* parent process */ char next_command[100]; close(pipe_descr[1]); if ( read(pipe_descr[0], next_command, sizeof(next_command)) == -1 ) { printf("wmjump: --- could not read the pipe ---\n"); return EXIT_FAILURE; } Display *disp; if (! (disp = XOpenDisplay(NULL))) { fputs("Cannot open display.\n", stderr); return EXIT_FAILURE; } int child_status; wait(&child_status); do_what_user_said (disp, next_command); XCloseDisplay(disp); not_enough = False ; if (! WIFEXITED(child_status)) { printf("wmjump: error") ;} else { if (piped) {if (WEXITSTATUS(child_status) == 3) {not_enough = True;} }} /* if ((child_status/256 == 3) && piped ) {not_enough = True;} */ } else { /* child process */ close(pipe_descr[0]); int exit_code = 0; if (piped) { FILE *named_pipe; named_pipe = fopen(g_strconcat(home,"/.wmjump/",PIPEFILE,NULL), "r"); gchar go_string[20]; fgets(go_string, 20, named_pipe); if (strcmp(go_string,"all\n") == 0) { current_only=False; exit_code = 3; } else if (strcmp(go_string,"current\n") == 0) { current_only=True; exit_code = 3; } else { /* exiting the program */ printf("wmjump: EXITING\n"); char a[100]; sprintf(a,"do_nothing"); if ( write(pipe_descr[1], a, strlen(a)+1) == -1 ) printf("wmjump: --- could not write to pipe ---\n"); exit(0); } fclose(named_pipe); } if (current_only) { do_check_desktop=True; } Display *disp; if (! (disp = XOpenDisplay(NULL))) { fputs("wmjump: Cannot open display.\n", stderr); return EXIT_FAILURE; } Window *client_list_direct_order; unsigned long client_list_size; if ((client_list_direct_order = get_client_list(disp, &client_list_size)) == NULL) { printf("wmjump: Cannot get client list"); return EXIT_FAILURE; } int szofw=sizeof(Window); Window *client_list = g_malloc(client_list_size*szofw); /* reversing the list if required */ unsigned int j; for (j=0; j<client_list_size/szofw; j++) { if (reverse_list) { client_list[j] = client_list_direct_order[client_list_size/szofw - j - 1]; } else { client_list[j] = client_list_direct_order[j]; } } unsigned int number_of_buttons; int* window_number = malloc(client_list_size / sizeof(Window) * sizeof(int)); gchar** title_of_button; gchar** name_of_style; title_of_button = g_malloc0( ( client_list_size/sizeof(Window) + 1) * sizeof(gchar*) ); name_of_style = g_malloc0( ( client_list_size/sizeof(Window) + 1) * sizeof(gchar*) ); Window win_we_leave; gboolean win_we_leave_is_blacklisted ; get_list_from_wm( disp, client_list, client_list_size, &number_of_buttons, window_number, title_of_button, name_of_style, groupnumber, &win_we_leave, &win_we_leave_is_blacklisted) ; XSync(disp,FALSE); XCloseDisplay(disp); our_user_interface(pipe_descr, client_list, client_list_size, number_of_buttons, title_of_button, name_of_style, window_number, win_we_leave, win_we_leave_is_blacklisted, loc_x, loc_y, timeout_sec); g_free(client_list); client_list = NULL ; g_free(client_list_direct_order); client_list_direct_order = NULL ; free(window_number); window_number = NULL ; g_strfreev(title_of_button); title_of_button = NULL ; g_strfreev(name_of_style); name_of_style = NULL ; exit(exit_code); } } if (verbose) { printf("wmjump exiting main loop\n"); printf("=======================================================\n\n"); } g_strfreev(lttr); lttr = NULL ; exit(0); }
int list_windows (Display *disp) {/*{{{*/ Window *client_list; unsigned long client_list_size; unsigned int i; int max_client_machine_len = 0; if ((client_list = get_client_list(disp, &client_list_size)) == NULL) { return EXIT_FAILURE; } /* find the longest client_machine name */ for (i = 0; i < client_list_size / sizeof(Window); i++) { gchar *client_machine; if ((client_machine = get_property(disp, client_list[i], XA_STRING, "WM_CLIENT_MACHINE", NULL))) { max_client_machine_len = strlen(client_machine); } g_free(client_machine); } /* print the list */ for (i = 0; i < client_list_size / sizeof(Window); i++) { gchar *title_utf8 = get_window_title(disp, client_list[i]); /* UTF8 */ gchar *title_out = get_output_str(title_utf8, TRUE); gchar *client_machine; gchar *class_out = get_window_class(disp, client_list[i]); /* UTF8 */ unsigned long *pid; unsigned long *desktop; int x, y, junkx, junky; unsigned int wwidth, wheight, bw, depth; Window junkroot; /* desktop ID */ if ((desktop = (unsigned long *)get_property(disp, client_list[i], XA_CARDINAL, "_NET_WM_DESKTOP", NULL)) == NULL) { desktop = (unsigned long *)get_property(disp, client_list[i], XA_CARDINAL, "_WIN_WORKSPACE", NULL); } /* client machine */ client_machine = get_property(disp, client_list[i], XA_STRING, "WM_CLIENT_MACHINE", NULL); /* pid */ pid = (unsigned long *)get_property(disp, client_list[i], XA_CARDINAL, "_NET_WM_PID", NULL); /* geometry */ XGetGeometry (disp, client_list[i], &junkroot, &junkx, &junky, &wwidth, &wheight, &bw, &depth); XTranslateCoordinates (disp, client_list[i], junkroot, junkx, junky, &x, &y, &junkroot); /* special desktop ID -1 means "all desktops", so we have to convert the desktop value to signed long */ printf("0x%.8lx %2ld", client_list[i], desktop ? (signed long)*desktop : 0); if (options.show_pid) { printf(" %-6lu", pid ? *pid : 0); } if (options.show_geometry) { printf(" %-4d %-4d %-4d %-4d", x, y, wwidth, wheight); } if (options.show_class) { printf(" %-20s ", class_out ? class_out : "N/A"); } printf(" %*s %s\n", max_client_machine_len, client_machine ? client_machine : "N/A", title_out ? title_out : "N/A" ); g_free(title_utf8); g_free(title_out); g_free(desktop); g_free(client_machine); g_free(class_out); g_free(pid); } g_free(client_list); return EXIT_SUCCESS; }/*}}}*/
int action_window_str (Display *disp, char mode) {/*{{{*/ Window activate = 0; Window *client_list; unsigned long client_list_size; unsigned int i; if (strcmp(SELECT_WINDOW_MAGIC, options.param_window) == 0) { activate = Select_Window(disp); if (activate) { return action_window(disp, activate, mode); } else { return EXIT_FAILURE; } } if (strcmp(ACTIVE_WINDOW_MAGIC, options.param_window) == 0) { activate = get_active_window(disp); if (activate) { return action_window(disp, activate, mode); } else { return EXIT_FAILURE; } } else { if ((client_list = get_client_list(disp, &client_list_size)) == NULL) { return EXIT_FAILURE; } for (i = 0; i < client_list_size / sizeof(Window); i++) { gchar *match_utf8; if (options.show_class) { match_utf8 = get_window_class(disp, client_list[i]); /* UTF8 */ } else { match_utf8 = get_window_title(disp, client_list[i]); /* UTF8 */ } if (match_utf8) { gchar *match; gchar *match_cf; gchar *match_utf8_cf = NULL; if (envir_utf8) { match = g_strdup(options.param_window); match_cf = g_utf8_casefold(options.param_window, -1); } else { if (! (match = g_locale_to_utf8(options.param_window, -1, NULL, NULL, NULL))) { match = g_strdup(options.param_window); } match_cf = g_utf8_casefold(match, -1); } if (!match || !match_cf) { continue; } match_utf8_cf = g_utf8_casefold(match_utf8, -1); if ((options.full_window_title_match && strcmp(match_utf8, match) == 0) || (!options.full_window_title_match && strstr(match_utf8_cf, match_cf))) { activate = client_list[i]; g_free(match); g_free(match_cf); g_free(match_utf8); g_free(match_utf8_cf); break; } g_free(match); g_free(match_cf); g_free(match_utf8); g_free(match_utf8_cf); } } g_free(client_list); if (activate) { return action_window(disp, activate, mode); } else { return EXIT_FAILURE; } } }/*}}}*/
Window* get_window_by_title(Display *disp, gchar *title, int *num) { Window *client_list; unsigned long client_list_size; /*窗口总数*/ int countAll = 0; /*具有相同标题title的窗口数目*/ int count = 0; /*记录具有相同标题的窗口在client_list中的下标*/ int *record; /*具有title标题的窗口*/ Window *windows; int i = 0; setlocale(LC_ALL, ""); init_charset(); if((client_list = get_client_list(disp, &client_list_size)) == NULL) { return NULL; } countAll = client_list_size / sizeof(Window); record = (int*)malloc(sizeof(int) * countAll); for(i = 0; i < countAll; ++i) { record[i] = -1; } for(i = 0; i < countAll; ++i) { gchar *title_utf8 = get_window_title(disp, client_list[i]); /* UTF8 */ #if MY_DEBUG_OUTPUT == 1 g_print("after get_window_title**\n"); #endif gchar *title_out = get_output_str(title_utf8, TRUE); #if MY_DEBUG_OUTPUT == 1 g_print(title_out); g_print("*******title_out is ok??\n"); #endif if(g_strcasecmp(title, title_out) == 0) { record[i] = 1; ++count; } g_free(title_utf8); g_free(title_out); } /*存放相同标题的窗口*/ windows = (Window*)malloc(sizeof(Window)*count); for(i = 0; i < countAll; ++i) { if(record[i] > 0) { windows[i] = client_list[i]; } } *num = count; free(record); return windows; }