rendered paste body#include <gtk/gtk.h>#include <string.h>#include "inet.h"#include "common.h"#include "network.h"#define MAXSIZE 512int c;int sockfd;GtkWidget *boardwidget;static GdkPixmap *pixmap = NULL;void update_board(){ for (int i = 0; i < SIZE; i++) for (int j = 0; j < SIZE; j++) if (board[i][j] == 1) { gdk_draw_rectangle(pixmap, boardwidget->style->white_gc, TRUE, j * 50, i * 50, 49, 49); gdk_draw_arc(pixmap, boardwidget->style->black_gc, FALSE, j * 50 + 5, i * 50 + 5, 40, 40, 0, 23040); gtk_widget_queue_draw_area(boardwidget, j * 50, i * 50, 50, 50); } else if (board[i][j] == 2) { gdk_draw_arc(pixmap, boardwidget->style->black_gc, TRUE, j * 50 + 5, i * 50 + 5, 40, 40, 0, 23040); gtk_widget_queue_draw_area(boardwidget, j * 50, i * 50, 50, 50); } else { gdk_draw_rectangle(pixmap, boardwidget->style->white_gc, TRUE, j * 50, i * 50, 49, 49); gtk_widget_queue_draw_area(boardwidget, j * 50, i * 50, 50, 50); }}static gboolean configure_event(GtkWidget *widget, GdkEventConfigure *event){ if (pixmap) g_object_unref(pixmap); pixmap = gdk_pixmap_new(widget->window, widget->allocation.width, widget->allocation.height, -1); gdk_draw_rectangle (pixmap, widget->style->white_gc, TRUE, 0, 0, widget->allocation.width, widget->allocation.height); for (int i = 0; i < 7; i++) { gdk_draw_line(pixmap, widget->style->black_gc, i*50 + 49, 0, i * 50 + 49, 400); gdk_draw_line(pixmap, widget->style->black_gc, 0, i*50 + 49, 400, i * 50 + 49); } return TRUE;}static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event){ gdk_draw_drawable(widget->window, widget->style->fg_gc[GTK_WIDGET_STATE (widget)], pixmap, event->area.x, event->area.y, event->area.x, event->area.y, event->area.width, event->area.height); return FALSE;}void input_callback(gpointer data, gint sockfd, GdkInputCondition condition){ int n; char recvline[MAXSIZE + 1]; /* * Now read a line from the socket and write it to * our standard output. */ n = readline(sockfd, recvline, MAXSIZE); if (n < 0) g_error("str_cli: readline error\n"); recvline[n] = '\0'; /* null terminate */ if (recvline[0] == '1') { for (int i = 0; i < SIZE; i++) { n = readline(sockfd, recvline, MAXSIZE); if (n < 0) g_error("str_cli: readline error\n"); recvline[n] = '\0'; /* null terminate */ for (int j = 0; j < SIZE; j++) { board[i][j] = recvline[j] - '0'; } } /* c = 1 + (2 - c); */ } update_board();}static gboolean board_clicked(GtkWidget *widget, GdkEventButton *event){ int n; char sendline[MAXSIZE + 1]; sprintf(sendline, "1\n%d,%d,%d\n", (int)event->x / 50, (int)event->y / 50, c); g_print("%s", sendline); n = strlen(sendline); if (writen(sockfd, sendline, n) != n) g_error("str_cli: writen error on socket\n"); return TRUE;}static void destroy(GtkWidget *widget, GdkEventButton *event){ gtk_main_quit();}int main(int argc, char **argv){ GtkWidget *window; /* GtkWidget *vbox; GtkWidget *hbox[8]; */ struct sockaddr_in serv_addr; gtk_init (&argc, &argv); if (strstr(argv[1], "-s")) { ; } else { ; } c = argv[2][0] - '0'; /* * Fill in the structure "serv_addr" with the address of the * server that we want to connect with. */ memset((char *) &serv_addr, 0, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = inet_addr(argv[1]); serv_addr.sin_port = htons(SERV_TCP_PORT); /* * Open a TCP socket (an Internet stream socket). */ if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) g_error("client: can't open stream socket"); if (fcntl(sockfd, F_SETFL, FASYNC) == -1) g_error("client: can't ioctl socket"); /* * Connect to the server. */ if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) g_error("client: can't connect to server"); writen(sockfd, "0\n", 2); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); /* vbox = gtk_vbox_new(TRUE, 0); gtk_widget_show(vbox); */ /* This packs the canvas into the window (a gtk container). */ /* gtk_container_add(GTK_CONTAINER(window), vbox); */ /* Here we connect the "destroy" event to a signal handler. * This event occurs when we call gtk_widget_destroy() on the window, * or if we return FALSE in the "delete_event" callback. */ g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(destroy), NULL); /* Sets the border width of the window. */ gtk_container_set_border_width(GTK_CONTAINER(window), 10); boardwidget = gtk_drawing_area_new(); gtk_widget_set_size_request(boardwidget, 400, 400); g_signal_connect (G_OBJECT (boardwidget), "button_release_event", (GtkSignalFunc) board_clicked, NULL); gtk_signal_connect (GTK_OBJECT (boardwidget), "expose_event", (GtkSignalFunc) expose_event, NULL); gtk_signal_connect (GTK_OBJECT(boardwidget),"configure_event", (GtkSignalFunc) configure_event, NULL); gtk_widget_set_events(boardwidget, GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK | GDK_EXPOSURE_MASK | GDK_LEAVE_NOTIFY); gtk_container_add(GTK_CONTAINER (window), boardwidget); gtk_widget_show(boardwidget); /* for (int i = 0; i < 8; i++) { hbox[i] = gtk_hbox_new(TRUE, 0); gtk_widget_show(hbox[i]); gtk_box_pack_start(GTK_BOX(vbox), hbox[i], TRUE, TRUE, 0); for (int j = 0; j < 8; j++) { button[i][j] = gtk_button_new_with_label("X"); g_signal_connect(G_OBJECT(button[i][j]), "clicked", G_CALLBACK(button_clicked), GINT_TO_POINTER(i*8+j));*/ /* The final step is to display this newly created widget. */ /* gtk_widget_show(button[i][j]); gtk_box_pack_start(GTK_BOX(hbox[i]), button[i][j], TRUE, TRUE, 0); } } */ /* and the window */ gtk_widget_show(window); gdk_input_add(sockfd, GDK_INPUT_READ, input_callback, NULL); gtk_main(); close(sockfd); return 0;}