/* From sch57.mcn.msk.su!karlson@pulsar.ac.msk.su Wed May 24 12:18:55 1995 Return-Path: sch57.mcn.msk.su!karlson@pulsar.ac.msk.su To: moshkow.ac.msk.su!moshkow@pulsar.ac.msk.su References: <199505191819.VAA00558@moshkow.ac.msk.su> Organization: sch57 From: Nikita Kozhekin (Kitya Karlson)"[SHAMIL']" Date: Wed, 24 May 95 10:05:22 +0400 (MSD) X-Mailer: dMail (Demos Mail v1.11a) Subject: Re: About Monitor 2.95 Lines: 414 âÎÒ ß ÈÕ È ÍÀØÅË. òÎËÜÊÎ âÛ ÐÓÑÑÊÈÅ ÁÓÊÂÛ ÈÇ ÊÎÌÌÅÍÒÀÐÈÅ ÓÁÅÐÈÒÅ. àËÜÒÅÐÍÀÒÈÂÍÀß ÊÎÄÈÐÎÂÊÀ ÑÈËÜÍÎ Linux ÍÅÐÂÈÐÓÅÒ ÎÄÍÀÊÎ ;-( åÑËÈ ÂÛ ÏÎËÓ×ÀÅÒÅ ÏÎ×ÒÓ Â êîè-8, ÒÎ ÝÒÎ ÍÅÎÁßÇÀÒÅËÜÍÎ. åÑËÈ ÂÛ ÏÎËÓ×ÈÒÅ ÏÎ×ÒÓ Â MS-DOS, ÒÎ ÍÅ ÇÀÁÓÄÜÒÅ ÇÀÌÅÍÈÒÜ CR LF ÍÀ CR. (ïÐÎÑÒÅÉØÈÉ ÑÏÎÑÎÁ -- ÇÀÃÐÓÇÈÒÜ Â jed, ÂÑÒÀÂÈÒÜ È ÓÄÀËÈÒÜ ÏÐÎÁÅË È ÂÛÉÒÈ). í. êÎÆÅÊÈÍ íÀ×ÀËÀ ÏÐÎÃÐÀÌÌÈÐÎÂÀÍÈß Â X-Window ëÈÑÒÈÍà 1. MESSAGE.H */ /* âÊËÞ×ÀÅÌÛÉ ÌÎÄÓËÜ: message.h */ #include #include #include /* ðÀÁÎÒÀ Ñ ÑÅÌÀÔÎÐÀÌÈ */ #include /* ðÀÁÎÒÀ Ñ ÐÀÇÄÅËßÅÌÎÉ ÏÀÌßÒÜÞ */ /* ðÀÁÎÒÀ  X-Window */ #include #include #include #include #include #include #include #define SEM_ID 2001 /* êËÞ× ÌÀÑÑÈÂÀ ÑÅÌÀÔÎÐΠ*/ #define SHM_ID 2002 /* êËÞ× ÐÀÇÄÅËßÅÌÎÉ ÏÀÌßÒÈ */ #define PERMIS 0666 /* ïÐÀÂÀ ÄÎÑÒÓÏÀ: ÂÑÅ ÌÎÃÓÒ ×ÈÒÀÒÜ È ÏÈÑÀÒÜ*/ #define MSG_TYPE_EMPTY 0 /* òÈÏÛ ÑÎÎÁÙÅÍÈÉ: */ #define MSG_TYPE_STRING 1 /* ÏÓÑÒÎÅ, ÑÒÐÎÊÀ, */ #define MSG_TYPE_FINISH 2 /* ÊÎÍÅÖ ÄÈÀËÎÃÀ */ #define MAX_STRING 20 /* ìÀÊÑÈÌÀËÜÍÀß ÄËÈÍÀ ÑÎÎÁÙÅÍÈß */ #define WND_WDT 200 /* øÈÐÈÍÀ ÎÊÍÀ */ #define WND_HGH 100 /* âÛÑÎÒÀ ÎÊÍÀ */ #define WND_MIN_WDT 50 /* ìÈÍÈÌÀËÜÍÀß ØÈÐÈÍÀ ÎÊÍÀ */ #define WND_MIN_HGH 50 /* ìÈÍÈÌÀËÜÍÀß ÂÛÑÎÒÀ ÎÊÍÀ */ #define WND_BORDER_WDT 5 /* øÈÐÈÍÀ ÁÎÐÄÞÐÀ */ struct msg_t { int type; char string[MAX_STRING]; }; /* ñÒÐÓÊÒÓÐÀ ÑÎÎÁÙÅÍÈß */ inline void sys_err( char * msg ) { puts( msg ); exit( 1 ); } /* ñÈÑÒÅÌÍÀß ÎØÈÁÊÀ */ /* ðÀÁÎÒÀ ÑÎ ÑÒÐÓÊÒÓÐÎÉ semun */ inline union semun semun (int i) { union semun s; s.val = i; return s; } inline union semun semun (struct semid_ds * b) { union semun s; s.buf = b; return s; } /* èÍÈÖÈÀËÈÇÀÖÈß ÎÊÍÀ ÍÀÏÈÑÀÍÀ  window.c */ void SetWindowManagerHints ( Display * prDisplay, char * psPrgClass, char * argv[], int argc, Window nWnd, int x , int y, int nWidth, int nHeight, int nMinWidth, int nMinHeight, char * psTitle, char * psIconTitle, Pixmap nIconPixmap); ëÈÑÒÈÍà 2. WINDOW.C /* èÍÈÖÈÀËÈÇÀÖÈß ÎÊÍÀ */ #include "message.h" void SetWindowManagerHints ( Display * prDisplay, char * psPrgClass, char * argv[], int argc, Window nWnd, int x, int y, int nWidth, int nHeight, int nMinWidth, int nMinHeight, char * psTitle, char * psIconTitle, Pixmap nIconPixmap) { XSizeHints rSizeHints; #ifdef X11R3 /* X11R3 È ÍÈÆÅ */ rSizeHints.flags = PPosition | PSize | PMinSize; rSizeHints.x = x; rSizeHints.y = y; rSizeHints.width = nWidth; rSizeHints.height = nHeight; rSizeHints.min_width = nMinWidth; rSizeHints.min_height = nMinHeight; XSetStandardProperties ( prDisplay, nWnd, psTitle, psIconTitle, nIconPixmap, argv, argc, &rSizeHints ); #else /* X11R4 È ÂÛØÅ */ XWMHints rWMHints; XClassHint rClassHint; XTextProperty prWindowName, prIconName; if ( !XStringListToTextProperty(&psTitle, 1, &prWindowName) || !XStringListToTextProperty( &psIconTitle, 1, &prIconName)) { sys_err("No memory!"); } rSizeHints.flags = PPosition | PSize | PMinSize; rSizeHints.min_width = nMinWidth; rSizeHints.min_height = nMinHeight; rWMHints.flags = StateHint | IconPixmapHint | InputHint; rWMHints.initial_state = NormalState; rWMHints.input = True; rWMHints.icon_pixmap = nIconPixmap; rClassHint.res_name = argv[0]; rClassHint.res_class = psPrgClass; XSetWMProperties ( prDisplay, nWnd, &prWindowName, &prIconName, argv, argc, &rSizeHints, &rWMHints, &rClassHint); #endif } ëÈÑÒÈÍà 3. CLIENT.C /* ëÈÑÒÈÍà ÏÐÎÃÐÀÌÌÛ ÊËÈÅÍÒÀ: client.c êÎÌÏÈËßÖÈß : g++ window.c client.c -o client -lX11 & */ #include "message.h" #define WND_X 0 /* êÎÎÐÄÈÍÀÒÛ ÎÊÍÀ */ #define WND_Y 100 #define WND_TITLE "Client Window" /* çÀÃÎËÎÂÎÊ ÎÊÍÀ */ #define WND_ICON_TITLE "Client Icon" /* çÀÃÎËÎÂÎÊ ÏÈÊÒÎÃÐÀÌÌÛ */ #define PRG_CLASS "Client" int main(int argc, char * argv[]) /* îÑÍÎÂÍÀß ÏÐÎÖÅÄÓÐÀ */ { Display *prDisplay; Window nWnd; int semid,shmid,x,y,nScreenNum; GC prGC; XEvent rEvent; msg_t *msg_p; char s[MAX_STRING]; KeySym nKeySym,naModList[2]; if (( semid = semget(SEM_ID,1, 0)) < 0 ) sys_err("CLIENT: can't get semaphore!"); /* ÷ÒÅÍÈÅ ÌÀÑÑÈÂÀ ÑÅÌÀÔÎÐΠ*/ if (( shmid = shmget(SHM_ID,sizeof(msg_t),0))<0) sys_err("CLIENT: can't get shared memory segment!"); /* äÎÑÒÓÏ Ê ÑÅÃÌÅÍÒÓ ÐÀÇÄÅËßÅÌÎÉ ÏÀÌßÒÈ */ if ((msg_p = (msg_t * ) shmat(shmid, 0, 0)) == NULL) sys_err("CLIENT: shared memory attach error!"); /* ïÎËÓ×ÅÍÈÅ ÀÄÐÅÑÀ */ if ((prDisplay = XOpenDisplay(NULL))==NULL) sys_err("CLIENT: can't connect to the X server!"); /* ïÎÄÊËÞ×ÅÍÈÅ Ê X-ÑÅÐÂÅÐÓ */ /* ðÅÃÈÑÒÐÀÖÈß ÎÊÍÀ */ nScreenNum = DefaultScreen(prDisplay); nWnd = XCreateSimpleWindow(prDisplay, RootWindow(prDisplay,nScreenNum), WND_X, WND_Y, WND_WDT, WND_HGH, WND_BORDER_WDT, BlackPixel(prDisplay, nScreenNum), WhitePixel(prDisplay,nScreenNum)); SetWindowManagerHints(prDisplay, PRG_CLASS, argv, argc, nWnd, WND_X, WND_Y, WND_WDT, WND_HGH, WND_MIN_WDT, WND_MIN_HGH, WND_TITLE, WND_ICON_TITLE, 0); /* âÛÁÎÐ ×ÒÅÍÈß ÑÎÁÛÒÈÉ */ XSelectInput(prDisplay, nWnd , ExposureMask | KeyPressMask); /* ïÎÊÀÇ ÎÊÍÀ */ XMapWindow(prDisplay, nWnd); /* óÑÒÀÍÎÂÊÀ ÍÎÂÎÉ ÊÍÎÏÊÈ ÊËÀÂÈÀÒÓÐÛ */ naModList[0] = XK_Control_L; naModList[1] = XK_Shift_L; XRebindKeysym( prDisplay, XK_A, naModList, 2, "EXIT", strlen("EXIT")); x = 0; y = 0; /* ñÎÇÄÀÍÈÅ ÃÐÀÔÈ×ÅÑÊÎÃÎ ÊÎÍÒÅÊÑÒÀ */ prGC = XCreateGC(prDisplay, nWnd, 0, NULL); /* ãËÀÂÍÛÉ ÖÈÊË: ×ÒÅÍÈÅ ÑÎÁÛÒÈÉ */ for (;;) { /* âÛÁÎÐ ÑÎÁÛÒÈß ÈÇ Î×ÅÐÅÄÈ */ XNextEvent(prDisplay, &rEvent); switch (rEvent.type) { case Expose : /* çÀÏÐÎÑ ÍÀ ÏÅÐÅÐÈÑÎÂÊÓ */ if (rEvent.xexpose.count!=0) break; XSetForeground(prDisplay, prGC, BlackPixel(prDisplay, 0)); /* âÛÂÎÄ ÑÒÐÎÊÈ */ XDrawString(prDisplay, nWnd, prGC, 10, 50, "Client,", strlen("Client,")); break; case KeyPress : /* íÀÆÀÒÈÅ ÊËÀÂÈØÈ */ memset(s, 0, sizeof(s)); XSetForeground(prDisplay, prGC, BlackPixel(prDisplay, 0)); XDrawString(prDisplay, nWnd, prGC, 10, 66, "TALK:", strlen("TALK:")); /* ÷ÒÅÍÈÅ ÑÒÐÎÊÈ-ÑÎÎÒÂÅÒÑÒÂÈß ÍÀÆÀÒÎÉ ÊËÀÂÈØÈ */ XLookupString(&rEvent.xkey, s, sizeof(s), &nKeySym, NULL); /* æÄÅÌ-Ñ ... */ while ( semctl (semid, 0, GETVAL, semun(0) ) || msg_p->type != MSG_TYPE_EMPTY); /* áËÎÊÈÐÎÂÊÀ */ semctl(semid, 0, GETVAL, semun(1)); strncpy(msg_p->string,s,MAX_STRING); /* ýÕÎ-ÎÒÎÁÐÀÆÅÍÈÅ */ XDrawString(prDisplay, nWnd, prGC, 10+x, 80 + y, s, strlen(s)); if ((10+x) > WND_WDT) {x =0; y=y+10;} else x = x + 10; if (strcmp(s,"EXIT")) msg_p->type= MSG_TYPE_STRING; else msg_p->type = MSG_TYPE_FINISH; /* ñÍßÒÈÅ ÁËÎÊÈÐÎÂÊÈ */ semctl(semid, 0, SETVAL, semun(0)); if (!strcmp(s,"EXIT")) { /* êÎÍÅÖ ÐÀÁÎÒÛ */ shmdt((char*)msg_p); XFreeGC(prDisplay,prGC); XCloseDisplay(prDisplay); exit(0); } break; } } /* îÑÂÎÁÎÆÄÅÍÈÅ ÃÐÀÔÈ×ÅÑÊÎÃÎ ÊÎÍÒÅÊÑÒÀ */ XFreeGC(prDisplay,prGC); XCloseDisplay(prDisplay); exit(0); } ëÈÑÒÈÍà 4. SERVER.C /* ëÈÑÒÈÍà ÏÐÎÃÐÀÌÌÛ ÑÅÐÂÅÐÀ: server.c êÎÌÏÈËßÖÈß : g++ window.c server.c -o server -lX11 & */ #include "message.h" #define WND_X 300 /* êÎÎÐÄÈÍÀÒÛ */ #define WND_Y 100 #define WND_TITLE "Server Window" /* çÀÃÎËÎÂÊÈ */ #define WND_ICON_TITLE "Server Icon" #define PRG_CLASS "Server" int main(int argc, char * argv[]) { Display *prDisplay; Window nWnd; int semid,shmid,x,y,nScreenNum; GC prGC; XEvent rEvent; msg_t *msg_p; char s[MAX_STRING]; /* ñÎÇÄÀÍÈÅ ÑÅÌÀÔÎÐΠ*/ if ((semid = semget(SEM_ID,1, PERMIS | IPC_CREAT)) < 0 ) sys_err("SERVER: can't create semaphore!"); /* ñÎÇÄÀÍÈÅ ÐÀÇÄÅËßÅÌÎÃÎ ÑÅÃÌÅÍÒÀ */ if ((shmid = shmget(SHM_ID,sizeof(msg_t), PERMIS | IPC_CREAT))<0) sys_err("SERVER: can't create shared memory segment!"); if ((msg_p = (msg_t * ) shmat(shmid, 0, 0)) == NULL) sys_err("SERVER: shared memory attach error!"); semctl( semid, 0 , SETVAL, semun(0)); msg_p->type = MSG_TYPE_EMPTY; /* ïÎÄÊËÞ×ÅÍÈÅ Ê X-ÑÅÐÂÅÐÓ */ if ((prDisplay = XOpenDisplay(NULL))==NULL) sys_err("SERVER: can't connect to the X server!"); /* èÍÈÖÈÀËÈÇÀÖÈß ÎÊÍÀ È ÝÊÐÀÍÀ */ nScreenNum = DefaultScreen(prDisplay); nWnd = XCreateSimpleWindow(prDisplay, RootWindow(prDisplay, nScreenNum), WND_X, WND_Y, WND_WDT, WND_HGH, WND_BORDER_WDT, BlackPixel(prDisplay, nScreenNum), WhitePixel(prDisplay, nScreenNum)); SetWindowManagerHints(prDisplay, PRG_CLASS, argv, argc, nWnd, WND_X, WND_Y, WND_WDT, WND_HGH, WND_MIN_WDT, WND_MIN_HGH, WND_TITLE, WND_ICON_TITLE, 0); /* âÛÁÎÐ ÑÎÁÛÒÈÉ: ÏÅÐÅÐÈÑÎÂÊÀ, ÊËÀÂÈÀÒÓÐÀ */ XSelectInput(prDisplay, nWnd, ExposureMask | KeyPressMask); /* âÛÂÎÄ ÎÊÍÀ */ XMapWindow(prDisplay, nWnd); x = 0; y = 0; /* ñÎÇÄÀÍÈÅ ÃÐÀÔÈ×ÅÑÊÎÃÎ ÊÎÍÒÅÊÑÒÀ */ prGC = XCreateGC(prDisplay, nWnd, 0 ,NULL); for (;;) { if (msg_p->type != MSG_TYPE_EMPTY) { /* áËÎÊÈÐÎÂÀÍÎ - ÆÄÅÌ-Ñ ... */ if (semctl(semid, 0, GETVAL, semun(0))) continue; /* áËÎÊÈÐÎÂÊÀ */ semctl(semid, 0, SETVAL, semun(1)); if (msg_p->type == MSG_TYPE_STRING) { XSetForeground(prDisplay, prGC, BlackPixel(prDisplay, 0)); /* âÛÂÎÄ ÏÐÎ×ÈÒÀÍÍÎÉ ÑÒÐÎÊÈ */ XDrawString(prDisplay, nWnd, prGC, 10 + x, 78 + y, msg_p->string, strlen(msg_p->string)); if ((10+x) > WND_WDT) {x =0; y=y+10;} else x = x + 10; } if (msg_p->type == MSG_TYPE_FINISH) break; msg_p->type = MSG_TYPE_EMPTY; /* îÒÌÅÍÀ ÁËÎÊÈÐÎÂÊÈ */ semctl(semid, 0, SETVAL, semun(0)); } /* âÛÂÎÄ ÏÐÈÃËÀØÅÍÈß ÄËß ÒÅÕ, ÊÒÎ ÏÎÍßË */ XSetForeground(prDisplay, prGC, BlackPixel(prDisplay, 0)); XDrawString(prDisplay, nWnd, prGC, 10, 50, "Server,",strlen("Server,")); XDrawString(prDisplay, nWnd, prGC, 20, 66, "LISTEN:",strlen("LISTEN:")); } /* îÑÂÎÁÎÆÄÅÍÈÅ ÃÐÀÔÈ×ÅÑÊÎÃÎ ÊÎÍÒÅÊÑÒÀ */ XFreeGC(prDisplay, prGC); /* çÀÊÐÛÒÈÅ ÄÈÑÏËÅß */ XCloseDisplay(prDisplay); if (semctl(semid, 0, IPC_RMID, semun(((struct semid_ds *) 0)) )<0) sys_err("SERVER: semaphore remove error!"); /* óÄÀËÅÍÈÅ ÌÀÑÑÈÂÀ ÑÅÌÀÔÎÐΠ*/ shmdt((char *)msg_p); /* óÄÀËÅÍÈÅ ÑÅÃÌÅÍÒÀ ÐÀÇÄÅËßÅÌÎÉ ÏÀÌßÒÈ */ if ( shmctl( shmid, IPC_RMID, (struct shmid_ds *) 0)<0) sys_err("SERVER: shared memory remove error!"); /* âÛÕÎÄ */ exit(0); } /* -- ----------------------------------------------------------- NIKITA KOZHEKIN (KITYA KARLSON) [SHAMIL] \ Microsoft is not the answer. e-mail : KARLSON@SCH57.MCN.MSK.SU\ Microsoft is the question. phone : (095) 373-0554 \ Linux is the answer. ----------------------------------------------------------- Russia, 111395, Moscow, Snayperskaya str., h.4, app. 182. | Letters are welcome from 13 to 12. /--\/---\/\/-| | E-mail welcome around the clock. | * <------/|----| ------------------------------------ \ / \/ K K K K /~~ | |_/\___/ KK KK \--\ |=| K K K K __/ | | */